[PATCH] Cleaning up the log-facility

From: FRIGN <dev_AT_frign.de>
Date: Mon, 3 Mar 2014 19:30:42 +0100

---
 quark.c | 113 +++++++++++++++++++++++++++++-------------------------------=
----
 1 file changed, 51 insertions(+), 62 deletions(-)
diff --git a/quark.c b/quark.c
index 234f6e2..971c4d7 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_@ -61,9 +67,7 @@ static const char texthtml[]         =3D "text/html";
 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 log(int type, const char *errstr, ...);
 static void response(void);
 static void responsecgi(void);
 static void responsedir(void);
_AT_@ -92,7 +96,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);
+			log(ERR, "client %s closed connection\n", host);
 			return -1;
 		}
 		offset +=3D r;
_AT_@ -122,31 +126,16 @@ atomiclog(int fd, const char *errstr, va_list ap) {
 }
=20
 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, ...) {
+log(int type, const char *errstr, ...) {
 	va_list ap;
=20
 	va_start(ap, errstr);
-	atomiclog(STDERR_FILENO, errstr, ap);
+	atomiclog((type =3D=3D MSG)?STDOUT_FILENO:STDERR_FILENO, errstr, ap);
 	va_end(ap);
-}
-
-void
-die(const char *errstr, ...) {
-	va_list ap;
-
-	va_start(ap, errstr);
-	atomiclog(STDERR_FILENO, errstr, ap);
-	va_end(ap);
-	exit(EXIT_FAILURE);
+=09
+	if(type =3D=3D DIE){
+		exit(EXIT_FAILURE);
+	};
 }
=20
 int
_AT_@ -158,7 +147,7 @@ responsehdr(const char *status) {
 		"Server: quark-"VERSION"\r\n",
 		status, tstamp()) >=3D MAXBUFLEN)
 	{
-		logerrmsg("snprintf failed, buffer size exceeded");
+		log(ERR, "snprintf failed, buffer size exceeded");
 		return -1;
 	}
 	return writetext(resbuf);
_AT_@ -170,7 +159,7 @@ responsecontentlen(off_t size) {
 		"Content-Length: %lu\r\n",
 		size) >=3D MAXBUFLEN)
 	{
-		logerrmsg("snprintf failed, buffer sizeof exceeded");
+		log(ERR, "snprintf failed, buffer sizeof exceeded");
 		return -1;
 	}
 	return writetext(resbuf);
_AT_@ -182,7 +171,7 @@ responselocation(const char *location, const char *path=
info) {
 		"Location: %s%s\r\n",
 		location, pathinfo) >=3D MAXBUFLEN)
 	{
-		logerrmsg("snprintf failed, buffer sizeof exceeded");
+		log(ERR, "snprintf failed, buffer sizeof exceeded");
 		return -1;
 	}
 	return writetext(resbuf);
_AT_@ -194,7 +183,7 @@ responsecontenttype(const char *mimetype) {
 		"Content-Type: %s\r\n",
 		mimetype) >=3D MAXBUFLEN)
 	{
-		logerrmsg("snprintf failed, buffer sizeof exceeded");
+		log(ERR, "snprintf failed, buffer sizeof exceeded");
 		return -1;
 	}
 	return writetext(resbuf);
_AT_@ -207,9 +196,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));
+			log(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));
+		log(ERR, "error reading from file: %s\n", strerror(errno));
 }
=20
 void
_AT_@ -220,7 +209,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);
+		log(ERR, "%s requests unknown path %s\n", host, reqbuf);
 		if(responsehdr(HttpNotFound) !=3D -1
 		&& responsecontenttype(texthtml) !=3D -1)
 			;
_AT_@ -268,7 +257,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");
+				log(ERR, "snprintf failed, buffer sizeof exceeded");
 				return;
 			}
 			if(writetext(resbuf) =3D=3D -1)
_AT_@ -287,7 +276,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);
+		log(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 +296,7 @@ responsedir(void) {
 			closedir(d);
 		}
 		else
-			logerrmsg("client %s requests %s but opendir failed: %s\n", host, reqbu=
f, strerror(errno));
+			log(ERR, "client %s requests %s but opendir failed: %s\n", host, reqbuf=
, strerror(errno));
 	}
 	else
 		responsefile(); /* docindex */
_AT_@ -328,9 +317,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);
+	log(ERR, "CGI SERVER_NAME=3D%s SCRIPT_NAME=3D%s REQUEST_URI=3D%s\n", reqh=
ost, cgi_script, reqbuf);
 	if(chdir(cgi_dir) =3D=3D -1)
