260 likes | 1.07k Views
Daemon Processes. Daemon processes and how to daemonize process syslogd daemon and syslog function Readings UNP Ch13. Daemon Processes. A daemon process Background process not associated with a controlling terminal Normally long-lived Unix systems have many daemon processes running
E N D
Daemon Processes • Daemon processes and how to daemonize process • syslogd daemon and syslog function • Readings • UNP Ch13
Daemon Processes • A daemon process • Background process not associated with a controlling terminal • Normally long-lived • Unix systems have many daemon processes running • init, syslogd, inetd/xinetd, crond, etc • ps -axj
Daemon Process Coding Rules • Set file mode creation mask to 0 (umask) • Allowing daemon to set specific file permissions • Create child process and let parent process exit (fork) • pid != ppid in child process, child not group leader • Prerequisite for start a new session • Create a new session (setsid) • Becomes session leader, has no controlling terminal • Step 2 is recommended again (fork and exit parent process) • Change working directory to root directory • Allows mounted file system to be unmounted • This step depends on specifics of daemon process • Unneeded file descriptors should be closed • Redirect descriptors 0, 1, 2 to /dev/null
How to Daemonize Process umask(0); // clear file creation mask if ((pid = fork()) < 0) return -1; if (pid) exit(0); // exit the parent process // now child is not group leader // create a new session and become session leader // new session does not have controlling terminal if (setsid() < 0) return -1; signal(SIGHUP, SIG_IGN); // ignoring SIGHUP If ((pid = fork()) < 0) return -1; If (pid) exit(0); // exit first child process to ensure we will not // accidentally obtain controlling terminal chdir(“/”); // change working directory to root directory close(all file descriptors); // redirect stdin, stdout, stderr open(“/dev/null”, O_RDONLY); open(“/dev/null”, RDWR); open(“/dev/null”, RDWR); openlog(…); //preparing log file return(0);
How to Daemonize a Process • Nochdir: if change to root directory “/”; • Noclose: if redirect stdin, stdout, stderr, to /dev/null #include <unistd.h> int daemon(intnochdir, intnoclose);
Error Logging • How to output messages when something happens? • Daemon processes do not have controlling terminal • Don’t want each daemon to have its own log file • A central daemon error-logging facility • syslogd • A daemon process waiting for messages from other processes
Syslogd Daemon • Different ways to communicate with syslogd • Unix domain socket • UDP socket (on port 514) • A file opened for accepting messages from kernel /etc/syslog.conf user process syslogd syslog /dev/log UDP port 514 /dev/klog log Unix domain datagram socket Internet domain datagram socket Kernel routines kernel
Syslog Function #include <syslog.h> void openlog(const char *ident, int option, int facility); void syslog(int priority, const char *msg, …); void closelog(void); • Ident • Some identifier added to each log message • Option • Various control options • Facility specifies type of sending processes • LOG_CRON, LOG_FTP, LOG_KERN, LOG_MAIL, etc. • Priority • Combination of level and facility • 8 levels are defined • LOG_WARNING, LOG_NOTICE (default), LOG_INFO, etc openlog(“lpd”, LOG_PID, LOG_LPR); syslog(LOG_ERR, “open error for %s: %m”, filename);
Daemon Process Example • See example1.c