aboutsummaryrefslogtreecommitdiff
blob: 160b9f8ec495fcb58a941cb9c32b81384edb3c33 (plain)
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# Copyright 1999-2009 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id$

inherit config

DESCRIPTION="Manage /etc/init.d scripts in runlevels"
MAINTAINER="Danny van Dyk <kugelfang@gentoo.org>"
SVN_DATE='$Date$'
VERSION=$(svn_date_to_version "${SVN_DATE}")

# source_rc_functions PRIVATE
# API for OpenRC or baselayout-1
source_rc_functions() {
	[[ ${RC_GOT_FUNCTIONS} = yes ]] && return
	source /etc/init.d/functions.sh || die "Failed to source functions.sh"
	# baselayout-1 compatibility
	if [[ -e ${svclib}/sh/rc-services.sh ]]; then
		source "${svclib}/sh/rc-services.sh" \
			|| die "Failed to source rc-services.sh"
	fi
}

# get_runlevel PRIVATE
# determine the current runlevel
get_runlevel() {
	if type rc_runlevel &>/dev/null; then
		rc_runlevel || die "rc_runlevel failed"
	elif [[ -n ${SOFTLEVEL} ]]; then
		echo "${SOFTLEVEL}"
	else
		die "Cannot determine runlevel"
	fi
}

# list_runlevels PRIVATE
# list runlevels for file $1
list_runlevels() {
	[[ ${#@} -eq 1 ]] || return
	local runlevels
	for x in ${ROOT}/etc/runlevels/* ; do
		[[ -d ${x} ]] || continue
		[[ -L ${ROOT}/etc/runlevels/${x##*/}/${1} ]] \
			&& runlevels=(${runlevels[@]} "${x##*/}")
	done
	echo -ne "${runlevels[@]}"
}

# is_script PRIVATE
# check if file $1 is a valid init script
is_script() {
	local file=${1}
	[[ -n ${file} ]] \
		|| return 1
	( [[ -L ${file} ]] \
		&& [[ ! -e $(canonicalise -f ${file}) ]] ) \
		&& return 1
	[[ -e ${file} ]] \
		&& [[ ${file%%.sh} == ${file} ]] \
		&& [[ ${file%%\~} == ${file} ]] \
		&& [[ -n `grep "^#\!/sbin/runscript" ${file}` ]]
}

# find_scripts PRIVATE
# browse directory $1 for init scripts and return a list
find_scripts() {
	local ret
	for file in ${1}/* ; do
		is_script ${file} \
			&& ret=(${ret[@]} "${file##*/}")
	done
	echo -ne ${ret[@]}
}

# run_runscript PRIVATE
# run RC_RUNSCRIPT with script $2- and command $1
run_runscript() {
	local command=${1}
	shift
	write_list_start "${1}"
	shift
	for script in ${@} ; do
		is_script ${ROOT}/etc/init.d/${script} \
			&& /sbin/runscript ${ROOT}/etc/init.d/${script} ${command}
	done
}

### add action

describe_add() {
	echo "Add script to existing runlevel(s)"
}

describe_add_parameters() {
	echo "<script> <runlevels>"
}

describe_add_options() {
	echo "script : Init script (from 'list' action)"
	echo "runlevels : Runlevels to add to (defaults to 'default')"
}

