c790c17ba5c6bfd124db576fa6b60e96732054e6
[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("%s", "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\n", groupname);
130 exit(EXIT_FAILURE);
131 }
132 if (setgid(g->gr_gid) < 0) {
133 PARA_EMERG_LOG("failed to set group id %d (%s)\n",
134 (int)g->gr_gid, strerror(errno));
135 exit(EXIT_FAILURE);
136 }
137 }
138 if (!username) {
139 PARA_EMERG_LOG("%s", "root privileges, but no user option given\n");
140 exit(EXIT_FAILURE);
141 }
142 tmp = para_strdup(username);
143 p = getpwnam(tmp);
144 free(tmp);
145 if (!p) {
146 PARA_EMERG_LOG("%s", "no such user\n");
147 exit(EXIT_FAILURE);
148 }
149 PARA_INFO_LOG("%s", "dropping root privileges\n");
150 setuid(p->pw_uid);
151 PARA_DEBUG_LOG("uid: %d, euid: %d\n", (int)getuid(), (int)geteuid());
152 }
153
154 /**
155 * Set/get the server uptime.
156 *
157 * \param set_or_get Chose one of the two modes.
158 *
159 * This should be called at startup time with \a set_or_get equal to \p
160 * UPTIME_SET which sets the uptime to zero. Subsequent calls with \a
161 * set_or_get equal to \p UPTIME_GET return the uptime.
162
163 * \return Zero if called with \a set_or_get equal to \p UPTIME_SET, the number
164 * of seconds ellapsed since the last reset otherwise.
165 *
166 * \sa time(2), difftime(3).
167 */
168 time_t server_uptime(enum uptime set_or_get)
169 {
170 static time_t startuptime;
171 time_t now;
172 double diff;
173
174 if (set_or_get == UPTIME_SET) {
175 time(&startuptime);
176 return 0;
177 }
178 time(&now);
179 diff = difftime(now, startuptime);
180 return (time_t) diff;
181 }
182
183 /**
184 * Construct string containing uptime.
185 *
186 * \return A dynamically allocated string of the form "days:hours:minutes".
187 *
188 * \sa server_uptime.
189 */
190 __malloc char *uptime_str(void)
191 {
192 long t = server_uptime(UPTIME_GET);
193 return make_message("%li:%02li:%02li", t / 86400,
194 (t / 3600) % 24, (t / 60) % 60);
195 }