Dear Mark,

here a solution which should work quite well. However, I did only some quick 
testing, so a more through testing is still missing.
I tried to stay close to your SysV init script, please counter check that. 
Especially also the parsing in script check-initd if you should need that.

$ cat /usr/local/lib/systemd/system/apt-cacher.service
[Unit]
Description=APT package caching proxy
Documentation=man:apt-cacher(8)
After=network.target
ConditionPathExists=/usr/sbin/apt-cacher

[Service]
Type=forking
RuntimeDirectory=apt-cacher
PIDFile=/run/apt-cacher/apt-cacher.pid

# Better eliminate the config options user and group to prevent inconsistencies.
# With systemd, the idea is to extend or override the service if these values
# should be changed. Without the setting the options, the daemon already keeps
# the invoking user as it is startet with.
User=www-data
Group=www-data

Environment=EXTRAOPT=
EnvironmentFile=-/etc/default/apt-cacher

# Nowadays I would recommend to eliminate this security check if the port is
# used by inetd. Then we need no helper script any more and can provice all
# other functionality by this service definition.
#ExecStartPre=/usr/local/lib/apt-cacher/check-inetd

ExecStart=/usr/sbin/apt-cacher -R 3 -d -p /run/apt-cacher/apt-cacher.pid 
$EXTRAOPT
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/usr/bin/kill $MAINPID
ExecStopPost=-/usr/share/apt-cacher/libcurl.pl EXIT

KillMode=process

[Install]
WantedBy=multi-user.target

$ cat /usr/local/lib/apt-cacher/check-inetd
#!/usr/bin/env sh

set -eu

NAME=apt-cacher
CONFIG_FILES="/etc/$NAME/$NAME.conf"

# Include configuration fragments.
if [ -d /etc/$NAME/conf.d ]; then
    for file in /etc/$NAME/conf.d/*; do
        [ -f "$file" ] && CONFIG_FILES="$CONFIG_FILES $file"
    done
fi

# Read the last configured value, matching the init script.
get_config() {
    local key="$1"
    local value

    value=$(sed -n "s/^[[:space:]]*$key[[:space:]]*=[[:space:]]*//p" \
        $CONFIG_FILES 2>/dev/null | tail -n 1)

    printf '%s' "$value" | tr -d '[:blank:]'
}

# Check whether inetd already handles the service.
port=$(get_config daemon_port)
port=${port:-3142}

if [ -f /etc/inetd.conf ] &&
   grep -E -q 
"^${port}[[:space:]]+stream[[:space:]]+tcp[[:space:]]+nowait[[:space:]]+www-data[[:space:]]+/usr/sbin/apt-cacher[[:space:]]+apt-cacher[[:space:]]+-i"
 \
       /etc/inetd.conf; then
    echo "apt-cacher is already handled by inetd on port $port"
    exit 1
fi

exit 0

Feel free to change it. Please consider the two longer comments in the service 
file.

Chris

Reply via email to