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_settime02
20 *
21 * EXECUTED BY : anyone
22 *
23 * TEST TITLE : Basic test for timer_settime(2)
24 *
25 * TEST CASE TOTAL : 4
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_settime(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 * setup individual test
44 * Execute system call
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_settime02 [-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 static int setup_test(int option);
77
78 char *TCID = "timer_settime02"; /* Test program identifier. */
79 int TST_TOTAL = 4; /* Total number of test cases. */
80
81 static struct itimerspec new_set, old_set, *old_temp;
82 static kernel_timer_t timer;
83 static int flag;
84
main(int ac,char ** av)85 int main(int ac, char **av)
86 {
87 int lc, i;
88
89 tst_parse_opts(ac, av, NULL, NULL);
90
91 setup();
92
93 for (lc = 0; TEST_LOOPING(lc); lc++) {
94
95 tst_count = 0;
96
97 for (i = 0; i < TST_TOTAL; i++) {
98
99 /* Set up individual test */
100 if (setup_test(i) == 0) {
101 TEST(ltp_syscall(__NR_timer_settime, timer,
102 flag, &new_set, old_temp));
103 tst_resm((TEST_RETURN == 0 ?
104 TPASS :
105 TFAIL | TTERRNO),
106 "%s",
107 (TEST_RETURN ==
108 0 ? "passed" : "failed")
109 );
110 }
111
112 }
113 }
114
115 cleanup();
116 tst_exit();
117 }
118
119 /* This function does set up for individual tests */
setup_test(int option)120 static int setup_test(int option)
121 {
122 struct timespec timenow; /* Used to obtain current time */
123 int rc = 0;
124
125 switch (option) {
126 case 0:
127 /* This is general initialization.
128 * make old_setting NULL
129 * make flags equal to zero
130 * use one-shot timer
131 */
132 old_temp = NULL;
133 new_set.it_interval.tv_sec = 0;
134 new_set.it_interval.tv_nsec = 0;
135 new_set.it_value.tv_sec = 5;
136 new_set.it_value.tv_nsec = 0;
137 flag = 0;
138 break;
139 case 1:
140 /* get the old_setting in old_set
141 * This test case also takes care
142 * of situation where the timerid is
143 * already armed
144 */
145 old_temp = &old_set;
146 break;
147 case 2:
148 /* Use the periodic timer */
149 new_set.it_interval.tv_sec = 5;
150 new_set.it_value.tv_sec = 0;
151 break;
152 case 3:
153 /* Use TIMER_ABSTIME flag for setting
154 * absolute time for timer
155 */
156 flag = TIMER_ABSTIME;
157 /*
158 * Let's not use the linux_syscall_number syscall(2)
159 * wrapper here because our primary test focus is
160 * timer_create, not clock_gettime. That's covered in
161 * those respective tests.
162 */
163 if (clock_gettime(CLOCK_REALTIME, &timenow) < 0) {
164 tst_resm(TWARN | TERRNO,
165 "clock_gettime failed; skipping the test");
166 rc = -1;
167 } else {
168 new_set.it_value.tv_sec = timenow.tv_sec + 25;
169 }
170 break;
171 }
172 return rc;
173 }
174
175 /* setup() - performs all ONE TIME setup for this test */
setup(void)176 void setup(void)
177 {
178
179 tst_sig(NOFORK, DEF_HANDLER, cleanup);
180
181 TEST_PAUSE;
182
183 if (ltp_syscall(__NR_timer_create, CLOCK_REALTIME, NULL, &timer) < 0)
184 tst_brkm(TBROK, NULL, "timer_create failed");
185 }
186
187 /*
188 * cleanup() - Performs one time cleanup for this test at
189 * completion or premature exit
190 */
cleanup(void)191 void cleanup(void)
192 {
193 }
194