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