audioc: Make all fds non-blocking.
[paraslash.git] / daemon.c
index a2fa1ca..bb75478 100644 (file)
--- a/daemon.c
+++ b/daemon.c
@@ -27,28 +27,36 @@ void daemon_init(void)
        pid_t pid;
        int null;
 
-       PARA_INFO_LOG("%s", "daemonizing\n");
-       if ((pid = fork()) < 0) {
-               PARA_EMERG_LOG("%s", "failed to init daemon\n");
-               exit(EXIT_FAILURE);
-       } else if (pid)
+       PARA_INFO_LOG("daemonizing\n");
+       pid = fork();
+       if (pid < 0)
+               goto err;
+       if (pid)
                exit(EXIT_SUCCESS); /* parent exits */
-       /* child */
-       setsid(); /* become session leader */
-       chdir("/");
+       /* become session leader */
+       if (setsid() < 0)
+               goto err;
+       if (chdir("/") < 0)
+               goto err;
        umask(0);
-
        null = open("/dev/null", O_RDONLY);
        if (null < 0)
-               exit(EXIT_FAILURE);
-       dup2(null, STDIN_FILENO);
-       dup2(null, STDOUT_FILENO);
-       dup2(null, STDERR_FILENO);
+               goto err;
+       if (dup2(null, STDIN_FILENO) < 0)
+               goto err;
+       if (dup2(null, STDOUT_FILENO) < 0)
+               goto err;
+       if (dup2(null, STDERR_FILENO) < 0)
+               goto err;
        close(null);
+       return;
+err:
+       PARA_EMERG_LOG("fatal: %s\n", strerror(errno));
+       exit(EXIT_FAILURE);
 }
 
 /**
- * fopen() a file in append mode.
+ * fopen() the given file in append mode.
  *
  * \param logfile_name The name of the file to open.
  *
@@ -58,11 +66,11 @@ FILE *open_log(const char *logfile_name)
 {
        FILE *logfile;
 
-       if (!logfile_name)
-               return NULL;
-       if (!(logfile = fopen(logfile_name, "a"))) {
-               PARA_EMERG_LOG("can not open %s, uid: %d\n", logfile_name,
-                       (int)getuid());
+       assert(logfile_name);
+       logfile = fopen(logfile_name, "a");
+       if (!logfile) {
+               PARA_EMERG_LOG("can not open %s: %s\n", logfile_name,
+                       strerror(errno));
                exit(EXIT_FAILURE);
        }
        setlinebuf(logfile);
@@ -80,7 +88,7 @@ void close_log(FILE* logfile)
 {
        if (!logfile)
                return;
-       PARA_INFO_LOG("%s", "closing logfile\n");
+       PARA_INFO_LOG("closing logfile\n");
        fclose(logfile);
 }
 
@@ -118,27 +126,28 @@ void para_drop_privileges(const char *username, const char *groupname)
        if (groupname) {
                struct group *g = getgrnam(groupname);
                if (!g) {
-                       PARA_EMERG_LOG("failed to get group %s\n", groupname);
+                       PARA_EMERG_LOG("failed to get group %s: %s\n",
+                               groupname, strerror(errno));
                        exit(EXIT_FAILURE);
                }
                if (setgid(g->gr_gid) < 0) {
-                       PARA_EMERG_LOG("failed to set group id %d (%s)\n",
+                       PARA_EMERG_LOG("failed to set group id %d: %s\n",
                                (int)g->gr_gid, strerror(errno));
                        exit(EXIT_FAILURE);
                }
        }
        if (!username) {
-               PARA_EMERG_LOG("%s", "root privileges, but no user option given\n");
+               PARA_EMERG_LOG("root privileges, but no user option given\n");
                exit(EXIT_FAILURE);
        }
        tmp = para_strdup(username);
        p = getpwnam(tmp);
        free(tmp);
        if (!p) {
-               PARA_EMERG_LOG("%s", "no such user\n");
+               PARA_EMERG_LOG("%s: no such user\n", username);
                exit(EXIT_FAILURE);
        }
-       PARA_INFO_LOG("%s", "dropping root privileges\n");
+       PARA_INFO_LOG("dropping root privileges\n");
        setuid(p->pw_uid);
        PARA_DEBUG_LOG("uid: %d, euid: %d\n", (int)getuid(), (int)geteuid());
 }