xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fcntl/fcntl04.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
4  * Copyright (c) 2017 Cyril Hrubis <[email protected]>
5  */
6  /*
7   * Basic test for fcntl(2) using F_GETFL argument.
8   */
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <errno.h>
14 #include <stdio.h>
15 
16 #include "tst_test.h"
17 
18 static int fd;
19 static char fname[255];
20 
verify_fcntl(void)21 static void verify_fcntl(void)
22 {
23 	TEST(fcntl(fd, F_GETFL, 0));
24 
25 	if (TST_RET == -1) {
26 		tst_res(TFAIL | TTERRNO, "fcntl(%s, F_GETFL, 0) failed",
27 			fname);
28 		return;
29 	}
30 
31 	if ((TST_RET & O_ACCMODE) != O_RDWR) {
32 		tst_res(TFAIL, "fcntl(%s, F_GETFL, 0) returned wrong "
33 			"access mode %li, expected %i", fname,
34 			TST_RET & O_ACCMODE, O_RDWR);
35 		return;
36 	}
37 
38 	tst_res(TPASS, "fcntl(%s, F_GETFL, 0) returned %lx",
39 		fname, TST_RET);
40 }
41 
setup(void)42 static void setup(void)
43 {
44 	sprintf(fname, "fcntl04_%d", getpid());
45 	fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0700);
46 }
47 
cleanup(void)48 static void cleanup(void)
49 {
50 	if (fd > 0)
51 		SAFE_CLOSE(fd);
52 }
53 
54 static struct tst_test test = {
55 	.needs_tmpdir = 1,
56 	.test_all = verify_fcntl,
57 	.setup = setup,
58 	.cleanup = cleanup,
59 };
60