File: init

package info (click to toggle)
mon 0.38.15-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 708 kB
  • ctags: 238
  • sloc: perl: 6,458; ansic: 774; sh: 332; makefile: 112
file content (105 lines) | stat: -rw-r--r-- 2,644 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
97
98
99
100
101
102
103
104
105
#!/bin/sh -e

# $Id: init,v 1.4 1999/09/05 20:48:02 roderick Exp $

script=`basename "$0"`

daemon=/usr/sbin/mon
cfg=/etc/mon/mon.cf
pid=/var/run/mon/mon.pid
desc='monitor daemon'
name=mon
user=daemon
group=shadow

# Add system dirs for start-stop-daemon.
PATH=$PATH:/sbin:/usr/sbin

[ -f $daemon -a -f $cfg ] || exit 0

# Don't bother if the user hasn't configured the package, there is no
# default configuration but there is a default config file (which just
# contains comments).

egrep -v '^[ 	]*$|^#' $cfg >/dev/null || exit 0

warn() {
    echo "$script:" "$@" >&2
}

die() {
    warn "$@"
    exit 1
}

usage() {
    [ $# = 0 ] || warn "$@"
    warn "usage: \`$script <action>'"
    warn "valid actions: start stop restart reload force-reload"
    exit 1
}

# I originally used su to start the daemon as user daemon, but ran into
# trouble with the differing requirements of su and secure-su.  The GNU
# getopt's troublsome behavior of looking for switches anywhere on the
# command line requires that one use -- if one wants to pass switches
# to the shell, but secure-su doesn't allow the --.  One could set
# POSIXLY_CORRECT to prevent this poor getopt_long() behavior, but then
# the sub-command would have that in its environment.  In disgust I
# gave up and switched to using Perl.

daemon_run() {
    perl -we '
    	$user = shift;
	$group = shift;
    	defined($uid = getpwnam $user)
	    or die "Cannot find uid for user $user\n";
	defined($gid = getgrnam $group)
	    or die "Cannot find gid for group $group\n";
	$( = $gid;
    	$) = "$gid $gid";
	if ($( ne "$gid $gid" || $) ne "$gid $gid") {
	    die "Error setting group ids, real/effective is now $(/$)\n";
	}
	$< = $uid;
	$> = $uid;
	if ($> != $uid || $< != $uid) {
	    die "Error setting user ids, real/effective is now $</$>\n";
	}
	exec @ARGV or die "Error running $ARGV[0]: $!\n";
    ' "$user" "$group" "$@" ||
	die "return $? setting ids and running:" "$@"
}

[ $# = 1 ] || usage "wrong number of args (got $# expected 1), args are:" "$@"

action=$1
set -- --pidfile $pid --startas $daemon -- -f

case x-$action in
    x-start)
	echo -n "Starting $desc: $name"
	daemon_run start-stop-daemon --start "$@"
	echo .
	;;
    x-stop)
	echo -n "Stopping $desc: $name"
	start-stop-daemon --stop --oknodo --quiet "$@"
	echo .
	;;
    x-restart)
    	echo -n "Restarting $desc: $name"
	start-stop-daemon --stop --oknodo --quiet "$@"
	daemon_run start-stop-daemon --start "$@"
	echo .
	;;
    x-reload | x-force-reload)
	echo "Reloading $desc configuration files."
	start-stop-daemon --stop --signal 1 "$@"
	;;
    *)
    	usage "invalid action \`$action'"
	;;
esac

exit 0