f6f3ccc830b861205d8ffbb4c9a5ceaec63a21b7
[apps/pfixtools.git] / postlicyd.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 © 2006-2007 Pierre Habouzit
34  */
35
36 #include <signal.h>
37 #include <time.h>
38 #include <getopt.h>
39
40 #include "postlicyd.h"
41
42 static sig_atomic_t cleanexit = false;
43 static sig_atomic_t sigint    = false;
44 static volatile int nbthreads = 0;
45
46 static void main_sighandler(int sig)
47 {
48     static time_t lastintr = 0;
49     time_t now = time(NULL);
50
51     switch (sig) {
52       case SIGINT:
53         if (sigint) {
54             if (now - lastintr >= 1)
55                 break;
56         } else {
57             lastintr = now;
58             sigint   = true;
59         }
60         return;
61
62       case SIGTERM:
63         break;
64
65       default:
66         return;
67     }
68
69     syslog(LOG_ERR, "Killed...");
70     exit(-1);
71 }
72
73 static void main_initialize(void)
74 {
75     openlog("postlicyd", LOG_PID, LOG_MAIL);
76     signal(SIGPIPE, SIG_IGN);
77     signal(SIGINT,  &main_sighandler);
78     signal(SIGTERM, &main_sighandler);
79     syslog(LOG_INFO, "Starting...");
80 }
81
82 void *job_run(void *_fd)
83 {
84     int fd = (intptr_t)_fd;
85
86     close(fd);
87     return NULL;
88 }
89
90 static void main_loop(void)
91 {
92     int sock = -1;
93
94     while (!sigint) {
95         int fd = accept(sock, NULL, 0);
96         pthread_attr_t attr;
97         pthread_t dummy;
98
99         if (fd < 0) {
100             if (errno != EINTR || errno != EAGAIN)
101                 UNIXERR("accept");
102             continue;
103         }
104
105         pthread_attr_init(&attr);
106         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
107         pthread_create(&dummy, &attr, job_run, (void *)(intptr_t)fd);
108         pthread_attr_destroy(&attr);
109     }
110
111     cleanexit = true;
112     close(sock);
113 }
114
115 static void main_shutdown(void)
116 {
117     syslog(LOG_INFO, cleanexit ? "Stopping..." : "Unclean exit...");
118     closelog();
119 }
120
121 int main(void)
122 {
123     if (atexit(main_shutdown)) {
124         fputs("Cannot hook my atexit function, quitting !\n", stderr);
125         return EXIT_FAILURE;
126     }
127
128     main_initialize();
129     main_loop();
130     main_shutdown();
131     return EXIT_SUCCESS;
132 }