robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | 2aabae1 | 2013-06-25 19:15:40 +0200 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2002 |
| 3 | * Ported by Paul Larson |
| 4 | * Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz> |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 5 | * |
Cyril Hrubis | 2aabae1 | 2013-06-25 19:15:40 +0200 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 10 | * |
Cyril Hrubis | 2aabae1 | 2013-06-25 19:15:40 +0200 | [diff] [blame] | 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 14 | * the GNU General Public License for more details. |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 15 | * |
Cyril Hrubis | 2aabae1 | 2013-06-25 19:15:40 +0200 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | /* |
Cyril Hrubis | 2aabae1 | 2013-06-25 19:15:40 +0200 | [diff] [blame] | 22 | * Test the ability of pipe to open the maximum even number of file |
| 23 | * descriptors permitted (or (maxfds - 3)/2 pipes) |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 24 | * |
| 25 | * ALGORITHM |
Cyril Hrubis | 2aabae1 | 2013-06-25 19:15:40 +0200 | [diff] [blame] | 26 | * 1. record file descriptors open prior to test run |
| 27 | * 2. open pipes until EMFILE is returned |
| 28 | * 3. check to see that the number of pipes opened is (maxfds - 3) / 2 |
| 29 | * 4. close all fds in range 0, maximal fd that were not open prior to |
| 30 | * the test execution |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 31 | */ |
robbiew | 6617b47 | 2003-12-15 17:08:10 +0000 | [diff] [blame] | 32 | #include <unistd.h> |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 33 | #include <fcntl.h> |
| 34 | #include <errno.h> |
robbiew | 8638910 | 2003-12-08 20:39:34 +0000 | [diff] [blame] | 35 | #include <stdlib.h> |
| 36 | #include <stdio.h> |
Cyril Hrubis | 2aabae1 | 2013-06-25 19:15:40 +0200 | [diff] [blame] | 37 | #include <string.h> |
| 38 | #include <dirent.h> |
| 39 | |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 40 | #include "test.h" |
Cyril Hrubis | 2aabae1 | 2013-06-25 19:15:40 +0200 | [diff] [blame] | 41 | #include "safe_macros.h" |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 42 | |
| 43 | char *TCID = "pipe07"; |
| 44 | int TST_TOTAL = 1; |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 45 | |
Cyril Hrubis | 2aabae1 | 2013-06-25 19:15:40 +0200 | [diff] [blame] | 46 | /* used to record file descriptors open at the test start */ |
| 47 | static int rec_fds[128]; |
| 48 | static int rec_fds_max; |
| 49 | static void record_open_fds(void); |
| 50 | static void close_test_fds(int max_fd); |
| 51 | |
| 52 | static void setup(void); |
| 53 | static void cleanup(void); |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 54 | |
| 55 | int main(int ac, char **av) |
| 56 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 57 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 58 | const char *msg; |
Cyril Hrubis | 2aabae1 | 2013-06-25 19:15:40 +0200 | [diff] [blame] | 59 | int min, ret; |
| 60 | int npipes; |
| 61 | int pipes[2], max_fd = 0; |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 62 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 63 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 64 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | 8638910 | 2003-12-08 20:39:34 +0000 | [diff] [blame] | 65 | |
Cyril Hrubis | 2aabae1 | 2013-06-25 19:15:40 +0200 | [diff] [blame] | 66 | setup(); |
| 67 | |
| 68 | min = getdtablesize() - rec_fds_max; |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 69 | |
| 70 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 71 | tst_count = 0; |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 72 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 73 | for (npipes = 0;; npipes++) { |
Cyril Hrubis | 2aabae1 | 2013-06-25 19:15:40 +0200 | [diff] [blame] | 74 | ret = pipe(pipes); |
| 75 | if (ret < 0) { |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 76 | if (errno != EMFILE) { |
vapier | 5ef7d2f | 2006-05-26 05:58:39 +0000 | [diff] [blame] | 77 | tst_brkm(TFAIL, cleanup, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 78 | "got unexpected error - %d", |
| 79 | errno); |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 80 | } |
| 81 | break; |
| 82 | } |
Cyril Hrubis | 2aabae1 | 2013-06-25 19:15:40 +0200 | [diff] [blame] | 83 | |
| 84 | max_fd = MAX(pipes[0], max_fd); |
| 85 | max_fd = MAX(pipes[1], max_fd); |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 86 | } |
Cyril Hrubis | 2aabae1 | 2013-06-25 19:15:40 +0200 | [diff] [blame] | 87 | |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 88 | if (npipes == (min / 2)) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 89 | tst_resm(TPASS, "Opened %d pipes", npipes); |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 90 | else |
| 91 | tst_resm(TFAIL, "Unable to open maxfds/2 pipes"); |
| 92 | |
Cyril Hrubis | 2aabae1 | 2013-06-25 19:15:40 +0200 | [diff] [blame] | 93 | close_test_fds(max_fd); |
| 94 | max_fd = 0; |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 95 | } |
Cyril Hrubis | 2aabae1 | 2013-06-25 19:15:40 +0200 | [diff] [blame] | 96 | |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 97 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 98 | tst_exit(); |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Cyril Hrubis | 2aabae1 | 2013-06-25 19:15:40 +0200 | [diff] [blame] | 101 | static void setup(void) |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 102 | { |
robbiew | 8638910 | 2003-12-08 20:39:34 +0000 | [diff] [blame] | 103 | tst_sig(FORK, DEF_HANDLER, cleanup); |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 104 | TEST_PAUSE; |
| 105 | |
Cyril Hrubis | 2aabae1 | 2013-06-25 19:15:40 +0200 | [diff] [blame] | 106 | record_open_fds(); |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Cyril Hrubis | 2aabae1 | 2013-06-25 19:15:40 +0200 | [diff] [blame] | 109 | static void record_open_fds(void) |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 110 | { |
Cyril Hrubis | 2aabae1 | 2013-06-25 19:15:40 +0200 | [diff] [blame] | 111 | DIR *dir = opendir("/proc/self/fd"); |
| 112 | int dir_fd, fd; |
| 113 | struct dirent *file; |
| 114 | |
| 115 | if (dir == NULL) |
| 116 | tst_brkm(TBROK | TERRNO, cleanup, "opendir()"); |
| 117 | |
| 118 | dir_fd = dirfd(dir); |
| 119 | |
| 120 | if (dir_fd == -1) |
| 121 | tst_brkm(TBROK | TERRNO, cleanup, "dirfd()"); |
| 122 | |
| 123 | errno = 0; |
| 124 | |
| 125 | while ((file = readdir(dir))) { |
| 126 | if (!strcmp(file->d_name, ".") || !strcmp(file->d_name, "..")) |
| 127 | continue; |
| 128 | |
| 129 | fd = atoi(file->d_name); |
| 130 | |
| 131 | if (fd == dir_fd) |
| 132 | continue; |
| 133 | |
| 134 | if (rec_fds_max >= ARRAY_SIZE(rec_fds)) { |
| 135 | tst_brkm(TBROK, cleanup, |
| 136 | "Too much file descriptors open"); |
| 137 | } |
| 138 | |
| 139 | rec_fds[rec_fds_max++] = fd; |
| 140 | } |
| 141 | |
| 142 | if (errno) |
| 143 | tst_brkm(TBROK | TERRNO, cleanup, "readdir()"); |
| 144 | |
| 145 | closedir(dir); |
| 146 | |
| 147 | tst_resm(TINFO, "Found %u files open", rec_fds_max); |
| 148 | } |
| 149 | |
| 150 | static int not_recorded(int fd) |
| 151 | { |
| 152 | int i; |
| 153 | |
| 154 | for (i = 0; i < rec_fds_max; i++) |
| 155 | if (fd == rec_fds[i]) |
| 156 | return 0; |
| 157 | |
| 158 | return 1; |
| 159 | } |
| 160 | |
| 161 | static void close_test_fds(int max_fd) |
| 162 | { |
| 163 | int i; |
| 164 | |
| 165 | for (i = 0; i <= max_fd; i++) { |
| 166 | if (not_recorded(i)) { |
| 167 | if (close(i)) { |
| 168 | if (errno == EBADF) |
| 169 | continue; |
| 170 | tst_resm(TWARN | TERRNO, "close(%i)", i); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | static void cleanup(void) |
| 177 | { |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 178 | } |