]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/pthread/tst-sem4.c
1 /* Copyright (C) 2002-2021 Free Software Foundation, Inc.
2 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.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
20 #include <semaphore.h>
27 remove_sem (int status
, void *arg
)
41 /* Start with a clean slate and register a clean-up action. No need to
42 act if sem_unlink fails because we will catch the same problem during the
44 sem_unlink ("/glibc-tst-sem4");
45 on_exit (remove_sem
, (void *) "/glibc-tst-sem4");
47 s
= sem_open ("/glibc-tst-sem4", O_CREAT
, 0600, 1);
52 puts ("sem_open not supported. Oh well.");
56 /* Maybe the shm filesystem has strict permissions. */
59 puts ("sem_open not allowed. Oh well.");
63 printf ("sem_open: %m\n");
67 /* We have the semaphore object. Now try again with O_EXCL, this
69 s2
= sem_open ("/glibc-tst-sem4", O_CREAT
| O_EXCL
, 0600, 1);
72 puts ("2nd sem_open didn't fail");
77 puts ("2nd sem_open returned wrong error");
81 /* Check the value. */
82 if (sem_getvalue (s
, &val
) == -1)
84 puts ("getvalue failed");
89 printf ("initial value wrong: got %d, expected 1\n", val
);
93 if (TEMP_FAILURE_RETRY (sem_wait (s
)) == -1)
95 puts ("1st sem_wait failed");
102 printf ("fork failed: %m\n");
110 /* Check the value. */
111 if (sem_getvalue (s
, &val
) == -1)
113 puts ("child: getvalue failed");
118 printf ("child: value wrong: got %d, expect 0\n", val
);
122 if (sem_post (s
) == -1)
124 puts ("child: post failed");
130 if (TEMP_FAILURE_RETRY (sem_wait (s
)) == -1)
132 puts ("2nd sem_wait failed");
136 if (sem_getvalue (s
, &val
) == -1)
138 puts ("parent: 2nd getvalue failed");
143 printf ("parent: value wrong: got %d, expected 0\n", val
);
151 #define TEST_FUNCTION do_test ()
152 #include "../test-skeleton.c"