1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 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");
66 static void kill_child_handler(int num)
72 static void keepalive(void (*work)(void *data), void *data)
76 void (*old_sighup)(int);
77 void (*old_sigterm)(int);
79 /* keep signals in their original state and make sure that some signals
80 to parent process also gets sent to the child..
82 old_sighup = signal(SIGHUP, kill_child_handler);
83 old_sigterm = signal(SIGTERM, kill_child_handler);
89 if (p == (pid_t) (-1))
92 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fork");
98 signal(SIGHUP, old_sighup); /* restore */
99 signal(SIGTERM, old_sigterm);/* restore */
105 /* enable signalling in kill_child_handler */
110 /* disable signalling in kill_child_handler */
115 yaz_log(YLOG_FATAL, "p1=%d != p=%d", p1, p);
119 if (WIFSIGNALED(status))
121 /* keep the child alive in case of errors, but _log_ */
122 switch(WTERMSIG(status)) {
124 yaz_log(YLOG_WARN, "Received SIGILL from child %ld", (long) p);
128 yaz_log(YLOG_WARN, "Received SIGABRT from child %ld", (long) p);
132 yaz_log(YLOG_WARN, "Received SIGSEGV from child %ld", (long) p);
136 yaz_log(YLOG_WARN, "Received SIGBUS from child %ld", (long) p);
140 yaz_log(YLOG_LOG, "Received SIGTERM from child %ld",
145 yaz_log(YLOG_WARN, "Received SIG %d from child %ld",
146 WTERMSIG(status), (long) p);
150 else if (status == 0)
151 cont = 0; /* child exited normally */
153 { /* child exited with error */
154 yaz_log(YLOG_LOG, "Exit %d from child %ld", status, (long) p);
157 if (cont) /* respawn slower as we get more errors */
164 int yaz_daemon(const char *progname,
166 void (*work)(void *data), void *data,
167 const char *pidfile, const char *uid)
172 /* open pidfile .. defer write until in child and after setuid */
175 pid_fd = open(pidfile, O_CREAT|O_RDWR, 0666);
178 yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", pidfile);
183 if (flags & YAZ_DAEMON_DEBUG)
185 /* in debug mode.. it's quite simple */
186 write_pidfile(pid_fd);
191 /* running in production mode. */
194 /* OK to use the non-thread version here */
195 struct passwd *pw = getpwnam(uid);
198 yaz_log(YLOG_FATAL, "%s: Unknown user", uid);
201 if (setuid(pw->pw_uid) < 0)
203 yaz_log(YLOG_FATAL|YLOG_ERRNO, "setuid");
206 /* Linux don't produce core dumps evern if the limit is right and
207 files are writable.. This fixes this. See prctl(2) */
209 #ifdef PR_SET_DUMPABLE
210 prctl(PR_SET_DUMPABLE, 1, 0, 0);
215 if (flags & YAZ_DAEMON_FORK)
217 /* create pipe so that parent waits until child has created
219 static int hand[2]; /* hand shake for child */
222 yaz_log(YLOG_FATAL|YLOG_ERRNO, "pipe");
236 int res = read(hand[0], dummy, 1);
237 if (res < 0 && errno != EINTR)
239 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read fork handshake");
256 open("/dev/null", O_RDWR);
264 write_pidfile(pid_fd);
266 if (flags & YAZ_DAEMON_KEEPALIVE)
268 keepalive(work, data);
284 * c-file-style: "Stroustrup"
285 * indent-tabs-mode: nil
287 * vim: shiftwidth=4 tabstop=8 expandtab