Merge daemon.c into common.c
[apps/pfixtools.git] / 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  */
35
36 #include <fcntl.h>
37 #include <grp.h>
38 #include <pwd.h>
39 #include <sys/un.h>
40
41 #include "common.h"
42
43 sig_atomic_t cleanexit = false;
44 sig_atomic_t sigint    = false;
45 sig_atomic_t sighup    = false;
46
47 void common_sighandler(int sig)
48 {
49     static time_t lastintr = 0;
50     time_t now = time(NULL);
51
52     switch (sig) {
53       case SIGINT:
54         if (sigint) {
55             if (now - lastintr >= 1)
56                 break;
57         } else {
58             lastintr = now;
59             sigint   = true;
60         }
61         return;
62
63       case SIGHUP:
64         sighup = true;
65         return;
66
67       default:
68         syslog(LOG_ERR, "Killed (got signal %d)...", sig);
69         exit(-1);
70     }
71 }
72
73 static int setnonblock(int sock)
74 {
75     int res = fcntl(sock, F_GETFL);
76
77     if (res < 0) {
78         UNIXERR("fcntl");
79         return -1;
80     }
81
82     if (fcntl(sock, F_SETFL, res | O_NONBLOCK) < 0) {
83         UNIXERR("fcntl");
84         return -1;
85     }
86
87     return 0;
88 }
89
90 int tcp_listen_nonblock(const struct sockaddr *addr, socklen_t len)
91 {
92     int sock;
93
94     switch (addr->sa_family) {
95       case AF_UNIX:
96         unlink(((struct sockaddr_un *)addr)->sun_path);
97         sock = socket(PF_UNIX, SOCK_STREAM, 0);
98         break;
99       case AF_INET:
100         sock = socket(PF_INET, SOCK_STREAM, 0);
101         break;
102       case AF_INET6:
103         sock = socket(PF_INET6, SOCK_STREAM, 0);
104         break;
105       default:
106         errno = EINVAL;
107         return -1;
108     }
109
110     if (sock < 0) {
111         UNIXERR("socket");
112         return -1;
113     }
114
115     if (addr->sa_family != AF_UNIX) {
116         int v = 1;
117         if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) < 0) {
118             UNIXERR("setsockopt(SO_REUSEADDR)");
119             close(sock);
120             return -1;
121         }
122     }
123
124     if (bind(sock, addr, len) < 0) {
125         UNIXERR("bind");
126         close(sock);
127         return -1;
128     }
129
130     if (setnonblock(sock)) {
131         close(sock);
132         return -1;
133     }
134
135     if (listen(sock, 0) < 0) {
136         UNIXERR("bind");
137         close(sock);
138         return -1;
139     }
140
141     return sock;
142 }
143
144 int accept_nonblock(int fd)
145 {
146     int sock = accept(fd, NULL, 0);
147
148     if (sock < 0) {
149         UNIXERR("accept");
150         return -1;
151     }
152
153     if (setnonblock(sock)) {
154         close(sock);
155         return -1;
156     }
157
158     return sock;
159 }
160
161 int daemon_detach(void)
162 {
163     pid_t pid;
164
165     close(STDIN_FILENO);
166     close(STDOUT_FILENO);
167     close(STDERR_FILENO);
168
169     open("/dev/null", O_RDWR);
170     open("/dev/null", O_RDWR);
171     open("/dev/null", O_RDWR);
172
173     pid = fork();
174     if (pid < 0)
175         return -1;
176     if (pid)
177         exit(0);
178
179     setsid();
180     return 0;
181 }
182
183 int drop_privileges(const char *user, const char *group)
184 {
185     if (!geteuid()) {
186         struct passwd *pw;
187         struct group *gr;
188
189         if (group) {
190             gr = getgrnam(group);
191             if (!gr)
192                 return -1;
193             setgid(gr->gr_gid);
194         }
195
196         pw = getpwnam(user);
197         if (!pw)
198             return -1;
199         if (!group) {
200             setgid(pw->pw_gid);
201         }
202         setuid(pw->pw_uid);
203     }
204
205     return 0;
206 }
207
208 void common_initialize(void)
209 {
210     extern initcall_t __madinit_start, __madinit_end;
211
212     initcall_t *call_p = &__madinit_start;
213     while (call_p < &__madinit_end) {
214         (*call_p++)();
215     }
216 }
217
218 void common_shutdown(void)
219 {
220     extern exitcall_t __madexit_start, __madexit_end;
221
222     exitcall_t *call_p = &__madexit_end;
223     while (call_p > &__madexit_start) {
224         (*--call_p)();
225     }
226 }