-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpp.back.kit.svc
198 lines (174 loc) · 5.03 KB
/
pp.back.kit.svc
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
pp_kit_service_group_script () {
typeset grp svcs scriptpath out
grp="$1"
svcs="$2"
scriptpath="/sbin/init.d/$grp"
out="$pp_destdir$scriptpath"
pp_add_file_if_missing $scriptpath run 755 || return 0
cat <<-. > $out
#!/sbin/sh
# generated by pp $pp_version
svcs="$svcs"
.
cat <<-'.' >> $out
#-- starts services in order.. stops them all if any break
pp_start () {
undo=
for svc in $svcs; do
/sbin/init.d/$svc start
case $? in
0|4)
undo="$svc $undo"
;;
*)
if test -n "$undo"; then
for svc in $undo; do
/sbin/init.d/$svc stop
done
return 1
fi
;;
esac
done
return 0
}
#-- stops services in reverse
pp_stop () {
reverse=
for svc in $svcs; do
reverse="$svc $reverse"
done
rc=0
for svc in $reverse; do
/sbin/init.d/$svc stop || rc=$?
done
return $rc
}
case "$1" in
start_msg) echo "Starting $svcs";;
stop_msg) echo "Stopping $svcs";;
start) pp_start;;
stop) pp_stop;;
*) echo "usage: $0 {start|stop|start_msg|stop_msg}"
exit 1;;
esac
.
}
pp_kit_service_script () {
typeset svc scriptpath out
svc="$1"
scriptpath="/sbin/init.d/$svc"
pp_load_service_vars "$svc"
test -n "$user" -a x"$user" != x"root" &&
cmd="SHELL=/usr/bin/sh /usr/bin/su $user -c \"exec `echo $cmd | sed -e 's,[$\\\`],\\&,g'`\""
if test -z "$pidfile"; then
pidfile="/var/run/$svc.pid"
cmd="$cmd & echo \$! > \$pidfile"
fi
pp_add_file_if_missing $scriptpath run 755
cat <<-. > $pp_destdir$scriptpath
svc="$svc"
pidfile="$pidfile"
pp_start () {
$cmd
}
.
cat <<-'.' >>$pp_destdir$scriptpath
pp_stop () {
if test ! -s "$pidfile"; then
echo "Unable to stop $svc (no pid file)"
return 1
else
read pid < "$pidfile"
if kill -0 "$pid" 2>/dev/null; then
if kill -${stop_signal:-TERM} "$pid"; then
rm -f "$pidfile"
return 0
else
echo "Unable to stop $svc"
return 1
fi
else
rm -f "$pidfile"
return 0
fi
fi
}
pp_running () {
if test ! -s "$pidfile"; then
return 1
else
read pid < "$pidfile"
kill -0 "$pid" 2>/dev/null
fi
}
case "$1" in
start_msg) echo "Starting the $svc service";;
stop_msg) echo "Stopping the $svc service";;
start)
if pp_running; then
echo "$svc already running";
exit 0
elif pp_start; then
echo "$svc started";
# rc(1M) says we should exit 4, but nobody expects it!
exit 0
else
exit 1
fi
;;
stop)
if pp_stop; then
echo "$svc stopped";
exit 0
else
exit 1
fi
;;
*) echo "usage: $0 {start|stop|start_msg|stop_msg}"
exit 1
;;
esac
.
}
pp_kit_make_service () {
typeset level priority startlevels stoplevels
typeset svc svcvar
svc="$1"
svcvar=`pp_makevar $svc`
#-- don't do anything if the script exists
if test -s "$pp_destdir/sbin/init.d/$svc"; then
pp_error "$pp_destdir/sbin/init.d/$svc exists"
return
fi
# symlink the script, depending on the priorities chosen
eval priority='${pp_kit_priority_'$svcvar'}'
test -z "$priority" && priority="${pp_kit_priority:-50}"
eval startlevels='${pp_kit_startlevels_'$svcvar'}'
test -z "$startlevels" && startlevels="$pp_kit_startlevels"
eval stoplevels='${pp_kit_stoplevels_'$svcvar'}'
test -z "$stoplevels" && stoplevels="$pp_kit_stoplevels"
# create the script and config file
pp_kit_service_script $svc
# fix the priority up
case "$priority" in
???) :;;
??) priority=0$priority;;
?) priority=00$priority;;
esac
if test x"$stoplevels" = x"auto"; then
stoplevels=
test -z "$startlevels" || for level in $startlevels; do
stoplevels="$stoplevels `expr $level - 1`"
done
fi
# create the symlinks
test -z "$startlevels" || for level in $startlevels; do
echo " ln -s /sbin/init.d/$svc /sbin/rc$level.d/S$priority$svc" >>$pp_wrkdir/%post.run
echo " rm /sbin/rc$level.d/S$priority$svc" >>$pp_wrkdir/%preun.run
done
test -z "$stoplevels" || for level in $stoplevels; do
echo " ln -s /sbin/init.d/$svc /sbin/rc$level.d/K$priority$svc" >>$pp_wrkdir/%post.run
echo " rm -f /sbin/rc$level.d/K$priority$svc" >>$pp_wrkdir/%preun.run
done
}