blob: 723d73e8d992dac0dd65c1e23f2ee0284976059c [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
subrata_modak4bb656a2009-02-26 12:02:09 +00003 *
plars865695b2001-08-27 22:15:12 +00004 * 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.
subrata_modak4bb656a2009-02-26 12:02:09 +00007 *
plars865695b2001-08-27 22:15:12 +00008 * 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.
subrata_modak4bb656a2009-02-26 12:02:09 +000011 *
plars865695b2001-08-27 22:15:12 +000012 * 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.
subrata_modak4bb656a2009-02-26 12:02:09 +000018 *
plars865695b2001-08-27 22:15:12 +000019 * 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.
subrata_modak4bb656a2009-02-26 12:02:09 +000022 *
plars865695b2001-08-27 22:15:12 +000023 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
subrata_modak4bb656a2009-02-26 12:02:09 +000025 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
plars865695b2001-08-27 22:15:12 +000030 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31 */
vapier8dc646d2009-08-28 10:54:04 +000032/* $Id: alarm03.c,v 1.10 2009/08/28 10:57:29 vapier Exp $ */
plars865695b2001-08-27 22:15:12 +000033/**********************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000034 *
plars865695b2001-08-27 22:15:12 +000035 * OS Test - Silicon Graphics, Inc.
subrata_modak4bb656a2009-02-26 12:02:09 +000036 *
plars865695b2001-08-27 22:15:12 +000037 * TEST IDENTIFIER : alarm03
subrata_modak4bb656a2009-02-26 12:02:09 +000038 *
plars865695b2001-08-27 22:15:12 +000039 * EXECUTED BY : anyone
subrata_modak4bb656a2009-02-26 12:02:09 +000040 *
plars865695b2001-08-27 22:15:12 +000041 * TEST TITLE : alarm(2) cleared by a fork
subrata_modak4bb656a2009-02-26 12:02:09 +000042 *
plars865695b2001-08-27 22:15:12 +000043 * PARENT DOCUMENT : usctpl01
subrata_modak4bb656a2009-02-26 12:02:09 +000044 *
plars865695b2001-08-27 22:15:12 +000045 * TEST CASE TOTAL : 1
subrata_modak4bb656a2009-02-26 12:02:09 +000046 *
plars865695b2001-08-27 22:15:12 +000047 * WALL CLOCK TIME : 1
subrata_modak4bb656a2009-02-26 12:02:09 +000048 *
plars865695b2001-08-27 22:15:12 +000049 * CPU TYPES : ALL
subrata_modak4bb656a2009-02-26 12:02:09 +000050 *
plars865695b2001-08-27 22:15:12 +000051 * AUTHOR : Richard Logan
subrata_modak4bb656a2009-02-26 12:02:09 +000052 *
plars865695b2001-08-27 22:15:12 +000053 * CO-PILOT : Dennis Arason
subrata_modak4bb656a2009-02-26 12:02:09 +000054 *
plars865695b2001-08-27 22:15:12 +000055 * DATE STARTED : 08/96
subrata_modak4bb656a2009-02-26 12:02:09 +000056 *
57 *
plars865695b2001-08-27 22:15:12 +000058 * TEST CASES
subrata_modak4bb656a2009-02-26 12:02:09 +000059 *
plars865695b2001-08-27 22:15:12 +000060 * 1.) alarm(100), fork, child's alarm(0) shall return 0;
61 * 2.) alarm(100), fork, parent's alarm(0) shall return non-zero.
subrata_modakbdbaec52009-02-26 12:14:51 +000062 *
plars865695b2001-08-27 22:15:12 +000063 * INPUT SPECIFICATIONS
64 * The standard options for system call tests are accepted.
65 * (See the parse_opts(3) man page).
subrata_modak4bb656a2009-02-26 12:02:09 +000066 *
67 *
plars865695b2001-08-27 22:15:12 +000068 * SPECIAL PROCEDURAL REQUIREMENTS
69 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000070 *
71 *
plars865695b2001-08-27 22:15:12 +000072 * 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).
subrata_modak4bb656a2009-02-26 12:02:09 +000077 *
plars865695b2001-08-27 22:15:12 +000078 * Setup:
79 * Setup signal handling.
80 * Pause for SIGUSR1 if option specified.
subrata_modak4bb656a2009-02-26 12:02:09 +000081 *
plars865695b2001-08-27 22:15:12 +000082 * 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.
subrata_modak4bb656a2009-02-26 12:02:09 +000088 *
plars865695b2001-08-27 22:15:12 +000089 * Cleanup:
90 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +000091 *
92 *
plars865695b2001-08-27 22:15:12 +000093 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
94
95#include <errno.h>
96#include <string.h>
97#include <signal.h>
98#include <stdlib.h>
robbiew2c945242002-11-11 19:01:25 +000099#include <sys/wait.h>
plars865695b2001-08-27 22:15:12 +0000100#include "test.h"
plars865695b2001-08-27 22:15:12 +0000101
102void setup();
103void cleanup();
Garrett Cooperb88371d2010-11-23 21:54:22 -0800104void trapper();
plars865695b2001-08-27 22:15:12 +0000105
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200106char *TCID = "alarm03";
107int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +0000108
subrata_modak56207ce2009-03-23 13:35:39 +0000109int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000110{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200111 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200112 const char *msg;
Garrett Cooper53740502010-12-16 00:04:01 -0800113 int status, retval = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000114
Garrett Cooper203d18a2010-12-13 18:12:24 -0800115 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
subrata_modak56207ce2009-03-23 13:35:39 +0000116 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000117
subrata_modak56207ce2009-03-23 13:35:39 +0000118 setup();
plars865695b2001-08-27 22:15:12 +0000119
subrata_modak56207ce2009-03-23 13:35:39 +0000120 for (lc = 0; TEST_LOOPING(lc); lc++) {
plars865695b2001-08-27 22:15:12 +0000121
Caspar Zhangd59a6592013-03-07 14:59:12 +0800122 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000123
subrata_modak56207ce2009-03-23 13:35:39 +0000124 /*
125 * Call alarm(2)
126 */
127 TEST(alarm(100));
subrata_modakbdbaec52009-02-26 12:14:51 +0000128
subrata_modak56207ce2009-03-23 13:35:39 +0000129 switch (FORK_OR_VFORK()) {
130 case -1:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800131 tst_brkm(TBROK | TERRNO, cleanup, "fork() failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000132 break;
plars865695b2001-08-27 22:15:12 +0000133
subrata_modak56207ce2009-03-23 13:35:39 +0000134 case 0:
135 TEST(alarm(0));
plars865695b2001-08-27 22:15:12 +0000136
subrata_modak56207ce2009-03-23 13:35:39 +0000137 if (TEST_RETURN != 0) {
138 retval = 1;
Garrett Cooperb88371d2010-11-23 21:54:22 -0800139 printf("%d: alarm(100), fork, alarm(0) child's "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800140 "alarm returned %ld\n",
141 getpid(), TEST_RETURN);
Cyril Hrubise38b9612014-06-02 17:20:57 +0200142 } else {
Garrett Cooperb88371d2010-11-23 21:54:22 -0800143 printf("%d: alarm(100), fork, alarm(0) child's "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800144 "alarm returned %ld\n",
145 getpid(), TEST_RETURN);
subrata_modak56207ce2009-03-23 13:35:39 +0000146 }
plars865695b2001-08-27 22:15:12 +0000147
subrata_modak56207ce2009-03-23 13:35:39 +0000148 exit(retval);
149 break;
plars865695b2001-08-27 22:15:12 +0000150
subrata_modak56207ce2009-03-23 13:35:39 +0000151 default:
Caspar Zhangd59a6592013-03-07 14:59:12 +0800152 tst_count++;
subrata_modak56207ce2009-03-23 13:35:39 +0000153 TEST(alarm(0));
plars7dfbd7a2003-08-25 15:13:13 +0000154/* The timer may be rounded up to the next nearest second, this is OK */
subrata_modak56207ce2009-03-23 13:35:39 +0000155 if (TEST_RETURN <= 0 || TEST_RETURN > 101) {
156 retval = 1;
157 tst_resm(TFAIL,
vapier8dc646d2009-08-28 10:54:04 +0000158 "alarm(100), fork, alarm(0) parent's alarm returned %ld",
subrata_modak56207ce2009-03-23 13:35:39 +0000159 TEST_RETURN);
Cyril Hrubise38b9612014-06-02 17:20:57 +0200160 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000161 tst_resm(TPASS,
vapier8dc646d2009-08-28 10:54:04 +0000162 "alarm(100), fork, alarm(0) parent's alarm returned %ld",
subrata_modak56207ce2009-03-23 13:35:39 +0000163 TEST_RETURN);
164 }
Garrett Cooper6b66d952010-12-17 20:42:35 -0800165 if (wait(&status) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800166 tst_brkm(TBROK | TERRNO, cleanup,
167 "wait failed");
Garrett Cooper6b66d952010-12-17 20:42:35 -0800168 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
169 tst_resm(TFAIL, "see failures reported above");
plars865695b2001-08-27 22:15:12 +0000170
subrata_modak56207ce2009-03-23 13:35:39 +0000171 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000172
Garrett Cooper2c282152010-12-16 00:55:50 -0800173 }
plars865695b2001-08-27 22:15:12 +0000174
subrata_modak56207ce2009-03-23 13:35:39 +0000175 cleanup();
Garrett Cooper6b66d952010-12-17 20:42:35 -0800176 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800177}
plars865695b2001-08-27 22:15:12 +0000178
Mike Frysingerc57fba52014-04-09 18:56:30 -0400179void setup(void)
plars865695b2001-08-27 22:15:12 +0000180{
Garrett Cooper2c282152010-12-16 00:55:50 -0800181
subrata_modak56207ce2009-03-23 13:35:39 +0000182 tst_sig(FORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000183
subrata_modak56207ce2009-03-23 13:35:39 +0000184 signal(SIGALRM, trapper);
plars865695b2001-08-27 22:15:12 +0000185
subrata_modak56207ce2009-03-23 13:35:39 +0000186 TEST_PAUSE;
Garrett Cooper2c282152010-12-16 00:55:50 -0800187}
plars865695b2001-08-27 22:15:12 +0000188
Mike Frysingerc57fba52014-04-09 18:56:30 -0400189void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000190{
Garrett Cooper2c282152010-12-16 00:55:50 -0800191}
plars865695b2001-08-27 22:15:12 +0000192
Garrett Cooperb88371d2010-11-23 21:54:22 -0800193void trapper(int sig)
plars865695b2001-08-27 22:15:12 +0000194{
subrata_modak56207ce2009-03-23 13:35:39 +0000195 signal(SIGALRM, trapper);
Chris Dearmanec6edca2012-10-17 19:54:01 -0700196}