robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 1 | /* |
| 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 Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 18 | */ |
| 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 | */ |
robbiew | 6617b47 | 2003-12-15 17:08:10 +0000 | [diff] [blame] | 48 | #include <unistd.h> |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 49 | #include <fcntl.h> |
| 50 | #include <errno.h> |
robbiew | 8638910 | 2003-12-08 20:39:34 +0000 | [diff] [blame] | 51 | #include <stdlib.h> |
| 52 | #include <stdio.h> |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 53 | #include "test.h" |
| 54 | #include "usctest.h" |
| 55 | |
| 56 | char *TCID = "pipe07"; |
| 57 | int TST_TOTAL = 1; |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 58 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 59 | int exp_enos[] = { EMFILE, 0 }; |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 60 | |
| 61 | int pipe_ret, pipes[2]; |
robbiew | 6617b47 | 2003-12-15 17:08:10 +0000 | [diff] [blame] | 62 | char currdir[PATH_MAX]; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 63 | char *tempdir = NULL; |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 64 | void setup(void); |
| 65 | void cleanup(void); |
| 66 | |
| 67 | int main(int ac, char **av) |
| 68 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 69 | int lc; |
| 70 | char *msg; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 71 | 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 */ |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 77 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 78 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 79 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 80 | setup(); |
vapier | 5ef7d2f | 2006-05-26 05:58:39 +0000 | [diff] [blame] | 81 | /* Get the currently used number of file descriptors */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 82 | mypid = getpid(); |
| 83 | cmdstring = malloc(BUFSIZ); |
vapier | 56ae285 | 2006-05-26 05:56:54 +0000 | [diff] [blame] | 84 | snprintf(cmdstring, BUFSIZ, "test -d /proc/%d/fd || exit 1 ; " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 85 | "ls -A -1 /proc/%d/fd | wc -l | awk {'print $1'} > pipe07.tmp", |
| 86 | mypid, mypid); |
vapier | 5ef7d2f | 2006-05-26 05:58:39 +0000 | [diff] [blame] | 87 | if (system(cmdstring) == 0) { |
| 88 | f = fopen("pipe07.tmp", "r"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 89 | fscanf(f, "%d", &usedfds); |
robbiew | 278f0b6 | 2003-12-12 19:56:13 +0000 | [diff] [blame] | 90 | fclose(f); |
vapier | 5ef7d2f | 2006-05-26 05:58:39 +0000 | [diff] [blame] | 91 | } else |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 92 | usedfds = 3; /* Could not get processes used fds, so assume 3 */ |
vapier | 56ae285 | 2006-05-26 05:56:54 +0000 | [diff] [blame] | 93 | unlink("pipe07.tmp"); |
robbiew | 8638910 | 2003-12-08 20:39:34 +0000 | [diff] [blame] | 94 | |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 95 | TEST_EXP_ENOS(exp_enos); |
| 96 | |
| 97 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 98 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame^] | 99 | /* reset tst_count in case we are looping */ |
| 100 | tst_count = 0; |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 101 | |
vapier | 5ef7d2f | 2006-05-26 05:58:39 +0000 | [diff] [blame] | 102 | min = getdtablesize(); |
| 103 | |
robbiew | 8638910 | 2003-12-08 20:39:34 +0000 | [diff] [blame] | 104 | /* subtract used fds */ |
| 105 | min -= usedfds; |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 106 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 107 | for (npipes = 0;; npipes++) { |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 108 | pipe_ret = pipe(pipes); |
| 109 | if (pipe_ret < 0) { |
| 110 | if (errno != EMFILE) { |
vapier | 5ef7d2f | 2006-05-26 05:58:39 +0000 | [diff] [blame] | 111 | tst_brkm(TFAIL, cleanup, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 112 | "got unexpected error - %d", |
| 113 | errno); |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 114 | } |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | if (npipes == (min / 2)) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 119 | tst_resm(TPASS, "Opened %d pipes", npipes); |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 120 | else |
| 121 | tst_resm(TFAIL, "Unable to open maxfds/2 pipes"); |
| 122 | |
| 123 | } |
| 124 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 125 | tst_exit(); |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 126 | |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /* |
| 130 | * setup() - performs all ONE TIME setup for this test. |
| 131 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 132 | void setup() |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 133 | { |
robbiew | 6617b47 | 2003-12-15 17:08:10 +0000 | [diff] [blame] | 134 | 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 140 | getcwd(currdir, PATH_MAX); |
robbiew | 6617b47 | 2003-12-15 17:08:10 +0000 | [diff] [blame] | 141 | |
| 142 | /* Create temp directory and cd to it */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 143 | snprintf(template, PATH_MAX, "%s/%.3sXXXXXX", TEMPDIR, TCID); |
| 144 | tempdir = mkdtemp(template); |
robbiew | 6617b47 | 2003-12-15 17:08:10 +0000 | [diff] [blame] | 145 | chdir(tempdir); |
robbiew | 8638910 | 2003-12-08 20:39:34 +0000 | [diff] [blame] | 146 | |
robbiew | 8638910 | 2003-12-08 20:39:34 +0000 | [diff] [blame] | 147 | tst_sig(FORK, DEF_HANDLER, cleanup); |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 148 | |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 149 | TEST_PAUSE; |
| 150 | |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * cleanup() - performs all ONE TIME cleanup for this test at |
vapier | 5ef7d2f | 2006-05-26 05:58:39 +0000 | [diff] [blame] | 155 | * completion or premature exit. |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 156 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 157 | void cleanup() |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 158 | { |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 159 | /* |
| 160 | * print timing stats if that option was specified. |
| 161 | * print errno log if that option was specified. |
| 162 | */ |
| 163 | TEST_CLEANUP; |
| 164 | |
robbiew | 6617b47 | 2003-12-15 17:08:10 +0000 | [diff] [blame] | 165 | /* 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 | |
robbiew | 6617b47 | 2003-12-15 17:08:10 +0000 | [diff] [blame] | 171 | rmdir(tempdir); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 172 | } |