This article introduces the concept and structure of daemon, and describes how to write daemon and report process errors.
1. concept:
Daemon is a long-lived daemon. they are often started during system auto-lifting and terminated only when the system is shut down. because they do not have control terminals, they are running in the background. first, we will introduce some common Daemon Processes in Linux:
- Init: The PID is 1. It is a system daemon and is responsible for starting system services. These services usually have their own daemon.
- Keventd: Provides the process context for the function running in the kernel.
- Kapmd: provides support for advanced power management in computer systems.
- Kswapd: pageout daemon, which writes dirty pages to disks at a low speed so that these pages can still be recycled as needed, this method supports the virtual storage subsystem.
- Bdflush: when the available memory reaches the limit, the dirty buffer is flushed from the buffer pool to the disk.
- Kupdated: fl dirty pages to the disk.
- Portmap: provides services that map Remote Procedure Call (RPC) program numbers to network port numbers.
- Syslogd: provides an interface for logging system messages for required programs. It can be printed to termino or written to a file.
- Inetd: listens on system network interfaces to obtain various network service requests from the network.
- Crond: Execute the specified command on the specified date and time, so that the program can be executed regularly.
- Cupsd: print the offline process, which processes all print requests to the system.
- NFSD, lockd, rpciod: Network File System ).
2. programming rules:
- First, you must call umask to set the file mode to 0.
- Call fork and then exit the parent process. To achieve the following:
- If the daemon is started as a simple shell command, the termination of the parent process makes the shell think that the command has been executed.
- The child process inherits the process group ID of the parent process, but has a new process ID, which ensures that the child process is not the leader process of the process group. this is not necessary for the setsid call in step 3.
- Call setsid to create a new session so that the calling process:
- Becomes the first process of a new session.
- Become the leader process of a new process group.
- No control terminal.
- Change the current working directory to the root directory to prevent the current working directory from being in the mounted file system.
- Disable unnecessary file descriptors.
- Some daemon enable/dev/null, which is suitable for filedes 0 1 2, so that standard input, output, and error routines will not produce any effect.
3. Error records:
The daemon has no control terminal, so you cannot use a simple perror to Implement error reporting. In Linux, there is a centralized daemon error logging facility, which is syslog.
There are three methods to generate log messages:
- The kernel routine can call the log function. Any user process can obtain this information by reading/dev/klog. Of course, you need to open this device first.
- Most user processes (Daemon) Call the syslog function to generate a log message, which sends the message to the UDP socket:/dev/log in the Unix domain.
- A user process on this host, or a user process connected to this host through a TCP/IP network, can send log messages to UDP port 514.
- Note that syslog does not generate these UDP datagram, but requires the process that generates the log message to perform network programming.
The SYSLOG function is as follows:
- Header file: <syslog. h>
- Prototype:
- Void openlog (const char * Ident, int option, int facility );
- Void syslog (INT priority, const char * format ,...);
- Void closelog ();
- Int setlogmask (INT maskpri );
- Returned value: the priority blocking value of the previous log record.
- Note:
- Openlog is optional. If you do not call openlog, it automatically calls openlog when you call syslog for the first time.
- Closelog call is also optional. It only closes the descriptor that was used to communicate with the syslogd daemon.
- Parameters:
- IDENT: log adds it to each log message. It is generally the program name (such as inetd ).
- Option: Specifies the blocking option.
- Log_cons: If the log message cannot be submitted to syslogd through Unix domain data, the message is written to the console.
- Log_ndelay: Open the Unix-domain datagram socket to syslogd immediately, instead of record the first message.
- Log_nowait: A sub-process that may be created when a message is logged. The application that captures the sigchld signal is blocked.
- Log_odelay: delay the connection to syslogd before the first message is recorded.
- Log_perror: in addition to sending the log message to Syslogd, it also writes to standard errors.
- Log_pid: each message contains a process ID.
- Facility: Let the configuration file describe that messages from different facilities will be processed in different ways.
- Log_auth: authorization program: Login, Su, Getty, etc.
- Log_authpriv: it is the same as log_auth, but it has permission restrictions when writing log files.
- Log_cron: cron and.
- Log_daemon: System daemprocess: inetd, routed, etc.
- Log_ftp: ftp daemon (ftpd ).
- Log_kern: Message generated by the kernel.
- LOG_LOCAL0-9: reserved for use locally.
- Log_lpr: Row printing system: LPD, LPC, etc.
- Log_mail: email system.
- Log_news: Usenet network news system.
- Log_syslog: syslogd daemon.
- Log_user: messages from other user processes (default ).
- Log_uucp: uucp system.
- Priority: this parameter is a combination of facility and level, which can be (from high to low ):
- Log_emerg: Emergency (the system is unavailable ).
- Log_alert: status that must be fixed immediately.
- Log_crit: critical.
- Log_err: Error status.
- Log_warning: warning status.
- Log_notice: normal, but important.
- Log_info: information message.
- Log_debug: Debug message.
There is also a variant function:
# Include <syslog. h>
# Include <stdarg. h>
Void vsyslog (INT priority, const char * format, va_list Arg );
4. Simple Example:
Openlog ("lpd", log_pid, log_lpr );
Syslog (log_err, "Open error for % s: % m", filename );
Equivalent:
Syslog (log_err | log_lpr, "Open error for % s: % m", filename );
However, we recommend that you use the first method, with open and close, to make the rules clearer.