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

3
NEED_ROOT=0 # this script can be run without be root
Sébastien Luttringer's avatar
Sébastien Luttringer committed
4
5
6
. /etc/rc.conf
. /etc/rc.d/functions

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

13
14
15
16
17
18
19
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
20
21
<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
22

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

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

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

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

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