static int daemonize (void) /* {{{ */
{
- pid_t child;
int status;
- char *base_dir;
/* These structures are static, because `sigaction' behaves weird if the are
* overwritten.. */
static struct sigaction sa_term;
static struct sigaction sa_pipe;
- if (stay_foreground)
- goto child_startup;
-
- child = fork ();
- if (child < 0)
+ if (!stay_foreground)
{
- fprintf (stderr, "daemonize: fork(2) failed.\n");
- return (-1);
- }
- else if (child > 0)
- {
- return (1);
- }
+ pid_t child;
+ char *base_dir;
- /* Change into the /tmp directory. */
- base_dir = (config_base_dir != NULL)
- ? config_base_dir
- : "/tmp";
- status = chdir (base_dir);
- if (status != 0)
- {
- fprintf (stderr, "daemonize: chdir (%s) failed.\n", base_dir);
- return (-1);
- }
+ child = fork ();
+ if (child < 0)
+ {
+ fprintf (stderr, "daemonize: fork(2) failed.\n");
+ return (-1);
+ }
+ else if (child > 0)
+ {
+ return (1);
+ }
+
+ /* Change into the /tmp directory. */
+ base_dir = (config_base_dir != NULL)
+ ? config_base_dir
+ : "/tmp";
+ status = chdir (base_dir);
+ if (status != 0)
+ {
+ fprintf (stderr, "daemonize: chdir (%s) failed.\n", base_dir);
+ return (-1);
+ }
- /* Become session leader */
- setsid ();
+ /* Become session leader */
+ setsid ();
- /* Open the first three file descriptors to /dev/null */
- close (2);
- close (1);
- close (0);
+ /* Open the first three file descriptors to /dev/null */
+ close (2);
+ close (1);
+ close (0);
- open ("/dev/null", O_RDWR);
- dup (0);
- dup (0);
+ open ("/dev/null", O_RDWR);
+ dup (0);
+ dup (0);
+ } /* if (!stay_foreground) */
-child_startup:
/* Install signal handlers */
memset (&sa_int, 0, sizeof (sa_int));
sa_int.sa_handler = sig_int_handler;