do_add() {
	[[ ${#@} -gt 0 ]] \
		|| die -q "Please specify the init script to be added!"
	local script=${1##*/}
	[[ -e ${ROOT}/etc/init.d/${script} ]] \
		|| die -q "Please specify a valid init script!"
	shift 1
	local runlevels=${@:-default}
	write_list_start "Adding $(highlight ${script}) to following runlevels"
	for runlevel in ${runlevels} ; do
		if [[ ! -d ${ROOT}/etc/runlevels/${runlevel} ]] ; then
			write_kv_list_entry ${runlevel} "[invalid]"
			continue
		else
			if [[ -L ${ROOT}/etc/runlevels/${runlevel}/${script} ]] ; then
				write_kv_list_entry ${runlevel} "[skipped]"
			else
				ln -sf \
					/etc/init.d/${script} \
					${ROOT}/etc/runlevels/${runlevel}/${script} \
					&& write_kv_list_entry ${runlevel} "[done]" \
					|| write_kv_list_entry ${runlevel} "[failed]"
			fi
		fi
	done
}

### delete action

describe_delete() {
	echo "Delete script from existing runlevel(s)"
}

describe_delete_parameters() {
	echo "<script> <runlevels>"
}

describe_delete_options() {
	echo "script : Init script (from 'list' action)"
	echo "runlevels : Runlevels to delete from (defaults to 'default')"
}

do_delete() {
	[[ ${#@} -gt 0 ]] \
		|| die -q "Please specify the init script to be deleted!"
	local script=${1##*/}
	shift 1
	[[ -e ${ROOT}/etc/init.d/${script} ]] || write_warning_msg \
		"Init script not found in /etc/init.d/. Continuing anyway."
	local runlevels=${@:-default}
	write_list_start "Deleting $(highlight ${script}) from following runlevels"
	for runlevel  in ${runlevels} ; do
		if [[ ! -d ${ROOT}/etc/runlevels/${runlevel} ]] ; then
			write_kv_list_entry ${runlevel} "[invalid]"
			continue
		else
			if [[ -L ${ROOT}/etc/runlevels/${runlevel}/${script} ]] ; then
				rm ${ROOT}/etc/runlevels/${runlevel}/${script} \
					&& write_kv_list_entry ${runlevel} "[done]" \
					|| write_kv_list_entry ${runlevel} "[failed]"
			else
				write_kv_list_entry ${runlevel} "[skipped]"
			fi
		fi
	done
}

### list action

describe_list() {
	echo "List all available init scripts"
}

describe_list_parameters() {
	echo "<runlevel>"
}

describe_list_options() {
	echo "runlevel : Runlevel to list (defaults to all)"
}

do_list() {
	local dir file item
	if [[ -n ${1} ]] && [[ -d ${ROOT}/etc/runlevels/${1} ]] ; then
		dir=${ROOT}/etc/runlevels/${1}
		write_list_start "Init scripts to be started by runlevel $(highlight ${1})"
	elif [[ -z ${1} ]] ; then
		dir=${ROOT}/etc/init.d
		write_list_start "Available init scripts"
	else
		die -q "${1} is no valid runlevel!"
	fi

	for file in $(find_scripts ${dir}) ; do
			[[ ${dir##*/} = init.d ]] \
				&& write_kv_list_entry ${file} "$(list_runlevels ${file})" \
				|| echo "  ${file}"
#				&& echo "  ${file} $(space $((20 - ${#file}))) | $(list_runlevels ${file})" \
#				|| echo "  ${file}"
	done
}

### show action

describe_show() {
	echo "Show init script status"
}

describe_show_parameters() {
	echo "<runlevels>"
}

describe_show_options() {
	echo "runlevels : Runlevels to list (defaults to current runlevel)"
}

do_show() {
	# subshell to avoid pollution of caller's environment
	(
		# core.bash redefines eval; unset it because OpenRC's
		# functions.sh needs the bash builtin
		unset -f eval
		source_rc_functions
		[[ $# -eq 0 ]] && set - "$(get_runlevel)"

		for runlevel in "$@"; do
			[[ -n ${runlevel} && -d ${ROOT}/etc/runlevels/${runlevel} ]] \
				|| die -q "\"${runlevel}\" is no valid runlevel"

			write_list_start \
				"Status of init scripts in runlevel \"${runlevel}\""
			for script in $(find_scripts "${ROOT}/etc/runlevels/${runlevel}")
			do
				status=unknown
				for x in stopping starting inactive started stopped; do
					if service_${x} ${script}; then
						status=${x}
						break
					fi
				done
				case ${status} in
					starting|inactive|unknown)
						write_kv_list_entry \
							${script} "$(highlight_warning [${x}])"
						;;
					started)
						write_kv_list_entry ${script} "$(highlight [${x}])"
						;;
					*)
						write_kv_list_entry ${script} [${x}]
						;;
				esac
			done
		done
	)
}

### start action

describe_start() {
	echo "Start given set of init scripts manually"
}

describe_start_parameters() {
	echo "<scripts>"
}

describe_start_options() {
	echo "scripts : Init scripts to start"
}

do_start() {
	[[ ${#@} -gt 0 ]] \
		|| die -q "Please specify the init script to be started!"
	run_runscript start "Starting init script$([[ ${#@} -gt 1 ]] && echo -ne 's')" ${@}
}

### stop action

describe_stop() {
	echo "Stop given set of init scripts manually"
}

describe_stop_parameters() {
	echo "<scripts>"
}

describe_stop_options() {
	echo "scripts : Init scripts to stop"
}

do_stop() {
	[[ ${#@} -gt 0 ]] \
		|| die -q "Please specify the init script to be stopped!"
	run_runscript stop "Stopping init script$([[ ${#@} -gt 1 ]] && echo -ne 's')" ${@}
}

### pause action

describe_pause() {
	echo "Pauses given set of init scripts manually"
}

describe_pause_parameters() {
	echo "<scripts>"
}

describe_pause_options() {
	echo "scripts : Init scripts to pause"
}

do_pause() {
	[[ ${#@} -gt 0 ]] \
		|| die -q "Please specify the init script to be paused!"
	run_runscript pause "Pausing init script$([[ ${#@} -gt 1 ]] && echo -ne 's')" ${@}
}

### reload action

describe_reload() {
	echo "Reload given set of init scripts"
}

describe_reload_parameters() {
	echo "<scripts>"
}

describe_reload_options() {
	echo "scripts : Init scripts to reload"
}

do_reload() {
	[[ ${#@} -gt 0 ]] \
		|| die -q "Please specify the init script to be reloaded!"
	run_runscript reload "Reloading init script$([[ ${#@} -gt 1 ]] && echo -ne 's')" ${@}
}

### restart action

describe_restart() {
	echo "Restart given set of init scripts"
}

describe_restart_parameters() {
	echo "<scripts>"
}

describe_restart_options() {
	echo "scripts : Init scripts to restart"
}

do_restart() {
	[[ ${#@} -gt 0 ]] \
		|| die -q "Please specify the init script to be restarted!"
	run_runscript restart "Restarting init script$([[ ${#@} -gt 1 ]] && echo -ne 's')" ${@}
}

# vim: set ft=eselect :