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
47 bool daemon_process = true;
48 int log_level = LOG_INFO;
49 bool log_syslog = false;
50 const char *log_state = "";
52 static FILE *pidfile = NULL;
54 void common_sighandler(int sig)
58 err("Killed (got signal %d)...", sig);
63 int setnonblock(int sock)
65 int res = fcntl(sock, F_GETFL);
72 if (fcntl(sock, F_SETFL, res | O_NONBLOCK) < 0) {
80 int tcp_bind(const struct sockaddr *addr, socklen_t len)
84 switch (addr->sa_family) {
86 unlink(((struct sockaddr_un *)addr)->sun_path);
87 sock = socket(PF_UNIX, SOCK_STREAM, 0);
90 sock = socket(PF_INET, SOCK_STREAM, 0);
93 sock = socket(PF_INET6, SOCK_STREAM, 0);
105 if (addr->sa_family != AF_UNIX) {
107 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) < 0) {
108 UNIXERR("setsockopt(SO_REUSEADDR)");
114 if (bind(sock, addr, len) < 0) {
123 int tcp_listen(const struct sockaddr *addr, socklen_t len)
125 int sock = tcp_bind(addr, len);
126 if (listen(sock, 0) < 0) {
134 int tcp_listen_nonblock(const struct sockaddr *addr, socklen_t len)
136 int sock = tcp_bind(addr, len);
137 if (setnonblock(sock)) {
141 if (listen(sock, 0) < 0) {
149 int accept_nonblock(int fd)
151 int sock = accept(fd, NULL, 0);
158 if (setnonblock(sock)) {
166 int xwrite(int fd, const char *s, size_t l)
169 int nb = write(fd, s, l);
171 if (errno == EINTR || errno == EAGAIN)
180 int daemon_detach(void)
185 close(STDOUT_FILENO);
186 close(STDERR_FILENO);
188 open("/dev/null", O_RDWR);
189 open("/dev/null", O_RDWR);
190 open("/dev/null", O_RDWR);
197 daemon_process = false;
205 int drop_privileges(const char *user, const char *group)
212 gr = getgrnam(group);
230 int pidfile_open(const char *name)
233 pidfile = fopen(name, "w");
236 fprintf(pidfile, "%d\n", getpid());
237 return fflush(pidfile);
242 int pidfile_refresh(void)
246 ftruncate(fileno(pidfile), 0);
247 fprintf(pidfile, "%d\n", getpid());
248 return fflush(pidfile);
253 static void pidfile_close(void)
256 if (daemon_process) {
258 ftruncate(fileno(pidfile), 0);
265 int common_setup(const char* pidfilename, bool unsafe, const char* runas_user,
266 const char* runas_group, bool daemonize)
268 if (pidfile_open(pidfilename) < 0) {
269 crit("unable to write pidfile %s", pidfilename);
273 if (!unsafe && drop_privileges(runas_user, runas_group) < 0) {
274 crit("unable to drop privileges");
278 if (daemonize && daemon_detach() < 0) {
279 crit("unable to fork");
291 static A(exitcall_t) __exit = ARRAY_INIT;
293 void common_register_exit(exitcall_t exitcall)
295 array_add(__exit, exitcall);
298 static void common_shutdown(void)
300 log_state = "stopping ";
301 if (daemon_process && log_syslog) {
305 for (int i = array_len(__exit) - 1 ; i >= 0 ; --i) {
306 array_elt(__exit, i)();
311 void common_init(void)
313 static bool __ran = false;
317 log_state = "starting ";
318 if (atexit(common_shutdown)) {
319 fputs("Cannot hook my atexit function, quitting !\n", stderr);