Eng.Fouad
Eng.Fouad

Reputation: 117597

Can you explain this C code? (Create a deamon program)

I want to understand the following code well:

/* Become deamon + unstoppable and no zombies children (= no wait()) */

if(fork() != 0)  return 0;        /* Parent returns OK to shell */
signal(SIGCLD, SIG_IGN);          /* ignore child death */
signal(SIGHUP, SIG_IGN);          /* ignore terminal hangups */
for(i = 0; i < 32; i++) close(i); /* close open files */
setpgrp();                        /* break away from process group */

Here is how I understand the above code:

1st line: Creating a child process and terminating the parent process, so the parent parent process will go back to the shell and the child process will continue executing the program in background.

2nd line: Ignore the signal that is supposed to be sent to the parent process (who's controlling the terminal) when the child process (who's executing the program) is terminated. I think this line will avoid the occurrence of zombies children?

3rd line: I read that it ignores POSIX's Hangup and I am not sure what it is exactly.

4th line: closing the open files whose descriptor files are 0 to 31 (I am not sure why from 0 to 31)

5th line: No idea what it does.


Can you please help me to understand this code well? Thanks in advance :)

Upvotes: 2

Views: 456

Answers (3)

Lloyd Macrohon
Lloyd Macrohon

Reputation: 1462

To create a daemon, you need to:

  1. Fork and have the parent exit.
  2. Make sure you aren't the process group leader. Shouldn't need to call setpgrp here as we've just forked and we are a member of process group not leader.
  3. Create a new session (setsid), this guarantees as so that we have no controlling terminal.
  4. We inherited all the descriptors from our parent, close the ones you don't need.
  5. Change working directory. So it's not on some mounted volumes that you might want to unmount. In fact, chroot if you can here.
  6. Set filemode creation mask to zero.

The signal lines in your code just sets them to ignore those signals. setpgrp isn't needed, it should be setsid. Then, you're just missing some of the other things you need to do.

Upvotes: 1

dstromberg
dstromberg

Reputation: 7177

1) fork()'ing and returning in the parent, has two meanings: A) Run in the background. B) Avoid zombies in a portable way

2) http://en.wikipedia.org/wiki/SIGCHLD

3) SIGHUP is often delivered to a process when a tty is closing. It more or less means "Continue running, even if the associated tty goes away".

4) Closing file descriptors allows starting a daemon from something like an ssh session, without the ssh session waiting around on close for the filedescriptors 0-31 to be closed. If you don't do this, daemons may sometimes cause ssh sessions to seem to hang on exit. There's nothing magic about 0-31 - some processes close more file descriptors than that, but of course 0, 1 and 2 have special meanings: stdin, stdout, stderr respectively.

5) http://en.wikipedia.org/wiki/Process_group

Upvotes: 2

You are asking what setpgrp does.

Here is the relevant man page

You could type man setpgrp to get it.

Read also the linux daemon howto

You could also use the daemon function

Upvotes: 6

Related Questions