tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: question on daemon framework

On Wed, 18 Feb 2009 01:38:11 -0800 (PST)
Bits Pilani <> wrote:

> Is there any documentation about how to write daemons in NetBSD? 
> Also is there a simple skeleton daemon that can be used as a reference to 
> implement my own daemon.
> Is the daemon framework identical to that of Linux? 

This doesn't seem to be a kernel-related question (except as far as it
concerns syscalls), but fork(2) is older than both BSD and Linux, and
is now also POSIX, so it'd indeed be the same to create a detached

As usual you might (or might not) want to then call setsid(2),
open(2) on "/dev/null", dup2(2)/close(2) or freopen(3) to redirect
stdin/stdout/stderr, and as necessary drop privileges with
setgid(2)/setegid(2)/setgroups(2)/setuid(2)/seteuid(2), etc

There also is a daemon(3) utility function in libc.  All the mentionned
functions have manual pages.

If the question really was kernel related, there are kernel threads,
which I believe are launched using kthread_create(9) and friends.

As for the networking part, select(2)/poll(2) would be available as
well as the BSD sockets API as in the case of Linux, yet
kqueue(2)/kevent(2) are also available as an enhanced polling mechanism
(various daemons in the base system use it and can be used as example
such as the inetd(8) source).  The portable event(3) library is also
part of the base system, which can abstract over
select/poll/kqueue/epoll (usually available as a package named libevent
on other systems).

I hope this helps,

Home | Main Index | Thread Index | Old Index