filter_chain.c: Cosmetics.
[paraslash.git] / daemon.c
index d3a43b9a2de32fe50f2a42009cbcfb7830972985..bb75478adae6f639b314fa7aa98a7ad2c3501e72 100644 (file)
--- a/daemon.c
+++ b/daemon.c
@@ -1,22 +1,10 @@
 /*
- * Copyright (C) 1997-2006 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 1997-2008 Andre Noll <maan@systemlinux.org>
  *
- *     This program is free software; you can redistribute it and/or modify
- *     it under the terms of the GNU General Public License as published by
- *     the Free Software Foundation; either version 2 of the License, or
- *     (at your option) any later version.
- *
- *     This program is distributed in the hope that it will be useful,
- *     but WITHOUT ANY WARRANTY; without even the implied warranty of
- *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *     GNU General Public License for more details.
- *
- *     You should have received a copy of the GNU General Public License
- *     along with this program; if not, write to the Free Software
- *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
-/** \file daemon.c some helpers for programs that detach from the console */
+/** \file daemon.c Some helpers for programs that detach from the console. */
 #include "para.h"
 #include "daemon.h"
 #include <pwd.h>
 #include "string.h"
 
 /**
- * do the usual stuff to become a daemon
+ * Do the usual stuff to become a daemon.
  *
  * Fork, become session leader, dup fd 0, 1, 2 to /dev/null.
  *
- * \sa fork(2), setsid(2), dup(2)
+ * \sa fork(2), setsid(2), dup(2).
  */
 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
+ * \param logfile_name The name of the file to open.
  *
- * Either calls exit() or returns a valid file handle.
+ * \return Either calls exit() or returns a valid file handle.
  */
 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,
-                       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);
@@ -82,37 +78,41 @@ FILE *open_log(const char *logfile_name)
 }
 
 /**
- * close the log file of the daemon
+ * Close the log file of the daemon.
  *
- * \param logfile the log file handle
+ * \param logfile The log file handle.
  *
- * It's OK to call this with logfile == NULL
+ * It's OK to call this with logfile == \p NULL.
  */
 void close_log(FILE* logfile)
 {
        if (!logfile)
                return;
-       PARA_INFO_LOG("%s", "closing logfile\n");
+       PARA_INFO_LOG("closing logfile\n");
        fclose(logfile);
 }
 
 /**
- * log the startup message containing the paraslash version
+ * Log the startup message containing the paraslash version.
  */
 void log_welcome(const char *whoami, int loglevel)
 {
-       PARA_INFO_LOG("welcome to %s " VERSION " ("BUILD_DATE")\n", whoami);
+       PARA_INFO_LOG("welcome to %s " PACKAGE_VERSION " ("BUILD_DATE")\n",
+               whoami);
        PARA_DEBUG_LOG("using loglevel %d\n", loglevel);
 }
 
 /**
- * give up superuser privileges
+ * Give up superuser privileges.
+ *
+ * \param username The user to switch to.
+ * \param groupname The group to switch to.
  *
  * This function returns immediately if not invoked with EUID zero. Otherwise,
  * it tries to obtain the GID of \a groupname and the UID of \a username.  On
  * success, effective and real GID/UID and the saved set-group-ID/set-user-ID
- * are all set accordingly. On errors, an appropriate message is logged and exit()
- * is called to terminate the process.
+ * are all set accordingly. On errors, an appropriate message is logged and
+ * exit() is called to terminate the process.
  *
  * \sa getpwnam(3), getuid(2), setuid(2), getgrnam(2), setgid(2)
  */
@@ -126,67 +126,71 @@ 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",
-                               g->gr_gid, strerror(errno));
+                       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");
-       setuid(p->pw_uid);
-       PARA_DEBUG_LOG("uid: %d, euid: %d\n", getuid(), geteuid());
+       PARA_INFO_LOG("dropping root privileges\n");
        setuid(p->pw_uid);
+       PARA_DEBUG_LOG("uid: %d, euid: %d\n", (int)getuid(), (int)geteuid());
 }
 
 /**
- * set/get the server uptime
+ * Set/get the server uptime.
  *
- * \param set_or_get chose one of the two modes
+ * \param set_or_get Chose one of the two modes.
  *
  * This should be called at startup time with \a set_or_get equal to \p
  * UPTIME_SET which sets the uptime to zero.  Subsequent calls with \a
- * set_or_get equal to \p UPTIME_GET return the number of seconds ellapsed
- * since the last reset.
+ * set_or_get equal to \p UPTIME_GET return the uptime.
+
+ * \return Zero if called with \a set_or_get equal to \p UPTIME_SET, the number
+ * of seconds ellapsed since the last reset otherwise.
  *
- * \sa time(2), difftime(3)
+ * \sa time(2), difftime(3).
  */
 time_t server_uptime(enum uptime set_or_get)
 {
        static time_t startuptime;
        time_t now;
+       double diff;
 
        if (set_or_get == UPTIME_SET) {
                time(&startuptime);
                return 0;
        }
        time(&now);
-       return (time_t) difftime(now, startuptime);
+       diff = difftime(now, startuptime);
+       return (time_t) diff;
 }
 
 /**
- * construct string containing uptime
+ * Construct string containing uptime.
  *
- * The format of the returned string is "days:hours:minutes"
+ * \return A dynamically allocated string of the form "days:hours:minutes".
  *
- * \sa server_uptime
+ * \sa server_uptime.
  */
 __malloc char *uptime_str(void)
 {
-       time_t t = server_uptime(UPTIME_GET);
+       long t = server_uptime(UPTIME_GET);
        return make_message("%li:%02li:%02li", t / 86400,
                (t / 3600) % 24, (t / 60) % 60);
 }