Skip to content

Commit 1965225

Browse files
committed
init: Fix init scripts to work with containers
Previously init scripts were not using pid file so pidof was used. This is usually not a problem, but when containers are used it may result to killing improper instance when issued on host. Solution is to always use pidfile. Also try to use LSB complaint status codes. Signed-off-by: Jan Friesse <[email protected]> Reviewed-by: Christine Caulfield <[email protected]>
1 parent c65923b commit 1965225

File tree

2 files changed

+92
-14
lines changed

2 files changed

+92
-14
lines changed

init/corosync-qdevice.in

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
desc="Corosync Qdevice daemon"
2323
prog="corosync-qdevice"
24+
prog_pid_file="@LOCALSTATEDIR@/run/corosync-qdevice/$prog.pid"
2425

2526
# set secure PATH
2627
PATH="/sbin:/bin:/usr/sbin:/usr/bin:@SBINDIR@"
@@ -35,10 +36,48 @@ failure()
3536
echo -ne "[FAILED]\r"
3637
}
3738

39+
# pid_var_run pid_file
40+
# Echo pid from given pid_file.
41+
# Returns LSB exit code for the 'status' action.
42+
pid_var_run()
43+
{
44+
local pid_file="$1"
45+
local pid
46+
47+
if [ -f "$pid_file" ]; then
48+
[ ! -r "$pid_file" ] && return 4
49+
pid=$(cat "$pid_file")
50+
[ -z "$pid" ] && return 1
51+
[ -n "${pid//[0-9]/}" ] && return 1
52+
if kill -n 0 "$pid" 2>/dev/null;then
53+
echo "$pid"
54+
return 0
55+
else
56+
return 1
57+
fi
58+
fi
59+
60+
return 3
61+
}
62+
63+
# status [-p pid_file] {program}
3864
status()
3965
{
40-
pid=$(pidof $1 2>/dev/null)
66+
local pid_file
67+
68+
if [ "$1" = "-p" ]; then
69+
pid_file=$2
70+
shift 2
71+
fi
72+
73+
pid=$(pid_var_run "$pid_file" 2>/dev/null)
4174
res=$?
75+
if [ $res -ne 0 -a -z "$pid_file" ]; then
76+
pid=$(__pids_pidof "$1")
77+
[ -n "$pid" ]
78+
res=$?
79+
fi
80+
4281
if [ $res -ne 0 ]; then
4382
echo "$1 is stopped"
4483
else
@@ -92,7 +131,7 @@ start()
92131
chmod 0770 "@LOCALSTATEDIR@/run/corosync-qdevice"
93132
fi
94133

95-
if status $prog > /dev/null 2>&1; then
134+
if status -p "$prog_pid_file" "$prog" > /dev/null 2>&1; then
96135
success
97136
else
98137
$prog $COROSYNC_QDEVICE_OPTIONS > /dev/null 2>&1
@@ -110,15 +149,15 @@ start()
110149

111150
stop()
112151
{
113-
! status $prog > /dev/null 2>&1 && return
152+
! status -p "$prog_pid_file" "$prog" > /dev/null 2>&1 && return
114153

115154
echo -n "Signaling $desc ($prog) to terminate: "
116-
kill -TERM "$(pidof $prog)" > /dev/null 2>&1
155+
kill -TERM "$(pid_var_run $prog_pid_file)" > /dev/null 2>&1
117156
success
118157
echo
119158

120159
echo -n "Waiting for $prog services to unload:"
121-
while status $prog > /dev/null 2>&1; do
160+
while status -p "$prog_pid_file" "$prog" > /dev/null 2>&1; do
122161
sleep 1
123162
echo -n "."
124163
done
@@ -144,12 +183,12 @@ restart|reload|force-reload)
144183
restart
145184
;;
146185
condrestart|try-restart)
147-
if status $prog > /dev/null 2>&1; then
186+
if status -p "$prog_pid_file" "$prog" > /dev/null 2>&1; then
148187
restart
149188
fi
150189
;;
151190
status)
152-
status $prog
191+
status -p "$prog_pid_file" "$prog"
153192
rtrn=$?
154193
;;
155194
stop)

init/corosync-qnetd.in

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
desc="Corosync Qdevice Network daemon"
2323
prog="corosync-qnetd"
24+
prog_pid_file="@LOCALSTATEDIR@/run/corosync-qnetd/$prog.pid"
2425

2526
# set secure PATH
2627
PATH="/sbin:/bin:/usr/sbin:/usr/bin:@SBINDIR@"
@@ -35,10 +36,48 @@ failure()
3536
echo -ne "[FAILED]\r"
3637
}
3738

39+
# pid_var_run pid_file
40+
# Echo pid from given pid_file.
41+
# Returns LSB exit code for the 'status' action.
42+
pid_var_run()
43+
{
44+
local pid_file="$1"
45+
local pid
46+
47+
if [ -f "$pid_file" ]; then
48+
[ ! -r "$pid_file" ] && return 4
49+
pid=$(cat "$pid_file")
50+
[ -z "$pid" ] && return 1
51+
[ -n "${pid//[0-9]/}" ] && return 1
52+
if kill -n 0 "$pid" 2>/dev/null;then
53+
echo "$pid"
54+
return 0
55+
else
56+
return 1
57+
fi
58+
fi
59+
60+
return 3
61+
}
62+
63+
# status [-p pid_file] {program}
3864
status()
3965
{
40-
pid=$(pidof $1 2>/dev/null)
66+
local pid_file
67+
68+
if [ "$1" = "-p" ]; then
69+
pid_file=$2
70+
shift 2
71+
fi
72+
73+
pid=$(pid_var_run "$pid_file" 2>/dev/null)
4174
res=$?
75+
if [ $res -ne 0 -a -z "$pid_file" ]; then
76+
pid=$(__pids_pidof "$1")
77+
[ -n "$pid" ]
78+
res=$?
79+
fi
80+
4281
if [ $res -ne 0 ]; then
4382
echo "$1 is stopped"
4483
else
@@ -95,7 +134,7 @@ start()
95134
fi
96135
fi
97136

98-
if status $prog > /dev/null 2>&1; then
137+
if status -p "$prog_pid_file" "$prog" > /dev/null 2>&1; then
99138
success
100139
else
101140
if [ -z "$COROSYNC_QNETD_RUNAS" ];then
@@ -117,15 +156,15 @@ start()
117156

118157
stop()
119158
{
120-
! status $prog > /dev/null 2>&1 && return
159+
! status -p "$prog_pid_file" "$prog" > /dev/null 2>&1 && return
121160

122161
echo -n "Signaling $desc ($prog) to terminate: "
123-
kill -TERM "$(pidof $prog)" > /dev/null 2>&1
162+
kill -TERM "$(pid_var_run $prog_pid_file)" > /dev/null 2>&1
124163
success
125164
echo
126165

127166
echo -n "Waiting for $prog services to unload:"
128-
while status $prog > /dev/null 2>&1; do
167+
while status -p "$prog_pid_file" "$prog" > /dev/null 2>&1; do
129168
sleep 1
130169
echo -n "."
131170
done
@@ -151,12 +190,12 @@ restart|reload|force-reload)
151190
restart
152191
;;
153192
condrestart|try-restart)
154-
if status $prog > /dev/null 2>&1; then
193+
if status -p "$prog_pid_file" "$prog" > /dev/null 2>&1; then
155194
restart
156195
fi
157196
;;
158197
status)
159-
status $prog
198+
status -p "$prog_pid_file" "$prog"
160199
rtrn=$?
161200
;;
162201
stop)

0 commit comments

Comments
 (0)