1 /* Test for shm_open cancellation handling: BZ #18243.
2 Copyright (C) 2016-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
21 #include <semaphore.h>
28 static sem_t sem
; /* Use to sync with thread start. */
29 static const char shm_name
[] = "/glibc-shm_open-cancel";
32 remove_shm (int status
, void *arg
)
34 shm_unlink (shm_name
);
40 pthread_setcancelstate (PTHREAD_CANCEL_DISABLE
, 0);
42 if (sem_wait (&sem
) != 0)
44 printf ("error: sem_wait failed: %m");
48 if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE
, 0) != 0)
50 printf ("error: pthread_setcancelstate failed: %m");
54 /* Neither sem_unlink or sem_open should act on thread cancellation. */
55 shm_unlink (shm_name
);
56 on_exit (remove_shm
, NULL
);
58 int fd
= shm_open (shm_name
, O_CREAT
, 0600);
62 if (errno
== ENOSYS
|| errno
== EACCES
)
69 if (pthread_setcancelstate (PTHREAD_CANCEL_DISABLE
, 0) != 0)
71 printf ("error: pthread_setcancelstate failed: %m");
77 printf ("error: pthread_setcancelstate failed: %m");
89 if (sem_init (&sem
, 0, 0))
91 printf ("error: sem_init failed: %m\n");
95 if (pthread_create (&td
, NULL
, tf
, NULL
) != 0)
97 printf ("error: pthread_create failed: %m\n");
101 if (pthread_cancel (td
) != 0)
103 printf ("error: pthread_cancel failed: %m\n");
107 if (sem_post (&sem
) != 0)
109 printf ("error: sem_post failed: %m\n");
114 if (pthread_join (td
, &r
) != 0)
116 printf ("error: pthread_join failed: %m\n");
120 if (r
== PTHREAD_CANCELED
)
122 puts ("error: pthread_join returned PTHREAD_CANCELED");
129 #define TEST_FUNCTION do_test ()
130 #include <test-skeleton.c>