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
59 static int epollfd = -1;
60 static bool sigint = false;
62 void job_delete(job_t **job)
68 if ((*job)->fd >= 0) {
75 static job_t *job_register_fd(job_t *job)
77 struct epoll_event event = { .data.ptr = job, .events = EPOLLRDHUP };
79 if (job->mode & (JOB_READ | JOB_LISTEN)) {
80 event.events |= EPOLLIN;
83 if (job->mode & (JOB_WRITE | JOB_CONN)) {
84 event.events |= EPOLLOUT;
87 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, job->fd, &event) < 0) {
88 syslog(LOG_ERR, "epoll_ctl error: %m");
96 void job_update_mode(job_t *job, int mode)
98 struct epoll_event event = { .data.ptr = job, .events = EPOLLRDHUP };
100 if (job->mode == mode)
104 if (job->mode & (JOB_READ | JOB_LISTEN)) {
105 event.events |= EPOLLIN;
108 if (job->mode & (JOB_WRITE | JOB_CONN)) {
109 event.events |= EPOLLOUT;
112 if (epoll_ctl(epollfd, EPOLL_CTL_MOD, job->fd, &event) < 0) {
113 syslog(LOG_ERR, "epoll_ctl error: %m");
118 job_t *job_accept(job_t *listener, int mode)
123 if ((sock = accept(listener->fd, NULL, 0)) < 0) {
124 syslog(LOG_ERR, "accept error: %m");
128 if (fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK)) {
129 syslog(LOG_ERR, "fcntl error: %m");
136 res->process = listener->process;
137 res->stop = listener->stop;
138 return job_register_fd(res);
141 static void job_sighandler(int sig)
143 static time_t lastintr = 0;
144 time_t now = time(NULL);
149 if (now - lastintr >= 1)
164 syslog(LOG_ERR, "Killed...");
168 void job_initialize(void)
170 signal(SIGPIPE, SIG_IGN);
171 signal(SIGINT, &job_sighandler);
172 signal(SIGTERM, &job_sighandler);
174 epollfd = epoll_create(128);
176 syslog(LOG_ERR, "epoll_create error: %m");
184 struct epoll_event events[FD_SETSIZE];
185 int todo = epoll_wait(epollfd, events, countof(events), -1);
188 if (errno == EAGAIN || errno == EINTR)
190 syslog(LOG_ERR, "epoll_wait error: %m");
195 job_t *job = events[--todo].data.ptr;
197 assert (job->process);
200 if (job->error || job->done) {
207 void job_shutdown(void)