blob: c7748701aa659f685f9133ddf96ee8ee3e228bcd [file] [log] [blame]
robbiew906d4682003-01-06 17:23:07 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2002
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiew906d4682003-01-06 17:23:07 +000018 */
19
20/*
21 * NAME
22 * pipe07.c
23 *
24 * DESCRIPTION
25 * Test the ability of pipe to open the maximum even number of file
26 * descriptors permitted (or (maxfds - 3)/2 pipes)
27 *
28 * ALGORITHM
29 * 1. open pipes until EMFILE is returned
30 * 2. check to see that the number of pipes opened is
31 * (maxfds - 3) / 2
32 *
33 * USAGE: <for command-line>
34 * pipe07 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
35 * where, -c n : Run n copies concurrently.
36 * -e : Turn on errno logging.
37 * -i n : Execute test n times.
38 * -I x : Execute test for x seconds.
39 * -P x : Pause for x seconds between iterations.
40 * -t : Turn on syscall timing.
41 *
42 * HISTORY
43 * 12/2002 Ported by Paul Larson
44 *
45 * RESTRICTIONS
46 * None
47 */
robbiew6617b472003-12-15 17:08:10 +000048#include <unistd.h>
robbiew906d4682003-01-06 17:23:07 +000049#include <fcntl.h>
50#include <errno.h>
robbiew86389102003-12-08 20:39:34 +000051#include <stdlib.h>
52#include <stdio.h>
robbiew906d4682003-01-06 17:23:07 +000053#include "test.h"
54#include "usctest.h"
55
56char *TCID = "pipe07";
57int TST_TOTAL = 1;
robbiew906d4682003-01-06 17:23:07 +000058
subrata_modak56207ce2009-03-23 13:35:39 +000059int exp_enos[] = { EMFILE, 0 };
robbiew906d4682003-01-06 17:23:07 +000060
61int pipe_ret, pipes[2];
robbiew6617b472003-12-15 17:08:10 +000062char currdir[PATH_MAX];
subrata_modak56207ce2009-03-23 13:35:39 +000063char *tempdir = NULL;
robbiew906d4682003-01-06 17:23:07 +000064void setup(void);
65void cleanup(void);
66
67int main(int ac, char **av)
68{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020069 int lc;
70 char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +000071 int min; /* number of file descriptors */
72 int usedfds; /* number of currently used file descriptors */
73 int npipes; /* number of pipes opened */
74 pid_t mypid; /* process of id of test */
75 char *cmdstring = NULL; /* hold command string to execute using system() */
76 FILE *f; /* used for retrieving the used fds */
robbiew906d4682003-01-06 17:23:07 +000077
Garrett Cooper53740502010-12-16 00:04:01 -080078 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080079 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew906d4682003-01-06 17:23:07 +000080 setup();
vapier5ef7d2f2006-05-26 05:58:39 +000081 /* Get the currently used number of file descriptors */
subrata_modak56207ce2009-03-23 13:35:39 +000082 mypid = getpid();
83 cmdstring = malloc(BUFSIZ);
vapier56ae2852006-05-26 05:56:54 +000084 snprintf(cmdstring, BUFSIZ, "test -d /proc/%d/fd || exit 1 ; "
subrata_modak56207ce2009-03-23 13:35:39 +000085 "ls -A -1 /proc/%d/fd | wc -l | awk {'print $1'} > pipe07.tmp",
86 mypid, mypid);
vapier5ef7d2f2006-05-26 05:58:39 +000087 if (system(cmdstring) == 0) {
88 f = fopen("pipe07.tmp", "r");
subrata_modak56207ce2009-03-23 13:35:39 +000089 fscanf(f, "%d", &usedfds);
robbiew278f0b62003-12-12 19:56:13 +000090 fclose(f);
vapier5ef7d2f2006-05-26 05:58:39 +000091 } else
subrata_modak56207ce2009-03-23 13:35:39 +000092 usedfds = 3; /* Could not get processes used fds, so assume 3 */
vapier56ae2852006-05-26 05:56:54 +000093 unlink("pipe07.tmp");
robbiew86389102003-12-08 20:39:34 +000094
robbiew906d4682003-01-06 17:23:07 +000095 TEST_EXP_ENOS(exp_enos);
96
97 for (lc = 0; TEST_LOOPING(lc); lc++) {
98
Caspar Zhangd59a6592013-03-07 14:59:12 +080099 /* reset tst_count in case we are looping */
100 tst_count = 0;
robbiew906d4682003-01-06 17:23:07 +0000101
vapier5ef7d2f2006-05-26 05:58:39 +0000102 min = getdtablesize();
103
robbiew86389102003-12-08 20:39:34 +0000104 /* subtract used fds */
105 min -= usedfds;
robbiew906d4682003-01-06 17:23:07 +0000106
subrata_modak56207ce2009-03-23 13:35:39 +0000107 for (npipes = 0;; npipes++) {
robbiew906d4682003-01-06 17:23:07 +0000108 pipe_ret = pipe(pipes);
109 if (pipe_ret < 0) {
110 if (errno != EMFILE) {
vapier5ef7d2f2006-05-26 05:58:39 +0000111 tst_brkm(TFAIL, cleanup,
subrata_modak56207ce2009-03-23 13:35:39 +0000112 "got unexpected error - %d",
113 errno);
robbiew906d4682003-01-06 17:23:07 +0000114 }
115 break;
116 }
117 }
118 if (npipes == (min / 2))
subrata_modak56207ce2009-03-23 13:35:39 +0000119 tst_resm(TPASS, "Opened %d pipes", npipes);
robbiew906d4682003-01-06 17:23:07 +0000120 else
121 tst_resm(TFAIL, "Unable to open maxfds/2 pipes");
122
123 }
124 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800125 tst_exit();
robbiew906d4682003-01-06 17:23:07 +0000126
robbiew906d4682003-01-06 17:23:07 +0000127}
128
129/*
130 * setup() - performs all ONE TIME setup for this test.
131 */
subrata_modak56207ce2009-03-23 13:35:39 +0000132void setup()
robbiew906d4682003-01-06 17:23:07 +0000133{
robbiew6617b472003-12-15 17:08:10 +0000134 char template[PATH_MAX];
135
136 /* I had to do this, instead of tst_tmpdir() b/c I was receiving *
137 * a SIGSEGV for some reason when I tried to use tst_tmpdir/tst_rmdir */
138
139 /* Save current working directory */
subrata_modak56207ce2009-03-23 13:35:39 +0000140 getcwd(currdir, PATH_MAX);
robbiew6617b472003-12-15 17:08:10 +0000141
142 /* Create temp directory and cd to it */
subrata_modak56207ce2009-03-23 13:35:39 +0000143 snprintf(template, PATH_MAX, "%s/%.3sXXXXXX", TEMPDIR, TCID);
144 tempdir = mkdtemp(template);
robbiew6617b472003-12-15 17:08:10 +0000145 chdir(tempdir);
robbiew86389102003-12-08 20:39:34 +0000146
robbiew86389102003-12-08 20:39:34 +0000147 tst_sig(FORK, DEF_HANDLER, cleanup);
robbiew906d4682003-01-06 17:23:07 +0000148
robbiew906d4682003-01-06 17:23:07 +0000149 TEST_PAUSE;
150
151}
152
153/*
154 * cleanup() - performs all ONE TIME cleanup for this test at
vapier5ef7d2f2006-05-26 05:58:39 +0000155 * completion or premature exit.
robbiew906d4682003-01-06 17:23:07 +0000156 */
subrata_modak56207ce2009-03-23 13:35:39 +0000157void cleanup()
robbiew906d4682003-01-06 17:23:07 +0000158{
robbiew906d4682003-01-06 17:23:07 +0000159 /*
160 * print timing stats if that option was specified.
161 * print errno log if that option was specified.
162 */
163 TEST_CLEANUP;
164
robbiew6617b472003-12-15 17:08:10 +0000165 /* I had to do this, instead of tst_tmpdir() b/c I was receiving *
166 * a SIGSEGV for some reason when I tried to use tst_tmpdir/tst_rmdir */
167
168 /* Chdir back to original working directory */
169 chdir(currdir);
170
robbiew6617b472003-12-15 17:08:10 +0000171 rmdir(tempdir);
Chris Dearmanec6edca2012-10-17 19:54:01 -0700172}