1 /*
2  * Copyright (c) Wipro Technologies Ltd, 2003.  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  * You should have received a copy of the GNU General Public License along
13  * with this program; if not, write the Free Software Foundation, Inc.,
14  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15  *
16  */
17 /**************************************************************************
18  *
19  *    TEST IDENTIFIER	: timer_create02
20  *
21  *    EXECUTED BY	: anyone
22  *
23  *    TEST TITLE	: Basic test for timer_create(2)
24  *
25  *    TEST CASE TOTAL	: 3
26  *
27  *    AUTHOR		: Aniruddha Marathe <aniruddha.marathe@wipro.com>
28  *
29  *    SIGNALS
30  * 	Uses SIGUSR1 to pause before test if option set.
31  * 	(See the parse_opts(3) man page).
32  *
33  *    DESCRIPTION
34  *     This is a Phase I test for the timer_create(2) system call.
35  *     It is intended to provide a limited exposure of the system call.
36  *
37  * 	Setup:
38  *	  Setup signal handling.
39  *	  Pause for SIGUSR1 if option specified.
40  *
41  * 	Test:
42  *	 Loop if the proper options are given.
43  *	 Execute system call with different notification types for
44  *	 clock ID CLOCK_REALTIME
45  *	 Check return code, if system call failed (return=-1)
46  *		Log the errno and Issue a FAIL message.
47  *	 Otherwise, Issue a PASS message.
48  *
49  * 	Cleanup:
50  * 	  Print errno log and/or timing stats if options given
51  *
52  * USAGE:  <for command-line>
53  * timer_create02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-p]
54  * where:
55  * 	-c n : Run n copies simultaneously.
56  *	-e   : Turn on errno logging.
57  *	-i n : Execute test n times.
58  *	-I x : Execute test for x seconds.
59  *	-p   : Pause for SIGUSR1 before starting
60  *	-P x : Pause for x seconds between iterations.
61  *	-t   : Turn on syscall timing.
62  *
63  *RESTRICTIONS:
64  * None
65  *****************************************************************************/
66 
67 #include <stdlib.h>
68 #include <errno.h>
69 #include <time.h>
70 #include <signal.h>
71 
72 #include "test.h"
73 #include "common_timers.h"
74 
75 void setup(void);
76 void setup_test(int option);
77 
78 char *TCID = "timer_create02";	/* Test program identifier.    */
79 int TST_TOTAL = 3;		/* Total number of test cases. */
80 static struct sigevent evp, *evp_ptr;
81 
main(int ac,char ** av)82 int main(int ac, char **av)
83 {
84 	int lc, i, j;
85 	kernel_timer_t created_timer_id;	/* holds the returned timer_id */
86 	char *message[3] = {
87 		"SIGEV_SIGNAL",
88 		"NULL",
89 		"SIGEV_NONE"
90 	};
91 	const char *mrstr = "MONOTONIC_RAW";
92 
93 	tst_parse_opts(ac, av, NULL, NULL);
94 
95 	setup();
96 
97 	for (lc = 0; TEST_LOOPING(lc); lc++) {
98 
99 		tst_count = 0;
100 
101 		for (i = 0; i < TST_TOTAL; i++) {
102 
103 			setup_test(i);
104 
105 			for (j = 0; j < CLOCKS_DEFINED; ++j) {
106 
107 				if (strstr(get_clock_str(clock_list[j]),
108 					   "CPUTIME_ID")) {
109 					/* (PROCESS_CPUTIME_ID &
110 					 *  THREAD_CPUTIME_ID)
111 					 * is not supported on kernel versions
112 					 * lower than 2.6.12
113 					 */
114 					if ((tst_kvercmp(2, 6, 12)) < 0) {
115 						continue;
116 					}
117 				}
118 				if (strstr(get_clock_str(clock_list[j]), mrstr))
119 					continue;
120 
121 				TEST(ltp_syscall(__NR_timer_create,
122 					clock_list[j], evp_ptr,
123 					&created_timer_id));
124 
125 				tst_resm((TEST_RETURN == 0 ?
126 					  TPASS :
127 					  TFAIL | TTERRNO),
128 					 "%s %s with notification type = %s",
129 					 get_clock_str(clock_list[j]),
130 					 (TEST_RETURN == 0 ?
131 					  "passed" : "failed"), message[i]);
132 			}
133 		}
134 	}
135 
136 	cleanup();
137 	tst_exit();
138 }
139 
140 /* setup_test() - sets up individual test */
setup_test(int option)141 void setup_test(int option)
142 {
143 	switch (option) {
144 	case 0:
145 		evp.sigev_value = (union sigval) 0;
146 		evp.sigev_signo = SIGALRM;
147 		evp.sigev_notify = SIGEV_SIGNAL;
148 		evp_ptr = &evp;
149 		break;
150 	case 1:
151 		evp_ptr = NULL;
152 		break;
153 	case 2:
154 		evp.sigev_value = (union sigval) 0;
155 		evp.sigev_signo = SIGALRM;	/* any will do */
156 		evp.sigev_notify = SIGEV_NONE;
157 		evp_ptr = &evp;
158 		break;
159 	}
160 }
161 
162 /* setup() - performs all ONE TIME setup for this test */
setup(void)163 void setup(void)
164 {
165 
166 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
167 
168 	TEST_PAUSE;
169 }
170 
171 /*
172  * cleanup() - Performs one time cleanup for this test at
173  * completion or premature exit
174  */
cleanup(void)175 void cleanup(void)
176 {
177 }
178