]> TLD Linux GIT Repositories - rc-scripts.git/blob - service
- merged TLDize patch
[rc-scripts.git] / service
1 #!/bin/sh
2
3 # Set up a default search path.
4 PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin"
5 export PATH
6
7 is_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
19 # check if SERVICE is present in systemd and ACTION is valid systemctl command
20 # returns false if systemd is disabled or not active
21 is_systemd_service() {
22         local SERVICE=$1 ACTION=$2
23
24         [ "$USE_SYSTEMD" = "no" ] && return 1
25
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
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
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
74 status_all() {
75         local SERVICE TYPE has_systemd
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         cd ${SERVICEDIR}
84         for SERVICE in *; do
85                 case "${SERVICE}" in
86                 functions | halt | killall | single| linuxconf| kudzu)
87                         ;;
88                 *)
89                 if ! is_ignored_file "${SERVICE}" \
90                                 && [ -x "${SERVICEDIR}/${SERVICE}" ]; then
91                         if [ "$has_systemd" ] && [ -f /lib/systemd/system/${SERVICE}.service ]; then
92                                 # D for SystemD
93                                 TYPE='D'
94                         else
95                                 # S for SysVinit
96                                 TYPE='S'
97                         fi
98                         if ! grep -qs "\Wstatus)" "$SERVICE"; then
99                                 printf " %s %-60s %s\n" "$TYPE:[?]" "$SERVICE:" "unknown"
100                                 continue
101                         else
102                                 out=$(env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" status 2>&1)
103                                 if [ "$?" = "0" -a -n "$out" ]; then
104                                         printf " %s %-60s %s\n" "$TYPE:[+]" "$SERVICE:" "running"
105                                         continue
106                                 else
107                                         printf " %s %-60s %s %s\n" "$TYPE:[-]" "$SERVICE:" "NOT running"
108                                         continue
109                                 fi
110                         fi
111                 fi
112                 ;;
113                 esac
114         done
115 }
116
117 VERSION="$(basename $0) ver. 1.0-tld"
118 USAGE="Usage: $(basename $0) < option > | --status-all | \
119 [ service_name [ command | --full-restart ] ]"
120
121 SERVICE=
122 USE_SYSTEMD=
123
124 if [ -d /etc/rc.d/init.d ]; then
125         SERVICEDIR="/etc/rc.d/init.d"
126 else
127         SERVICEDIR="/etc/init.d"
128 fi
129
130 if [ $# -eq 0 ]; then
131         echo "${USAGE}" >&2
132         exit 1
133 fi
134
135 cd /
136 while [ $# -gt 0 ]; do
137         case "${1}" in
138         --help | -h | --h* )
139                 echo "${USAGE}" >&2
140                 exit 0
141                 ;;
142         --version | -V )
143                 echo "${VERSION}" >&2
144                 exit 0
145                 ;;
146         --ignore-dependencies)
147                 export SYSTEMCTL_IGNORE_DEPENDENCIES=1
148                 shift
149                 ;;
150         --skip-redirect)
151                 export SYSTEMCTL_SKIP_REDIRECT=1
152                 shift
153                 ;;
154         --no-systemd)
155                 USE_SYSTEMD=no
156                 shift
157                 ;;
158           *)
159                 if [ -z "${SERVICE}" -a $# -eq 1 -a "${1}" = "--status-all" ]; then
160                         status_all
161                         exit 0
162                 elif [ $# -eq 2 -a "${2}" = "--full-restart" ]; then
163                         SERVICE="${1}"
164                         if [ -x "${SERVICEDIR}/${SERVICE}" ]; then
165                                 env -i LANG=$LANG PATH=$PATH TERM=$TERM "${SERVICEDIR}/${SERVICE}" stop
166                                 env -i LANG=$LANG PATH=$PATH TERM=$TERM "${SERVICEDIR}/${SERVICE}" start
167                                 exit $?
168                         fi
169                 elif [ -z "${SERVICE}" ]; then
170                         SERVICE="${1}"
171                 elif [ -z "${ACTION}" ]; then
172                         ACTION="${1}"
173                 else
174                         OPTIONS="${OPTIONS} ${1}"
175                 fi
176                 shift
177                 ;;
178         esac
179 done
180
181 if is_systemd_service "${SERVICE}" "${ACTION}"; then
182         echo >&2 "Redirecting to /bin/systemctl --output=cat ${ACTION} ${SERVICE}.service ${OPTIONS}"
183         exec /bin/systemctl --output=cat ${ACTION} ${SERVICE}.service ${OPTIONS}
184 elif [ -x "${SERVICEDIR}/${SERVICE}" ]; then
185         exec env -i LANG=$LANG PATH=$PATH TERM=$TERM "${SERVICEDIR}/${SERVICE}" ${ACTION} ${OPTIONS}
186 else
187         echo "${SERVICE}: unrecognized service" >&2
188         exit 1
189 fi