How do I get my program to act like a daemon?
2014-02-19 21:00
627 查看
Here are the steps to become a daemon:
1. fork() so the parent can exit, this returns control to the command line or shell invoking your program. This step is required so that the new process is guaranteed not to be a process group leader. The next step, setsid(), fails if you're a process group
leader.
2. setsid() to become a process group and session group leader. Since a controlling terminal is associated with a session, and this new session has not yet acquired a controlling terminal our process now has no controlling terminal, which is a Good Thing for
daemons.
3. fork() again so the parent, (the session group leader), can exit. This means that we, as a non-session group leader, can never regain a controlling terminal.
4. chdir("/") to ensure that our process doesn't keep any directory in use. Failure to do this could make it so that an administrator couldn't unmount a filesystem, because it was our current directory. [Equivalently, we could change to any directory containing
files important to the daemon's operation.]
5. umask(0) so that we have complete control over the permissions of anything we write. We don't know what umask we may have inherited. [This step is optional]
6. close() fds 0, 1, and 2. This releases the standard in, out, and error we inherited from our parent process. We have no way of knowing where these fds might have been redirected to. Note that many daemons use sysconf() to determine the limit _SC_OPEN_MAX.
_SC_OPEN_MAX tells you the maximun open files/process. Then in a loop, the daemon can close all possible file descriptors. You have to decide if you need to do this or not. If you think that there might be file-descriptors open you should close them, since
there's a limit on number of concurrent file descriptors.
7. Establish new open descriptors for stdin, stdout and stderr. Even if you don't plan to use them, it is still a good idea to have them open. The precise handling of these is a matter of taste; if you have a logfile, for example, you might wish to open it
as stdout or stderr, and open `/dev/null' as stdin; alternatively, you could open`/dev/console' as stderr and/or stdout, and `/dev/null' as stdin, or any other combination that makes sense for your particular daemon.
来源于Unix Programming Frequently Asked Questions:
http://web.archive.org/web/20120418113033/http://www.steve.org.uk/Reference/Unix/faq_toc.html
1. fork() so the parent can exit, this returns control to the command line or shell invoking your program. This step is required so that the new process is guaranteed not to be a process group leader. The next step, setsid(), fails if you're a process group
leader.
2. setsid() to become a process group and session group leader. Since a controlling terminal is associated with a session, and this new session has not yet acquired a controlling terminal our process now has no controlling terminal, which is a Good Thing for
daemons.
3. fork() again so the parent, (the session group leader), can exit. This means that we, as a non-session group leader, can never regain a controlling terminal.
4. chdir("/") to ensure that our process doesn't keep any directory in use. Failure to do this could make it so that an administrator couldn't unmount a filesystem, because it was our current directory. [Equivalently, we could change to any directory containing
files important to the daemon's operation.]
5. umask(0) so that we have complete control over the permissions of anything we write. We don't know what umask we may have inherited. [This step is optional]
6. close() fds 0, 1, and 2. This releases the standard in, out, and error we inherited from our parent process. We have no way of knowing where these fds might have been redirected to. Note that many daemons use sysconf() to determine the limit _SC_OPEN_MAX.
_SC_OPEN_MAX tells you the maximun open files/process. Then in a loop, the daemon can close all possible file descriptors. You have to decide if you need to do this or not. If you think that there might be file-descriptors open you should close them, since
there's a limit on number of concurrent file descriptors.
7. Establish new open descriptors for stdin, stdout and stderr. Even if you don't plan to use them, it is still a good idea to have them open. The precise handling of these is a matter of taste; if you have a logfile, for example, you might wish to open it
as stdout or stderr, and open `/dev/null' as stdin; alternatively, you could open`/dev/console' as stderr and/or stdout, and `/dev/null' as stdin, or any other combination that makes sense for your particular daemon.
#include <unistd.h> int daemon() { //1. the new process is guaranteed not to be a process group leader pid_t pid = fork(); if(pid == -1) { //errno return -1; } else if(pid == 0) { ///child process //2. become a process group and session group leader pid = setsid(); if(pid == -1) { //errno return -1; } //3. as a non-session group leader, can never regain a controlling terminal pid = fork(); if(pid == -1) { //errno return -1; } else if(pid == 0) { ///child process //4. our process doesn't keep any directory in use // [Equivalently, we could change to any directory containing files important to the daemon's operation.] int iRet = chdir("/"); if(iRet == -1) { //errno return -1; } //5. we have complete control over the permissions of anything we write // [This step is optional] umask(0); //6. close() fds 0, 1, and 2 //7. Establish new open descriptors for stdin, stdout and stderr int iMaxfd = open("/dev/null", O_RDWR); if(iMaxfd == -1) { //errno return -1; } //redirect 0, 1, 2 dup2(iMaxfd, 0); dup2(iMaxfd, 1); dup2(iMaxfd, 2); close(iMaxfd); for(int i = 2; i < iMaxfd - 1; i ++) { close(i); } return 0; } else { //parent process exit(0); return 0; } } else { //parent process exit(0); return 0; } }
来源于Unix Programming Frequently Asked Questions:
http://web.archive.org/web/20120418113033/http://www.steve.org.uk/Reference/Unix/faq_toc.html
相关文章推荐
- How do I add capabilities to my executable in qt for symbian?
- How do I get Chinese input to work?
- I was eager to get my paddles wet and see how this kayak performed in the water.
- How do you link a C++ program to C functions?
- How do you get the server to recognize changes to groups immediately?
- How do I use locales and resource bundles to internationalize my application?
- How do I get multiple process to log to the same file?
- question 002: dev c++ 当中如何调整字体大小?How to get the first program with C++? c++属于什么软件?
- How do you force a java swt program to “move itself to the foreground”?
- How do I add a directory to my include path (@INC) at runtime?
- How to run a Java Program as a daemon (service) on Linux (openSUSE) using a shell script
- How to do Photoshop-like effects in SVG
- How do I perform a manual full refresh instead of using the Apply program to do the full refresh?
- How do engines like Unreal relate to OpenGL or D3D?
- How to Get My Paper Accepted at Top Software Engineering Conferences
- How To Do A JavaScript Cross-Domain POST or GET With jQuery or XMLHttpRequest
- How do I download or save a YouTube video to my computer?
- jsvc - How to convert a java program to daemon with jsvc
- How do I get ASP.NET Web API to return JSON instead of XML using Chrome
- How do I get Chinese input to work?