File: runit-helper

package info (click to toggle)
dh-runit 2.17.2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 364 kB
  • sloc: sh: 376; perl: 355; haskell: 49; makefile: 13
file content (240 lines) | stat: -rwxr-xr-x 7,449 bytes parent folder | download
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/bin/sh
# Copyright (C) 2017 Dmitry Bogatov <KAction@gnu.org>

# Author: Dmitry Bogatov <KAction@gnu.org>

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

set -e

# rescan and invoke-rc.d actions are done only if runit is init
is_runit () {
	[ -f  /run/runit.stopit ]
}

is_installed () {
	[ -f  /sbin/runit ]
}

# No-Op if the service is not enabled
# sv can't send signals to a disabled service
is_enabled () {
	[ -h /etc/service/"$NAME" ]
}

sv_warn () {
	echo "warning: sv: failed to signal $NAME"
	true
}

# workaround for races as in #919296
ok_pipe () {
	[ -p "/etc/sv/$NAME/supervise/ok" ]
}

postinst () {
	local action="${1}" previous="${2:-}"

	if [ -x /usr/sbin/policy-rc.d ]; then
		set +e ; /usr/sbin/policy-rc.d "${NAME}" start ; rc=$? ; set -e
		if [ "$rc" = "101" ]; then
			echo "runit: ${NAME}: start action denied by policy-rc.d" && return 0
		fi
	fi
	# create supervise links at runtime, only if runit is installed
	mkdir -p /run/runit/supervise
	if [ -e /usr/bin/cpsv ]; then
		cpsv p "$NAME"
	fi

	# It is important to not override local admin
	# decision (see #899242 and 942323 ).
	if [ "${ENABLE}" = yes ]; then
		if [ ! -h "/etc/runit/runsvdir/default/.$NAME" ] && [ ! -e "/etc/runit/runsvdir/default/.$NAME" ] ; then
		    if [ ! -h "/etc/runit/runsvdir/default/$NAME" ]; then
			# $NAME link tested to avoid infinte loop of symlinks
			ln -sf "/etc/sv/$NAME" "/etc/runit/runsvdir/default/$NAME"
			if is_runit && pidof runsvdir > /dev/null ; then
				# always force a rescan after enable
				kill -s ALRM 1
			fi
		    fi
		fi
	# ENABLE=no is always a no-op
	fi

	#invoke-rc.d
	if is_runit && is_enabled ; then
		if [ "${action}" = 'configure' ] || [ "${action}" = 'abort-upgrade' ] || \
			[ "${action}" = 'abort-deconfigure' ] || [ "${action}" = 'abort-remove' ] ; then
			if [ "${ONUPGRADE}" = restart ] && [ -n "${previous}" ] && ok_pipe ; then
				sv restart ${NAME} || sv_warn
			elif [ "${ONUPGRADE}" = reload ] && [ -n "${previous}" ] && ok_pipe ; then
				sv reload ${NAME} || sv_warn
			elif  ok_pipe ; then
				# ONUPGRADE=stop || ONUPGRADE=nostop
				# ONUPGRADE= restart || reload and [ ! -n "${previous}" ]
				sv start ${NAME} || sv_warn
			else
				return 0
			fi
		fi
	fi
}

prerm () {
	local action="${1}"
	# invoke-rc.d
	if is_runit && is_enabled ; then
		if [ -x /usr/sbin/policy-rc.d ]; then
			set +e ; /usr/sbin/policy-rc.d "${NAME}" stop ; rc=$? ; set -e
			if [ "$rc" = "101" ]; then
				echo "runit: ${NAME}: stop action denied by policy-rc.d" && return 0
			fi
		fi
		if [ "${ONUPGRADE}" = stop ] && ok_pipe ; then
			sv stop ${NAME} || sv_warn
		elif [ "${action}" = 'remove' ] && ok_pipe ; then
			# ONUPGRADE=restart || ONUPGRADE=nostop
			sv stop ${NAME} || sv_warn
		else
			return 0
		fi
	fi	
}

