1 /*
2 * 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/
31 *
32 */
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).
73 *
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 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
104
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 #include <stdlib.h>
114 #include "test.h"
115
116 void setup();
117 void cleanup();
118
119 char *TCID = "dup03";
120 int TST_TOTAL = 1;
121
122 char filename[255];
123 int *fd = NULL;
124 int nfds = 0;
125
main(int ac,char ** av)126 int main(int ac, char **av)
127 {
128 int lc;
129
130 tst_parse_opts(ac, av, NULL, NULL);
131
132 setup();
133
134 for (lc = 0; TEST_LOOPING(lc); lc++) {
135
136 tst_count = 0;
137
138 TEST(dup(fd[0]));
139
140 if (TEST_RETURN == -1) {
141 if (TEST_ERRNO == EMFILE)
142 tst_resm(TPASS,
143 "dup failed as expected with "
144 "EMFILE");
145 else
146 tst_resm(TFAIL | TTERRNO,
147 "dup failed unexpectedly");
148 } else {
149 tst_resm(TFAIL, "dup succeeded unexpectedly");
150
151 if (close(TEST_RETURN) == -1)
152 tst_brkm(TBROK | TERRNO, cleanup,
153 "close failed");
154 }
155
156 }
157
158 cleanup();
159 tst_exit();
160 }
161
setup(void)162 void setup(void)
163 {
164 long maxfds;
165
166 maxfds = sysconf(_SC_OPEN_MAX);
167 /*
168 * Read the errors section if you're so inclined to determine
169 * why == -1 matters for errno.
170 */
171 if (maxfds < 1)
172 tst_brkm((maxfds == -1 ? TBROK | TERRNO : TBROK), NULL,
173 "sysconf(_SC_OPEN_MAX) failed");
174
175 fd = malloc(maxfds * sizeof(int));
176 if (fd == NULL)
177 tst_brkm(TBROK | TERRNO, NULL, "malloc failed");
178 fd[0] = -1;
179
180 tst_sig(FORK, DEF_HANDLER, cleanup);
181
182 TEST_PAUSE;
183
184 tst_tmpdir();
185
186 sprintf(filename, "dupfile");
187 for (nfds = 1; nfds <= maxfds; nfds++)
188 if ((fd[nfds - 1] =
189 open(filename, O_RDWR | O_CREAT, 0700)) == -1) {
190 if (errno == EMFILE)
191 break;
192 else
193 tst_brkm(TBROK | TBROK, cleanup, "open failed");
194 nfds--;
195 }
196
197 if (nfds == 0)
198 tst_brkm(TBROK, cleanup, "unable to open at least one file");
199 if (nfds > maxfds)
200 tst_brkm(TBROK, cleanup,
201 "unable to open enough files to use all file descriptors, "
202 "tried %ld", maxfds);
203 }
204
cleanup(void)205 void cleanup(void)
206 {
207 int i;
208
209 for (i = 0; i <= nfds; i++)
210 close(fd[i]);
211
212 tst_rmdir();
213 }
214