|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
1 | 2 | /* |
2 | 3 | * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. |
3 | | - * |
4 | | - * This program is free software; you can redistribute it and/or modify it |
5 | | - * under the terms of version 2 of the GNU General Public License as |
6 | | - * published by the Free Software Foundation. |
7 | | - * |
8 | | - * This program is distributed in the hope that it would be useful, but |
9 | | - * WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
11 | | - * |
12 | | - * Further, this software is distributed without any warranty that it is |
13 | | - * free of the rightful claim of any third person regarding infringement |
14 | | - * or the like. Any license provided herein, whether implied or |
15 | | - * otherwise, applies only to this software file. Patent licenses, if |
16 | | - * any, provided herein do not apply to combinations of this program with |
17 | | - * other software, or any other product whatsoever. |
18 | | - * |
19 | | - * You should have received a copy of the GNU General Public License along |
20 | | - * with this program; if not, write the Free Software Foundation, Inc., |
21 | | - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
22 | | - * |
23 | | - * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
24 | | - * Mountain View, CA 94043, or: |
25 | | - * |
26 | | - * http://www.sgi.com |
27 | | - * |
28 | | - * For further information regarding this notice, see: |
29 | | - * |
30 | | - * http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 4 | + * Copyright (c) 2020 SUSE LLC |
31 | 5 | * |
32 | 6 | */ |
33 | | -/* $Id: dup03.c,v 1.5 2009/10/13 14:00:46 subrata_modak Exp $ */ |
34 | | -/********************************************************** |
35 | | - * |
36 | | - * OS Test - Silicon Graphics, Inc. |
37 | | - * |
38 | | - * TEST IDENTIFIER : dup03 |
39 | | - * |
40 | | - * EXECUTED BY : anyone |
41 | | - * |
42 | | - * TEST TITLE : Negative test for dup(2) (too many fds) |
43 | | - * |
44 | | - * PARENT DOCUMENT : usctpl01 |
45 | | - * |
46 | | - * TEST CASE TOTAL : 1 |
47 | | - * |
48 | | - * WALL CLOCK TIME : 1 |
49 | | - * |
50 | | - * CPU TYPES : ALL |
51 | | - * |
52 | | - * |
53 | | - * DATE STARTED : 06/94 |
54 | | - * |
55 | | - * INITIAL RELEASE : UNICOS 7.0 |
56 | | - * |
57 | | - * TEST CASES |
58 | | - * |
59 | | - * 1.) dup(2) returns...(See Description) |
60 | | - * |
61 | | - * INPUT SPECIFICATIONS |
62 | | - * The standard options for system call tests are accepted. |
63 | | - * (See the parse_opts(3) man page). |
64 | | - * |
65 | | - * OUTPUT SPECIFICATIONS |
66 | | - *$ |
67 | | - * DURATION |
68 | | - * Terminates - with frequency and infinite modes. |
69 | | - * |
70 | | - * SIGNALS |
71 | | - * Uses SIGUSR1 to pause before test if option set. |
72 | | - * (See the parse_opts(3) man page). |
| 7 | +/*\ |
| 8 | + * [DESCRIPTION] |
| 9 | + * Negative test for dup(2) (too many fds). |
73 | 10 | * |
74 | | - * RESOURCES |
75 | | - * None |
76 | | - * |
77 | | - * ENVIRONMENTAL NEEDS |
78 | | - * No run-time environmental needs. |
79 | | - * |
80 | | - * SPECIAL PROCEDURAL REQUIREMENTS |
81 | | - * None |
82 | | - * |
83 | | - * INTERCASE DEPENDENCIES |
84 | | - * None |
85 | | - * |
86 | | - * DETAILED DESCRIPTION |
87 | | - * |
88 | | - * Setup: |
89 | | - * Setup signal handling. |
90 | | - * Pause for SIGUSR1 if option specified. |
91 | | - * |
92 | | - * Test: |
93 | | - * Loop if the proper options are given. |
94 | | - * Execute system call |
95 | | - * Check return code, if system call failed (return=-1) |
96 | | - * Log the errno and Issue a FAIL message. |
97 | | - * Otherwise, Issue a PASS message. |
98 | | - * |
99 | | - * Cleanup: |
100 | | - * Print errno log and/or timing stats if options given |
101 | | - * |
102 | | - * |
103 | | - *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/ |
| 11 | + * [ALGORITHM] |
| 12 | + * Open the maximum allowed number of file descriptors and then try to call |
| 13 | + * dup() once more and verify it fails with EMFILE. |
| 14 | +\*/ |
104 | 15 |
|
105 | | -#ifndef _GNU_SOURCE |
106 | | -#define _GNU_SOURCE |
107 | | -#endif |
108 | | -#include <sys/types.h> |
109 | | -#include <fcntl.h> |
110 | | -#include <errno.h> |
111 | | -#include <string.h> |
112 | | -#include <signal.h> |
113 | 16 | #include <stdlib.h> |
114 | | -#include "test.h" |
115 | | -#include "safe_macros.h" |
| 17 | +#include "tst_test.h" |
116 | 18 |
|
117 | | -void setup(); |
118 | | -void cleanup(); |
| 19 | +int *fd; |
| 20 | +int nfds; |
119 | 21 |
|
120 | | -char *TCID = "dup03"; |
121 | | -int TST_TOTAL = 1; |
122 | | - |
123 | | -char filename[255]; |
124 | | -int *fd = NULL; |
125 | | -int nfds = 0; |
126 | | - |
127 | | -int main(int ac, char **av) |
| 22 | +static void run(void) |
128 | 23 | { |
129 | | - int lc; |
130 | | - |
131 | | - tst_parse_opts(ac, av, NULL, NULL); |
| 24 | + TEST(dup(fd[0])); |
132 | 25 |
|
133 | | - setup(); |
134 | | - |
135 | | - for (lc = 0; TEST_LOOPING(lc); lc++) { |
136 | | - |
137 | | - tst_count = 0; |
138 | | - |
139 | | - TEST(dup(fd[0])); |
140 | | - |
141 | | - if (TEST_RETURN == -1) { |
142 | | - if (TEST_ERRNO == EMFILE) |
143 | | - tst_resm(TPASS, |
144 | | - "dup failed as expected with " |
145 | | - "EMFILE"); |
146 | | - else |
147 | | - tst_resm(TFAIL | TTERRNO, |
148 | | - "dup failed unexpectedly"); |
149 | | - } else { |
150 | | - tst_resm(TFAIL, "dup succeeded unexpectedly"); |
151 | | - |
152 | | - SAFE_CLOSE(cleanup, TEST_RETURN); |
153 | | - } |
| 26 | + if (TST_RET < -1) { |
| 27 | + tst_res(TFAIL, "Invalid dup() return value %ld", TST_RET); |
| 28 | + return; |
| 29 | + } |
154 | 30 |
|
| 31 | + if (TST_RET == -1) { |
| 32 | + if (TST_ERR == EMFILE) |
| 33 | + tst_res(TPASS | TERRNO, "dup() failed as expected"); |
| 34 | + else |
| 35 | + tst_res(TFAIL | TERRNO, "dup() failed unexpectedly"); |
| 36 | + return; |
155 | 37 | } |
156 | 38 |
|
157 | | - cleanup(); |
158 | | - tst_exit(); |
| 39 | + tst_res(TFAIL, "dup() succeeded unexpectedly"); |
| 40 | + SAFE_CLOSE(TST_RET); |
159 | 41 | } |
160 | 42 |
|
161 | | -void setup(void) |
| 43 | +static void setup(void) |
162 | 44 | { |
163 | 45 | long maxfds; |
164 | 46 |
|
165 | 47 | maxfds = sysconf(_SC_OPEN_MAX); |
166 | | - /* |
167 | | - * Read the errors section if you're so inclined to determine |
168 | | - * why == -1 matters for errno. |
169 | | - */ |
170 | | - if (maxfds < 1) |
171 | | - tst_brkm((maxfds == -1 ? TBROK | TERRNO : TBROK), NULL, |
172 | | - "sysconf(_SC_OPEN_MAX) failed"); |
| 48 | + if (maxfds == -1) |
| 49 | + tst_brk(TBROK, "sysconf(_SC_OPEN_MAX) failed"); |
173 | 50 |
|
174 | | - fd = malloc(maxfds * sizeof(int)); |
175 | | - if (fd == NULL) |
176 | | - tst_brkm(TBROK | TERRNO, NULL, "malloc failed"); |
177 | | - fd[0] = -1; |
| 51 | + fd = SAFE_MALLOC(maxfds * sizeof(int)); |
178 | 52 |
|
179 | | - tst_sig(FORK, DEF_HANDLER, cleanup); |
| 53 | + fd[0] = SAFE_OPEN("dupfile", O_RDWR | O_CREAT, 0700); |
180 | 54 |
|
181 | | - TEST_PAUSE; |
182 | | - |
183 | | - tst_tmpdir(); |
184 | | - |
185 | | - sprintf(filename, "dupfile"); |
186 | | - for (nfds = 1; nfds <= maxfds; nfds++) |
187 | | - if ((fd[nfds - 1] = |
188 | | - open(filename, O_RDWR | O_CREAT, 0700)) == -1) { |
189 | | - if (errno == EMFILE) |
190 | | - break; |
191 | | - else |
192 | | - tst_brkm(TBROK | TBROK, cleanup, "open failed"); |
193 | | - nfds--; |
194 | | - } |
195 | | - |
196 | | - if (nfds == 0) |
197 | | - tst_brkm(TBROK, cleanup, "unable to open at least one file"); |
198 | | - if (nfds > maxfds) |
199 | | - tst_brkm(TBROK, cleanup, |
200 | | - "unable to open enough files to use all file descriptors, " |
201 | | - "tried %ld", maxfds); |
| 55 | + for (nfds = 1; nfds < maxfds; nfds++) { |
| 56 | + fd[nfds] = SAFE_DUP(fd[0]); |
| 57 | + if (fd[nfds] >= maxfds - 1) |
| 58 | + break; |
| 59 | + } |
202 | 60 | } |
203 | 61 |
|
204 | | -void cleanup(void) |
| 62 | +static void cleanup(void) |
205 | 63 | { |
206 | 64 | int i; |
207 | 65 |
|
208 | | - for (i = 0; i <= nfds; i++) |
209 | | - close(fd[i]); |
210 | | - |
211 | | - tst_rmdir(); |
| 66 | + for (i = 0; i < nfds; i++) |
| 67 | + SAFE_CLOSE(fd[i]); |
212 | 68 | } |
| 69 | + |
| 70 | +static struct tst_test test = { |
| 71 | + .test_all = run, |
| 72 | + .setup = setup, |
| 73 | + .cleanup = cleanup, |
| 74 | + .needs_tmpdir = 1, |
| 75 | +}; |
0 commit comments