blob: ab30268fbce67bc0e938b587f05885af4d8a5224 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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 Gaofed96412012-10-24 10:10:29 +080020 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
plars865695b2001-08-27 22:15:12 +000022 *
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_modak4bb656a2009-02-26 12:02:09 +000032 *
plars865695b2001-08-27 22:15:12 +000033 * OS Test - Silicon Graphics, Inc.
plars865695b2001-08-27 22:15:12 +000034 * TEST IDENTIFIER : fork01
plars865695b2001-08-27 22:15:12 +000035 * EXECUTED BY : anyone
plars865695b2001-08-27 22:15:12 +000036 * TEST TITLE : Basic test for fork(2)
plars865695b2001-08-27 22:15:12 +000037 * PARENT DOCUMENT : frktds02
plars865695b2001-08-27 22:15:12 +000038 * TEST CASE TOTAL : 2
plars865695b2001-08-27 22:15:12 +000039 * WALL CLOCK TIME : 1
plars865695b2001-08-27 22:15:12 +000040 * CPU TYPES : ALL
plars865695b2001-08-27 22:15:12 +000041 * AUTHOR : Kathy Olmsted
plars865695b2001-08-27 22:15:12 +000042 * CO-PILOT : Steve Shaw
plars865695b2001-08-27 22:15:12 +000043 * DATE STARTED : 06/17/92
plars865695b2001-08-27 22:15:12 +000044 * INITIAL RELEASE : UNICOS 7.0
plars865695b2001-08-27 22:15:12 +000045 * TEST CASES
Wanlong Gao284f2482012-09-18 17:14:01 +080046 * 1.) fork returns without error
47 * 2.) fork returns the pid of the child
plars865695b2001-08-27 22:15:12 +000048 * INPUT SPECIFICATIONS
Wanlong Gao284f2482012-09-18 17:14:01 +080049 * The standard options for system call tests are accepted.
plars865695b2001-08-27 22:15:12 +000050 * (See the parse_opts(3) man page).
plars865695b2001-08-27 22:15:12 +000051 * OUTPUT SPECIFICATIONS
plars865695b2001-08-27 22:15:12 +000052 * DURATION
Wanlong Gao284f2482012-09-18 17:14:01 +080053 * Terminates - with frequency and infinite modes.
plars865695b2001-08-27 22:15:12 +000054 * SIGNALS
Wanlong Gao284f2482012-09-18 17:14:01 +080055 * Uses SIGUSR1 to pause before test if option set.
56 * (See the parse_opts(3) man page).
plars865695b2001-08-27 22:15:12 +000057 * RESOURCES
Wanlong Gao284f2482012-09-18 17:14:01 +080058 * None
plars865695b2001-08-27 22:15:12 +000059 * ENVIRONMENTAL NEEDS
60 * No run-time environmental needs.
plars865695b2001-08-27 22:15:12 +000061 * SPECIAL PROCEDURAL REQUIREMENTS
Wanlong Gao284f2482012-09-18 17:14:01 +080062 * None
plars865695b2001-08-27 22:15:12 +000063 * INTERCASE DEPENDENCIES
Wanlong Gao284f2482012-09-18 17:14:01 +080064 * None
plars865695b2001-08-27 22:15:12 +000065 * DETAILED DESCRIPTION
Wanlong Gao284f2482012-09-18 17:14:01 +080066 * Setup:
67 * Setup signal handling.
plars865695b2001-08-27 22:15:12 +000068 * Pause for SIGUSR1 if option specified.
Wanlong Gao284f2482012-09-18 17:14:01 +080069 * Test:
plars865695b2001-08-27 22:15:12 +000070 * 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_modak4bb656a2009-02-26 12:02:09 +000084 *
Wanlong Gao284f2482012-09-18 17:14:01 +080085 * Cleanup:
86 * Print errno log and/or timing stats if options given
87 */
plars865695b2001-08-27 22:15:12 +000088
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"
plars865695b2001-08-27 22:15:12 +000097
98#define KIDEXIT 42
Wanlong Gao284f2482012-09-18 17:14:01 +080099static void setup();
100static void cleanup();
plars865695b2001-08-27 22:15:12 +0000101
Wanlong Gao284f2482012-09-18 17:14:01 +0800102#define LINE_SZ 20
103#define FILENAME "childpid"
plars865695b2001-08-27 22:15:12 +0000104
Wanlong Gao284f2482012-09-18 17:14:01 +0800105char *TCID = "fork01";
106int TST_TOTAL = 2;
plars865695b2001-08-27 22:15:12 +0000107
Wanlong Gao284f2482012-09-18 17:14:01 +0800108/*
plars865695b2001-08-27 22:15:12 +0000109 * child_pid - the child side of the test
Wanlong Gao284f2482012-09-18 17:14:01 +0800110 * determine the PID and write to a file
111 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400112static void child_pid(void)
plars865695b2001-08-27 22:15:12 +0000113{
subrata_modak56207ce2009-03-23 13:35:39 +0000114
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);
plars865695b2001-08-27 22:15:12 +0000122
123}
124
Wanlong Gao284f2482012-09-18 17:14:01 +0800125/*
plars865695b2001-08-27 22:15:12 +0000126 * parent_pid - the parent side of the test
Wanlong Gao284f2482012-09-18 17:14:01 +0800127 * read the value determined by the child
128 * compare and report results
129 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400130static void parent_pid(void)
plars865695b2001-08-27 22:15:12 +0000131{
subrata_modak56207ce2009-03-23 13:35:39 +0000132
133 int fildes;
134 char tmp_line[LINE_SZ];
135 pid_t child_id;
136
Wanlong Gao284f2482012-09-18 17:14:01 +0800137 fildes = open(FILENAME, O_RDWR);
138 if (fildes == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000139 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_modak358c3ee2009-10-26 14:55:46 +0000151 "child reported a pid of %d. parent received %ld from fork()",
subrata_modak56207ce2009-03-23 13:35:39 +0000152 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 }
plars865695b2001-08-27 22:15:12 +0000161}
162
subrata_modak56207ce2009-03-23 13:35:39 +0000163int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000164{
Wanlong Gao284f2482012-09-18 17:14:01 +0800165 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200166 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +0000167 int fails;
168 int kid_status, wait_status;
169
Wanlong Gao284f2482012-09-18 17:14:01 +0800170 msg = parse_opts(ac, av, NULL, NULL);
171 if (msg != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800172 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000173
subrata_modak56207ce2009-03-23 13:35:39 +0000174 setup();
plars865695b2001-08-27 22:15:12 +0000175
subrata_modak56207ce2009-03-23 13:35:39 +0000176 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800177 tst_count = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000178 fails = 0;
179
subrata_modak56207ce2009-03-23 13:35:39 +0000180 TEST(fork());
subrata_modak56207ce2009-03-23 13:35:39 +0000181 if (TEST_RETURN == -1) {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200182 tst_resm(TFAIL, "fork() Failed, errno=%d : %s",
183 TEST_ERRNO, strerror(TEST_ERRNO));
184 tst_resm(TBROK, "unable to continue");
subrata_modak56207ce2009-03-23 13:35:39 +0000185 }
186 if (TEST_RETURN == 0) {
187 /* child */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200188 child_pid();
subrata_modak56207ce2009-03-23 13:35:39 +0000189 exit(KIDEXIT);
190 } else {
191 /* parent */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200192 tst_resm(TPASS, "fork() returned %ld",
193 TEST_RETURN);
subrata_modak56207ce2009-03-23 13:35:39 +0000194 /* wait for the child to complete */
195 wait_status = waitpid(TEST_RETURN, &kid_status, 0);
Cyril Hrubise38b9612014-06-02 17:20:57 +0200196
197 if (wait_status == TEST_RETURN) {
198 if (kid_status != KIDEXIT << 8) {
subrata_modak56207ce2009-03-23 13:35:39 +0000199 tst_resm(TBROK,
Cyril Hrubise38b9612014-06-02 17:20:57 +0200200 "incorrect child status returned on wait(): %d",
201 kid_status);
subrata_modak56207ce2009-03-23 13:35:39 +0000202 fails++;
203 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200204 } 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_modak56207ce2009-03-23 13:35:39 +0000215 } /* TEST_RETURN */
Garrett Cooper2c282152010-12-16 00:55:50 -0800216 }
subrata_modak56207ce2009-03-23 13:35:39 +0000217
subrata_modak56207ce2009-03-23 13:35:39 +0000218 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -0800219 tst_exit();
220}
plars865695b2001-08-27 22:15:12 +0000221
Mike Frysingerc57fba52014-04-09 18:56:30 -0400222static void setup(void)
plars865695b2001-08-27 22:15:12 +0000223{
Garrett Cooper2c282152010-12-16 00:55:50 -0800224
subrata_modak56207ce2009-03-23 13:35:39 +0000225 tst_sig(FORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000226
subrata_modak56207ce2009-03-23 13:35:39 +0000227 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000228
subrata_modak56207ce2009-03-23 13:35:39 +0000229 tst_tmpdir();
Garrett Cooper2c282152010-12-16 00:55:50 -0800230}
plars865695b2001-08-27 22:15:12 +0000231
Mike Frysingerc57fba52014-04-09 18:56:30 -0400232static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000233{
plars865695b2001-08-27 22:15:12 +0000234
subrata_modak56207ce2009-03-23 13:35:39 +0000235 tst_rmdir();
Garrett Cooper2c282152010-12-16 00:55:50 -0800236
Wanlong Gao284f2482012-09-18 17:14:01 +0800237}