🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.dev.c-programming
Article #2

Re: Creating Daemons: Best Practice?

#2
From: Dances With Crows
Date: Tue, 21 Sep 2004 16:05
52 lines
3319 bytes
["Followup-To:" header set to comp.os.linux.misc.]
On Tue, 21 Sep 2004 17:00:34 +0200, Herr Fieldmarshall staggered into
the Black Sun and said:
> I've created a program that runs as a daemon, but my question now is,
> what is the best practice concerning distributing and installing it?

Put the source in a tarball, make sure there's a README in the tarball.
If you haven't used the autotools to make yourself a configure script,
you probably need to if you want this to have any sort of wide
distribution.

> The executable needs access to some config file, and some other data
> files.  I assume it's best practice to place the executable in
> /usr/local/bin and all the rest in /usr/local/etc.

That'll work for now.  If you use the autotools, you can pass
"--prefix=/usr" and the executable will go in /usr/bin .  There isn't a
/usr/etc though, so think about another location for these mysterious
"other files".

> how can I assure that the executable will always find the config file?
> (I don't like hard-coding ../etc/configfile.conf).

Many daemons take a -C option like so:  daemon -C /foo/bar/config.conf .
Have your daemon do a getopt() on C, look for that file first, if it's
not there, look in 1 or 2 hardcoded paths, if nothing is found, write
informational messages to stderr and the logfile and exit(1).  Like so:

mydaemon: could not find config file in /etc/mydaemon.conf or
/etc/mydaemon/mydaemon.conf , no -C option given.  Exiting because
there's no point in running without a config file.

> I've also read that it is for security reasons best to use chroot to
> run the deamon, but how do I do this in such a way that my data and
> config files remain accessible?

That's up to the person who sets up the chroot.  Remember that any
shared libraries your program links to must also be in the chroot
environment.  If I were you, I'd get the thing working properly before
worrying about chroot.

> My last point is how do I get the daemon neatly installed in init.d ?
> Is there some script for it?

You'll have to write your own init script.  Take a look at what's
already there for some guidance.  HTH,

-- 
Matt G|There is no Darkness in Eternity/But only Light too dim for us to see
Brainbench MVP for Linux Admin /    mail: TRAP + SPAN don't belong
http://www.brainbench.com     /                Hire me! 
-----------------------------/ http://crow202.dyndns.org/~mhgraham/resume
Technical Details
Message-ID: <slrncl0k97.nmm.danSPANceswitTRAPhcrows@samantha.crow202.dyndns.org>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!fu-berlin.de!uni-berlin.de!not-for-mail
References: <cipfmi$9nm$1@snic.vub.ac.be>

Show Full Headers
Complete Article Headers
X-Google-Thread: 1103af,b8ad6e866a257b4
X-Google-Thread: faa64,b8ad6e866a257b4
X-Google-Thread: 100a19,b8ad6e866a257b4
X-Google-Attributes: gid1103af,gidfaa64,gid100a19,public
X-Google-Language: ENGLISH,ASCII-7-bit
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!fu-berlin.de!uni-berlin.de!not-for-mail
From: Dances With Crows <danSPANceswitTRAPhcrows@gmail.com>
Newsgroups: linux.dev.c-programming,comp.os.linux.misc,alt.os.linux,be.comp.os.linux
Subject: Re: Creating Daemons: Best Practice?
Followup-To: comp.os.linux.misc
Date: 21 Sep 2004 16:05:32 GMT
Lines: 52
Message-ID: <slrncl0k97.nmm.danSPANceswitTRAPhcrows@samantha.crow202.dyndns.org>
References: <cipfmi$9nm$1@snic.vub.ac.be>
Reply-To: daSPANnceswithcroTRAPws@gmail.com
X-Trace: news.uni-berlin.de wwdLOrXM6BE0LzzXjs4S0wMSSxsVyUSYsRt1av7e6a4HTk/aE=
User-Agent: slrn/0.9.8.0 (Linux)