1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved.
4*49cdfc7eSAndroid Build Coastguard Worker * Author: Vatsal Avasthi
5*49cdfc7eSAndroid Build Coastguard Worker *
6*49cdfc7eSAndroid Build Coastguard Worker * Test Description:
7*49cdfc7eSAndroid Build Coastguard Worker * 1) flock() returns -1 and sets error number to EBADF if the file descriptor
8*49cdfc7eSAndroid Build Coastguard Worker * is invalid.
9*49cdfc7eSAndroid Build Coastguard Worker * 2) flock() returns -1 and sets error number to EINVAL if the argument
10*49cdfc7eSAndroid Build Coastguard Worker * operation does not include LOCK_SH,LOCK_EX,LOCK_UN.
11*49cdfc7eSAndroid Build Coastguard Worker * 3) flock() returns -1 and sets error number to EINVAL if an invalid
12*49cdfc7eSAndroid Build Coastguard Worker * combination of locking modes is used i.e LOCK_SH with LOCK_EX
13*49cdfc7eSAndroid Build Coastguard Worker */
14*49cdfc7eSAndroid Build Coastguard Worker
15*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <sys/file.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 int badfd = -1;
21*49cdfc7eSAndroid Build Coastguard Worker static int fd;
22*49cdfc7eSAndroid Build Coastguard Worker
23*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
24*49cdfc7eSAndroid Build Coastguard Worker int *fd;
25*49cdfc7eSAndroid Build Coastguard Worker int operation;
26*49cdfc7eSAndroid Build Coastguard Worker int exp_err;
27*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
28*49cdfc7eSAndroid Build Coastguard Worker {&badfd, LOCK_SH, EBADF},
29*49cdfc7eSAndroid Build Coastguard Worker {&fd, LOCK_NB, EINVAL},
30*49cdfc7eSAndroid Build Coastguard Worker {&fd, LOCK_SH | LOCK_EX, EINVAL},
31*49cdfc7eSAndroid Build Coastguard Worker };
32*49cdfc7eSAndroid Build Coastguard Worker
verify_flock(unsigned n)33*49cdfc7eSAndroid Build Coastguard Worker static void verify_flock(unsigned n)
34*49cdfc7eSAndroid Build Coastguard Worker {
35*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = &tcases[n];
36*49cdfc7eSAndroid Build Coastguard Worker
37*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN("testfile", O_RDWR);
38*49cdfc7eSAndroid Build Coastguard Worker TEST(flock(*tc->fd, tc->operation));
39*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET == 0) {
40*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "flock() succeeded unexpectedly");
41*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
42*49cdfc7eSAndroid Build Coastguard Worker return;
43*49cdfc7eSAndroid Build Coastguard Worker }
44*49cdfc7eSAndroid Build Coastguard Worker
45*49cdfc7eSAndroid Build Coastguard Worker if (tc->exp_err == TST_ERR) {
46*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS | TTERRNO, "flock() failed expectedly");
47*49cdfc7eSAndroid Build Coastguard Worker } else {
48*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "flock() failed unexpectedly, "
49*49cdfc7eSAndroid Build Coastguard Worker "expected %s", tst_strerrno(tc->exp_err));
50*49cdfc7eSAndroid Build Coastguard Worker }
51*49cdfc7eSAndroid Build Coastguard Worker
52*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
53*49cdfc7eSAndroid Build Coastguard Worker }
54*49cdfc7eSAndroid Build Coastguard Worker
setup(void)55*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
56*49cdfc7eSAndroid Build Coastguard Worker {
57*49cdfc7eSAndroid Build Coastguard Worker int fd1;
58*49cdfc7eSAndroid Build Coastguard Worker
59*49cdfc7eSAndroid Build Coastguard Worker fd1 = SAFE_OPEN("testfile", O_CREAT | O_TRUNC | O_RDWR, 0666);
60*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd1);
61*49cdfc7eSAndroid Build Coastguard Worker }
62*49cdfc7eSAndroid Build Coastguard Worker
63*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
64*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
65*49cdfc7eSAndroid Build Coastguard Worker .test = verify_flock,
66*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
67*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
68*49cdfc7eSAndroid Build Coastguard Worker };
69