1 / 9

Daemon Processes

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

moe
Download Presentation

Daemon Processes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Daemon Processes • Daemon processes and how to daemonize process • syslogd daemon and syslog function • Readings • UNP Ch13

  2. 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

  3. 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

  4. 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);

  5. 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);

  6. 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

  7. 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

  8. 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);

  9. Daemon Process Example • See example1.c

More Related