blob: 73e4830572fe523bd332dda9067fa322c7ef1b20 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, 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 */
subrata_modakda124b92009-10-13 14:00:45 +000033/* $Id: dup03.c,v 1.5 2009/10/13 14:00:46 subrata_modak Exp $ */
plars865695b2001-08-27 22:15:12 +000034/**********************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000035 *
plars865695b2001-08-27 22:15:12 +000036 * OS Test - Silicon Graphics, Inc.
subrata_modak4bb656a2009-02-26 12:02:09 +000037 *
plars865695b2001-08-27 22:15:12 +000038 * TEST IDENTIFIER : dup03
subrata_modak4bb656a2009-02-26 12:02:09 +000039 *
plars865695b2001-08-27 22:15:12 +000040 * EXECUTED BY : anyone
subrata_modak4bb656a2009-02-26 12:02:09 +000041 *
plars865695b2001-08-27 22:15:12 +000042 * TEST TITLE : Negative test for dup(2) (too many fds)
subrata_modak4bb656a2009-02-26 12:02:09 +000043 *
plars865695b2001-08-27 22:15:12 +000044 * PARENT DOCUMENT : usctpl01
subrata_modak4bb656a2009-02-26 12:02:09 +000045 *
plars865695b2001-08-27 22:15:12 +000046 * TEST CASE TOTAL : 1
subrata_modak4bb656a2009-02-26 12:02:09 +000047 *
plars865695b2001-08-27 22:15:12 +000048 * WALL CLOCK TIME : 1
subrata_modak4bb656a2009-02-26 12:02:09 +000049 *
plars865695b2001-08-27 22:15:12 +000050 * CPU TYPES : ALL
subrata_modak4bb656a2009-02-26 12:02:09 +000051 *
plars865695b2001-08-27 22:15:12 +000052 * AUTHOR : Richard Logan
subrata_modak4bb656a2009-02-26 12:02:09 +000053 *
plars865695b2001-08-27 22:15:12 +000054 * CO-PILOT : William Roske
subrata_modak4bb656a2009-02-26 12:02:09 +000055 *
plars865695b2001-08-27 22:15:12 +000056 * DATE STARTED : 06/94
subrata_modak4bb656a2009-02-26 12:02:09 +000057 *
plars865695b2001-08-27 22:15:12 +000058 * INITIAL RELEASE : UNICOS 7.0
subrata_modak4bb656a2009-02-26 12:02:09 +000059 *
plars865695b2001-08-27 22:15:12 +000060 * TEST CASES
subrata_modak4bb656a2009-02-26 12:02:09 +000061 *
plars865695b2001-08-27 22:15:12 +000062 * 1.) dup(2) returns...(See Description)
subrata_modakbdbaec52009-02-26 12:14:51 +000063 *
plars865695b2001-08-27 22:15:12 +000064 * INPUT SPECIFICATIONS
65 * The standard options for system call tests are accepted.
66 * (See the parse_opts(3) man page).
subrata_modak4bb656a2009-02-26 12:02:09 +000067 *
plars865695b2001-08-27 22:15:12 +000068 * OUTPUT SPECIFICATIONS
subrata_modak56207ce2009-03-23 13:35:39 +000069 *$
plars865695b2001-08-27 22:15:12 +000070 * DURATION
71 * Terminates - with frequency and infinite modes.
subrata_modak4bb656a2009-02-26 12:02:09 +000072 *
plars865695b2001-08-27 22:15:12 +000073 * SIGNALS
74 * Uses SIGUSR1 to pause before test if option set.
75 * (See the parse_opts(3) man page).
76 *
77 * RESOURCES
78 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000079 *
plars865695b2001-08-27 22:15:12 +000080 * ENVIRONMENTAL NEEDS
81 * No run-time environmental needs.
subrata_modak4bb656a2009-02-26 12:02:09 +000082 *
plars865695b2001-08-27 22:15:12 +000083 * SPECIAL PROCEDURAL REQUIREMENTS
84 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000085 *
plars865695b2001-08-27 22:15:12 +000086 * INTERCASE DEPENDENCIES
87 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000088 *
plars865695b2001-08-27 22:15:12 +000089 * DETAILED DESCRIPTION
subrata_modak4bb656a2009-02-26 12:02:09 +000090 *
plars865695b2001-08-27 22:15:12 +000091 * Setup:
92 * Setup signal handling.
93 * Pause for SIGUSR1 if option specified.
subrata_modak4bb656a2009-02-26 12:02:09 +000094 *
plars865695b2001-08-27 22:15:12 +000095 * Test:
96 * Loop if the proper options are given.
97 * Execute system call
98 * Check return code, if system call failed (return=-1)
99 * Log the errno and Issue a FAIL message.
100 * Otherwise, Issue a PASS message.
subrata_modak4bb656a2009-02-26 12:02:09 +0000101 *
plars865695b2001-08-27 22:15:12 +0000102 * Cleanup:
103 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +0000104 *
105 *
plars865695b2001-08-27 22:15:12 +0000106 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
107
108#include <sys/types.h>
109#include <sys/fcntl.h>
110#include <errno.h>
111#include <string.h>
112#include <signal.h>
113#include <stdlib.h>
114#include "test.h"
115#include "usctest.h"
116
117void setup();
118void cleanup();
119
subrata_modak56207ce2009-03-23 13:35:39 +0000120char *TCID = "dup03"; /* Test program identifier. */
121int TST_TOTAL = 1; /* Total number of test cases. */
plars865695b2001-08-27 22:15:12 +0000122extern int Tst_count; /* Test Case counter for tst_* routines */
123
plars865695b2001-08-27 22:15:12 +0000124char Fname[255];
125int *Fd = NULL;
subrata_modak56207ce2009-03-23 13:35:39 +0000126int Nfds = 0;
plars865695b2001-08-27 22:15:12 +0000127
128/***********************************************************************
129 * Main
130 ***********************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000131int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000132{
subrata_modak56207ce2009-03-23 13:35:39 +0000133 int lc; /* loop counter */
134 char *msg; /* message returned from parse_opts */
135
plars865695b2001-08-27 22:15:12 +0000136 /***************************************************************
137 * parse standard options
138 ***************************************************************/
Garrett Coopere1f008e2010-12-14 00:21:59 -0800139 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
subrata_modak56207ce2009-03-23 13:35:39 +0000140 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
141 tst_exit();
142 }
plars865695b2001-08-27 22:15:12 +0000143
144 /***************************************************************
145 * perform global setup for test
146 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000147 setup();
plars865695b2001-08-27 22:15:12 +0000148
149 /***************************************************************
150 * check looping state if -c option given
151 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000152 for (lc = 0; TEST_LOOPING(lc); lc++) {
plars865695b2001-08-27 22:15:12 +0000153
subrata_modak56207ce2009-03-23 13:35:39 +0000154 /* reset Tst_count in case we are looping. */
155 Tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000156
subrata_modak56207ce2009-03-23 13:35:39 +0000157 /*
158 * Call dup(2)
159 */
160 TEST(dup(Fd[0]));
subrata_modakbdbaec52009-02-26 12:14:51 +0000161
subrata_modak56207ce2009-03-23 13:35:39 +0000162 /* check return code */
163 if (TEST_RETURN == -1) {
164 if (STD_FUNCTIONAL_TEST) {
165 if (TEST_ERRNO == EMFILE) {
166 tst_resm(TPASS,
167 "dup(%d) Failed, errno=%d : %s",
168 Fd[0], TEST_ERRNO,
169 strerror(TEST_ERRNO));
170 } else {
171 tst_resm(TFAIL,
172 "dup(%d) Failed, errno=%d %s, expected %d (EMFILE)",
173 Fd[0], TEST_ERRNO,
174 strerror(TEST_ERRNO), EMFILE);
175 }
176 }
177 } else {
178 tst_resm(TFAIL,
subrata_modakda124b92009-10-13 14:00:45 +0000179 "dup(%d) returned %ld, expected -1, errno:%d (EMFILE)",
subrata_modak56207ce2009-03-23 13:35:39 +0000180 Fd[0], TEST_RETURN, EMFILE);
181
182 /* close the new file so loops do not open too many files */
183 if (close(TEST_RETURN) == -1) {
184 tst_brkm(TBROK, cleanup,
185 "close(%s) Failed, errno=%d : %s",
186 Fname, errno, strerror(errno));
187 }
plars865695b2001-08-27 22:15:12 +0000188 }
plars865695b2001-08-27 22:15:12 +0000189
subrata_modak56207ce2009-03-23 13:35:39 +0000190 } /* End for TEST_LOOPING */
plars865695b2001-08-27 22:15:12 +0000191
192 /***************************************************************
193 * cleanup and exit
194 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000195 cleanup();
plars865695b2001-08-27 22:15:12 +0000196
subrata_modak56207ce2009-03-23 13:35:39 +0000197 return 0;
198} /* End main */
plars865695b2001-08-27 22:15:12 +0000199
200/***************************************************************
201 * setup() - performs all ONE TIME setup for this test.
202 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000203void setup()
plars865695b2001-08-27 22:15:12 +0000204{
subrata_modak56207ce2009-03-23 13:35:39 +0000205 long maxfds;
plars865695b2001-08-27 22:15:12 +0000206
subrata_modak56207ce2009-03-23 13:35:39 +0000207 /*
208 * Initialize Fd in case we get a quick signal
209 */
210 maxfds = sysconf(_SC_OPEN_MAX);
211 if (maxfds < 1) {
212 tst_brkm(TBROK, cleanup,
213 "sysconf(_SC_OPEN_MAX) Failed, errno=%d : %s",
214 errno, strerror(errno));
plars865695b2001-08-27 22:15:12 +0000215 }
plars865695b2001-08-27 22:15:12 +0000216
subrata_modak56207ce2009-03-23 13:35:39 +0000217 Fd = (int *)malloc(maxfds * sizeof(int));
218 Fd[0] = -1;
plars865695b2001-08-27 22:15:12 +0000219
subrata_modak56207ce2009-03-23 13:35:39 +0000220 /* capture signals */
221 tst_sig(FORK, DEF_HANDLER, cleanup);
222
223 /* Pause if that option was specified */
224 TEST_PAUSE;
225
226 /* make a temp directory and cd to it */
227 tst_tmpdir();
228
229 /*
230 * open the file as many times as it takes to use up all fds
231 */
232 sprintf(Fname, "dupfile");
233 for (Nfds = 1; Nfds <= maxfds; Nfds++) {
234 if ((Fd[Nfds - 1] = open(Fname, O_RDWR | O_CREAT, 0700)) == -1) {
235
236 Nfds--; /* on a open failure, decrement the counter */
237 if (errno == EMFILE) {
238 break;
239 } else { /* open failed for some other reason */
240 tst_brkm(TBROK, cleanup,
241 "open(%s, O_RDWR|O_CREAT,0700) Failed, errno=%d : %s",
242 Fname, errno, strerror(errno));
243 }
244 }
245 }
246
247 /*
248 * make sure at least one was open and that all fds were opened.
249 */
250 if (Nfds == 0) {
251 tst_brkm(TBROK, cleanup, "Unable to open at least one file");
252 }
253 if (Nfds > maxfds) {
254 tst_brkm(TBROK, cleanup,
subrata_modakda124b92009-10-13 14:00:45 +0000255 "Unable to open enough files to use all file descriptors, tried %ld",
subrata_modak56207ce2009-03-23 13:35:39 +0000256 maxfds);
257 }
258} /* End setup() */
plars865695b2001-08-27 22:15:12 +0000259
260/***************************************************************
261 * cleanup() - performs all ONE TIME cleanup for this test at
262 * completion or premature exit.
263 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000264void cleanup()
plars865695b2001-08-27 22:15:12 +0000265{
subrata_modak56207ce2009-03-23 13:35:39 +0000266 /*
267 * print timing stats if that option was specified.
268 * print errno log if that option was specified.
269 */
270 TEST_CLEANUP;
plars865695b2001-08-27 22:15:12 +0000271
subrata_modak56207ce2009-03-23 13:35:39 +0000272 /* close the open file we've been dup'ing */
273 if (Fd) {
274 for (; Nfds > 0; Nfds--) {
275 if (close(Fd[Nfds - 1]) == -1) {
276 tst_resm(TWARN,
277 "close(%s) Failed, errno=%d : %s",
278 Fname, errno, strerror(errno));
279 }
280 Fd[Nfds] = -1;
281 }
282 free(Fd);
plars865695b2001-08-27 22:15:12 +0000283 }
plars865695b2001-08-27 22:15:12 +0000284
subrata_modak56207ce2009-03-23 13:35:39 +0000285 /* Remove tmp dir and all files in it */
286 tst_rmdir();
plars865695b2001-08-27 22:15:12 +0000287
subrata_modak56207ce2009-03-23 13:35:39 +0000288 /* exit with return code appropriate for results */
289 tst_exit();
290} /* End cleanup() */