1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
8 * \brief Unix daemon management
27 #include <sys/types.h>
37 #include <sys/prctl.h>
40 #include <yaz/daemon.h>
42 #include <yaz/snprintf.h>
45 static void write_pidfile(int pid_fd)
50 yaz_snprintf(buf, sizeof(buf), "%ld", (long) getpid());
51 if (ftruncate(pid_fd, 0))
53 yaz_log(YLOG_FATAL|YLOG_ERRNO, "ftruncate");
56 if (write(pid_fd, buf, strlen(buf)) != (int) strlen(buf))
58 yaz_log(YLOG_FATAL|YLOG_ERRNO, "write");
65 int child_got_signal_from_us = 0;
67 static void normal_stop_handler(int num)
71 /* relay signal to child */
72 child_got_signal_from_us = 1;
77 static void immediate_exit_handler(int num)
82 static pid_t keepalive_pid = 0;
84 static void keepalive(void (*work)(void *data), void *data)
92 void (*old_sighup)(int);
93 void (*old_sigterm)(int);
94 void (*old_sigusr1)(int);
95 void (*old_sigusr2)(int);
97 keepalive_pid = getpid();
99 /* keep signals in their original state and make sure that some signals
100 to parent process also gets sent to the child.. */
101 old_sighup = signal(SIGHUP, normal_stop_handler);
102 old_sigterm = signal(SIGTERM, normal_stop_handler);
103 old_sigusr1 = signal(SIGUSR1, normal_stop_handler);
104 old_sigusr2 = signal(SIGUSR2, immediate_exit_handler);
105 while (cont && !child_got_signal_from_us)
110 if (p == (pid_t) (-1))
112 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fork");
118 signal(SIGHUP, old_sighup); /* restore */
119 signal(SIGTERM, old_sigterm);/* restore */
120 signal(SIGUSR1, old_sigusr1);/* restore */
121 signal(SIGUSR2, old_sigusr2);/* restore */
127 /* enable signalling in kill_child_handler */
132 /* disable signalling in kill_child_handler */
137 yaz_log(YLOG_FATAL, "p1=%d != p=%d", p1, p);
141 if (WIFSIGNALED(status))
143 /* keep the child alive in case of errors, but _log_ */
144 switch (WTERMSIG(status))
147 yaz_log(YLOG_WARN, "Received SIGILL from child %ld", (long) p);
152 yaz_log(YLOG_WARN, "Received SIGABRT from child %ld", (long) p);
157 yaz_log(YLOG_WARN, "Received SIGSEGV from child %ld", (long) p);
162 yaz_log(YLOG_WARN, "Received SIGBUS from child %ld", (long) p);
167 yaz_log(YLOG_LOG, "Received SIGTERM from child %ld",
172 yaz_log(YLOG_WARN, "Received SIG %d from child %ld",
173 WTERMSIG(status), (long) p);
177 else if (WIFEXITED(status))
180 if (WEXITSTATUS(status) != 0)
181 { /* child exited with error */
182 yaz_log(YLOG_LOG, "Exit %d from child %ld",
183 WEXITSTATUS(status), (long) p);
186 if (cont) /* respawn slower as we get more errors */
191 yaz_log(YLOG_WARN, "keepalive stop. %d SIGILL signal(s)", no_sigill);
193 yaz_log(YLOG_WARN, "keepalive stop. %d SIGABRT signal(s)", no_sigabrt);
195 yaz_log(YLOG_WARN, "keepalive stop. %d SIGSEGV signal(s)", no_sigsegv);
197 yaz_log(YLOG_WARN, "keepalive stop. %d SIGBUS signal(s)", no_sigbus);
201 void yaz_daemon_stop(void)
205 kill(keepalive_pid, SIGUSR2); /* invoke immediate_exit_handler */
210 int yaz_daemon(const char *progname,
212 void (*work)(void *data), void *data,
213 const char *pidfile, const char *uid)
218 /* open pidfile .. defer write until in child and after setuid */
221 pid_fd = open(pidfile, O_CREAT|O_RDWR, 0666);
224 yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", pidfile);
229 if (flags & YAZ_DAEMON_DEBUG)
231 /* in debug mode.. it's quite simple */
232 write_pidfile(pid_fd);
237 /* running in production mode. */
240 /* OK to use the non-thread version here */
241 struct passwd *pw = getpwnam(uid);
244 yaz_log(YLOG_FATAL, "%s: Unknown user", uid);
247 if (setuid(pw->pw_uid) < 0)
249 yaz_log(YLOG_FATAL|YLOG_ERRNO, "setuid");
252 /* Linux don't produce core dumps evern if the limit is right and
253 files are writable.. This fixes this. See prctl(2) */
255 #ifdef PR_SET_DUMPABLE
256 prctl(PR_SET_DUMPABLE, 1, 0, 0);
261 if (flags & YAZ_DAEMON_FORK)
263 /* create pipe so that parent waits until child has created
265 static int hand[2]; /* hand shake for child */
268 yaz_log(YLOG_FATAL|YLOG_ERRNO, "pipe");
282 int res = read(hand[0], dummy, 1);
283 if (res < 0 && errno != EINTR)
285 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read fork handshake");
302 open("/dev/null", O_RDWR);
310 write_pidfile(pid_fd);
312 if (flags & YAZ_DAEMON_KEEPALIVE)
314 keepalive(work, data);
330 * c-file-style: "Stroustrup"
331 * indent-tabs-mode: nil
333 * vim: shiftwidth=4 tabstop=8 expandtab