Skip to contentSkip to conversation

daemonization

DISCUSSION xlp 3 participants 3 messages Jun 28, 2002

lore
Share & tools
lore

xlp

author
#0 lore raw

Hi, I want to understand How can a executale object in Unix can become a "daemon" ? How can it deal with stdout, and ttys it's connected to? What are the C library function I should read man?.

bye

#1 lore raw

On Fri, 2002-06-28 15:49:13 -0500, xlp <[email protected]> wrote in message <20020628154913.A377@nietzsche>:

Quoted 1 line

Hi, I want to understand How can a executale object in Unix can become a "daemon" ? How can it deal with stdout, and ttys it's connected to? What are the C library function I should read man?.

- chdir() to "/"

Most important are these:

- become session leader: setsid()
- fork() and let parent exit(0)
- close() stdin, stdout and stderr and re-open() them with /dev/null
- maybe do a chroot()
- maybe open any IP ports to listen on them
- Drop root privileges: setuid(), setgid()
- Ready (TM)
- Afterwards: At any file you open, don't forget O_NOCTTY!

MfG, JBG
PS: And ___always___ check the return values of those library calls!!!

Signature · 3 lines
Jan-Benedict Glaw   .   [email protected]   .   +49-172-7608481
	 -- New APT-Proxy written in shell script --
	   http://lug-owl.de/~jbglaw/software/ap2/
#2 lore raw
Quoted 3 lines · xlp

Hi, I want to understand How can a executale object in Unix can become a "daemon" ? How can it deal with stdout, and ttys it's connected to? What are the C library function I should read man?.

Daemonisation isn't covered in any particular manual page. However, it gets its own chapter in Stevens[1].

[1] Advanced Programming in the UNIX(R) Environment
W Richard Stevens
Addison-Wesley

This book is highly recommended, but not cheap (I paid UKP42, ~= EUR60).

However, it doesn't cover sockets; there is another book on network programming by the same author but, IIRC, it covers both sockets and TLI, and you're unlikely to need to use TLI.

Quoted 6 lines · Jan-Benedict Glaw

- chdir() to "/"

Most important are these:

- become session leader: setsid()
- fork() and let parent exit(0)

The fork() comes first; everything else should be done in the child.

Quoted 1 line

- close() stdin, stdout and stderr and re-open() them with /dev/null

And try to close() all other descriptors.

Signature · 1 line
Glynn Clements <[email protected]>