tst-sem7.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Copyright (C) 2003 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
  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. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <fcntl.h>
  17. #include <semaphore.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. static void
  22. remove_sem (int status, void *arg)
  23. {
  24. sem_unlink (arg);
  25. }
  26. int
  27. main (void)
  28. {
  29. #if defined(__GLIBC__) || defined(__UCLIBC__)
  30. sem_t *s;
  31. sem_t *s2;
  32. sem_t *s3;
  33. s = sem_open ("/glibc-tst-sem7", O_CREAT, 0600, 1);
  34. if (s == SEM_FAILED)
  35. {
  36. if (errno == ENOSYS)
  37. {
  38. puts ("sem_open not supported. Oh well.");
  39. return 0;
  40. }
  41. /* Maybe the shm filesystem has strict permissions. */
  42. if (errno == EACCES)
  43. {
  44. puts ("sem_open not allowed. Oh well.");
  45. return 0;
  46. }
  47. printf ("sem_open: %m\n");
  48. return 1;
  49. }
  50. on_exit (remove_sem, (void *) "/glibc-tst-sem7");
  51. /* We have the semaphore object. Now try again. We should get the
  52. same address. */
  53. s2 = sem_open ("/glibc-tst-sem7", O_CREAT, 0600, 1);
  54. if (s2 == SEM_FAILED)
  55. {
  56. puts ("2nd sem_open failed");
  57. return 1;
  58. }
  59. if (s != s2)
  60. {
  61. puts ("2nd sem_open didn't return the same address");
  62. return 1;
  63. }
  64. /* And again, this time without O_CREAT. */
  65. s3 = sem_open ("/glibc-tst-sem7", 0);
  66. if (s3 == SEM_FAILED)
  67. {
  68. puts ("3rd sem_open failed");
  69. return 1;
  70. }
  71. if (s != s3)
  72. {
  73. puts ("3rd sem_open didn't return the same address");
  74. return 1;
  75. }
  76. /* Now close the handle. Three times. */
  77. if (sem_close (s2) != 0)
  78. {
  79. puts ("1st sem_close failed");
  80. return 1;
  81. }
  82. if (sem_close (s) != 0)
  83. {
  84. puts ("2nd sem_close failed");
  85. return 1;
  86. }
  87. if (sem_close (s3) != 0)
  88. {
  89. puts ("3rd sem_close failed");
  90. return 1;
  91. }
  92. return 0;
  93. #else
  94. return 23;
  95. #endif
  96. }