tst-shm-cancel.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* Test for shm_open cancellation handling: BZ #18243.
  2. Copyright (C) 2016-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 <pthread.h>
  16. #include <sys/mman.h>
  17. #include <semaphore.h>
  18. #include <stdio.h>
  19. #include <fcntl.h>
  20. #include <unistd.h>
  21. #include <errno.h>
  22. #include <stdlib.h>
  23. static sem_t sem; /* Use to sync with thread start. */
  24. static char shm_name[sizeof "/glibc-shm_open-cancel-" + sizeof (pid_t) * 3];
  25. static void
  26. init_shm_name (void)
  27. {
  28. snprintf (shm_name, sizeof (shm_name), "/glibc-shm_open-cancel-%u",
  29. getpid ());
  30. }
  31. static void
  32. remove_shm (int status, void *arg)
  33. {
  34. shm_unlink (shm_name);
  35. }
  36. static void *
  37. tf (void *arg)
  38. {
  39. pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, 0);
  40. if (sem_wait (&sem) != 0)
  41. {
  42. printf ("error: sem_wait failed: %m");
  43. exit (1);
  44. }
  45. if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, 0) != 0)
  46. {
  47. printf ("error: pthread_setcancelstate failed: %m");
  48. exit (1);
  49. }
  50. /* Neither sem_unlink or sem_open should act on thread cancellation. */
  51. shm_unlink (shm_name);
  52. on_exit (remove_shm, NULL);
  53. int fd = shm_open (shm_name, O_CREAT, 0600);
  54. if (fd == -1)
  55. {
  56. int exit_code;
  57. if (errno == ENOSYS || errno == EACCES)
  58. exit_code = 77;
  59. else
  60. exit_code = 1;
  61. exit (exit_code);
  62. }
  63. if (pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, 0) != 0)
  64. {
  65. printf ("error: pthread_setcancelstate failed: %m");
  66. exit (1);
  67. }
  68. if (close (fd) != 0)
  69. {
  70. printf ("error: pthread_setcancelstate failed: %m");
  71. exit (1);
  72. }
  73. return NULL;
  74. }
  75. static int
  76. do_test (void)
  77. {
  78. pthread_t td;
  79. init_shm_name ();
  80. if (sem_init (&sem, 0, 0))
  81. {
  82. printf ("error: sem_init failed: %m\n");
  83. exit (1);
  84. }
  85. if (pthread_create (&td, NULL, tf, NULL) != 0)
  86. {
  87. printf ("error: pthread_create failed: %m\n");
  88. exit (1);
  89. }
  90. if (pthread_cancel (td) != 0)
  91. {
  92. printf ("error: pthread_cancel failed: %m\n");
  93. exit (1);
  94. }
  95. if (sem_post (&sem) != 0)
  96. {
  97. printf ("error: sem_post failed: %m\n");
  98. exit (1);
  99. }
  100. void *r;
  101. if (pthread_join (td, &r) != 0)
  102. {
  103. printf ("error: pthread_join failed: %m\n");
  104. exit (1);
  105. }
  106. if (r == PTHREAD_CANCELED)
  107. {
  108. puts ("error: pthread_join returned PTHREAD_CANCELED");
  109. exit (1);
  110. }
  111. return 0;
  112. }
  113. #define TEST_FUNCTION do_test ()
  114. #include <test-skeleton.c>