]> git.pld-linux.org Git - projects/rc-scripts.git/blame - service
netfs: respect VSERVER_ISOLATION_NET here as well
[projects/rc-scripts.git] / service
CommitLineData
763b88c2
AM
1#!/bin/sh
2
3b917b57
AM
3# Set up a default search path.
4PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin"
5export PATH
6
58f95af1
ER
7is_ignored_file() {
8 case "$1" in
9 skeleton | README | *.dpkg-dist | *.dpkg-old | rc | rcS | single | reboot | bootclean.sh)
10 return 0
11 ;;
12 *rpmorig | *rpmnew | *rpmsave | *~ | *.orig)
13 return 0
14 ;;
15 esac
16 return 1
17}
18
cb7c8b9c 19# check if SERVICE is present in systemd and ACTION is valid systemctl command
8e9e592e
ER
20# returns false if systemd is disabled or not active
21is_systemd_service() {
cb7c8b9c 22 local SERVICE=$1 ACTION=$2
8e9e592e
ER
23
24 [ "$USE_SYSTEMD" = "no" ] && return 1
cb7c8b9c 25
0c5d56cd
ER
26 # if we are called from systemd itself, because some .service specified
27 # invocation via /sbin/service. this avoids loops
28 # detect this via CMDLINE var, which has leaked from geninitrd
29 if [ -n "$CMDLINE" ]; then
30 echo >&2 "Warning: CMDLINE env set, likely you are defining .service to use /sbin/service, please use /etc/rc.d/init.d/<SERVICE> instead"
31 return 1
32 fi
33
cb7c8b9c
ER
34 case "$ACTION" in
35 # list obtained as: man systemctl | grep N.*A.*M.*E
36 start | \
37 stop | \
38 reload | \
39 restart | \
40 try-restart | \
41 reload-or-restart | \
42 reload-or-try-restart | \
43 isolate | \
44 kill | \
45 is-active | \
46 status | \
47 show | \
48 reset-failed | \
49 enable | \
50 disable | \
51 is-enabled | \
52 reenable | \
53 preset | \
54 mask | \
55 unmask | \
56 link | \
57 load | \
58 snapshot | \
59 delete | \
60 set-environment | \
61 unset-environment )
62 ;;
63 *)
64 #echo "Not valid systemd command"
65 return 1
66 esac
67
8e9e592e
ER
68 [ -x /bin/systemd_booted ] || return 1
69 /bin/systemd_booted || return 1
70
71 /bin/systemctl show "$SERVICE".service | grep -q LoadError= && return 1 || return 0
72}
73
cd835e00 74status_all() {
ab907cc4
ER
75 local SERVICE TYPE has_systemd has_upstart
76
77 if [ "$USE_SYSTEMD" != "no" ] && [ -x /bin/systemd_booted ] && /bin/systemd_booted; then
78 has_systemd=1
79 else
80 unset has_systemd
81 fi
82
83 if [ "$USE_UPSTART" != "no" ] && [ -x /sbin/initctl ]; then
84 has_upstart=1
85 else
86 unset has_upstart
87 fi
88
cd835e00
ER
89 cd ${SERVICEDIR}
90 for SERVICE in *; do
91 case "${SERVICE}" in
92 functions | halt | killall | single| linuxconf| kudzu)
93 ;;
94 *)
95 if ! is_ignored_file "${SERVICE}" \
96 && [ -x "${SERVICEDIR}/${SERVICE}" ]; then
ab907cc4
ER
97 if [ "$has_systemd" ] && [ -f /lib/systemd/system/${SERVICE}.service ]; then
98 # D for SystemD
99 TYPE='D'
100 elif [ "$has_upstart" ] && [ -f /etc/init/${SERVICE}.conf ]; then
88a9daf2
ER
101 # U for upstart
102 TYPE='U'
103 else
104 # S for SysVinit
105 TYPE='S'
106 fi
cd835e00 107 if ! grep -qs "\Wstatus)" "$SERVICE"; then
ab907cc4 108 printf " %s %-60s %s\n" "$TYPE:[?]" "$SERVICE:" "unknown"
cd835e00
ER
109 continue
110 else
111 out=$(env -i USE_UPSTART=$USE_UPSTART LANG="$LANG" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" status 2>&1)
112 if [ "$?" = "0" -a -n "$out" ]; then
ab907cc4 113 printf " %s %-60s %s\n" "$TYPE:[+]" "$SERVICE:" "running"
cd835e00
ER
114 continue
115 else
ab907cc4 116 printf " %s %-60s %s %s\n" "$TYPE:[-]" "$SERVICE:" "NOT running"
cd835e00
ER
117 continue
118 fi
119 fi
cd835e00
ER
120 fi
121 ;;
122 esac
123 done
124}
125
86ae1ad7 126VERSION="$(basename $0) ver. 0.91-pld"
bf14fcab 127USAGE="Usage: $(basename $0) < option > | --status-all | \
763b88c2 128[ service_name [ command | --full-restart ] ]"
3b917b57 129
763b88c2 130SERVICE=
9fb27aac 131USE_UPSTART=
8e9e592e 132USE_SYSTEMD=
3b917b57 133
763b88c2 134if [ -d /etc/rc.d/init.d ]; then
1050f687 135 SERVICEDIR="/etc/rc.d/init.d"
763b88c2 136else
1050f687 137 SERVICEDIR="/etc/init.d"
763b88c2
AM
138fi
139
140if [ $# -eq 0 ]; then
1050f687
JR
141 echo "${USAGE}" >&2
142 exit 1
763b88c2
AM
143fi
144
a6c22d3d
AM
145cd /
146while [ $# -gt 0 ]; do
1050f687 147 case "${1}" in
dd0f0d33 148 --help | -h | --h* )
1050f687
JR
149 echo "${USAGE}" >&2
150 exit 0
151 ;;
dd0f0d33 152 --version | -V )
1050f687
JR
153 echo "${VERSION}" >&2
154 exit 0
155 ;;
75ca301e
AM
156 --ignore-dependencies)
157 export SYSTEMCTL_IGNORE_DEPENDENCIES=1
158 shift
159 ;;
160 --skip-redirect)
161 export SYSTEMCTL_SKIP_REDIRECT=1
162 shift
163 ;;
36924fb4
ER
164 --upstart)
165 USE_UPSTART=yes
9fb27aac
JK
166 shift
167 ;;
36924fb4
ER
168 --no-upstart)
169 USE_UPSTART=no
9fb27aac
JK
170 shift
171 ;;
8e9e592e
ER
172 --no-systemd)
173 USE_SYSTEMD=no
174 shift
175 ;;
cd835e00 176 *)
1050f687 177 if [ -z "${SERVICE}" -a $# -eq 1 -a "${1}" = "--status-all" ]; then
cd835e00 178 status_all
1050f687
JR
179 exit 0
180 elif [ $# -eq 2 -a "${2}" = "--full-restart" ]; then
181 SERVICE="${1}"
182 if [ -x "${SERVICEDIR}/${SERVICE}" ]; then
9fb27aac
JK
183 env -i USE_UPSTART=$USE_UPSTART LANG=$LANG PATH=$PATH TERM=$TERM "${SERVICEDIR}/${SERVICE}" stop
184 env -i USE_UPSTART=$USE_UPSTART LANG=$LANG PATH=$PATH TERM=$TERM "${SERVICEDIR}/${SERVICE}" start
1050f687
JR
185 exit $?
186 fi
187 elif [ -z "${SERVICE}" ]; then
188 SERVICE="${1}"
dd0f0d33
ER
189 elif [ -z "${ACTION}" ]; then
190 ACTION="${1}"
1050f687
JR
191 else
192 OPTIONS="${OPTIONS} ${1}"
193 fi
194 shift
195 ;;
196 esac
763b88c2
AM
197done
198
cb7c8b9c 199if is_systemd_service "${SERVICE}" "${ACTION}"; then
81ea6e4f
ER
200 echo >&2 "Redirecting to /bin/systemctl --output=cat ${ACTION} ${SERVICE}.service ${OPTIONS}"
201 exec /bin/systemctl --output=cat ${ACTION} ${SERVICE}.service ${OPTIONS}
aa57b7e6
AM
202elif [ -x "${SERVICEDIR}/${SERVICE}" ]; then
203 exec env -i USE_UPSTART=$USE_UPSTART LANG=$LANG PATH=$PATH TERM=$TERM "${SERVICEDIR}/${SERVICE}" ${ACTION} ${OPTIONS}
763b88c2 204else
1050f687
JR
205 echo "${SERVICE}: unrecognized service" >&2
206 exit 1
763b88c2 207fi
This page took 0.122646 seconds and 4 git commands to generate.