commit 54a95cd2290dbf9a7ecd91598a03a418c6f43b04
Author: FRIGN <dev_AT_frign.de>
Date: Mon Aug 11 15:23:48 2014 +0200
Rectify error-handling
diff --git a/quark.c b/quark.c
index dadc78e..52e637f 100644
--- a/quark.c
+++ b/quark.c
_AT_@ -513,21 +513,21 @@ main(int argc, char *argv[]) {
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
- if ((i = getaddrinfo(servername, serverport, &hints, &ai)))
- die("error getaddrinfo: %s
", gai_strerror(i));
+ if ((i = getaddrinfo(servername, serverport, &hints, &ai))) {
+ logerrmsg("error getaddrinfo: %s
", gai_strerror(i));
+ goto err;
+ }
if ((fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) == -1) {
- freeaddrinfo(ai);
- die("error socket: %s
", strerror(errno));
+ logerrmsg("error socket: %s
", strerror(errno));
+ goto err;
}
if (bind(fd, ai->ai_addr, ai->ai_addrlen) == -1) {
- close(fd);
- freeaddrinfo(ai);
- die("error bind: %s
", strerror(errno));
+ logerrmsg("error bind: %s
", strerror(errno));
+ goto err;
}
if (listen(fd, SOMAXCONN) == -1) {
- close(fd);
- freeaddrinfo(ai);
- die("error listen: %s
", strerror(errno));
+ logerrmsg("error listen: %s
", strerror(errno));
+ goto err;
}
if (!strcmp(serverport, "80"))
_AT_@ -535,29 +535,47 @@ main(int argc, char *argv[]) {
else
i = snprintf(location, sizeof location, "
http://%s:%s", servername, serverport);
if (i >= sizeof location) {
- close(fd);
- freeaddrinfo(ai);
- die("error location too long
");
+ logerrmsg("error location too long
");
+ goto err;
}
- if (chdir(docroot) == -1)
- die("error chdir %s: %s
", docroot, strerror(errno));
- if (chroot(".") == -1)
- die("error chroot .: %s
", strerror(errno));
+ if (chdir(docroot) == -1) {
+ logerrmsg("error chdir %s: %s
", docroot, strerror(errno));
+ goto err;
+ }
+ if (chroot(".") == -1) {
+ logerrmsg("error chroot .: %s
", strerror(errno));
+ goto err;
+ }
- if (gpwd && setgid(gpwd->gr_gid) == -1)
- die("error cannot set group id
");
- if (upwd && setuid(upwd->pw_uid) == -1)
- die("error cannot set user id
");
+ if (gpwd && setgid(gpwd->gr_gid) == -1) {
+ logerrmsg("error cannot set group id
");
+ goto err;
+ }
+ if (upwd && setuid(upwd->pw_uid) == -1) {
+ logerrmsg("error cannot set user id
");
+ goto err;
+ }
- if (getuid() == 0)
- die("error won't run with root permissions, choose another user
");
- if (getgid() == 0)
- die("error won't run with root permissions, choose another group
");
+ if (getuid() == 0) {
+ logerrmsg("error won't run with root permissions, choose another user
");
+ goto err;
+ }
+ if (getgid() == 0) {
+ logerrmsg("error won't run with root permissions, choose another group
");
+ goto err;
+ }
logmsg("ready %s:%s %s
", servername, serverport, docroot);
serve(fd); /* main loop */
+ close(fd);
freeaddrinfo(ai);
- return 0;
+ return EXIT_SUCCESS;
+err:
+ if (fd)
+ close(fd);
+ if (ai)
+ freeaddrinfo(ai);
+ return EXIT_FAILURE;
}
Received on Mon Aug 11 2014 - 15:22:10 CEST