Browse Source

Hand over multiple sockets if we can

master
Con Kolivas 10 years ago
parent
commit
00f9cf297d
  1. 43
      src/ckpool.c
  2. 4
      src/ckpool.h
  3. 17
      src/connector.c

43
src/ckpool.c

@ -312,8 +312,8 @@ retry:
broadcast_proc(ckp, buf); broadcast_proc(ckp, buf);
send_unix_msg(sockd, "success"); send_unix_msg(sockd, "success");
} }
} else if (cmdmatch(buf, "getfd")) { } else if (cmdmatch(buf, "getxfd")) {
int connfd = send_procmsg(ckp->connector, "getfd"); int connfd = send_procmsg(ckp->connector, buf);
if (connfd > 0) { if (connfd > 0) {
int newfd = get_fd(connfd); int newfd = get_fd(connfd);
@ -1221,16 +1221,21 @@ static struct option long_options[] = {
}; };
#endif #endif
static void send_recv_path(const char *path, const char *msg) static bool send_recv_path(const char *path, const char *msg)
{ {
int sockd = open_unix_client(path); int sockd = open_unix_client(path);
bool ret = false;
char *response; char *response;
send_unix_msg(sockd, msg); send_unix_msg(sockd, msg);
response = recv_unix_msg(sockd); response = recv_unix_msg(sockd);
if (response) {
ret = true;
LOGWARNING("Received: %s in response to %s request", response, msg); LOGWARNING("Received: %s in response to %s request", response, msg);
dealloc(response); dealloc(response);
}
Close(sockd); Close(sockd);
return ret;
} }
int main(int argc, char **argv) int main(int argc, char **argv)
@ -1456,19 +1461,31 @@ int main(int argc, char **argv)
ckp.main.processname = strdup("main"); ckp.main.processname = strdup("main");
ckp.main.sockname = strdup("listener"); ckp.main.sockname = strdup("listener");
name_process_sockname(&ckp.main.us, &ckp.main); name_process_sockname(&ckp.main.us, &ckp.main);
ckp.oldconnfd = ckzalloc(sizeof(int *) * ckp.serverurls);
if (ckp.handover) { if (ckp.handover) {
int sockd = open_unix_client(ckp.main.us.path); const char *path = ckp.main.us.path;
if (sockd > 0 && send_unix_msg(sockd, "getfd")) { if (send_recv_path(path, "ping")) {
ckp.oldconnfd = get_fd(sockd); for (i = 0; i < ckp.serverurls; i++) {
Close(sockd); char getfd[16];
int sockd;
send_recv_path(ckp.main.us.path, "reject");
send_recv_path(ckp.main.us.path, "reconnect");
send_recv_path(ckp.main.us.path, "shutdown");
if (ckp.oldconnfd > 0) snprintf(getfd, 15, "getxfd%d", i);
LOGWARNING("Inherited old socket with new file descriptor %d!", ckp.oldconnfd); sockd = open_unix_client(path);
if (sockd < 1)
break;
if (!send_unix_msg(sockd, getfd))
break;
ckp.oldconnfd[i] = get_fd(sockd);
Close(sockd);
if (!ckp.oldconnfd[i])
break;
LOGWARNING("Inherited old server socket %d with new file descriptor %d!",
i, ckp.oldconnfd[i]);
}
send_recv_path(path, "reject");
send_recv_path(path, "reconnect");
send_recv_path(path, "shutdown");
} }
} }

4
src/ckpool.h

@ -109,8 +109,8 @@ struct ckpool_instance {
/* Logfile */ /* Logfile */
FILE *logfp; FILE *logfp;
int logfd; int logfd;
/* Connector fd if we inherited it from a running process */ /* Connector fds if we inherit them from a running process */
int oldconnfd; int *oldconnfd;
/* Should we inherit a running instance's socket and shut it down */ /* Should we inherit a running instance's socket and shut it down */
bool handover; bool handover;
/* How many clients maximum to accept before rejecting further */ /* How many clients maximum to accept before rejecting further */

17
src/connector.c

@ -722,8 +722,12 @@ retry:
} }
passthrough_client(cdata, client); passthrough_client(cdata, client);
dec_instance_ref(cdata, client); dec_instance_ref(cdata, client);
} else if (cmdmatch(buf, "getfd")) { } else if (cmdmatch(buf, "getxfd")) {
send_fd(cdata->serverfd[0], sockd); int fdno = -1;
sscanf(buf, "getxfd%d", &fdno);
if (fdno > -1 && fdno < cdata->serverfds)
send_fd(cdata->serverfd[fdno], sockd);
} else } else
LOGWARNING("Unhandled connector message: %s", buf); LOGWARNING("Unhandled connector message: %s", buf);
goto retry; goto retry;
@ -738,7 +742,7 @@ int connector(proc_instance_t *pi)
cdata_t *cdata = ckzalloc(sizeof(cdata_t)); cdata_t *cdata = ckzalloc(sizeof(cdata_t));
char *url = NULL, *port = NULL; char *url = NULL, *port = NULL;
ckpool_t *ckp = pi->ckp; ckpool_t *ckp = pi->ckp;
int sockd, ret = 0; int sockd, ret = 0, i;
const int on = 1; const int on = 1;
int tries = 0; int tries = 0;
@ -751,9 +755,10 @@ int connector(proc_instance_t *pi)
else else
cdata->serverfd = ckalloc(sizeof(int *) * ckp->serverurls); cdata->serverfd = ckalloc(sizeof(int *) * ckp->serverurls);
if (ckp->oldconnfd > 0) { for (i = 0; i < ckp->serverurls; i++) {
/* Only handing over the first interface socket for now */ if (!ckp->oldconnfd[i])
cdata->serverfd[0] = ckp->oldconnfd; break;
cdata->serverfd[i] = ckp->oldconnfd[i];
cdata->serverfds++; cdata->serverfds++;
} }

Loading…
Cancel
Save