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