implement the event loop, add a stopper.
[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
58 #include "job.h"
59
60 static int epollfd = -1;
61 static bool sigint = false;
62
63 static void job_wipe(job_t *job)
64 {
65     if (job->fd >= 0) {
66         close(job->fd);
67         job->fd = -1;
68     }
69 }
70 DO_DELETE(job_t, job);
71
72 void job_release(job_t **job)
73 {
74     if (*job) {
75         if ((*job)->stop) {
76             (*job)->stop(*job);
77         }
78         job_delete(job);
79     }
80 }
81
82 static job_t *job_register_fd(job_t *job)
83 {
84     struct epoll_event event = { .data.ptr = job, .events = EPOLLRDHUP };
85
86     if (job->state & JOB_READ || job->state & JOB_LISTEN) {
87         event.events |= EPOLLIN;
88     }
89
90     if (job->state & JOB_WRITE || job->state & JOB_CONN) {
91         event.events |= EPOLLIN;
92     }
93
94     if (epoll_ctl(epollfd, EPOLL_CTL_ADD, job->fd, &event) < 0) {
95         syslog(LOG_ERR, "epoll_ctl error: %m");
96         job->error = true;
97         job_release(&job);
98     }
99
100     return job;
101 }
102
103 void job_update_state(job_t *job, int state)
104 {
105     struct epoll_event event = { .data.ptr = job, .events = EPOLLRDHUP };
106
107     if (job->state == state)
108         return;
109
110     job->state = state;
111
112     if (job->state & JOB_READ || job->state & JOB_LISTEN) {
113         event.events |= EPOLLIN;
114     }
115
116     if (job->state & JOB_WRITE || job->state & JOB_CONN) {
117         event.events |= EPOLLIN;
118     }
119
120     epoll_ctl(epollfd, EPOLL_CTL_MOD, job->fd, &event);
121 }
122
123 job_t *job_accept(job_t *listener, int state)
124 {
125     int sock;
126     job_t *res;
127
128     if ((sock = accept(listener->fd, NULL, 0)) < 0) {
129         syslog(LOG_ERR, "accept error: %m");
130         return NULL;
131     }
132
133     if (fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK)) {
134         syslog(LOG_ERR, "fcntl error: %m");
135         return NULL;
136     }
137
138     res          = job_new();
139     res->fd      = sock;
140     res->state   = state;
141     res->process = listener->process;
142     res->stop    = listener->stop;
143     return job_register_fd(res);
144 }
145
146 static void job_handler(int sig)
147 {
148     static time_t lastintr = 0;
149     time_t now = time(NULL);
150
151     switch (sig) {
152       case SIGINT:
153         if (sigint) {
154             if (now - lastintr >= 1)
155                 break;
156         } else {
157             lastintr = now;
158             sigint   = true;
159         }
160         return;
161
162       case SIGTERM:
163         break;
164
165       default:
166         return;
167     }
168
169     syslog(LOG_ERR, "Killed...");
170     exit(-1);
171 }
172
173 void job_initialize(void)
174 {
175     signal(SIGPIPE, SIG_IGN);
176     signal(SIGINT, &job_handler);
177     signal(SIGTERM, &job_handler);
178
179     epollfd = epoll_create(128);
180     if (epollfd < 0) {
181         syslog(LOG_ERR, "epoll_create error: %m");
182         exit(EX_OSERR);
183     }
184 }
185
186 void job_loop(void)
187 {
188     while (!sigint) {
189         struct epoll_event events[FD_SETSIZE];
190         int todo = epoll_wait(epollfd, events, countof(events), -1);
191
192         if (todo < 0) {
193             if (errno == EAGAIN || errno == EINTR)
194                 continue;
195             syslog(LOG_ERR, "epoll_wait error: %m");
196             exit(EX_OSERR);
197         }
198
199         while (todo) {
200             job_t *job = events[--todo].data.ptr;
201
202             assert (job->process);
203             job->process(job);
204
205             if (job->error || job->done) {
206                 job_release(&job);
207             }
208         }
209     }
210 }
211
212 void job_shutdown(void)
213 {
214     if (epollfd >= 0) {
215         close(epollfd);
216         epollfd = -1;
217     }
218 }