Link to home
Start Free TrialLog in
Avatar of arcarson
arcarson

asked on

Writing a Unix Daemon

Can anyone give me a tutorial on how to write a linux daemon?
Avatar of chris_calabrese
chris_calabrese

The traditional meaning of Daemon is a program that starts when the system starts and does something useful.

However, I'm guessing you specifically mean a network daemon, one that listens on a well-known network port and responds to connections (like an HTTP server).

Assuming you have root access to the machine, the easy way to do this is by writing a program that reads stdin and writes stdout.  You then stick it in /etc/inetd.conf (/etc/inet/inetd.conf on some systems).  See the inetd.conf man page for details, though you can probably figure it out from the other entries.

If you can't use inetd, then I'm guessing this is a homework assignment...
Avatar of jkr
>>writing a program that reads stdin and writes stdout

Sorry, but daemons usualy close sdtin/out/err immediately after startup to "detach" - that makes the basic difference when writing such a thing...
ASKER CERTIFIED SOLUTION
Avatar of mnashadka
mnashadka

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I ws under the impression that a daemon process should not be a session group (and thus a process group) leader because if pre-chance it opened a tty without ONOCTTY, that tty would then become the controlling tty of the group.  So setsid() before the second fork.  Apart from that, the usual thing is to close all file descriptors, and chdir to /

ie.
fork() - let parent die
close all fd's
chidr("/");
setsid() (or setpgrp)
fork()
Do your stuff...

Unless strictly neccessary, try to avoid holding files open for any length of time.
I'm going to jump back into this one... Before we can answer the question, we have to know whether arcarson wants to actually write a 'daemon' or simply wants a network listener. All technical commentary is pointless before we know this.
Avatar of arcarson

ASKER

I want to write a daemon. Something that will start up when the server boots.
make a deamon program using the above code fragment . To startup your program when the server starts put the command for your progam in a script in /etc/rc directory .