tst-sem7.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. sem_t *s;
  30. sem_t *s2;
  31. sem_t *s3;
  32. s = sem_open ("/glibc-tst-sem7", O_CREAT, 0600, 1);
  33. if (s == SEM_FAILED)
  34. {
  35. if (errno == ENOSYS)
  36. {
  37. puts ("sem_open not supported. Oh well.");
  38. return 0;
  39. }
  40. /* Maybe the shm filesystem has strict permissions. */
  41. if (errno == EACCES)
  42. {
  43. puts ("sem_open not allowed. Oh well.");
  44. return 0;
  45. }
  46. printf ("sem_open: %m\n");
  47. return 1;
  48. }
  49. on_exit (remove_sem, (void *) "/glibc-tst-sem7");
  50. /* We have the semaphore object. Now try again. We should get the
  51. same address. */
  52. s2 = sem_open ("/glibc-tst-sem7", O_CREAT, 0600, 1);
  53. if (s2 == SEM_FAILED)
  54. {
  55. puts ("2nd sem_open failed");
  56. return 1;
  57. }
  58. if (s != s2)
  59. {
  60. puts ("2nd sem_open didn't return the same address");
  61. return 1;
  62. }
  63. /* And again, this time without O_CREAT. */
  64. s3 = sem_open ("/glibc-tst-sem7", 0);
  65. if (s3 == SEM_FAILED)
  66. {
  67. puts ("3rd sem_open failed");
  68. return 1;
  69. }
  70. if (s != s3)
  71. {
  72. puts ("3rd sem_open didn't return the same address");
  73. return 1;
  74. }
  75. /* Now close the handle. Three times. */
  76. if (sem_close (s2) != 0)
  77. {
  78. puts ("1st sem_close failed");
  79. return 1;
  80. }
  81. if (sem_close (s) != 0)
  82. {
  83. puts ("2nd sem_close failed");
  84. return 1;
  85. }
  86. if (sem_close (s3) != 0)
  87. {
  88. puts ("3rd sem_close failed");
  89. return 1;
  90. }
  91. return 0;
  92. }