1 /* Copyright 2022, Google Inc. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 *
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following disclaimer
11 * in the documentation and/or other materials provided with the
12 * distribution.
13 * * Neither the name of Google Inc. nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include "test_skel.h"
31
main(int argc,char * argv[])32 int main(int argc, char *argv[]) {
33 // We need an invalid timer value. The assert()'s below should
34 // be static asserts but it is not avalible in older C versions.
35 #define kInvalidTimer 9999
36 assert(kInvalidTimer != ITIMER_REAL);
37 assert(kInvalidTimer != ITIMER_VIRTUAL);
38 assert(kInvalidTimer != ITIMER_PROF);
39
40 // This should fail with EINVAL.
41 struct kernel_itimerval curr_itimer;
42 assert(sys_getitimer(kInvalidTimer, &curr_itimer) == -1);
43 assert(errno == EINVAL);
44
45 // Create a read-only page.
46 size_t page_size = getpagesize();
47 void* read_only_page = sys_mmap(NULL, page_size, PROT_READ,
48 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
49 assert(read_only_page != MAP_FAILED);
50
51 // This should fail with EFAULT.
52 assert(sys_getitimer(ITIMER_REAL,
53 (struct kernel_itimerval*) read_only_page) == -1);
54 assert(errno == EFAULT);
55
56 // This should complete without an error.
57 assert(sys_getitimer(ITIMER_REAL, &curr_itimer) == 0);
58
59 // Set up a real time timer with very long interval and value so that
60 // we do not need to handle SIGALARM in test.
61 struct kernel_itimerval new_itimer;
62 const time_t kIntervalSec = 60 * 60 * 24 * 365; // One year.
63 const long kIntervalUSec = 123;
64 new_itimer.it_interval.tv_sec = kIntervalSec;
65 new_itimer.it_interval.tv_usec = kIntervalUSec;
66 new_itimer.it_value = new_itimer.it_interval;
67 assert(sys_setitimer(ITIMER_REAL, &new_itimer, NULL) == 0);
68
69 assert(sys_getitimer(ITIMER_REAL, &curr_itimer) == 0);
70 assert(kernel_timeval_eq(&curr_itimer.it_interval, &new_itimer.it_interval));
71
72 // Disable timer.
73 struct kernel_itimerval empty_itimer;
74 empty_itimer.it_interval.tv_sec = 0;
75 empty_itimer.it_interval.tv_usec = 0;
76 empty_itimer.it_value = empty_itimer.it_interval;
77 assert(sys_setitimer(ITIMER_REAL, &empty_itimer, NULL) == 0);
78
79 // We should read back an empty itimer.
80 assert(sys_getitimer(ITIMER_REAL, &curr_itimer) == 0);
81 assert(kernel_itimerval_eq(&curr_itimer, &empty_itimer));
82
83 return 0;
84 }
85