• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   // Invalid timer returns EINVAL.
41   assert(sys_setitimer(kInvalidTimer, NULL, NULL) == -1);
42   assert(errno == EINVAL);
43 
44   const int kSignal = SIGALRM;
45   const size_t kSigsetSize = sizeof(struct kernel_sigset_t);
46 
47   // Block SIGALRM.
48   struct kernel_sigset_t sigalarm_only;
49   struct kernel_sigset_t old_sigset;
50   assert(sys_sigemptyset(&sigalarm_only) == 0);
51   assert(sys_sigaddset(&sigalarm_only, kSignal) == 0);
52   assert(sys_rt_sigprocmask(SIG_BLOCK, &sigalarm_only, &old_sigset,
53                             kSigsetSize) == 0);
54 
55   // Set up a real time timer.
56   struct kernel_itimerval new_itimer = {};
57   const long kIntervalUSec = 123;
58   new_itimer.it_interval.tv_sec = 0;
59   new_itimer.it_interval.tv_usec = kIntervalUSec;
60   new_itimer.it_value = new_itimer.it_interval;
61   assert(sys_setitimer(ITIMER_REAL, &new_itimer, NULL) == 0);
62 
63   // Wait for alarm.
64   struct timespec timeout;
65   const unsigned long kNanoSecsPerSec = 1000000000;
66   const unsigned long kNanoSecsPerMicroSec = 1000;
67 
68   // Use a timeout 3 times of the timer interval.
69   unsigned long duration_ns = kIntervalUSec * kNanoSecsPerMicroSec * 3;
70   timeout.tv_sec = duration_ns / kNanoSecsPerSec ;
71   timeout.tv_nsec = duration_ns % kNanoSecsPerSec;
72 
73   int sig;
74   do {
75     sig = sys_sigtimedwait(&sigalarm_only, NULL, &timeout);
76   } while (sig == -1 && errno == EINTR);
77   assert(sig == kSignal);
78 
79   // Disable timer, check saving of old timer value.
80   struct kernel_itimerval empty_itimer = {};
81   struct kernel_itimerval old_itimer;
82   empty_itimer.it_interval.tv_sec = 0;
83   empty_itimer.it_interval.tv_usec = 0;
84   empty_itimer.it_value = empty_itimer.it_interval;
85 
86   assert(sys_setitimer(ITIMER_REAL, &empty_itimer, &old_itimer) == 0);
87   assert(kernel_timeval_eq(&old_itimer.it_interval, &new_itimer.it_interval));
88 
89   return 0;
90 }
91