Daemon-daemon practices

Source: Internet
Author: User
Tags signal handler superuser permission

In UNIX systems, daemon follows the following common practices:

  • If the daemon uses a lock file, the file is usually stored in the/var/run directory.. Note: The daemon may require the superuser permission to create files in this directory.The name of the lock file is usually name. pid, where name is the name of the daemon or service.For example, the name of the cron daemon lock file is/var/run/crond. pid.
  • If the daemon supports configuration options, the configuration files are usually stored in the/etc directory. The configuration file name is usually name. conf, where name is the name of the daemon or service.. For example, the configuration file of the syslogd daemon is/etc/syslog. conf.
  • Daemon can be started using command lines, but they are usually started by one of the system initialization scripts (/etc/rc * or/etc/init. d /*).. If the daemon should be restarted automatically when it is terminated, we can include _ respawn record items for the daemon in the/etc/inittab, init restarts the daemon.
  • If a daemon has a configuration file, when the daemon starts, it will read the file, but it will not be viewed later. If an administrator changes the configuration file, the daemon may need to be stopped and then started to make the change take effect.To avoid this problem, some daemon will capture the SIGHUP signal and repeat the configuration file when they receive the signal. Because daemon processes are not combined with terminals, they are the first session process of the uncontrolled terminal, or members of the orphan process group, the daemon does not expect to receive SIGHUP. Therefore, they can safely reuse it.

Instance

The program shown in program list 13-3 illustrates a way for a daemon to repeat its configuration file. The program uses sigwait and multithreading (refer to http://www.cnblogs.com/nufangrensheng/p/3540453.html ).

Program list 13-3 The Daemon reread the configuration file

#include <pthread.h><syslog.h>  already_running( * *= sigwait(&mask, &(err != ( argc,  *                *((cmd = strrchr(argv[], )) === argv[++    =&= (sigaction(SIGHUP, &sa, NULL) < &((err = pthread_sigmask(SIG_BLOCK, &mask, NULL)) != = pthread_create(&tid, NULL, thr_fn, (err !=     

This program calls daemonizein http://www.cnblogs.com/nufangrensheng/p/3544104.htmlto keep the thread running. After returning from this function, call callback. At this point, the SIGHUP signal is still ignored (???), Therefore, the system's default processing method for this signal needs to be restored; otherwise, the thread that calls sigwait will never see this signal.

As is recommended for multithreaded programs, we block the signal and create a thread to process the signal. The only task of this thread is to wait for SIGHUP and SIGTERM. When the SIGHUP signal is received, the thread calls the reread function to reread its configuration file. When it receives the SIGTERM signal, it records a message and terminates it.

Recall interval. Because we have blocked these signals, when these signals are generated for the process, the daemon will not die, but will call the sigwait thread to indicate that the signal has been received when it returns.

Instance

For example. Because of this, it is difficult to identify a proper process for the signal in the 13-3 program list. In addition, due to implementation differences, it is not guaranteed that the daemon will respond as expected.

The program list 13-4 indicates that the daemon can capture SIGHUP and re-read its configuration file without multithreading.

#include <syslog.h><errno.h>  lockfile(  already_running( argc,  *            *((cmd = strrchr(argv[], )) === argv[++    =&&= (sigaction(SIGTERM, &sa, NULL) < =&&= (sigaction(SIGHUP, &sa, NULL) <     

After the daemon is initialized, we configure the signal handler for SIGHUP and SIGTERM. We can put the re-reading logic in the signal processing program, or we can set only one flag in it, and the main thread of the daemon process will do all the necessary work.

This blog is excerpted from advanced programming for UNIX environments (version 2) and used only for personal learning records. For more information about this book, see:Http://www.apuebook.com/.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.