Add getitimer and setitimer syscalls

Change-Id: Id26acbfbeed4684d85503724f1a45b47571b1a52
Reviewed-on: https://chromium-review.googlesource.com/c/linux-syscall-support/+/3739366
Reviewed-by: Mike Frysinger <vapier@chromium.org>
diff --git a/linux_syscall_support.h b/linux_syscall_support.h
index f73e418..75d3174 100644
--- a/linux_syscall_support.h
+++ b/linux_syscall_support.h
@@ -267,6 +267,12 @@
   long               tv_usec;
 };
 
+/* include/linux/time.h                                                      */
+struct kernel_itimerval {
+  struct kernel_timeval it_interval;
+  struct kernel_timeval it_value;
+};
+
 /* include/linux/resource.h                                                  */
 struct kernel_rusage {
   struct kernel_timeval ru_utime;
@@ -3844,6 +3850,8 @@
                       struct kernel_dirent64*, d, int,    c)
   LSS_INLINE _syscall0(gid_t,   getegid)
   LSS_INLINE _syscall0(uid_t,   geteuid)
+  LSS_INLINE _syscall2(int,     getitimer,       int,        w,
+                       struct kernel_itimerval*, c)
   #if defined(__NR_getpgrp)
     LSS_INLINE _syscall0(pid_t,   getpgrp)
   #endif
@@ -3965,6 +3973,9 @@
   LSS_INLINE _syscall1(int,     setfsuid,        uid_t,       u)
   LSS_INLINE _syscall1(int,     setuid,          uid_t,       u)
   LSS_INLINE _syscall1(int,     setgid,          gid_t,       g)
