--- quark.c | 129 ++++++++++++++++++++++++++++--------------------------------= ---- 1 file changed, 57 insertions(+), 72 deletions(-) diff --git a/quark.c b/quark.c index 234f6e2..ae018ea 100644 --- a/quark.c +++ b/quark.c _AT_@ -26,6 +26,12 @@ enum { HEAD =3D 5, }; =20 +enum { + MSG =3D 0, + ERR =3D 1, + DIE =3D 2, +}; + typedef struct { const char *extension; const char *mimetype; _AT_@ -60,10 +66,7 @@ static const char texthtml[] =3D "text/html"; =20 static ssize_t writetext(const char *buf); static ssize_t writedata(const char *buf, size_t buflen); -static void atomiclog(int fd, const char *errstr, va_list ap); -static void die(const char *errstr, ...); -static void logmsg(const char *errstr, ...); -static void logerrmsg(const char *errstr, ...); +static void qlog(int type, const char *errstr, ...); static void response(void); static void responsecgi(void); static void responsedir(void); _AT_@ -92,7 +95,7 @@ writedata(const char *buf, size_t buf_len) { =20 while(offset < buf_len) { if((r =3D write(req.fd, buf + offset, buf_len - offset)) =3D=3D -1) { - logerrmsg("client %s closed connection\n", host); + qlog(ERR, "client %s closed connection\n", host); return -1; } offset +=3D r; _AT_@ -106,47 +109,29 @@ writetext(const char *buf) { } =20 void -atomiclog(int fd, const char *errstr, va_list ap) { +qlog(int type, const char *errstr, ...) { + va_list ap; static char buf[512]; int n; =20 + va_start(ap, errstr); +=09 /* assemble the message in buf and write it in one pass to avoid interleaved concurrent writes on a shared fd. */ n =3D snprintf(buf, sizeof buf, "%s: ", tstamp()); n +=3D vsnprintf(buf + n, sizeof buf - n, errstr, ap); - if (n >=3D sizeof buf) + if (n >=3D sizeof buf){ n =3D sizeof buf - 1; - write(fd, buf, n); -} - -void -logmsg(const char *errstr, ...) { - va_list ap; - - va_start(ap, errstr); - atomiclog(STDOUT_FILENO, errstr, ap); - va_end(ap); -} - -void -logerrmsg(const char *errstr, ...) { - va_list ap; - - va_start(ap, errstr); - atomiclog(STDERR_FILENO, errstr, ap); - va_end(ap); -} - -void -die(const char *errstr, ...) { - va_list ap; - - va_start(ap, errstr); - atomiclog(STDERR_FILENO, errstr, ap); + }; + write((type =3D=3D MSG)?STDOUT_FILENO:STDERR_FILENO, buf, n); +=09 va_end(ap); - exit(EXIT_FAILURE); +=09 + if(type =3D=3D DIE){ + exit(EXIT_FAILURE); + }; } =20 int _AT_@ -158,7 +143,7 @@ responsehdr(const char *status) { "Server: quark-"VERSION"\r\n", status, tstamp()) >=3D MAXBUFLEN) { - logerrmsg("snprintf failed, buffer size exceeded"); + qlog(ERR, "snprintf failed, buffer size exceeded"); return -1; } return writetext(resbuf); _AT_@ -170,7 +155,7 @@ responsecontentlen(off_t size) { "Content-Length: %lu\r\n", size) >=3D MAXBUFLEN) { - logerrmsg("snprintf failed, buffer sizeof exceeded"); + qlog(ERR, "snprintf failed, buffer sizeof exceeded"); return -1; } return writetext(resbuf); _AT_@ -182,7 +167,7 @@ responselocation(const char *location, const char *path= info) { "Location: %s%s\r\n", location, pathinfo) >=3D MAXBUFLEN) { - logerrmsg("snprintf failed, buffer sizeof exceeded"); + qlog(ERR, "snprintf failed, buffer sizeof exceeded"); return -1; } return writetext(resbuf); _AT_@ -194,7 +179,7 @@ responsecontenttype(const char *mimetype) { "Content-Type: %s\r\n", mimetype) >=3D MAXBUFLEN) { - logerrmsg("snprintf failed, buffer sizeof exceeded"); + qlog(ERR, "snprintf failed, buffer sizeof exceeded"); return -1; } return writetext(resbuf); _AT_@ -207,9 +192,9 @@ responsefiledata(int fd, off_t size) { =20 for(; (n =3D read(fd, buf, MIN(size, sizeof buf))) > 0; size -=3D n) if(write(req.fd, buf, n) !=3D n) - logerrmsg("error writing to client %s: %s\n", host, strerror(errno)); + qlog(ERR, "error writing to client %s: %s\n", host, strerror(errno)); if(n =3D=3D -1) - logerrmsg("error reading from file: %s\n", strerror(errno)); + qlog(ERR, "error reading from file: %s\n", strerror(errno)); } =20 void _AT_@ -220,7 +205,7 @@ responsefile(void) { struct stat st; =20 if(stat(reqbuf, &st) =3D=3D -1 || (ffd =3D open(reqbuf, O_RDONLY)) =3D=3D= -1) { - logerrmsg("%s requests unknown path %s\n", host, reqbuf); + qlog(ERR, "%s requests unknown path %s\n", host, reqbuf); if(responsehdr(HttpNotFound) !=3D -1 && responsecontenttype(texthtml) !=3D -1) ; _AT_@ -268,7 +253,7 @@ responsedirdata(DIR *d) { if(snprintf(resbuf, MAXBUFLEN, "<a href=3D'%s%s'>%s</a><br>\r\n", reqbuf, e->d_name, e->d_name) >=3D MAXBUFLEN) { - logerrmsg("snprintf failed, buffer sizeof exceeded"); + qlog(ERR, "snprintf failed, buffer sizeof exceeded"); return; } if(writetext(resbuf) =3D=3D -1) _AT_@ -287,7 +272,7 @@ responsedir(void) { /* add directory terminator if necessary */ reqbuf[len++] =3D '/'; reqbuf[len] =3D 0; - logmsg("redirecting %s to %s%s\n", host, location, reqbuf); + qlog(MSG, "redirecting %s to %s%s\n", host, location, reqbuf); if(responsehdr(HttpMoved) !=3D -1 && responselocation(location, reqbuf) !=3D -1 && responsecontenttype(texthtml) !=3D -1) _AT_@ -307,7 +292,7 @@ responsedir(void) { closedir(d); } else - logerrmsg("client %s requests %s but opendir failed: %s\n", host, reqbu= f, strerror(errno)); + qlog(ERR, "client %s requests %s but opendir failed: %s\n", host, reqbu= f, strerror(errno)); } else responsefile(); /* docindex */ _AT_@ -328,9 +313,9 @@ responsecgi(void) { setenv("SERVER_NAME", reqhost, 1); setenv("SCRIPT_NAME", cgi_script, 1); setenv("REQUEST_URI", reqbuf, 1); - logmsg("CGI SERVER_NAME=3D%s SCRIPT_NAME=3D%s REQUEST_URI=3D%s\n", reqhos= t, cgi_script, reqbuf); + qlog(ERR, "CGI SERVER_NAME=3D%s SCRIPT_NAME=3D%s REQUEST_URI=3D%s\n", req= host, cgi_script, reqbuf); if(chdir(cgi_dir) =3D=3D -1) - logerrmsg("chdir to cgi directory %s failed: %s\n", cgi_dir, strerror(er= rno)); + qlog(ERR, "chdir to cgi directory %s failed: %s\n", cgi_dir, strerror(er= rno)); if((cgi =3D popen(cgi_script, "r"))) { if(responsehdr(HttpOk) =3D=3D -1) return; _AT_@ -343,7 +328,7 @@ responsecgi(void) { pclose(cgi); } else { - logerrmsg("%s requests %s, but cannot run cgi script %s\n", host, cgi_sc= ript, reqbuf); + qlog(ERR, "%s requests %s, but cannot run cgi script %s\n", host, cgi_sc= ript, reqbuf); if(responsehdr(HttpNotFound) !=3D -1 && responsecontenttype(texthtml) !=3D -1) ; _AT_@ -361,7 +346,7 @@ response(void) { =20 for(p =3D reqbuf; *p; p++) if(*p =3D=3D '\\' || (*p =3D=3D '/' && *(p + 1) =3D=3D '.')) { /* don't = serve bogus or hidden files */ - logerrmsg("%s requests bogus or hidden file %s\n", host, reqbuf); + qlog(ERR, "%s requests bogus or hidden file %s\n", host, reqbuf); if(responsehdr(HttpUnauthorized) !=3D -1 && responsecontenttype(texthtml) !=3D -1) ; _AT_@ -371,7 +356,7 @@ response(void) { writetext("\r\n<html><body>401 Unauthorized</body></html>\r\n"); return; } - logmsg("%s requests: %s\n", host, reqbuf); + qlog(MSG, "%s requests: %s\n", host, reqbuf); if(cgi_mode) responsecgi(); else { _AT_@ -390,7 +375,7 @@ request(void) { =20 do { /* MAXBUFLEN byte of reqbuf is emergency 0 terminator */ if((r =3D read(req.fd, reqbuf + offset, MAXBUFLEN - offset)) =3D=3D -1) { - logerrmsg("read: %s\n", strerror(errno)); + qlog(ERR, "read: %s\n", strerror(errno)); return -1; } offset +=3D r; _AT_@ -434,7 +419,7 @@ request(void) { memmove(reqbuf, res, (p - res) + 1); return 0; invalid_request: - logerrmsg("%s performs invalid request %s\n", host, reqbuf); + qlog(ERR, "%s performs invalid request %s\n", host, reqbuf); return -1; } =20 _AT_@ -448,7 +433,7 @@ serve(int fd) { salen =3D sizeof sa; if((req.fd =3D accept(fd, &sa, &salen)) =3D=3D -1) { /* el cheapo socket release */ - logerrmsg("cannot accept: %s, sleep a second...\n", strerror(errno)); + qlog(ERR, "cannot accept: %s, sleep a second...\n", strerror(errno)); sleep(1); continue; } _AT_@ -465,10 +450,10 @@ serve(int fd) { close(req.fd); exit(EXIT_SUCCESS); } else if (result =3D=3D -1) - logerrmsg("fork failed: %s\n", strerror(errno)); + qlog(ERR, "fork failed: %s\n", strerror(errno)); close(req.fd); } - logmsg("shutting down\n"); + qlog(MSG, "shutting down\n"); } =20 void _AT_@ -488,7 +473,7 @@ sighandler(int sig) { case SIGQUIT: case SIGABRT: case SIGTERM: - logerrmsg("received signal %s, closing down\n", signame[sig] ? signame[s= ig] : ""); + qlog(ERR, "received signal %s, closing down\n", signame[sig] ? signame[s= ig] : ""); close(fd); running =3D 0; break; _AT_@ -518,17 +503,17 @@ main(int argc, char *argv[]) { /* arguments */ for(i =3D 1; i < argc; i++) if(!strcmp(argv[i], "-v")) - die("quark-"VERSION", =C2=A9 2009-2011 Anselm R Garbe\n"); + qlog(DIE, "quark-"VERSION", =C2=A9 2009-2011 Anselm R Garbe\n"); else - die("usage: quark [-v]\n"); + qlog(DIE, "usage: quark [-v]\n"); =20 /* sanity checks */ if(user) if(!(upwd =3D getpwnam(user))) - die("error: invalid user %s\n", user); + qlog(DIE, "error: invalid user %s\n", user); if(group) if(!(gpwd =3D getgrnam(group))) - die("error: invalid group %s\n", group); + qlog(DIE, "error: invalid group %s\n", group); =20 signal(SIGCHLD, sighandler); signal(SIGHUP, sighandler); _AT_@ -545,20 +530,20 @@ main(int argc, char *argv[]) { hints.ai_socktype =3D SOCK_STREAM; hints.ai_flags =3D AI_PASSIVE; if((i =3D getaddrinfo(servername, serverport, &hints, &ai))) - die("error: getaddrinfo: %s\n", gai_strerror(i)); + qlog(DIE, "error: getaddrinfo: %s\n", gai_strerror(i)); if((fd =3D socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) =3D= =3D -1) { freeaddrinfo(ai); - die("error: socket: %s\n", strerror(errno)); + qlog(DIE, "error: socket: %s\n", strerror(errno)); } if(bind(fd, ai->ai_addr, ai->ai_addrlen) =3D=3D -1) { close(fd); freeaddrinfo(ai); - die("error: bind: %s\n", strerror(errno)); + qlog(DIE, "error: bind: %s\n", strerror(errno)); } if(listen(fd, SOMAXCONN) =3D=3D -1) { close(fd); freeaddrinfo(ai); - die("error: listen: %s\n", strerror(errno)); + qlog(DIE, "error: listen: %s\n", strerror(errno)); } =20 if(!strcmp(serverport, "80")) _AT_@ -568,27 +553,27 @@ main(int argc, char *argv[]) { if(i >=3D sizeof location) { close(fd); freeaddrinfo(ai); - die("error: location too long\n"); + qlog(DIE, "error: location too long\n"); } =20 if(chdir(docroot) =3D=3D -1) - die("error: chdir %s: %s\n", docroot, strerror(errno)); + qlog(DIE, "error: chdir %s: %s\n", docroot, strerror(errno)); if(chroot(".") =3D=3D -1) - die("error: chroot .: %s\n", strerror(errno)); + qlog(DIE, "error: chroot .: %s\n", strerror(errno)); =20 if(gpwd) if(setgid(gpwd->gr_gid) =3D=3D -1) - die("error: cannot set group id\n"); + qlog(DIE, "error: cannot set group id\n"); if(upwd) if(setuid(upwd->pw_uid) =3D=3D -1) - die("error: cannot set user id\n"); + qlog(DIE, "error: cannot set user id\n"); =20 if(getuid() =3D=3D 0) - die("error: won't run with root permissions, choose another user\n"); + qlog(DIE, "error: won't run with root permissions, choose another user\n= "); if(getgid() =3D=3D 0) - die("error: won't run with root permissions, choose another group\n"); + qlog(DIE, "error: won't run with root permissions, choose another group\= n"); =20 - logmsg("listening on %s:%s using %s as root directory\n", servername, ser= verport, docroot); + qlog(MSG, "listening on %s:%s using %s as root directory\n", servername, = serverport, docroot); =20 serve(fd); /* main loop */ freeaddrinfo(ai); --=20 1.8.3.2 --Multipart=_Mon__3_Mar_2014_21_53_20_+0100_zSPz5Di.zhUG8ejo--Received on Mon Sep 17 2001 - 00:00:00 CEST
This archive was generated by hypermail 2.3.0 : Tue Mar 04 2014 - 00:00:22 CET