1 /* $Id: process.c,v 1.1 2007-06-08 13:57:19 adam Exp $
2 Copyright (c) 2006-2007, Index Data.
4 This file is part of Pazpar2.
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
37 static void write_pidfile(const char *pidfile)
41 FILE *f = fopen(pidfile, "w");
44 yaz_log(YLOG_ERRNO|YLOG_FATAL, "Couldn't create %s", pidfile);
47 fprintf(f, "%ld", (long) getpid());
53 void kill_child_handler(int num)
59 int pazpar2_process(int debug, int flags,
60 void (*work)(void *data), void *data,
61 const char *pidfile, const char *uid /* not yet used */)
65 void (*old_sighup)(int);
66 void (*old_sigterm)(int);
71 /* in debug mode.. it's quite simple */
72 write_pidfile(pidfile);
76 /* running in production mode. */
78 /* keep signals in their original state and make sure that some signals
79 to parent process also gets sent to the child.. Normally this
80 should not happen. We want the _child_ process to be terminated
81 normally. However, if the parent process is terminated, we
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 */
102 write_pidfile(pidfile);
107 /* 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 in an abnormal way */
157 yaz_log(YLOG_LOG, "Exit %d from child %ld", status, (long) p);
160 if (cont) /* respawn slower as we get more errors */
170 * indent-tabs-mode: nil
172 * vim: shiftwidth=4 tabstop=8 expandtab