2008-05-24 16:05:49 +08:00
|
|
|
/* tinyproxy - A fast light-weight HTTP proxy
|
|
|
|
* Copyright (C) 2002 Robert James Kaes <rjkaes@users.sourceforge.net>
|
2002-05-23 12:39:32 +08:00
|
|
|
*
|
2008-05-24 16:05:49 +08:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2002-05-23 12:39:32 +08:00
|
|
|
*
|
2008-05-24 16:05:49 +08:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2002-05-23 12:39:32 +08:00
|
|
|
*
|
2008-05-24 16:05:49 +08:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* This file contains functions which are useful when writing a
|
|
|
|
* daemon process. The functions include a "makedaemon" function and
|
|
|
|
* a function to portably set a signal handler.
|
2002-05-23 12:39:32 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tinyproxy.h"
|
|
|
|
|
|
|
|
#include "daemon.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fork a child process and then kill the parent so make the calling
|
|
|
|
* program a daemon process.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
makedaemon(void)
|
|
|
|
{
|
2005-08-15 11:54:31 +08:00
|
|
|
if (fork() != 0)
|
|
|
|
exit(0);
|
2002-05-23 12:39:32 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
setsid();
|
|
|
|
set_signal_handler(SIGHUP, SIG_IGN);
|
2002-05-23 12:39:32 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
if (fork() != 0)
|
|
|
|
exit(0);
|
2002-05-23 12:39:32 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
chdir("/");
|
2008-08-24 13:57:38 +08:00
|
|
|
umask(0177);
|
2002-05-23 12:39:32 +08:00
|
|
|
|
2004-08-11 05:24:24 +08:00
|
|
|
#if NDEBUG
|
|
|
|
/*
|
|
|
|
* When not in debugging mode, close the standard file
|
|
|
|
* descriptors.
|
|
|
|
*/
|
2005-08-15 11:54:31 +08:00
|
|
|
close(0);
|
|
|
|
close(1);
|
|
|
|
close(2);
|
2002-10-04 04:38:06 +08:00
|
|
|
#endif
|
2002-05-23 12:39:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pass a signal number and a signal handling function into this function
|
|
|
|
* to handle signals sent to the process.
|
|
|
|
*/
|
|
|
|
signal_func *
|
2005-08-16 02:35:09 +08:00
|
|
|
set_signal_handler(int signo, signal_func *func)
|
2002-05-23 12:39:32 +08:00
|
|
|
{
|
2005-08-15 11:54:31 +08:00
|
|
|
struct sigaction act, oact;
|
2002-05-23 12:39:32 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
act.sa_handler = func;
|
|
|
|
sigemptyset(&act.sa_mask);
|
|
|
|
act.sa_flags = 0;
|
|
|
|
if (signo == SIGALRM) {
|
2002-05-23 12:39:32 +08:00
|
|
|
#ifdef SA_INTERRUPT
|
2005-08-15 11:54:31 +08:00
|
|
|
act.sa_flags |= SA_INTERRUPT; /* SunOS 4.x */
|
2002-05-23 12:39:32 +08:00
|
|
|
#endif
|
2005-08-15 11:54:31 +08:00
|
|
|
} else {
|
2002-05-23 12:39:32 +08:00
|
|
|
#ifdef SA_RESTART
|
2005-08-15 11:54:31 +08:00
|
|
|
act.sa_flags |= SA_RESTART; /* SVR4, 4.4BSD */
|
2002-05-23 12:39:32 +08:00
|
|
|
#endif
|
2005-08-15 11:54:31 +08:00
|
|
|
}
|
2002-05-23 12:39:32 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
if (sigaction(signo, &act, &oact) < 0)
|
|
|
|
return SIG_ERR;
|
2002-05-23 12:39:32 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
return oact.sa_handler;
|
2002-05-23 12:39:32 +08:00
|
|
|
}
|