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