1 /******************************************************************************/
2 /* postlicyd: a postfix policy daemon with a lot of features */
4 /* ________________________________________________________________________ */
6 /* Redistribution and use in source and binary forms, with or without */
7 /* modification, are permitted provided that the following conditions */
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 */
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 /******************************************************************************/
33 * Copyright © 2007 Pierre Habouzit
42 #include <sys/epoll.h>
43 #include <sys/socket.h>
44 #include <sys/types.h>
49 # include <linux/poll.h>
51 # define EPOLLRDHUP POLLRDHUP
60 static int epollfd = -1;
61 static bool sigint = false;
63 static void job_wipe(job_t *job)
70 DO_DELETE(job_t, job);
72 void job_release(job_t **job)
82 static job_t *job_register_fd(job_t *job)
84 struct epoll_event event = { .data.ptr = job, .events = EPOLLRDHUP };
86 if (job->state & JOB_READ || job->state & JOB_LISTEN) {
87 event.events |= EPOLLIN;
90 if (job->state & JOB_WRITE || job->state & JOB_CONN) {
91 event.events |= EPOLLIN;
94 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, job->fd, &event) < 0) {
95 syslog(LOG_ERR, "epoll_ctl error: %m");
103 void job_update_state(job_t *job, int state)
105 struct epoll_event event = { .data.ptr = job, .events = EPOLLRDHUP };
107 if (job->state == state)
112 if (job->state & JOB_READ || job->state & JOB_LISTEN) {
113 event.events |= EPOLLIN;
116 if (job->state & JOB_WRITE || job->state & JOB_CONN) {
117 event.events |= EPOLLIN;
120 epoll_ctl(epollfd, EPOLL_CTL_MOD, job->fd, &event);
123 job_t *job_accept(job_t *listener, int state)
128 if ((sock = accept(listener->fd, NULL, 0)) < 0) {
129 syslog(LOG_ERR, "accept error: %m");
133 if (fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK)) {
134 syslog(LOG_ERR, "fcntl error: %m");
141 res->process = listener->process;
142 res->stop = listener->stop;
143 return job_register_fd(res);
146 static void job_handler(int sig)
148 static time_t lastintr = 0;
149 time_t now = time(NULL);
154 if (now - lastintr >= 1)
169 syslog(LOG_ERR, "Killed...");
173 void job_initialize(void)
175 signal(SIGPIPE, SIG_IGN);
176 signal(SIGINT, &job_handler);
177 signal(SIGTERM, &job_handler);
179 epollfd = epoll_create(128);
181 syslog(LOG_ERR, "epoll_create error: %m");
189 struct epoll_event events[FD_SETSIZE];
190 int todo = epoll_wait(epollfd, events, countof(events), -1);
193 if (errno == EAGAIN || errno == EINTR)
195 syslog(LOG_ERR, "epoll_wait error: %m");
200 job_t *job = events[--todo].data.ptr;
202 assert (job->process);
205 if (job->error || job->done) {
212 void job_shutdown(void)