]> the.earth.li Git - onak.git/commitdiff
Add error checking when trying to daemonize in keyd
authorJonathan McDowell <noodles@earth.li>
Mon, 6 Jan 2014 18:58:11 +0000 (10:58 -0800)
committerJonathan McDowell <noodles@earth.li>
Mon, 6 Jan 2014 18:58:11 +0000 (10:58 -0800)
We weren't checking the return from freopen, which causes warnings when
compiling with -D_FORTIFY_SOURCE=2. Do so, and also for the setsid().

keyd.c

diff --git a/keyd.c b/keyd.c
index b8b8ad37e6bf1eff136eb089807687cc51650958..8b61d0cece78569b0ca8079c1902f4d4e4db5a38 100644 (file)
--- a/keyd.c
+++ b/keyd.c
@@ -65,11 +65,35 @@ void daemonize(void)
                exit(EXIT_SUCCESS);
        }
 
-       setsid();
+       if (setsid() == -1) {
+               logthing(LOGTHING_CRITICAL,
+                       "Couldn't set process group leader: %d (%s)",
+                       errno,
+                       strerror(errno));
+               exit(EXIT_FAILURE);
+       }
 
-       freopen("/dev/null", "r", stdin);
-       freopen("/dev/null", "w", stdout);
-       freopen("/dev/null", "w", stderr);
+       if (!freopen("/dev/null", "r", stdin)) {
+               logthing(LOGTHING_CRITICAL,
+                       "Couldn't reopen stdin to NULL: %d (%s)",
+                       errno,
+                       strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+       if (!freopen("/dev/null", "w", stdout)) {
+               logthing(LOGTHING_CRITICAL,
+                       "Couldn't reopen stdout to NULL: %d (%s)",
+                       errno,
+                       strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+       if (!freopen("/dev/null", "w", stderr)) {
+               logthing(LOGTHING_CRITICAL,
+                       "Couldn't reopen stderr to NULL: %d (%s)",
+                       errno,
+                       strerror(errno));
+               exit(EXIT_FAILURE);
+       }
 
        return;
 }