Sent port index with the message

This commit is contained in:
2022-07-04 12:01:46 -05:00
parent 23be8915d6
commit b3500ad90b
6 changed files with 30 additions and 41 deletions

View File

@@ -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))

View File

@@ -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);
}
}
}

View File

@@ -31,8 +31,7 @@
#include <swebs/sitefile.h>
#include <swebs/connections.h>
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) {

View File

@@ -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. */

View File

@@ -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

View File

@@ -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;