1# Lock Test with abnormal or abrupt termination (System crash or SIGKILL)
2
3use Test;
4use File::NFSLock;
5use Fcntl qw(O_CREAT O_RDWR O_RDONLY O_TRUNC LOCK_EX);
6
7$| = 1; # Buffer must be autoflushed because of fork() below.
8plan tests => 10;
9
10my $datafile = "testfile.dat";
11
12# Wipe lock file in case it exists
13unlink ("$datafile$File::NFSLock::LOCK_EXTENSION");
14
15# Create a blank file
16sysopen ( FH, $datafile, O_CREAT | O_RDWR | O_TRUNC );
17close (FH);
18# test 1
19ok (-e $datafile && !-s _);
20
21
22# test 2
23ok (pipe(RD1,WR1)); # Connected pipe for child1
24
25my $pid = fork;
26if (!$pid) {
27  # Child #1 process
28  # Obtain exclusive lock
29  my $lock = new File::NFSLock {
30    file => $datafile,
31    lock_type => LOCK_EX,
32  };
33  open(STDERR,">/dev/null");
34  print WR1 !!$lock; # Send boolean success status down pipe
35  close(WR1); # Signal to parent that the Blocking lock is done
36  close(RD1);
37  if ($lock) {
38    sleep 10;  # hold the lock for a moment
39    sysopen(FH, $datafile, O_RDWR | O_TRUNC);
40    # And then put a magic word into the file
41    print FH "exclusive\n";
42    close FH;
43  }
44  exit;
45}
46
47# test 3
48ok 1; # Fork successful
49close (WR1);
50# Waiting for child1 to finish its lock status
51my $child1_lock = <RD1>;
52close (RD1);
53# Report status of the child1_lock.
54# It should have been successful
55# test 4
56ok ($child1_lock);
57
58# Pretend like the box crashed rudely while the lock is aquired
59# test 5
60ok (kill "KILL", $pid);
61
62# Clear the zombie
63# test 6
64ok (wait);
65
66# test 7
67ok (pipe(RD2,WR2)); # Connected pipe for child2
68if (!fork) {
69  # The last lock died, so this should aquire fine.
70  my $lock = new File::NFSLock {
71    file => $datafile,
72    lock_type => LOCK_EX,
73    blocking_timeout => 10,
74  };
75  if ($lock) {
76    sysopen(FH, $datafile, O_RDWR | O_TRUNC);
77    # Immediately put the magic word into the file
78    print FH "lock2\n";
79    truncate (FH, tell FH);
80    close FH;
81  }
82  print WR2 !!$lock; # Send boolean success status down pipe
83  close(WR2); # Signal to parent that the Blocking lock is done
84  close(RD2);
85  exit; # Release this new lock
86}
87# test 8
88ok 1; # Fork successful
89close (WR2);
90
91# Waiting for child2 to finish its lock status
92my $child2_lock = <RD2>;
93close (RD2);
94# Report status of the child2_lock.
95# This should have been successful.
96# test 9
97ok ($child2_lock);
98
99# Load up whatever the file says now
100sysopen(FH, $datafile, O_RDONLY);
101
102$_ = <FH>;
103# test 10
104ok /lock2/;
105close FH;
106
107# Wipe the temporary file
108unlink $datafile;
109