2 * Copyright (C) 1995-2008, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: daemon.c,v 1.3 2008-02-21 10:18:15 adam Exp $
10 * \brief Unix daemon management
29 #include <sys/types.h>
41 #include <yaz/daemon.h>
43 #include <yaz/snprintf.h>
46 static void write_pidfile(int pid_fd)
51 yaz_snprintf(buf, sizeof(buf), "%ld", (long) getpid());
52 if (ftruncate(pid_fd, 0))
54 yaz_log(YLOG_FATAL|YLOG_ERRNO, "ftruncate");
57 if (write(pid_fd, buf, strlen(buf)) != strlen(buf))
59 yaz_log(YLOG_FATAL|YLOG_ERRNO, "write");
67 static void kill_child_handler(int num)
73 static void keepalive(void (*work)(void *data), void *data)
77 void (*old_sighup)(int);
78 void (*old_sigterm)(int);
80 /* keep signals in their original state and make sure that some signals
81 to parent process also gets sent to the child..
83 old_sighup = signal(SIGHUP, kill_child_handler);
84 old_sigterm = signal(SIGTERM, kill_child_handler);
90 if (p == (pid_t) (-1))
93 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fork");
99 signal(SIGHUP, old_sighup); /* restore */
100 signal(SIGTERM, old_sigterm);/* restore */
106 /* enable signalling in kill_child_handler */
111 /* disable signalling in kill_child_handler */
116 yaz_log(YLOG_FATAL, "p1=%d != p=%d", p1, p);
120 if (WIFSIGNALED(status))
122 /* keep the child alive in case of errors, but _log_ */
123 switch(WTERMSIG(status)) {
125 yaz_log(YLOG_WARN, "Received SIGILL from child %ld", (long) p);
129 yaz_log(YLOG_WARN, "Received SIGABRT from child %ld", (long) p);
133 yaz_log(YLOG_WARN, "Received SIGSEGV from child %ld", (long) p);
137 yaz_log(YLOG_WARN, "Received SIGBUS from child %ld", (long) p);
141 yaz_log(YLOG_LOG, "Received SIGTERM from child %ld",
146 yaz_log(YLOG_WARN, "Received SIG %d from child %ld",
147 WTERMSIG(status), (long) p);
151 else if (status == 0)
152 cont = 0; /* child exited normally */
154 { /* child exited with error */
155 yaz_log(YLOG_LOG, "Exit %d from child %ld", status, (long) p);
158 if (cont) /* respawn slower as we get more errors */
165 int yaz_daemon(const char *progname,
167 void (*work)(void *data), void *data,
168 const char *pidfile, const char *uid)
173 /* open pidfile .. defer write until in child and after setuid */
176 pid_fd = open(pidfile, O_CREAT|O_RDWR, 0666);
179 yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", pidfile);
184 if (flags & YAZ_DAEMON_DEBUG)
186 /* in debug mode.. it's quite simple */
187 write_pidfile(pid_fd);
192 /* running in production mode. */
195 /* OK to use the non-thread version here */
196 struct passwd *pw = getpwnam(uid);
199 yaz_log(YLOG_FATAL, "%s: Unknown user", uid);
202 if (setuid(pw->pw_uid) < 0)
204 yaz_log(YLOG_FATAL|YLOG_ERRNO, "setuid");
209 if (flags & YAZ_DAEMON_FORK)
211 /* create pipe so that parent waits until child has created
213 static int hand[2]; /* hand shake for child */
216 yaz_log(YLOG_FATAL|YLOG_ERRNO, "pipe");
230 int res = read(hand[0], dummy, 1);
231 if (res < 0 && errno != EINTR)
233 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read fork handshake");
250 open("/dev/null", O_RDWR);
255 write_pidfile(pid_fd);
257 if (flags & YAZ_DAEMON_KEEPALIVE)
259 keepalive(work, data);
275 * indent-tabs-mode: nil
277 * vim: shiftwidth=4 tabstop=8 expandtab