1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2012 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)
88 void (*old_sighup)(int);
89 void (*old_sigterm)(int);
90 void (*old_sigusr1)(int);
91 void (*old_sigusr2)(int);
93 keepalive_pid = getpid();
95 /* keep signals in their original state and make sure that some signals
96 to parent process also gets sent to the child.. */
97 old_sighup = signal(SIGHUP, normal_stop_handler);
98 old_sigterm = signal(SIGTERM, normal_stop_handler);
99 old_sigusr1 = signal(SIGUSR1, normal_stop_handler);
100 old_sigusr2 = signal(SIGUSR2, immediate_exit_handler);
101 while (cont && !child_got_signal_from_us)
106 if (p == (pid_t) (-1))
108 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fork");
114 signal(SIGHUP, old_sighup); /* restore */
115 signal(SIGTERM, old_sigterm);/* restore */
116 signal(SIGUSR1, old_sigusr1);/* restore */
117 signal(SIGUSR2, old_sigusr2);/* restore */
123 /* enable signalling in kill_child_handler */
128 /* disable signalling in kill_child_handler */
133 yaz_log(YLOG_FATAL, "p1=%d != p=%d", p1, p);
137 if (WIFSIGNALED(status))
139 /* keep the child alive in case of errors, but _log_ */
140 switch (WTERMSIG(status))
143 yaz_log(YLOG_WARN, "Received SIGILL from child %ld", (long) p);
147 yaz_log(YLOG_WARN, "Received SIGABRT from child %ld", (long) p);
151 yaz_log(YLOG_WARN, "Received SIGSEGV from child %ld", (long) p);
155 yaz_log(YLOG_WARN, "Received SIGBUS from child %ld", (long) p);
159 yaz_log(YLOG_LOG, "Received SIGTERM from child %ld",
164 yaz_log(YLOG_WARN, "Received SIG %d from child %ld",
165 WTERMSIG(status), (long) p);
169 else if (WIFEXITED(status))
172 if (WEXITSTATUS(status) != 0)
173 { /* child exited with error */
174 yaz_log(YLOG_LOG, "Exit %d from child %ld",
175 WEXITSTATUS(status), (long) p);
178 if (cont) /* respawn slower as we get more errors */
185 void yaz_daemon_stop(void)
189 kill(keepalive_pid, SIGUSR2); /* invoke immediate_exit_handler */
194 int yaz_daemon(const char *progname,
196 void (*work)(void *data), void *data,
197 const char *pidfile, const char *uid)
202 /* open pidfile .. defer write until in child and after setuid */
205 pid_fd = open(pidfile, O_CREAT|O_RDWR, 0666);
208 yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", pidfile);
213 if (flags & YAZ_DAEMON_DEBUG)
215 /* in debug mode.. it's quite simple */
216 write_pidfile(pid_fd);
221 /* running in production mode. */
224 /* OK to use the non-thread version here */
225 struct passwd *pw = getpwnam(uid);
228 yaz_log(YLOG_FATAL, "%s: Unknown user", uid);
231 if (setuid(pw->pw_uid) < 0)
233 yaz_log(YLOG_FATAL|YLOG_ERRNO, "setuid");
236 /* Linux don't produce core dumps evern if the limit is right and
237 files are writable.. This fixes this. See prctl(2) */
239 #ifdef PR_SET_DUMPABLE
240 prctl(PR_SET_DUMPABLE, 1, 0, 0);
245 if (flags & YAZ_DAEMON_FORK)
247 /* create pipe so that parent waits until child has created
249 static int hand[2]; /* hand shake for child */
252 yaz_log(YLOG_FATAL|YLOG_ERRNO, "pipe");
266 int res = read(hand[0], dummy, 1);
267 if (res < 0 && errno != EINTR)
269 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read fork handshake");
286 open("/dev/null", O_RDWR);
294 write_pidfile(pid_fd);
296 if (flags & YAZ_DAEMON_KEEPALIVE)
298 keepalive(work, data);
314 * c-file-style: "Stroustrup"
315 * indent-tabs-mode: nil
317 * vim: shiftwidth=4 tabstop=8 expandtab