1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2008 Index Data
3 * See the file LICENSE for details.
8 * \brief Unix daemon management
27 #include <sys/types.h>
39 #include <yaz/daemon.h>
41 #include <yaz/snprintf.h>
44 static void write_pidfile(int pid_fd)
49 yaz_snprintf(buf, sizeof(buf), "%ld", (long) getpid());
50 if (ftruncate(pid_fd, 0))
52 yaz_log(YLOG_FATAL|YLOG_ERRNO, "ftruncate");
55 if (write(pid_fd, buf, strlen(buf)) != strlen(buf))
57 yaz_log(YLOG_FATAL|YLOG_ERRNO, "write");
65 static void kill_child_handler(int num)
71 static void keepalive(void (*work)(void *data), void *data)
75 void (*old_sighup)(int);
76 void (*old_sigterm)(int);
78 /* keep signals in their original state and make sure that some signals
79 to parent process also gets sent to the child..
81 old_sighup = signal(SIGHUP, kill_child_handler);
82 old_sigterm = signal(SIGTERM, kill_child_handler);
88 if (p == (pid_t) (-1))
91 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fork");
97 signal(SIGHUP, old_sighup); /* restore */
98 signal(SIGTERM, old_sigterm);/* restore */
104 /* enable signalling in kill_child_handler */
109 /* disable signalling in kill_child_handler */
114 yaz_log(YLOG_FATAL, "p1=%d != p=%d", p1, p);
118 if (WIFSIGNALED(status))
120 /* keep the child alive in case of errors, but _log_ */
121 switch(WTERMSIG(status)) {
123 yaz_log(YLOG_WARN, "Received SIGILL from child %ld", (long) p);
127 yaz_log(YLOG_WARN, "Received SIGABRT from child %ld", (long) p);
131 yaz_log(YLOG_WARN, "Received SIGSEGV from child %ld", (long) p);
135 yaz_log(YLOG_WARN, "Received SIGBUS from child %ld", (long) p);
139 yaz_log(YLOG_LOG, "Received SIGTERM from child %ld",
144 yaz_log(YLOG_WARN, "Received SIG %d from child %ld",
145 WTERMSIG(status), (long) p);
149 else if (status == 0)
150 cont = 0; /* child exited normally */
152 { /* child exited with error */
153 yaz_log(YLOG_LOG, "Exit %d from child %ld", status, (long) p);
156 if (cont) /* respawn slower as we get more errors */
163 int yaz_daemon(const char *progname,
165 void (*work)(void *data), void *data,
166 const char *pidfile, const char *uid)
171 /* open pidfile .. defer write until in child and after setuid */
174 pid_fd = open(pidfile, O_CREAT|O_RDWR, 0666);
177 yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", pidfile);
182 if (flags & YAZ_DAEMON_DEBUG)
184 /* in debug mode.. it's quite simple */
185 write_pidfile(pid_fd);
190 /* running in production mode. */
193 /* OK to use the non-thread version here */
194 struct passwd *pw = getpwnam(uid);
197 yaz_log(YLOG_FATAL, "%s: Unknown user", uid);
200 if (setuid(pw->pw_uid) < 0)
202 yaz_log(YLOG_FATAL|YLOG_ERRNO, "setuid");
207 if (flags & YAZ_DAEMON_FORK)
209 /* create pipe so that parent waits until child has created
211 static int hand[2]; /* hand shake for child */
214 yaz_log(YLOG_FATAL|YLOG_ERRNO, "pipe");
228 int res = read(hand[0], dummy, 1);
229 if (res < 0 && errno != EINTR)
231 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read fork handshake");
248 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