rc.d 3.23 KB
Newer Older
Sébastien Luttringer's avatar
Sébastien Luttringer committed
1
2
3
4
5
#!/bin/bash

. /etc/rc.conf
. /etc/rc.d/functions

6
# print usage and exit
Sébastien Luttringer's avatar
Sébastien Luttringer committed
7
usage() {
Sébastien Luttringer's avatar
Sébastien Luttringer committed
8
9
	local name=${0##*/}
	cat >&2 << EOF
10
usage: $name <action> [options] [daemons]
Sébastien Luttringer's avatar
Sébastien Luttringer committed
11

12
13
14
15
16
17
18
options:
  -s, --started     Filter started daemons
  -S, --stopped     Filter stopped daemons
  -a, --auto        Filter auto started daemons
  -A, --noauto      Filter manually started daemons

<daemons> is a space separated list of script in /etc/rc.d
Sébastien Luttringer's avatar
Sébastien Luttringer committed
19
20
<action> can be a start, stop, restart, reload, status, ...
WARNING: initscripts are free to implement or not the above actions.
Sébastien Luttringer's avatar
Sébastien Luttringer committed
21

Sébastien Luttringer's avatar
Sébastien Luttringer committed
22
e.g: $name list
23
24
     $name list sshd gpm
     $name list --started gpm
Sébastien Luttringer's avatar
Sébastien Luttringer committed
25
     $name start sshd gpm
26
27
     $name stop --noauto
     $name help
Sébastien Luttringer's avatar
Sébastien Luttringer committed
28
EOF
29
30
31
32
33
34
35
36
37
	exit ${1:-1}
}

# filter list of daemons
filter_daemons() {
	local -a new_daemons=()
	for daemon in "${daemons[@]}"; do
		# check if daemons is valid
		if ! have_daemon "$daemon"; then
Jelle van der Waa's avatar
Jelle van der Waa committed
38
			printf "${C_FAIL}:: ${C_DONE}Daemon script ${C_FAIL}${daemon}${C_DONE} does \
39
40
41
42
not exist or is not executable.${C_CLEAR}\n" >&2
			exit 2
		fi
		# check filter
Lukas Fleischer's avatar
Lukas Fleischer committed
43
44
45
46
		(( ${filter[started]} )) && ck_daemon "$daemon" && continue
		(( ${filter[stopped]} )) && ! ck_daemon "$daemon" && continue
		(( ${filter[auto]} )) && ck_autostart "$daemon" && continue
		(( ${filter[noauto]} )) && ! ck_autostart "$daemon" && continue
47
48
49
		new_daemons+=("$daemon")
	done
	daemons=("${new_daemons[@]}")
Sébastien Luttringer's avatar
Sébastien Luttringer committed
50
51
}

52
(( $# < 1 )) && usage
Sébastien Luttringer's avatar
Sébastien Luttringer committed
53

54
# ret store the return code of rc.d
55
declare -i ret=0
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
# daemons store daemons on which action will be executed
declare -a daemons=()
# filter store current filter mode
declare -A filter=([started]=0 [stopped]=0 [auto]=0 [noauto]=0)

# parse options
argv=$(getopt -l 'started,stopped,auto,noauto' -- 'sSaA' "$@") || usage
eval set -- "$argv"

# create an initial daemon list
while [[ "$1" != -- ]]; do
	case "$1" in
		-s|--started)		filter[started]=1 ;;
		-S|--stopped)		filter[stopped]=1 ;;
		-a|--auto)			filter[auto]=1 ;;
		-A|--noauto)		filter[noauto]=1 ;;
	esac
	shift
done

# remove --
shift
# get action
action=$1
shift

# get initial daemons list
for daemon; do
	daemons+=("$daemon")
done

# going into script directory
cd /etc/rc.d

case $action in
Sébastien Luttringer's avatar
Sébastien Luttringer committed
91
	help)
92
93
		usage 0 2>&1
	;;
Sébastien Luttringer's avatar
Sébastien Luttringer committed
94
	list)
95
96
97
98
		# list take all daemons by default
		[[ -z $daemons ]] && for d in *; do have_daemon "$d" && daemons+=("$d"); done
		filter_daemons
		for daemon in "${daemons[@]}"; do
Sébastien Luttringer's avatar
Sébastien Luttringer committed
99
			# print running / stopped satus
100
101
			if ! ck_daemon "$daemon"; then
				s_status="${C_OTHER}[${C_DONE}STARTED${C_OTHER}]"
Sébastien Luttringer's avatar
Sébastien Luttringer committed
102
			else
103
				s_status="${C_OTHER}[${C_FAIL}STOPPED${C_OTHER}]"
Sébastien Luttringer's avatar
Sébastien Luttringer committed
104
105
			fi
			# print auto / manual status
106
107
			if ! ck_autostart "$daemon"; then
				s_auto="${C_OTHER}[${C_DONE}AUTO${C_OTHER}]"
Sébastien Luttringer's avatar
Sébastien Luttringer committed
108
			else
109
				s_auto="${C_OTHER}[${C_FAIL}    ${C_OTHER}]"
Sébastien Luttringer's avatar
Sébastien Luttringer committed
110
			fi
111
			printf "$s_status$s_auto${C_CLEAR} $daemon\n"
Sébastien Luttringer's avatar
Sébastien Luttringer committed
112
		done
113
	;;
Sébastien Luttringer's avatar
Sébastien Luttringer committed
114
	*)
115
116
117
		# other actions need an explicit daemons list
		[[ -z $daemons ]] && usage
		filter_daemons
118
119
		# set same environment variables as init
		runlevel=$(/sbin/runlevel)
120
		ENV=('PATH=/bin:/usr/bin:/sbin:/usr/sbin'
Dave Reisner's avatar
Dave Reisner committed
121
122
123
124
			"PREVLEVEL=${runlevel%% *}"
			"RUNLEVEL=${runlevel##* }"
			"CONSOLE=${CONSOLE:-/dev/console}"
			"TERM=$TERM")
125
		cd /
126
127
		for daemon in "${daemons[@]}"; do
			env -i "${ENV[@]}" "/etc/rc.d/$daemon" "$action"
128
			(( ret += !! $? ))  # clamp exit value to 0/1
Sébastien Luttringer's avatar
Sébastien Luttringer committed
129
		done
130
	;;
Sébastien Luttringer's avatar
Sébastien Luttringer committed
131
132
esac

133
exit $ret
Sébastien Luttringer's avatar
Sébastien Luttringer committed
134

135
# vim: set ts=2 sw=2 ft=sh noet: