From b3500ad90b98c8d4d1cceb094fae0f540191e51b Mon Sep 17 00:00:00 2001 From: Nate Choe Date: Mon, 4 Jul 2022 12:01:46 -0500 Subject: [PATCH] Sent port index with the message --- src/connections.c | 2 +- src/main.c | 26 ++++++-------------------- src/runner.c | 10 ++-------- src/swebs/runner.h | 3 +-- src/swebs/util.h | 4 ++-- src/util.c | 26 ++++++++++++++++++-------- 6 files changed, 30 insertions(+), 41 deletions(-) diff --git a/src/connections.c b/src/connections.c index 945a217..955f82b 100644 --- a/src/connections.c +++ b/src/connections.c @@ -346,7 +346,7 @@ static int processChar(Connection *conn, char c, Sitefile *site) { if (--conn->currLineLen < 0) return 1; if (conn->currLine[conn->currLineLen] != '\r') - return 1; + ++conn->currLineLen; conn->currLine[conn->currLineLen] = '\0'; if (conn->progress == RECEIVE_REQUEST) { if (processRequest(conn)) diff --git a/src/main.c b/src/main.c index c1a1590..7a6025a 100644 --- a/src/main.c +++ b/src/main.c @@ -42,11 +42,10 @@ typedef struct { static Runner *runners; static int processes; -static int *pending; +static volatile int *pending; static Sitefile *site; static int mainfd; /* fd of the UNIX socket */ static struct sockaddr_un addr; -static volatile ConnInfo *conninfo; /* We want to be able to handle a signal at any time, so some global variables * are needed. */ static const int signals[] = { @@ -99,7 +98,7 @@ static void createProcess(int id) { exit(EXIT_FAILURE); } close(mainfd); - runServer(connfd, site, pending, id, conninfo); + runServer(connfd, site, pending, id); createLog("child runServer() finished"); exit(EXIT_SUCCESS); } @@ -123,7 +122,6 @@ int main(int argc, char **argv) { int i; int pendingid; int backlog; - int conninfoid; Listener **listeners; struct pollfd *pollfds; @@ -153,18 +151,8 @@ int main(int argc, char **argv) { exit(EXIT_FAILURE); } - conninfoid = smalloc(sizeof *conninfo); - if (conninfoid < 0) { - createErrorLog("smalloc() failed", errno); - exit(EXIT_FAILURE); - } - conninfo = saddr(conninfoid); - if (conninfo == NULL) { - createErrorLog("saddr() failed", errno); - exit(EXIT_FAILURE); - } - - memset(pending, 0, sizeof(int) * (processes - 1)); + for (i = 0; i < processes - 1; ++i) + pending[i] = 0; mainfd = socket(AF_UNIX, SOCK_STREAM, 0); addr.sun_family = AF_UNIX; @@ -188,6 +176,7 @@ int main(int argc, char **argv) { createLog("swebs started"); for (;;) { + createLog("poll() started"); if (poll(pollfds, site->portcount, -1) < 0) { if (errno == EINTR) continue; @@ -202,14 +191,11 @@ int main(int argc, char **argv) { if (pollfds[i].revents & POLLIN) { int j, lowestproc, fd; fd = acceptConnection(listeners[i]); - while (conninfo->valid) ; lowestproc = 0; for (j = 0; j < processes - 1; j++) if (pending[j] < pending[lowestproc]) lowestproc = j; - conninfo->portind = i; - conninfo->valid = 1; - sendFd(fd, runners[lowestproc].fd); + sendFd(fd, runners[lowestproc].fd, &i, sizeof i); } } } diff --git a/src/runner.c b/src/runner.c index a1d857c..6b811fa 100644 --- a/src/runner.c +++ b/src/runner.c @@ -31,8 +31,7 @@ #include #include -void runServer(int connfd, Sitefile *site, int *pending, int id, - volatile ConnInfo *conninfo) { +void runServer(int connfd, Sitefile *site, volatile int *pending, int id) { int allocConns = 100; struct pollfd *fds; Connection *connections; @@ -114,16 +113,11 @@ remove: int newfd; int portind; createLog("Main fd has data"); - newfd = recvFd(connfd); + newfd = recvFd(connfd, &portind, sizeof portind); if (newfd < 0) { createLog("Message received that included an invalid fd, quitting"); exit(EXIT_FAILURE); } - while (conninfo->valid == 0) ; - portind = conninfo->portind; - conninfo->valid = 0; - - createLog("Obtained file descriptor from child"); newstream = createStream(contexts[portind], O_NONBLOCK, newfd); if (newstream == NULL) { diff --git a/src/swebs/runner.h b/src/swebs/runner.h index 64ac845..705e576 100644 --- a/src/swebs/runner.h +++ b/src/swebs/runner.h @@ -28,8 +28,7 @@ typedef struct { int portind; } ConnInfo; -void runServer(int connfd, Sitefile *site, int *pending, int id, - volatile ConnInfo *info); +void runServer(int connfd, Sitefile *site, volatile int *pending, int id); /* pending and info are shared memory. pending[id] is the amount of connections * that are being processed by that process, and info contains info about the * connection being sent through. */ diff --git a/src/swebs/util.h b/src/swebs/util.h index 1edbab9..fd9a6a2 100644 --- a/src/swebs/util.h +++ b/src/swebs/util.h @@ -41,8 +41,8 @@ int istrcmp(char *s1, char *s2); /* case insensitive strcmp */ RequestType getType(char *str); -void sendFd(int fd, int dest); -int recvFd(int source); +void sendFd(int fd, int dest, void *data, size_t len); +int recvFd(int source, void *data, size_t len); int createTmpName(char *path); /* WIll set the 5 characters at the end of path to random data so that that diff --git a/src/util.c b/src/util.c index b5a6754..44478f7 100644 --- a/src/util.c +++ b/src/util.c @@ -173,7 +173,7 @@ RequestType getType(char *str) { return INVALID; } -void sendFd(int fd, int dest) { +void sendFd(int fd, int dest, void *data, size_t len) { struct msghdr msg; struct cmsghdr *cmsg; char iobuf[1]; @@ -183,8 +183,14 @@ void sendFd(int fd, int dest) { struct cmsghdr align; } u; memset(&msg, 0, sizeof(msg)); - io.iov_base = iobuf; - io.iov_len = sizeof(iobuf); + if (data == NULL) { + io.iov_base = iobuf; + io.iov_len = sizeof(iobuf); + } + else { + io.iov_base = data; + io.iov_len = len; + } msg.msg_iov = &io; msg.msg_iovlen = 1; msg.msg_control = u.buf; @@ -197,7 +203,7 @@ void sendFd(int fd, int dest) { sendmsg(dest, &msg, 0); } -int recvFd(int source) { +int recvFd(int source, void *data, size_t len) { union { char buff[CMSG_SPACE(sizeof(int))]; struct cmsghdr align; @@ -205,15 +211,19 @@ int recvFd(int source) { struct msghdr msg; struct cmsghdr *cmsg; struct iovec iov; - int data; ssize_t nr; int ret; + char buf[1]; msg.msg_name = NULL; msg.msg_namelen = 0; - iov.iov_base = &data; - iov.iov_len = sizeof(data); + if (data == NULL) { + data = buf; + len = sizeof buf; + } + iov.iov_base = data; + iov.iov_len = len; msg.msg_iov = &iov; msg.msg_iovlen = 1; @@ -224,7 +234,7 @@ int recvFd(int source) { return -1; cmsg = CMSG_FIRSTHDR(&msg); - if (cmsg == NULL || cmsg->cmsg_len != CMSG_LEN(sizeof(data))) + if (cmsg == NULL || cmsg->cmsg_len != CMSG_LEN(len)) return -1; if (cmsg->cmsg_level != SOL_SOCKET) return -1;