+  LSS_INLINE _syscall3(int,     setitimer,       int,         w,
+                       const struct kernel_itimerval*,        n,
+                       struct kernel_itimerval*, o)
   LSS_INLINE _syscall2(int,     setpgid,         pid_t,       p,
                        pid_t,          g)
   LSS_INLINE _syscall3(int,     setpriority,     int,         a,
diff --git a/tests/Makefile b/tests/Makefile
index a6f1ec6..7b18ed2 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -41,8 +41,10 @@
 
 TESTS = \
 	fallocate \
+	getitimer \
 	getrandom \
 	lstat \
+	setitimer \
 	sigaction \
 	sigtimedwait \
 	stat \
diff --git a/tests/getitimer.c b/tests/getitimer.c
new file mode 100644
index 0000000..507948f
--- /dev/null
+++ b/tests/getitimer.c
@@ -0,0 +1,84 @@
+/* Copyright 2022, Google Inc.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ *     * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "test_skel.h"
+
+int main(int argc, char *argv[]) {
+  // We need an invalid timer value. The assert()'s below should
+  // be static asserts but it is not avalible in older C versions.
+#define kInvalidTimer 9999
+  assert(kInvalidTimer != ITIMER_REAL);
+  assert(kInvalidTimer != ITIMER_VIRTUAL);
+  assert(kInvalidTimer != ITIMER_PROF);
+
+  // This should fail with EINVAL.
+  struct kernel_itimerval curr_itimer;
+  assert(sys_getitimer(kInvalidTimer, &curr_itimer) == -1);
+  assert(errno == EINVAL);
+
+  // Create a read-only page.
+  size_t page_size = getpagesize();
+  void* read_only_page = sys_mmap(NULL, page_size, PROT_READ,
+                                  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+  assert(read_only_page != MAP_FAILED);
+
+  // This should fail with EFAULT.
+  assert(sys_getitimer(ITIMER_REAL,
+                       (struct kernel_itimerval*) read_only_page) == -1);
+  assert(errno == EFAULT);
+
+  // This should complete without an error.
+  assert(sys_getitimer(ITIMER_REAL, &curr_itimer) == 0);
+
+  // Set up a real time timer with very long interval and value so that
+  // we do not need to handle SIGALARM in test.
+  struct kernel_itimerval new_itimer;
+  const time_t kIntervalSec = 60 * 60 * 24 * 365;  // One year.
+  const long kIntervalUSec = 123;
+  new_itimer.it_interval.tv_sec = kIntervalSec;
+  new_itimer.it_interval.tv_usec = kIntervalUSec;
+  new_itimer.it_value = new_itimer.it_interval;
+  assert(sys_setitimer(ITIMER_REAL, &new_itimer, NULL) == 0);
+
+  assert(sys_getitimer(ITIMER_REAL, &curr_itimer) == 0);
+  assert(kernel_timeval_eq(&curr_itimer.it_interval, &new_itimer.it_interval));
+
+  // Disable timer.
+  struct kernel_itimerval empty_itimer;
+  empty_itimer.it_interval.tv_sec = 0;
+  empty_itimer.it_interval.tv_usec = 0;
+  empty_itimer.it_value = empty_itimer.it_interval;
+  assert(sys_setitimer(ITIMER_REAL, &empty_itimer, NULL) == 0);
+
+  // We should read back an empty itimer.
+  assert(sys_getitimer(ITIMER_REAL, &curr_itimer) == 0);
+  assert(kernel_itimerval_eq(&curr_itimer, &empty_itimer));
+
+  return 0;
+}
diff --git a/tests/setitimer.c b/tests/setitimer.c
new file mode 100644
index 0000000..1bd7114
--- /dev/null
+++ b/tests/setitimer.c
@@ -0,0 +1,90 @@
+/* Copyright 2022, Google Inc.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ *     * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "test_skel.h"
+
+int main(int argc, char *argv[]) {
+  // We need an invalid timer value. The assert()'s below should
+  // be static asserts but it is not avalible in older C versions.
+#define kInvalidTimer 9999
+  assert(kInvalidTimer != ITIMER_REAL);
+  assert(kInvalidTimer != ITIMER_VIRTUAL);
+  assert(kInvalidTimer != ITIMER_PROF);
+
+  // Invalid timer returns EINVAL.
+  assert(sys_setitimer(kInvalidTimer, NULL, NULL) == -1);
+  assert(errno == EINVAL);
+
+  const int kSignal = SIGALRM;
+  const size_t kSigsetSize = sizeof(struct kernel_sigset_t);
+
+  // Block SIGALRM.
+  struct kernel_sigset_t sigalarm_only;
+  struct kernel_sigset_t old_sigset;
+  assert(sys_sigemptyset(&sigalarm_only) == 0);
+  assert(sys_sigaddset(&sigalarm_only, kSignal) == 0);
+  assert(sys_rt_sigprocmask(SIG_BLOCK, &sigalarm_only, &old_sigset,
+                            kSigsetSize) == 0);
+
+  // Set up a real time timer.
+  struct kernel_itimerval new_itimer = {};
+  const long kIntervalUSec = 123;
+  new_itimer.it_interval.tv_sec = 0;
+  new_itimer.it_interval.tv_usec = kIntervalUSec;
+  new_itimer.it_value = new_itimer.it_interval;
+  assert(sys_setitimer(ITIMER_REAL, &new_itimer, NULL) == 0);
+
+  // Wait for alarm.
+  struct timespec timeout;
+  const unsigned long kNanoSecsPerSec = 1000000000;
+  const unsigned long kNanoSecsPerMicroSec = 1000;
+
+  // Use a timeout 3 times of the timer interval.
+  unsigned long duration_ns = kIntervalUSec * kNanoSecsPerMicroSec * 3;
+  timeout.tv_sec = duration_ns / kNanoSecsPerSec ;
+  timeout.tv_nsec = duration_ns % kNanoSecsPerSec;
+
+  int sig;
+  do {
+    sig = sys_sigtimedwait(&sigalarm_only, NULL, &timeout);
+  } while (sig == -1 && errno == EINTR);
+  assert(sig == kSignal);
+
+  // Disable timer, check saving of old timer value.
+  struct kernel_itimerval empty_itimer = {};
+  struct kernel_itimerval old_itimer;
+  empty_itimer.it_interval.tv_sec = 0;
+  empty_itimer.it_interval.tv_usec = 0;
+  empty_itimer.it_value = empty_itimer.it_interval;
+
+  assert(sys_setitimer(ITIMER_REAL, &empty_itimer, &old_itimer) == 0);
+  assert(kernel_timeval_eq(&old_itimer.it_interval, &new_itimer.it_interval));
+
+  return 0;
+}
diff --git a/tests/test_skel.h b/tests/test_skel.h
index 238c48d..024906c 100644
--- a/tests/test_skel.h
+++ b/tests/test_skel.h
@@ -54,6 +54,7 @@
 #include <sys/stat.h>
 #include <sys/vfs.h>
 #include <sys/wait.h>
+#include <unistd.h>
 
 #include <linux/capability.h>
 
@@ -72,3 +73,17 @@
   }
 }
 #define assert_buffers_eq(obj1, obj2) assert_buffers_eq_len(obj1, obj2, sizeof(*obj1))
+
+// Returns true iff pointed timevals are equal.
+static inline bool kernel_timeval_eq(const struct kernel_timeval* lhs,
+                                     const struct kernel_timeval* rhs) {
+  return (lhs->tv_sec == rhs->tv_sec) && (lhs->tv_usec == rhs->tv_usec);
+}
+
+// Returns true iff pointed itimervals are equal.
+static inline bool kernel_itimerval_eq(const struct kernel_itimerval* lhs,
+                                       const struct kernel_itimerval* rhs) {
+  return kernel_timeval_eq(&lhs->it_interval, &rhs->it_interval) &&
+         kernel_timeval_eq(&lhs->it_value, &rhs->it_value);
+}
+