pfix-srsd: add a -I option
[apps/pfixtools.git] / common / common.h
1 /******************************************************************************/
2 /*          pfixtools: a collection of postfix related tools                  */
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 CONTRIBUTORS ``AS IS'' AND ANY EXPRESS   */
20 /*  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED         */
21 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE    */
22 /*  DISCLAIMED.  IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY         */
23 /*  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL        */
24 /*  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS   */
25 /*  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)     */
26 /*  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,       */
27 /*  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN  */
28 /*  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
29 /*  POSSIBILITY OF SUCH DAMAGE.                                               */
30 /*                                                                            */
31 /*   Copyright (c) 2006-2008 the Authors                                      */
32 /*   see AUTHORS and source files for details                                 */
33 /******************************************************************************/
34
35 /*
36  * Copyright © 2007 Pierre Habouzit
37  * Copyright © 2008 Florent Bruneau
38  */
39
40 #ifndef PFIXTOOLS_COMMON_H
41 #define PFIXTOOLS_COMMON_H
42
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <limits.h>
46 #include <netinet/in.h>
47 #include <signal.h>
48 #include <stdbool.h>
49 #include <stdbool.h>
50 #include <stddef.h>
51 #include <stdint.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <syslog.h>
55 #include <sys/socket.h>
56 #include <sys/stat.h>
57 #include <sys/types.h>
58 #include <time.h>
59 #include <unistd.h>
60
61 #include "mem.h"
62
63 #define __tostr(x)  #x
64 #define STR(x)      __tostr(x)
65
66 typedef int  (*initcall_t)(void);
67 typedef void (*exitcall_t)(void);
68
69 void common_register_exit(exitcall_t exitcall);
70 void common_init(void);
71
72 #define module_init(fn)                                                        \
73     __attribute__((constructor,used))                                          \
74     static void __init_wrapper__ ## fn (void) {                                \
75         common_init();                                                         \
76         if (fn() != 0) {                                                       \
77             exit(-1);                                                          \
78         }                                                                      \
79     }
80 #define module_exit(fn)                                                        \
81     __attribute__((constructor,used))                                          \
82     static void __exit_wrapper ## fn(void) {                                   \
83         common_init();                                                         \
84         common_register_exit(fn);                                              \
85     }
86
87 #define likely(expr)    __builtin_expect((expr) != 0, 1)
88 #define unlikely(expr)  __builtin_expect((expr) != 0, 0)
89
90 #define __level_name(L)                                            \
91   ( (L) == LOG_DEBUG   ? "debug "                                  \
92   : (L) == LOG_NOTICE  ? "notice"                                  \
93   : (L) == LOG_INFO    ? "info  "                                  \
94   : (L) == LOG_WARNING ? "warn  "                                  \
95   : (L) == LOG_ERR     ? "error "                                  \
96   : (L) == LOG_CRIT    ? "crit  "                                  \
97   : (L) == LOG_ALERT   ? "alert "                                  \
98   : "???   " )
99
100 #define __log(Level, Fmt, ...)                                     \
101     if (log_level >= Level) {                                      \
102         if (log_syslog) {                                          \
103             syslog(Level, "%s" Fmt, log_state, ##__VA_ARGS__);     \
104         } else {                                                   \
105             fprintf(stderr, "[%s] %s" Fmt "\n",                    \
106                     __level_name(Level), log_state, ##__VA_ARGS__);\
107         }                                                          \
108     }
109
110 #define debug(Fmt, ...)  __log(LOG_DEBUG,   Fmt, ##__VA_ARGS__)
111 #define notice(Fmt, ...) __log(LOG_NOTICE,  Fmt, ##__VA_ARGS__)
112 #define info(Fmt, ...)   __log(LOG_INFO,    Fmt, ##__VA_ARGS__)
113 #define warn(Fmt, ...)   __log(LOG_WARNING, Fmt, ##__VA_ARGS__)
114 #define err(Fmt, ...)    __log(LOG_ERR,     Fmt, ##__VA_ARGS__)
115 #define crit(Fmt, ...)   __log(LOG_CRIT,    Fmt, ##__VA_ARGS__)
116 #define alert(Fmt, ...)  __log(LOG_ALERT,   Fmt, ##__VA_ARGS__)
117 #define emerg(Fmt, ...)  __log(LOG_ALERT,   Fmt, ##__VA_ARGS__)
118
119 #define UNIXERR(fun)     err("%s:%d:%s %s: %m",                      \
120                              __FILE__, __LINE__, __func__, fun)
121
122 extern int          log_level;
123 extern bool         log_syslog;
124 extern const char  *log_state;
125
126 void common_sighandler(int sig);
127
128 int setnonblock(int sock);
129 int tcp_bind(const struct sockaddr *addr, socklen_t len);
130 int tcp_listen(const struct sockaddr *addr, socklen_t len);
131 int tcp_listen_nonblock(const struct sockaddr *addr, socklen_t len);
132 int accept_nonblock(int fd);
133 int xwrite(int fd, const char *s, size_t l);
134
135 int daemon_detach(void);
136 int drop_privileges(const char *user, const char *group);
137
138 int pidfile_open(const char *name);
139 int pidfile_refresh(void);
140
141 int common_setup(const char* pidfile, bool unsafe, const char* runas_user,
142                  const char* runas_group, bool daemonize);
143
144 static inline void common_startup(void)
145 {
146     signal(SIGPIPE, SIG_IGN);
147     signal(SIGSEGV, &common_sighandler);
148 }
149
150
151 #define DECLARE_MAIN                                              \
152     static int main_initialize(void)                              \
153     {                                                             \
154         log_syslog = true;                                        \
155         openlog(DAEMON_NAME, LOG_PID, LOG_MAIL);                  \
156         common_startup();                                         \
157         return 0;                                                 \
158     }                                                             \
159                                                                   \
160     static void main_shutdown(void)                               \
161     {                                                             \
162         closelog();                                               \
163     }                                                             \
164                                                                   \
165     module_init(main_initialize);                                 \
166     module_exit(main_shutdown);
167
168 #endif