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);
send_unix_msg(sockd, "success");
}
} else if (cmdmatch(buf, "getfd")) {
int connfd = send_procmsg(ckp->connector, "getfd");
} else if (cmdmatch(buf, "getxfd")) {
int connfd = send_procmsg(ckp->connector, buf);
if (connfd > 0) {
int newfd = get_fd(connfd);
@ -1221,16 +1221,21 @@ static struct option long_options[] = {
};
#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);
bool ret = false;
char *response;
send_unix_msg(sockd, msg);
response = recv_unix_msg(sockd);
if (response) {
ret = true;
LOGWARNING("Received: %s in response to %s request", response, msg);
dealloc(response);
}
Close(sockd);
return ret;
}
int main(int argc, char **argv)
@ -1456,19 +1461,31 @@ int main(int argc, char **argv)
ckp.main.processname = strdup("main");
ckp.main.sockname = strdup("listener");
name_process_sockname(&ckp.main.us, &ckp.main);
ckp.oldconnfd = ckzalloc(sizeof(int *) * ckp.serverurls);
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")) {
ckp.oldconnfd = get_fd(sockd);
Close(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 (send_recv_path(path, "ping")) {
for (i = 0; i < ckp.serverurls; i++) {
char getfd[16];
int sockd;
if (ckp.oldconnfd > 0)
LOGWARNING("Inherited old socket with new file descriptor %d!", ckp.oldconnfd);
snprintf(getfd, 15, "getxfd%d", i);
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 */
FILE *logfp;
int logfd;
/* Connector fd if we inherited it from a running process */
int oldconnfd;
/* Connector fds if we inherit them from a running process */
int *oldconnfd;
/* Should we inherit a running instance's socket and shut it down */
bool handover;
/* How many clients maximum to accept before rejecting further */

17
src/connector.c

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

Loading…
Cancel
Save