the djb way

services, services!


mathopd


Protocol: TCP
Standard Port: 80

mathopd ("math-op-dee") is a fast, full-featured, incredibly compact HTTP server by Michiel Boland. It has been out for several years now and continues to be refined by the author. We recently upgraded to version 1.5, released in December 2003. Check out http://www.mathopd.org/ for the latest releases and documentation.

mathopd is based on a single-process, single-threaded, multiplex IO model using the poll(2) function. This type of server design is generally considered superior to pre-forked servers, --like Apache-- or spawn/exec models, --like publicfile under tcpserver-- for busy web hosts. Speed aside, we also like mathopd for these reasons:

mathopd also gets a security nod on Bernstein's publicfile page --a non-trivial distinction.

Best of all, mathopd works great as a daemontools service, with full tai-stamped multilogging, reliable signaling, and easy administration. If you need the stuff that publicfile just don't got --cgi, fine-grained control over virtual hosts, conventional user directories, authenticated realms, etc.-- mathopd is worth a cookie.

A daemontools run script for /var/svc.d/mathopd/run:


#!/bin/sh
# mathopd/run
# daemontools run script for mathopd http server
# ===
exec 2>&1
echo "*** Starting mathopd..."
exec /usr/local/sbin/mathopd \
  -f /usr/local/etc/mathopd.conf \
  -n

### that's all, folks!

The -f option is used to tell mathopd where to find its configuration file. The -n option keeps mathopd in the foreground, the necessary magic for daemontools.

The usual run script for the multilogger in /var/svc.d/mathopd/log/run:


#!/bin/sh
# mathopd/log/run
exec setuidgid multilog multilog t /var/multilog/mathopd

### that's all, folks!

The log directory in /var/multilog:

# mkdir -p /var/multilog/mathopd
# chown multilog:nofiles /var/multilog/mathopd

Everything else is done in mathopd's configuration file. Here are some commented snippets relevant to a daemontools setup:


# /usr/local/etc/mathopd.conf
# mathopd config, running under daemontools
# ===

## run under non-priveleged uids (see cgi.txt):
User mathopd
StayRoot On
Control {
  ScriptUser cgiexec
}

## secure umask:
Umask 026

## logging picked up by multilog from stderr:
Log /dev/stderr
ErrorLog /dev/stderr

## use localtime (not GMT) to match tai-stamp in error output:
LogGMT Off

## remove Ctime from logs; use other fields as desired:
LogFormat {
    #Ctime
    Status
    ServerName
    RemoteAddress
    Method
    URI
    QueryString
    UserAgent
}

## no PIDFile necessary for daemontools:
#PIDFile

<snip>

We like being able to pick 'n choose the fields to output in our logs; several more are available than shown here. The logs will display the selected fields, tab-separated in the same order as listed in the LogFormat block, right after the multilog tai-stamp.

Link into /service:

# ln -s /var/svc.d/mathopd /service/mathopd

mathopd should be up and serving! Check out your logs as usual, and work on fine-tuning the configuration file for your particular installation.

mathopd responds to a number of signals, accessible with the svc utility:

svc -d   stop mathopd
svc -u   start mathopd
svc -i
svc -t
SIGINT
SIGTERM
stop mathopd (will restart under daemontools)
svc -h SIGHUP reopen log files (NA /dev/stderr)
svc -1 SIGUSR1 disconnect all current connections;
new connections accepted
svc -2 SIGUSR2 close server sockets, complete current connections;
used for "graceful" shutdowns
(will restart under daemontools)
svc -2 &&\
svc -d
SIGUSR2 close server sockets, complete current connections;
used for "graceful" shutdowns
(will stay down)
svc -q SIGQUIT toggle debug logging

The QUIT, USR1 and USR2 signals are available if you have applied the daemontools-0.76.sigq12.patch patch for daemontools, as suggested in our installation instructions.

The debug signaling capability is very cool. While watching your multilog on one terminal, switch to another and use:

# svc -q /service/mathopd

to turn debug output on and off. The "innards" of mathopd are now at your disposal.

Some other time we need to tell the rest of the story: mathopd as an excellent platform for web scripting! Until then, get to know mathopd with daemontools. It's a fine web-server for any host built the djb way.


Copyright © 2004, Wayne Marshall.
All rights reserved.

Last edit 2004.04.06, wcm.