commit 1ab31939db39a302bbda11d47d51607aa45c3f41
parent d9fa317220c824b0a1f7950fda760f45f9db3905
Author: Santtu Lakkala <inz@inz.fi>
Date: Sat, 15 May 2021 01:19:43 +0300
Improve error reporting
Diffstat:
M | main.c | | | 33 | ++++++++++++++++++++++----------- |
1 file changed, 22 insertions(+), 11 deletions(-)
diff --git a/main.c b/main.c
@@ -1208,7 +1208,7 @@ static void child_timeout(EV_P_ ev_timer *w, int revents)
client_close(EV_A_ c);
}
-static void listen_cb (EV_P_ ev_io *w, int revents)
+static void listen_cb(EV_P_ ev_io *w, int revents)
{
struct listener *l = (struct listener *)w;
struct client *c = malloc(sizeof(*c));
@@ -1233,11 +1233,17 @@ static void listen_cb (EV_P_ ev_io *w, int revents)
ev_io_start(EV_A_ &c->watcher);
}
-void usage(void)
+static void usage(void)
{
exit(1);
}
+static void croak(const char *s)
+{
+ perror(s);
+ exit(1);
+}
+
int main (int argc, char *argv[])
{
#ifdef USE_TLS
@@ -1306,7 +1312,7 @@ int main (int argc, char *argv[])
}
if (getaddrinfo(bindto, port, &hints, &addrs))
- usage();
+ croak("Resolving bind address failed");
for (ai = addrs; ai; ai = ai->ai_next) {
lfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
@@ -1319,16 +1325,17 @@ int main (int argc, char *argv[])
if (!bind(lfd, ai->ai_addr, ai->ai_addrlen))
break;
- perror("Bind failed");
close(lfd);
}
+ if (!ai)
+ croak("Bind failed");
+
freeaddrinfo(addrs);
- if (listen(lfd, 10)) {
- perror("Listen failed");
- }
+ if (listen(lfd, 10))
+ croak("Listen failed");
#ifdef USE_TLS
if (keyfile && certfile) {
@@ -1344,14 +1351,18 @@ int main (int argc, char *argv[])
if (group) {
struct group *g = getgrnam(group);
- if (!g || setgid(g->gr_gid))
- usage();
+ if (!g)
+ croak("No such group");
+ if (setgid(g->gr_gid))
+ croak("setgid failed");
}
if (user) {
struct passwd *u = getpwnam(user);
- if (!u || setuid(u->pw_uid))
- usage();
+ if (!u)
+ croak("No such user");
+ if (setuid(u->pw_uid))
+ croak("setuid failed");
}
if (dofork) {