rc.d 3.21 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
     $name help
Sébastien Luttringer's avatar
Sébastien Luttringer committed
27
EOF
28
29
30
31
32
33
34
35
36
	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
37
			printf "${C_FAIL}:: ${C_DONE}Daemon script ${C_FAIL}${daemon}${C_DONE} does \
38
39
40
41
not exist or is not executable.${C_CLEAR}\n" >&2
			exit 2
		fi
		# check filter
Lukas Fleischer's avatar
Lukas Fleischer committed
42
43
44
45
		(( ${filter[started]} )) && ck_daemon "$daemon" && continue
		(( ${filter[stopped]} )) && ! ck_daemon "$daemon" && continue
		(( ${filter[auto]} )) && ck_autostart "$daemon" && continue
		(( ${filter[noauto]} )) && ! ck_autostart "$daemon" && continue
46
47
48
		new_daemons+=("$daemon")
	done
	daemons=("${new_daemons[@]}")
Sébastien Luttringer's avatar
Sébastien Luttringer committed
49
50
}

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

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

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

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