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