1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 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 sigusr2_handler(int num)
81 static pid_t keepalive_pid = 0;
83 static void keepalive(void (*work)(void *data), void *data)
91 void (*old_sighup)(int);
92 void (*old_sigterm)(int);
93 void (*old_sigusr1)(int);
94 struct sigaction sa2, sa1;
96 keepalive_pid = getpid();
98 /* keep signals in their original state and make sure that some signals
99 to parent process also gets sent to the child.. */
100 old_sighup = signal(SIGHUP, normal_stop_handler);
101 old_sigterm = signal(SIGTERM, normal_stop_handler);
102 old_sigusr1 = signal(SIGUSR1, normal_stop_handler);
104 sigemptyset(&sa2.sa_mask);
105 sa2.sa_handler = sigusr2_handler;
107 sigaction(SIGUSR2, &sa2, &sa1);
109 while (cont && !child_got_signal_from_us)
114 if (p == (pid_t) (-1))
116 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fork");
122 signal(SIGHUP, old_sighup); /* restore */
123 signal(SIGTERM, old_sigterm);/* restore */
124 signal(SIGUSR1, old_sigusr1);/* restore */
125 sigaction(SIGUSR2, &sa1, NULL);
131 /* enable signalling in kill_child_handler */
134 p1 = waitpid(p, &status, 0);
136 /* disable signalling in kill_child_handler */
139 if (p1 == (pid_t)(-1))
143 yaz_log(YLOG_FATAL|YLOG_ERRNO, "waitpid");
150 yaz_log(YLOG_FATAL, "p1=%d != p=%d", p1, p);
153 if (WIFSIGNALED(status))
155 /* keep the child alive in case of errors, but _log_ */
156 switch (WTERMSIG(status))
159 yaz_log(YLOG_WARN, "Received SIGILL from child %ld", (long) p);
164 yaz_log(YLOG_WARN, "Received SIGABRT from child %ld", (long) p);
169 yaz_log(YLOG_WARN, "Received SIGSEGV from child %ld", (long) p);
174 yaz_log(YLOG_WARN, "Received SIGBUS from child %ld", (long) p);
179 yaz_log(YLOG_LOG, "Received SIGTERM from child %ld",
184 yaz_log(YLOG_WARN, "Received SIG %d from child %ld",
185 WTERMSIG(status), (long) p);
189 else if (WIFEXITED(status))
192 if (WEXITSTATUS(status) != 0)
193 { /* child exited with error */
194 yaz_log(YLOG_LOG, "Exit %d from child %ld",
195 WEXITSTATUS(status), (long) p);
198 if (cont) /* respawn slower as we get more errors */
203 yaz_log(YLOG_WARN, "keepalive stop. %d SIGILL signal(s)", no_sigill);
205 yaz_log(YLOG_WARN, "keepalive stop. %d SIGABRT signal(s)", no_sigabrt);
207 yaz_log(YLOG_WARN, "keepalive stop. %d SIGSEGV signal(s)", no_sigsegv);
209 yaz_log(YLOG_WARN, "keepalive stop. %d SIGBUS signal(s)", no_sigbus);
213 void yaz_daemon_stop(void)
217 kill(keepalive_pid, SIGUSR2); /* invoke immediate_exit_handler */
222 int yaz_daemon(const char *progname,
224 void (*work)(void *data), void *data,
225 const char *pidfile, const char *uid)
230 /* open pidfile .. defer write until in child and after setuid */
233 pid_fd = open(pidfile, O_CREAT|O_RDWR, 0666);
236 yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", pidfile);
241 if (flags & YAZ_DAEMON_DEBUG)
243 /* in debug mode.. it's quite simple */
244 write_pidfile(pid_fd);
249 /* running in production mode. */
252 /* OK to use the non-thread version here */
253 struct passwd *pw = getpwnam(uid);
256 yaz_log(YLOG_FATAL, "%s: Unknown user", uid);
259 if (setuid(pw->pw_uid) < 0)
261 yaz_log(YLOG_FATAL|YLOG_ERRNO, "setuid");
264 /* Linux don't produce core dumps evern if the limit is right and
265 files are writable.. This fixes this. See prctl(2) */
267 #ifdef PR_SET_DUMPABLE
268 prctl(PR_SET_DUMPABLE, 1, 0, 0);
273 if (flags & YAZ_DAEMON_FORK)
275 /* create pipe so that parent waits until child has created
277 static int hand[2]; /* hand shake for child */
280 yaz_log(YLOG_FATAL|YLOG_ERRNO, "pipe");
294 int res = read(hand[0], dummy, 1);
295 if (res < 0 && errno != EINTR)
297 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read fork handshake");
314 open("/dev/null", O_RDWR);
322 write_pidfile(pid_fd);
324 if (flags & YAZ_DAEMON_KEEPALIVE)
326 keepalive(work, data);
342 * c-file-style: "Stroustrup"
343 * indent-tabs-mode: nil
345 * vim: shiftwidth=4 tabstop=8 expandtab