Main Page | Modules | Data Structures | File List | Data Fields | Related Pages

Daemonize Functions
[SB Library]

Daemonize Functions. More...

Defines

#define MAX_PIDDATA_LENGTH   16
 Maximum length of pidfile data.

Functions

int sb_daemon_drop_privileges (void)
 Drop privileges.
int sb_daemon_no_corefile (void)
 Suppress generating core files.
int sb_daemon_is_started_by_init (void)
 Check if daemon was started by init process.
int sb_daemon_is_started_by_inetd (void)
 Check if daemon was started by inetd process.
int sb_daemon_is_started_by_root (void)
 Check if daemon was started by root.
int sb_daemon_is_started_suid (void)
 Check if daemon was started suid.
int sb_daemon_create_fd (int fd, int mode)
 Create file descriptor with given mode.
int sb_daemon_change_user (uid_t uid, gid_t gid)
 Change to given uid and gid.
int sb_daemon_detach (const char *rundir)
 Daemonize process.
int sb_daemon_init (const char *name, const char *rundir, const char *pidfile)
 Initialize daemon.
int sb_daemon_is_running (const char *pidfile)
 Check if daemon is running.
int sb_daemon_destroy (const char *pidfile)
 Quit daemon.
int sb_daemon_pidfile_create_and_lock (const char *pidfile)
 Create the pidfile.
int sb_daemon_pidfile_write (int fd)
 Write pid to pidfile.
pid_t sb_daemon_pidfile_read (const char *pidfile)
 Read pid from pidfile.

Detailed Description

Daemonize Functions.

This module provides functions for writing daemons. A daemon is a computer program which runs in the background. Usually it runs all the time and is unnoticed by users, until it got woken up by some actions. Most daemons are started by the root user at boot time via RC scripts or System V init scripts.

Many tasks have to be done to set up a daemon correctly and this module will perform them for you (Sometimes I will release a library with these and more functions to simplify programming in a *nix environment).


Function Documentation

int sb_daemon_change_user uid_t  uid,
gid_t  gid
 

Change to given uid and gid.

This function tries to become the given user. It will only work, if daemon has been started as root user. So it will return -1, if you are not root while starting the daemon. It will first change gid and than uid. Real and effective IDs will be checked to match the given values. All tests have to be passed, to return success.

Parameters:
uid uid to change to
gid gid to change to
Returns:
0 if successfull, -1 if error

Definition at line 186 of file daemonize.c.

References sb_daemon_is_started_by_root().

int sb_daemon_create_fd int  fd,
int  mode
 

Create file descriptor with given mode.

This function is internally used for creating stdin, stdout and stderr after all file descriptors were closed. You will not need to call this function, it's meant to be used internally.

Definition at line 164 of file daemonize.c.

Referenced by sb_daemon_detach().

int sb_daemon_destroy const char *  pidfile  ) 
 

Quit daemon.

Parameters:
pidfile path to pid file
Returns:
0 if successfull, -1 if error

Definition at line 413 of file daemonize.c.

References sb_file_remove().

Referenced by main().

int sb_daemon_detach const char *  rundir  ) 
 

Daemonize process.

Will do all the forking, change directory, ... stuff for you. At least you have to call this function to become a daemon.

Parameters:
rundir working directory for process
Returns:
0 if successfull, -1 if error

Definition at line 209 of file daemonize.c.

References sb_daemon_create_fd(), sb_daemon_is_started_by_inetd(), sb_daemon_is_started_by_init(), and sb_daemon_is_started_by_root().

Referenced by sb_daemon_init().

int sb_daemon_drop_privileges void   ) 
 

Drop privileges.

Use it, if you only need special privileges on startup and can drop them later (most daemons will match this rule). If your program becomes unsafe running privileged, then use this function. Another reason to call this function is, if you want to ignore the filesystems suid bits for your daemons binary. Use the daemon_change_user() function to become a special user.

Returns:
0 if successfull, -1 if error

Definition at line 54 of file daemonize.c.

Referenced by main().

