test-sysvsem.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* Basic tests for SYSV semaphore functions.
  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 <intprops.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <errno.h>
  19. #include <string.h>
  20. #include <stdbool.h>
  21. #include <sys/types.h>
  22. #include <sys/ipc.h>
  23. #include <sys/sem.h>
  24. #include <test-sysvipc.h>
  25. #include <support/support.h>
  26. #include <support/check.h>
  27. #include <support/temp_file.h>
  28. #include <support/xtime.h>
  29. #include <support/xsignal.h>
  30. /* These are for the temporary file we generate. */
  31. static char *name;
  32. static int semid;
  33. static void
  34. remove_sem (void)
  35. {
  36. /* Enforce message queue removal in case of early test failure.
  37. Ignore error since the sem may already have being removed. */
  38. semctl (semid, 0, IPC_RMID, 0);
  39. }
  40. static void
  41. do_prepare (int argc, char *argv[])
  42. {
  43. int fd = create_temp_file ("tst-sysvsem.", &name);
  44. if (fd == -1)
  45. FAIL_EXIT1 ("cannot create temporary file (errno=%d)", errno);
  46. }
  47. #define PREPARE do_prepare
  48. /* It is not an extensive test, but rather a functional one aimed to check
  49. correct parameter passing on kernel. */
  50. #define SEM_MODE 0644
  51. union semun
  52. {
  53. int val;
  54. struct semid_ds *buf;
  55. unsigned short *array;
  56. };
  57. static int
  58. do_test (void)
  59. {
  60. atexit (remove_sem);
  61. key_t key = ftok (name, 'G');
  62. if (key == -1)
  63. FAIL_EXIT1 ("ftok failed");
  64. semid = semget(key, 1, IPC_CREAT | IPC_EXCL | SEM_MODE);
  65. if (semid == -1)
  66. {
  67. if (errno == ENOSYS)
  68. FAIL_UNSUPPORTED ("msgget not supported");
  69. FAIL_EXIT1 ("semget failed (errno=%d)", errno);
  70. }
  71. TEST_COMPARE (semctl (semid, 0, first_sem_invalid_cmd (), NULL), -1);
  72. TEST_COMPARE (errno, EINVAL);
  73. /* Get semaphore kernel information and do some sanity checks. */
  74. struct semid_ds seminfo;
  75. if (semctl (semid, 0, IPC_STAT, (union semun) { .buf = &seminfo }) == -1)
  76. FAIL_EXIT1 ("semctl with IPC_STAT failed (errno=%d)", errno);
  77. if (seminfo.sem_perm.__key != key)
  78. FAIL_EXIT1 ("semid_ds::sem_perm::key (%d) != %d",
  79. (int) seminfo.sem_perm.__key, (int) key);
  80. if (seminfo.sem_perm.mode != SEM_MODE)
  81. FAIL_EXIT1 ("semid_ds::sem_perm::mode (%o) != %o",
  82. seminfo.sem_perm.mode, SEM_MODE);
  83. if (seminfo.sem_nsems != 1)
  84. FAIL_EXIT1 ("semid_ds::sem_nsems (%lu) != 1",
  85. (long unsigned) seminfo.sem_nsems);
  86. /* Some lock/unlock basic tests. */
  87. struct sembuf sb1 = { 0, 1, 0 };
  88. if (semop (semid, &sb1, 1) == -1)
  89. FAIL_EXIT1 ("semop failed (errno=%i)", errno);
  90. struct sembuf sb2 = { 0, -1, 0 };
  91. if (semop (semid, &sb2, 1) == -1)
  92. FAIL_EXIT1 ("semop failed (errno=%i)", errno);
  93. #ifdef _GNU_SOURCE
  94. /* Set a time for half a second. The semaphore operation should timeout
  95. with EAGAIN. */
  96. {
  97. struct timespec ts = { 0 /* sec */, 500000000 /* nsec */ };
  98. if (semtimedop (semid, &sb2, 1, &ts) != -1
  99. || (errno != EAGAIN && errno != ENOSYS))
  100. FAIL_EXIT1 ("semtimedop succeed or returned errno != {EAGAIN,ENOSYS} "
  101. "(errno=%i)", errno);
  102. }
  103. {
  104. support_create_timer (0, 100000000, false, NULL);
  105. struct timespec ts = { TYPE_MAXIMUM (time_t), 0 };
  106. TEST_COMPARE (semtimedop (semid, &sb2, 1, &ts), -1);
  107. TEST_VERIFY (errno == EINTR || errno == EOVERFLOW);
  108. }
  109. #endif
  110. /* Finally free up the semnaphore resource. */
  111. if (semctl (semid, 0, IPC_RMID, 0) == -1)
  112. FAIL_EXIT1 ("semctl failed (errno=%d)", errno);
  113. return 0;
  114. }
  115. #include <support/test-driver.c>