tst-pselect.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* Copyright (C) 2006-2019 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. <http://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <signal.h>
  16. #include <stdio.h>
  17. #include <unistd.h>
  18. #include <sys/select.h>
  19. #include <sys/wait.h>
  20. #include <stdlib.h>
  21. static volatile int handler_called;
  22. static void
  23. handler (int sig)
  24. {
  25. handler_called = 1;
  26. }
  27. static int
  28. do_test (void)
  29. {
  30. struct sigaction sa;
  31. sa.sa_handler = handler;
  32. sa.sa_flags = 0;
  33. sigemptyset (&sa.sa_mask);
  34. if (sigaction (SIGUSR1, &sa, NULL) != 0)
  35. {
  36. puts ("sigaction failed");
  37. return 1;
  38. }
  39. sa.sa_handler = SIG_IGN;
  40. if (sigaction (SIGCHLD, &sa, NULL) != 0)
  41. {
  42. puts ("2nd sigaction failed");
  43. return 1;
  44. }
  45. sigset_t ss_usr1;
  46. sigemptyset (&ss_usr1);
  47. sigaddset (&ss_usr1, SIGUSR1);
  48. if (sigprocmask (SIG_BLOCK, &ss_usr1, NULL) != 0)
  49. {
  50. puts ("sigprocmask failed");
  51. return 1;
  52. }
  53. int fds[2][2];
  54. if (pipe (fds[0]) != 0 || pipe (fds[1]) != 0)
  55. {
  56. puts ("pipe failed");
  57. return 1;
  58. }
  59. fd_set rfds;
  60. FD_ZERO (&rfds);
  61. sigset_t ss;
  62. sigprocmask (SIG_SETMASK, NULL, &ss);
  63. sigdelset (&ss, SIGUSR1);
  64. struct timespec to = { .tv_sec = 0, .tv_nsec = 500000000 };
  65. pid_t parent = getpid ();
  66. pid_t p = fork ();
  67. if (p == 0)
  68. {
  69. close (fds[0][1]);
  70. close (fds[1][0]);
  71. FD_SET (fds[0][0], &rfds);
  72. int e;
  73. do
  74. {
  75. if (getppid () != parent)
  76. exit (2);
  77. errno = 0;
  78. e = pselect (fds[0][0] + 1, &rfds, NULL, NULL, &to, &ss);
  79. }
  80. while (e == 0);
  81. if (e != -1)
  82. {
  83. puts ("child: pselect did not fail");
  84. return 0;
  85. }
  86. if (errno != EINTR)
  87. {
  88. puts ("child: pselect did not set errno to EINTR");
  89. return 0;
  90. }
  91. TEMP_FAILURE_RETRY (write (fds[1][1], "foo", 3));
  92. exit (0);
  93. }
  94. close (fds[0][0]);
  95. close (fds[1][1]);
  96. FD_SET (fds[1][0], &rfds);
  97. kill (p, SIGUSR1);
  98. int e = pselect (fds[1][0] + 1, &rfds, NULL, NULL, NULL, &ss);
  99. if (e == -1)
  100. {
  101. puts ("parent: pselect failed");
  102. return 1;
  103. }
  104. if (e != 1)
  105. {
  106. puts ("parent: pselect did not report readable fd");
  107. return 1;
  108. }
  109. if (!FD_ISSET (fds[1][0], &rfds))
  110. {
  111. puts ("parent: pselect reports wrong fd");
  112. return 1;
  113. }
  114. return 0;
  115. }
  116. #define TEST_FUNCTION do_test ()
  117. #include "../test-skeleton.c"