blob: 08591a0278ce23fe67af16328f6f632480ef23b3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/ipc/msg.c
Ingo Molnar5a06a362006-07-30 03:04:11 -07003 * Copyright (C) 1992 Krishna Balasubramanian
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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 Kujau624dffc2006-01-15 02:43:54 +010015 * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
Steve Grubb073115d2006-04-02 17:07:33 -040016 *
17 * support for audit of ipc object properties and permission changes
18 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
Kirill Korotaev1e786932006-10-02 02:18:21 -070019 *
20 * namespaces support
21 * OpenVZ, SWsoft Inc.
22 * Pavel Emelianov <xemul@openvz.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 */
24
Randy.Dunlapc59ede72006-01-11 12:17:46 -080025#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#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 Waychison19b49462005-09-06 15:17:10 -070036#include <linux/seq_file.h>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080037#include <linux/mutex.h>
Kirill Korotaev1e786932006-10-02 02:18:21 -070038#include <linux/nsproxy.h>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/current.h>
41#include <asm/uaccess.h>
42#include "util.h"
43
Ingo Molnar5a06a362006-07-30 03:04:11 -070044/*
45 * one msg_receiver structure for each sleeping receiver:
46 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070047struct msg_receiver {
Ingo Molnar5a06a362006-07-30 03:04:11 -070048 struct list_head r_list;
49 struct task_struct *r_tsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Ingo Molnar5a06a362006-07-30 03:04:11 -070051 int r_mode;
52 long r_msgtype;
53 long r_maxsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds80491eb2006-11-04 09:55:00 -080055 struct msg_msg *volatile r_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056};
57
58/* one msg_sender for each sleeping sender */
59struct msg_sender {
Ingo Molnar5a06a362006-07-30 03:04:11 -070060 struct list_head list;
61 struct task_struct *tsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
63
64#define SEARCH_ANY 1
65#define SEARCH_EQUAL 2
66#define SEARCH_NOTEQUAL 3
67#define SEARCH_LESSEQUAL 4
68
Ingo Molnar5a06a362006-07-30 03:04:11 -070069static atomic_t msg_bytes = ATOMIC_INIT(0);
70static atomic_t msg_hdrs = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Kirill Korotaev1e786932006-10-02 02:18:21 -070072static struct ipc_ids init_msg_ids;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Kirill Korotaev1e786932006-10-02 02:18:21 -070074#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 Molnar5a06a362006-07-30 03:04:11 -070077#define msg_unlock(msq) ipc_unlock(&(msq)->q_perm)
Kirill Korotaev1e786932006-10-02 02:18:21 -070078#define msg_checkid(ns, msq, msgid) \
79 ipc_checkid(&msg_ids(ns), &msq->q_perm, msgid)
80#define msg_buildid(ns, id, seq) \
81 ipc_buildid(&msg_ids(ns), id, seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Nadia Derbey7ca7e562007-10-18 23:40:48 -070083static void freeque(struct ipc_namespace *, struct msg_queue *);
Kirill Korotaev1e786932006-10-02 02:18:21 -070084static int newque (struct ipc_namespace *ns, key_t key, int msgflg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -070086static int sysvipc_msg_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#endif
88
Cedric Le Goater7d69a1f2007-07-15 23:40:58 -070089static void __msg_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Kirill Korotaev1e786932006-10-02 02:18:21 -070091 ns->ids[IPC_MSG_IDS] = ids;
92 ns->msg_ctlmax = MSGMAX;
93 ns->msg_ctlmnb = MSGMNB;
94 ns->msg_ctlmni = MSGMNI;
Nadia Derbey7ca7e562007-10-18 23:40:48 -070095 ipc_init_ids(ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Kirill Korotaev1e786932006-10-02 02:18:21 -070098int msg_init_ns(struct ipc_namespace *ns)
99{
100 struct ipc_ids *ids;
101
102 ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
103 if (ids == NULL)
104 return -ENOMEM;
105
106 __msg_init_ns(ns, ids);
107 return 0;
108}
109
110void msg_exit_ns(struct ipc_namespace *ns)
111{
Kirill Korotaev1e786932006-10-02 02:18:21 -0700112 struct msg_queue *msq;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700113 int next_id;
114 int total, in_use;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700115
116 mutex_lock(&msg_ids(ns).mutex);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700117
118 in_use = msg_ids(ns).in_use;
119
120 for (total = 0, next_id = 0; total < in_use; next_id++) {
121 msq = idr_find(&msg_ids(ns).ipcs_idr, next_id);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700122 if (msq == NULL)
123 continue;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700124 ipc_lock_by_ptr(&msq->q_perm);
125 freeque(ns, msq);
126 total++;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700127 }
128 mutex_unlock(&msg_ids(ns).mutex);
129
130 kfree(ns->ids[IPC_MSG_IDS]);
131 ns->ids[IPC_MSG_IDS] = NULL;
132}
Kirill Korotaev1e786932006-10-02 02:18:21 -0700133
134void __init msg_init(void)
135{
136 __msg_init_ns(&init_ipc_ns, &init_msg_ids);
137 ipc_init_proc_interface("sysvipc/msg",
138 " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
139 IPC_MSG_IDS, sysvipc_msg_proc_show);
140}
141
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700142static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s)
143{
144 ipc_rmid(&msg_ids(ns), &s->q_perm);
145}
146
Kirill Korotaev1e786932006-10-02 02:18:21 -0700147static int newque (struct ipc_namespace *ns, key_t key, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 struct msg_queue *msq;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700150 int id, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Ingo Molnar5a06a362006-07-30 03:04:11 -0700152 msq = ipc_rcu_alloc(sizeof(*msq));
153 if (!msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return -ENOMEM;
155
Ingo Molnar5a06a362006-07-30 03:04:11 -0700156 msq->q_perm.mode = msgflg & S_IRWXUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 msq->q_perm.key = key;
158
159 msq->q_perm.security = NULL;
160 retval = security_msg_queue_alloc(msq);
161 if (retval) {
162 ipc_rcu_putref(msq);
163 return retval;
164 }
165
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700166 /*
167 * ipc_addid() locks msq
168 */
Kirill Korotaev1e786932006-10-02 02:18:21 -0700169 id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700170 if (id == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 security_msg_queue_free(msq);
172 ipc_rcu_putref(msq);
173 return -ENOSPC;
174 }
175
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700176 msq->q_perm.id = msg_buildid(ns, id, msq->q_perm.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 msq->q_stime = msq->q_rtime = 0;
178 msq->q_ctime = get_seconds();
179 msq->q_cbytes = msq->q_qnum = 0;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700180 msq->q_qbytes = ns->msg_ctlmnb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 msq->q_lspid = msq->q_lrpid = 0;
182 INIT_LIST_HEAD(&msq->q_messages);
183 INIT_LIST_HEAD(&msq->q_receivers);
184 INIT_LIST_HEAD(&msq->q_senders);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 msg_unlock(msq);
187
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700188 return msq->q_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
Ingo Molnar5a06a362006-07-30 03:04:11 -0700191static inline void ss_add(struct msg_queue *msq, struct msg_sender *mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700193 mss->tsk = current;
194 current->state = TASK_INTERRUPTIBLE;
195 list_add_tail(&mss->list, &msq->q_senders);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
Ingo Molnar5a06a362006-07-30 03:04:11 -0700198static inline void ss_del(struct msg_sender *mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700200 if (mss->list.next != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 list_del(&mss->list);
202}
203
Ingo Molnar5a06a362006-07-30 03:04:11 -0700204static void ss_wakeup(struct list_head *h, int kill)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 struct list_head *tmp;
207
208 tmp = h->next;
209 while (tmp != h) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700210 struct msg_sender *mss;
211
212 mss = list_entry(tmp, struct msg_sender, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 tmp = tmp->next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700214 if (kill)
215 mss->list.next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 wake_up_process(mss->tsk);
217 }
218}
219
Ingo Molnar5a06a362006-07-30 03:04:11 -0700220static void expunge_all(struct msg_queue *msq, int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 struct list_head *tmp;
223
224 tmp = msq->q_receivers.next;
225 while (tmp != &msq->q_receivers) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700226 struct msg_receiver *msr;
227
228 msr = list_entry(tmp, struct msg_receiver, r_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 tmp = tmp->next;
230 msr->r_msg = NULL;
231 wake_up_process(msr->r_tsk);
232 smp_mb();
233 msr->r_msg = ERR_PTR(res);
234 }
235}
Ingo Molnar5a06a362006-07-30 03:04:11 -0700236
237/*
238 * freeque() wakes up waiters on the sender and receiver waiting queue,
239 * removes the message queue from message queue ID
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700240 * IDR, and cleans up all the messages associated with this queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 *
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700242 * msg_ids.mutex and the spinlock for this message queue are held
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800243 * before freeque() is called. msg_ids.mutex remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 */
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700245static void freeque(struct ipc_namespace *ns, struct msg_queue *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 struct list_head *tmp;
248
Ingo Molnar5a06a362006-07-30 03:04:11 -0700249 expunge_all(msq, -EIDRM);
250 ss_wakeup(&msq->q_senders, 1);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700251 msg_rmid(ns, msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 msg_unlock(msq);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 tmp = msq->q_messages.next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700255 while (tmp != &msq->q_messages) {
256 struct msg_msg *msg = list_entry(tmp, struct msg_msg, m_list);
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 tmp = tmp->next;
259 atomic_dec(&msg_hdrs);
260 free_msg(msg);
261 }
262 atomic_sub(msq->q_cbytes, &msg_bytes);
263 security_msg_queue_free(msq);
264 ipc_rcu_putref(msq);
265}
266
Ingo Molnar5a06a362006-07-30 03:04:11 -0700267asmlinkage long sys_msgget(key_t key, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 struct msg_queue *msq;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700270 int ret;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700271 struct ipc_namespace *ns;
272
273 ns = current->nsproxy->ipc_ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700274
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700275 ret = idr_pre_get(&msg_ids(ns).ipcs_idr, GFP_KERNEL);
276
277 if (key == IPC_PRIVATE) {
278 if (!ret)
279 ret = -ENOMEM;
280 else {
281 mutex_lock(&msg_ids(ns).mutex);
282 ret = newque(ns, key, msgflg);
283 mutex_unlock(&msg_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700285 } else {
286 mutex_lock(&msg_ids(ns).mutex);
287 msq = (struct msg_queue *) ipc_findkey(&msg_ids(ns), key);
288 if (msq == NULL) {
289 /* key not used */
290 if (!(msgflg & IPC_CREAT))
291 ret = -ENOENT;
292 else if (!ret)
293 ret = -ENOMEM;
294 else
295 ret = newque(ns, key, msgflg);
296 } else {
297 /* msq has been locked by ipc_findkey() */
298
299 if (msgflg & IPC_CREAT && msgflg & IPC_EXCL)
300 ret = -EEXIST;
301 else {
302 if (ipcperms(&msq->q_perm, msgflg))
303 ret = -EACCES;
304 else {
305 ret = security_msg_queue_associate(
306 msq, msgflg);
307 if (!ret)
308 ret = msq->q_perm.id;
309 }
310 }
311 msg_unlock(msq);
312 }
313 mutex_unlock(&msg_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return ret;
317}
318
Ingo Molnar5a06a362006-07-30 03:04:11 -0700319static inline unsigned long
320copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 switch(version) {
323 case IPC_64:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700324 return copy_to_user(buf, in, sizeof(*in));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 case IPC_OLD:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700326 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 struct msqid_ds out;
328
Ingo Molnar5a06a362006-07-30 03:04:11 -0700329 memset(&out, 0, sizeof(out));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
332
333 out.msg_stime = in->msg_stime;
334 out.msg_rtime = in->msg_rtime;
335 out.msg_ctime = in->msg_ctime;
336
Ingo Molnar5a06a362006-07-30 03:04:11 -0700337 if (in->msg_cbytes > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 out.msg_cbytes = USHRT_MAX;
339 else
340 out.msg_cbytes = in->msg_cbytes;
341 out.msg_lcbytes = in->msg_cbytes;
342
Ingo Molnar5a06a362006-07-30 03:04:11 -0700343 if (in->msg_qnum > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 out.msg_qnum = USHRT_MAX;
345 else
346 out.msg_qnum = in->msg_qnum;
347
Ingo Molnar5a06a362006-07-30 03:04:11 -0700348 if (in->msg_qbytes > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 out.msg_qbytes = USHRT_MAX;
350 else
351 out.msg_qbytes = in->msg_qbytes;
352 out.msg_lqbytes = in->msg_qbytes;
353
354 out.msg_lspid = in->msg_lspid;
355 out.msg_lrpid = in->msg_lrpid;
356
Ingo Molnar5a06a362006-07-30 03:04:11 -0700357 return copy_to_user(buf, &out, sizeof(out));
358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 default:
360 return -EINVAL;
361 }
362}
363
364struct msq_setbuf {
365 unsigned long qbytes;
366 uid_t uid;
367 gid_t gid;
368 mode_t mode;
369};
370
Ingo Molnar5a06a362006-07-30 03:04:11 -0700371static inline unsigned long
372copy_msqid_from_user(struct msq_setbuf *out, void __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 switch(version) {
375 case IPC_64:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700376 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 struct msqid64_ds tbuf;
378
Ingo Molnar5a06a362006-07-30 03:04:11 -0700379 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return -EFAULT;
381
382 out->qbytes = tbuf.msg_qbytes;
383 out->uid = tbuf.msg_perm.uid;
384 out->gid = tbuf.msg_perm.gid;
385 out->mode = tbuf.msg_perm.mode;
386
387 return 0;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 case IPC_OLD:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700390 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 struct msqid_ds tbuf_old;
392
Ingo Molnar5a06a362006-07-30 03:04:11 -0700393 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return -EFAULT;
395
396 out->uid = tbuf_old.msg_perm.uid;
397 out->gid = tbuf_old.msg_perm.gid;
398 out->mode = tbuf_old.msg_perm.mode;
399
Ingo Molnar5a06a362006-07-30 03:04:11 -0700400 if (tbuf_old.msg_qbytes == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 out->qbytes = tbuf_old.msg_lqbytes;
402 else
403 out->qbytes = tbuf_old.msg_qbytes;
404
405 return 0;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 default:
408 return -EINVAL;
409 }
410}
411
Ingo Molnar5a06a362006-07-30 03:04:11 -0700412asmlinkage long sys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 struct kern_ipc_perm *ipcp;
Jeff Garzik8e1c0912007-07-17 05:40:59 -0400415 struct msq_setbuf uninitialized_var(setbuf);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700416 struct msg_queue *msq;
417 int err, version;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700418 struct ipc_namespace *ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (msqid < 0 || cmd < 0)
421 return -EINVAL;
422
423 version = ipc_parse_version(&cmd);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700424 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 switch (cmd) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700427 case IPC_INFO:
428 case MSG_INFO:
429 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 struct msginfo msginfo;
431 int max_id;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (!buf)
434 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700435 /*
436 * We must not return kernel stack data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 * due to padding, it's not enough
438 * to set all member fields.
439 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 err = security_msg_queue_msgctl(NULL, cmd);
441 if (err)
442 return err;
443
Ingo Molnar5a06a362006-07-30 03:04:11 -0700444 memset(&msginfo, 0, sizeof(msginfo));
Kirill Korotaev1e786932006-10-02 02:18:21 -0700445 msginfo.msgmni = ns->msg_ctlmni;
446 msginfo.msgmax = ns->msg_ctlmax;
447 msginfo.msgmnb = ns->msg_ctlmnb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 msginfo.msgssz = MSGSSZ;
449 msginfo.msgseg = MSGSEG;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700450 mutex_lock(&msg_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (cmd == MSG_INFO) {
Kirill Korotaev1e786932006-10-02 02:18:21 -0700452 msginfo.msgpool = msg_ids(ns).in_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 msginfo.msgmap = atomic_read(&msg_hdrs);
454 msginfo.msgtql = atomic_read(&msg_bytes);
455 } else {
456 msginfo.msgmap = MSGMAP;
457 msginfo.msgpool = MSGPOOL;
458 msginfo.msgtql = MSGTQL;
459 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700460 max_id = ipc_get_maxid(&msg_ids(ns));
Kirill Korotaev1e786932006-10-02 02:18:21 -0700461 mutex_unlock(&msg_ids(ns).mutex);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700462 if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700464 return (max_id < 0) ? 0 : max_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700466 case MSG_STAT: /* msqid is an index rather than a msg queue id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 case IPC_STAT:
468 {
469 struct msqid64_ds tbuf;
470 int success_return;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (!buf)
473 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Ingo Molnar5a06a362006-07-30 03:04:11 -0700475 memset(&tbuf, 0, sizeof(tbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Kirill Korotaev1e786932006-10-02 02:18:21 -0700477 msq = msg_lock(ns, msqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (msq == NULL)
479 return -EINVAL;
480
Ingo Molnar5a06a362006-07-30 03:04:11 -0700481 if (cmd == MSG_STAT) {
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700482 success_return = msq->q_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 } else {
484 err = -EIDRM;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700485 if (msg_checkid(ns, msq, msqid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 goto out_unlock;
487 success_return = 0;
488 }
489 err = -EACCES;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700490 if (ipcperms(&msq->q_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 goto out_unlock;
492
493 err = security_msg_queue_msgctl(msq, cmd);
494 if (err)
495 goto out_unlock;
496
497 kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
498 tbuf.msg_stime = msq->q_stime;
499 tbuf.msg_rtime = msq->q_rtime;
500 tbuf.msg_ctime = msq->q_ctime;
501 tbuf.msg_cbytes = msq->q_cbytes;
502 tbuf.msg_qnum = msq->q_qnum;
503 tbuf.msg_qbytes = msq->q_qbytes;
504 tbuf.msg_lspid = msq->q_lspid;
505 tbuf.msg_lrpid = msq->q_lrpid;
506 msg_unlock(msq);
507 if (copy_msqid_to_user(buf, &tbuf, version))
508 return -EFAULT;
509 return success_return;
510 }
511 case IPC_SET:
512 if (!buf)
513 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700514 if (copy_msqid_from_user(&setbuf, buf, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 break;
517 case IPC_RMID:
518 break;
519 default:
520 return -EINVAL;
521 }
522
Kirill Korotaev1e786932006-10-02 02:18:21 -0700523 mutex_lock(&msg_ids(ns).mutex);
524 msq = msg_lock(ns, msqid);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700525 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (msq == NULL)
527 goto out_up;
528
529 err = -EIDRM;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700530 if (msg_checkid(ns, msq, msqid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 goto out_unlock_up;
532 ipcp = &msq->q_perm;
Steve Grubb073115d2006-04-02 17:07:33 -0400533
534 err = audit_ipc_obj(ipcp);
535 if (err)
536 goto out_unlock_up;
Jeff Garzik8e1c0912007-07-17 05:40:59 -0400537 if (cmd == IPC_SET) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700538 err = audit_ipc_set_perm(setbuf.qbytes, setbuf.uid, setbuf.gid,
539 setbuf.mode);
Linda Knippersac032212006-05-16 22:03:48 -0400540 if (err)
541 goto out_unlock_up;
542 }
Steve Grubb073115d2006-04-02 17:07:33 -0400543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 err = -EPERM;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700545 if (current->euid != ipcp->cuid &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700547 /* We _could_ check for CAP_CHOWN above, but we don't */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 goto out_unlock_up;
549
550 err = security_msg_queue_msgctl(msq, cmd);
551 if (err)
552 goto out_unlock_up;
553
554 switch (cmd) {
555 case IPC_SET:
556 {
557 err = -EPERM;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700558 if (setbuf.qbytes > ns->msg_ctlmnb && !capable(CAP_SYS_RESOURCE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 goto out_unlock_up;
560
561 msq->q_qbytes = setbuf.qbytes;
562
563 ipcp->uid = setbuf.uid;
564 ipcp->gid = setbuf.gid;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700565 ipcp->mode = (ipcp->mode & ~S_IRWXUGO) |
566 (S_IRWXUGO & setbuf.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 msq->q_ctime = get_seconds();
568 /* sleeping receivers might be excluded by
569 * stricter permissions.
570 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700571 expunge_all(msq, -EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 /* sleeping senders might be able to send
573 * due to a larger queue size.
574 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700575 ss_wakeup(&msq->q_senders, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 msg_unlock(msq);
577 break;
578 }
579 case IPC_RMID:
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700580 freeque(ns, msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 break;
582 }
583 err = 0;
584out_up:
Kirill Korotaev1e786932006-10-02 02:18:21 -0700585 mutex_unlock(&msg_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return err;
587out_unlock_up:
588 msg_unlock(msq);
589 goto out_up;
590out_unlock:
591 msg_unlock(msq);
592 return err;
593}
594
Ingo Molnar5a06a362006-07-30 03:04:11 -0700595static int testmsg(struct msg_msg *msg, long type, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
597 switch(mode)
598 {
599 case SEARCH_ANY:
600 return 1;
601 case SEARCH_LESSEQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700602 if (msg->m_type <=type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return 1;
604 break;
605 case SEARCH_EQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700606 if (msg->m_type == type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return 1;
608 break;
609 case SEARCH_NOTEQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700610 if (msg->m_type != type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return 1;
612 break;
613 }
614 return 0;
615}
616
Ingo Molnar5a06a362006-07-30 03:04:11 -0700617static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700619 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 tmp = msq->q_receivers.next;
622 while (tmp != &msq->q_receivers) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700623 struct msg_receiver *msr;
624
625 msr = list_entry(tmp, struct msg_receiver, r_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 tmp = tmp->next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700627 if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
628 !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
629 msr->r_msgtype, msr->r_mode)) {
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 list_del(&msr->r_list);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700632 if (msr->r_maxsize < msg->m_ts) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 msr->r_msg = NULL;
634 wake_up_process(msr->r_tsk);
635 smp_mb();
636 msr->r_msg = ERR_PTR(-E2BIG);
637 } else {
638 msr->r_msg = NULL;
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700639 msq->q_lrpid = task_pid_vnr(msr->r_tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 msq->q_rtime = get_seconds();
641 wake_up_process(msr->r_tsk);
642 smp_mb();
643 msr->r_msg = msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return 1;
646 }
647 }
648 }
649 return 0;
650}
651
suzuki651971c2006-12-06 20:37:48 -0800652long do_msgsnd(int msqid, long mtype, void __user *mtext,
653 size_t msgsz, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
655 struct msg_queue *msq;
656 struct msg_msg *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 int err;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700658 struct ipc_namespace *ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700659
Kirill Korotaev1e786932006-10-02 02:18:21 -0700660 ns = current->nsproxy->ipc_ns;
661
662 if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (mtype < 1)
665 return -EINVAL;
666
suzuki651971c2006-12-06 20:37:48 -0800667 msg = load_msg(mtext, msgsz);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700668 if (IS_ERR(msg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return PTR_ERR(msg);
670
671 msg->m_type = mtype;
672 msg->m_ts = msgsz;
673
Kirill Korotaev1e786932006-10-02 02:18:21 -0700674 msq = msg_lock(ns, msqid);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700675 err = -EINVAL;
676 if (msq == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 goto out_free;
678
679 err= -EIDRM;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700680 if (msg_checkid(ns, msq, msqid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 goto out_unlock_free;
682
683 for (;;) {
684 struct msg_sender s;
685
Ingo Molnar5a06a362006-07-30 03:04:11 -0700686 err = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (ipcperms(&msq->q_perm, S_IWUGO))
688 goto out_unlock_free;
689
690 err = security_msg_queue_msgsnd(msq, msg, msgflg);
691 if (err)
692 goto out_unlock_free;
693
Ingo Molnar5a06a362006-07-30 03:04:11 -0700694 if (msgsz + msq->q_cbytes <= msq->q_qbytes &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 1 + msq->q_qnum <= msq->q_qbytes) {
696 break;
697 }
698
699 /* queue full, wait: */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700700 if (msgflg & IPC_NOWAIT) {
701 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 goto out_unlock_free;
703 }
704 ss_add(msq, &s);
705 ipc_rcu_getref(msq);
706 msg_unlock(msq);
707 schedule();
708
709 ipc_lock_by_ptr(&msq->q_perm);
710 ipc_rcu_putref(msq);
711 if (msq->q_perm.deleted) {
712 err = -EIDRM;
713 goto out_unlock_free;
714 }
715 ss_del(&s);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (signal_pending(current)) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700718 err = -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 goto out_unlock_free;
720 }
721 }
722
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700723 msq->q_lspid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 msq->q_stime = get_seconds();
725
Ingo Molnar5a06a362006-07-30 03:04:11 -0700726 if (!pipelined_send(msq, msg)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 /* noone is waiting for this message, enqueue it */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700728 list_add_tail(&msg->m_list, &msq->q_messages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 msq->q_cbytes += msgsz;
730 msq->q_qnum++;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700731 atomic_add(msgsz, &msg_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 atomic_inc(&msg_hdrs);
733 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 err = 0;
736 msg = NULL;
737
738out_unlock_free:
739 msg_unlock(msq);
740out_free:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700741 if (msg != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 free_msg(msg);
743 return err;
744}
745
suzuki651971c2006-12-06 20:37:48 -0800746asmlinkage long
747sys_msgsnd(int msqid, struct msgbuf __user *msgp, size_t msgsz, int msgflg)
748{
749 long mtype;
750
751 if (get_user(mtype, &msgp->mtype))
752 return -EFAULT;
753 return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
754}
755
Ingo Molnar5a06a362006-07-30 03:04:11 -0700756static inline int convert_mode(long *msgtyp, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700758 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 * find message of correct type.
760 * msgtyp = 0 => get first.
761 * msgtyp > 0 => get first message of matching type.
Ingo Molnar5a06a362006-07-30 03:04:11 -0700762 * msgtyp < 0 => get message with least type must be < abs(msgtype).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700764 if (*msgtyp == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return SEARCH_ANY;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700766 if (*msgtyp < 0) {
767 *msgtyp = -*msgtyp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return SEARCH_LESSEQUAL;
769 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700770 if (msgflg & MSG_EXCEPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return SEARCH_NOTEQUAL;
772 return SEARCH_EQUAL;
773}
774
suzuki651971c2006-12-06 20:37:48 -0800775long do_msgrcv(int msqid, long *pmtype, void __user *mtext,
776 size_t msgsz, long msgtyp, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 struct msg_queue *msq;
779 struct msg_msg *msg;
780 int mode;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700781 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 if (msqid < 0 || (long) msgsz < 0)
784 return -EINVAL;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700785 mode = convert_mode(&msgtyp, msgflg);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700786 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Kirill Korotaev1e786932006-10-02 02:18:21 -0700788 msq = msg_lock(ns, msqid);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700789 if (msq == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return -EINVAL;
791
792 msg = ERR_PTR(-EIDRM);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700793 if (msg_checkid(ns, msq, msqid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 goto out_unlock;
795
796 for (;;) {
797 struct msg_receiver msr_d;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700798 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 msg = ERR_PTR(-EACCES);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700801 if (ipcperms(&msq->q_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 goto out_unlock;
803
804 msg = ERR_PTR(-EAGAIN);
805 tmp = msq->q_messages.next;
806 while (tmp != &msq->q_messages) {
807 struct msg_msg *walk_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700808
809 walk_msg = list_entry(tmp, struct msg_msg, m_list);
810 if (testmsg(walk_msg, msgtyp, mode) &&
811 !security_msg_queue_msgrcv(msq, walk_msg, current,
812 msgtyp, mode)) {
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 msg = walk_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700815 if (mode == SEARCH_LESSEQUAL &&
816 walk_msg->m_type != 1) {
817 msg = walk_msg;
818 msgtyp = walk_msg->m_type - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 } else {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700820 msg = walk_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 break;
822 }
823 }
824 tmp = tmp->next;
825 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700826 if (!IS_ERR(msg)) {
827 /*
828 * Found a suitable message.
829 * Unlink it from the queue.
830 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
832 msg = ERR_PTR(-E2BIG);
833 goto out_unlock;
834 }
835 list_del(&msg->m_list);
836 msq->q_qnum--;
837 msq->q_rtime = get_seconds();
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700838 msq->q_lrpid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 msq->q_cbytes -= msg->m_ts;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700840 atomic_sub(msg->m_ts, &msg_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 atomic_dec(&msg_hdrs);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700842 ss_wakeup(&msq->q_senders, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 msg_unlock(msq);
844 break;
845 }
846 /* No message waiting. Wait for a message */
847 if (msgflg & IPC_NOWAIT) {
848 msg = ERR_PTR(-ENOMSG);
849 goto out_unlock;
850 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700851 list_add_tail(&msr_d.r_list, &msq->q_receivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 msr_d.r_tsk = current;
853 msr_d.r_msgtype = msgtyp;
854 msr_d.r_mode = mode;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700855 if (msgflg & MSG_NOERROR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 msr_d.r_maxsize = INT_MAX;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700857 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 msr_d.r_maxsize = msgsz;
859 msr_d.r_msg = ERR_PTR(-EAGAIN);
860 current->state = TASK_INTERRUPTIBLE;
861 msg_unlock(msq);
862
863 schedule();
864
865 /* Lockless receive, part 1:
866 * Disable preemption. We don't hold a reference to the queue
867 * and getting a reference would defeat the idea of a lockless
868 * operation, thus the code relies on rcu to guarantee the
869 * existance of msq:
870 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
871 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
872 * rcu_read_lock() prevents preemption between reading r_msg
873 * and the spin_lock() inside ipc_lock_by_ptr().
874 */
875 rcu_read_lock();
876
877 /* Lockless receive, part 2:
878 * Wait until pipelined_send or expunge_all are outside of
879 * wake_up_process(). There is a race with exit(), see
880 * ipc/mqueue.c for the details.
881 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700882 msg = (struct msg_msg*)msr_d.r_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 while (msg == NULL) {
884 cpu_relax();
Ingo Molnar5a06a362006-07-30 03:04:11 -0700885 msg = (struct msg_msg *)msr_d.r_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
887
888 /* Lockless receive, part 3:
889 * If there is a message or an error then accept it without
890 * locking.
891 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700892 if (msg != ERR_PTR(-EAGAIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 rcu_read_unlock();
894 break;
895 }
896
897 /* Lockless receive, part 3:
898 * Acquire the queue spinlock.
899 */
900 ipc_lock_by_ptr(&msq->q_perm);
901 rcu_read_unlock();
902
903 /* Lockless receive, part 4:
904 * Repeat test after acquiring the spinlock.
905 */
906 msg = (struct msg_msg*)msr_d.r_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700907 if (msg != ERR_PTR(-EAGAIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 goto out_unlock;
909
910 list_del(&msr_d.r_list);
911 if (signal_pending(current)) {
912 msg = ERR_PTR(-ERESTARTNOHAND);
913out_unlock:
914 msg_unlock(msq);
915 break;
916 }
917 }
918 if (IS_ERR(msg))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700919 return PTR_ERR(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
suzuki651971c2006-12-06 20:37:48 -0800922 *pmtype = msg->m_type;
923 if (store_msg(mtext, msg, msgsz))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700924 msgsz = -EFAULT;
suzuki651971c2006-12-06 20:37:48 -0800925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 free_msg(msg);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return msgsz;
929}
930
suzuki651971c2006-12-06 20:37:48 -0800931asmlinkage long sys_msgrcv(int msqid, struct msgbuf __user *msgp, size_t msgsz,
932 long msgtyp, int msgflg)
933{
934 long err, mtype;
935
936 err = do_msgrcv(msqid, &mtype, msgp->mtext, msgsz, msgtyp, msgflg);
937 if (err < 0)
938 goto out;
939
940 if (put_user(mtype, &msgp->mtype))
941 err = -EFAULT;
942out:
943 return err;
944}
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -0700947static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Mike Waychison19b49462005-09-06 15:17:10 -0700949 struct msg_queue *msq = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Mike Waychison19b49462005-09-06 15:17:10 -0700951 return seq_printf(s,
Ingo Molnar5a06a362006-07-30 03:04:11 -0700952 "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
953 msq->q_perm.key,
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700954 msq->q_perm.id,
Ingo Molnar5a06a362006-07-30 03:04:11 -0700955 msq->q_perm.mode,
956 msq->q_cbytes,
957 msq->q_qnum,
958 msq->q_lspid,
959 msq->q_lrpid,
960 msq->q_perm.uid,
961 msq->q_perm.gid,
962 msq->q_perm.cuid,
963 msq->q_perm.cgid,
964 msq->q_stime,
965 msq->q_rtime,
966 msq->q_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968#endif