1 /******************************************************************************/
2 /* pfixtools: a collection of postfix related tools */
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 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. */
31 /* Copyright (c) 2006-2008 the Authors */
32 /* see AUTHORS and source files for details */
33 /******************************************************************************/
36 * Copyright © 2007 Pierre Habouzit
37 * Copyright © 2008 Florent Bruneau
40 #ifndef PFIXTOOLS_COMMON_H
41 #define PFIXTOOLS_COMMON_H
46 #include <netinet/in.h>
55 #include <sys/socket.h>
57 #include <sys/types.h>
64 #define STR(x) __tostr(x)
66 typedef int (*initcall_t)(void);
67 typedef void (*exitcall_t)(void);
69 void common_register_exit(exitcall_t exitcall);
70 void common_init(void);
72 #define module_init(fn) \
73 __attribute__((constructor,used)) \
74 static void __init_wrapper__ ## fn (void) { \
80 #define module_exit(fn) \
81 __attribute__((constructor,used)) \
82 static void __exit_wrapper ## fn(void) { \
84 common_register_exit(fn); \
87 #define likely(expr) __builtin_expect((expr) != 0, 1)
88 #define unlikely(expr) __builtin_expect((expr) != 0, 0)
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 " \
100 #define __log(Level, Fmt, ...) \
101 if (log_level >= Level) { \
103 syslog(Level, "%s" Fmt, log_state, ##__VA_ARGS__); \
105 fprintf(stderr, "[%s] %s" Fmt "\n", \
106 __level_name(Level), log_state, ##__VA_ARGS__);\
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__)
119 #define UNIXERR(fun) err("%s:%d:%s %s: %m", \
120 __FILE__, __LINE__, __func__, fun)
122 extern int log_level;
123 extern bool log_syslog;
124 extern const char *log_state;
126 void common_sighandler(int sig);
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);
135 int daemon_detach(void);
136 int drop_privileges(const char *user, const char *group);
138 int pidfile_open(const char *name);
139 int pidfile_refresh(void);
141 int common_setup(const char* pidfile, bool unsafe, const char* runas_user,
142 const char* runas_group, bool daemonize);
144 static inline void common_startup(void)
146 signal(SIGPIPE, SIG_IGN);
147 signal(SIGSEGV, &common_sighandler);
151 #define DECLARE_MAIN \
152 static int main_initialize(void) \
155 openlog(DAEMON_NAME, LOG_PID, LOG_MAIL); \
160 static void main_shutdown(void) \
165 module_init(main_initialize); \
166 module_exit(main_shutdown);