aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Gundersen <teg@jklm.no>2011-06-18 22:30:22 +0200
committerTom Gundersen <teg@jklm.no>2011-06-18 22:30:22 +0200
commitf5d571110b9cfe45ecb32602510c924409e52d63 (patch)
treef7738f1b652c909eb6b27103b6b9393e5af563a6
parent4cad51425325abf13b7a8772ff09f852238423c2 (diff)
parent042d197b4d989ec6461204b9d897054e5f5dd492 (diff)
downloadinitscripts-f5d571110b9cfe45ecb32602510c924409e52d63.tar.xz
Merge remote-tracking branch 'seblu/master'
-rw-r--r--bash-completion8
-rw-r--r--functions10
-rwxr-xr-xrc.d16
-rw-r--r--zsh-completion5
4 files changed, 33 insertions, 6 deletions
diff --git a/bash-completion b/bash-completion
index 5151972..489b5dc 100644
--- a/bash-completion
+++ b/bash-completion
@@ -5,15 +5,17 @@ _rc.d ()
local action="help list start stop reload restart"
local cur="${COMP_WORDS[COMP_CWORD]}"
local caction="${COMP_WORDS[1]}"
- if ((${COMP_CWORD} == 1)); then
+ if ((COMP_CWORD == 1)); then
COMPREPLY=($(compgen -W "${action}" -- "$cur"))
- elif [[ "$caction" =~ help|list ]]; then
+ elif [[ "$caction" == help ]]; then
COMPREPLY=()
+ elif [[ "$caction" == list ]]; then
+ ((COMP_CWORD == 2)) && COMPREPLY=($(compgen -W "started stopped" -- "$cur")) || COMPREPLY=()
elif [[ "$caction" == start ]]; then
COMPREPLY=($(comm -23 <(cd /etc/rc.d && compgen -f -X 'functions*' "$cur"|sort) <(cd /run/daemons/ && compgen -f "$cur"|sort)))
elif [[ "$caction" =~ stop|restart|reload ]]; then
COMPREPLY=($(cd /run/daemons/ && compgen -f "$cur"|sort))
- elif ((${COMP_CWORD} > 1)); then
+ elif ((COMP_CWORD > 1)); then
COMPREPLY=($(cd /etc/rc.d && compgen -f -X 'functions*' "$cur"|sort))
fi
}
diff --git a/functions b/functions
index adf4ea9..2ee56b4 100644
--- a/functions
+++ b/functions
@@ -394,5 +394,15 @@ for f in /etc/rc.d/functions.d/*; do
[[ -e $f ]] && . "$f"
done
+# Exit current shell if user is not root
+need_root() {
+ (( $EUID != 0 )) && printf 'You need to be root.\n' && exit 1
+}
+
+# Quit script if it's not running by root
+# This can be disabled in scripts sourcing functions by setting NEED_ROOT=0
+# A local call to need_root can be done to ensure part of script need root privilege
+(( ${NEED_ROOT:-1} == 1 )) && need_root
+
# End of file
# vim: set ts=2 sw=2 noet:
diff --git a/rc.d b/rc.d
index ad0a1e9..6d9a9f7 100755
--- a/rc.d
+++ b/rc.d
@@ -1,14 +1,22 @@
#!/bin/bash
+NEED_ROOT=0 # this script can be run without be root
. /etc/rc.conf
. /etc/rc.d/functions
usage() {
local name=${0##*/}
cat >&2 << EOF
-usage: $name action daemon ...
+usage: $name <action> <daemon> [daemon] ...
+ $name list [started|stopped]
+ $name help
+
+<daemon> is the name of a script in /etc/rc.d
+<action> can be a start, stop, restart, reload, status, ...
+WARNING: initscripts are free to implement or not the above actions.
e.g: $name list
+ $name list started
$name help
$name start sshd gpm
EOF
@@ -23,13 +31,16 @@ case $1 in
usage
;;
list)
+ shift
cd /etc/rc.d/
for d in *; do
have_daemon "$d" || continue
# print running / stopped satus
if ! ck_daemon "$d"; then
+ [[ "$1" == stopped ]] && continue
printf "${C_OTHER}[${C_DONE}STARTED${C_OTHER}]"
else
+ [[ "$1" == started ]] && continue
printf "${C_OTHER}[${C_FAIL}STOPPED${C_OTHER}]"
fi
# print auto / manual status
@@ -40,7 +51,7 @@ case $1 in
fi
printf " ${C_CLEAR}$d\n"
done
- ;;
+ ;;
*)
# check min args count
(( $# < 2 )) && usage
@@ -62,6 +73,7 @@ case $1 in
fi
(( ret += !! $? )) # clamp exit value to 0/1
done
+ ;;
esac
exit $ret
diff --git a/zsh-completion b/zsh-completion
index f1b7165..e5c2850 100644
--- a/zsh-completion
+++ b/zsh-completion
@@ -15,9 +15,12 @@ _rc.d () {
curcontext="${curcontext%:*:*}:rc.d-${action}:"
case $action in
- list|help)
+ help)
_arguments "*: :"
;;
+ list)
+ _arguments "2: :(started stopped)"
+ ;;
start)
_arguments "*: :($(comm -23 <(echo /etc/rc.d/*(N-*:t)|tr ' ' '\n') <(echo /run/daemons/*(N:t)|tr ' ' '\n')))"
;;