diff --git a/src/libckpool.c b/src/libckpool.c index 7572bad6..b958da6e 100644 --- a/src/libckpool.c +++ b/src/libckpool.c @@ -695,6 +695,65 @@ bool send_unix_msg(int sockd, const char *buf) return true; } +/* Send a single message to a process instance when there will be no response, + * closing the socket immediately. */ +bool send_proc(proc_instance_t *pi, const char *msg) +{ + char *path = pi->us.path; + bool ret = false; + int sockd; + + if (unlikely(!path || !strlen(path))) { + LOGERR("Attempted to send message to null path in send_proc"); + goto out; + } + if (unlikely(!msg || !strlen(msg))) { + LOGERR("Attempted to send null message to socket %s in send_proc", path); + goto out; + } + sockd = open_unix_client(path); + if (unlikely(sockd < 0)) { + LOGWARNING("Failed to open socket %s", path); + goto out; + } + if (unlikely(!send_unix_msg(sockd, msg))) + LOGWARNING("Failed to send %s to socket %s", msg, path); + else + ret = true; + close(sockd); +out: + return ret; +} + +/* Send a single message to a process instance and retrieve the response, then + * close the socket. */ +char *send_recv_proc(proc_instance_t *pi, const char *msg) +{ + char *path = pi->us.path, *buf = NULL; + int sockd; + + if (unlikely(!path || !strlen(path))) { + LOGERR("Attempted to send message to null path in send_proc"); + goto out; + } + if (unlikely(!msg || !strlen(msg))) { + LOGERR("Attempted to send null message to socket %s in send_proc", path); + goto out; + } + sockd = open_unix_client(path); + if (unlikely(sockd < 0)) { + LOGWARNING("Failed to open socket %s", path); + goto out; + } + if (unlikely(!send_unix_msg(sockd, msg))) + LOGWARNING("Failed to send %s to socket %s", msg, path); + else + buf = recv_unix_msg(sockd); + close(sockd); +out: + return buf; +} + json_t *json_rpc_call(connsock_t *cs, const char *rpc_req) { diff --git a/src/libckpool.h b/src/libckpool.h index 21595edd..10a4f21f 100644 --- a/src/libckpool.h +++ b/src/libckpool.h @@ -226,6 +226,8 @@ int open_unix_server(const char *server_path); int open_unix_client(const char *server_path); char *recv_unix_msg(int sockd); bool send_unix_msg(int sockd, const char *buf); +bool send_proc(proc_instance_t *pi, const char *msg); +char *send_recv_proc(proc_instance_t *pi, const char *msg); json_t *json_rpc_call(connsock_t *cs, const char *rpc_req);