Trivial documentation cleanups.
[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("%s", "daemonizing\n");
31 if ((pid = fork()) < 0) {
32 PARA_EMERG_LOG("%s", "failed to init daemon\n");
33 exit(EXIT_FAILURE);
34 } else if (pid)
35 exit(EXIT_SUCCESS); /* parent exits */
36 /* child */
37 setsid(); /* become session leader */
38 chdir("/");
39 umask(0);
40
41 null = open("/dev/null", O_RDONLY);
42 if (null < 0)
43 exit(EXIT_FAILURE);
44 dup2(null, STDIN_FILENO);
45 dup2(null, STDOUT_FILENO);
46 dup2(null, STDERR_FILENO);
47 close(null);
48 }
49
50 /**
51 * fopen() a file in append mode.
52 *
53 * \param logfile_name The name of the file to open.
54 *
55 * \return Either calls exit() or returns a valid file handle.
56 */
57 FILE *open_log(const char *logfile_name)
58 {
59 FILE *logfile;
60
61 if (!logfile_name)
62 return NULL;
63 if (!(logfile = fopen(logfile_name, "a"))) {
64 PARA_EMERG_LOG("can not open %s, uid: %d\n", logfile_name,
65 (int)getuid());
66 exit(EXIT_FAILURE);
67 }
68 setlinebuf(logfile);
69 return logfile;
70 }
71
72 /**
73 * Close the log file of the daemon.
74 *
75 * \param logfile The log file handle.
76 *
77 * It's OK to call this with logfile == \p NULL.
78 */
79 void close_log(FILE* logfile)
80 {
81 if (!logfile)
82 return;
83 PARA_INFO_LOG("%s", "closing logfile\n");
84 fclose(logfile);
85 }
86
87 /**
88 * Log the startup message containing the paraslash version.
89 */
90 void log_welcome(const char *whoami, int loglevel)
91 {
92 PARA_INFO_LOG("welcome to %s " PACKAGE_VERSION " ("BUILD_DATE")\n",
93 whoami);
94 PARA_DEBUG_LOG("using loglevel %d\n", loglevel);
95 }
96
97 /**
98 * Give up superuser privileges.
99 *
100 * \param username The user to switch to.
101 * \param groupname The group to switch to.
102 *
103 * This function returns immediately if not invoked with EUID zero. Otherwise,
104 * it tries to obtain the GID of \a groupname and the UID of \a username. On
105 * success, effective and real GID/UID and the saved set-group-ID/set-user-ID
106 * are all set accordingly. On errors, an appropriate message is logged and
107 * exit() is called to terminate the process.
108 *
109 * \sa getpwnam(3), getuid(2), setuid(2), getgrnam(2), setgid(2)
110 */
111 void para_drop_privileges(const char *username, const char *groupname)
112 {
113 struct passwd *p;
114 char *tmp;
115
116 if (geteuid())
117 return;
118 if (groupname) {
119 struct group *g = getgrnam(groupname);
120 if (!g) {
121 PARA_EMERG_LOG("failed to get group %s\n", groupname);
122 exit(EXIT_FAILURE);
123 }
124 if (setgid(g->gr_gid) < 0) {
125 PARA_EMERG_LOG("failed to set group id %d (%s)\n",
126 (int)g->gr_gid, strerror(errno));
127 exit(EXIT_FAILURE);
128 }
129 }
130 if (!username) {
131 PARA_EMERG_LOG("%s", "root privileges, but no user option given\n");
132 exit(EXIT_FAILURE);
133 }
134 tmp = para_strdup(username);
135 p = getpwnam(tmp);
136 free(tmp);
137 if (!p) {
138 PARA_EMERG_LOG("%s", "no such user\n");
139 exit(EXIT_FAILURE);
140 }
141 PARA_INFO_LOG("%s", "dropping root privileges\n");
142 setuid(p->pw_uid);
143 PARA_DEBUG_LOG("uid: %d, euid: %d\n", (int)getuid(), (int)geteuid());
144 }
145
146 /**
147 * Set/get the server uptime.
148 *
149 * \param set_or_get Chose one of the two modes.
150 *
151 * This should be called at startup time with \a set_or_get equal to \p
152 * UPTIME_SET which sets the uptime to zero. Subsequent calls with \a
153 * set_or_get equal to \p UPTIME_GET return the uptime.
154
155 * \return Zero if called with \a set_or_get equal to \p UPTIME_SET, the number
156 * of seconds ellapsed since the last reset otherwise.
157 *
158 * \sa time(2), difftime(3).
159 */
160 time_t server_uptime(enum uptime set_or_get)
161 {
162 static time_t startuptime;
163 time_t now;
164 double diff;
165
166 if (set_or_get == UPTIME_SET) {
167 time(&startuptime);
168 return 0;
169 }
170 time(&now);
171 diff = difftime(now, startuptime);
172 return (time_t) diff;
173 }
174
175 /**
176 * Construct string containing uptime.
177 *
178 * \return A dynamically allocated string of the form "days:hours:minutes".
179 *
180 * \sa server_uptime.
181 */
182 __malloc char *uptime_str(void)
183 {
184 long t = server_uptime(UPTIME_GET);
185 return make_message("%li:%02li:%02li", t / 86400,
186 (t / 3600) % 24, (t / 60) % 60);
187 }