Improved daemonise routine to stabilise startup

This commit is contained in:
Joey Schulze 2007-05-26 14:37:43 +00:00
parent 244eb7aa6e
commit 75b4840911

38
klogd.c
View File

@ -243,9 +243,14 @@
* people have submitted patches: Troels Walsted Hansen * people have submitted patches: Troels Walsted Hansen
* <troels@thule.no>, Wolfgang Oertl <Wolfgang.Oertl@uibk.ac.at> * <troels@thule.no>, Wolfgang Oertl <Wolfgang.Oertl@uibk.ac.at>
* and Thomas Roessler. * and Thomas Roessler.
*
* Thu Apr 29 15:24:07 2004: Solar Designer <solar@openwall.com> * Thu Apr 29 15:24:07 2004: Solar Designer <solar@openwall.com>
* Prevent potential buffer overflow in reading messages from the * Prevent potential buffer overflow in reading messages from the
* kernel log rinbuffer. * kernel log rinbuffer.
*
* Sat May 26 16:33:18 2007: Martin Schulze <joey@infodrom.org>
* Improved daemonise routine to stabilise startup
*
*/ */
@ -357,6 +362,19 @@ static void CloseLogSrc()
} }
/*
* Signal handler to terminate the parent process.
*/
#ifndef TESTING
void doexit(sig)
int sig;
{
exit (0);
}
#endif
void restart(sig) void restart(sig)
int sig; int sig;
@ -994,6 +1012,7 @@ int main(argc, argv)
*output = (char *) 0; *output = (char *) 0;
#ifndef TESTING #ifndef TESTING
pid_t ppid = getpid();
chdir ("/"); chdir ("/");
#endif #endif
/* Parse the command-line. */ /* Parse the command-line. */
@ -1073,10 +1092,13 @@ int main(argc, argv)
{ {
if (!check_pid(PidFile)) if (!check_pid(PidFile))
{ {
signal (SIGTERM, doexit);
if ( fork() == 0 ) if ( fork() == 0 )
{ {
auto int fl; auto int fl;
int num_fds = getdtablesize(); int num_fds = getdtablesize();
signal (SIGTERM, SIG_DFL);
/* This is the child closing its file descriptors. */ /* This is the child closing its file descriptors. */
for (fl= 0; fl <= num_fds; ++fl) for (fl= 0; fl <= num_fds; ++fl)
@ -1090,7 +1112,16 @@ int main(argc, argv)
setsid(); setsid();
} }
else else
exit(0); {
/*
* Parent process
*/
sleep(300);
/*
* Not reached unless something major went wrong.
*/
exit(1);
}
} }
else else
{ {
@ -1166,6 +1197,11 @@ int main(argc, argv)
InitMsyms(); InitMsyms();
} }
#ifndef TESTING
if (getpid() != ppid)
kill (ppid, SIGTERM);
#endif
/* The main loop. */ /* The main loop. */
while (1) while (1)
{ {