Implement lightweight getaddrinfo_a wrappers.
[apps/pfixtools.git] / job.c
1 /******************************************************************************/
2 /*          postlicyd: a postfix policy daemon with a lot of features         */
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 <errno.h>
37 #include <fcntl.h>
38 #include <signal.h>
39 #include <stdbool.h>
40 #include <syslog.h>
41 #include <sysexits.h>
42 #include <sys/epoll.h>
43 #include <sys/socket.h>
44 #include <sys/types.h>
45 #include <time.h>
46 #include <unistd.h>
47
48 #ifndef EPOLLRDHUP
49 #  include <linux/poll.h>
50 #  ifdef POLLRDHUP
51 #    define EPOLLRDHUP POLLRDHUP
52 #  else
53 #    define EPOLLRDHUP 0
54 #  endif
55 #endif
56
57 #include "job.h"
58 #include "gai.h"
59
60 static int epollfd = -1;
61 static bool sigint = false;
62
63 void job_delete(job_t **job)
64 {
65     if (*job) {
66         if ((*job)->stop) {
67             (*job)->stop(*job);
68         }
69         if ((*job)->fd >= 0) {
70             close((*job)->fd);
71         }
72         p_delete(job);
73     }
74 }
75
76 static job_t *job_register_fd(job_t *job)
77 {
78     struct epoll_event event = { .data.ptr = job, .events = EPOLLRDHUP };
79
80     if (job->mode & (JOB_READ | JOB_LISTEN)) {
81         event.events |= EPOLLIN;
82     }
83
84     if (job->mode & (JOB_WRITE | JOB_CONN)) {
85         event.events |= EPOLLOUT;
86     }
87
88     if (epoll_ctl(epollfd, EPOLL_CTL_ADD, job->fd, &event) < 0) {
89         syslog(LOG_ERR, "epoll_ctl error: %m");
90         job->error = true;
91         job_delete(&job);
92     }
93
94     return job;
95 }
96
97 void job_update_mode(job_t *job, int mode)
98 {
99     struct epoll_event event = { .data.ptr = job, .events = EPOLLRDHUP };
100
101     if (job->mode == mode)
102         return;
103
104     job->mode = mode;
105     if (job->mode & (JOB_READ | JOB_LISTEN)) {
106         event.events |= EPOLLIN;
107     }
108
109     if (job->mode & (JOB_WRITE | JOB_CONN)) {
110         event.events |= EPOLLOUT;
111     }
112
113     if (epoll_ctl(epollfd, EPOLL_CTL_MOD, job->fd, &event) < 0) {
114         syslog(LOG_ERR, "epoll_ctl error: %m");
115         job->error = true;
116     }
117 }
118
119 job_t *job_accept(job_t *listener, int mode)
120 {
121     int sock;
122     job_t *res;
123
124     if ((sock = accept(listener->fd, NULL, 0)) < 0) {
125         syslog(LOG_ERR, "accept error: %m");
126         return NULL;
127     }
128
129     if (fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK)) {
130         syslog(LOG_ERR, "fcntl error: %m");
131         return NULL;
132     }
133
134     res          = job_new();
135     res->fd      = sock;
136     res->mode    = mode;
137     res->process = listener->process;
138     res->stop    = listener->stop;
139     return job_register_fd(res);
140 }
141
142 static void job_sighandler(int sig)
143 {
144     static time_t lastintr = 0;
145     time_t now = time(NULL);
146
147     switch (sig) {
148       case SIGINT:
149         if (sigint) {
150             if (now - lastintr >= 1)
151                 break;
152         } else {
153             lastintr = now;
154             sigint   = true;
155         }
156         return;
157
158       case SIGTERM:
159         break;
160
161       default:
162         return;
163     }
164
165     syslog(LOG_ERR, "Killed...");
166     exit(-1);
167 }
168
169 void job_initialize(void)
170 {
171     signal(SIGPIPE, SIG_IGN);
172     signal(SIGINT,  &job_sighandler);
173     signal(SIGTERM, &job_sighandler);
174
175     epollfd = epoll_create(128);
176     if (epollfd < 0) {
177         syslog(LOG_ERR, "epoll_create error: %m");
178         exit(EX_OSERR);
179     }
180 }
181
182 void job_loop(void)
183 {
184     while (!sigint) {
185         struct epoll_event events[FD_SETSIZE];
186         int todo = epoll_wait(epollfd, events, countof(events), -1);
187
188         if (todo < 0) {
189             if (errno == EAGAIN || errno == EINTR)
190                 continue;
191             syslog(LOG_ERR, "epoll_wait error: %m");
192             exit(EX_OSERR);
193         }
194
195         while (todo) {
196             job_t *job = events[--todo].data.ptr;
197
198             assert (job->process);
199             job->process(job);
200
201             if (job->error || job->done) {
202                 job_delete(&job);
203             }
204         }
205
206         gai_process();
207     }
208 }
209
210 void job_shutdown(void)
211 {
212     if (epollfd >= 0) {
213         close(epollfd);
214         epollfd = -1;
215     }
216 }