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 void job_delete(job_t **job)
69 if ((*job)->fd >= 0) {
76 static job_t *job_register_fd(job_t *job)
78 struct epoll_event event = { .data.ptr = job, .events = EPOLLRDHUP };
80 if (job->mode & (JOB_READ | JOB_LISTEN)) {
81 event.events |= EPOLLIN;
84 if (job->mode & (JOB_WRITE | JOB_CONN)) {
85 event.events |= EPOLLOUT;
88 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, job->fd, &event) < 0) {
89 syslog(LOG_ERR, "epoll_ctl error: %m");
97 void job_update_mode(job_t *job, int mode)
99 struct epoll_event event = { .data.ptr = job, .events = EPOLLRDHUP };
101 if (job->mode == mode)
105 if (job->mode & (JOB_READ | JOB_LISTEN)) {
106 event.events |= EPOLLIN;
109 if (job->mode & (JOB_WRITE | JOB_CONN)) {
110 event.events |= EPOLLOUT;
113 if (epoll_ctl(epollfd, EPOLL_CTL_MOD, job->fd, &event) < 0) {
114 syslog(LOG_ERR, "epoll_ctl error: %m");
119 job_t *job_accept(job_t *listener, int mode)
124 if ((sock = accept(listener->fd, NULL, 0)) < 0) {
125 syslog(LOG_ERR, "accept error: %m");
129 if (fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK)) {
130 syslog(LOG_ERR, "fcntl error: %m");
137 res->process = listener->process;
138 res->stop = listener->stop;
139 return job_register_fd(res);
142 static void job_sighandler(int sig)
144 static time_t lastintr = 0;
145 time_t now = time(NULL);
150 if (now - lastintr >= 1)
165 syslog(LOG_ERR, "Killed...");
169 void job_initialize(void)
171 signal(SIGPIPE, SIG_IGN);
172 signal(SIGINT, &job_sighandler);
173 signal(SIGTERM, &job_sighandler);
175 epollfd = epoll_create(128);
177 syslog(LOG_ERR, "epoll_create error: %m");
185 struct epoll_event events[FD_SETSIZE];
186 int todo = epoll_wait(epollfd, events, countof(events), -1);
189 if (errno == EAGAIN || errno == EINTR)
191 syslog(LOG_ERR, "epoll_wait error: %m");
196 job_t *job = events[--todo].data.ptr;
198 assert (job->process);
201 if (job->error || job->done) {
210 void job_shutdown(void)