1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2009 Index Data
3 * See the file LICENSE for details.
8 * \brief Unix daemon management
27 #include <sys/types.h>
36 #include <yaz/daemon.h>
38 #include <yaz/snprintf.h>
41 static void write_pidfile(int pid_fd)
46 yaz_snprintf(buf, sizeof(buf), "%ld", (long) getpid());
47 if (ftruncate(pid_fd, 0))
49 yaz_log(YLOG_FATAL|YLOG_ERRNO, "ftruncate");
52 if (write(pid_fd, buf, strlen(buf)) != strlen(buf))
54 yaz_log(YLOG_FATAL|YLOG_ERRNO, "write");
62 static void kill_child_handler(int num)
68 static void keepalive(void (*work)(void *data), void *data)
72 void (*old_sighup)(int);
73 void (*old_sigterm)(int);
75 /* keep signals in their original state and make sure that some signals
76 to parent process also gets sent to the child..
78 old_sighup = signal(SIGHUP, kill_child_handler);
79 old_sigterm = signal(SIGTERM, kill_child_handler);
85 if (p == (pid_t) (-1))
88 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fork");
94 signal(SIGHUP, old_sighup); /* restore */
95 signal(SIGTERM, old_sigterm);/* restore */
101 /* enable signalling in kill_child_handler */
106 /* disable signalling in kill_child_handler */
111 yaz_log(YLOG_FATAL, "p1=%d != p=%d", p1, p);
115 if (WIFSIGNALED(status))
117 /* keep the child alive in case of errors, but _log_ */
118 switch(WTERMSIG(status)) {
120 yaz_log(YLOG_WARN, "Received SIGILL from child %ld", (long) p);
124 yaz_log(YLOG_WARN, "Received SIGABRT from child %ld", (long) p);
128 yaz_log(YLOG_WARN, "Received SIGSEGV from child %ld", (long) p);
132 yaz_log(YLOG_WARN, "Received SIGBUS from child %ld", (long) p);
136 yaz_log(YLOG_LOG, "Received SIGTERM from child %ld",
141 yaz_log(YLOG_WARN, "Received SIG %d from child %ld",
142 WTERMSIG(status), (long) p);
146 else if (status == 0)
147 cont = 0; /* child exited normally */
149 { /* child exited with error */
150 yaz_log(YLOG_LOG, "Exit %d from child %ld", status, (long) p);
153 if (cont) /* respawn slower as we get more errors */
160 int yaz_daemon(const char *progname,
162 void (*work)(void *data), void *data,
163 const char *pidfile, const char *uid)
168 /* open pidfile .. defer write until in child and after setuid */
171 pid_fd = open(pidfile, O_CREAT|O_RDWR, 0666);
174 yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", pidfile);
179 if (flags & YAZ_DAEMON_DEBUG)
181 /* in debug mode.. it's quite simple */
182 write_pidfile(pid_fd);
187 /* running in production mode. */
190 /* OK to use the non-thread version here */
191 struct passwd *pw = getpwnam(uid);
194 yaz_log(YLOG_FATAL, "%s: Unknown user", uid);
197 if (setuid(pw->pw_uid) < 0)
199 yaz_log(YLOG_FATAL|YLOG_ERRNO, "setuid");
204 if (flags & YAZ_DAEMON_FORK)
206 /* create pipe so that parent waits until child has created
208 static int hand[2]; /* hand shake for child */
211 yaz_log(YLOG_FATAL|YLOG_ERRNO, "pipe");
225 int res = read(hand[0], dummy, 1);
226 if (res < 0 && errno != EINTR)
228 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read fork handshake");
245 open("/dev/null", O_RDWR);
253 write_pidfile(pid_fd);
255 if (flags & YAZ_DAEMON_KEEPALIVE)
257 keepalive(work, data);
273 * indent-tabs-mode: nil
275 * vim: shiftwidth=4 tabstop=8 expandtab