Properly handled signals
This commit is contained in:
2
Makefile
2
Makefile
@@ -2,7 +2,7 @@ SRC = $(wildcard src/*.c)
|
|||||||
OBJ = $(subst .c,.o,$(subst src,work,$(SRC)))
|
OBJ = $(subst .c,.o,$(subst src,work,$(SRC)))
|
||||||
LIBS = gnutls
|
LIBS = gnutls
|
||||||
LDFLAGS = -pie -lrt -ldl $(shell pkg-config --libs $(LIBS))
|
LDFLAGS = -pie -lrt -ldl $(shell pkg-config --libs $(LIBS))
|
||||||
CFLAGS := -O2 -pipe -Wall -Wpedantic -Wshadow -ansi
|
CFLAGS := -O2 -pipe -Wall -Wpedantic -Wshadow -ansi -D_XOPEN_SOURCE=500
|
||||||
CFLAGS += -Isrc/ -fpie -D_POSIX_C_SOURCE=200809L $(shell pkg-config --cflags $(LIBS))
|
CFLAGS += -Isrc/ -fpie -D_POSIX_C_SOURCE=200809L $(shell pkg-config --cflags $(LIBS))
|
||||||
INSTALLDIR := /usr/sbin
|
INSTALLDIR := /usr/sbin
|
||||||
HEADERDIR := /usr/include/
|
HEADERDIR := /usr/include/
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
define port 8000
|
define port 8000
|
||||||
|
|
||||||
#define transport TLS
|
define transport TLS
|
||||||
define key domain.key
|
define key domain.key
|
||||||
define cert domain.crt
|
define cert domain.crt
|
||||||
define timeout 2000
|
define timeout 2000
|
||||||
|
|||||||
85
src/main.c
85
src/main.c
@@ -48,10 +48,42 @@ static Sitefile *site;
|
|||||||
static struct sockaddr_un addr;
|
static struct sockaddr_un addr;
|
||||||
/* 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[] = {
|
||||||
|
SIGPIPE, SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGTRAP, SIGABRT, SIGBUS, SIGFPE,
|
||||||
|
SIGKILL, SIGSEGV, SIGTERM, SIGTTIN, SIGTTOU, SIGURG, SIGXCPU, SIGXFSZ, SIGPIPE,
|
||||||
|
};
|
||||||
|
|
||||||
|
static void exitClean(int signal) {
|
||||||
|
freeListener(listener);
|
||||||
|
close(mainfd);
|
||||||
|
remove(addr.sun_path);
|
||||||
|
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;
|
||||||
|
int i;
|
||||||
socklen_t addrlen;
|
socklen_t addrlen;
|
||||||
|
|
||||||
createLog("Creating a new process");
|
createLog("Creating a new process");
|
||||||
@@ -60,7 +92,7 @@ static void createProcess(int id) {
|
|||||||
pid = fork();
|
pid = fork();
|
||||||
switch (pid) {
|
switch (pid) {
|
||||||
case -1:
|
case -1:
|
||||||
createLog("fork() failed");
|
createErrorLog("fork() failed", errno);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
case 0:
|
case 0:
|
||||||
break;
|
break;
|
||||||
@@ -72,11 +104,15 @@ static void createProcess(int id) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < sizeof(signals) / sizeof(signals[0]); i++)
|
||||||
|
unsetsignal(signals[i]);
|
||||||
|
unsetsignal(SIGCHLD);
|
||||||
|
|
||||||
connfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
connfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
if (connfd < 0)
|
if (connfd < 0)
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
if (connect(connfd, (struct sockaddr *) &addr, sizeof(addr))) {
|
if (connect(connfd, (struct sockaddr *) &addr, sizeof(addr))) {
|
||||||
createLog("connect() failed, killing child");
|
createErrorLog("connect() failed, killing child", errno);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
close(mainfd);
|
close(mainfd);
|
||||||
@@ -99,23 +135,6 @@ static void remakeChild(int signal) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void exitClean(int signal) {
|
|
||||||
freeListener(listener);
|
|
||||||
close(mainfd);
|
|
||||||
remove(addr.sun_path);
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
int i;
|
int i;
|
||||||
int pendingid;
|
int pendingid;
|
||||||
@@ -124,12 +143,12 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
pendingid = smalloc(sizeof(int) * (processes - 1));
|
pendingid = smalloc(sizeof(int) * (processes - 1));
|
||||||
if (pendingid < 0) {
|
if (pendingid < 0) {
|
||||||
createLog("smalloc() failed");
|
createErrorLog("smalloc() failed", errno);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
pending = saddr(pendingid);
|
pending = saddr(pendingid);
|
||||||
if (pending == NULL) {
|
if (pending == NULL) {
|
||||||
createLog("saddr() failed");
|
createErrorLog("saddr() failed", errno);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
memset(pending, 0, sizeof(int) * (processes - 1));
|
memset(pending, 0, sizeof(int) * (processes - 1));
|
||||||
@@ -148,24 +167,9 @@ int main(int argc, char **argv) {
|
|||||||
for (i = 0; i < processes - 1; i++)
|
for (i = 0; i < processes - 1; i++)
|
||||||
createProcess(i);
|
createProcess(i);
|
||||||
|
|
||||||
setsignal(SIGPIPE, SIG_IGN);
|
for (i = 0; i < sizeof(signals) / sizeof(signals[0]); i++)
|
||||||
setsignal(SIGHUP, exitClean);
|
setsignal(signals[i], exitClean);
|
||||||
setsignal(SIGINT, exitClean);
|
|
||||||
setsignal(SIGQUIT, exitClean);
|
|
||||||
setsignal(SIGILL, exitClean);
|
|
||||||
setsignal(SIGTRAP, exitClean);
|
|
||||||
setsignal(SIGABRT, exitClean);
|
|
||||||
setsignal(SIGBUS, exitClean);
|
|
||||||
setsignal(SIGFPE, exitClean);
|
|
||||||
setsignal(SIGKILL, exitClean);
|
|
||||||
setsignal(SIGSEGV, exitClean);
|
|
||||||
setsignal(SIGTERM, exitClean);
|
|
||||||
setsignal(SIGTTIN, exitClean);
|
|
||||||
setsignal(SIGTTOU, exitClean);
|
|
||||||
setsignal(SIGURG, exitClean);
|
|
||||||
setsignal(SIGXCPU, exitClean);
|
|
||||||
setsignal(SIGXFSZ, exitClean);
|
|
||||||
setsignal(SIGPIPE, exitClean);
|
|
||||||
setsignal(SIGCHLD, remakeChild);
|
setsignal(SIGCHLD, remakeChild);
|
||||||
|
|
||||||
createLog("swebs started");
|
createLog("swebs started");
|
||||||
@@ -178,7 +182,8 @@ int main(int argc, char **argv) {
|
|||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
if (errno == ENOTSOCK || errno == EOPNOTSUPP ||
|
if (errno == ENOTSOCK || errno == EOPNOTSUPP ||
|
||||||
errno == EINVAL) {
|
errno == EINVAL) {
|
||||||
createLog("You've majorly screwed up");
|
createErrorLog("You've majorly screwed up. Good luck",
|
||||||
|
errno);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <swebs/util.h>
|
#include <swebs/util.h>
|
||||||
@@ -160,7 +161,7 @@ NULL
|
|||||||
createLog("Couldn't find swebs user");
|
createLog("Couldn't find swebs user");
|
||||||
else
|
else
|
||||||
if (seteuid(swebs->pw_uid))
|
if (seteuid(swebs->pw_uid))
|
||||||
createLog("seteuid() failed");
|
createErrorLog("seteuid() failed", errno);
|
||||||
root = getpwnam("root");
|
root = getpwnam("root");
|
||||||
if (root == NULL) {
|
if (root == NULL) {
|
||||||
createLog("Couldn't find root user, quitting");
|
createLog("Couldn't find root user, quitting");
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ void sfree(void *addr);
|
|||||||
void sdestroy(int id);
|
void sdestroy(int id);
|
||||||
|
|
||||||
int createLog(char *msg);
|
int createLog(char *msg);
|
||||||
|
int createErrorLog(char *msg, int err);
|
||||||
int istrcmp(char *s1, char *s2);
|
int istrcmp(char *s1, char *s2);
|
||||||
/* case insensitive strcmp */
|
/* case insensitive strcmp */
|
||||||
RequestType getType(char *str);
|
RequestType getType(char *str);
|
||||||
|
|||||||
19
src/util.c
19
src/util.c
@@ -76,6 +76,25 @@ int createLog(char *msg) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int createErrorLog(char *msg, int err) {
|
||||||
|
time_t currenttime;
|
||||||
|
struct tm *timeinfo;
|
||||||
|
time(¤ttime);
|
||||||
|
timeinfo = gmtime(¤ttime);
|
||||||
|
if (timeinfo == NULL)
|
||||||
|
return 1;
|
||||||
|
fprintf(logs, "[%d-%02d-%02dT%02d:%02d:%02dZ] %s: %s\n",
|
||||||
|
timeinfo->tm_year + 1900,
|
||||||
|
timeinfo->tm_mon + 1,
|
||||||
|
timeinfo->tm_mday,
|
||||||
|
timeinfo->tm_hour,
|
||||||
|
timeinfo->tm_min,
|
||||||
|
timeinfo->tm_sec,
|
||||||
|
msg, strerror(err));
|
||||||
|
fflush(logs);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int istrcmp(char *s1, char *s2) {
|
int istrcmp(char *s1, char *s2) {
|
||||||
int i;
|
int i;
|
||||||
for (i = 0;; i++) {
|
for (i = 0;; i++) {
|
||||||
|
|||||||
Reference in New Issue
Block a user