1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
5 * Mountain View, CA 94043, or:
6 *
7 * http://www.sgi.com
8 *
9 * For further information regarding this notice, see:
10 *
11 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
12 *
13 * AUTHOR : William Roske
14 * CO-PILOT : Dave Fenner
15 */
16
17 /*\
18 * [Description]
19 *
20 * Basic test for fcntl(2) using F_GETLK argument.
21 *
22 * If the lock could be placed, fcntl() does not actually place it, but
23 * returns F_UNLCK in the l_type field of lock and leaves the other field
24 * of the structure unchanged.
25 */
26
27 #include <stdio.h>
28 #include <sys/types.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <sys/stat.h>
32 #include "tst_test.h"
33
34 static int fd = -1, pid;
35 static struct flock flocks;
36
verify_fcntl(void)37 static void verify_fcntl(void)
38 {
39 /* F_GETLK will change flock.l_type to F_UNLCK, so need to reset */
40 flocks.l_type = F_RDLCK;
41
42 TST_EXP_PASS(fcntl(fd, F_GETLK, &flocks), "fcntl(%d, F_GETLK, &flocks)", fd);
43 TST_EXP_EQ_LI(flocks.l_type, F_UNLCK);
44 TST_EXP_EQ_LI(flocks.l_whence, SEEK_CUR);
45 TST_EXP_EQ_LI(flocks.l_start, 0);
46 TST_EXP_EQ_LI(flocks.l_len, 0);
47 TST_EXP_EQ_LI(flocks.l_pid, pid);
48 }
49
setup(void)50 static void setup(void)
51 {
52 pid = getpid();
53 fd = SAFE_OPEN("filename", O_RDWR | O_CREAT, 0700);
54
55 flocks.l_whence = SEEK_CUR;
56 flocks.l_start = 0;
57 flocks.l_len = 0;
58 flocks.l_pid = pid;
59 }
60
cleanup(void)61 static void cleanup(void)
62 {
63 if (fd > -1)
64 SAFE_CLOSE(fd);
65 }
66
67 static struct tst_test test = {
68 .needs_tmpdir = 1,
69 .test_all = verify_fcntl,
70 .setup = setup,
71 .cleanup = cleanup,
72 };
73