From: Roy Marples Date: Wed, 3 Sep 2008 19:27:47 +0000 (+0000) Subject: Allow 5 control socket connections at once. Time each out after 5 minutes. X-Git-Tag: v5.0.0~310 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=26ab34ee2033ef098d367df1ea5d77fd7c22a592;p=thirdparty%2Fdhcpcd.git Allow 5 control socket connections at once. Time each out after 5 minutes. --- diff --git a/control.c b/control.c index 6b0f1a19..79c79bc8 100644 --- a/control.c +++ b/control.c @@ -45,19 +45,34 @@ struct sockaddr_un sun; static char buffer[1024]; static char *argvp[255]; +static int fds[5]; + +static void +remove_control_data(void *arg) +{ + size_t i; + + for (i = 0; i < sizeof(fds); i++) { + if (&fds[i] == arg) { + close(fds[i]); + delete_event(fds[i]); + fds[i] = -1; + } + } +} + static void handle_control_data(void *arg) { ssize_t bytes; - int argc, s = (int)arg; + int argc, *s = arg; char *e, *p; char **ap; for (;;) { - bytes = read(s, buffer, sizeof(buffer)); + bytes = read(*s, buffer, sizeof(buffer)); if (bytes == -1 || bytes == 0) { - close(s); - delete_event(s); + remove_control_data(arg); return; } p = buffer; @@ -78,12 +93,21 @@ handle_control(_unused void *arg) { struct sockaddr_un run; socklen_t len; - int s; + size_t i; + + for (i = 0; i < sizeof(fds); i++) { + if (fds[i] == -1) + break; + } + if (i >= sizeof(fds)) + return; len = sizeof(run); - if ((s = accept(fd, (struct sockaddr *)&run, &len)) == -1) + if ((fds[i] = accept(fd, (struct sockaddr *)&run, &len)) == -1) return; - add_event(s, handle_control_data, (void *)s); + add_event(fds[i], handle_control_data, &fds[i]); + /* Timeout the connection after 5 minutes - should be plenty */ + add_timeout_sec(300, remove_control_data, &fds[i]); } static int @@ -109,11 +133,12 @@ start_control(void) chmod(CONTROLSOCKET, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) == -1 || set_cloexec(fd) == -1 || set_nonblock(fd) == -1 || - listen(fd, 0) == -1) + listen(fd, sizeof(fds)) == -1) { close(fd); return -1; } + memset(fds, -1, sizeof(fds)); add_event(fd, handle_control, NULL); return fd; }