From ca805e44f05173d75f29eafb0907f97938b78b46 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Tue, 10 Jun 2014 22:56:57 +1000 Subject: [PATCH] Provide a getfd function for getting the main connector file descriptor for seamless restarts --- src/ckpool.c | 13 ++++++++++++- src/connector.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/ckpool.c b/src/ckpool.c index 41772d2b..7898037a 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -79,6 +79,7 @@ static void *listener(void *arg) { proc_instance_t *pi = (proc_instance_t *)arg; unixsock_t *us = &pi->us; + ckpool_t *ckp = pi->ckp; char *buf = NULL; int sockd; @@ -90,7 +91,7 @@ retry: LOGERR("Failed to accept on socket in listener"); goto out; } - /* Insert parsing and repeat code here */ + buf = recv_unix_msg(sockd); if (!buf) { LOGWARNING("Failed to get message in listener"); @@ -101,6 +102,16 @@ retry: } else if (!strncasecmp(buf, "ping", 4)) { LOGDEBUG("Listener received ping request"); send_unix_msg(sockd, "pong"); + } else if (!strncasecmp(buf, "getfd", 5)) { + char *msg; + + msg = send_recv_proc(ckp->connector, "getfd"); + if (!msg) + LOGWARNING("Failed to receive fd data from connector"); + else { + send_unix_data(sockd, msg, sizeof(struct msghdr)); + free(msg); + } } else { LOGINFO("Listener received unhandled message: %s", buf); send_unix_msg(sockd, "unknown"); diff --git a/src/connector.c b/src/connector.c index afb3b9f9..b21205e8 100644 --- a/src/connector.c +++ b/src/connector.c @@ -430,6 +430,29 @@ static client_instance_t *client_by_id(conn_instance_t *ci, int id) return client; } +static void send_fd(int fd, int sockd) +{ + struct cmsghdr cmptr; + struct iovec iov[1]; + struct msghdr msg; + char buf[2]; + + memset(&cmptr, 0, sizeof(struct cmsghdr)); + iov[0].iov_base = buf; + iov[0].iov_len = 2; + msg.msg_iov = iov; + msg.msg_iovlen = 1; + msg.msg_name = NULL; + msg.msg_namelen = 0; + cmptr.cmsg_level = SOL_SOCKET; + cmptr.cmsg_type = SCM_RIGHTS; + cmptr.cmsg_len = CMSG_LEN(sizeof(int)); + *(int *)CMSG_DATA(&cmptr) = fd; + buf[1] = 0; + buf[0] = 0; + send_unix_data(sockd, &msg, sizeof(struct msghdr)); +} + static int connector_loop(proc_instance_t *pi, conn_instance_t *ci) { int sockd, client_id, ret = 0, selret; @@ -488,6 +511,10 @@ retry: LOGINFO("Connector dropped client id: %d", client_id); goto retry; } + if (!strncasecmp(buf, "getfd", 5)) { + send_fd(ci->serverfd, sockd); + goto retry; + } /* Anything else should be a json message to send to a client */ json_msg = json_loads(buf, 0, NULL); @@ -504,6 +531,7 @@ retry: realloc_strcat(&buf, "\n"); send_client(ci, client_id, buf); json_decref(json_msg); + buf = NULL; goto retry; out: