disowning a process
Hi, I'm not sure if there's a posix way of doing this, but wanted to check if there is a way in linux.
I want to have a daemon that fork/execs a new process, but don't want (for various reasons) the responsibility for cleaning up those process with the wait() function family. I'm assuming that if the init process became the parent of one of these forked processes, then it would clean them up for me (is this assumption true?). Besides the daemon process exiting, is there a way to disown the process on purpose so that init inherits it?
Thanks,
Davy
Quoted 10 lines · Davy Durham
Hi, I'm not sure if there's a posix way of doing this, but wanted to check if there is a way in linux.
I want to have a daemon that fork/execs a new process, but don't want (for various reasons) the responsibility for cleaning up those process
with thewait()function family. I'm assuming that if the init process
became the parent of one of these forked processes, then it would clean
them up for me (is this assumption true?). Besides the daemon process
exiting, is there a way to disown the process on purpose so that init inherits it?
Try man daemon.
The way I use to do it was simply do a double fork. That is (simplified)...
if ((pid = fork()) < 0) {
perror("fork");
} else if (!pid) {
/* child */
if ((pid = fork()) < 0) {
perror("child fork");
exit(-1);
} if (pid) {
/* child parent */
/* Here we detach from the child */
exit(0);
}
/* Now this code is a child running almost as a daemon
with init as the parent. */
setsid();
/* Now the child is completely detached from the original
parent */
/* ... daemon code here ... */
exit(0);
}/* parent code here */
-- Steve
Cool.. I looked at the daemon function and I might be able to use it..
However, I compiled your code... seems to work.. but where is the wait() done on the middle parrent so that it isn't left defunct?
Quoted 32 lines · Steven Rostedt
Try man daemon.
The way I use to do it was simply do a double fork. That is (simplified)...
if ((pid = fork()) < 0) { perror("fork"); } else if (!pid) { /* child */ if ((pid = fork()) < 0) { perror("child fork"); exit(-1); } if (pid) { /* child parent */ /* Here we detach from the child */ exit(0); } /* Now this code is a child running almost as a daemon with init as the parent. */ setsid(); /* Now the child is completely detached from the original parent */ /* ... daemon code here ... */ exit(0); }/* parent code here */
-- Steve
Quoted 4 lines · Davy Durham
Cool.. I looked at the daemon function and I might be able to use it..
However, I compiled your code... seems to work.. but where is the
wait()done on the middle parrent so that it isn't left defunct?
Sorry, forgot about that...
[...]
Quoted 2 lines
Quoted 2 lines
/* parent code here */
waitpid(pid,&status,0);
/* and then you can look at WEXITSTATUS(status) to
see if the second fork succeeded. */
if (WEXITSTATUS(status)) {
/* failed */
} else {
/* succeeded */
}/* Here the second child and parent are now divorced */
-- Steve
Quoted 4 lines · Davy Durham
Cool.. I looked at the daemon function and I might be able to use it..
However, I compiled your code... seems to work.. but where is the
wait()done on the middle parrent so that it isn't left defunct?
I added "waitpid(pid,NULL,0);" after the outer-most if.. which is where the grand parent cleans up the pid of the intermediate parent.
However, now trying the daemon() function.. which seems to work the same way.. it definately leaves a pid around.. so I guess you really need to do a wait() in the parent after forking.. however you don't know exactly which pid to wait for so you might be reaping some other child you've previously spawned..
So, for now I'm going to stick with the explicit double fork code.
Thanks!
Quoted 42 lines
Quoted 26 lines · Steven Rostedt
Try man daemon.
The way I use to do it was simply do a double fork. That is (simplified)...
if ((pid = fork()) < 0) { perror("fork"); } else if (!pid) { /* child */ if ((pid = fork()) < 0) { perror("child fork"); exit(-1); } if (pid) { /* child parent */ /* Here we detach from the child */ exit(0); } /* Now this code is a child running almost as a daemon with init as the parent. */ setsid(); /* Now the child is completely detached from the original parent */ /* ... daemon code here ... */ exit(0); }Quoted 6 lines
/* parent code here */
-- Steve
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Quoted 4 lines · Davy Durham
Davy Durham wrote:
I added "
waitpid(pid,NULL,0);" after the outer-most if.. which is where the grand parent cleans up the pid of the intermediate parent.
That's correct.
Quoted 6 lines
However, now trying the
daemon()function.. which seems to work the same way.. it definately leaves a pid around.. so I guess you really need to do await()in the parent after forking.. however you don't know exactly which pid to wait for so you might be reaping some other child you've previously spawned..
The daemon function is really just the inner fork and setsid. So with using the daemon function it looks like this:
if ((pid = fork()) < 0) {
perror("fork");
} else if (!pid) {
if (daemon(0,0) < 0) {
exit(-1);
}
/* daemon code here */
exit(0);
}
waitpid(pid,NULL,0);-- Steve
Quoted 1 line · Davy Durham
Cool.. I looked at the daemon function and I might be able to use it..
Using daemon() is generally wise - it is basically a double fork and then one exits so that the orphan child becomes owned by init. However it also knows about platform specific considerations like setpgrp v setsid, whether an ioctl must be done to disown the controlling tty etc which can be fairly OS generation specific.
Quoted 7 lines · Alan Cox
Quoted 3 lines · Davy Durham
Cool.. I looked at the daemon function and I might be able to use it..
Quoted 7 lines
Using
daemon()is generally wise - it is basically a double fork and then one exits so that the orphan child becomes owned by init. However it also knows about platform specific considerations like setpgrp v setsid, whether an ioctl must be done to disown the controlling tty etc which can be fairly OS generation specific.
Well, when I tried using it in a program with some sleeps to test.. I noticed that the intermediate process that daemon creates is not cleaned up with a wait() call (so I see a defunct process in the ps listing).
If I manually do the double fork() then I can call waitpid() myself for the pid that I know it spawned. But if I just call wait() after calling daemon, then I don't know if I just cleaned up the pid it spawned (do I?), or some other previously spawned one (for perhaps totally different reasons)..
For my specifics it may not be a problem, but I guess I'm just whining about the fact that daemon() doesn't clean it up itself (or can it?)
Thanks much,
Davy
Quoted 3 lines · Davy Durham
Well, when I tried using it in a program with some sleeps to test.. I noticed that the intermediate process that daemon creates is not cleaned up with a
wait()call (so I see a defunct process in the ps listing).
You didn't use it right. See below.
Quoted 6 lines
If I manually do the double
fork()then I can callwaitpid()myself for
the pid that I know it spawned. But if I just callwait()after
calling daemon, then I don't know if I just cleaned up the pid it spawned (do I?), or some other previously spawned one (for perhaps totally different reasons)..
You still need to fork the first child, and the daemon does the second fork and exits the parent, and does all the necessary things for the system to make a proper daemon.
Quoted 3 lines
For my specifics it may not be a problem, but I guess I'm just whining about the fact that
daemon()doesn't clean it up itself (or can it?)
Attached is a simple C program that uses daemon the way you want to.
-- Steve
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main (int argc, char **argv)
{
pid_t pid;
if ((pid = fork()) < 0) {
perror("fork");
} else if (!pid) {
if (daemon(1,1) < 0) {
perror("daemon");
exit(-1);
}
printf("daemon is %d\n",getpid());
sleep(100);
exit(0);
}
printf("middle child is %d (and died)\n",pid);
waitpid(pid,NULL,0);
printf("parent is %d\n",getpid());
sleep(100);
return 0;
}Quoted 5 lines · Steven Rostedt
} else if (!pid) {
if (daemon(1,1) < 0) {
perror("daemon");
exit(-1);
}You probably want to use daemon(0,0), I was just playing with this, and forgot to put it back. See man daemon for details.
-- Steve
Ok, that's cool.. and it makes sense too..
Thanks
Quoted 18 lines · Steven Rostedt
Quoted 7 lines · Steven Rostedt
} else if (!pid) { if (daemon(1,1) < 0) { perror("daemon"); exit(-1); }You probably want to use
daemon(0,0), I was just playing with this, and forgot to put it back. See man daemon for details.-- Steve
Quoted 7 lines · Davy Durham
Hi, I'm not sure if there's a posix way of doing this, but wanted to check if there is a way in linux.
I want to have a daemon that fork/execs a new process, but don't want (for various reasons) the responsibility for cleaning up those process with the
wait()function family. I'm assuming that if the init process became the parent of one of these forked processes, then it would clean them up for me (is this assumption true?). Besides the daemon process exiting, is there a way to disown the process on purpose so that init inherits it?Thanks,
Davy
Sounds like a job for the Richard Stevens "Advanced Programming in the UNIX Environment" book. Check out chapter 13, "daemon processes". It explains the subtleties of process groups, signals, inheritance, etc.. better than most.
-Scott-
Quoted 5 lines · J. Scott Kasten
Sounds like a job for the Richard Stevens "Advanced Programming in the UNIX Environment" book. Check out chapter 13, "daemon processes". It explains the subtleties of process groups, signals, inheritance, etc.. better than most.
Definitely a good read. God rest his soul. But it doesn't let you know that there's already a daemon function out there. But what daemon does is pretty much a complete version of Stevens' example Program 13.1 "daemon_init".
Davy, I recommend that if you don't already have that book, get it.
-- Steve