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. |
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).
|
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.
Definition at line 186 of file daemonize.c. References sb_daemon_is_started_by_root(). |
|
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(). |
|
Quit daemon.
Definition at line 413 of file daemonize.c. References sb_file_remove(). Referenced by main(). |
|
Daemonize process. Will do all the forking, change directory, ... stuff for you. At least you have to call this function to become a daemon.
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(). |
|
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.
Definition at line 54 of file daemonize.c. Referenced by main(). |
|
Initialize daemon.
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(). |
|
Check if daemon is running.
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(). |
|
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.
Definition at line 115 of file daemonize.c. Referenced by sb_daemon_detach(). |
|
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.
Definition at line 102 of file daemonize.c. Referenced by sb_daemon_detach(). |
|
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.
Definition at line 132 of file daemonize.c. Referenced by main(), sb_daemon_change_user(), and sb_daemon_detach(). |
|
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.
Definition at line 144 of file daemonize.c. Referenced by main(). |
|
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.
Definition at line 82 of file daemonize.c. Referenced by main(). |
|
Create the pidfile. Create and lock the pidfile. If error is returned, errno is set appropriately.
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(). |
|
Read pid from pidfile.
Definition at line 491 of file daemonize.c. References MAX_PIDDATA_LENGTH, and sb_file_open(). Referenced by main(). |
|
Write pid to pidfile.
Definition at line 460 of file daemonize.c. References MAX_PIDDATA_LENGTH. Referenced by main(), and sb_daemon_init(). |