-		logerrmsg("chdir to cgi directory %s failed: %s\n", cgi_dir, strerror(er=
rno));
+		log(ERR, "chdir to cgi directory %s failed: %s\n", cgi_dir, strerror(err=
no));
 	if((cgi =3D popen(cgi_script, "r"))) {
 		if(responsehdr(HttpOk) =3D=3D -1)
 			return;
_AT_@ -343,7 +332,7 @@ responsecgi(void) {
 		pclose(cgi);
 	}
 	else {
-		logerrmsg("%s requests %s, but cannot run cgi script %s\n", host, cgi_sc=
ript, reqbuf);
+		log(ERR, "%s requests %s, but cannot run cgi script %s\n", host, cgi_scr=
ipt, reqbuf);
 		if(responsehdr(HttpNotFound) !=3D -1
 		&& responsecontenttype(texthtml) !=3D -1)
 			;
_AT_@ -361,7 +350,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);
+			log(ERR, "%s requests bogus or hidden file %s\n", host, reqbuf);
 			if(responsehdr(HttpUnauthorized) !=3D -1
 			&& responsecontenttype(texthtml) !=3D -1)
 				;
_AT_@ -371,7 +360,7 @@ response(void) {
 				writetext("\r\n<html><body>401 Unauthorized</body></html>\r\n");
 			return;
 		}
-	logmsg("%s requests: %s\n", host, reqbuf);
+	log(MSG, "%s requests: %s\n", host, reqbuf);
 	if(cgi_mode)
 		responsecgi();
 	else {
_AT_@ -390,7 +379,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));
+			log(ERR, "read: %s\n", strerror(errno));
 			return -1;
 		}
 		offset +=3D r;
_AT_@ -434,7 +423,7 @@ request(void) {
 	memmove(reqbuf, res, (p - res) + 1);
 	return 0;
 invalid_request:
-	logerrmsg("%s performs invalid request %s\n", host, reqbuf);
+	log(ERR, "%s performs invalid request %s\n", host, reqbuf);
 	return -1;
 }
=20
_AT_@ -448,7 +437,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));
+			log(ERR, "cannot accept: %s, sleep a second...\n", strerror(errno));
 			sleep(1);
 			continue;
 		}
_AT_@ -465,10 +454,10 @@ serve(int fd) {
 			close(req.fd);
 			exit(EXIT_SUCCESS);
 		} else if (result =3D=3D -1)
-			logerrmsg("fork failed: %s\n", strerror(errno));
+			log(ERR, "fork failed: %s\n", strerror(errno));
 		close(req.fd);
 	}
-	logmsg("shutting down\n");
+	log(MSG, "shutting down\n");
 }
=20
 void
_AT_@ -488,7 +477,7 @@ sighandler(int sig) {
 	case SIGQUIT:
 	case SIGABRT:
 	case SIGTERM:
-		logerrmsg("received signal %s, closing down\n", signame[sig] ? signame[s=
ig] : "");
+		log(ERR, "received signal %s, closing down\n", signame[sig] ? signame[si=
g] : "");
 		close(fd);
 		running =3D 0;
 		break;
_AT_@ -518,17 +507,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");
+			log(DIE, "quark-"VERSION", =C2=A9 2009-2011 Anselm R Garbe\n");
 		else
-			die("usage: quark [-v]\n");
+			log(DIE, "usage: quark [-v]\n");
=20
 	/* sanity checks */
 	if(user)
 		if(!(upwd =3D getpwnam(user)))
-			die("error: invalid user %s\n", user);
+			log(DIE, "error: invalid user %s\n", user);
 	if(group)
 		if(!(gpwd =3D getgrnam(group)))
-			die("error: invalid group %s\n", group);
+			log(DIE, "error: invalid group %s\n", group);
=20
 	signal(SIGCHLD, sighandler);
 	signal(SIGHUP, sighandler);
_AT_@ -545,20 +534,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));
+		log(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));
+		log(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));
+		log(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));
+		log(DIE, "error: listen: %s\n", strerror(errno));
 	}
=20
 	if(!strcmp(serverport, "80"))
_AT_@ -568,27 +557,27 @@ main(int argc, char *argv[]) {
 	if(i >=3D sizeof location) {
 		close(fd);
 		freeaddrinfo(ai);
-		die("error: location too long\n");
+		log(DIE, "error: location too long\n");
 	}
=20
 	if(chdir(docroot) =3D=3D -1)
-		die("error: chdir %s: %s\n", docroot, strerror(errno));
+		log(DIE, "error: chdir %s: %s\n", docroot, strerror(errno));
 	if(chroot(".") =3D=3D -1)
-		die("error: chroot .: %s\n", strerror(errno));
+		log(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");
+			log(DIE, "error: cannot set group id\n");
 	if(upwd)
 		if(setuid(upwd->pw_uid) =3D=3D -1)
-			die("error: cannot set user id\n");
+			log(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");
+		log(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");
+		log(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);
+	log(MSG, "listening on %s:%s using %s as root directory\n", servername, s=
erverport, docroot);
=20
 	serve(fd); /* main loop */
 	freeaddrinfo(ai);
--=20
1.8.3.2
--Multipart=_Mon__3_Mar_2014_19_39_13_+0100_pmUVVkxf+Gg5bEgz--
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Mon Mar 03 2014 - 21:48:02 CET