Defines | Functions | Variables

daemonize.c File Reference

SIP-router core ::Module: SIP-router core. More...

#include <sys/types.h>
#include <signal.h>
#include <syslog.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include "daemonize.h"
#include "globals.h"
#include "dprint.h"
#include "signals.h"
#include "cfg/cfg.h"
Include dependency graph for daemonize.c:

Go to the source code of this file.

Defines

Functions

Variables


Detailed Description

Definition in file daemonize.c.


Function Documentation

void daemon_status_init ( void   ) 

Must be called before any other daemon_status function has a chance to run.

Definition at line 98 of file daemonize.c.

References daemon_status_fd.

void daemon_status_no_wait ( void   ) 

Should be called after fork(), for each new process that doesn't use daemon_status_wait().

Definition at line 200 of file daemonize.c.

References daemon_status_fd.

Referenced by daemonize().

Here is the caller graph for this function:

void daemon_status_on_fork_cleanup (  ) 

Should be called after fork(), for each new process that _does_ _not_ use daemon_status_send() or daemon_status_wait().

Definition at line 182 of file daemonize.c.

References daemon_status_fd.

Referenced by fork_process().

Here is the caller graph for this function:

int daemon_status_pre_daemonize ( void   ) 

Must be called before forking. Typically the parent process will call daemon_status_wait() while one of the children will call daemon_status_send() at some point.

Returns:
0 on success, -1 on error (and sets errno).

Definition at line 113 of file daemonize.c.

References daemon_status_fd.

Referenced by daemonize().

Here is the caller graph for this function:

int daemon_status_send ( char  status  ) 
Parameters:
status - status byte
Returns:
0 on success, -1 on error. Note: if init is not complete (only init, but no pre-daemonize) it will return success always.

Definition at line 163 of file daemonize.c.

References daemon_status_fd.

int daemon_status_wait ( char *  status  ) 
Parameters:
status - filled with the sent status (a char).
Returns:
0 on success, -1 on error (e.g. process died before sending status, not intialized a.s.o.). Side-effects: it will close the write side of the pipe (must not be used from the same process as the daemon_status_send()). Note: if init is not complete (only init, but no pre-daemonize) it will return success always and status 0.

Definition at line 135 of file daemonize.c.

References daemon_status_fd.

Referenced by daemonize().

Here is the caller graph for this function:

int daemonize ( char *  name,
int  status_wait 
)
Parameters:
name - daemon name used for logging (used when opening syslog).
status_wait - if 1 the original process will wait until it gets an exit code send using daemon_status_send().
Returns:
0 in the child process (in case of daemonize mode), -1 on error. The original process that called daemonize() will be terminated if dont_daemonize == 0. The exit code depends on status_wait. If status_wait is non-zero, the original process will wait for a status code, that must be sent with daemon_status_send() (daemon_status_send() must be called or the original process will remain waiting until all the children close()). If status_wait is 0, the original process will exit immediately with exit(0). Global variables/config params used: dont_daemonize chroot_dir working_dir pid_file - if set the pid will be written here (ascii). pgid_file - if set, the pgid will be written here (ascii). log_stderr - if not set syslog will be opened (openlog(name,...))

Side-effects: sets own_pgid after becoming session leader (own process group).

Definition at line 269 of file daemonize.c.

References daemon_status_fd, daemon_status_no_wait(), daemon_status_pre_daemonize(), daemon_status_wait(), enable_dumpable(), log_facility, and log_stderr.

Here is the call graph for this function:

int enable_dumpable ( void   ) 
Returns:
0 when no critical error occured, -1 on such error

Definition at line 213 of file daemonize.c.

Referenced by daemonize().

Here is the caller graph for this function:


Variable Documentation

int daemon_status_fd[2] [static]

This pipe is used to send the desired exit status to the initial process, that waits for it in the foreground. This way late errors preventing startup (e.g. during modules child inits or TCP late init) can still be reported back.

Definition at line 90 of file daemonize.c.

Referenced by daemon_status_init(), daemon_status_no_wait(), daemon_status_on_fork_cleanup(), daemon_status_pre_daemonize(), daemon_status_send(), daemon_status_wait(), and daemonize().