1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2012 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);
78 void (*old_sigusr1)(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);
85 old_sigusr1 = signal(SIGUSR1, kill_child_handler);
91 if (p == (pid_t) (-1))
94 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fork");
100 signal(SIGHUP, old_sighup); /* restore */
101 signal(SIGTERM, old_sigterm);/* restore */
102 signal(SIGUSR1, old_sigusr1);/* restore */
108 /* enable signalling in kill_child_handler */
113 /* disable signalling in kill_child_handler */
118 yaz_log(YLOG_FATAL, "p1=%d != p=%d", p1, p);
122 if (WIFSIGNALED(status))
124 /* keep the child alive in case of errors, but _log_ */
125 switch(WTERMSIG(status)) {
127 yaz_log(YLOG_WARN, "Received SIGILL from child %ld", (long) p);
131 yaz_log(YLOG_WARN, "Received SIGABRT from child %ld", (long) p);
135 yaz_log(YLOG_WARN, "Received SIGSEGV from child %ld", (long) p);
139 yaz_log(YLOG_WARN, "Received SIGBUS from child %ld", (long) p);
143 yaz_log(YLOG_LOG, "Received SIGTERM from child %ld",
148 yaz_log(YLOG_WARN, "Received SIG %d from child %ld",
149 WTERMSIG(status), (long) p);
153 else if (status == 0)
154 cont = 0; /* child exited normally */
156 { /* child exited with error */
157 yaz_log(YLOG_LOG, "Exit %d from child %ld", status, (long) p);
160 if (cont) /* respawn slower as we get more errors */
167 int yaz_daemon(const char *progname,
169 void (*work)(void *data), void *data,
170 const char *pidfile, const char *uid)
175 /* open pidfile .. defer write until in child and after setuid */
178 pid_fd = open(pidfile, O_CREAT|O_RDWR, 0666);
181 yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", pidfile);
186 if (flags & YAZ_DAEMON_DEBUG)
188 /* in debug mode.. it's quite simple */
189 write_pidfile(pid_fd);
194 /* running in production mode. */
197 /* OK to use the non-thread version here */
198 struct passwd *pw = getpwnam(uid);
201 yaz_log(YLOG_FATAL, "%s: Unknown user", uid);
204 if (setuid(pw->pw_uid) < 0)
206 yaz_log(YLOG_FATAL|YLOG_ERRNO, "setuid");
209 /* Linux don't produce core dumps evern if the limit is right and
210 files are writable.. This fixes this. See prctl(2) */
212 #ifdef PR_SET_DUMPABLE
213 prctl(PR_SET_DUMPABLE, 1, 0, 0);
218 if (flags & YAZ_DAEMON_FORK)
220 /* create pipe so that parent waits until child has created
222 static int hand[2]; /* hand shake for child */
225 yaz_log(YLOG_FATAL|YLOG_ERRNO, "pipe");
239 int res = read(hand[0], dummy, 1);
240 if (res < 0 && errno != EINTR)
242 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read fork handshake");
259 open("/dev/null", O_RDWR);
267 write_pidfile(pid_fd);
269 if (flags & YAZ_DAEMON_KEEPALIVE)
271 keepalive(work, data);
287 * c-file-style: "Stroustrup"
288 * indent-tabs-mode: nil
290 * vim: shiftwidth=4 tabstop=8 expandtab