Difference in task structure of a process and of a daemon
I am trying to differentiate a daemon from a normal process(user or kernel). I have tried to check if the task structure of a daemon has some special field. I also noticed that daemonizing a process means there is no controlling terminal associated with a daemon but not sure if it helps while programming. Please suggest if there is any way to differentiate a daemon from a normal process.
Thank you in advance!
Daemons are processes and as far as I know, there's nothing magical that separates a daemon from a normal process. The main thing, as you note, is that there's no controlling terminal. You might find [UTL=http://www.enderunix.org/docs/eng/daemon.php]this page[/URL] helpful, as it describes how to set up a daemon and how to use setsid to detach from a controlling terminal. One thing that this page doesn't mention, though, that I learned some time ago, is that a daemon should fork a second time after the setsid(), with the parent exiting and the child continuing to run. The reason for this is that the child will not be the process group leader (which means it cannot regain a controlling terminal since only a process group leader may do that -- not sure if there is any other use to that; it's been awhile since I looked at this seriously).
In short, I don't think there's any binary flag in task_stuct to tell whether a process is a daemon. It's more a combination of events, i.e. a background process that does not have a controlling terminal (i.e. runs totally in the background) with no stdin or stdout. Otherwise, AFAIK a daemon is just like any other process.
A possible (if superficial) definition:
A process is considered to be a daemon if it doesn't have a controlling terminal and its parent is init (ppid==1).
@btmiller,from the document you referred to, it is clear that a daemon is just like any other process with probably two distinct properties of its own which are no controlling terminal and parented to init process. Though these might be the necessary conditions for a daemon but not sufficient, using these to identify daemons.
Please do let know any points I have missed,if any.
A daemon can be detected using combination of following properties, i.e., -
* Daemons are reparented to init process : (task->parent->pid == 1)
* There is no controlling terminal associated with a daemon : (task->signal->tty == NULL)
* Setsid is called while daemonizing : (task->group_leader->signal->leader == 1)
if (((task->signal->tty)==NULL) && (task->parent->pid == 1)) && (task->group_leader->signal->leader == 1))
code can be used in a kernel module to list all daemons.
Ummm... not exactly.
Any process put into the background is reparented to init when the parent process tree terminates - the classic way to do that is using the "nohup" command. This isn't necessary for bash though.
When the parent process tree terminates, the process automatically becomes the task group leader for any processes it starts, and its signal leader is init (pid 1, due to reparenting). No need for a setsid system call.
All that is necessary is to redirect stdin/stdout/stderr, put it in the background and log out (the redirection is needed to disconnect from the controlling terminal - otherwise the process gets an I/O error on these file descriptors).
The definition is: http://en.wikipedia.org/wiki/Daemon_%28computing%29
Trivia side note: The name "daemon" comes from "Dump And Examine MONitor" from pre-UNIX history (TOPS 10, I believe). It was the name for a system service that provided for core dumps of user memory and for debugging the system monitor (now called a "kernel").
|All times are GMT -5. The time now is 12:40 PM.|