1 /* $Id: process.c,v 1.3 2007-06-18 11:10:20 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
33 #include <sys/types.h>
42 static void write_pidfile(const char *pidfile)
46 FILE *f = fopen(pidfile, "w");
49 yaz_log(YLOG_ERRNO|YLOG_FATAL, "Couldn't create %s", pidfile);
52 fprintf(f, "%ld", (long) getpid());
58 void kill_child_handler(int num)
64 int pazpar2_process(int debug, int daemon,
65 void (*work)(void *data), void *data,
66 const char *pidfile, const char *uid /* not yet used */)
70 void (*old_sighup)(int);
71 void (*old_sigterm)(int);
75 /* in debug mode.. it's quite simple */
76 write_pidfile(pidfile);
81 write_pidfile(pidfile);
82 /* running in production mode. */
85 /* OK to use the non-thread version here */
86 struct passwd *pw = getpwnam(uid);
89 yaz_log(YLOG_FATAL, "%s: Unknown user", uid);
92 if (setuid(pw->pw_uid) < 0)
94 yaz_log(YLOG_FATAL|YLOG_ERRNO, "setuid");
101 /* create pipe so that parent waits until child has created
103 static int hand[2]; /* hand shake for child */
106 yaz_log(YLOG_FATAL|YLOG_ERRNO, "pipe");
120 int res = read(hand[0], dummy, 1);
121 if (res < 0 && errno != EINTR)
123 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read fork handshake");
140 open("/dev/null", O_RDWR);
144 /* keep signals in their original state and make sure that some signals
145 to parent process also gets sent to the child..
147 old_sighup = signal(SIGHUP, kill_child_handler);
148 old_sigterm = signal(SIGTERM, kill_child_handler);
154 if (p == (pid_t) (-1))
157 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fork");
163 signal(SIGHUP, old_sighup); /* restore */
164 signal(SIGTERM, old_sigterm);/* restore */
170 /* enable signalling in kill_child_handler */
175 /* disable signalling in kill_child_handler */
180 yaz_log(YLOG_FATAL, "p1=%d != p=%d", p1, p);
184 if (WIFSIGNALED(status))
186 /* keep the child alive in case of errors, but _log_ */
187 switch(WTERMSIG(status)) {
189 yaz_log(YLOG_WARN, "Received SIGILL from child %ld", (long) p);
193 yaz_log(YLOG_WARN, "Received SIGABRT from child %ld", (long) p);
197 yaz_log(YLOG_WARN, "Received SIGSEGV from child %ld", (long) p);
201 yaz_log(YLOG_WARN, "Received SIGBUS from child %ld", (long) p);
205 yaz_log(YLOG_LOG, "Received SIGTERM from child %ld",
210 yaz_log(YLOG_WARN, "Received SIG %d from child %ld",
211 WTERMSIG(status), (long) p);
215 else if (status == 0)
216 cont = 0; /* child exited normally */
218 { /* child exited with error */
219 yaz_log(YLOG_LOG, "Exit %d from child %ld", status, (long) p);
222 if (cont) /* respawn slower as we get more errors */
232 * indent-tabs-mode: nil
234 * vim: shiftwidth=4 tabstop=8 expandtab