tst-settimeofday.c 1.7 KB

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