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
20  * with this program; if not, write the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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  */
32 /* $Id: alarm03.c,v 1.10 2009/08/28 10:57:29 vapier Exp $ */
33 /**********************************************************
34  *
35  *    OS Test - Silicon Graphics, Inc.
36  *
37  *    TEST IDENTIFIER	: alarm03
38  *
39  *    EXECUTED BY	: anyone
40  *
41  *    TEST TITLE	: alarm(2) cleared by a fork
42  *
43  *    PARENT DOCUMENT	: usctpl01
44  *
45  *    TEST CASE TOTAL	: 1
46  *
47  *    WALL CLOCK TIME	: 1
48  *
49  *    CPU TYPES		: ALL
50  *
51  *    AUTHOR		: Richard Logan
52  *
53  *    CO-PILOT		: Dennis Arason
54  *
55  *    DATE STARTED	: 08/96
56  *
57  *
58  *    TEST CASES
59  *
60  * 	1.) alarm(100), fork, child's alarm(0) shall return 0;
61  *	2.) alarm(100), fork, parent's alarm(0) shall return non-zero.
62  *
63  *    INPUT SPECIFICATIONS
64  * 	The standard options for system call tests are accepted.
65  *	(See the parse_opts(3) man page).
66  *
67  *
68  *    SPECIAL PROCEDURAL REQUIREMENTS
69  * 	None
70  *
71  *
72  *    DETAILED DESCRIPTION
73  *	This is a Phase I test for the alarm(2) system call.  It is intended
74  *	to provide a limited exposure of the system call, for now.  It
75  *	should/will be extended when full functional tests are written for
76  *	alarm(2).
77  *
78  * 	Setup:
79  * 	  Setup signal handling.
80  *	  Pause for SIGUSR1 if option specified.
81  *
82  * 	Test:
83  *	 Loop if the proper options are given.
84  * 	  Execute system call
85  *	  Check return code, if system call failed (return=-1)
86  *		Log the errno and Issue a FAIL message.
87  *	  Otherwise, Issue a PASS message.
88  *
89  * 	Cleanup:
90  * 	  Print errno log and/or timing stats if options given
91  *
92  *
93  *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
94 
95 #include <errno.h>
96 #include <string.h>
97 #include <signal.h>
98 #include <stdlib.h>
99 #include <sys/wait.h>
100 #include "test.h"
101 
102 void setup();
103 void cleanup();
104 void trapper();
105 
106 char *TCID = "alarm03";
107 int TST_TOTAL = 1;
108 
main(int ac,char ** av)109 int main(int ac, char **av)
110 {
111 	int lc;
112 	int status, retval = 0;
113 
114 	tst_parse_opts(ac, av, NULL, NULL);
115 
116 	setup();
117 
118 	for (lc = 0; TEST_LOOPING(lc); lc++) {
119 
120 		tst_count = 0;
121 
122 		/*
123 		 * Call alarm(2)
124 		 */
125 		TEST(alarm(100));
126 
127 		switch (FORK_OR_VFORK()) {
128 		case -1:
129 			tst_brkm(TBROK | TERRNO, cleanup, "fork() failed");
130 			break;
131 
132 		case 0:
133 			TEST(alarm(0));
134 
135 			if (TEST_RETURN != 0) {
136 				retval = 1;
137 				printf("%d: alarm(100), fork, alarm(0) child's "
138 				       "alarm returned %ld\n",
139 				       getpid(), TEST_RETURN);
140 			} else {
141 				printf("%d: alarm(100), fork, alarm(0) child's "
142 				       "alarm returned %ld\n",
143 				       getpid(), TEST_RETURN);
144 			}
145 
146 			exit(retval);
147 			break;
148 
149 		default:
150 			tst_count++;
151 			TEST(alarm(0));
152 /* The timer may be rounded up to the next nearest second, this is OK */
153 			if (TEST_RETURN <= 0 || TEST_RETURN > 101) {
154 				retval = 1;
155 				tst_resm(TFAIL,
156 					 "alarm(100), fork, alarm(0) parent's alarm returned %ld",
157 					 TEST_RETURN);
158 			} else {
159 				tst_resm(TPASS,
160 					 "alarm(100), fork, alarm(0) parent's alarm returned %ld",
161 					 TEST_RETURN);
162 			}
163 			if (wait(&status) == -1)
164 				tst_brkm(TBROK | TERRNO, cleanup,
165 					 "wait failed");
166 			if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
167 				tst_resm(TFAIL, "see failures reported above");
168 
169 		}
170 
171 	}
172 
173 	cleanup();
174 	tst_exit();
175 }
176 
setup(void)177 void setup(void)
178 {
179 
180 	tst_sig(FORK, DEF_HANDLER, cleanup);
181 
182 	signal(SIGALRM, trapper);
183 
184 	TEST_PAUSE;
185 }
186 
cleanup(void)187 void cleanup(void)
188 {
189 }
190 
trapper(int sig)191 void trapper(int sig)
192 {
193 	signal(SIGALRM, trapper);
194 }
195