Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/ipc/msg.c |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 3 | * Copyright (C) 1992 Krishna Balasubramanian |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * Removed all the remaining kerneld mess |
| 6 | * Catch the -EFAULT stuff properly |
| 7 | * Use GFP_KERNEL for messages as in 1.2 |
| 8 | * Fixed up the unchecked user space derefs |
| 9 | * Copyright (C) 1998 Alan Cox & Andi Kleen |
| 10 | * |
| 11 | * /proc/sysvipc/msg support (c) 1999 Dragos Acostachioaie <dragos@iname.com> |
| 12 | * |
| 13 | * mostly rewritten, threaded and wake-one semantics added |
| 14 | * MSGMAX limit removed, sysctl's added |
Christian Kujau | 624dffc | 2006-01-15 02:43:54 +0100 | [diff] [blame] | 15 | * (c) 1999 Manfred Spraul <manfred@colorfullife.com> |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 16 | * |
| 17 | * support for audit of ipc object properties and permission changes |
| 18 | * Dustin Kirkland <dustin.kirkland@us.ibm.com> |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 19 | * |
| 20 | * namespaces support |
| 21 | * OpenVZ, SWsoft Inc. |
| 22 | * Pavel Emelianov <xemul@openvz.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | */ |
| 24 | |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 25 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/slab.h> |
| 27 | #include <linux/msg.h> |
| 28 | #include <linux/spinlock.h> |
| 29 | #include <linux/init.h> |
| 30 | #include <linux/proc_fs.h> |
| 31 | #include <linux/list.h> |
| 32 | #include <linux/security.h> |
| 33 | #include <linux/sched.h> |
| 34 | #include <linux/syscalls.h> |
| 35 | #include <linux/audit.h> |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 36 | #include <linux/seq_file.h> |
Ingo Molnar | 5f921ae | 2006-03-26 01:37:17 -0800 | [diff] [blame] | 37 | #include <linux/mutex.h> |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 38 | #include <linux/nsproxy.h> |
Ingo Molnar | 5f921ae | 2006-03-26 01:37:17 -0800 | [diff] [blame] | 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <asm/current.h> |
| 41 | #include <asm/uaccess.h> |
| 42 | #include "util.h" |
| 43 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 44 | /* |
| 45 | * one msg_receiver structure for each sleeping receiver: |
| 46 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | struct msg_receiver { |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 48 | struct list_head r_list; |
| 49 | struct task_struct *r_tsk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 51 | int r_mode; |
| 52 | long r_msgtype; |
| 53 | long r_maxsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Linus Torvalds | 80491eb | 2006-11-04 09:55:00 -0800 | [diff] [blame] | 55 | struct msg_msg *volatile r_msg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | /* one msg_sender for each sleeping sender */ |
| 59 | struct msg_sender { |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 60 | struct list_head list; |
| 61 | struct task_struct *tsk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | #define SEARCH_ANY 1 |
| 65 | #define SEARCH_EQUAL 2 |
| 66 | #define SEARCH_NOTEQUAL 3 |
| 67 | #define SEARCH_LESSEQUAL 4 |
| 68 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 69 | static atomic_t msg_bytes = ATOMIC_INIT(0); |
| 70 | static atomic_t msg_hdrs = ATOMIC_INIT(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 72 | static struct ipc_ids init_msg_ids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 74 | #define msg_ids(ns) (*((ns)->ids[IPC_MSG_IDS])) |
| 75 | |
| 76 | #define msg_lock(ns, id) ((struct msg_queue*)ipc_lock(&msg_ids(ns), id)) |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 77 | #define msg_unlock(msq) ipc_unlock(&(msq)->q_perm) |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 78 | #define msg_rmid(ns, id) ((struct msg_queue*)ipc_rmid(&msg_ids(ns), id)) |
| 79 | #define msg_checkid(ns, msq, msgid) \ |
| 80 | ipc_checkid(&msg_ids(ns), &msq->q_perm, msgid) |
| 81 | #define msg_buildid(ns, id, seq) \ |
| 82 | ipc_buildid(&msg_ids(ns), id, seq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 84 | static void freeque (struct ipc_namespace *ns, struct msg_queue *msq, int id); |
| 85 | static int newque (struct ipc_namespace *ns, key_t key, int msgflg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | #ifdef CONFIG_PROC_FS |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 87 | static int sysvipc_msg_proc_show(struct seq_file *s, void *it); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | #endif |
| 89 | |
Cedric Le Goater | 7d69a1f | 2007-07-15 23:40:58 -0700 | [diff] [blame^] | 90 | static void __msg_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | { |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 92 | ns->ids[IPC_MSG_IDS] = ids; |
| 93 | ns->msg_ctlmax = MSGMAX; |
| 94 | ns->msg_ctlmnb = MSGMNB; |
| 95 | ns->msg_ctlmni = MSGMNI; |
| 96 | ipc_init_ids(ids, ns->msg_ctlmni); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 99 | int msg_init_ns(struct ipc_namespace *ns) |
| 100 | { |
| 101 | struct ipc_ids *ids; |
| 102 | |
| 103 | ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL); |
| 104 | if (ids == NULL) |
| 105 | return -ENOMEM; |
| 106 | |
| 107 | __msg_init_ns(ns, ids); |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | void msg_exit_ns(struct ipc_namespace *ns) |
| 112 | { |
| 113 | int i; |
| 114 | struct msg_queue *msq; |
| 115 | |
| 116 | mutex_lock(&msg_ids(ns).mutex); |
| 117 | for (i = 0; i <= msg_ids(ns).max_id; i++) { |
| 118 | msq = msg_lock(ns, i); |
| 119 | if (msq == NULL) |
| 120 | continue; |
| 121 | |
| 122 | freeque(ns, msq, i); |
| 123 | } |
| 124 | mutex_unlock(&msg_ids(ns).mutex); |
| 125 | |
Pavel Emelianov | c7e12b8 | 2006-11-02 22:07:03 -0800 | [diff] [blame] | 126 | ipc_fini_ids(ns->ids[IPC_MSG_IDS]); |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 127 | kfree(ns->ids[IPC_MSG_IDS]); |
| 128 | ns->ids[IPC_MSG_IDS] = NULL; |
| 129 | } |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 130 | |
| 131 | void __init msg_init(void) |
| 132 | { |
| 133 | __msg_init_ns(&init_ipc_ns, &init_msg_ids); |
| 134 | ipc_init_proc_interface("sysvipc/msg", |
| 135 | " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n", |
| 136 | IPC_MSG_IDS, sysvipc_msg_proc_show); |
| 137 | } |
| 138 | |
| 139 | static int newque (struct ipc_namespace *ns, key_t key, int msgflg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | struct msg_queue *msq; |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 142 | int id, retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 144 | msq = ipc_rcu_alloc(sizeof(*msq)); |
| 145 | if (!msq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | return -ENOMEM; |
| 147 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 148 | msq->q_perm.mode = msgflg & S_IRWXUGO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | msq->q_perm.key = key; |
| 150 | |
| 151 | msq->q_perm.security = NULL; |
| 152 | retval = security_msg_queue_alloc(msq); |
| 153 | if (retval) { |
| 154 | ipc_rcu_putref(msq); |
| 155 | return retval; |
| 156 | } |
| 157 | |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 158 | id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni); |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 159 | if (id == -1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | security_msg_queue_free(msq); |
| 161 | ipc_rcu_putref(msq); |
| 162 | return -ENOSPC; |
| 163 | } |
| 164 | |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 165 | msq->q_id = msg_buildid(ns, id, msq->q_perm.seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | msq->q_stime = msq->q_rtime = 0; |
| 167 | msq->q_ctime = get_seconds(); |
| 168 | msq->q_cbytes = msq->q_qnum = 0; |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 169 | msq->q_qbytes = ns->msg_ctlmnb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | msq->q_lspid = msq->q_lrpid = 0; |
| 171 | INIT_LIST_HEAD(&msq->q_messages); |
| 172 | INIT_LIST_HEAD(&msq->q_receivers); |
| 173 | INIT_LIST_HEAD(&msq->q_senders); |
| 174 | msg_unlock(msq); |
| 175 | |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 176 | return msq->q_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 179 | static inline void ss_add(struct msg_queue *msq, struct msg_sender *mss) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | { |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 181 | mss->tsk = current; |
| 182 | current->state = TASK_INTERRUPTIBLE; |
| 183 | list_add_tail(&mss->list, &msq->q_senders); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 186 | static inline void ss_del(struct msg_sender *mss) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | { |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 188 | if (mss->list.next != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | list_del(&mss->list); |
| 190 | } |
| 191 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 192 | static void ss_wakeup(struct list_head *h, int kill) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | { |
| 194 | struct list_head *tmp; |
| 195 | |
| 196 | tmp = h->next; |
| 197 | while (tmp != h) { |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 198 | struct msg_sender *mss; |
| 199 | |
| 200 | mss = list_entry(tmp, struct msg_sender, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | tmp = tmp->next; |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 202 | if (kill) |
| 203 | mss->list.next = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | wake_up_process(mss->tsk); |
| 205 | } |
| 206 | } |
| 207 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 208 | static void expunge_all(struct msg_queue *msq, int res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | { |
| 210 | struct list_head *tmp; |
| 211 | |
| 212 | tmp = msq->q_receivers.next; |
| 213 | while (tmp != &msq->q_receivers) { |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 214 | struct msg_receiver *msr; |
| 215 | |
| 216 | msr = list_entry(tmp, struct msg_receiver, r_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | tmp = tmp->next; |
| 218 | msr->r_msg = NULL; |
| 219 | wake_up_process(msr->r_tsk); |
| 220 | smp_mb(); |
| 221 | msr->r_msg = ERR_PTR(res); |
| 222 | } |
| 223 | } |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 224 | |
| 225 | /* |
| 226 | * freeque() wakes up waiters on the sender and receiver waiting queue, |
| 227 | * removes the message queue from message queue ID |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | * array, and cleans up all the messages associated with this queue. |
| 229 | * |
Ingo Molnar | 5f921ae | 2006-03-26 01:37:17 -0800 | [diff] [blame] | 230 | * msg_ids.mutex and the spinlock for this message queue is hold |
| 231 | * before freeque() is called. msg_ids.mutex remains locked on exit. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | */ |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 233 | static void freeque(struct ipc_namespace *ns, struct msg_queue *msq, int id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | { |
| 235 | struct list_head *tmp; |
| 236 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 237 | expunge_all(msq, -EIDRM); |
| 238 | ss_wakeup(&msq->q_senders, 1); |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 239 | msq = msg_rmid(ns, id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | msg_unlock(msq); |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | tmp = msq->q_messages.next; |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 243 | while (tmp != &msq->q_messages) { |
| 244 | struct msg_msg *msg = list_entry(tmp, struct msg_msg, m_list); |
| 245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | tmp = tmp->next; |
| 247 | atomic_dec(&msg_hdrs); |
| 248 | free_msg(msg); |
| 249 | } |
| 250 | atomic_sub(msq->q_cbytes, &msg_bytes); |
| 251 | security_msg_queue_free(msq); |
| 252 | ipc_rcu_putref(msq); |
| 253 | } |
| 254 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 255 | asmlinkage long sys_msgget(key_t key, int msgflg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | struct msg_queue *msq; |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 258 | int id, ret = -EPERM; |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 259 | struct ipc_namespace *ns; |
| 260 | |
| 261 | ns = current->nsproxy->ipc_ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 263 | mutex_lock(&msg_ids(ns).mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | if (key == IPC_PRIVATE) |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 265 | ret = newque(ns, key, msgflg); |
| 266 | else if ((id = ipc_findkey(&msg_ids(ns), key)) == -1) { /* key not used */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | if (!(msgflg & IPC_CREAT)) |
| 268 | ret = -ENOENT; |
| 269 | else |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 270 | ret = newque(ns, key, msgflg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } else if (msgflg & IPC_CREAT && msgflg & IPC_EXCL) { |
| 272 | ret = -EEXIST; |
| 273 | } else { |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 274 | msq = msg_lock(ns, id); |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 275 | BUG_ON(msq == NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | if (ipcperms(&msq->q_perm, msgflg)) |
| 277 | ret = -EACCES; |
| 278 | else { |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 279 | int qid = msg_buildid(ns, id, msq->q_perm.seq); |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 280 | |
| 281 | ret = security_msg_queue_associate(msq, msgflg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | if (!ret) |
| 283 | ret = qid; |
| 284 | } |
| 285 | msg_unlock(msq); |
| 286 | } |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 287 | mutex_unlock(&msg_ids(ns).mutex); |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 288 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | return ret; |
| 290 | } |
| 291 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 292 | static inline unsigned long |
| 293 | copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | { |
| 295 | switch(version) { |
| 296 | case IPC_64: |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 297 | return copy_to_user(buf, in, sizeof(*in)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | case IPC_OLD: |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 299 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | struct msqid_ds out; |
| 301 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 302 | memset(&out, 0, sizeof(out)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
| 304 | ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm); |
| 305 | |
| 306 | out.msg_stime = in->msg_stime; |
| 307 | out.msg_rtime = in->msg_rtime; |
| 308 | out.msg_ctime = in->msg_ctime; |
| 309 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 310 | if (in->msg_cbytes > USHRT_MAX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | out.msg_cbytes = USHRT_MAX; |
| 312 | else |
| 313 | out.msg_cbytes = in->msg_cbytes; |
| 314 | out.msg_lcbytes = in->msg_cbytes; |
| 315 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 316 | if (in->msg_qnum > USHRT_MAX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | out.msg_qnum = USHRT_MAX; |
| 318 | else |
| 319 | out.msg_qnum = in->msg_qnum; |
| 320 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 321 | if (in->msg_qbytes > USHRT_MAX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | out.msg_qbytes = USHRT_MAX; |
| 323 | else |
| 324 | out.msg_qbytes = in->msg_qbytes; |
| 325 | out.msg_lqbytes = in->msg_qbytes; |
| 326 | |
| 327 | out.msg_lspid = in->msg_lspid; |
| 328 | out.msg_lrpid = in->msg_lrpid; |
| 329 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 330 | return copy_to_user(buf, &out, sizeof(out)); |
| 331 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | default: |
| 333 | return -EINVAL; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | struct msq_setbuf { |
| 338 | unsigned long qbytes; |
| 339 | uid_t uid; |
| 340 | gid_t gid; |
| 341 | mode_t mode; |
| 342 | }; |
| 343 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 344 | static inline unsigned long |
| 345 | copy_msqid_from_user(struct msq_setbuf *out, void __user *buf, int version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | { |
| 347 | switch(version) { |
| 348 | case IPC_64: |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 349 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | struct msqid64_ds tbuf; |
| 351 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 352 | if (copy_from_user(&tbuf, buf, sizeof(tbuf))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | return -EFAULT; |
| 354 | |
| 355 | out->qbytes = tbuf.msg_qbytes; |
| 356 | out->uid = tbuf.msg_perm.uid; |
| 357 | out->gid = tbuf.msg_perm.gid; |
| 358 | out->mode = tbuf.msg_perm.mode; |
| 359 | |
| 360 | return 0; |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 361 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | case IPC_OLD: |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 363 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | struct msqid_ds tbuf_old; |
| 365 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 366 | if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | return -EFAULT; |
| 368 | |
| 369 | out->uid = tbuf_old.msg_perm.uid; |
| 370 | out->gid = tbuf_old.msg_perm.gid; |
| 371 | out->mode = tbuf_old.msg_perm.mode; |
| 372 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 373 | if (tbuf_old.msg_qbytes == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | out->qbytes = tbuf_old.msg_lqbytes; |
| 375 | else |
| 376 | out->qbytes = tbuf_old.msg_qbytes; |
| 377 | |
| 378 | return 0; |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 379 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | default: |
| 381 | return -EINVAL; |
| 382 | } |
| 383 | } |
| 384 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 385 | asmlinkage long sys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | struct kern_ipc_perm *ipcp; |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 388 | struct msq_setbuf setbuf; |
| 389 | struct msg_queue *msq; |
| 390 | int err, version; |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 391 | struct ipc_namespace *ns; |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | if (msqid < 0 || cmd < 0) |
| 394 | return -EINVAL; |
| 395 | |
| 396 | version = ipc_parse_version(&cmd); |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 397 | ns = current->nsproxy->ipc_ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
| 399 | switch (cmd) { |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 400 | case IPC_INFO: |
| 401 | case MSG_INFO: |
| 402 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | struct msginfo msginfo; |
| 404 | int max_id; |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | if (!buf) |
| 407 | return -EFAULT; |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 408 | /* |
| 409 | * We must not return kernel stack data. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | * due to padding, it's not enough |
| 411 | * to set all member fields. |
| 412 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | err = security_msg_queue_msgctl(NULL, cmd); |
| 414 | if (err) |
| 415 | return err; |
| 416 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 417 | memset(&msginfo, 0, sizeof(msginfo)); |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 418 | msginfo.msgmni = ns->msg_ctlmni; |
| 419 | msginfo.msgmax = ns->msg_ctlmax; |
| 420 | msginfo.msgmnb = ns->msg_ctlmnb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | msginfo.msgssz = MSGSSZ; |
| 422 | msginfo.msgseg = MSGSEG; |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 423 | mutex_lock(&msg_ids(ns).mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | if (cmd == MSG_INFO) { |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 425 | msginfo.msgpool = msg_ids(ns).in_use; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | msginfo.msgmap = atomic_read(&msg_hdrs); |
| 427 | msginfo.msgtql = atomic_read(&msg_bytes); |
| 428 | } else { |
| 429 | msginfo.msgmap = MSGMAP; |
| 430 | msginfo.msgpool = MSGPOOL; |
| 431 | msginfo.msgtql = MSGTQL; |
| 432 | } |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 433 | max_id = msg_ids(ns).max_id; |
| 434 | mutex_unlock(&msg_ids(ns).mutex); |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 435 | if (copy_to_user(buf, &msginfo, sizeof(struct msginfo))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | return -EFAULT; |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 437 | return (max_id < 0) ? 0 : max_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | } |
| 439 | case MSG_STAT: |
| 440 | case IPC_STAT: |
| 441 | { |
| 442 | struct msqid64_ds tbuf; |
| 443 | int success_return; |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | if (!buf) |
| 446 | return -EFAULT; |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 447 | if (cmd == MSG_STAT && msqid >= msg_ids(ns).entries->size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | return -EINVAL; |
| 449 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 450 | memset(&tbuf, 0, sizeof(tbuf)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 452 | msq = msg_lock(ns, msqid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | if (msq == NULL) |
| 454 | return -EINVAL; |
| 455 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 456 | if (cmd == MSG_STAT) { |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 457 | success_return = msg_buildid(ns, msqid, msq->q_perm.seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | } else { |
| 459 | err = -EIDRM; |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 460 | if (msg_checkid(ns, msq, msqid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | goto out_unlock; |
| 462 | success_return = 0; |
| 463 | } |
| 464 | err = -EACCES; |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 465 | if (ipcperms(&msq->q_perm, S_IRUGO)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | goto out_unlock; |
| 467 | |
| 468 | err = security_msg_queue_msgctl(msq, cmd); |
| 469 | if (err) |
| 470 | goto out_unlock; |
| 471 | |
| 472 | kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm); |
| 473 | tbuf.msg_stime = msq->q_stime; |
| 474 | tbuf.msg_rtime = msq->q_rtime; |
| 475 | tbuf.msg_ctime = msq->q_ctime; |
| 476 | tbuf.msg_cbytes = msq->q_cbytes; |
| 477 | tbuf.msg_qnum = msq->q_qnum; |
| 478 | tbuf.msg_qbytes = msq->q_qbytes; |
| 479 | tbuf.msg_lspid = msq->q_lspid; |
| 480 | tbuf.msg_lrpid = msq->q_lrpid; |
| 481 | msg_unlock(msq); |
| 482 | if (copy_msqid_to_user(buf, &tbuf, version)) |
| 483 | return -EFAULT; |
| 484 | return success_return; |
| 485 | } |
| 486 | case IPC_SET: |
| 487 | if (!buf) |
| 488 | return -EFAULT; |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 489 | if (copy_msqid_from_user(&setbuf, buf, version)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | break; |
| 492 | case IPC_RMID: |
| 493 | break; |
| 494 | default: |
| 495 | return -EINVAL; |
| 496 | } |
| 497 | |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 498 | mutex_lock(&msg_ids(ns).mutex); |
| 499 | msq = msg_lock(ns, msqid); |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 500 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | if (msq == NULL) |
| 502 | goto out_up; |
| 503 | |
| 504 | err = -EIDRM; |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 505 | if (msg_checkid(ns, msq, msqid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | goto out_unlock_up; |
| 507 | ipcp = &msq->q_perm; |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 508 | |
| 509 | err = audit_ipc_obj(ipcp); |
| 510 | if (err) |
| 511 | goto out_unlock_up; |
Linda Knippers | ac03221 | 2006-05-16 22:03:48 -0400 | [diff] [blame] | 512 | if (cmd==IPC_SET) { |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 513 | err = audit_ipc_set_perm(setbuf.qbytes, setbuf.uid, setbuf.gid, |
| 514 | setbuf.mode); |
Linda Knippers | ac03221 | 2006-05-16 22:03:48 -0400 | [diff] [blame] | 515 | if (err) |
| 516 | goto out_unlock_up; |
| 517 | } |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 518 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | err = -EPERM; |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 520 | if (current->euid != ipcp->cuid && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN)) |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 522 | /* We _could_ check for CAP_CHOWN above, but we don't */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | goto out_unlock_up; |
| 524 | |
| 525 | err = security_msg_queue_msgctl(msq, cmd); |
| 526 | if (err) |
| 527 | goto out_unlock_up; |
| 528 | |
| 529 | switch (cmd) { |
| 530 | case IPC_SET: |
| 531 | { |
| 532 | err = -EPERM; |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 533 | if (setbuf.qbytes > ns->msg_ctlmnb && !capable(CAP_SYS_RESOURCE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | goto out_unlock_up; |
| 535 | |
| 536 | msq->q_qbytes = setbuf.qbytes; |
| 537 | |
| 538 | ipcp->uid = setbuf.uid; |
| 539 | ipcp->gid = setbuf.gid; |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 540 | ipcp->mode = (ipcp->mode & ~S_IRWXUGO) | |
| 541 | (S_IRWXUGO & setbuf.mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | msq->q_ctime = get_seconds(); |
| 543 | /* sleeping receivers might be excluded by |
| 544 | * stricter permissions. |
| 545 | */ |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 546 | expunge_all(msq, -EAGAIN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | /* sleeping senders might be able to send |
| 548 | * due to a larger queue size. |
| 549 | */ |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 550 | ss_wakeup(&msq->q_senders, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | msg_unlock(msq); |
| 552 | break; |
| 553 | } |
| 554 | case IPC_RMID: |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 555 | freeque(ns, msq, msqid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | break; |
| 557 | } |
| 558 | err = 0; |
| 559 | out_up: |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 560 | mutex_unlock(&msg_ids(ns).mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | return err; |
| 562 | out_unlock_up: |
| 563 | msg_unlock(msq); |
| 564 | goto out_up; |
| 565 | out_unlock: |
| 566 | msg_unlock(msq); |
| 567 | return err; |
| 568 | } |
| 569 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 570 | static int testmsg(struct msg_msg *msg, long type, int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | { |
| 572 | switch(mode) |
| 573 | { |
| 574 | case SEARCH_ANY: |
| 575 | return 1; |
| 576 | case SEARCH_LESSEQUAL: |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 577 | if (msg->m_type <=type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | return 1; |
| 579 | break; |
| 580 | case SEARCH_EQUAL: |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 581 | if (msg->m_type == type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | return 1; |
| 583 | break; |
| 584 | case SEARCH_NOTEQUAL: |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 585 | if (msg->m_type != type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | return 1; |
| 587 | break; |
| 588 | } |
| 589 | return 0; |
| 590 | } |
| 591 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 592 | static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | { |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 594 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | |
| 596 | tmp = msq->q_receivers.next; |
| 597 | while (tmp != &msq->q_receivers) { |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 598 | struct msg_receiver *msr; |
| 599 | |
| 600 | msr = list_entry(tmp, struct msg_receiver, r_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | tmp = tmp->next; |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 602 | if (testmsg(msg, msr->r_msgtype, msr->r_mode) && |
| 603 | !security_msg_queue_msgrcv(msq, msg, msr->r_tsk, |
| 604 | msr->r_msgtype, msr->r_mode)) { |
| 605 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | list_del(&msr->r_list); |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 607 | if (msr->r_maxsize < msg->m_ts) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | msr->r_msg = NULL; |
| 609 | wake_up_process(msr->r_tsk); |
| 610 | smp_mb(); |
| 611 | msr->r_msg = ERR_PTR(-E2BIG); |
| 612 | } else { |
| 613 | msr->r_msg = NULL; |
| 614 | msq->q_lrpid = msr->r_tsk->pid; |
| 615 | msq->q_rtime = get_seconds(); |
| 616 | wake_up_process(msr->r_tsk); |
| 617 | smp_mb(); |
| 618 | msr->r_msg = msg; |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 619 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | return 1; |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | return 0; |
| 625 | } |
| 626 | |
suzuki | 651971c | 2006-12-06 20:37:48 -0800 | [diff] [blame] | 627 | long do_msgsnd(int msqid, long mtype, void __user *mtext, |
| 628 | size_t msgsz, int msgflg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | { |
| 630 | struct msg_queue *msq; |
| 631 | struct msg_msg *msg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | int err; |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 633 | struct ipc_namespace *ns; |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 634 | |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 635 | ns = current->nsproxy->ipc_ns; |
| 636 | |
| 637 | if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | if (mtype < 1) |
| 640 | return -EINVAL; |
| 641 | |
suzuki | 651971c | 2006-12-06 20:37:48 -0800 | [diff] [blame] | 642 | msg = load_msg(mtext, msgsz); |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 643 | if (IS_ERR(msg)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | return PTR_ERR(msg); |
| 645 | |
| 646 | msg->m_type = mtype; |
| 647 | msg->m_ts = msgsz; |
| 648 | |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 649 | msq = msg_lock(ns, msqid); |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 650 | err = -EINVAL; |
| 651 | if (msq == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | goto out_free; |
| 653 | |
| 654 | err= -EIDRM; |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 655 | if (msg_checkid(ns, msq, msqid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | goto out_unlock_free; |
| 657 | |
| 658 | for (;;) { |
| 659 | struct msg_sender s; |
| 660 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 661 | err = -EACCES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | if (ipcperms(&msq->q_perm, S_IWUGO)) |
| 663 | goto out_unlock_free; |
| 664 | |
| 665 | err = security_msg_queue_msgsnd(msq, msg, msgflg); |
| 666 | if (err) |
| 667 | goto out_unlock_free; |
| 668 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 669 | if (msgsz + msq->q_cbytes <= msq->q_qbytes && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | 1 + msq->q_qnum <= msq->q_qbytes) { |
| 671 | break; |
| 672 | } |
| 673 | |
| 674 | /* queue full, wait: */ |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 675 | if (msgflg & IPC_NOWAIT) { |
| 676 | err = -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | goto out_unlock_free; |
| 678 | } |
| 679 | ss_add(msq, &s); |
| 680 | ipc_rcu_getref(msq); |
| 681 | msg_unlock(msq); |
| 682 | schedule(); |
| 683 | |
| 684 | ipc_lock_by_ptr(&msq->q_perm); |
| 685 | ipc_rcu_putref(msq); |
| 686 | if (msq->q_perm.deleted) { |
| 687 | err = -EIDRM; |
| 688 | goto out_unlock_free; |
| 689 | } |
| 690 | ss_del(&s); |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 691 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | if (signal_pending(current)) { |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 693 | err = -ERESTARTNOHAND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | goto out_unlock_free; |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | msq->q_lspid = current->tgid; |
| 699 | msq->q_stime = get_seconds(); |
| 700 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 701 | if (!pipelined_send(msq, msg)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | /* noone is waiting for this message, enqueue it */ |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 703 | list_add_tail(&msg->m_list, &msq->q_messages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | msq->q_cbytes += msgsz; |
| 705 | msq->q_qnum++; |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 706 | atomic_add(msgsz, &msg_bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | atomic_inc(&msg_hdrs); |
| 708 | } |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 709 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | err = 0; |
| 711 | msg = NULL; |
| 712 | |
| 713 | out_unlock_free: |
| 714 | msg_unlock(msq); |
| 715 | out_free: |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 716 | if (msg != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | free_msg(msg); |
| 718 | return err; |
| 719 | } |
| 720 | |
suzuki | 651971c | 2006-12-06 20:37:48 -0800 | [diff] [blame] | 721 | asmlinkage long |
| 722 | sys_msgsnd(int msqid, struct msgbuf __user *msgp, size_t msgsz, int msgflg) |
| 723 | { |
| 724 | long mtype; |
| 725 | |
| 726 | if (get_user(mtype, &msgp->mtype)) |
| 727 | return -EFAULT; |
| 728 | return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg); |
| 729 | } |
| 730 | |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 731 | static inline int convert_mode(long *msgtyp, int msgflg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | { |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 733 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | * find message of correct type. |
| 735 | * msgtyp = 0 => get first. |
| 736 | * msgtyp > 0 => get first message of matching type. |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 737 | * msgtyp < 0 => get message with least type must be < abs(msgtype). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | */ |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 739 | if (*msgtyp == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | return SEARCH_ANY; |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 741 | if (*msgtyp < 0) { |
| 742 | *msgtyp = -*msgtyp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | return SEARCH_LESSEQUAL; |
| 744 | } |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 745 | if (msgflg & MSG_EXCEPT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | return SEARCH_NOTEQUAL; |
| 747 | return SEARCH_EQUAL; |
| 748 | } |
| 749 | |
suzuki | 651971c | 2006-12-06 20:37:48 -0800 | [diff] [blame] | 750 | long do_msgrcv(int msqid, long *pmtype, void __user *mtext, |
| 751 | size_t msgsz, long msgtyp, int msgflg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | { |
| 753 | struct msg_queue *msq; |
| 754 | struct msg_msg *msg; |
| 755 | int mode; |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 756 | struct ipc_namespace *ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | |
| 758 | if (msqid < 0 || (long) msgsz < 0) |
| 759 | return -EINVAL; |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 760 | mode = convert_mode(&msgtyp, msgflg); |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 761 | ns = current->nsproxy->ipc_ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 763 | msq = msg_lock(ns, msqid); |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 764 | if (msq == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | return -EINVAL; |
| 766 | |
| 767 | msg = ERR_PTR(-EIDRM); |
Kirill Korotaev | 1e78693 | 2006-10-02 02:18:21 -0700 | [diff] [blame] | 768 | if (msg_checkid(ns, msq, msqid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | goto out_unlock; |
| 770 | |
| 771 | for (;;) { |
| 772 | struct msg_receiver msr_d; |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 773 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | |
| 775 | msg = ERR_PTR(-EACCES); |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 776 | if (ipcperms(&msq->q_perm, S_IRUGO)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | goto out_unlock; |
| 778 | |
| 779 | msg = ERR_PTR(-EAGAIN); |
| 780 | tmp = msq->q_messages.next; |
| 781 | while (tmp != &msq->q_messages) { |
| 782 | struct msg_msg *walk_msg; |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 783 | |
| 784 | walk_msg = list_entry(tmp, struct msg_msg, m_list); |
| 785 | if (testmsg(walk_msg, msgtyp, mode) && |
| 786 | !security_msg_queue_msgrcv(msq, walk_msg, current, |
| 787 | msgtyp, mode)) { |
| 788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | msg = walk_msg; |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 790 | if (mode == SEARCH_LESSEQUAL && |
| 791 | walk_msg->m_type != 1) { |
| 792 | msg = walk_msg; |
| 793 | msgtyp = walk_msg->m_type - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | } else { |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 795 | msg = walk_msg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | break; |
| 797 | } |
| 798 | } |
| 799 | tmp = tmp->next; |
| 800 | } |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 801 | if (!IS_ERR(msg)) { |
| 802 | /* |
| 803 | * Found a suitable message. |
| 804 | * Unlink it from the queue. |
| 805 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) { |
| 807 | msg = ERR_PTR(-E2BIG); |
| 808 | goto out_unlock; |
| 809 | } |
| 810 | list_del(&msg->m_list); |
| 811 | msq->q_qnum--; |
| 812 | msq->q_rtime = get_seconds(); |
| 813 | msq->q_lrpid = current->tgid; |
| 814 | msq->q_cbytes -= msg->m_ts; |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 815 | atomic_sub(msg->m_ts, &msg_bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | atomic_dec(&msg_hdrs); |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 817 | ss_wakeup(&msq->q_senders, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | msg_unlock(msq); |
| 819 | break; |
| 820 | } |
| 821 | /* No message waiting. Wait for a message */ |
| 822 | if (msgflg & IPC_NOWAIT) { |
| 823 | msg = ERR_PTR(-ENOMSG); |
| 824 | goto out_unlock; |
| 825 | } |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 826 | list_add_tail(&msr_d.r_list, &msq->q_receivers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | msr_d.r_tsk = current; |
| 828 | msr_d.r_msgtype = msgtyp; |
| 829 | msr_d.r_mode = mode; |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 830 | if (msgflg & MSG_NOERROR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | msr_d.r_maxsize = INT_MAX; |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 832 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | msr_d.r_maxsize = msgsz; |
| 834 | msr_d.r_msg = ERR_PTR(-EAGAIN); |
| 835 | current->state = TASK_INTERRUPTIBLE; |
| 836 | msg_unlock(msq); |
| 837 | |
| 838 | schedule(); |
| 839 | |
| 840 | /* Lockless receive, part 1: |
| 841 | * Disable preemption. We don't hold a reference to the queue |
| 842 | * and getting a reference would defeat the idea of a lockless |
| 843 | * operation, thus the code relies on rcu to guarantee the |
| 844 | * existance of msq: |
| 845 | * Prior to destruction, expunge_all(-EIRDM) changes r_msg. |
| 846 | * Thus if r_msg is -EAGAIN, then the queue not yet destroyed. |
| 847 | * rcu_read_lock() prevents preemption between reading r_msg |
| 848 | * and the spin_lock() inside ipc_lock_by_ptr(). |
| 849 | */ |
| 850 | rcu_read_lock(); |
| 851 | |
| 852 | /* Lockless receive, part 2: |
| 853 | * Wait until pipelined_send or expunge_all are outside of |
| 854 | * wake_up_process(). There is a race with exit(), see |
| 855 | * ipc/mqueue.c for the details. |
| 856 | */ |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 857 | msg = (struct msg_msg*)msr_d.r_msg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | while (msg == NULL) { |
| 859 | cpu_relax(); |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 860 | msg = (struct msg_msg *)msr_d.r_msg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | /* Lockless receive, part 3: |
| 864 | * If there is a message or an error then accept it without |
| 865 | * locking. |
| 866 | */ |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 867 | if (msg != ERR_PTR(-EAGAIN)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | rcu_read_unlock(); |
| 869 | break; |
| 870 | } |
| 871 | |
| 872 | /* Lockless receive, part 3: |
| 873 | * Acquire the queue spinlock. |
| 874 | */ |
| 875 | ipc_lock_by_ptr(&msq->q_perm); |
| 876 | rcu_read_unlock(); |
| 877 | |
| 878 | /* Lockless receive, part 4: |
| 879 | * Repeat test after acquiring the spinlock. |
| 880 | */ |
| 881 | msg = (struct msg_msg*)msr_d.r_msg; |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 882 | if (msg != ERR_PTR(-EAGAIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | goto out_unlock; |
| 884 | |
| 885 | list_del(&msr_d.r_list); |
| 886 | if (signal_pending(current)) { |
| 887 | msg = ERR_PTR(-ERESTARTNOHAND); |
| 888 | out_unlock: |
| 889 | msg_unlock(msq); |
| 890 | break; |
| 891 | } |
| 892 | } |
| 893 | if (IS_ERR(msg)) |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 894 | return PTR_ERR(msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | |
| 896 | msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz; |
suzuki | 651971c | 2006-12-06 20:37:48 -0800 | [diff] [blame] | 897 | *pmtype = msg->m_type; |
| 898 | if (store_msg(mtext, msg, msgsz)) |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 899 | msgsz = -EFAULT; |
suzuki | 651971c | 2006-12-06 20:37:48 -0800 | [diff] [blame] | 900 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | free_msg(msg); |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 902 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | return msgsz; |
| 904 | } |
| 905 | |
suzuki | 651971c | 2006-12-06 20:37:48 -0800 | [diff] [blame] | 906 | asmlinkage long sys_msgrcv(int msqid, struct msgbuf __user *msgp, size_t msgsz, |
| 907 | long msgtyp, int msgflg) |
| 908 | { |
| 909 | long err, mtype; |
| 910 | |
| 911 | err = do_msgrcv(msqid, &mtype, msgp->mtext, msgsz, msgtyp, msgflg); |
| 912 | if (err < 0) |
| 913 | goto out; |
| 914 | |
| 915 | if (put_user(mtype, &msgp->mtype)) |
| 916 | err = -EFAULT; |
| 917 | out: |
| 918 | return err; |
| 919 | } |
| 920 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | #ifdef CONFIG_PROC_FS |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 922 | static int sysvipc_msg_proc_show(struct seq_file *s, void *it) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | { |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 924 | struct msg_queue *msq = it; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 926 | return seq_printf(s, |
Ingo Molnar | 5a06a36 | 2006-07-30 03:04:11 -0700 | [diff] [blame] | 927 | "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n", |
| 928 | msq->q_perm.key, |
| 929 | msq->q_id, |
| 930 | msq->q_perm.mode, |
| 931 | msq->q_cbytes, |
| 932 | msq->q_qnum, |
| 933 | msq->q_lspid, |
| 934 | msq->q_lrpid, |
| 935 | msq->q_perm.uid, |
| 936 | msq->q_perm.gid, |
| 937 | msq->q_perm.cuid, |
| 938 | msq->q_perm.cgid, |
| 939 | msq->q_stime, |
| 940 | msq->q_rtime, |
| 941 | msq->q_ctime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | } |
| 943 | #endif |