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