Rectify error-handling

This commit is contained in:
FRIGN 2014-08-11 15:23:48 +02:00
parent 00e2aa8da6
commit 54a95cd229

70
quark.c
View file

@ -513,21 +513,21 @@ main(int argc, char *argv[]) {
hints.ai_family = AF_INET; hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; hints.ai_flags = AI_PASSIVE;
if ((i = getaddrinfo(servername, serverport, &hints, &ai))) if ((i = getaddrinfo(servername, serverport, &hints, &ai))) {
die("error\tgetaddrinfo: %s\n", gai_strerror(i)); logerrmsg("error\tgetaddrinfo: %s\n", gai_strerror(i));
goto err;
}
if ((fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) == -1) { if ((fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) == -1) {
freeaddrinfo(ai); logerrmsg("error\tsocket: %s\n", strerror(errno));
die("error\tsocket: %s\n", strerror(errno)); goto err;
} }
if (bind(fd, ai->ai_addr, ai->ai_addrlen) == -1) { if (bind(fd, ai->ai_addr, ai->ai_addrlen) == -1) {
close(fd); logerrmsg("error\tbind: %s\n", strerror(errno));
freeaddrinfo(ai); goto err;
die("error\tbind: %s\n", strerror(errno));
} }
if (listen(fd, SOMAXCONN) == -1) { if (listen(fd, SOMAXCONN) == -1) {
close(fd); logerrmsg("error\tlisten: %s\n", strerror(errno));
freeaddrinfo(ai); goto err;
die("error\tlisten: %s\n", strerror(errno));
} }
if (!strcmp(serverport, "80")) if (!strcmp(serverport, "80"))
@ -535,29 +535,47 @@ main(int argc, char *argv[]) {
else else
i = snprintf(location, sizeof location, "http://%s:%s", servername, serverport); i = snprintf(location, sizeof location, "http://%s:%s", servername, serverport);
if (i >= sizeof location) { if (i >= sizeof location) {
close(fd); logerrmsg("error\tlocation too long\n");
freeaddrinfo(ai); goto err;
die("error\tlocation too long\n");
} }
if (chdir(docroot) == -1) if (chdir(docroot) == -1) {
die("error\tchdir %s: %s\n", docroot, strerror(errno)); logerrmsg("error\tchdir %s: %s\n", docroot, strerror(errno));
if (chroot(".") == -1) goto err;
die("error\tchroot .: %s\n", strerror(errno)); }
if (chroot(".") == -1) {
logerrmsg("error\tchroot .: %s\n", strerror(errno));
goto err;
}
if (gpwd && setgid(gpwd->gr_gid) == -1) if (gpwd && setgid(gpwd->gr_gid) == -1) {
die("error\tcannot set group id\n"); logerrmsg("error\tcannot set group id\n");
if (upwd && setuid(upwd->pw_uid) == -1) goto err;
die("error\tcannot set user id\n"); }
if (upwd && setuid(upwd->pw_uid) == -1) {
logerrmsg("error\tcannot set user id\n");
goto err;
}
if (getuid() == 0) if (getuid() == 0) {
die("error\twon't run with root permissions, choose another user\n"); logerrmsg("error\twon't run with root permissions, choose another user\n");
if (getgid() == 0) goto err;
die("error\twon't run with root permissions, choose another group\n"); }
if (getgid() == 0) {
logerrmsg("error\twon't run with root permissions, choose another group\n");
goto err;
}
logmsg("ready\t%s:%s\t%s\n", servername, serverport, docroot); logmsg("ready\t%s:%s\t%s\n", servername, serverport, docroot);
serve(fd); /* main loop */ serve(fd); /* main loop */
close(fd);
freeaddrinfo(ai); freeaddrinfo(ai);
return 0; return EXIT_SUCCESS;
err:
if (fd)
close(fd);
if (ai)
freeaddrinfo(ai);
return EXIT_FAILURE;
} }