plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2001 |
| 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 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
| 22 | * pipe11.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Check if many children can read what is written to a pipe by the |
| 26 | * parent. |
| 27 | * |
| 28 | * ALGORITHM |
| 29 | * 1. Open a pipe and write to it |
| 30 | * 2. Fork a large number of children |
| 31 | * 3. Have the children read the pipe and check how many characters |
| 32 | * each got |
| 33 | * |
| 34 | * USAGE: <for command-line> |
| 35 | * pipe11 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 36 | * where, -c n : Run n copies concurrently. |
| 37 | * -f : Turn off functionality Testing. |
| 38 | * -i n : Execute test n times. |
| 39 | * -I x : Execute test for x seconds. |
| 40 | * -P x : Pause for x seconds between iterations. |
| 41 | * -t : Turn on syscall timing. |
| 42 | * |
| 43 | * HISTORY |
| 44 | * 07/2001 Ported by Wayne Boyer |
| 45 | * |
| 46 | * RESTRICTIONS |
| 47 | * None |
| 48 | */ |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 49 | #include <sys/types.h> |
| 50 | #include <sys/wait.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | #include <errno.h> |
| 52 | #include <stdio.h> |
| 53 | #include <limits.h> |
| 54 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | |
| 56 | char *TCID = "pipe11"; |
| 57 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 59 | void do_child(void); |
| 60 | void do_child_uclinux(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | void setup(void); |
| 62 | void cleanup(void); |
| 63 | |
| 64 | #define NUMCHILD 50 |
| 65 | #define NCPERCHILD 50 |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 66 | char rawchars[] = |
| 67 | "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | int kidid; |
| 69 | int numchild; /* no of children to fork */ |
| 70 | int ncperchild; /* no of chars child should read */ |
| 71 | int szcharbuf; /* size of char buf */ |
| 72 | int pipewrcnt; /* chars written to pipe */ |
| 73 | char *wrbuf, *rdbuf; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 74 | int fd[2]; /* fds for pipe read/write */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | |
robbiew | a728d28 | 2003-09-08 21:36:41 +0000 | [diff] [blame] | 76 | ssize_t safe_read(int fd, void *buf, size_t count) |
| 77 | { |
| 78 | ssize_t n; |
| 79 | |
| 80 | do { |
| 81 | n = read(fd, buf, count); |
| 82 | } while (n < 0 && errno == EINTR); |
| 83 | |
| 84 | return n; |
| 85 | } |
| 86 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 87 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 89 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 90 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | int i; |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 93 | int fork_ret, status; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 94 | int written; /* no of chars read and written */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 96 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 97 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 98 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 99 | #ifdef UCLINUX |
| 100 | maybe_run_child(&do_child_uclinux, "ddddd", &fd[0], &fd[1], &kidid, |
| 101 | &ncperchild, &szcharbuf); |
| 102 | #endif |
| 103 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | setup(); |
| 105 | |
| 106 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 107 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 108 | /* reset tst_count in case we are looping */ |
| 109 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | |
| 111 | TEST(pipe(fd)); |
| 112 | |
| 113 | if (TEST_RETURN != 0) { |
| 114 | tst_resm(TFAIL, "pipe creation failed"); |
| 115 | continue; |
| 116 | } |
| 117 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | written = write(fd[1], wrbuf, szcharbuf); |
| 119 | if (written != szcharbuf) { |
| 120 | tst_brkm(TBROK, cleanup, "write to pipe failed"); |
| 121 | } |
| 122 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 123 | refork: |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | ++kidid; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 125 | fork_ret = FORK_OR_VFORK(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | |
| 127 | if (fork_ret < 0) { |
| 128 | tst_brkm(TBROK, cleanup, "fork() failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 129 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | |
| 131 | if ((fork_ret != 0) && (fork_ret != -1) && (kidid < numchild)) { |
| 132 | goto refork; |
| 133 | } |
| 134 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 135 | if (fork_ret == 0) { /* child */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 136 | #ifdef UCLINUX |
| 137 | if (self_exec(av[0], "ddddd", fd[0], fd[1], kidid, |
| 138 | ncperchild, szcharbuf) < 0) { |
| 139 | tst_brkm(TBROK, cleanup, "self_exec failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 140 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 141 | #else |
| 142 | do_child(); |
| 143 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /* parent */ |
| 147 | sleep(5); |
| 148 | tst_resm(TINFO, "There are %d children to wait for", kidid); |
| 149 | for (i = 1; i <= kidid; ++i) { |
| 150 | wait(&status); |
| 151 | if (status == 0) { |
| 152 | tst_resm(TPASS, "child %d exitted successfully", |
| 153 | i); |
| 154 | } else { |
| 155 | tst_resm(TFAIL, "child %d exitted with bad " |
| 156 | "status", i); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | cleanup(); |
| 161 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 162 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /* |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 166 | * do_child() |
| 167 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 168 | void do_child(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 169 | { |
| 170 | int nread; |
| 171 | |
| 172 | if (close(fd[1])) { |
| 173 | tst_resm(TINFO, "child %d " "could not close pipe", kidid); |
| 174 | exit(0); |
| 175 | } |
| 176 | nread = safe_read(fd[0], rdbuf, ncperchild); |
| 177 | if (nread == ncperchild) { |
| 178 | tst_resm(TINFO, "child %d " "got %d chars", kidid, nread); |
| 179 | exit(0); |
| 180 | } else { |
| 181 | tst_resm(TFAIL, "child %d did not receive expected no of " |
| 182 | "characters, got %d characters", kidid, nread); |
| 183 | exit(1); |
| 184 | } |
| 185 | } |
| 186 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 187 | /* |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 188 | * do_child_uclinux() - as above, but mallocs rdbuf first |
| 189 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 190 | void do_child_uclinux(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 191 | { |
Cyril Hrubis | 4e2bab8 | 2014-09-24 16:34:35 +0200 | [diff] [blame] | 192 | if ((rdbuf = malloc(szcharbuf)) == NULL) { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 193 | tst_brkm(TBROK, cleanup, "malloc of rdbuf failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 194 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 195 | |
| 196 | do_child(); |
| 197 | } |
| 198 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 199 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 200 | * setup() - performs all ONE TIME setup for this test. |
| 201 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 202 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 203 | { |
| 204 | int i, j; |
| 205 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 206 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 207 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 208 | TEST_PAUSE; |
| 209 | |
| 210 | numchild = NUMCHILD; |
| 211 | ncperchild = NCPERCHILD; |
| 212 | |
| 213 | kidid = 0; |
| 214 | |
| 215 | /* allocate read and write buffers */ |
| 216 | szcharbuf = numchild * ncperchild; |
| 217 | |
| 218 | /* make sure pipe write doesn't block */ |
| 219 | if (szcharbuf == PIPE_BUF) { |
| 220 | /* adjust number of characters per child */ |
| 221 | ncperchild = szcharbuf / numchild; |
| 222 | } |
| 223 | |
Cyril Hrubis | 4e2bab8 | 2014-09-24 16:34:35 +0200 | [diff] [blame] | 224 | if ((wrbuf = malloc(szcharbuf)) == NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 225 | tst_brkm(TBROK, cleanup, "malloc failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 226 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 227 | |
Cyril Hrubis | 4e2bab8 | 2014-09-24 16:34:35 +0200 | [diff] [blame] | 228 | if ((rdbuf = malloc(szcharbuf)) == NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 229 | tst_brkm(TBROK, cleanup, "malloc of rdbuf failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 230 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 231 | |
| 232 | /* initialize wrbuf */ |
| 233 | j = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 234 | for (i = 0; i < szcharbuf;) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 235 | wrbuf[i++] = rawchars[j++]; |
| 236 | if (j >= sizeof(rawchars)) { |
| 237 | j = 0; |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 244 | * completion or premature exit. |
| 245 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 246 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 247 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 248 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 249 | } |