postrm () {
	local action="${1}"

	if [ "${action}" != 'purge' ] && [ "${action}" != 'remove' ] ; then
	    return
	fi

	# When "ENABLE=no" the $NAME link is an admin decision
	# so we don't remove it.
	# Links in other runsvdirs is responsibility of administrator.
	if [ "${action}" = 'remove' ] && [ "${ENABLE}" = yes ] ; then
		rm -f "/etc/runit/runsvdir/default/$NAME"
	fi
	if [ "${action}" = 'purge' ] ; then
		rm -f "/etc/runit/runsvdir/default/$NAME"
		rm -f "/etc/runit/runsvdir/default/.$NAME"
		#TODO: make sure runsv exit before we remove supervise files, printf x on supervise
	fi

	# If runscript was never invoked, there will be no files
	# in this directory, and `dpkg' will remove it. In this case,
	# we have nothing to do.
	for supervise in "/etc/sv/$NAME/supervise" \
	                 "/etc/sv/$NAME/log/supervise"; do
		if [ -d "$supervise" ] ; then
			#TODO test/loop that runsv is gone before remove supervise files
			# Actually only `down' may be absent, but it does not
			# matter.

			for file in control lock ok pid stat status down ; do
				rm -f "$supervise/$file"
			done

			# It should be empty now. If it is not, it means that
			# system administrator put something there. It is very
			# stupid, but will of user is sacred, and directory is
			# left as-is in such case.
			#
			# NOTE: Non-POSIX option is used. The day coreutils will
			# no longer be essential, it will require a fix.
			if [ -h "$supervise" ]; then
				rm "$supervise"
			else
				rmdir --ignore-fail-on-non-empty "$supervise"
			fi
		fi
	done

	if [ "${action}" = 'purge' ] ; then
		#rm -f "/etc/runit/runsvdir/default/$NAME"
		#rm -f "/etc/runit/runsvdir/default/.$NAME"
		readonly logdir="/var/log/runit/${NAME}"
		if [ -d "${logdir}" ] ; then
			rm -r "${logdir}"
		fi
	fi
}

#called as NAME='servicename' PKG='pkgname' /usr/lib/runit-helper/runit-helper sv_purge
sv_purge () {
	#TODO block non root usage || add support for user services (non-root-usage??)
	[ -z "$PKG" ] && echo "fatal: PKG $PKG empty" && return 1
	[ -z "$NAME" ] && echo "fata: NAME $NAME empty" && return 1
	if echo "$NAME" | grep -qE '.*/$' ; then
		echo "fatal: NAME $NAME not allowed, ends with /" && return 1
		#ends with / , dangerous with rm -rf
	fi
	#svdir=/etc/service
	etcdir=/etc/sv
	runtimesvdir=/usr/share/runit/sv.now
	logdir=/var/log/runit
	rm_svdir () { #dir $runtimesvdir/$NAME #lock supervise/lock
		dir="$1" ; lock="$2" ;
		echo "removing (purge) $dir ..."
		if [ -e "$dir/$lock" ]; then
			if timeout 5 flock "$dir/$lock" true ; then
				rm -rf "$dir" && echo " .. done"
			else
				echo "warning: runsv still running: $dir not purged!"
			fi
		else
			rm -rf "$dir" && echo "..done"
		fi
	}
	#if it's enabled, try to make sure runsv exits before the dir is removed
	if [ -h /etc/service/"$NAME" ]; then
		#TODO what to do with instances? if name=@default : disable and touch down ?
		echo "$NAME: purged by: $PKG" > "/etc/service/$NAME/down"
		timeout 0.2 printf x >  "/etc/service/$NAME/supervise/control"
		rm -f "/etc/service/$NAME"
	fi
	rm -f "/etc/service/.$NAME"
	rm -f "/etc/runit/runsvdir/default/$NAME"
	rm -f "/etc/runit/runsvdir/default/.$NAME"
	# must check if runsv is still running - and wait - before removing the service directory
	#etc or runtimedir
	if [ -d "$runtimesvdir/$NAME"  ] && [ -r "$runtimesvdir/$NAME/.meta/pkg" ]; then
		thispkg="$(cat "$runtimesvdir/$NAME/.meta/pkg")"
		if [ "$thispkg" = "$PKG" ]; then
			rm_svdir "$runtimesvdir/$NAME" "supervise/lock"
		fi
	elif [ -d "$etcdir/$NAME" ] && [ -r "$etcdir/$NAME/.meta/pkg" ] ; then
		thispkg="$(cat "$etcdir/$NAME/.meta/pkg")"
		if [ "$thispkg" = "$PKG" ]; then
			rm_svdir "$etcdir/$NAME" "supervise/lock"
		fi
	fi
	#logdir
	if [ -d "$logdir/$NAME" ] && [ -r "$logdir/$NAME/.pkg" ]; then
		thispkg="$(cat "$logdir/$NAME/.pkg")"
		if [ "$thispkg" = "$PKG" ]; then
			rm_svdir "$logdir/$NAME" "lock"
		fi
	fi
	#sysv overrides files
	rm -f "/etc/runit/override-sysv.d/$NAME.pkgrunit"
	#remove supervise
	if [ -d "/run/runit/supervise/$NAME" ]; then
		rm_svdir "/run/runit/supervise/$NAME" "lock"
	fi
}

"$@"
# vim: sw=4:et