123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- #!/bin/sh --
- # vim: ft=sh
- set -ue
- : "${XDG_RUNTIME_DIR:=${TMPDIR:-/tmp}/runtime-${USER:-$(id -un)}}"
- : "${XDG_DATA_HOME:=${HOME:?}/.local/share}"
- : "${S6_SYSPATH:=${XDG_DATA_HOME}/s6/user}"
- : "${S6_RUNPATH:=${XDG_RUNTIME_DIR}/s6/user}"
- : "${S6_LIVE:=${S6_RUNPATH}/s6-rc}"
- : "${S6_COMPILED:=${S6_SYSPATH}/rc/compiled}"
- : "${S6_SVDIR:=${S6_RUNPATH}/service}"
- : "${S6_CONFIG_DIR:=${S6_SYSPATH}/config}"
- : "${S6_LOG_DIR:=${S6_RUNPATH}/log}"
- : "${S6_OWNER_PID_FILE:=${S6_SVDIR}/.s6-svscan/owner.pid}"
- : "${S6_WAIT_FOR_STOP_MS:=1000}" # how many miliseconds wait for services to stop
- unexport_env() {
- # unexport unneeded variables that may be exported to configure this script
- # for example if run as:
- # S6_RUNPATH=some/other/path s6-user-init start ...
- unset \
- S6_RUNPATH \
- S6_LIVE \
- S6_COMPILED \
- S6_SVDIR \
- S6_OWNER_PID_FILE \
- S6_WAIT_FOR_STOP_MS
- }
- start() {
- pid="${1:?}"
- mkdir -p "${S6_SVDIR}"
- if s6-svscanctl -a "${S6_SVDIR}" 2>/dev/null; then
- echo "s6-svscan already running on '${S6_SVDIR}'" 1>&2
- exit 1
- fi
- echo "${pid}" | install -Dm600 /proc/self/fd/0 "${S6_OWNER_PID_FILE}"
- export S6_CONFIG_DIR
- export S6_LOG_DIR
- export S6_SYSPATH
- live="${S6_LIVE}"
- compiled="${S6_COMPILED}"
- svdir="${S6_SVDIR}"
- unexport_env
- exec s6-rc-svscan \
- -l "${live}" \
- -c "${compiled}" \
- -s "${svdir}"
- }
- stop() {
- pid="${1:?}"
- [ -f "${S6_OWNER_PID_FILE}" ] || {
- echo "pid file does not exist: '${S6_OWNER_PID_FILE}'" 1>&2
- exit 1
- }
- IFS= read -r owner <"${S6_OWNER_PID_FILE}"
- [ "${owner}" = "${pid}" ]
- s6-rc -l "${S6_LIVE}" -bda change || : # stop all services
- (
- cd "${S6_SVDIR}" &&
- exec s6-svwait -D -t "${S6_WAIT_FOR_STOP_MS}" ./* 2>/dev/null
- ) || : # wati for services to stop
- s6-svscanctl -n "${S6_SVDIR}" || : # stop svscan
- cd "${S6_LIVE%/*}"
- dir="$(readlink "${S6_LIVE}")"
- rm -rf -- "${dir:?}"
- unlink "${S6_LIVE}"
- rm -rf -- "${S6_SVDIR:?}"
- }
- usage() {
- printf '%s\n' \
- "Usage: ${0##*/} [action]" \
- ' start <pid> - start user services' \
- ' stop <pid> - stop user services' \
- ' help - show this help'
- exit "${1:-0}"
- }
- case "${1:?}" in
- start) start "${2:?provide owner pid}" ;;
- stop) stop "${2:?provide owner pid}" ;;
- help | -h) usage 0 ;;
- *) usage 1 ;;
- esac
|