1 /* Test for settimeofday
2    Copyright (C) 2021-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <time.h>
20 #include <stdlib.h>
21 #include <sys/time.h>
22 #include <support/check.h>
23 #include <support/xtime.h>
24 
25 #define TIMESPEC_SEC_Y2038_OV 0x7FFFFFFF
26 #define FUTURE_TIME (TIMESPEC_SEC_Y2038_OV - 10)
27 
28 static int
do_test(void)29 do_test (void)
30 {
31   const struct timeval tv = { FUTURE_TIME, 0};
32   struct timespec tv_future, tv_now;
33 
34   /* Check if altering target time is allowed.  */
35   if (getenv (SETTIME_ENV_NAME) == NULL)
36     FAIL_UNSUPPORTED ("settimeofday is executed only when "\
37                       SETTIME_ENV_NAME" is set\n");
38 
39   tv_now = xclock_now (CLOCK_REALTIME);
40   int ret = settimeofday (&tv, NULL);
41   if (ret == -1)
42     FAIL_EXIT1 ("settimeofday failed: %m\n");
43 
44   tv_future = xclock_now (CLOCK_REALTIME);
45 
46   /* Restore old time value on target machine.  */
47   xclock_settime (CLOCK_REALTIME, (const struct timespec*) &tv_now);
48 
49   if (tv_future.tv_sec < tv.tv_sec)
50     FAIL_EXIT1 ("settimeofday set wrong time!\n");
51 
52   return 0;
53 }
54 
55 #include <support/test-driver.c>
56