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 <chrubis@suse.cz>
5 */
6 /*
7 * Basic test for fcntl(2) using F_DUPFD 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 int fd;
20 static char fname[256];
21
22 static const int min_fds[] = {0, 1, 2, 3, 10, 100};
23
verify_fcntl(unsigned int n)24 static void verify_fcntl(unsigned int n)
25 {
26 int min_fd = min_fds[n];
27
28 TEST(fcntl(fd, F_DUPFD, min_fd));
29
30 if (TST_RET == -1) {
31 tst_res(TFAIL | TTERRNO, "fcntl(%s, F_DUPFD, %i) failed",
32 fname, min_fd);
33 return;
34 }
35
36 if (TST_RET < min_fd) {
37 tst_res(TFAIL, "fcntl(%s, F_DUPFD, %i) returned %ld < %i",
38 fname, min_fd, TST_RET, min_fd);
39 }
40
41 tst_res(TPASS, "fcntl(%s, F_DUPFD, %i) returned %ld",
42 fname, min_fd, TST_RET);
43
44 SAFE_CLOSE(TST_RET);
45 }
46
setup(void)47 static void setup(void)
48 {
49 sprintf(fname, "fcntl02_%d", getpid());
50 fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0700);
51 }
52
cleanup(void)53 static void cleanup(void)
54 {
55 if (fd > 0)
56 SAFE_CLOSE(fd);
57 }
58
59 static struct tst_test test = {
60 .needs_tmpdir = 1,
61 .test = verify_fcntl,
62 .tcnt = ARRAY_SIZE(min_fds),
63 .setup = setup,
64 .cleanup = cleanup,
65 };
66