int sb_daemon_init const char *  name,
const char *  rundir,
const char *  pidfile
 

Initialize daemon.

Parameters:
name name of process
rundir working directory for process
pidfile path to pid file
Returns:
0 if successfull, -1 if error

Definition at line 345 of file daemonize.c.

References EM_TYPE_SYSLOG, emClose(), emInit(), sb_daemon_detach(), sb_daemon_is_running(), sb_daemon_pidfile_create_and_lock(), and sb_daemon_pidfile_write().

Referenced by main().

int sb_daemon_is_running const char *  pidfile  ) 
 

Check if daemon is running.

Parameters:
pidfile path to pid file
Returns:
1 if daemon is running, 0 if not and -1 if error

Definition at line 384 of file daemonize.c.

References ERRNO, sb_daemon_pidfile_create_and_lock(), sb_file_close(), and sb_file_remove().

Referenced by main(), and sb_daemon_init().

int sb_daemon_is_started_by_inetd void   ) 
 

Check if daemon was started by inetd process.

If your program was started by inetd, than you will get a socket for communication with inetd using stdin, stdout and stderr. It would be bad to close these file descriptors. Other tasks of daemonizing a process - e.g. setsid() - will fail if started by init.

Returns:
1 if started by inetd, 0 if not

Definition at line 115 of file daemonize.c.

Referenced by sb_daemon_detach().

int sb_daemon_is_started_by_init void   ) 
 

Check if daemon was started by init process.

Needed, if you are started by init process. It's no good idea to fork, if your program is started by init. Your program might be restarted by init if configured as respawning program, because of exiting main process after forking. Other tasks of daemonizing a process - e.g. setsid() - will fail if started by init.

Returns:
1 if started by init, 0 if not

Definition at line 102 of file daemonize.c.

Referenced by sb_daemon_detach().

int sb_daemon_is_started_by_root void   ) 
 

Check if daemon was started by root.

Some system functions are not allowed to execute, if you're not root. You can check for this using this function.

Returns:
1 if started by root, 0 if not

Definition at line 132 of file daemonize.c.

Referenced by main(), sb_daemon_change_user(), and sb_daemon_detach().

int sb_daemon_is_started_suid void   ) 
 

Check if daemon was started suid.

Use this function to check, if the daemon was started suid. Starting a daemon with filesystems suid bits can be a security risc. You really should check for that. Use daemon_change_user() to become a specific user.

Returns:
1 if started suid, 0 if not

Definition at line 144 of file daemonize.c.

Referenced by main().

int sb_daemon_no_corefile void   ) 
 

Suppress generating core files.

Use it, if your program should not generate core files. It can be a security risc, because of sensitive data stored in core files. Most users should be using this function.

Returns:
0 if successfull, -1 if error

Definition at line 82 of file daemonize.c.

Referenced by main().

int sb_daemon_pidfile_create_and_lock const char *  pidfile  ) 
 

Create the pidfile.

Create and lock the pidfile. If error is returned, errno is set appropriately.

Parameters:
pidfile path to pid file
Returns:
file descriptor of pidfile if successfull, -1 if error

Definition at line 424 of file daemonize.c.

References sb_file_lock_acquire_write(), sb_file_open(), and sb_file_open_ext().

Referenced by main(), sb_daemon_init(), and sb_daemon_is_running().

pid_t sb_daemon_pidfile_read const char *  pidfile  ) 
 

Read pid from pidfile.

Parameters:
pidfile path to pid file
Returns:
pid if successfull, -1 if error

Definition at line 491 of file daemonize.c.

References MAX_PIDDATA_LENGTH, and sb_file_open().

Referenced by main().

int sb_daemon_pidfile_write int  fd  ) 
 

Write pid to pidfile.

Parameters:
fd file descriptor for opened pidfile
Returns:
0 if successfull, -1 if error

Definition at line 460 of file daemonize.c.

References MAX_PIDDATA_LENGTH.

Referenced by main(), and sb_daemon_init().


Generated on Wed Mar 30 13:43:27 2005 for Mntd by  doxygen 1.3.9.1