plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 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 Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 20 | * with this program; if not, write the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 22 | * |
| 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 | * |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 32 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 33 | * OS Test - Silicon Graphics, Inc. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 34 | * TEST IDENTIFIER : fork01 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 35 | * EXECUTED BY : anyone |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 36 | * TEST TITLE : Basic test for fork(2) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 37 | * PARENT DOCUMENT : frktds02 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 38 | * TEST CASE TOTAL : 2 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 39 | * WALL CLOCK TIME : 1 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 40 | * CPU TYPES : ALL |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 41 | * AUTHOR : Kathy Olmsted |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 42 | * CO-PILOT : Steve Shaw |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 43 | * DATE STARTED : 06/17/92 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | * INITIAL RELEASE : UNICOS 7.0 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | * TEST CASES |
Wanlong Gao | 284f248 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 46 | * 1.) fork returns without error |
| 47 | * 2.) fork returns the pid of the child |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | * INPUT SPECIFICATIONS |
Wanlong Gao | 284f248 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 49 | * The standard options for system call tests are accepted. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 50 | * (See the parse_opts(3) man page). |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | * OUTPUT SPECIFICATIONS |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 52 | * DURATION |
Wanlong Gao | 284f248 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 53 | * Terminates - with frequency and infinite modes. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | * SIGNALS |
Wanlong Gao | 284f248 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 55 | * Uses SIGUSR1 to pause before test if option set. |
| 56 | * (See the parse_opts(3) man page). |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | * RESOURCES |
Wanlong Gao | 284f248 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 58 | * None |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | * ENVIRONMENTAL NEEDS |
| 60 | * No run-time environmental needs. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | * SPECIAL PROCEDURAL REQUIREMENTS |
Wanlong Gao | 284f248 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 62 | * None |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | * INTERCASE DEPENDENCIES |
Wanlong Gao | 284f248 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 64 | * None |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | * DETAILED DESCRIPTION |
Wanlong Gao | 284f248 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 66 | * Setup: |
| 67 | * Setup signal handling. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | * Pause for SIGUSR1 if option specified. |
Wanlong Gao | 284f248 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 69 | * Test: |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | * Loop if the proper options are given. |
| 71 | * fork() |
| 72 | * Check return code, if system call failed (return=-1) |
| 73 | * Log the errno and Issue a FAIL message. |
| 74 | * Otherwise, Issue a PASS message. |
| 75 | * CHILD: |
| 76 | * determine PID |
| 77 | * write to PID to a file and close the file |
| 78 | * exit |
| 79 | * PARENT: |
| 80 | * wait for child to exit |
| 81 | * read child PID from file |
| 82 | * compare child PID to fork() return code and report |
| 83 | * results |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 84 | * |
Wanlong Gao | 284f248 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 85 | * Cleanup: |
| 86 | * Print errno log and/or timing stats if options given |
| 87 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | |
| 89 | #include <errno.h> |
| 90 | #include <string.h> |
| 91 | #include <signal.h> |
| 92 | #include <fcntl.h> |
| 93 | #include <stdlib.h> |
| 94 | #include <sys/types.h> |
| 95 | #include <sys/wait.h> |
| 96 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | |
| 98 | #define KIDEXIT 42 |
Wanlong Gao | 284f248 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 99 | static void setup(); |
| 100 | static void cleanup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | |
Wanlong Gao | 284f248 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 102 | #define LINE_SZ 20 |
| 103 | #define FILENAME "childpid" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | |
Wanlong Gao | 284f248 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 105 | char *TCID = "fork01"; |
| 106 | int TST_TOTAL = 2; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | |
Wanlong Gao | 284f248 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 108 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | * child_pid - the child side of the test |
Wanlong Gao | 284f248 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 110 | * determine the PID and write to a file |
| 111 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 112 | static void child_pid(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 114 | |
| 115 | int fildes; |
| 116 | char tmp_line[LINE_SZ]; |
| 117 | |
| 118 | fildes = creat(FILENAME, 0700); |
| 119 | sprintf(tmp_line, "%d\n", getpid()); |
| 120 | write(fildes, tmp_line, LINE_SZ); |
| 121 | close(fildes); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | |
| 123 | } |
| 124 | |
Wanlong Gao | 284f248 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 125 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | * parent_pid - the parent side of the test |
Wanlong Gao | 284f248 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 127 | * read the value determined by the child |
| 128 | * compare and report results |
| 129 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 130 | static void parent_pid(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 132 | |
| 133 | int fildes; |
| 134 | char tmp_line[LINE_SZ]; |
| 135 | pid_t child_id; |
| 136 | |
Wanlong Gao | 284f248 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 137 | fildes = open(FILENAME, O_RDWR); |
| 138 | if (fildes == -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 139 | tst_brkm(TBROK, cleanup, |
| 140 | "parent open failed. errno: %d (%s)\n", |
| 141 | errno, strerror(errno)); |
| 142 | } else { |
| 143 | if (read(fildes, tmp_line, LINE_SZ) == 0) { |
| 144 | tst_brkm(TBROK, cleanup, |
| 145 | "fork(): parent failed to read PID from file errno: %d (%s)", |
| 146 | errno, strerror(errno)); |
| 147 | } else { |
| 148 | child_id = atoi(tmp_line); |
| 149 | if (TEST_RETURN != child_id) { |
| 150 | tst_resm(TFAIL, |
subrata_modak | 358c3ee | 2009-10-26 14:55:46 +0000 | [diff] [blame] | 151 | "child reported a pid of %d. parent received %ld from fork()", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 152 | child_id, TEST_RETURN); |
| 153 | } else { |
| 154 | tst_resm(TPASS, |
| 155 | "child pid and fork() return agree: %d", |
| 156 | child_id); |
| 157 | } |
| 158 | } |
| 159 | close(fildes); |
| 160 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | } |
| 162 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 163 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | { |
Wanlong Gao | 284f248 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 165 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 166 | const char *msg; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 167 | int fails; |
| 168 | int kid_status, wait_status; |
| 169 | |
Wanlong Gao | 284f248 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 170 | msg = parse_opts(ac, av, NULL, NULL); |
| 171 | if (msg != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 172 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 174 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 175 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 176 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 177 | tst_count = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 178 | fails = 0; |
| 179 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 180 | TEST(fork()); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 181 | if (TEST_RETURN == -1) { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 182 | tst_resm(TFAIL, "fork() Failed, errno=%d : %s", |
| 183 | TEST_ERRNO, strerror(TEST_ERRNO)); |
| 184 | tst_resm(TBROK, "unable to continue"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 185 | } |
| 186 | if (TEST_RETURN == 0) { |
| 187 | /* child */ |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 188 | child_pid(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 189 | exit(KIDEXIT); |
| 190 | } else { |
| 191 | /* parent */ |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 192 | tst_resm(TPASS, "fork() returned %ld", |
| 193 | TEST_RETURN); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 194 | /* wait for the child to complete */ |
| 195 | wait_status = waitpid(TEST_RETURN, &kid_status, 0); |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 196 | |
| 197 | if (wait_status == TEST_RETURN) { |
| 198 | if (kid_status != KIDEXIT << 8) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 199 | tst_resm(TBROK, |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 200 | "incorrect child status returned on wait(): %d", |
| 201 | kid_status); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 202 | fails++; |
| 203 | } |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 204 | } else { |
| 205 | tst_resm(TBROK, |
| 206 | "wait() for child status failed with %d errno: %d : %s", |
| 207 | wait_status, errno, |
| 208 | strerror(errno)); |
| 209 | fails++; |
| 210 | } |
| 211 | if (fails == 0) { |
| 212 | /* verification tests */ |
| 213 | parent_pid(); |
| 214 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 215 | } /* TEST_RETURN */ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 216 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 217 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 218 | cleanup(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 219 | tst_exit(); |
| 220 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 221 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 222 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 223 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 224 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 225 | tst_sig(FORK, DEF_HANDLER, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 226 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 227 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 228 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 229 | tst_tmpdir(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 230 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 231 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 232 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 233 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 234 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 235 | tst_rmdir(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 236 | |
Wanlong Gao | 284f248 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 237 | } |