1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/sh
# Build-time packaging only: never enable or start services.
set -eu
[ "$#" -ge 3 ] || { echo 'usage: initify-package systemd|openrc PKGDIR UNIT...' >&2; exit 1; }
init=$1
pkgdir=$2
shift 2
case "$init" in systemd|openrc) ;; *) echo "Unsupported init system: $init" >&2; exit 1;; esac
[ -n "$pkgdir" ] && [ "$pkgdir" != / ] || { echo 'A staging PKGDIR is required' >&2; exit 1; }
mkdir -p "$pkgdir"
pkgdir=$(cd "$pkgdir" && pwd -P)
[ "$pkgdir" != / ] || exit 1
work=$(mktemp -d)
trap 'rm -rf "$work"' 0
trap 'exit 1' HUP INT TERM
for unit do
name=$(basename "$unit")
case "$name" in ''|*[!a-zA-Z0-9_.-]*|-*|.*) echo "Unsafe unit name: $name" >&2; exit 1;; esac
[ ! -e "$work/$name" ] || { echo "Duplicate unit: $name" >&2; exit 1; }
[ -f "$unit" ] || { echo "Missing unit: $unit" >&2; exit 1; }
cp "$unit" "$work/$name"
if [ "$init" = systemd ]; then
case "$name" in *.service|*.timer|*.path|*.socket|*.target) ;; *) echo "Unsupported unit file: $name" >&2; exit 1;; esac
continue
fi
case "$name" in *.service) ;; *) echo "OpenRC conversion does not support $name" >&2; exit 1;; esac
# initify silently ignores unknown directives. Allow only the subset whose
# execution semantics this wrapper handles; reject expansions/shell syntax.
awk '
function reject() { print FILENAME ":" NR ": unsupported: " $0 > "/dev/stderr"; bad=1 }
/^[[:space:]]*([#;]|$)/ { next }
/^\[(Unit|Service|Install)\]$/ { section=$0; next }
{
n=index($0,"="); if (!n) { reject(); next }
key=substr($0,1,n-1); value=substr($0,n+1)
if (seen[section,key]++) { reject(); next }
if (section=="[Unit]" && key=="Description" && value ~ /^[a-zA-Z0-9 .,:_()+\/-]+$/) next
if (section=="[Service]" && key=="Type" && value=="simple") next
if (section=="[Service]" && key=="ExecStart" && value ~ /^\/[a-zA-Z0-9_.\/-]+( [a-zA-Z0-9_.\/:,@=+-]+)*$/) { start++; next }
if (section=="[Service]" && key=="PIDFile" && value ~ /^\/run\/[a-zA-Z0-9_.\/-]+$/ && value !~ /\.\./) next
# Enablement is administrator policy; installation never enables a unit.
if (section=="[Install]" && key=="WantedBy" && value=="multi-user.target") next
reject()
}
END { if (start!=1) { print FILENAME ": exactly one ExecStart is required" > "/dev/stderr"; bad=1 }; exit bad }
' "$work/$name"
command -v initify >/dev/null 2>&1 || { echo 'initify is required as a native build dependency' >&2; exit 1; }
initify --name "${name%.service}" "$work/$name"
generated=$work/${name%.service}
[ -s "$generated" ] || { echo "initify did not generate $name" >&2; exit 1; }
grep -Fx '#!/sbin/openrc-run' "$generated" >/dev/null
# systemd Type=simple is a foreground process. OpenRC must background it
# and track a PID; initify alone does not emit this setting.
printf '\ncommand_background=true\n' >> "$generated"
if ! grep -Eq '^pidfile=.' "$generated"; then
printf 'pidfile=/run/%s.pid\n' "${name%.service}" >> "$generated"
fi
sh -n "$generated"
done
# Do not leave a partially installed set when validation/conversion fails.
for unit do
name=$(basename "$unit")
if [ "$init" = systemd ]; then
mkdir -p "$pkgdir/usr/lib/systemd/system"
install -m644 "$work/$name" "$pkgdir/usr/lib/systemd/system/$name"
else
mkdir -p "$pkgdir/etc/init.d"
install -m755 "$work/${name%.service}" "$pkgdir/etc/init.d/${name%.service}"
fi
done