Cleanup daemon startup.
[apps/pfixtools.git] / common / common.c
1 /******************************************************************************/
2 /*          pfixtools: a collection of postfix related tools                  */
3 /*          ~~~~~~~~~                                                         */
4 /*  ________________________________________________________________________  */
5 /*                                                                            */
6 /*  Redistribution and use in source and binary forms, with or without        */
7 /*  modification, are permitted provided that the following conditions        */
8 /*  are met:                                                                  */
9 /*                                                                            */
10 /*  1. Redistributions of source code must retain the above copyright         */
11 /*     notice, this list of conditions and the following disclaimer.          */
12 /*  2. Redistributions in binary form must reproduce the above copyright      */
13 /*     notice, this list of conditions and the following disclaimer in the    */
14 /*     documentation and/or other materials provided with the distribution.   */
15 /*  3. The names of its contributors may not be used to endorse or promote    */
16 /*     products derived from this software without specific prior written     */
17 /*     permission.                                                            */
18 /*                                                                            */
19 /*  THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND   */
20 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE     */
21 /*  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR        */
22 /*  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS    */
23 /*  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR    */
24 /*  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF      */
25 /*  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS  */
26 /*  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN   */
27 /*  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)   */
28 /*  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF    */
29 /*  THE POSSIBILITY OF SUCH DAMAGE.                                           */
30 /******************************************************************************/
31
32 /*
33  * Copyright © 2007 Pierre Habouzit
34  * Copyright © 2008 Florent Bruneau
35  */
36
37 #include <fcntl.h>
38 #include <grp.h>
39 #include <pwd.h>
40 #include <sys/un.h>
41
42 #include "common.h"
43
44 sig_atomic_t sigint  = false;
45 sig_atomic_t sighup  = false;
46
47 bool daemon_process  = true;
48
49 static FILE *pidfile = NULL;
50
51 void common_sighandler(int sig)
52 {
53     switch (sig) {
54       case SIGINT:
55         sigint = true;
56         return;
57
58       case SIGHUP:
59         sighup = true;
60         return;
61
62       default:
63         syslog(LOG_ERR, "Killed (got signal %d)...", sig);
64         exit(-1);
65     }
66 }
67
68 static int setnonblock(int sock)
69 {
70     int res = fcntl(sock, F_GETFL);
71
72     if (res < 0) {
73         UNIXERR("fcntl");
74         return -1;
75     }
76
77     if (fcntl(sock, F_SETFL, res | O_NONBLOCK) < 0) {
78         UNIXERR("fcntl");
79         return -1;
80     }
81
82     return 0;
83 }
84
85 int tcp_bind(const struct sockaddr *addr, socklen_t len)
86 {
87     int sock;
88
89     switch (addr->sa_family) {
90       case AF_UNIX:
91         unlink(((struct sockaddr_un *)addr)->sun_path);
92         sock = socket(PF_UNIX, SOCK_STREAM, 0);
93         break;
94       case AF_INET:
95         sock = socket(PF_INET, SOCK_STREAM, 0);
96         break;
97       case AF_INET6:
98         sock = socket(PF_INET6, SOCK_STREAM, 0);
99         break;
100       default:
101         errno = EINVAL;
102         return -1;
103     }
104
105     if (sock < 0) {
106         UNIXERR("socket");
107         return -1;
108     }
109
110     if (addr->sa_family != AF_UNIX) {
111         int v = 1;
112         if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) < 0) {
113             UNIXERR("setsockopt(SO_REUSEADDR)");
114             close(sock);
115             return -1;
116         }
117     }
118
119     if (bind(sock, addr, len) < 0) {
120         UNIXERR("bind");
121         close(sock);
122         return -1;
123     }
124
125     return sock;
126 }
127
128 int tcp_listen(const struct sockaddr *addr, socklen_t len)
129 {
130     int sock = tcp_bind(addr, len);
131     if (listen(sock, 0) < 0) {
132         UNIXERR("bind");
133         close(sock);
134         return -1;
135     }
136     return sock;
137 }
138
139 int tcp_listen_nonblock(const struct sockaddr *addr, socklen_t len)
140 {
141     int sock = tcp_bind(addr, len);
142     if (setnonblock(sock)) {
143         close(sock);
144         return -1;
145     }
146     if (listen(sock, 0) < 0) {
147         UNIXERR("bind");
148         close(sock);
149         return -1;
150     }
151     return sock;
152 }
153
154 int accept_nonblock(int fd)
155 {
156     int sock = accept(fd, NULL, 0);
157
158     if (sock < 0) {
159         UNIXERR("accept");
160         return -1;
161     }
162
163     if (setnonblock(sock)) {
164         close(sock);
165         return -1;
166     }
167
168     return sock;
169 }
170
171 int xwrite(int fd, const char *s, size_t l)
172 {
173     while (l > 0) {
174         int nb = write(fd, s, l);
175         if (nb < 0) {
176             if (errno == EINTR || errno == EAGAIN)
177                 continue;
178             return -1;
179         }
180         l -= nb;
181     }
182     return 0;
183 }
184
185 int daemon_detach(void)
186 {
187     pid_t pid;
188
189     close(STDIN_FILENO);
190     close(STDOUT_FILENO);
191     close(STDERR_FILENO);
192
193     open("/dev/null", O_RDWR);
194     open("/dev/null", O_RDWR);
195     open("/dev/null", O_RDWR);
196
197     pid = fork();
198     if (pid < 0) {
199         return -1;
200                 }
201     if (pid) {
202                                 daemon_process = false;
203         exit(0);
204                 }
205
206     setsid();
207     return 0;
208 }
209
210 int drop_privileges(const char *user, const char *group)
211 {
212     if (!geteuid()) {
213         struct passwd *pw;
214         struct group *gr;
215
216         if (group) {
217             gr = getgrnam(group);
218             if (!gr)
219                 return -1;
220             setgid(gr->gr_gid);
221         }
222
223         pw = getpwnam(user);
224         if (!pw)
225             return -1;
226         if (!group) {
227             setgid(pw->pw_gid);
228         }
229         setuid(pw->pw_uid);
230     }
231
232     return 0;
233 }
234
235 int pidfile_open(const char *name)
236 {
237                 struct flock lock;
238                 p_clear(&lock, 1);
239                 lock.l_type = F_WRLCK;
240     if (name) {
241         pidfile = fopen(name, "w");
242         if (!pidfile)
243             return -1;
244                                 if (fcntl(fileno(pidfile), F_SETLK, &lock) == -1) {
245                                                 syslog(LOG_ERR, "program already started");
246                                                 fclose(pidfile);
247                                                 pidfile = NULL;
248                                                 return -1;
249                                 }
250                                 fprintf(pidfile, "%d\n", getpid());
251         return fflush(pidfile);
252     }
253     return 0;
254 }
255
256 int pidfile_refresh(void)
257 {
258     if (pidfile) {
259         rewind(pidfile);
260         ftruncate(fileno(pidfile), 0);
261         fprintf(pidfile, "%d\n", getpid());
262         return fflush(pidfile);
263     }
264     return 0;
265 }
266
267 static void pidfile_close(void)
268 {
269                 struct flock lock;
270                 p_clear(&lock, 1);
271                 lock.l_type = F_UNLCK;
272     if (pidfile) {
273         rewind(pidfile);
274         ftruncate(fileno(pidfile), 0);
275         fcntl(fileno(pidfile), F_SETLK, &lock);
276                                 fclose(pidfile);
277         pidfile = NULL;
278     }
279 }
280
281 int common_setup(const char* pidfilename, bool unsafe, const char* runas_user,
282                  const char* runas_group, bool daemonize)
283 {
284     if (!unsafe && drop_privileges(runas_user, runas_group) < 0) {
285         syslog(LOG_CRIT, "unable to drop privileges");
286         return EXIT_FAILURE;
287     }
288
289     if (daemonize && daemon_detach() < 0) {
290         syslog(LOG_CRIT, "unable to fork");
291         return EXIT_FAILURE;
292     }
293
294                 if (pidfile_open(pidfilename) < 0) {
295         syslog(LOG_CRIT, "unable to write pidfile %s", pidfilename);
296         return EXIT_FAILURE;
297     }
298
299     pidfile_refresh();
300     return EXIT_SUCCESS;
301 }
302
303 extern initcall_t __madinit[];
304 extern exitcall_t __madexit[];
305
306 static void common_shutdown(void)
307 {
308                 if (daemon_process) {
309                                 syslog(LOG_INFO, "Stopping...");
310                 }
311                 pidfile_close();
312     for (int i = -1; __madexit[i]; i--) {
313         (*__madexit[i])();
314     }
315 }
316
317 static void __attribute__((__constructor__,__used__))
318 common_initialize(void)
319 {
320     if (atexit(common_shutdown)) {
321         fputs("Cannot hook my atexit function, quitting !\n", stderr);
322         abort();
323     }
324
325     for (int i = 0; __madinit[i]; i++) {
326         if ((*__madinit[i])()) {
327             exit(EXIT_FAILURE);
328         }
329     }
330 }
331