ogg_afh.c: Store the bitrate in KHz rather than in Hz.
[paraslash.git] / daemon.c
index 616fd2c2f9adf7f9afb927ce840a00a0ae01aa97..666b0a5c645cbc0f34b3347a3e021355a6444193 100644 (file)
--- a/daemon.c
+++ b/daemon.c
@@ -1,25 +1,18 @@
 /*
  * Copyright (C) 1997-2006 Andre Noll <maan@systemlinux.org>
  *
 /*
  * Copyright (C) 1997-2006 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 */
 #include "para.h"
 #include "daemon.h"
 #include <pwd.h>
  */
 
 /** \file daemon.c some helpers for programs that detach from the console */
 #include "para.h"
 #include "daemon.h"
 #include <pwd.h>
+
+/* getgrnam() */
+#include <sys/types.h>
+#include <grp.h>
+
 #include "string.h"
 
 /**
 #include "string.h"
 
 /**
@@ -61,15 +54,17 @@ void daemon_init(void)
  *
  * Either calls exit() or returns a valid file handle.
  */
  *
  * Either calls exit() or returns a valid file handle.
  */
-/* might be called from para_log, so we must not use para_log */
 FILE *open_log(const char *logfile_name)
 {
        FILE *logfile;
 
        if (!logfile_name)
                return NULL;
 FILE *open_log(const char *logfile_name)
 {
        FILE *logfile;
 
        if (!logfile_name)
                return NULL;
-       if (!(logfile = fopen(logfile_name, "a")))
+       if (!(logfile = fopen(logfile_name, "a"))) {
+               PARA_EMERG_LOG("can not open %s, uid: %d\n", logfile_name,
+                       getuid());
                exit(EXIT_FAILURE);
                exit(EXIT_FAILURE);
+       }
        setlinebuf(logfile);
        return logfile;
 }
        setlinebuf(logfile);
        return logfile;
 }
@@ -94,27 +89,44 @@ void close_log(FILE* logfile)
  */
 void log_welcome(const char *whoami, int loglevel)
 {
  */
 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
  *
        PARA_DEBUG_LOG("using loglevel %d\n", loglevel);
 }
 
 /**
  * give up superuser privileges
  *
- * This function returns immediately if not invoked with EUID zero.  Otherwise,
- * it tries to obtain the UID for the user specified by \a username and exits
- * if this user was not found. On success, effective and real UID and the saved
- * set-user-ID are all set to the UID of \a username.
+ * \param username the user to switch to
+ * \param groupname the group to switch to
  *
  *
- * \sa getpwnam(3), getuid(2), setuid(2)
+ * 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.
+ *
+ * \sa getpwnam(3), getuid(2), setuid(2), getgrnam(2), setgid(2)
  */
  */
-void para_drop_privileges(const char *username)
+void para_drop_privileges(const char *username, const char *groupname)
 {
        struct passwd *p;
        char *tmp;
 
        if (geteuid())
                return;
 {
        struct passwd *p;
        char *tmp;
 
        if (geteuid())
                return;
+       if (groupname) {
+               struct group *g = getgrnam(groupname);
+               if (!g) {
+                       PARA_EMERG_LOG("failed to get group %s\n", groupname);
+                       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));
+                       exit(EXIT_FAILURE);
+               }
+       }
        if (!username) {
                PARA_EMERG_LOG("%s", "root privileges, but no user option given\n");
                exit(EXIT_FAILURE);
        if (!username) {
                PARA_EMERG_LOG("%s", "root privileges, but no user option given\n");
                exit(EXIT_FAILURE);
@@ -126,10 +138,9 @@ void para_drop_privileges(const char *username)
                PARA_EMERG_LOG("%s", "no such user\n");
                exit(EXIT_FAILURE);
        }
                PARA_EMERG_LOG("%s", "no such user\n");
                exit(EXIT_FAILURE);
        }
-       PARA_NOTICE_LOG("%s", "dropping root privileges\n");
+       PARA_INFO_LOG("%s", "dropping root privileges\n");
        setuid(p->pw_uid);
        PARA_DEBUG_LOG("uid: %d, euid: %d\n", getuid(), geteuid());
        setuid(p->pw_uid);
        PARA_DEBUG_LOG("uid: %d, euid: %d\n", getuid(), geteuid());
-       setuid(p->pw_uid);
 }
 
 /**
 }
 
 /**
@@ -148,13 +159,15 @@ time_t server_uptime(enum uptime set_or_get)
 {
        static time_t startuptime;
        time_t now;
 {
        static time_t startuptime;
        time_t now;
+       double diff;
 
        if (set_or_get == UPTIME_SET) {
                time(&startuptime);
                return 0;
        }
        time(&now);
 
        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;
 }
 
 /**
 }
 
 /**
@@ -170,4 +183,3 @@ __malloc char *uptime_str(void)
        return make_message("%li:%02li:%02li", t / 86400,
                (t / 3600) % 24, (t / 60) % 60);
 }
        return make_message("%li:%02li:%02li", t / 86400,
                (t / 3600) % 24, (t / 60) % 60);
 }
-