]> git.ipfire.org Git - thirdparty/util-linux.git/blame - tests/ts/misc/flock
tests: check for ps --ppid
[thirdparty/util-linux.git] / tests / ts / misc / flock
CommitLineData
10146e56
KZ
1#!/bin/bash
2
3# This file is part of util-linux.
4#
5# This file is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This file is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14
15TS_TOPDIR="${0%/*}/../.."
16TS_DESC="flock"
17
18. $TS_TOPDIR/functions.sh
19ts_init "$*"
20
21ts_check_test_command "$TS_CMD_FLOCK"
ca6d721a 22ts_check_prog "ps"
ae02bd0b 23ts_check_prog "timeout"
10146e56 24
d25d4534
KZ
25if ! ps --ppid $$ &>/dev/null; then
26 ts_skip "no ps --ppid"
27fi
10146e56
KZ
28
29function do_lock {
30 local opts="$1"
31 local expected_rc="$2"
32 local mesg="$3"
33
34 $TS_CMD_FLOCK $1 $TS_OUTDIR/lockfile \
35 echo "$mesg" \
36 > $TS_OUTPUT 2>&1
37
38 local rc="$?"
39
40 if [ "$rc" == "$expected_rc" ]; then
41 ts_log "Success"
42 else
43 ts_log "Failed [rc=$rc]"
44 fi
45}
46
47# general lock
48GEN_OUTPUT="$TS_OUTPUT"
49START=$(date '+%s')
ca6d721a 50# running flock in background is not the best usage example
10146e56
KZ
51$TS_CMD_FLOCK --shared $TS_OUTDIR/lockfile \
52 bash -c 'echo "Locking"; sleep 3; echo "Unlocking"' \
53 > $GEN_OUTPUT 2>&1 &
ca6d721a 54pid=$!
10146e56 55
ca6d721a
RM
56# check for running background process
57if [ "$pid" -le "0" ] || ! kill -s 0 "$pid" &>/dev/null; then
58 ts_die "unable to run flock"
59fi
60# the lock should be established when flock has a child
61timeout 1s bash -c "while [ \$(ps --ppid $pid |wc -l) -lt 2 ]; do sleep 0.1 ;done" \
62 || ts_die "timeout waiting for flock child"
10146e56
KZ
63
64ts_init_subtest "non-block"
65do_lock "--nonblock --conflict-exit-code 123" 123 "You will never see this!"
66ts_finalize_subtest
67
68
69ts_init_subtest "no-fork"
70do_lock "--no-fork --nonblock --conflict-exit-code 123" 123 "You will never see this!"
71ts_finalize_subtest
72
73
74ts_init_subtest "shared"
75do_lock "--shared" 0 "Have shared lock"
76ts_finalize_subtest
77
78
79# this is the same as non-block test (exclusive lock is the default), but here
80# we explicitly specify --exclusive on command line
81ts_init_subtest "exclusive"
82do_lock "--nonblock --exclusive --conflict-exit-code 123" 123 "You will never see this!"
83ts_finalize_subtest
84
85
86ts_init_subtest "timeout"
87do_lock "--timeout 5 --conflict-exit-code 5" 0 "After timeout."
88END=$(date '+%s')
89ts_finalize_subtest
90
91
92# expected is 3 seconds (see "sleep 3" for the general lock), but we should not
93# rely on exact number due to scheduler, machine load, etc. Let's check for
94# inmterval <3,5>.
95#
96ts_init_subtest "time-check"
97TIMEDIFF=$(( $END - $START ))
98if [ $TIMEDIFF -lt 3 ]; then
99 ts_log "general lock failed [$TIMEDIFF sec]"
100elif [ $TIMEDIFF -gt 5 ]; then
101 ts_log "wait too long [$TIMEDIFF sec]"
102else
103 ts_log "success"
104fi
105ts_finalize_subtest "diff ${TIMEDIFF} sec"
106
107
108echo "Unlocked" >> $GEN_OUTPUT
109ts_finalize