xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fcntl/fcntl03.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_GETFD argument.
8   */
9 
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <unistd.h>
14 #include <errno.h>
15 #include <stdio.h>
16 
17 #include "tst_test.h"
18 
19 static char fname[255];
20 static int fd;
21 
verify_fcntl(void)22 static void verify_fcntl(void)
23 {
24 	TEST(fcntl(fd, F_GETFD, 0));
25 
26 	if (TST_RET == -1) {
27 		tst_res(TFAIL | TTERRNO, "fcntl(%s, F_GETFD, 0) failed",
28 			fname);
29 		return;
30 	}
31 
32 	tst_res(TPASS, "fcntl(%s, F_GETFD, 0) returned %ld",
33 		fname, TST_RET);
34 }
35 
setup(void)36 static void setup(void)
37 {
38 	sprintf(fname, "fcntl03_%d", getpid());
39 	fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0700);
40 }
41 
cleanup(void)42 static void cleanup(void)
43 {
44 	if (fd > 0)
45 		SAFE_CLOSE(fd);
46 }
47 
48 static struct tst_test test = {
49 	.needs_tmpdir = 1,
50 	.test_all = verify_fcntl,
51 	.setup = setup,
52 	.cleanup = cleanup,
53 };
54