tst-pselect.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* Copyright (C) 2006-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <intprops.h>
  16. #include <support/check.h>
  17. #include <support/support.h>
  18. #include <support/xsignal.h>
  19. #include <support/xunistd.h>
  20. #include <support/xtime.h>
  21. #include <stdlib.h>
  22. static volatile int handler_called;
  23. static void
  24. handler (int sig)
  25. {
  26. handler_called = 1;
  27. }
  28. static void
  29. test_pselect_basic (void)
  30. {
  31. struct sigaction sa;
  32. sa.sa_handler = handler;
  33. sa.sa_flags = 0;
  34. sigemptyset (&sa.sa_mask);
  35. xsigaction (SIGUSR1, &sa, NULL);
  36. sa.sa_handler = SIG_IGN;
  37. xsigaction (SIGCHLD, &sa, NULL);
  38. sigset_t ss_usr1;
  39. sigemptyset (&ss_usr1);
  40. sigaddset (&ss_usr1, SIGUSR1);
  41. TEST_COMPARE (sigprocmask (SIG_BLOCK, &ss_usr1, NULL), 0);
  42. int fds[2][2];
  43. xpipe (fds[0]);
  44. xpipe (fds[1]);
  45. fd_set rfds;
  46. FD_ZERO (&rfds);
  47. sigset_t ss;
  48. TEST_COMPARE (sigprocmask (SIG_SETMASK, NULL, &ss), 0);
  49. sigdelset (&ss, SIGUSR1);
  50. struct timespec to = { .tv_sec = 0, .tv_nsec = 500000000 };
  51. pid_t parent = getpid ();
  52. pid_t p = xfork ();
  53. if (p == 0)
  54. {
  55. xclose (fds[0][1]);
  56. xclose (fds[1][0]);
  57. FD_SET (fds[0][0], &rfds);
  58. int e;
  59. do
  60. {
  61. if (getppid () != parent)
  62. FAIL_EXIT1 ("getppid()=%d != parent=%d", getppid(), parent);
  63. errno = 0;
  64. e = pselect (fds[0][0] + 1, &rfds, NULL, NULL, &to, &ss);
  65. }
  66. while (e == 0);
  67. TEST_COMPARE (e, -1);
  68. TEST_COMPARE (errno, EINTR);
  69. TEMP_FAILURE_RETRY (write (fds[1][1], "foo", 3));
  70. exit (0);
  71. }
  72. xclose (fds[0][0]);
  73. xclose (fds[1][1]);
  74. FD_SET (fds[1][0], &rfds);
  75. TEST_COMPARE (kill (p, SIGUSR1), 0);
  76. int e = pselect (fds[1][0] + 1, &rfds, NULL, NULL, NULL, &ss);
  77. TEST_COMPARE (e, 1);
  78. TEST_VERIFY (FD_ISSET (fds[1][0], &rfds));
  79. }
  80. static void
  81. test_pselect_large_timeout (void)
  82. {
  83. support_create_timer (0, 100000000, false, NULL);
  84. int fds[2];
  85. xpipe (fds);
  86. fd_set rfds;
  87. FD_ZERO (&rfds);
  88. FD_SET (fds[0], &rfds);
  89. sigset_t ss;
  90. TEST_COMPARE (sigprocmask (SIG_SETMASK, NULL, &ss), 0);
  91. sigdelset (&ss, SIGALRM);
  92. struct timespec ts = { TYPE_MAXIMUM (time_t), 0 };
  93. TEST_COMPARE (pselect (fds[0] + 1, &rfds, NULL, NULL, &ts, &ss), -1);
  94. TEST_VERIFY (errno == EINTR || errno == EOVERFLOW);
  95. }
  96. static int
  97. do_test (void)
  98. {
  99. test_pselect_basic ();
  100. test_pselect_large_timeout ();
  101. return 0;
  102. }
  103. #include <support/test-driver.c>