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