1 | /* Copyright (C) 1995-2021 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. |
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 | #include <sys/sem.h> |
20 | #include <stdarg.h> |
21 | #include <ipc_priv.h> |
22 | #include <sysdep.h> |
23 | #include <shlib-compat.h> |
24 | #include <linux/posix_types.h> /* For __kernel_mode_t. */ |
25 | |
26 | /* The struct used to issue the syscall. For architectures that assume |
27 | 64-bit time as default (!__ASSUME_TIME64_SYSCALLS) the syscall will |
28 | split the resulting 64-bit sem_{o,c}time in two fields (sem_{o,c}time |
29 | and __sem_{o,c}time_high). */ |
30 | union semun |
31 | { |
32 | int val; /* value for SETVAL */ |
33 | struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */ |
34 | unsigned short int *array; /* array for GETALL & SETALL */ |
35 | struct seminfo *__buf; /* buffer for IPC_INFO */ |
36 | }; |
37 | |
38 | #if __IPC_TIME64 == 0 |
39 | # define semun64 semun |
40 | typedef union semun semctl_arg_t; |
41 | #else |
42 | # include <struct_kernel_semid64_ds.h> |
43 | |
44 | union ksemun64 |
45 | { |
46 | int val; |
47 | struct kernel_semid64_ds *buf; |
48 | unsigned short int *array; |
49 | struct seminfo *__buf; |
50 | }; |
51 | |
52 | # if __TIMESIZE == 64 |
53 | # define semun64 semun |
54 | # else |
55 | /* The struct used when __semctl64 is called. */ |
56 | union semun64 |
57 | { |
58 | int val; |
59 | struct __semid64_ds *buf; |
60 | unsigned short int *array; |
61 | struct seminfo *__buf; |
62 | }; |
63 | # endif |
64 | |
65 | static void |
66 | semid64_to_ksemid64 (const struct __semid64_ds *semid64, |
67 | struct kernel_semid64_ds *ksemid) |
68 | { |
69 | ksemid->sem_perm = semid64->sem_perm; |
70 | ksemid->sem_otime = semid64->sem_otime; |
71 | ksemid->sem_otime_high = semid64->sem_otime >> 32; |
72 | ksemid->sem_ctime = semid64->sem_ctime; |
73 | ksemid->sem_ctime_high = semid64->sem_ctime >> 32; |
74 | ksemid->sem_nsems = semid64->sem_nsems; |
75 | } |
76 | |
77 | static void |
78 | ksemid64_to_semid64 (const struct kernel_semid64_ds *ksemid, |
79 | struct __semid64_ds *semid64) |
80 | { |
81 | semid64->sem_perm = ksemid->sem_perm; |
82 | semid64->sem_otime = ksemid->sem_otime |
83 | | ((__time64_t) ksemid->sem_otime_high << 32); |
84 | semid64->sem_ctime = ksemid->sem_ctime |
85 | | ((__time64_t) ksemid->sem_ctime_high << 32); |
86 | semid64->sem_nsems = ksemid->sem_nsems; |
87 | } |
88 | |
89 | static union ksemun64 |
90 | semun64_to_ksemun64 (int cmd, union semun64 semun64, |
91 | struct kernel_semid64_ds *buf) |
92 | { |
93 | union ksemun64 r = { 0 }; |
94 | switch (cmd) |
95 | { |
96 | case SETVAL: |
97 | r.val = semun64.val; |
98 | break; |
99 | case GETALL: |
100 | case SETALL: |
101 | r.array = semun64.array; |
102 | break; |
103 | case SEM_STAT: |
104 | case SEM_STAT_ANY: |
105 | case IPC_STAT: |
106 | case IPC_SET: |
107 | r.buf = buf; |
108 | semid64_to_ksemid64 (semun64.buf, r.buf); |
109 | break; |
110 | case IPC_INFO: |
111 | case SEM_INFO: |
112 | r.__buf = semun64.__buf; |
113 | break; |
114 | } |
115 | return r; |
116 | } |
117 | |
118 | typedef union ksemun64 semctl_arg_t; |
119 | #endif |
120 | |
121 | static int |
122 | semctl_syscall (int semid, int semnum, int cmd, semctl_arg_t arg) |
123 | { |
124 | #ifdef __ASSUME_DIRECT_SYSVIPC_SYSCALLS |
125 | return INLINE_SYSCALL_CALL (semctl, semid, semnum, cmd | __IPC_64, |
126 | arg.array); |
127 | #else |
128 | return INLINE_SYSCALL_CALL (ipc, IPCOP_semctl, semid, semnum, cmd | __IPC_64, |
129 | SEMCTL_ARG_ADDRESS (arg)); |
130 | #endif |
131 | } |
132 | |
133 | /* POSIX states ipc_perm mode should have type of mode_t. */ |
134 | _Static_assert (sizeof ((struct semid_ds){0}.sem_perm.mode) |
135 | == sizeof (mode_t), |
136 | "sizeof (msqid_ds.msg_perm.mode) != sizeof (mode_t)" ); |
137 | |
138 | int |
139 | __semctl64 (int semid, int semnum, int cmd, ...) |
140 | { |
141 | union semun64 arg64 = { 0 }; |
142 | va_list ap; |
143 | |
144 | /* Get the argument only if required. */ |
145 | switch (cmd) |
146 | { |
147 | case SETVAL: /* arg.val */ |
148 | case GETALL: /* arg.array */ |
149 | case SETALL: |
150 | case IPC_STAT: /* arg.buf */ |
151 | case IPC_SET: |
152 | case SEM_STAT: |
153 | case SEM_STAT_ANY: |
154 | case IPC_INFO: /* arg.__buf */ |
155 | case SEM_INFO: |
156 | va_start (ap, cmd); |
157 | arg64 = va_arg (ap, union semun64); |
158 | va_end (ap); |
159 | break; |
160 | case IPC_RMID: /* arg ignored. */ |
161 | case GETNCNT: |
162 | case GETPID: |
163 | case GETVAL: |
164 | case GETZCNT: |
165 | break; |
166 | default: |
167 | __set_errno (EINVAL); |
168 | return -1; |
169 | } |
170 | |
171 | #if __IPC_TIME64 |
172 | struct kernel_semid64_ds ksemid; |
173 | union ksemun64 ksemun = semun64_to_ksemun64 (cmd, arg64, &ksemid); |
174 | # ifdef __ASSUME_SYSVIPC_BROKEN_MODE_T |
175 | if (cmd == IPC_SET) |
176 | ksemid.sem_perm.mode *= 0x10000U; |
177 | # endif |
178 | union ksemun64 arg = ksemun; |
179 | #else |
180 | union semun arg = arg64; |
181 | #endif |
182 | |
183 | int ret = semctl_syscall (semid, semnum, cmd, arg); |
184 | if (ret < 0) |
185 | return ret; |
186 | |
187 | switch (cmd) |
188 | { |
189 | case IPC_STAT: |
190 | case SEM_STAT: |
191 | case SEM_STAT_ANY: |
192 | #ifdef __ASSUME_SYSVIPC_BROKEN_MODE_T |
193 | arg.buf->sem_perm.mode >>= 16; |
194 | #else |
195 | /* Old Linux kernel versions might not clear the mode padding. */ |
196 | if (sizeof ((struct semid_ds){0}.sem_perm.mode) |
197 | != sizeof (__kernel_mode_t)) |
198 | arg.buf->sem_perm.mode &= 0xFFFF; |
199 | #endif |
200 | |
201 | #if __IPC_TIME64 |
202 | ksemid64_to_semid64 (arg.buf, arg64.buf); |
203 | #endif |
204 | } |
205 | |
206 | return ret; |
207 | } |
208 | #if __TIMESIZE != 64 |
209 | libc_hidden_def (__semctl64) |
210 | |
211 | |
212 | /* The 64-bit time_t semid_ds version might have a different layout and |
213 | internal field alignment. */ |
214 | |
215 | static void |
216 | semid_to_semid64 (struct __semid64_ds *ds64, const struct semid_ds *ds) |
217 | { |
218 | ds64->sem_perm = ds->sem_perm; |
219 | ds64->sem_otime = ds->sem_otime |
220 | | ((__time64_t) ds->__sem_otime_high << 32); |
221 | ds64->sem_ctime = ds->sem_ctime |
222 | | ((__time64_t) ds->__sem_ctime_high << 32); |
223 | ds64->sem_nsems = ds->sem_nsems; |
224 | } |
225 | |
226 | static void |
227 | semid64_to_semid (struct semid_ds *ds, const struct __semid64_ds *ds64) |
228 | { |
229 | ds->sem_perm = ds64->sem_perm; |
230 | ds->sem_otime = ds64->sem_otime; |
231 | ds->__sem_otime_high = 0; |
232 | ds->sem_ctime = ds64->sem_ctime; |
233 | ds->__sem_ctime_high = 0; |
234 | ds->sem_nsems = ds64->sem_nsems; |
235 | } |
236 | |
237 | static union semun64 |
238 | semun_to_semun64 (int cmd, union semun semun, struct __semid64_ds *semid64) |
239 | { |
240 | union semun64 r = { 0 }; |
241 | switch (cmd) |
242 | { |
243 | case SETVAL: |
244 | r.val = semun.val; |
245 | break; |
246 | case GETALL: |
247 | case SETALL: |
248 | r.array = semun.array; |
249 | break; |
250 | case SEM_STAT: |
251 | case SEM_STAT_ANY: |
252 | case IPC_STAT: |
253 | case IPC_SET: |
254 | r.buf = semid64; |
255 | semid_to_semid64 (r.buf, semun.buf); |
256 | break; |
257 | case IPC_INFO: |
258 | case SEM_INFO: |
259 | r.__buf = semun.__buf; |
260 | break; |
261 | } |
262 | return r; |
263 | } |
264 | |
265 | int |
266 | __semctl (int semid, int semnum, int cmd, ...) |
267 | { |
268 | union semun arg = { 0 }; |
269 | |
270 | va_list ap; |
271 | |
272 | /* Get the argument only if required. */ |
273 | switch (cmd) |
274 | { |
275 | case SETVAL: /* arg.val */ |
276 | case GETALL: /* arg.array */ |
277 | case SETALL: |
278 | case IPC_STAT: /* arg.buf */ |
279 | case IPC_SET: |
280 | case SEM_STAT: |
281 | case SEM_STAT_ANY: |
282 | case IPC_INFO: /* arg.__buf */ |
283 | case SEM_INFO: |
284 | va_start (ap, cmd); |
285 | arg = va_arg (ap, union semun); |
286 | va_end (ap); |
287 | break; |
288 | /* __semctl64 handles non-supported commands. */ |
289 | } |
290 | |
291 | struct __semid64_ds semid64; |
292 | union semun64 arg64 = semun_to_semun64 (cmd, arg, &semid64); |
293 | |
294 | int ret = __semctl64 (semid, semnum, cmd, arg64); |
295 | if (ret < 0) |
296 | return ret; |
297 | |
298 | switch (cmd) |
299 | { |
300 | case IPC_STAT: |
301 | case SEM_STAT: |
302 | case SEM_STAT_ANY: |
303 | semid64_to_semid (arg.buf, arg64.buf); |
304 | } |
305 | |
306 | return ret; |
307 | } |
308 | #endif |
309 | |
310 | #ifndef DEFAULT_VERSION |
311 | # ifndef __ASSUME_SYSVIPC_BROKEN_MODE_T |
312 | # define DEFAULT_VERSION GLIBC_2_2 |
313 | # else |
314 | # define DEFAULT_VERSION GLIBC_2_31 |
315 | # endif |
316 | #endif |
317 | versioned_symbol (libc, __semctl, semctl, DEFAULT_VERSION); |
318 | |
319 | #if defined __ASSUME_SYSVIPC_BROKEN_MODE_T \ |
320 | && SHLIB_COMPAT (libc, GLIBC_2_2, GLIBC_2_31) |
321 | int |
322 | attribute_compat_text_section |
323 | __semctl_mode16 (int semid, int semnum, int cmd, ...) |
324 | { |
325 | semctl_arg_t arg = { 0 }; |
326 | va_list ap; |
327 | |
328 | /* Get the argument only if required. */ |
329 | switch (cmd) |
330 | { |
331 | case SETVAL: /* arg.val */ |
332 | case GETALL: /* arg.array */ |
333 | case SETALL: |
334 | case IPC_STAT: /* arg.buf */ |
335 | case IPC_SET: |
336 | case SEM_STAT: |
337 | case SEM_STAT_ANY: |
338 | case IPC_INFO: /* arg.__buf */ |
339 | case SEM_INFO: |
340 | va_start (ap, cmd); |
341 | arg = va_arg (ap, semctl_arg_t); |
342 | va_end (ap); |
343 | break; |
344 | } |
345 | |
346 | return semctl_syscall (semid, semnum, cmd, arg); |
347 | } |
348 | compat_symbol (libc, __semctl_mode16, semctl, GLIBC_2_2); |
349 | #endif |
350 | |
351 | #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2) |
352 | /* Since semctl use a variadic argument for semid_ds there is not need to |
353 | define and tie the compatibility symbol to the old 'union semun' |
354 | definition. */ |
355 | int |
356 | attribute_compat_text_section |
357 | __old_semctl (int semid, int semnum, int cmd, ...) |
358 | { |
359 | union semun arg = { 0 }; |
360 | va_list ap; |
361 | |
362 | /* Get the argument only if required. */ |
363 | switch (cmd) |
364 | { |
365 | case SETVAL: /* arg.val */ |
366 | case GETALL: /* arg.array */ |
367 | case SETALL: |
368 | case IPC_STAT: /* arg.buf */ |
369 | case IPC_SET: |
370 | case SEM_STAT: |
371 | case SEM_STAT_ANY: |
372 | case IPC_INFO: /* arg.__buf */ |
373 | case SEM_INFO: |
374 | va_start (ap, cmd); |
375 | arg = va_arg (ap, union semun); |
376 | va_end (ap); |
377 | break; |
378 | } |
379 | |
380 | #if defined __ASSUME_DIRECT_SYSVIPC_SYSCALLS \ |
381 | && !defined __ASSUME_SYSVIPC_DEFAULT_IPC_64 |
382 | /* For architectures that have wire-up semctl but also have __IPC_64 to a |
383 | value different than default (0x0) it means the compat symbol used the |
384 | __NR_ipc syscall. */ |
385 | return INLINE_SYSCALL_CALL (semctl, semid, semnum, cmd, arg.array); |
386 | # else |
387 | return INLINE_SYSCALL_CALL (ipc, IPCOP_semctl, semid, semnum, cmd, |
388 | SEMCTL_ARG_ADDRESS (arg)); |
389 | # endif |
390 | } |
391 | compat_symbol (libc, __old_semctl, semctl, GLIBC_2_0); |
392 | #endif |
393 | |