/* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2012 Index Data
+ * Copyright (C) Index Data
* See the file LICENSE for details.
*/
{
if (child_pid)
{
- /* tell child to terminate . ensure keepalive stops running -
- let wait(2) wait once - so we exit AFTER the child */
- child_got_signal_from_us = 1;
+ /* relay signal to child */
kill(child_pid, num);
}
}
-static void graceful_stop_handler(int num)
+static void log_reopen_handler(int num)
{
+ yaz_log_reopen();
if (child_pid)
- {
- /* tell our child to stop listening and wait some in time
- in the hope that it has stopped listening by then. Then
- we exit - quite possibly the child is still running - serving
- remaining requests */
kill(child_pid, num);
- sleep(2);
- _exit(0);
- }
}
+static void sigusr2_handler(int num)
+{
+ child_got_signal_from_us = 1;
+}
+
+static pid_t keepalive_pid = 0;
+
static void keepalive(void (*work)(void *data), void *data)
{
+ int no_sigill = 0;
+ int no_sigabrt = 0;
+ int no_sigsegv = 0;
+ int no_sigbus = 0;
int run = 1;
int cont = 1;
- void (*old_sighup)(int);
void (*old_sigterm)(int);
void (*old_sigusr1)(int);
-
+ struct sigaction sa2, sa1;
+
+ keepalive_pid = getpid();
+
/* keep signals in their original state and make sure that some signals
- to parent process also gets sent to the child..
- */
- old_sighup = signal(SIGHUP, normal_stop_handler);
+ to parent process also gets sent to the child.. */
old_sigterm = signal(SIGTERM, normal_stop_handler);
- old_sigusr1 = signal(SIGUSR1, graceful_stop_handler);
+ old_sigusr1 = signal(SIGUSR1, normal_stop_handler);
+
+ sigemptyset(&sa2.sa_mask);
+ sa2.sa_handler = sigusr2_handler;
+ sa2.sa_flags = 0;
+ sigaction(SIGUSR2, &sa2, &sa1);
+
while (cont && !child_got_signal_from_us)
{
pid_t p = fork();
int status;
if (p == (pid_t) (-1))
{
-
yaz_log(YLOG_FATAL|YLOG_ERRNO, "fork");
exit(1);
}
else if (p == 0)
{
- /* child */
- signal(SIGHUP, old_sighup); /* restore */
+ /* child */
signal(SIGTERM, old_sigterm);/* restore */
signal(SIGUSR1, old_sigusr1);/* restore */
-
+ sigaction(SIGUSR2, &sa1, NULL);
+
work(data);
exit(0);
}
-
+
/* enable signalling in kill_child_handler */
child_pid = p;
-
- p1 = wait(&status);
-
+
+ p1 = waitpid(p, &status, 0);
+
/* disable signalling in kill_child_handler */
child_pid = 0;
-
+
+ if (p1 == (pid_t)(-1))
+ {
+ if (errno != EINTR)
+ {
+ yaz_log(YLOG_FATAL|YLOG_ERRNO, "waitpid");
+ break;
+ }
+ continue;
+ }
if (p1 != p)
{
yaz_log(YLOG_FATAL, "p1=%d != p=%d", p1, p);
- exit(1);
+ break;
}
-
if (WIFSIGNALED(status))
{
/* keep the child alive in case of errors, but _log_ */
- switch(WTERMSIG(status)) {
+ switch (WTERMSIG(status))
+ {
case SIGILL:
yaz_log(YLOG_WARN, "Received SIGILL from child %ld", (long) p);
cont = 1;
+ no_sigill++;
break;
case SIGABRT:
yaz_log(YLOG_WARN, "Received SIGABRT from child %ld", (long) p);
cont = 1;
+ no_sigabrt++;
break ;
case SIGSEGV:
yaz_log(YLOG_WARN, "Received SIGSEGV from child %ld", (long) p);
cont = 1;
+ ++no_sigsegv;
break;
- case SIGBUS:
+ case SIGBUS:
yaz_log(YLOG_WARN, "Received SIGBUS from child %ld", (long) p);
cont = 1;
+ no_sigbus++;
break;
case SIGTERM:
yaz_log(YLOG_LOG, "Received SIGTERM from child %ld",
cont = 0;
}
}
- else if (status == 0)
- cont = 0; /* child exited normally */
- else
- { /* child exited with error */
- yaz_log(YLOG_LOG, "Exit %d from child %ld", status, (long) p);
+ else if (WIFEXITED(status))
+ {
cont = 0;
+ if (WEXITSTATUS(status) != 0)
+ { /* child exited with error */
+ yaz_log(YLOG_LOG, "Exit %d from child %ld",
+ WEXITSTATUS(status), (long) p);
+ }
}
if (cont) /* respawn slower as we get more errors */
sleep(1 + run/5);
run++;
}
+ if (no_sigill)
+ yaz_log(YLOG_WARN, "keepalive stop. %d SIGILL signal(s)", no_sigill);
+ if (no_sigabrt)
+ yaz_log(YLOG_WARN, "keepalive stop. %d SIGABRT signal(s)", no_sigabrt);
+ if (no_sigsegv)
+ yaz_log(YLOG_WARN, "keepalive stop. %d SIGSEGV signal(s)", no_sigsegv);
+ if (no_sigbus)
+ yaz_log(YLOG_WARN, "keepalive stop. %d SIGBUS signal(s)", no_sigbus);
+ yaz_log(YLOG_LOG, "keepalive stop");
}
#endif
+void yaz_daemon_stop(void)
+{
+#if HAVE_PWD_H
+ if (keepalive_pid)
+ kill(keepalive_pid, SIGUSR2); /* invoke immediate_exit_handler */
+#endif
+}
+
+
int yaz_daemon(const char *progname,
unsigned int flags,
void (*work)(void *data), void *data,
yaz_log(YLOG_FATAL, "%s: Unknown user", uid);
exit(1);
}
+ if (flags & YAZ_DAEMON_LOG_REOPEN)
+ {
+ FILE *f = yaz_log_file();
+ if (f)
+ {
+ if (fchown(fileno(f), pw->pw_uid, -1))
+ yaz_log(YLOG_WARN|YLOG_ERRNO, "fchown logfile");
+ }
+ }
if (setuid(pw->pw_uid) < 0)
{
yaz_log(YLOG_FATAL|YLOG_ERRNO, "setuid");
}
switch (fork())
{
- case 0:
+ case 0:
break;
case -1:
return 1;
close(hand[0]);
if (setsid() < 0)
return 1;
-
+
close(0);
close(1);
close(2);
write_pidfile(pid_fd);
+ if (flags & YAZ_DAEMON_LOG_REOPEN)
+ {
+ signal(SIGHUP, log_reopen_handler);
+ }
if (flags & YAZ_DAEMON_KEEPALIVE)
{
keepalive(work, data);