Removed double free, improved error checking
This commit is contained in:
@@ -97,8 +97,8 @@ void freeConnection(Connection *conn) {
|
|||||||
free(conn->currLine);
|
free(conn->currLine);
|
||||||
free(conn->body);
|
free(conn->body);
|
||||||
for (i = 0; i < conn->fieldCount; i++) {
|
for (i = 0; i < conn->fieldCount; i++) {
|
||||||
free(conn->pathFields[i].var.data);
|
free(conn->fields[i].field);
|
||||||
free(conn->pathFields[i].value.data);
|
free(conn->fields[i].value);
|
||||||
}
|
}
|
||||||
for (i = 0; i < conn->pathFieldCount; i++) {
|
for (i = 0; i < conn->pathFieldCount; i++) {
|
||||||
free(conn->pathFields[i].var.data);
|
free(conn->pathFields[i].var.data);
|
||||||
@@ -367,7 +367,8 @@ static int processChar(Connection *conn, char c, Sitefile *site) {
|
|||||||
}
|
}
|
||||||
if (conn->progress == RECEIVE_BODY &&
|
if (conn->progress == RECEIVE_BODY &&
|
||||||
conn->receivedBody >= conn->bodylen)
|
conn->receivedBody >= conn->bodylen)
|
||||||
sendResponse(conn, site);
|
if (sendResponse(conn, site))
|
||||||
|
return 1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
23
src/main.c
23
src/main.c
@@ -51,7 +51,7 @@ static ConnInfo *conninfo;
|
|||||||
* are needed. */
|
* are needed. */
|
||||||
static const int signals[] = {
|
static const int signals[] = {
|
||||||
SIGPIPE, SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGTRAP, SIGABRT, SIGBUS, SIGFPE,
|
SIGPIPE, SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGTRAP, SIGABRT, SIGBUS, SIGFPE,
|
||||||
SIGKILL, SIGSEGV, SIGTERM, SIGTTIN, SIGTTOU, SIGURG, SIGXCPU, SIGXFSZ, SIGPIPE,
|
SIGKILL, SIGSEGV, SIGTERM, SIGTTIN, SIGTTOU, SIGURG, SIGXCPU, SIGXFSZ,
|
||||||
};
|
};
|
||||||
|
|
||||||
static void exitClean(int signal) {
|
static void exitClean(int signal) {
|
||||||
@@ -60,26 +60,6 @@ static void exitClean(int signal) {
|
|||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void setsignal(int signal, void (*handler)(int)) {
|
|
||||||
struct sigaction action;
|
|
||||||
sigset_t sigset;
|
|
||||||
sigemptyset(&sigset);
|
|
||||||
action.sa_handler = handler;
|
|
||||||
action.sa_mask = sigset;
|
|
||||||
action.sa_flags = SA_NODEFER;
|
|
||||||
sigaction(signal, &action, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void unsetsignal(int signal) {
|
|
||||||
struct sigaction action;
|
|
||||||
sigset_t sigset;
|
|
||||||
sigemptyset(&sigset);
|
|
||||||
action.sa_handler = SIG_DFL;
|
|
||||||
action.sa_mask = sigset;
|
|
||||||
action.sa_flags = SA_NODEFER;
|
|
||||||
sigaction(signal, &action, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void createProcess(int id) {
|
static void createProcess(int id) {
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
int connfd;
|
int connfd;
|
||||||
@@ -156,6 +136,7 @@ int main(int argc, char **argv) {
|
|||||||
if (listeners[i] == NULL) {
|
if (listeners[i] == NULL) {
|
||||||
fprintf(stderr, "Failed to listen on port %hu\n",
|
fprintf(stderr, "Failed to listen on port %hu\n",
|
||||||
site->ports[i].num);
|
site->ports[i].num);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
pollfds[i].fd = listenerfd(listeners[i]);
|
pollfds[i].fd = listenerfd(listeners[i]);
|
||||||
pollfds[i].events = POLLIN;
|
pollfds[i].events = POLLIN;
|
||||||
|
|||||||
@@ -180,23 +180,25 @@ foundport:
|
|||||||
if (fullmatch(&site->content[i].path, conn->path.data) == 0) {
|
if (fullmatch(&site->content[i].path, conn->path.data) == 0) {
|
||||||
switch (site->content[i].command) {
|
switch (site->content[i].command) {
|
||||||
case READ:
|
case READ:
|
||||||
readResponse(conn,
|
if (readResponse(conn,
|
||||||
site->content[i].arg);
|
site->content[i].arg))
|
||||||
|
return 1;
|
||||||
break;
|
break;
|
||||||
case THROW:
|
case THROW:
|
||||||
sendErrorResponse(conn->stream,
|
if (sendErrorResponse(conn->stream,
|
||||||
site->content[i].arg);
|
site->content[i].arg))
|
||||||
|
return 1;
|
||||||
break;
|
break;
|
||||||
case LINKED:
|
case LINKED:
|
||||||
#if DYNAMIC_LINKED_PAGES
|
#if DYNAMIC_LINKED_PAGES
|
||||||
if (!site->getResponse)
|
if (!site->getResponse)
|
||||||
sendErrorResponse(conn->stream,
|
sendErrorResponse(conn->stream,
|
||||||
ERROR_500);
|
ERROR_500);
|
||||||
else
|
else if (linkedResponse(conn,
|
||||||
linkedResponse(conn,
|
site->getResponse))
|
||||||
site->getResponse);
|
return 1;
|
||||||
#else
|
#else
|
||||||
/* Unreachable state */
|
/* Unreachable state (filtered by startup) */
|
||||||
sendErrorResponse(conn->stream,
|
sendErrorResponse(conn->stream,
|
||||||
ERROR_500);
|
ERROR_500);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <swebs/util.h>
|
||||||
#include <swebs/responseutil.h>
|
#include <swebs/responseutil.h>
|
||||||
|
|
||||||
#define CONST_FIELDS "Server: swebs/0.1\r\n"
|
#define CONST_FIELDS "Server: swebs/0.1\r\n"
|
||||||
@@ -29,8 +30,12 @@
|
|||||||
static int resilientSend(Stream *stream, void *data, size_t len) {
|
static int resilientSend(Stream *stream, void *data, size_t len) {
|
||||||
char *buffer = (char *) data;
|
char *buffer = (char *) data;
|
||||||
size_t left = len;
|
size_t left = len;
|
||||||
|
|
||||||
while (left) {
|
while (left) {
|
||||||
ssize_t sent = sendStream(stream, buffer, left);
|
ssize_t sent;
|
||||||
|
|
||||||
|
sent = sendStream(stream, buffer, left);
|
||||||
|
|
||||||
if (sent < 0)
|
if (sent < 0)
|
||||||
return 1;
|
return 1;
|
||||||
if (sent == 0)
|
if (sent == 0)
|
||||||
|
|||||||
@@ -97,8 +97,11 @@ void runServer(int connfd, Sitefile *site, int *pending, int id,
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (i = 1; i < connCount; i++) {
|
for (i = 1; i < connCount; i++) {
|
||||||
|
if (fds[i].revents & POLLIN) {
|
||||||
|
createFormatLog("Connection %d has data", i);
|
||||||
if (updateConnection(connections + i, site))
|
if (updateConnection(connections + i, site))
|
||||||
goto remove;
|
goto remove;
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
remove:
|
remove:
|
||||||
freeConnection(connections + i);
|
freeConnection(connections + i);
|
||||||
@@ -115,6 +118,7 @@ remove:
|
|||||||
Stream *newstream;
|
Stream *newstream;
|
||||||
int newfd;
|
int newfd;
|
||||||
int portind;
|
int portind;
|
||||||
|
createLog("Main fd has data");
|
||||||
newfd = recvFd(connfd);
|
newfd = recvFd(connfd);
|
||||||
if (newfd < 0) {
|
if (newfd < 0) {
|
||||||
createLog("Message received that included an invalid fd, quitting");
|
createLog("Message received that included an invalid fd, quitting");
|
||||||
|
|||||||
@@ -58,4 +58,8 @@ int createTmpName(char *path);
|
|||||||
*
|
*
|
||||||
* Returns non-zero on error, uses rand()
|
* Returns non-zero on error, uses rand()
|
||||||
* */
|
* */
|
||||||
|
|
||||||
|
void setsignal(int signal, void (*handler)(int));
|
||||||
|
void unsetsignal(int signal);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
21
src/util.c
21
src/util.c
@@ -23,6 +23,7 @@
|
|||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <signal.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/shm.h>
|
#include <sys/shm.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
@@ -257,3 +258,23 @@ int createTmpName(char *path) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setsignal(int signal, void (*handler)(int)) {
|
||||||
|
struct sigaction action;
|
||||||
|
sigset_t sigset;
|
||||||
|
sigemptyset(&sigset);
|
||||||
|
action.sa_handler = handler;
|
||||||
|
action.sa_mask = sigset;
|
||||||
|
action.sa_flags = 0;
|
||||||
|
sigaction(signal, &action, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void unsetsignal(int signal) {
|
||||||
|
struct sigaction action;
|
||||||
|
sigset_t sigset;
|
||||||
|
sigemptyset(&sigset);
|
||||||
|
action.sa_handler = SIG_DFL;
|
||||||
|
action.sa_mask = sigset;
|
||||||
|
action.sa_flags = SA_NODEFER;
|
||||||
|
sigaction(signal, &action, NULL);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user