test-sysvipc.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* Basic definition for Sysv IPC test functions.
  2. Copyright (C) 2020-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. #ifndef _TEST_SYSV_H
  16. #define _TEST_SYSV_H
  17. #include <sys/ipc.h>
  18. #include <sys/sem.h>
  19. #include <sys/msg.h>
  20. #include <sys/shm.h>
  21. #include <include/array_length.h>
  22. /* Return the first invalid SysV IPC command from common shared
  23. between message queue, shared memory, and semaphore. */
  24. static inline int
  25. first_common_invalid_cmd (void)
  26. {
  27. const int common_cmds[] = {
  28. IPC_RMID,
  29. IPC_SET,
  30. IPC_STAT,
  31. IPC_INFO,
  32. };
  33. int invalid = 0;
  34. for (int i = 0; i < array_length (common_cmds); i++)
  35. {
  36. if (invalid == common_cmds[i])
  37. {
  38. invalid++;
  39. i = 0;
  40. }
  41. }
  42. return invalid;
  43. }
  44. /* Return the first invalid SysV IPC command for semaphore. */
  45. static inline int
  46. first_sem_invalid_cmd (void)
  47. {
  48. const int sem_cmds[] = {
  49. GETPID,
  50. GETVAL,
  51. GETALL,
  52. GETNCNT,
  53. GETZCNT,
  54. SETVAL,
  55. SETALL,
  56. SEM_STAT,
  57. SEM_INFO,
  58. #ifdef SEM_STAT_ANY
  59. SEM_STAT_ANY,
  60. #endif
  61. };
  62. int invalid = first_common_invalid_cmd ();
  63. for (int i = 0; i < array_length (sem_cmds); i++)
  64. {
  65. if (invalid == sem_cmds[i])
  66. {
  67. invalid++;
  68. i = 0;
  69. }
  70. }
  71. return invalid;
  72. }
  73. /* Return the first invalid SysV IPC command for message queue. */
  74. static inline int
  75. first_msg_invalid_cmd (void)
  76. {
  77. const int msg_cmds[] = {
  78. MSG_STAT,
  79. MSG_INFO,
  80. #ifdef MSG_STAT_ANY
  81. MSG_STAT_ANY,
  82. #endif
  83. };
  84. int invalid = first_common_invalid_cmd ();
  85. for (int i = 0; i < array_length (msg_cmds); i++)
  86. {
  87. if (invalid == msg_cmds[i])
  88. {
  89. invalid++;
  90. i = 0;
  91. }
  92. }
  93. return invalid;
  94. }
  95. /* Return the first invalid SysV IPC command for shared memory. */
  96. static inline int
  97. first_shm_invalid_cmd (void)
  98. {
  99. const int shm_cmds[] = {
  100. SHM_STAT,
  101. SHM_INFO,
  102. #ifdef SHM_STAT_ANY
  103. SHM_STAT_ANY,
  104. #endif
  105. SHM_LOCK,
  106. SHM_UNLOCK
  107. };
  108. int invalid = first_common_invalid_cmd ();
  109. for (int i = 0; i < array_length (shm_cmds); i++)
  110. {
  111. if (invalid == shm_cmds[i])
  112. {
  113. invalid++;
  114. i = 0;
  115. }
  116. }
  117. return invalid;
  118. }
  119. #endif /* _TEST_SYSV_H */