REQUIREMENTS: Mention that libid3tag0-dev should be installed for id3v2.
[paraslash.git] / daemon.c
1 /*
2  * Copyright (C) 1997-2008 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file daemon.c Some helpers for programs that detach from the console. */
8 #include "para.h"
9 #include "daemon.h"
10 #include <pwd.h>
11
12 /* getgrnam() */
13 #include <sys/types.h>
14 #include <grp.h>
15
16 #include "string.h"
17
18 /**
19  * Do the usual stuff to become a daemon.
20  *
21  * Fork, become session leader, dup fd 0, 1, 2 to /dev/null.
22  *
23  * \sa fork(2), setsid(2), dup(2).
24  */
25 void daemon_init(void)
26 {
27         pid_t pid;
28         int null;
29
30         PARA_INFO_LOG("daemonizing\n");
31         pid = fork();
32         if (pid < 0)
33                 goto err;
34         if (pid)
35                 exit(EXIT_SUCCESS); /* parent exits */
36         /* become session leader */
37         if (setsid() < 0)
38                 goto err;
39         if (chdir("/") < 0)
40                 goto err;
41         umask(0);
42         null = open("/dev/null", O_RDONLY);
43         if (null < 0)
44                 goto err;
45         if (dup2(null, STDIN_FILENO) < 0)
46                 goto err;
47         if (dup2(null, STDOUT_FILENO) < 0)
48                 goto err;
49         if (dup2(null, STDERR_FILENO) < 0)
50                 goto err;
51         close(null);
52         return;
53 err:
54         PARA_EMERG_LOG("fatal: %s\n", strerror(errno));
55         exit(EXIT_FAILURE);
56 }
57
58 /**
59  * fopen() the given file in append mode.
60  *
61  * \param logfile_name The name of the file to open.
62  *
63  * \return Either calls exit() or returns a valid file handle.
64  */
65 FILE *open_log(const char *logfile_name)
66 {
67         FILE *logfile;
68
69         assert(logfile_name);
70         logfile = fopen(logfile_name, "a");
71         if (!logfile) {
72                 PARA_EMERG_LOG("can not open %s: %s\n", logfile_name,
73                         strerror(errno));
74                 exit(EXIT_FAILURE);
75         }
76         setlinebuf(logfile);
77         return logfile;
78 }
79
80 /**
81  * Close the log file of the daemon.
82  *
83  * \param logfile The log file handle.
84  *
85  * It's OK to call this with logfile == \p NULL.
86  */
87 void close_log(FILE* logfile)
88 {
89         if (!logfile)
90                 return;
91         PARA_INFO_LOG("closing logfile\n");
92         fclose(logfile);
93 }
94
95 /**
96  * Log the startup message containing the paraslash version.
97  */
98 void log_welcome(const char *whoami, int loglevel)
99 {
100         PARA_INFO_LOG("welcome to %s " PACKAGE_VERSION " ("BUILD_DATE")\n",
101                 whoami);
102         PARA_DEBUG_LOG("using loglevel %d\n", loglevel);
103 }
104
105 /**
106  * Give up superuser privileges.
107  *
108  * \param username The user to switch to.
109  * \param groupname The group to switch to.
110  *
111  * This function returns immediately if not invoked with EUID zero. Otherwise,
112  * it tries to obtain the GID of \a groupname and the UID of \a username.  On
113  * success, effective and real GID/UID and the saved set-group-ID/set-user-ID
114  * are all set accordingly. On errors, an appropriate message is logged and
115  * exit() is called to terminate the process.
116  *
117  * \sa getpwnam(3), getuid(2), setuid(2), getgrnam(2), setgid(2)
118  */
119 void para_drop_privileges(const char *username, const char *groupname)
120 {
121         struct passwd *p;
122         char *tmp;
123
124         if (geteuid())
125                 return;
126         if (groupname) {
127                 struct group *g = getgrnam(groupname);
128                 if (!g) {
129                         PARA_EMERG_LOG("failed to get group %s: %s\n",
130                                 groupname, strerror(errno));
131                         exit(EXIT_FAILURE);
132                 }
133                 if (setgid(g->gr_gid) < 0) {
134                         PARA_EMERG_LOG("failed to set group id %d: %s\n",
135                                 (int)g->gr_gid, strerror(errno));
136                         exit(EXIT_FAILURE);
137                 }
138         }
139         if (!username) {
140                 PARA_EMERG_LOG("root privileges, but no user option given\n");
141                 exit(EXIT_FAILURE);
142         }
143         tmp = para_strdup(username);
144         p = getpwnam(tmp);
145         free(tmp);
146         if (!p) {
147                 PARA_EMERG_LOG("%s: no such user\n", username);
148                 exit(EXIT_FAILURE);
149         }
150         PARA_INFO_LOG("dropping root privileges\n");
151         setuid(p->pw_uid);
152         PARA_DEBUG_LOG("uid: %d, euid: %d\n", (int)getuid(), (int)geteuid());
153 }
154
155 /**
156  * Set/get the server uptime.
157  *
158  * \param set_or_get Chose one of the two modes.
159  *
160  * This should be called at startup time with \a set_or_get equal to \p
161  * UPTIME_SET which sets the uptime to zero.  Subsequent calls with \a
162  * set_or_get equal to \p UPTIME_GET return the uptime.
163
164  * \return Zero if called with \a set_or_get equal to \p UPTIME_SET, the number
165  * of seconds ellapsed since the last reset otherwise.
166  *
167  * \sa time(2), difftime(3).
168  */
169 time_t server_uptime(enum uptime set_or_get)
170 {
171         static time_t startuptime;
172         time_t now;
173         double diff;
174
175         if (set_or_get == UPTIME_SET) {
176                 time(&startuptime);
177                 return 0;
178         }
179         time(&now);
180         diff = difftime(now, startuptime);
181         return (time_t) diff;
182 }
183
184 /**
185  * Construct string containing uptime.
186  *
187  * \return A dynamically allocated string of the form "days:hours:minutes".
188  *
189  * \sa server_uptime.
190  */
191 __malloc char *uptime_str(void)
192 {
193         long t = server_uptime(UPTIME_GET);
194         return make_message("%li:%02li:%02li", t / 86400,
195                 (t / 3600) % 24, (t / 60) % 60);
196 }