1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2002
4 * Ported from SPIE, section2/iosuite/dup3.c, by Airong Zhang
5 * Copyright (c) 2013 Cyril Hrubis <[email protected]>
6 * Copyright (c) Linux Test Project, 2006-2024
7 */
8
9 /*\
10 * [Description]
11 *
12 * Verify that the file descriptor created by dup(2) syscall has the same
13 * access mode as the old one.
14 */
15
16 #include "tst_test.h"
17
18 static const char *testfile = "dup07";
19
20 static struct tcase {
21 char *mode_desc;
22 int mode;
23 } tcases[] = {
24 {"read only", 0444},
25 {"write only", 0222},
26 {"read/write", 0666},
27 };
28
run(unsigned int n)29 static void run(unsigned int n)
30 {
31 int oldfd, dupfd;
32 struct stat oldbuf, dupbuf;
33 struct tcase *tc = &tcases[n];
34
35 oldfd = SAFE_CREAT(testfile, tc->mode);
36 dupfd = TST_EXP_FD_SILENT(dup(oldfd), "dup() %s file", tc->mode_desc);
37 if (TST_PASS) {
38 SAFE_FSTAT(oldfd, &oldbuf);
39 SAFE_FSTAT(dupfd, &dupbuf);
40
41 if (oldbuf.st_mode != dupbuf.st_mode)
42 tst_res(TFAIL, "%s and dup do not match", tc->mode_desc);
43 else
44 tst_res(TPASS, "Passed in %s mode", tc->mode_desc);
45
46 SAFE_CLOSE(dupfd);
47 }
48
49 SAFE_CLOSE(oldfd);
50 SAFE_UNLINK(testfile);
51 }
52
53 static struct tst_test test = {
54 .needs_tmpdir = 1,
55 .test = run,
56 .tcnt = ARRAY_SIZE(tcases),
57 };
58