commit cb73df5b7e3746edcb70fd57b462161efec5385a
parent 0fbb73a1590a323d17ac96dad034c608b891e56c
Author: Christoph Lohmann <20h@r-36.net>
Date: Sun, 27 Feb 2011 10:56:56 +0100
Cleaning up signal handling.
Diffstat:
main.c | | | 42 | ++++++++++++++++++++++++++++++++---------- |
1 file changed, 32 insertions(+), 10 deletions(-)
diff --git a/main.c b/main.c
@@ -33,6 +33,8 @@ enum {
int glfd = -1;
int loglvl = 15;
+int running = 1;
+char *logfile = nil;
char *argv0;
char *stdbase = "/var/gopher";
@@ -57,7 +59,6 @@ char *htredir = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
int
dropprivileges(struct group *gr, struct passwd *pw)
{
-
if(gr != nil)
if(setgroups(1, &gr->gr_gid) != 0 || setgid(gr->gr_gid) != 0)
return -1;
@@ -194,19 +195,41 @@ handlerequest(int sock, char *base, char *ohost, char *port, char *clienth,
}
void
-hndlsigchld(int signo)
+sighandler(int sig)
{
- int status;
-
- while(waitpid(-1, &status, WNOHANG) > 0);
+ switch(sig) {
+ case SIGCHLD:
+ while(waitpid(-1, NULL, WNOHANG) > 0);
+ break;
+ case SIGHUP:
+ case SIGINT:
+ case SIGQUIT:
+ case SIGABRT:
+ case SIGTERM:
+ if(logfile != nil)
+ stoplogging(glfd);
+ running = 0;
+ break;
+ default:
+ break;
+ }
+}
- return;
+void
+initsignals(void)
+{
+ signal(SIGCHLD, sighandler);
+ signal(SIGHUP, sighandler);
+ signal(SIGINT, sighandler);
+ signal(SIGQUIT, sighandler);
+ signal(SIGABRT, sighandler);
+ signal(SIGTERM, sighandler);
+ signal(SIGKILL, sighandler);
}
void
usage(void)
{
-
tprintf(2, "usage: %s [-d] [-l logfile] [-v loglvl] [-b base]"
" [-p port] [-o sport] [-u user] [-g group] [-h host]"
" [-i IP]\n",
@@ -222,7 +245,7 @@ main(int argc, char *argv[])
struct sockaddr_storage clt;
socklen_t cltlen;
int sock, list, opt, dofork;
- char *port, *base, *logfile, clienth[NI_MAXHOST], clientp[NI_MAXSERV];
+ char *port, *base, clienth[NI_MAXHOST], clientp[NI_MAXSERV];
char *user, *group, *bindip, *ohost, *sport;
struct passwd *us;
struct group *gr;
@@ -230,7 +253,6 @@ main(int argc, char *argv[])
base = stdbase;
port = stdport;
dofork = 1;
- logfile = nil;
user = nil;
group = nil;
us = nil;
@@ -349,7 +371,7 @@ main(int argc, char *argv[])
signal(SIGCHLD, hndlsigchld);
cltlen = sizeof(clt);
- for(;;) {
+ while(running) {
sock = accept(list, (struct sockaddr *)&clt, &cltlen);
if(sock < 0) {
perror("accept");