About newd
newd is a base daemon to start new OpenBSD daemons from. It carries the privilege separated process layout, the imsg pipes between the processes, the newctl control socket, a yacc configuration parser with macro and include support, logging, and the rc.d and man page scaffolding, and nothing else.
Process Layout
Privilege separated process layout with parent, control and engine processes
Communication
imsg pipes between processes for efficient communication
Control Interface
newctl control socket for daemon management
Configuration
Yacc configuration parser with macro and include support
Processes
- newd (parent): runs as root, parses
newd.conf, forks the children, pushes configuration to them and answersnewctl show parent. - control: chrooted and unprivileged, owns
/var/run/newd.sockand routesnewctlrequests to the parent and replies back tonewctl. - engine: chrooted and unprivileged, where the work of the new daemon belongs. Receives configuration from the parent and answers
newctl show engine.
Startup is: the parent sends IMSG_CFG_DONE to both children, the engine acknowledges with IMSG_CFG_DONE, and the parent then sends IMSG_CTL_START to both, at which point the engine calls engine_launch().
Building a New Daemon
To build a new daemon based on newd:
- Copy and rename
newd/newctl/NEWD_*/_newdthroughout. - Add configuration keywords to
parse.y(the%tokenline, the sorted table inlookup()and aconf_mainrule) and fields tostruct newd. - Add
IMSG_*types tonewd.h, handle them in the*_dispatch_*functions, and send any state the engine needs fromnewd_configure(). - Add
newctlcommands toparser.cand handlers tonewctl.c. - Widen the
pledgeandunveilcalls as needed.
Installation
Compile and install:
make
doas make install
doas groupadd _newd
doas useradd -d /var/empty -L daemon -g _newd -s /sbin/nologin _newd
doas cp newd/examples/etc/rc.d/newd /etc/rc.d/
doas cp newd/examples/etc/newd.conf /etc/
Once newd.conf is in place, run rcctl enable newd and rcctl start newd. The daemon can also be run manually from the command line. To keep newd from daemonizing, run newd -d.
To check it is alive:
newctl show parent
newctl show control
newctl show engine