File: services

package info (click to toggle)
hibernate 1.07-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 316 kB
  • ctags: 52
  • sloc: sh: 1,014; makefile: 33
file content (96 lines) | stat: -rw-r--r-- 2,631 bytes parent folder | download
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
# -*- sh -*-
# vim:ft=sh:ts=8:sw=4:noet

AddConfigHandler ServicesOptions
AddConfigHelp "StopServices <service name> [...]" "The services listed are stopped prior to suspending. The service name must correspond to the name of an init.d script that is active in the current runlevel."
AddConfigHelp "StartServices <service name> [...]" "The services listed are started after resuming. The service name must correspond to the name of an init.d script that is active in the current runlevel."
AddConfigHelp "RestartServices <service name> [...]" "The services listed are stopped before suspending and started after resuming. The service name must correspond to the name of an init.d script that is active in the current runlevel."

# ExecuteServices <action> <services ...>
ExecuteServices() {
    local action
    local services
    local service
    action=$1
    shift
    services=$@
    ret=0
    for service in $services ; do
	[ -x "$INITDIR/$service" ] || continue
	CMD="$INITDIR/$service $action"
	vecho 2 "Executing $CMD"
	$CMD || ret=1
	RESTARTED_SERVICES="$service $RESTARTED_SERVICES"
    done
    return $ret
}

ServicesStop() {
    [ -z "$SERVICES_HOOKED" ] && return 0
    ExecuteServices stop $SERVICES_STOP
    RESTARTED_SERVICES=
    ExecuteServices stop $SERVICES_RESTART
    return 0 # Don't abort just because a service failed to stop, right?
}

ServicesStart() {
    [ -z "$SERVICES_HOOKED" ] && return 0
    ExecuteServices start $RESTARTED_SERVICES
    ExecuteServices start $SERVICES_START
    # preserve exit code
}

ServicesOptions() {
    case $1 in
	stopservices)
	    shift
	    SERVICES_STOP="$@"
	    ;;
	startservices)
	    shift
	    SERVICES_START="$@"
	    ;;
	restartservices)
	    shift
	    SERVICES_RESTART="$@"
	    ;;
	*)
	    return 1
    esac
    if [ -z "$SERVICES_HOOKED" ] ; then
	AddSuspendHook 30 ServicesStop
	AddResumeHook 30 ServicesStart
	SERVICES_HOOKED=1
	ServicesDetectDistro
    fi
    return 0
}

ServicesDetectDistro() {
    # Use either a given $DISTRIBUTION or autodetect one.
    case "$DISTRIBUTION" in
	gentoo)
	    INITDIR=/etc/init.d
	    ;;
	suse|mandrake)
	    INITDIR=/etc/init.d
	    ;;
	debian|slackware|redhat|fedora)
	    INITDIR=/etc/init.d
	    ;;
    	*)
	    # Auto-detect
	    if [ -d "/etc/init.d/" ] ; then
	    	INITDIR=/etc/init.d
	    elif [ -d "/etc/rc.d/init.d" ] ; then
	    	INITDIR=/etc/rc.d/init.d
	    else
		vecho 0 "Can not determine init.d directory. Services will not be suspended!"
		SERVICES_HOOKED=""
	    fi
    esac
    vecho 3 "Using '$INITDIR' as init.d directory."
    return 0
}

# $Id: services 623 2005-01-04 16:58:41Z dagobah $