1  /* Test for clock_adjtime
       2     Copyright (C) 2021-2023 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/timespec.h>
      24  
      25  #ifndef ADJTIME_CALL
      26  # define ADJTIME_CALL(__clock, __timex) clock_adjtime (__clock, __timex)
      27  #endif
      28  
      29  static int
      30  do_test (void)
      31  {
      32    struct timespec tv_then, tv_now;
      33    struct timex delta;
      34  
      35    /* Check if altering target time is allowed.  */
      36    if (getenv (SETTIME_ENV_NAME) == NULL)
      37      FAIL_UNSUPPORTED ("clock_adjtime is executed only when "\
      38                        SETTIME_ENV_NAME" is set\n");
      39  
      40    tv_then = xclock_now (CLOCK_REALTIME);
      41  
      42    /* Setup time value to adjust - 1 sec. */
      43    delta.time.tv_sec = 1;
      44    delta.time.tv_usec = 0;
      45    delta.modes = ADJ_SETOFFSET;
      46  
      47    int ret = ADJTIME_CALL (CLOCK_REALTIME, &delta);
      48    if (ret == -1)
      49      FAIL_EXIT1 ("clock_adjtime failed: %m\n");
      50  
      51    tv_now = xclock_now (CLOCK_REALTIME);
      52  
      53    /* Check if clock_adjtime adjusted the system time.  */
      54    struct timespec r = timespec_sub (tv_now, tv_then);
      55    TEST_COMPARE (support_timespec_check_in_range
      56                  ((struct timespec) { 1, 0 }, r, 0.9, 1.1), 1);
      57  
      58    return 0;
      59  }
      60  
      61  #include <support/test-driver.c>