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