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) International Business Machines Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker * 07/2001 Ported by Wayne Boyer
5*49cdfc7eSAndroid Build Coastguard Worker */
6*49cdfc7eSAndroid Build Coastguard Worker
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * Test whether the access mode are the same for both file descriptors.
11*49cdfc7eSAndroid Build Coastguard Worker *
12*49cdfc7eSAndroid Build Coastguard Worker * Create file with mode, dup2, [change mode], check mode
13*49cdfc7eSAndroid Build Coastguard Worker *
14*49cdfc7eSAndroid Build Coastguard Worker * - read only, dup2, read only ? "0444"
15*49cdfc7eSAndroid Build Coastguard Worker * - write only, dup2, write only ? "0222"
16*49cdfc7eSAndroid Build Coastguard Worker * - read/write, dup2 read/write ? "0666"
17*49cdfc7eSAndroid Build Coastguard Worker * - read/write/execute, dup2, set read only, read only ? "0444"
18*49cdfc7eSAndroid Build Coastguard Worker * - read/write/execute, dup2, set write only, write only ? "0222"
19*49cdfc7eSAndroid Build Coastguard Worker * - read/write/execute, dup2, set read/write, read/write ? "0666"
20*49cdfc7eSAndroid Build Coastguard Worker */
21*49cdfc7eSAndroid Build Coastguard Worker
22*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
26*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_macros.h"
27*49cdfc7eSAndroid Build Coastguard Worker
28*49cdfc7eSAndroid Build Coastguard Worker static char testfile[40];
29*49cdfc7eSAndroid Build Coastguard Worker static int ofd = -1, nfd = -1;
30*49cdfc7eSAndroid Build Coastguard Worker
31*49cdfc7eSAndroid Build Coastguard Worker /* set these to a known index into our local file descriptor table */
32*49cdfc7eSAndroid Build Coastguard Worker static int duprdo, dupwro, duprdwr;
33*49cdfc7eSAndroid Build Coastguard Worker
34*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
35*49cdfc7eSAndroid Build Coastguard Worker int *nfd;
36*49cdfc7eSAndroid Build Coastguard Worker mode_t mode;
37*49cdfc7eSAndroid Build Coastguard Worker /* 0 - set mode before dup2, 1 - change mode after dup2 */
38*49cdfc7eSAndroid Build Coastguard Worker int flag;
39*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
40*49cdfc7eSAndroid Build Coastguard Worker {&duprdo, 0444, 0},
41*49cdfc7eSAndroid Build Coastguard Worker {&dupwro, 0222, 0},
42*49cdfc7eSAndroid Build Coastguard Worker {&duprdwr, 0666, 0},
43*49cdfc7eSAndroid Build Coastguard Worker {&duprdo, 0444, 1},
44*49cdfc7eSAndroid Build Coastguard Worker {&dupwro, 0222, 1},
45*49cdfc7eSAndroid Build Coastguard Worker {&duprdwr, 0666, 1},
46*49cdfc7eSAndroid Build Coastguard Worker };
47*49cdfc7eSAndroid Build Coastguard Worker
setup(void)48*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
49*49cdfc7eSAndroid Build Coastguard Worker {
50*49cdfc7eSAndroid Build Coastguard Worker int nextfd;
51*49cdfc7eSAndroid Build Coastguard Worker
52*49cdfc7eSAndroid Build Coastguard Worker umask(0);
53*49cdfc7eSAndroid Build Coastguard Worker sprintf(testfile, "dup202.%d", getpid());
54*49cdfc7eSAndroid Build Coastguard Worker
55*49cdfc7eSAndroid Build Coastguard Worker /* Pick up fds that are known not to collide with creat */
56*49cdfc7eSAndroid Build Coastguard Worker nextfd = SAFE_CREAT(testfile, 0777);
57*49cdfc7eSAndroid Build Coastguard Worker duprdo = SAFE_DUP(nextfd);
58*49cdfc7eSAndroid Build Coastguard Worker dupwro = SAFE_DUP(nextfd);
59*49cdfc7eSAndroid Build Coastguard Worker duprdwr = SAFE_DUP(nextfd);
60*49cdfc7eSAndroid Build Coastguard Worker /* SAFE_CLOSE will set fd to -1 */
61*49cdfc7eSAndroid Build Coastguard Worker close(duprdwr);
62*49cdfc7eSAndroid Build Coastguard Worker close(dupwro);
63*49cdfc7eSAndroid Build Coastguard Worker close(duprdo);
64*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(nextfd);
65*49cdfc7eSAndroid Build Coastguard Worker SAFE_UNLINK(testfile);
66*49cdfc7eSAndroid Build Coastguard Worker
67*49cdfc7eSAndroid Build Coastguard Worker }
68*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)69*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
70*49cdfc7eSAndroid Build Coastguard Worker {
71*49cdfc7eSAndroid Build Coastguard Worker close(ofd);
72*49cdfc7eSAndroid Build Coastguard Worker close(nfd);
73*49cdfc7eSAndroid Build Coastguard Worker }
74*49cdfc7eSAndroid Build Coastguard Worker
run(unsigned int i)75*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int i)
76*49cdfc7eSAndroid Build Coastguard Worker {
77*49cdfc7eSAndroid Build Coastguard Worker struct stat oldbuf, newbuf;
78*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = tcases + i;
79*49cdfc7eSAndroid Build Coastguard Worker
80*49cdfc7eSAndroid Build Coastguard Worker if (tc->flag)
81*49cdfc7eSAndroid Build Coastguard Worker ofd = SAFE_CREAT(testfile, 0777);
82*49cdfc7eSAndroid Build Coastguard Worker else
83*49cdfc7eSAndroid Build Coastguard Worker ofd = SAFE_CREAT(testfile, tc->mode);
84*49cdfc7eSAndroid Build Coastguard Worker nfd = *tc->nfd;
85*49cdfc7eSAndroid Build Coastguard Worker
86*49cdfc7eSAndroid Build Coastguard Worker TEST(dup2(ofd, nfd));
87*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET == -1) {
88*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "call failed unexpectedly");
89*49cdfc7eSAndroid Build Coastguard Worker goto free;
90*49cdfc7eSAndroid Build Coastguard Worker }
91*49cdfc7eSAndroid Build Coastguard Worker if (tc->flag) {
92*49cdfc7eSAndroid Build Coastguard Worker SAFE_CHMOD(testfile, tc->mode);
93*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "original mode 0777, new mode 0%o after chmod", tc->mode);
94*49cdfc7eSAndroid Build Coastguard Worker }
95*49cdfc7eSAndroid Build Coastguard Worker
96*49cdfc7eSAndroid Build Coastguard Worker SAFE_FSTAT(ofd, &oldbuf);
97*49cdfc7eSAndroid Build Coastguard Worker
98*49cdfc7eSAndroid Build Coastguard Worker SAFE_FSTAT(nfd, &newbuf);
99*49cdfc7eSAndroid Build Coastguard Worker
100*49cdfc7eSAndroid Build Coastguard Worker if (oldbuf.st_mode != newbuf.st_mode)
101*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "original(%o) and duped(%o) are not same mode",
102*49cdfc7eSAndroid Build Coastguard Worker oldbuf.st_mode, newbuf.st_mode);
103*49cdfc7eSAndroid Build Coastguard Worker else
104*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "original(%o) and duped(%o) are the same mode",
105*49cdfc7eSAndroid Build Coastguard Worker oldbuf.st_mode, newbuf.st_mode);
106*49cdfc7eSAndroid Build Coastguard Worker
107*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(nfd);
108*49cdfc7eSAndroid Build Coastguard Worker free:
109*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(ofd);
110*49cdfc7eSAndroid Build Coastguard Worker SAFE_UNLINK(testfile);
111*49cdfc7eSAndroid Build Coastguard Worker }
112*49cdfc7eSAndroid Build Coastguard Worker
113*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
114*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
115*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
116*49cdfc7eSAndroid Build Coastguard Worker .test = run,
117*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
118*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
119*49cdfc7eSAndroid Build Coastguard Worker };
120