1 /* Basic definition for Sysv IPC test functions.
2    Copyright (C) 2020-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #ifndef _TEST_SYSV_H
20 #define _TEST_SYSV_H
21 
22 #include <sys/ipc.h>
23 #include <sys/sem.h>
24 #include <sys/msg.h>
25 #include <sys/shm.h>
26 #include <include/array_length.h>
27 
28 /* Return the first invalid SysV IPC command from common shared
29    between message queue, shared memory, and semaphore.  */
30 static inline int
first_common_invalid_cmd(void)31 first_common_invalid_cmd (void)
32 {
33   const int common_cmds[] = {
34     IPC_RMID,
35     IPC_SET,
36     IPC_STAT,
37     IPC_INFO,
38   };
39 
40   int invalid = 0;
41   for (int i = 0; i < array_length (common_cmds); i++)
42     {
43       if (invalid == common_cmds[i])
44 	{
45 	  invalid++;
46 	  i = 0;
47         }
48     }
49 
50   return invalid;
51 }
52 
53 /* Return the first invalid SysV IPC command for semaphore.  */
54 static inline int
first_sem_invalid_cmd(void)55 first_sem_invalid_cmd (void)
56 {
57   const int sem_cmds[] = {
58     GETPID,
59     GETVAL,
60     GETALL,
61     GETNCNT,
62     GETZCNT,
63     SETVAL,
64     SETALL,
65     SEM_STAT,
66     SEM_INFO,
67 #ifdef SEM_STAT_ANY
68     SEM_STAT_ANY,
69 #endif
70   };
71 
72   int invalid = first_common_invalid_cmd ();
73   for (int i = 0; i < array_length (sem_cmds); i++)
74     {
75       if (invalid == sem_cmds[i])
76 	{
77 	  invalid++;
78 	  i = 0;
79 	}
80     }
81 
82   return invalid;
83 }
84 
85 /* Return the first invalid SysV IPC command for message queue.  */
86 static inline int
first_msg_invalid_cmd(void)87 first_msg_invalid_cmd (void)
88 {
89   const int msg_cmds[] = {
90     MSG_STAT,
91     MSG_INFO,
92 #ifdef MSG_STAT_ANY
93     MSG_STAT_ANY,
94 #endif
95   };
96 
97   int invalid = first_common_invalid_cmd ();
98   for (int i = 0; i < array_length (msg_cmds); i++)
99     {
100       if (invalid == msg_cmds[i])
101 	{
102 	  invalid++;
103 	  i = 0;
104 	}
105     }
106 
107   return invalid;
108 }
109 
110 /* Return the first invalid SysV IPC command for shared memory.  */
111 static inline int
first_shm_invalid_cmd(void)112 first_shm_invalid_cmd (void)
113 {
114   const int shm_cmds[] = {
115     SHM_STAT,
116     SHM_INFO,
117 #ifdef SHM_STAT_ANY
118     SHM_STAT_ANY,
119 #endif
120     SHM_LOCK,
121     SHM_UNLOCK
122   };
123 
124   int invalid = first_common_invalid_cmd ();
125   for (int i = 0; i < array_length (shm_cmds); i++)
126     {
127       if (invalid == shm_cmds[i])
128 	{
129 	  invalid++;
130 	  i = 0;
131 	}
132     }
133 
134   return invalid;
135 }
136 
137 #endif /* _TEST_SYSV_H  */
138