Sent port index with the message
This commit is contained in:
@@ -346,7 +346,7 @@ static int processChar(Connection *conn, char c, Sitefile *site) {
|
|||||||
if (--conn->currLineLen < 0)
|
if (--conn->currLineLen < 0)
|
||||||
return 1;
|
return 1;
|
||||||
if (conn->currLine[conn->currLineLen] != '\r')
|
if (conn->currLine[conn->currLineLen] != '\r')
|
||||||
return 1;
|
++conn->currLineLen;
|
||||||
conn->currLine[conn->currLineLen] = '\0';
|
conn->currLine[conn->currLineLen] = '\0';
|
||||||
if (conn->progress == RECEIVE_REQUEST) {
|
if (conn->progress == RECEIVE_REQUEST) {
|
||||||
if (processRequest(conn))
|
if (processRequest(conn))
|
||||||
|
|||||||
26
src/main.c
26
src/main.c
@@ -42,11 +42,10 @@ typedef struct {
|
|||||||
|
|
||||||
static Runner *runners;
|
static Runner *runners;
|
||||||
static int processes;
|
static int processes;
|
||||||
static int *pending;
|
static volatile int *pending;
|
||||||
static Sitefile *site;
|
static Sitefile *site;
|
||||||
static int mainfd; /* fd of the UNIX socket */
|
static int mainfd; /* fd of the UNIX socket */
|
||||||
static struct sockaddr_un addr;
|
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
|
/* We want to be able to handle a signal at any time, so some global variables
|
||||||
* are needed. */
|
* are needed. */
|
||||||
static const int signals[] = {
|
static const int signals[] = {
|
||||||
@@ -99,7 +98,7 @@ static void createProcess(int id) {
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
close(mainfd);
|
close(mainfd);
|
||||||
runServer(connfd, site, pending, id, conninfo);
|
runServer(connfd, site, pending, id);
|
||||||
createLog("child runServer() finished");
|
createLog("child runServer() finished");
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
@@ -123,7 +122,6 @@ int main(int argc, char **argv) {
|
|||||||
int i;
|
int i;
|
||||||
int pendingid;
|
int pendingid;
|
||||||
int backlog;
|
int backlog;
|
||||||
int conninfoid;
|
|
||||||
Listener **listeners;
|
Listener **listeners;
|
||||||
struct pollfd *pollfds;
|
struct pollfd *pollfds;
|
||||||
|
|
||||||
@@ -153,18 +151,8 @@ int main(int argc, char **argv) {
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
conninfoid = smalloc(sizeof *conninfo);
|
for (i = 0; i < processes - 1; ++i)
|
||||||
if (conninfoid < 0) {
|
pending[i] = 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));
|
|
||||||
|
|
||||||
mainfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
mainfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
addr.sun_family = AF_UNIX;
|
addr.sun_family = AF_UNIX;
|
||||||
@@ -188,6 +176,7 @@ int main(int argc, char **argv) {
|
|||||||
createLog("swebs started");
|
createLog("swebs started");
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
|
createLog("poll() started");
|
||||||
if (poll(pollfds, site->portcount, -1) < 0) {
|
if (poll(pollfds, site->portcount, -1) < 0) {
|
||||||
if (errno == EINTR)
|
if (errno == EINTR)
|
||||||
continue;
|
continue;
|
||||||
@@ -202,14 +191,11 @@ int main(int argc, char **argv) {
|
|||||||
if (pollfds[i].revents & POLLIN) {
|
if (pollfds[i].revents & POLLIN) {
|
||||||
int j, lowestproc, fd;
|
int j, lowestproc, fd;
|
||||||
fd = acceptConnection(listeners[i]);
|
fd = acceptConnection(listeners[i]);
|
||||||
while (conninfo->valid) ;
|
|
||||||
lowestproc = 0;
|
lowestproc = 0;
|
||||||
for (j = 0; j < processes - 1; j++)
|
for (j = 0; j < processes - 1; j++)
|
||||||
if (pending[j] < pending[lowestproc])
|
if (pending[j] < pending[lowestproc])
|
||||||
lowestproc = j;
|
lowestproc = j;
|
||||||
conninfo->portind = i;
|
sendFd(fd, runners[lowestproc].fd, &i, sizeof i);
|
||||||
conninfo->valid = 1;
|
|
||||||
sendFd(fd, runners[lowestproc].fd);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
10
src/runner.c
10
src/runner.c
@@ -31,8 +31,7 @@
|
|||||||
#include <swebs/sitefile.h>
|
#include <swebs/sitefile.h>
|
||||||
#include <swebs/connections.h>
|
#include <swebs/connections.h>
|
||||||
|
|
||||||
void runServer(int connfd, Sitefile *site, int *pending, int id,
|
void runServer(int connfd, Sitefile *site, volatile int *pending, int id) {
|
||||||
volatile ConnInfo *conninfo) {
|
|
||||||
int allocConns = 100;
|
int allocConns = 100;
|
||||||
struct pollfd *fds;
|
struct pollfd *fds;
|
||||||
Connection *connections;
|
Connection *connections;
|
||||||
@@ -114,16 +113,11 @@ remove:
|
|||||||
int newfd;
|
int newfd;
|
||||||
int portind;
|
int portind;
|
||||||
createLog("Main fd has data");
|
createLog("Main fd has data");
|
||||||
newfd = recvFd(connfd);
|
newfd = recvFd(connfd, &portind, sizeof portind);
|
||||||
if (newfd < 0) {
|
if (newfd < 0) {
|
||||||
createLog("Message received that included an invalid fd, quitting");
|
createLog("Message received that included an invalid fd, quitting");
|
||||||
exit(EXIT_FAILURE);
|
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);
|
newstream = createStream(contexts[portind], O_NONBLOCK, newfd);
|
||||||
if (newstream == NULL) {
|
if (newstream == NULL) {
|
||||||
|
|||||||
@@ -28,8 +28,7 @@ typedef struct {
|
|||||||
int portind;
|
int portind;
|
||||||
} ConnInfo;
|
} ConnInfo;
|
||||||
|
|
||||||
void runServer(int connfd, Sitefile *site, int *pending, int id,
|
void runServer(int connfd, Sitefile *site, volatile int *pending, int id);
|
||||||
volatile ConnInfo *info);
|
|
||||||
/* pending and info are shared memory. pending[id] is the amount of connections
|
/* 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
|
* that are being processed by that process, and info contains info about the
|
||||||
* connection being sent through. */
|
* connection being sent through. */
|
||||||
|
|||||||
@@ -41,8 +41,8 @@ int istrcmp(char *s1, char *s2);
|
|||||||
/* case insensitive strcmp */
|
/* case insensitive strcmp */
|
||||||
RequestType getType(char *str);
|
RequestType getType(char *str);
|
||||||
|
|
||||||
void sendFd(int fd, int dest);
|
void sendFd(int fd, int dest, void *data, size_t len);
|
||||||
int recvFd(int source);
|
int recvFd(int source, void *data, size_t len);
|
||||||
|
|
||||||
int createTmpName(char *path);
|
int createTmpName(char *path);
|
||||||
/* WIll set the 5 characters at the end of path to random data so that that
|
/* WIll set the 5 characters at the end of path to random data so that that
|
||||||
|
|||||||
26
src/util.c
26
src/util.c
@@ -173,7 +173,7 @@ RequestType getType(char *str) {
|
|||||||
return INVALID;
|
return INVALID;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendFd(int fd, int dest) {
|
void sendFd(int fd, int dest, void *data, size_t len) {
|
||||||
struct msghdr msg;
|
struct msghdr msg;
|
||||||
struct cmsghdr *cmsg;
|
struct cmsghdr *cmsg;
|
||||||
char iobuf[1];
|
char iobuf[1];
|
||||||
@@ -183,8 +183,14 @@ void sendFd(int fd, int dest) {
|
|||||||
struct cmsghdr align;
|
struct cmsghdr align;
|
||||||
} u;
|
} u;
|
||||||
memset(&msg, 0, sizeof(msg));
|
memset(&msg, 0, sizeof(msg));
|
||||||
io.iov_base = iobuf;
|
if (data == NULL) {
|
||||||
io.iov_len = sizeof(iobuf);
|
io.iov_base = iobuf;
|
||||||
|
io.iov_len = sizeof(iobuf);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
io.iov_base = data;
|
||||||
|
io.iov_len = len;
|
||||||
|
}
|
||||||
msg.msg_iov = &io;
|
msg.msg_iov = &io;
|
||||||
msg.msg_iovlen = 1;
|
msg.msg_iovlen = 1;
|
||||||
msg.msg_control = u.buf;
|
msg.msg_control = u.buf;
|
||||||
@@ -197,7 +203,7 @@ void sendFd(int fd, int dest) {
|
|||||||
sendmsg(dest, &msg, 0);
|
sendmsg(dest, &msg, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int recvFd(int source) {
|
int recvFd(int source, void *data, size_t len) {
|
||||||
union {
|
union {
|
||||||
char buff[CMSG_SPACE(sizeof(int))];
|
char buff[CMSG_SPACE(sizeof(int))];
|
||||||
struct cmsghdr align;
|
struct cmsghdr align;
|
||||||
@@ -205,15 +211,19 @@ int recvFd(int source) {
|
|||||||
struct msghdr msg;
|
struct msghdr msg;
|
||||||
struct cmsghdr *cmsg;
|
struct cmsghdr *cmsg;
|
||||||
struct iovec iov;
|
struct iovec iov;
|
||||||
int data;
|
|
||||||
ssize_t nr;
|
ssize_t nr;
|
||||||
int ret;
|
int ret;
|
||||||
|
char buf[1];
|
||||||
|
|
||||||
msg.msg_name = NULL;
|
msg.msg_name = NULL;
|
||||||
msg.msg_namelen = 0;
|
msg.msg_namelen = 0;
|
||||||
|
|
||||||
iov.iov_base = &data;
|
if (data == NULL) {
|
||||||
iov.iov_len = sizeof(data);
|
data = buf;
|
||||||
|
len = sizeof buf;
|
||||||
|
}
|
||||||
|
iov.iov_base = data;
|
||||||
|
iov.iov_len = len;
|
||||||
msg.msg_iov = &iov;
|
msg.msg_iov = &iov;
|
||||||
msg.msg_iovlen = 1;
|
msg.msg_iovlen = 1;
|
||||||
|
|
||||||
@@ -224,7 +234,7 @@ int recvFd(int source) {
|
|||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
cmsg = CMSG_FIRSTHDR(&msg);
|
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;
|
return -1;
|
||||||
if (cmsg->cmsg_level != SOL_SOCKET)
|
if (cmsg->cmsg_level != SOL_SOCKET)
|
||||||
return -1;
|
return -1;
|
||||||
|
|||||||
Reference in New Issue
Block a user