]>
Commit | Line | Data |
---|---|---|
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 | ||
15 | TS_TOPDIR="${0%/*}/../.." | |
16 | TS_DESC="flock" | |
17 | ||
18 | . $TS_TOPDIR/functions.sh | |
19 | ts_init "$*" | |
20 | ||
21 | ts_check_test_command "$TS_CMD_FLOCK" | |
ca6d721a | 22 | ts_check_prog "ps" |
ae02bd0b | 23 | ts_check_prog "timeout" |
10146e56 | 24 | |
d25d4534 KZ |
25 | if ! ps --ppid $$ &>/dev/null; then |
26 | ts_skip "no ps --ppid" | |
27 | fi | |
10146e56 KZ |
28 | |
29 | function 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 | |
48 | GEN_OUTPUT="$TS_OUTPUT" | |
49 | START=$(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 | 54 | pid=$! |
10146e56 | 55 | |
ca6d721a RM |
56 | # check for running background process |
57 | if [ "$pid" -le "0" ] || ! kill -s 0 "$pid" &>/dev/null; then | |
58 | ts_die "unable to run flock" | |
59 | fi | |
60 | # the lock should be established when flock has a child | |
61 | timeout 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 | |
64 | ts_init_subtest "non-block" | |
65 | do_lock "--nonblock --conflict-exit-code 123" 123 "You will never see this!" | |
66 | ts_finalize_subtest | |
67 | ||
68 | ||
69 | ts_init_subtest "no-fork" | |
70 | do_lock "--no-fork --nonblock --conflict-exit-code 123" 123 "You will never see this!" | |
71 | ts_finalize_subtest | |
72 | ||
73 | ||
74 | ts_init_subtest "shared" | |
75 | do_lock "--shared" 0 "Have shared lock" | |
76 | ts_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 | |
81 | ts_init_subtest "exclusive" | |
82 | do_lock "--nonblock --exclusive --conflict-exit-code 123" 123 "You will never see this!" | |
83 | ts_finalize_subtest | |
84 | ||
85 | ||
86 | ts_init_subtest "timeout" | |
87 | do_lock "--timeout 5 --conflict-exit-code 5" 0 "After timeout." | |
88 | END=$(date '+%s') | |
89 | ts_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 | # | |
96 | ts_init_subtest "time-check" | |
97 | TIMEDIFF=$(( $END - $START )) | |
98 | if [ $TIMEDIFF -lt 3 ]; then | |
99 | ts_log "general lock failed [$TIMEDIFF sec]" | |
100 | elif [ $TIMEDIFF -gt 5 ]; then | |
101 | ts_log "wait too long [$TIMEDIFF sec]" | |
102 | else | |
103 | ts_log "success" | |
104 | fi | |
105 | ts_finalize_subtest "diff ${TIMEDIFF} sec" | |
106 | ||
107 | ||
108 | echo "Unlocked" >> $GEN_OUTPUT | |
109 | ts_finalize |