Move acl functions to separate file acl.c.
[paraslash.git] / daemon.c
1 /*
2  * Copyright (C) 1997-2006 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  * 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 == 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 number of seconds ellapsed
154  * since the last reset.
155  *
156  * \sa time(2), difftime(3)
157  */
158 time_t server_uptime(enum uptime set_or_get)
159 {
160         static time_t startuptime;
161         time_t now;
162         double diff;
163
164         if (set_or_get == UPTIME_SET) {
165                 time(&startuptime);
166                 return 0;
167         }
168         time(&now);
169         diff = difftime(now, startuptime);
170         return (time_t) diff;
171 }
172
173 /**
174  * construct string containing uptime
175  *
176  * The format of the returned string is "days:hours:minutes"
177  *
178  * \sa server_uptime
179  */
180 __malloc char *uptime_str(void)
181 {
182         long t = server_uptime(UPTIME_GET);
183         return make_message("%li:%02li:%02li", t / 86400,
184                 (t / 3600) % 24, (t / 60) % 60);
185 }