blob: a9b61e340d62571a35921f69b5c891985592bdc4 [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
Wanlong Gaofed96412012-10-24 10:10:29 +080020 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
plars865695b2001-08-27 22:15:12 +000022 *
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 *
subrata_modak4bb656a2009-02-26 12:02:09 +000052 *
plars865695b2001-08-27 22:15:12 +000053 * DATE STARTED : 06/94
subrata_modak4bb656a2009-02-26 12:02:09 +000054 *
plars865695b2001-08-27 22:15:12 +000055 * INITIAL RELEASE : UNICOS 7.0
subrata_modak4bb656a2009-02-26 12:02:09 +000056 *
plars865695b2001-08-27 22:15:12 +000057 * TEST CASES
subrata_modak4bb656a2009-02-26 12:02:09 +000058 *
plars865695b2001-08-27 22:15:12 +000059 * 1.) dup(2) returns...(See Description)
subrata_modakbdbaec52009-02-26 12:14:51 +000060 *
plars865695b2001-08-27 22:15:12 +000061 * INPUT SPECIFICATIONS
62 * The standard options for system call tests are accepted.
63 * (See the parse_opts(3) man page).
subrata_modak4bb656a2009-02-26 12:02:09 +000064 *
plars865695b2001-08-27 22:15:12 +000065 * OUTPUT SPECIFICATIONS
subrata_modak56207ce2009-03-23 13:35:39 +000066 *$
plars865695b2001-08-27 22:15:12 +000067 * DURATION
68 * Terminates - with frequency and infinite modes.
subrata_modak4bb656a2009-02-26 12:02:09 +000069 *
plars865695b2001-08-27 22:15:12 +000070 * SIGNALS
71 * Uses SIGUSR1 to pause before test if option set.
72 * (See the parse_opts(3) man page).
73 *
74 * RESOURCES
75 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000076 *
plars865695b2001-08-27 22:15:12 +000077 * ENVIRONMENTAL NEEDS
78 * No run-time environmental needs.
subrata_modak4bb656a2009-02-26 12:02:09 +000079 *
plars865695b2001-08-27 22:15:12 +000080 * SPECIAL PROCEDURAL REQUIREMENTS
81 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000082 *
plars865695b2001-08-27 22:15:12 +000083 * INTERCASE DEPENDENCIES
84 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000085 *
plars865695b2001-08-27 22:15:12 +000086 * DETAILED DESCRIPTION
subrata_modak4bb656a2009-02-26 12:02:09 +000087 *
plars865695b2001-08-27 22:15:12 +000088 * Setup:
89 * Setup signal handling.
90 * Pause for SIGUSR1 if option specified.
subrata_modak4bb656a2009-02-26 12:02:09 +000091 *
plars865695b2001-08-27 22:15:12 +000092 * 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.
subrata_modak4bb656a2009-02-26 12:02:09 +000098 *
plars865695b2001-08-27 22:15:12 +000099 * Cleanup:
100 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +0000101 *
102 *
plars865695b2001-08-27 22:15:12 +0000103 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
104
Garrett Cooper15697992010-12-18 06:31:28 -0800105#ifndef _GNU_SOURCE
106#define _GNU_SOURCE
107#endif
plars865695b2001-08-27 22:15:12 +0000108#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"
plars865695b2001-08-27 22:15:12 +0000115
116void setup();
117void cleanup();
118
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200119char *TCID = "dup03";
120int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +0000121
Garrett Cooper83599b92010-12-18 10:29:05 -0800122char filename[255];
Garrett Cooper15697992010-12-18 06:31:28 -0800123int *fd = NULL;
124int nfds = 0;
plars865695b2001-08-27 22:15:12 +0000125
subrata_modak56207ce2009-03-23 13:35:39 +0000126int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000127{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200128 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200129 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +0000130
Garrett Cooper15697992010-12-18 06:31:28 -0800131 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
subrata_modak56207ce2009-03-23 13:35:39 +0000132 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800133
subrata_modak56207ce2009-03-23 13:35:39 +0000134 setup();
plars865695b2001-08-27 22:15:12 +0000135
subrata_modak56207ce2009-03-23 13:35:39 +0000136 for (lc = 0; TEST_LOOPING(lc); lc++) {
plars865695b2001-08-27 22:15:12 +0000137
Caspar Zhangd59a6592013-03-07 14:59:12 +0800138 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000139
Garrett Cooper15697992010-12-18 06:31:28 -0800140 TEST(dup(fd[0]));
subrata_modakbdbaec52009-02-26 12:14:51 +0000141
subrata_modak56207ce2009-03-23 13:35:39 +0000142 if (TEST_RETURN == -1) {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200143 if (TEST_ERRNO == EMFILE)
144 tst_resm(TPASS,
145 "dup failed as expected with "
146 "EMFILE");
147 else
148 tst_resm(TFAIL | TTERRNO,
149 "dup failed unexpectedly");
subrata_modak56207ce2009-03-23 13:35:39 +0000150 } else {
Garrett Cooper15697992010-12-18 06:31:28 -0800151 tst_resm(TFAIL, "dup succeeded unexpectedly");
subrata_modak56207ce2009-03-23 13:35:39 +0000152
Garrett Cooper15697992010-12-18 06:31:28 -0800153 if (close(TEST_RETURN) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800154 tst_brkm(TBROK | TERRNO, cleanup,
155 "close failed");
plars865695b2001-08-27 22:15:12 +0000156 }
plars865695b2001-08-27 22:15:12 +0000157
Garrett Cooper2c282152010-12-16 00:55:50 -0800158 }
plars865695b2001-08-27 22:15:12 +0000159
subrata_modak56207ce2009-03-23 13:35:39 +0000160 cleanup();
Garrett Cooper15697992010-12-18 06:31:28 -0800161 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800162}
plars865695b2001-08-27 22:15:12 +0000163
Mike Frysingerc57fba52014-04-09 18:56:30 -0400164void setup(void)
plars865695b2001-08-27 22:15:12 +0000165{
subrata_modak56207ce2009-03-23 13:35:39 +0000166 long maxfds;
plars865695b2001-08-27 22:15:12 +0000167
subrata_modak56207ce2009-03-23 13:35:39 +0000168 maxfds = sysconf(_SC_OPEN_MAX);
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800169 /*
Garrett Cooper15697992010-12-18 06:31:28 -0800170 * Read the errors section if you're so inclined to determine
171 * why == -1 matters for errno.
172 */
173 if (maxfds < 1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800174 tst_brkm((maxfds == -1 ? TBROK | TERRNO : TBROK), NULL,
175 "sysconf(_SC_OPEN_MAX) failed");
plars865695b2001-08-27 22:15:12 +0000176
Garrett Cooper15697992010-12-18 06:31:28 -0800177 fd = malloc(maxfds * sizeof(int));
Garrett Cooper83599b92010-12-18 10:29:05 -0800178 if (fd == NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800179 tst_brkm(TBROK | TERRNO, NULL, "malloc failed");
Garrett Cooper15697992010-12-18 06:31:28 -0800180 fd[0] = -1;
plars865695b2001-08-27 22:15:12 +0000181
subrata_modak56207ce2009-03-23 13:35:39 +0000182 tst_sig(FORK, DEF_HANDLER, cleanup);
183
subrata_modak56207ce2009-03-23 13:35:39 +0000184 TEST_PAUSE;
185
subrata_modak56207ce2009-03-23 13:35:39 +0000186 tst_tmpdir();
187
Garrett Cooper83599b92010-12-18 10:29:05 -0800188 sprintf(filename, "dupfile");
189 for (nfds = 1; nfds <= maxfds; nfds++)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800190 if ((fd[nfds - 1] =
191 open(filename, O_RDWR | O_CREAT, 0700)) == -1) {
Garrett Cooper83599b92010-12-18 10:29:05 -0800192 if (errno == EMFILE)
subrata_modak56207ce2009-03-23 13:35:39 +0000193 break;
Garrett Cooper83599b92010-12-18 10:29:05 -0800194 else
Wanlong Gao354ebb42012-12-07 10:10:04 +0800195 tst_brkm(TBROK | TBROK, cleanup, "open failed");
Garrett Cooper83599b92010-12-18 10:29:05 -0800196 nfds--;
subrata_modak56207ce2009-03-23 13:35:39 +0000197 }
subrata_modak56207ce2009-03-23 13:35:39 +0000198
Garrett Cooper83599b92010-12-18 10:29:05 -0800199 if (nfds == 0)
200 tst_brkm(TBROK, cleanup, "unable to open at least one file");
201 if (nfds > maxfds)
subrata_modak56207ce2009-03-23 13:35:39 +0000202 tst_brkm(TBROK, cleanup,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800203 "unable to open enough files to use all file descriptors, "
204 "tried %ld", maxfds);
Garrett Cooper2c282152010-12-16 00:55:50 -0800205}
plars865695b2001-08-27 22:15:12 +0000206
Mike Frysingerc57fba52014-04-09 18:56:30 -0400207void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000208{
Garrett Cooper83599b92010-12-18 10:29:05 -0800209 int i;
210
Eryu Guana009d082011-11-11 12:52:34 +0800211 for (i = 0; i <= nfds; i++)
Garrett Cooper83599b92010-12-18 10:29:05 -0800212 close(fd[i]);
plars865695b2001-08-27 22:15:12 +0000213
subrata_modak56207ce2009-03-23 13:35:39 +0000214 tst_rmdir();
Eryu Guana009d082011-11-11 12:52:34 +0800215}