1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /* Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved.
3*49cdfc7eSAndroid Build Coastguard Worker * Author: Vatsal Avasthi
4*49cdfc7eSAndroid Build Coastguard Worker *
5*49cdfc7eSAndroid Build Coastguard Worker * Test Description:
6*49cdfc7eSAndroid Build Coastguard Worker * This test verifies that flock() behavior with different locking
7*49cdfc7eSAndroid Build Coastguard Worker * combinations along with LOCK_SH and LOCK_EX:
8*49cdfc7eSAndroid Build Coastguard Worker * 1) flock() succeeded in acquiring shared lock on shared lock file.
9*49cdfc7eSAndroid Build Coastguard Worker * 2) flock() failed to acquire exclusive lock on shared lock file.
10*49cdfc7eSAndroid Build Coastguard Worker * 3) flock() failed to acquire shared lock on exclusive lock file.
11*49cdfc7eSAndroid Build Coastguard Worker * 4) flock() failed to acquire exclusive lock on exclusive lock file.
12*49cdfc7eSAndroid Build Coastguard Worker */
13*49cdfc7eSAndroid Build Coastguard Worker
14*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <sys/file.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
17*49cdfc7eSAndroid Build Coastguard Worker
18*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
19*49cdfc7eSAndroid Build Coastguard Worker
20*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
21*49cdfc7eSAndroid Build Coastguard Worker int operation;
22*49cdfc7eSAndroid Build Coastguard Worker char *f_lock;
23*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
24*49cdfc7eSAndroid Build Coastguard Worker {LOCK_SH, "shared lock"},
25*49cdfc7eSAndroid Build Coastguard Worker {LOCK_EX, "exclusive lock"},
26*49cdfc7eSAndroid Build Coastguard Worker };
27*49cdfc7eSAndroid Build Coastguard Worker
child(int opt,int should_pass,char * lock)28*49cdfc7eSAndroid Build Coastguard Worker static void child(int opt, int should_pass, char *lock)
29*49cdfc7eSAndroid Build Coastguard Worker {
30*49cdfc7eSAndroid Build Coastguard Worker int retval, fd1;
31*49cdfc7eSAndroid Build Coastguard Worker
32*49cdfc7eSAndroid Build Coastguard Worker fd1 = SAFE_OPEN("testfile", O_RDWR);
33*49cdfc7eSAndroid Build Coastguard Worker retval = flock(fd1, opt);
34*49cdfc7eSAndroid Build Coastguard Worker if (should_pass) {
35*49cdfc7eSAndroid Build Coastguard Worker tst_res(retval == -1 ? TFAIL : TPASS,
36*49cdfc7eSAndroid Build Coastguard Worker " Child acquiring %s got %d", lock, retval);
37*49cdfc7eSAndroid Build Coastguard Worker } else {
38*49cdfc7eSAndroid Build Coastguard Worker tst_res(retval == -1 ? TPASS : TFAIL,
39*49cdfc7eSAndroid Build Coastguard Worker " Child acquiring %s got %d", lock, retval);
40*49cdfc7eSAndroid Build Coastguard Worker }
41*49cdfc7eSAndroid Build Coastguard Worker
42*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd1);
43*49cdfc7eSAndroid Build Coastguard Worker exit(0);
44*49cdfc7eSAndroid Build Coastguard Worker }
45*49cdfc7eSAndroid Build Coastguard Worker
verify_flock(unsigned n)46*49cdfc7eSAndroid Build Coastguard Worker static void verify_flock(unsigned n)
47*49cdfc7eSAndroid Build Coastguard Worker {
48*49cdfc7eSAndroid Build Coastguard Worker int fd2;
49*49cdfc7eSAndroid Build Coastguard Worker pid_t pid;
50*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = &tcases[n];
51*49cdfc7eSAndroid Build Coastguard Worker
52*49cdfc7eSAndroid Build Coastguard Worker fd2 = SAFE_OPEN("testfile", O_RDWR);
53*49cdfc7eSAndroid Build Coastguard Worker TEST(flock(fd2, tc->operation));
54*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET != 0) {
55*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "flock() failed to acquire %s",
56*49cdfc7eSAndroid Build Coastguard Worker tc->f_lock);
57*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd2);
58*49cdfc7eSAndroid Build Coastguard Worker return;
59*49cdfc7eSAndroid Build Coastguard Worker }
60*49cdfc7eSAndroid Build Coastguard Worker
61*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Parent had %s", tc->f_lock);
62*49cdfc7eSAndroid Build Coastguard Worker
63*49cdfc7eSAndroid Build Coastguard Worker pid = SAFE_FORK();
64*49cdfc7eSAndroid Build Coastguard Worker if (pid == 0)
65*49cdfc7eSAndroid Build Coastguard Worker child(LOCK_SH | LOCK_NB, tc->operation & LOCK_SH, "shared lock");
66*49cdfc7eSAndroid Build Coastguard Worker else
67*49cdfc7eSAndroid Build Coastguard Worker tst_reap_children();
68*49cdfc7eSAndroid Build Coastguard Worker
69*49cdfc7eSAndroid Build Coastguard Worker pid = SAFE_FORK();
70*49cdfc7eSAndroid Build Coastguard Worker if (pid == 0)
71*49cdfc7eSAndroid Build Coastguard Worker child(LOCK_EX | LOCK_NB, 0, "exclusive lock");
72*49cdfc7eSAndroid Build Coastguard Worker else
73*49cdfc7eSAndroid Build Coastguard Worker tst_reap_children();
74*49cdfc7eSAndroid Build Coastguard Worker
75*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd2);
76*49cdfc7eSAndroid Build Coastguard Worker }
77*49cdfc7eSAndroid Build Coastguard Worker
setup(void)78*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
79*49cdfc7eSAndroid Build Coastguard Worker {
80*49cdfc7eSAndroid Build Coastguard Worker int fd;
81*49cdfc7eSAndroid Build Coastguard Worker
82*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN("testfile", O_CREAT | O_TRUNC | O_RDWR, 0644);
83*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
84*49cdfc7eSAndroid Build Coastguard Worker }
85*49cdfc7eSAndroid Build Coastguard Worker
86*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
87*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
88*49cdfc7eSAndroid Build Coastguard Worker .test = verify_flock,
89*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
90*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
91*49cdfc7eSAndroid Build Coastguard Worker .forks_child = 1,
92*49cdfc7eSAndroid Build Coastguard Worker };
93