Coding style

This commit is contained in:
FRIGN 2014-08-08 16:46:01 +02:00
parent f915f82a40
commit 10416a352b

43
quark.c
View file

@ -116,10 +116,8 @@ atomiclog(int fd, const char *errstr, va_list ap) {
static char buf[512]; static char buf[512];
int n; int n;
/* /* assemble the message in buf and write it in one pass
assemble the message in buf and write it in one pass to avoid interleaved concurrent writes on a shared fd. */
to avoid interleaved concurrent writes on a shared fd.
*/
n = snprintf(buf, sizeof buf, "%s: ", tstamp()); n = snprintf(buf, sizeof buf, "%s: ", tstamp());
n += vsnprintf(buf + n, sizeof buf - n, errstr, ap); n += vsnprintf(buf + n, sizeof buf - n, errstr, ap);
if (n >= sizeof buf) if (n >= sizeof buf)
@ -197,8 +195,7 @@ responsefile(void) {
return; return;
if (req.type == GET) if (req.type == GET)
writetext("\r\n<html><body>404 Not Found</body></html>\r\n"); writetext("\r\n<html><body>404 Not Found</body></html>\r\n");
} } else {
else {
/* check if modified */ /* check if modified */
t = st.st_mtim.tv_sec; t = st.st_mtim.tv_sec;
memcpy(mod, asctime(gmtime(&t)), 24); memcpy(mod, asctime(gmtime(&t)), 24);
@ -279,13 +276,13 @@ responsedir(void) {
if ((d = opendir(reqbuf))) { if ((d = opendir(reqbuf))) {
responsedirdata(d); responsedirdata(d);
closedir(d); closedir(d);
} } else {
else
logerrmsg("client %s requests %s but opendir failed: %s\n", host, reqbuf, strerror(errno)); logerrmsg("client %s requests %s but opendir failed: %s\n", host, reqbuf, strerror(errno));
} }
else } else {
responsefile(); /* docindex */ responsefile(); /* docindex */
} }
}
void void
responsecgi(void) { responsecgi(void) {
@ -315,8 +312,7 @@ responsecgi(void) {
} }
} }
pclose(cgi); pclose(cgi);
} } else {
else {
logerrmsg("%s requests %s, but cannot run cgi script %s\n", host, cgi_script, reqbuf); logerrmsg("%s requests %s, but cannot run cgi script %s\n", host, cgi_script, reqbuf);
if (putresentry(HEADER, HttpNotFound, tstamp()) if (putresentry(HEADER, HttpNotFound, tstamp())
|| putresentry(CONTENTTYPE, texthtml)) || putresentry(CONTENTTYPE, texthtml))
@ -336,17 +332,15 @@ response(void) {
logerrmsg("%s requests bogus or hidden file %s\n", host, reqbuf); logerrmsg("%s requests bogus or hidden file %s\n", host, reqbuf);
if (putresentry(HEADER, HttpUnauthorized, tstamp()) if (putresentry(HEADER, HttpUnauthorized, tstamp())
|| putresentry(CONTENTTYPE, texthtml)) || putresentry(CONTENTTYPE, texthtml))
;
else
return; return;
if (req.type == GET) if (req.type == GET)
writetext("\r\n<html><body>401 Unauthorized</body></html>\r\n"); writetext("\r\n<html><body>401 Unauthorized</body></html>\r\n");
return; return;
} }
logmsg("%s requests: %s\n", host, reqbuf); logmsg("%s requests: %s\n", host, reqbuf);
if(cgi_mode) if (cgi_mode) {
responsecgi(); responsecgi();
else { } else {
if (stat(reqbuf, &st) != -1 && S_ISDIR(st.st_mode)) if (stat(reqbuf, &st) != -1 && S_ISDIR(st.st_mode))
responsedir(); responsedir();
else else
@ -380,14 +374,14 @@ request(void) {
int r; int r;
size_t offset = 0; size_t offset = 0;
/* read request into reqbuf */ /* read request into reqbuf (MAXBUFLEN byte of reqbuf is emergency 0 terminator */
for (; r > 0 && offset < MAXBUFLEN && (!strstr(reqbuf, "\r\n") || !strstr(reqbuf, "\n"));) { for (; r > 0 && offset < MAXBUFLEN && (!strstr(reqbuf, "\r\n") || !strstr(reqbuf, "\n"));) {
if ((r = read(req.fd, reqbuf + offset, MAXBUFLEN - offset)) == -1) { if ((r = read(req.fd, reqbuf + offset, MAXBUFLEN - offset)) == -1) {
logerrmsg("read: %s\n", strerror(errno)); logerrmsg("read: %s\n", strerror(errno));
return -1; return -1;
} }
offset += r; offset += r;
reqbuf[offset] = 0; /* MAXBUFLEN byte of reqbuf is emergency 0 terminator */ reqbuf[offset] = 0;
} }
/* extract host and mod */ /* extract host and mod */
@ -452,8 +446,9 @@ serve(int fd) {
shutdown(req.fd, SHUT_WR); shutdown(req.fd, SHUT_WR);
close(req.fd); close(req.fd);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} else if (result == -1) } else if (result == -1) {
logerrmsg("fork failed: %s\n", strerror(errno)); logerrmsg("fork failed: %s\n", strerror(errno));
}
close(req.fd); close(req.fd);
} }
logmsg("shutting down\n"); logmsg("shutting down\n");
@ -495,11 +490,9 @@ main(int argc, char *argv[]) {
} ARGEND; } ARGEND;
/* sanity checks */ /* sanity checks */
if(user) if (user && !(upwd = getpwnam(user)))
if(!(upwd = getpwnam(user)))
die("error: invalid user %s\n", user); die("error: invalid user %s\n", user);
if(group) if (group && !(gpwd = getgrnam(group)))
if(!(gpwd = getgrnam(group)))
die("error: invalid group %s\n", group); die("error: invalid group %s\n", group);
signal(SIGCHLD, sighandler); signal(SIGCHLD, sighandler);
@ -547,11 +540,9 @@ main(int argc, char *argv[]) {
if (chroot(".") == -1) if (chroot(".") == -1)
die("error: chroot .: %s\n", strerror(errno)); die("error: chroot .: %s\n", strerror(errno));
if(gpwd) if (gpwd && setgid(gpwd->gr_gid) == -1)
if(setgid(gpwd->gr_gid) == -1)
die("error: cannot set group id\n"); die("error: cannot set group id\n");
if(upwd) if (upwd && setuid(upwd->pw_uid) == -1)
if(setuid(upwd->pw_uid) == -1)
die("error: cannot set user id\n"); die("error: cannot set user id\n");
if (getuid() == 0) if (getuid() == 0)