aboutsummaryrefslogtreecommitdiffstats
path: root/service
blob: c48f88b16de9f2dd576176607b15599f367d2d1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/bin/bash
# -*- Mode: shell-script -*-
# Copyright (C) 2001 by Chmouel Boudjnah <chmouel@mandrakesoft.com>
#			MandrakeSoft.
# Redistribution of this file is permitted under the terms of the GNU
# Public License (GPL)
# Original version writen by RedHat.

debug=
service=
opt=
fullrestart=
fullrestartall=
statusall=

servicedir=/etc/init.d
xinetddir=/etc/xinetd.d

basename=${0##*/}

. /etc/init.d/functions

function service_available () {
	local subsys
	cd $servicedir

	for subsys in *;do
		case $subsys in
			kheader|numlock|sound|usb|netfs|kudzu|local|pcmcia| \
			network|local|dm|harddrake|xfs) continue;;
		esac
		is_ignored_file "$service" && continue
		grep -q chkconfig $subsys || continue
		egrep -q 'restart.*\)' $subsys || continue
		[[ -x $subsys ]] || continue
		[[ -e /var/lock/subsys/$subsys ]] || continue
		echo $(egrep -a '^#.*chkconfig: ' $subsys|awk '{print $4}') $subsys
	done | sort -n|cut -d" " -f2
}

function usage () {
	cat <<EOF 1>&2
Usage: $basename -[Rfshv] SERVICE ARGUMENTS
	-f|--full-restart:	Do a fullrestart of the service.
	-R|--full-restart-all:	Do a fullrestart of all running services.
	-s|--status-all:	Print a status of all services.
	--ignore-dependencies:  Do not start required systemd services
	--skip-redirect:	Do not redirect to systemd
	-d|--debug:		Launch with debug.
	-h|--help:		This help.
EOF
exit 1
}
function check_if_inetd() {
    local serv=$1
    if [[ ! -f ${servicedir}/${serv} ]];then
	if [[ -f ${xinetddir}/${serv} ]];then
	    if egrep -q ".*disable.*yes.*" ${xinetddir}/${serv};then
		echo "$serv is a xinetd service and it is disabled"
		echo "to activate it do the following command:"
		echo "chkconfig ${serv} on"
		service=
		return
	    fi
	    service=xinetd
	    [[ $options = "start" ]] && options=reload
	    if [[ $options != reload ]] && [[ -z $fullrestart ]];then
		echo "There is no such option for xinetd services"
		echo "You can only use the start option to reload a xinetd service"
		service=
	    fi
	    echo "${serv} is a xinetd service"
	fi
    fi
    return
}

while [[ $1 = --* ]] || [[ $1 = -* ]];do
    opt=$1
    shift
    case $opt in
	--full-restart|-f) fullrestart=yes;;
	--full-restart-all|-R) fullrestartall=yes;;
	--status-all|-s) statusall=yes;;
	--debug|-d) set -x ; debug="/bin/bash -x";;
	--help|-h) usage;;
	--ignore-dependencies) export SYSTEMCTL_IGNORE_DEPENDENCIES=1;;
	--skip-redirect) export SYSTEMCTL_SKIP_REDIRECT=1;;
	*) echo "Unknow option $opt"; usage;
    esac
done

service=${1##*/}; shift;
options="$@"

#nuts ? (yes)
while :;do
    [[ -z $service && -n $statusall ]] && break
    [[ -z $service && -n $fullrestartall ]] && break
    [[ -n $service && -n $statusall ]] && { echo "--status-all doesn't need arguments"; usage ;}
    [[ -n $service && -n $fullrestartall ]] && { echo "--full-restart-all doesn't need arguments"; usage ;}
    [[ -z $service ]] && usage
    [[ -n $service ]] && break
done

if [[ -n $fullrestartall ]];then
    for subsys in $(service_available);do $0 -f $subsys;done
    exit 0 #for glibc post upgrades
fi

if [[ -n $statusall ]];then
    cd $servicedir
    for service in *;do
	case $service in
	    functions | halt | killall | single| linuxconf| kudzu | \
		mandrake_firstime | mandrake_everytime)
		continue ;;
	esac
	is_ignored_file "$service" && continue
	if egrep -q '^([^#]*)status\)' $service;then
	    $debug $servicedir/$service status
	fi
    done
    exit 0;
fi

if [[ -n $fullrestart ]];then
    check_if_inetd "$service"
    cd "$servicedir"
    if [[ -f ./$service ]];then
	$debug ./$service stop
	$debug ./$service start
	exit $?
    else
	echo "Cannot find $servicedir/$service"
	usage
    fi
    exit 1
fi

[[ -z $options ]] && { echo  "I need an action"; options="--help" ;}

check_if_inetd "$service" ; [[ -z $service ]] && exit 1

if ! is_ignored_file "$service" && [[ -f $servicedir/$service ]]; then
    env -i PATH="$PATH" TERM="$TERM" SYSTEMCTL_IGNORE_DEPENDENCIES=${SYSTEMCTL_IGNORE_DEPENDENCIES} SYSTEMCTL_SKIP_REDIRECT=${SYSTEMCTL_SKIP_REDIRECT} $debug "$servicedir/$service" $options
    exit $?
elif [[ -f /lib/systemd/system/"$service".service ]] && /bin/mountpoint -q /sys/fs/cgroup/systemd; then
    echo $"Redirecting to /bin/systemctl ${options} ${service}.service" > /dev/stderr
    exec /bin/systemctl ${options} ${service}.service
else
    echo "Cannot find $service service"
    usage
fi