1  /* Test for sigtimedwait timeout.
       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 <intprops.h>
      21  #include <errno.h>
      22  #include <signal.h>
      23  #include <support/check.h>
      24  #include <support/xtime.h>
      25  #include <support/timespec.h>
      26  #include <support/support.h>
      27  #include <stdbool.h>
      28  
      29  static int
      30  test_sigtimedwait_timeout (bool zero_tmo)
      31  {
      32    /* We wait for half a second.  */
      33    struct timespec ts;
      34    xclock_gettime (CLOCK_REALTIME, &ts);
      35    struct timespec timeout = make_timespec (0, zero_tmo ? 0 : TIMESPEC_HZ/2);
      36    ts = timespec_add (ts, timeout);
      37  
      38    /* Set sigset to just wait for timeout.  */
      39    sigset_t ss_usr1;
      40    sigemptyset (&ss_usr1);
      41    sigaddset (&ss_usr1, SIGUSR1);
      42  
      43    int ret = sigtimedwait (&ss_usr1, NULL, &timeout);
      44    if (ret != -1)
      45      FAIL_EXIT1 ("sigtimedwait failed: %m\n");
      46  
      47    TEST_TIMESPEC_NOW_OR_AFTER (CLOCK_REALTIME, ts);
      48  
      49    return 0;
      50  }
      51  
      52  static void
      53  test_sigtimedwait_large_timeout (void)
      54  {
      55    support_create_timer (0, 100000000, false, NULL);
      56    struct timespec ts = { TYPE_MAXIMUM (time_t), 0 };
      57  
      58    sigset_t ss_usr1;
      59    sigemptyset (&ss_usr1);
      60    sigaddset (&ss_usr1, SIGUSR1);
      61  
      62    TEST_COMPARE (sigtimedwait (&ss_usr1, NULL, &ts), -1);
      63    TEST_VERIFY (errno == EINTR || errno == EOVERFLOW);
      64  }
      65  
      66  static int
      67  do_test (void)
      68  {
      69    /* Check if sigtimedwait exits immediately.  */
      70    test_sigtimedwait_timeout (true);
      71  
      72    /* Check if sigtimedwait exits after specified timeout.  */
      73    test_sigtimedwait_timeout (false);
      74  
      75    test_sigtimedwait_large_timeout ();
      76  
      77    return 0;
      78  }
      79  
      80  #include <support/test-driver.c>