| 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/msg.h> |
| 20 | #include <ipc_priv.h> |
| 21 | #include <sysdep.h> |
| 22 | #include <shlib-compat.h> |
| 23 | #include <errno.h> |
| 24 | |
| 25 | #ifndef DEFAULT_VERSION |
| 26 | # ifndef __ASSUME_SYSVIPC_BROKEN_MODE_T |
| 27 | # define DEFAULT_VERSION GLIBC_2_2 |
| 28 | # else |
| 29 | # define DEFAULT_VERSION GLIBC_2_31 |
| 30 | # endif |
| 31 | #endif |
| 32 | |
| 33 | static int |
| 34 | msgctl_syscall (int msqid, int cmd, struct msqid_ds *buf) |
| 35 | { |
| 36 | #ifdef __ASSUME_DIRECT_SYSVIPC_SYSCALLS |
| 37 | return INLINE_SYSCALL_CALL (msgctl, msqid, cmd | __IPC_64, buf); |
| 38 | #else |
| 39 | return INLINE_SYSCALL_CALL (ipc, IPCOP_msgctl, msqid, cmd | __IPC_64, 0, |
| 40 | buf); |
| 41 | #endif |
| 42 | } |
| 43 | |
| 44 | int |
| 45 | __new_msgctl (int msqid, int cmd, struct msqid_ds *buf) |
| 46 | { |
| 47 | /* POSIX states ipc_perm mode should have type of mode_t. */ |
| 48 | _Static_assert (sizeof ((struct msqid_ds){0}.msg_perm.mode) |
| 49 | == sizeof (mode_t), |
| 50 | "sizeof (msqid_ds.msg_perm.mode) != sizeof (mode_t)" ); |
| 51 | |
| 52 | #ifdef __ASSUME_SYSVIPC_BROKEN_MODE_T |
| 53 | struct msqid_ds tmpds; |
| 54 | if (cmd == IPC_SET) |
| 55 | { |
| 56 | tmpds = *buf; |
| 57 | tmpds.msg_perm.mode *= 0x10000U; |
| 58 | buf = &tmpds; |
| 59 | } |
| 60 | #endif |
| 61 | |
| 62 | int ret = msgctl_syscall (msqid, cmd, buf); |
| 63 | |
| 64 | #ifdef __ASSUME_SYSVIPC_BROKEN_MODE_T |
| 65 | if (ret >= 0) |
| 66 | { |
| 67 | switch (cmd) |
| 68 | { |
| 69 | case IPC_STAT: |
| 70 | case MSG_STAT: |
| 71 | case MSG_STAT_ANY: |
| 72 | buf->msg_perm.mode >>= 16; |
| 73 | } |
| 74 | } |
| 75 | #endif |
| 76 | |
| 77 | return ret; |
| 78 | } |
| 79 | versioned_symbol (libc, __new_msgctl, msgctl, DEFAULT_VERSION); |
| 80 | |
| 81 | #if defined __ASSUME_SYSVIPC_BROKEN_MODE_T \ |
| 82 | && SHLIB_COMPAT (libc, GLIBC_2_2, GLIBC_2_31) |
| 83 | int |
| 84 | attribute_compat_text_section |
| 85 | __msgctl_mode16 (int msqid, int cmd, struct msqid_ds *buf) |
| 86 | { |
| 87 | return msgctl_syscall (msqid, cmd, buf); |
| 88 | } |
| 89 | compat_symbol (libc, __msgctl_mode16, msgctl, GLIBC_2_2); |
| 90 | #endif |
| 91 | |
| 92 | #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2) |
| 93 | struct __old_msqid_ds |
| 94 | { |
| 95 | struct __old_ipc_perm msg_perm; /* structure describing operation permission */ |
| 96 | struct msg *__msg_first; /* pointer to first message on queue */ |
| 97 | struct msg *__msg_last; /* pointer to last message on queue */ |
| 98 | __time_t msg_stime; /* time of last msgsnd command */ |
| 99 | __time_t msg_rtime; /* time of last msgrcv command */ |
| 100 | __time_t msg_ctime; /* time of last change */ |
| 101 | struct wait_queue *__wwait; /* ??? */ |
| 102 | struct wait_queue *__rwait; /* ??? */ |
| 103 | unsigned short int __msg_cbytes; /* current number of bytes on queue */ |
| 104 | unsigned short int msg_qnum; /* number of messages currently on queue */ |
| 105 | unsigned short int msg_qbytes; /* max number of bytes allowed on queue */ |
| 106 | __ipc_pid_t msg_lspid; /* pid of last msgsnd() */ |
| 107 | __ipc_pid_t msg_lrpid; /* pid of last msgrcv() */ |
| 108 | }; |
| 109 | |
| 110 | int |
| 111 | attribute_compat_text_section |
| 112 | __old_msgctl (int msqid, int cmd, struct __old_msqid_ds *buf) |
| 113 | { |
| 114 | #if defined __ASSUME_DIRECT_SYSVIPC_SYSCALLS \ |
| 115 | && !defined __ASSUME_SYSVIPC_DEFAULT_IPC_64 |
| 116 | /* For architecture that have wire-up msgctl but also have __IPC_64 to a |
| 117 | value different than default (0x0) it means the compat symbol used the |
| 118 | __NR_ipc syscall. */ |
| 119 | return INLINE_SYSCALL_CALL (msgctl, msqid, cmd, buf); |
| 120 | #else |
| 121 | return INLINE_SYSCALL_CALL (ipc, IPCOP_msgctl, msqid, cmd, 0, buf); |
| 122 | #endif |
| 123 | } |
| 124 | compat_symbol (libc, __old_msgctl, msgctl, GLIBC_2_0); |
| 125 | #endif |
| 126 | |