daemons

This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.



Subject: Re: how to make a daemon
From: fred smith <fredex@fcshome.stoneham.ma.us>
Date: Sat, 1 May 1999 14:28:57 -0400


On Sat, May 01, 1999 at 07:26:27AM -0500, Steve Cohen wrote:
> I have written a little program that listens to /dev/ttyS0 and every
> time it sees
> a byte, it sends that byte out /dev/ttyS1 and also through a
> TCP/IP socket.
> This program runs on a linux box that is basically used for nothing
> else.
> 
> I would like to have this functionality constantly running on the box.
> To do
> so, do I need to make my program a daemon, and if so how?  Basically
> this program needs to run with root privileges.  I have to log in as su
> to get it to work.  I want it instead to happen automatically, so that
> if the machine goes down, it can be rebooted and do what it's supposed
> to with no further interaction.
> 
> Any suggestions?

Yes.

The simplest way to do it is to have it started from your /etc/rc.d/rc.local
script every time the system boots. If it is named "myprog", you would add
a line something like:

	/usr/local/bin/myprog&

and it's ls entry would look something like:

-rwsr-xr-x   1 root     root       807668 Jan  1 21:33 /usr/local/bin/myprog

notice I've assumed it needs to be "setuid root", i.e., it is owner/group
root and the leftmost 'x' in the permissions is instead an 's'. it may be
that simply starting it from the startup scripts would suffice for this
without needing to be setuid, but I'd have to study a bit to be sure.

Alternatively, a "real" daemon isn't hard to program but is a little more
complex. Basically you need to fork(), have the parent exit, have the
child close stdin, stdout, stderr, (and then open three dummy files,
such as open /dev/null 3 times) relinquish its control terminal then
fork again and the parent also exits after this second fork.

A good (though not inexpensive) book which will tell you more than you
ever wanted to know about this subject (among others), but is worth the
bucks if you're serious about Unix programming, is W. Richard Stevens'
"Advanced Programming in the Unix Environment". Mr. Stevens is an
excellent write, his stuff is clear and understandable, and he gives
excellent examples and explanations. he's no relative of mine, I simply
think his books are great.
============

Subject: Re: how to make a daemon
From: ignatz@homebru.dminet.com (Dave Ihnat)
Date: Sat, 1 May 1999 19:23:38 -0500 (CDT)


Reverend wrote:

> a server is a daemon... if you set the program to operate as a server,
> then it's a daemon. If it's not acting as a server (handling incoming
> connections and whatnot) then you just have a service.. which is what your
> program sounds like. Regardless.. check out that config file.

Not precisely; you have to detach it from the controlling process; this is
accomplished by closing stdin, stdout, and stderr (opening appropriate
replacements if necessary), and ignoring group signals.

=======

Subject: Re: how to make a daemon
From: "Fred Lenk RHL Linux account" <fllnx@commpower.com>
Date: Mon, 3 May 1999 11:09:15 -0700


Start the program in the /etc/rc.d/init.d directory.
see other files in that directory to see how you
can do this.

===


Subject: RE: question about daemons etc.
From: "Jay Freeman" <saurik@saurik.com>
Date: Tue, 4 May 1999 22:19:59 -0500


I would probably put it in /etc/inittab with an entry like:

<G>:<X>:respawn:/usr/local/sbin/myprogram -my --args

:where <G> is any two alphanumeric characters that uniquely represent this
entry in the file (such as "mp", which might stand for "my program"), and
where <X> is a list of the runlevels you want it to be up at (either just
one, "3", or multiple, "345", etc.).  Practically, you probably want this
running all the time, so you can look at the line:

id:<Y>:initdefault:

:which tells initd what runlevel to start at, and use that for <X>.  The
respawn automatically restarts the daemon if it dies.  This means however
that if your program automatically forks then it will just get run a million
times in a row, so to do this you will need to either add an option to not
fork or simply remove that part.

I might have misunderstood what you meant by restart, if your program just
needs to start once and then you trust it to stay running, it might be
better to use a line like:

<G>:<X>:boot:/usr/local/sbin/myprogram -my --args

:which MIGHT (not sure, I used to know this, but forgot) have it run a
little sooner, either way it should be running before people log in though.

===

Subject: Re: question about daemons etc.
From: Vlad Petersen <vladimip@uniserve.com>
Date: Tue, 04 May 1999 20:29:34 -0700


Steve Cohen wrote:
.....
> I would like to be able to automate the whole thing.  Have the program
> run at root privilege level as soon as the machine is started - even
> before anyone has logged on to it if possible.
> 
> What is the best way to do this?  Do I need to make my program a daemon?
> If so, how?  What "run level" does it need to be at.  Or is there an
> easier way to get all the above to happen?

This is how to make a daemon:

http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16

, although there are Unix programming books that elaborate on the issue
in more details.

The most straightforward way to make the program start automatically is
put it into /etc/rc.d/rc.local.

===

Subject: Re: question about daemons etc.
From: Andrae Muys <a.muys@mailbox.uq.edu.au>
Date: Wed, 5 May 1999 13:38:51 +1000 (GMT+1000)


On Tue, 4 May 1999, Steve Cohen wrote:

> I have written a little server program that does all this.  However, to
> run it,
> I need to log on as root and restart it manually.
> 
> I would like to be able to automate the whole thing.  Have the program
> run at root privilege level as soon as the machine is started - even
> before anyone has logged on to it if possible.
> 
> What is the best way to do this?  Do I need to make my program a daemon?
> If so, how?  What "run level" does it need to be at.  Or is there an
> easier way to get all the above to happen?

I while back I wrote a short introduction to the boot process on a redhat
box.  I don't know if it's useful to you, but it might give you a start.
Personally I normally create my own init.d/ script and add the appropriate
links to rc?.d/.

BTW: A good skeleton script is /etc/rc.d/init.d/crond
The intro: http://www.uq.edu.au/~cmamuys/documentation/sysVinit.help

Andrae Muys

P.S. If I might suggest you do a careful audit of your source code looking
specifically for security bugs.  If it's a network daemon, it's a
potential remote exploit.  You might also consider using ssh instead of
telnet.

====


the rest of The Pile (a partial mailing list archive)

doom@kzsu.stanford.edu