blob: 74e67203567560b18df4605297c23ccfea3a1f9b [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
Ingo Molnar5a06a362006-07-30 03:04:11 -070076#define msg_unlock(msq) ipc_unlock(&(msq)->q_perm)
Kirill Korotaev1e786932006-10-02 02:18:21 -070077#define msg_buildid(ns, id, seq) \
78 ipc_buildid(&msg_ids(ns), id, seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Nadia Derbey7ca7e562007-10-18 23:40:48 -070080static void freeque(struct ipc_namespace *, struct msg_queue *);
Nadia Derbey7748dbf2007-10-18 23:40:49 -070081static int newque(struct ipc_namespace *, struct ipc_params *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -070083static int sysvipc_msg_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#endif
85
Cedric Le Goater7d69a1f2007-07-15 23:40:58 -070086static void __msg_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Kirill Korotaev1e786932006-10-02 02:18:21 -070088 ns->ids[IPC_MSG_IDS] = ids;
89 ns->msg_ctlmax = MSGMAX;
90 ns->msg_ctlmnb = MSGMNB;
91 ns->msg_ctlmni = MSGMNI;
Nadia Derbey7ca7e562007-10-18 23:40:48 -070092 ipc_init_ids(ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Kirill Korotaev1e786932006-10-02 02:18:21 -070095int msg_init_ns(struct ipc_namespace *ns)
96{
97 struct ipc_ids *ids;
98
99 ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
100 if (ids == NULL)
101 return -ENOMEM;
102
103 __msg_init_ns(ns, ids);
104 return 0;
105}
106
107void msg_exit_ns(struct ipc_namespace *ns)
108{
Kirill Korotaev1e786932006-10-02 02:18:21 -0700109 struct msg_queue *msq;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700110 int next_id;
111 int total, in_use;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700112
113 mutex_lock(&msg_ids(ns).mutex);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700114
115 in_use = msg_ids(ns).in_use;
116
117 for (total = 0, next_id = 0; total < in_use; next_id++) {
118 msq = idr_find(&msg_ids(ns).ipcs_idr, next_id);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700119 if (msq == NULL)
120 continue;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700121 ipc_lock_by_ptr(&msq->q_perm);
122 freeque(ns, msq);
123 total++;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700124 }
125 mutex_unlock(&msg_ids(ns).mutex);
126
127 kfree(ns->ids[IPC_MSG_IDS]);
128 ns->ids[IPC_MSG_IDS] = NULL;
129}
Kirill Korotaev1e786932006-10-02 02:18:21 -0700130
131void __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
Nadia Derbey023a5352007-10-18 23:40:51 -0700139static inline struct msg_queue *msg_lock(struct ipc_namespace *ns, int id)
140{
141 return (struct msg_queue *) ipc_lock(&msg_ids(ns), id);
142}
143
144static inline struct msg_queue *msg_lock_check(struct ipc_namespace *ns,
145 int id)
146{
147 return (struct msg_queue *) ipc_lock_check(&msg_ids(ns), id);
148}
149
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700150static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s)
151{
152 ipc_rmid(&msg_ids(ns), &s->q_perm);
153}
154
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700155static int newque(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 struct msg_queue *msq;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700158 int id, retval;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700159 key_t key = params->key;
160 int msgflg = params->flg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Ingo Molnar5a06a362006-07-30 03:04:11 -0700162 msq = ipc_rcu_alloc(sizeof(*msq));
163 if (!msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return -ENOMEM;
165
Ingo Molnar5a06a362006-07-30 03:04:11 -0700166 msq->q_perm.mode = msgflg & S_IRWXUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 msq->q_perm.key = key;
168
169 msq->q_perm.security = NULL;
170 retval = security_msg_queue_alloc(msq);
171 if (retval) {
172 ipc_rcu_putref(msq);
173 return retval;
174 }
175
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700176 /*
177 * ipc_addid() locks msq
178 */
Kirill Korotaev1e786932006-10-02 02:18:21 -0700179 id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700180 if (id == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 security_msg_queue_free(msq);
182 ipc_rcu_putref(msq);
183 return -ENOSPC;
184 }
185
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700186 msq->q_perm.id = msg_buildid(ns, id, msq->q_perm.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 msq->q_stime = msq->q_rtime = 0;
188 msq->q_ctime = get_seconds();
189 msq->q_cbytes = msq->q_qnum = 0;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700190 msq->q_qbytes = ns->msg_ctlmnb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 msq->q_lspid = msq->q_lrpid = 0;
192 INIT_LIST_HEAD(&msq->q_messages);
193 INIT_LIST_HEAD(&msq->q_receivers);
194 INIT_LIST_HEAD(&msq->q_senders);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 msg_unlock(msq);
197
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700198 return msq->q_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Ingo Molnar5a06a362006-07-30 03:04:11 -0700201static inline void ss_add(struct msg_queue *msq, struct msg_sender *mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700203 mss->tsk = current;
204 current->state = TASK_INTERRUPTIBLE;
205 list_add_tail(&mss->list, &msq->q_senders);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Ingo Molnar5a06a362006-07-30 03:04:11 -0700208static inline void ss_del(struct msg_sender *mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700210 if (mss->list.next != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 list_del(&mss->list);
212}
213
Ingo Molnar5a06a362006-07-30 03:04:11 -0700214static void ss_wakeup(struct list_head *h, int kill)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 struct list_head *tmp;
217
218 tmp = h->next;
219 while (tmp != h) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700220 struct msg_sender *mss;
221
222 mss = list_entry(tmp, struct msg_sender, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 tmp = tmp->next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700224 if (kill)
225 mss->list.next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 wake_up_process(mss->tsk);
227 }
228}
229
Ingo Molnar5a06a362006-07-30 03:04:11 -0700230static void expunge_all(struct msg_queue *msq, int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 struct list_head *tmp;
233
234 tmp = msq->q_receivers.next;
235 while (tmp != &msq->q_receivers) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700236 struct msg_receiver *msr;
237
238 msr = list_entry(tmp, struct msg_receiver, r_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 tmp = tmp->next;
240 msr->r_msg = NULL;
241 wake_up_process(msr->r_tsk);
242 smp_mb();
243 msr->r_msg = ERR_PTR(res);
244 }
245}
Ingo Molnar5a06a362006-07-30 03:04:11 -0700246
247/*
248 * freeque() wakes up waiters on the sender and receiver waiting queue,
249 * removes the message queue from message queue ID
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700250 * IDR, and cleans up all the messages associated with this queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 *
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700252 * msg_ids.mutex and the spinlock for this message queue are held
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800253 * before freeque() is called. msg_ids.mutex remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 */
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700255static void freeque(struct ipc_namespace *ns, struct msg_queue *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 struct list_head *tmp;
258
Ingo Molnar5a06a362006-07-30 03:04:11 -0700259 expunge_all(msq, -EIDRM);
260 ss_wakeup(&msq->q_senders, 1);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700261 msg_rmid(ns, msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 msg_unlock(msq);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 tmp = msq->q_messages.next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700265 while (tmp != &msq->q_messages) {
266 struct msg_msg *msg = list_entry(tmp, struct msg_msg, m_list);
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 tmp = tmp->next;
269 atomic_dec(&msg_hdrs);
270 free_msg(msg);
271 }
272 atomic_sub(msq->q_cbytes, &msg_bytes);
273 security_msg_queue_free(msq);
274 ipc_rcu_putref(msq);
275}
276
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700277static inline int msg_security(void *msq, int msgflg)
278{
279 return security_msg_queue_associate((struct msg_queue *) msq, msgflg);
280}
281
Ingo Molnar5a06a362006-07-30 03:04:11 -0700282asmlinkage long sys_msgget(key_t key, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Kirill Korotaev1e786932006-10-02 02:18:21 -0700284 struct ipc_namespace *ns;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700285 struct ipc_ops msg_ops;
286 struct ipc_params msg_params;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700287
288 ns = current->nsproxy->ipc_ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700289
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700290 msg_ops.getnew = newque;
291 msg_ops.associate = msg_security;
292 msg_ops.more_checks = NULL;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700293
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700294 msg_params.key = key;
295 msg_params.flg = msgflg;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700296
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700297 return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Ingo Molnar5a06a362006-07-30 03:04:11 -0700300static inline unsigned long
301copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 switch(version) {
304 case IPC_64:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700305 return copy_to_user(buf, in, sizeof(*in));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 case IPC_OLD:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700307 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 struct msqid_ds out;
309
Ingo Molnar5a06a362006-07-30 03:04:11 -0700310 memset(&out, 0, sizeof(out));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
313
314 out.msg_stime = in->msg_stime;
315 out.msg_rtime = in->msg_rtime;
316 out.msg_ctime = in->msg_ctime;
317
Ingo Molnar5a06a362006-07-30 03:04:11 -0700318 if (in->msg_cbytes > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 out.msg_cbytes = USHRT_MAX;
320 else
321 out.msg_cbytes = in->msg_cbytes;
322 out.msg_lcbytes = in->msg_cbytes;
323
Ingo Molnar5a06a362006-07-30 03:04:11 -0700324 if (in->msg_qnum > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 out.msg_qnum = USHRT_MAX;
326 else
327 out.msg_qnum = in->msg_qnum;
328
Ingo Molnar5a06a362006-07-30 03:04:11 -0700329 if (in->msg_qbytes > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 out.msg_qbytes = USHRT_MAX;
331 else
332 out.msg_qbytes = in->msg_qbytes;
333 out.msg_lqbytes = in->msg_qbytes;
334
335 out.msg_lspid = in->msg_lspid;
336 out.msg_lrpid = in->msg_lrpid;
337
Ingo Molnar5a06a362006-07-30 03:04:11 -0700338 return copy_to_user(buf, &out, sizeof(out));
339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 default:
341 return -EINVAL;
342 }
343}
344
345struct msq_setbuf {
346 unsigned long qbytes;
347 uid_t uid;
348 gid_t gid;
349 mode_t mode;
350};
351
Ingo Molnar5a06a362006-07-30 03:04:11 -0700352static inline unsigned long
353copy_msqid_from_user(struct msq_setbuf *out, void __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 switch(version) {
356 case IPC_64:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700357 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 struct msqid64_ds tbuf;
359
Ingo Molnar5a06a362006-07-30 03:04:11 -0700360 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return -EFAULT;
362
363 out->qbytes = tbuf.msg_qbytes;
364 out->uid = tbuf.msg_perm.uid;
365 out->gid = tbuf.msg_perm.gid;
366 out->mode = tbuf.msg_perm.mode;
367
368 return 0;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 case IPC_OLD:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700371 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 struct msqid_ds tbuf_old;
373
Ingo Molnar5a06a362006-07-30 03:04:11 -0700374 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return -EFAULT;
376
377 out->uid = tbuf_old.msg_perm.uid;
378 out->gid = tbuf_old.msg_perm.gid;
379 out->mode = tbuf_old.msg_perm.mode;
380
Ingo Molnar5a06a362006-07-30 03:04:11 -0700381 if (tbuf_old.msg_qbytes == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 out->qbytes = tbuf_old.msg_lqbytes;
383 else
384 out->qbytes = tbuf_old.msg_qbytes;
385
386 return 0;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 default:
389 return -EINVAL;
390 }
391}
392
Ingo Molnar5a06a362006-07-30 03:04:11 -0700393asmlinkage long sys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 struct kern_ipc_perm *ipcp;
Jeff Garzik8e1c0912007-07-17 05:40:59 -0400396 struct msq_setbuf uninitialized_var(setbuf);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700397 struct msg_queue *msq;
398 int err, version;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700399 struct ipc_namespace *ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (msqid < 0 || cmd < 0)
402 return -EINVAL;
403
404 version = ipc_parse_version(&cmd);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700405 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 switch (cmd) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700408 case IPC_INFO:
409 case MSG_INFO:
410 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 struct msginfo msginfo;
412 int max_id;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (!buf)
415 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700416 /*
417 * We must not return kernel stack data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 * due to padding, it's not enough
419 * to set all member fields.
420 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 err = security_msg_queue_msgctl(NULL, cmd);
422 if (err)
423 return err;
424
Ingo Molnar5a06a362006-07-30 03:04:11 -0700425 memset(&msginfo, 0, sizeof(msginfo));
Kirill Korotaev1e786932006-10-02 02:18:21 -0700426 msginfo.msgmni = ns->msg_ctlmni;
427 msginfo.msgmax = ns->msg_ctlmax;
428 msginfo.msgmnb = ns->msg_ctlmnb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 msginfo.msgssz = MSGSSZ;
430 msginfo.msgseg = MSGSEG;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700431 mutex_lock(&msg_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (cmd == MSG_INFO) {
Kirill Korotaev1e786932006-10-02 02:18:21 -0700433 msginfo.msgpool = msg_ids(ns).in_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 msginfo.msgmap = atomic_read(&msg_hdrs);
435 msginfo.msgtql = atomic_read(&msg_bytes);
436 } else {
437 msginfo.msgmap = MSGMAP;
438 msginfo.msgpool = MSGPOOL;
439 msginfo.msgtql = MSGTQL;
440 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700441 max_id = ipc_get_maxid(&msg_ids(ns));
Kirill Korotaev1e786932006-10-02 02:18:21 -0700442 mutex_unlock(&msg_ids(ns).mutex);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700443 if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700445 return (max_id < 0) ? 0 : max_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700447 case MSG_STAT: /* msqid is an index rather than a msg queue id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 case IPC_STAT:
449 {
450 struct msqid64_ds tbuf;
451 int success_return;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (!buf)
454 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Ingo Molnar5a06a362006-07-30 03:04:11 -0700456 if (cmd == MSG_STAT) {
Nadia Derbey023a5352007-10-18 23:40:51 -0700457 msq = msg_lock(ns, msqid);
458 if (IS_ERR(msq))
459 return PTR_ERR(msq);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700460 success_return = msq->q_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 } else {
Nadia Derbey023a5352007-10-18 23:40:51 -0700462 msq = msg_lock_check(ns, msqid);
463 if (IS_ERR(msq))
464 return PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 success_return = 0;
466 }
467 err = -EACCES;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700468 if (ipcperms(&msq->q_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 goto out_unlock;
470
471 err = security_msg_queue_msgctl(msq, cmd);
472 if (err)
473 goto out_unlock;
474
Nadia Derbey023a5352007-10-18 23:40:51 -0700475 memset(&tbuf, 0, sizeof(tbuf));
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
478 tbuf.msg_stime = msq->q_stime;
479 tbuf.msg_rtime = msq->q_rtime;
480 tbuf.msg_ctime = msq->q_ctime;
481 tbuf.msg_cbytes = msq->q_cbytes;
482 tbuf.msg_qnum = msq->q_qnum;
483 tbuf.msg_qbytes = msq->q_qbytes;
484 tbuf.msg_lspid = msq->q_lspid;
485 tbuf.msg_lrpid = msq->q_lrpid;
486 msg_unlock(msq);
487 if (copy_msqid_to_user(buf, &tbuf, version))
488 return -EFAULT;
489 return success_return;
490 }
491 case IPC_SET:
492 if (!buf)
493 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700494 if (copy_msqid_from_user(&setbuf, buf, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 break;
497 case IPC_RMID:
498 break;
499 default:
500 return -EINVAL;
501 }
502
Kirill Korotaev1e786932006-10-02 02:18:21 -0700503 mutex_lock(&msg_ids(ns).mutex);
Nadia Derbey023a5352007-10-18 23:40:51 -0700504 msq = msg_lock_check(ns, msqid);
505 if (IS_ERR(msq)) {
506 err = PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 goto out_up;
Nadia Derbey023a5352007-10-18 23:40:51 -0700508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 ipcp = &msq->q_perm;
Steve Grubb073115d2006-04-02 17:07:33 -0400511
512 err = audit_ipc_obj(ipcp);
513 if (err)
514 goto out_unlock_up;
Jeff Garzik8e1c0912007-07-17 05:40:59 -0400515 if (cmd == IPC_SET) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700516 err = audit_ipc_set_perm(setbuf.qbytes, setbuf.uid, setbuf.gid,
517 setbuf.mode);
Linda Knippersac032212006-05-16 22:03:48 -0400518 if (err)
519 goto out_unlock_up;
520 }
Steve Grubb073115d2006-04-02 17:07:33 -0400521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 err = -EPERM;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700523 if (current->euid != ipcp->cuid &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700525 /* We _could_ check for CAP_CHOWN above, but we don't */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 goto out_unlock_up;
527
528 err = security_msg_queue_msgctl(msq, cmd);
529 if (err)
530 goto out_unlock_up;
531
532 switch (cmd) {
533 case IPC_SET:
534 {
535 err = -EPERM;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700536 if (setbuf.qbytes > ns->msg_ctlmnb && !capable(CAP_SYS_RESOURCE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 goto out_unlock_up;
538
539 msq->q_qbytes = setbuf.qbytes;
540
541 ipcp->uid = setbuf.uid;
542 ipcp->gid = setbuf.gid;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700543 ipcp->mode = (ipcp->mode & ~S_IRWXUGO) |
544 (S_IRWXUGO & setbuf.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 msq->q_ctime = get_seconds();
546 /* sleeping receivers might be excluded by
547 * stricter permissions.
548 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700549 expunge_all(msq, -EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 /* sleeping senders might be able to send
551 * due to a larger queue size.
552 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700553 ss_wakeup(&msq->q_senders, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 msg_unlock(msq);
555 break;
556 }
557 case IPC_RMID:
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700558 freeque(ns, msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 break;
560 }
561 err = 0;
562out_up:
Kirill Korotaev1e786932006-10-02 02:18:21 -0700563 mutex_unlock(&msg_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return err;
565out_unlock_up:
566 msg_unlock(msq);
567 goto out_up;
568out_unlock:
569 msg_unlock(msq);
570 return err;
571}
572
Ingo Molnar5a06a362006-07-30 03:04:11 -0700573static int testmsg(struct msg_msg *msg, long type, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 switch(mode)
576 {
577 case SEARCH_ANY:
578 return 1;
579 case SEARCH_LESSEQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700580 if (msg->m_type <=type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return 1;
582 break;
583 case SEARCH_EQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700584 if (msg->m_type == type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return 1;
586 break;
587 case SEARCH_NOTEQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700588 if (msg->m_type != type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return 1;
590 break;
591 }
592 return 0;
593}
594
Ingo Molnar5a06a362006-07-30 03:04:11 -0700595static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700597 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 tmp = msq->q_receivers.next;
600 while (tmp != &msq->q_receivers) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700601 struct msg_receiver *msr;
602
603 msr = list_entry(tmp, struct msg_receiver, r_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 tmp = tmp->next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700605 if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
606 !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
607 msr->r_msgtype, msr->r_mode)) {
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 list_del(&msr->r_list);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700610 if (msr->r_maxsize < msg->m_ts) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 msr->r_msg = NULL;
612 wake_up_process(msr->r_tsk);
613 smp_mb();
614 msr->r_msg = ERR_PTR(-E2BIG);
615 } else {
616 msr->r_msg = NULL;
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700617 msq->q_lrpid = task_pid_vnr(msr->r_tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 msq->q_rtime = get_seconds();
619 wake_up_process(msr->r_tsk);
620 smp_mb();
621 msr->r_msg = msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return 1;
624 }
625 }
626 }
627 return 0;
628}
629
suzuki651971c2006-12-06 20:37:48 -0800630long do_msgsnd(int msqid, long mtype, void __user *mtext,
631 size_t msgsz, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 struct msg_queue *msq;
634 struct msg_msg *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 int err;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700636 struct ipc_namespace *ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700637
Kirill Korotaev1e786932006-10-02 02:18:21 -0700638 ns = current->nsproxy->ipc_ns;
639
640 if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (mtype < 1)
643 return -EINVAL;
644
suzuki651971c2006-12-06 20:37:48 -0800645 msg = load_msg(mtext, msgsz);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700646 if (IS_ERR(msg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return PTR_ERR(msg);
648
649 msg->m_type = mtype;
650 msg->m_ts = msgsz;
651
Nadia Derbey023a5352007-10-18 23:40:51 -0700652 msq = msg_lock_check(ns, msqid);
653 if (IS_ERR(msq)) {
654 err = PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 goto out_free;
Nadia Derbey023a5352007-10-18 23:40:51 -0700656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 for (;;) {
659 struct msg_sender s;
660
Ingo Molnar5a06a362006-07-30 03:04:11 -0700661 err = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 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 Molnar5a06a362006-07-30 03:04:11 -0700669 if (msgsz + msq->q_cbytes <= msq->q_qbytes &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 1 + msq->q_qnum <= msq->q_qbytes) {
671 break;
672 }
673
674 /* queue full, wait: */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700675 if (msgflg & IPC_NOWAIT) {
676 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 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 Molnar5a06a362006-07-30 03:04:11 -0700691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (signal_pending(current)) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700693 err = -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 goto out_unlock_free;
695 }
696 }
697
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700698 msq->q_lspid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 msq->q_stime = get_seconds();
700
Ingo Molnar5a06a362006-07-30 03:04:11 -0700701 if (!pipelined_send(msq, msg)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 /* noone is waiting for this message, enqueue it */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700703 list_add_tail(&msg->m_list, &msq->q_messages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 msq->q_cbytes += msgsz;
705 msq->q_qnum++;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700706 atomic_add(msgsz, &msg_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 atomic_inc(&msg_hdrs);
708 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 err = 0;
711 msg = NULL;
712
713out_unlock_free:
714 msg_unlock(msq);
715out_free:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700716 if (msg != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 free_msg(msg);
718 return err;
719}
720
suzuki651971c2006-12-06 20:37:48 -0800721asmlinkage long
722sys_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 Molnar5a06a362006-07-30 03:04:11 -0700731static inline int convert_mode(long *msgtyp, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700733 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 * find message of correct type.
735 * msgtyp = 0 => get first.
736 * msgtyp > 0 => get first message of matching type.
Ingo Molnar5a06a362006-07-30 03:04:11 -0700737 * msgtyp < 0 => get message with least type must be < abs(msgtype).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700739 if (*msgtyp == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return SEARCH_ANY;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700741 if (*msgtyp < 0) {
742 *msgtyp = -*msgtyp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return SEARCH_LESSEQUAL;
744 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700745 if (msgflg & MSG_EXCEPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return SEARCH_NOTEQUAL;
747 return SEARCH_EQUAL;
748}
749
suzuki651971c2006-12-06 20:37:48 -0800750long do_msgrcv(int msqid, long *pmtype, void __user *mtext,
751 size_t msgsz, long msgtyp, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 struct msg_queue *msq;
754 struct msg_msg *msg;
755 int mode;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700756 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 if (msqid < 0 || (long) msgsz < 0)
759 return -EINVAL;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700760 mode = convert_mode(&msgtyp, msgflg);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700761 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Nadia Derbey023a5352007-10-18 23:40:51 -0700763 msq = msg_lock_check(ns, msqid);
764 if (IS_ERR(msq))
765 return PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 for (;;) {
768 struct msg_receiver msr_d;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700769 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 msg = ERR_PTR(-EACCES);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700772 if (ipcperms(&msq->q_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 goto out_unlock;
774
775 msg = ERR_PTR(-EAGAIN);
776 tmp = msq->q_messages.next;
777 while (tmp != &msq->q_messages) {
778 struct msg_msg *walk_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700779
780 walk_msg = list_entry(tmp, struct msg_msg, m_list);
781 if (testmsg(walk_msg, msgtyp, mode) &&
782 !security_msg_queue_msgrcv(msq, walk_msg, current,
783 msgtyp, mode)) {
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 msg = walk_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700786 if (mode == SEARCH_LESSEQUAL &&
787 walk_msg->m_type != 1) {
788 msg = walk_msg;
789 msgtyp = walk_msg->m_type - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 } else {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700791 msg = walk_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 break;
793 }
794 }
795 tmp = tmp->next;
796 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700797 if (!IS_ERR(msg)) {
798 /*
799 * Found a suitable message.
800 * Unlink it from the queue.
801 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
803 msg = ERR_PTR(-E2BIG);
804 goto out_unlock;
805 }
806 list_del(&msg->m_list);
807 msq->q_qnum--;
808 msq->q_rtime = get_seconds();
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700809 msq->q_lrpid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 msq->q_cbytes -= msg->m_ts;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700811 atomic_sub(msg->m_ts, &msg_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 atomic_dec(&msg_hdrs);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700813 ss_wakeup(&msq->q_senders, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 msg_unlock(msq);
815 break;
816 }
817 /* No message waiting. Wait for a message */
818 if (msgflg & IPC_NOWAIT) {
819 msg = ERR_PTR(-ENOMSG);
820 goto out_unlock;
821 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700822 list_add_tail(&msr_d.r_list, &msq->q_receivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 msr_d.r_tsk = current;
824 msr_d.r_msgtype = msgtyp;
825 msr_d.r_mode = mode;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700826 if (msgflg & MSG_NOERROR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 msr_d.r_maxsize = INT_MAX;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700828 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 msr_d.r_maxsize = msgsz;
830 msr_d.r_msg = ERR_PTR(-EAGAIN);
831 current->state = TASK_INTERRUPTIBLE;
832 msg_unlock(msq);
833
834 schedule();
835
836 /* Lockless receive, part 1:
837 * Disable preemption. We don't hold a reference to the queue
838 * and getting a reference would defeat the idea of a lockless
839 * operation, thus the code relies on rcu to guarantee the
840 * existance of msq:
841 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
842 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
843 * rcu_read_lock() prevents preemption between reading r_msg
844 * and the spin_lock() inside ipc_lock_by_ptr().
845 */
846 rcu_read_lock();
847
848 /* Lockless receive, part 2:
849 * Wait until pipelined_send or expunge_all are outside of
850 * wake_up_process(). There is a race with exit(), see
851 * ipc/mqueue.c for the details.
852 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700853 msg = (struct msg_msg*)msr_d.r_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 while (msg == NULL) {
855 cpu_relax();
Ingo Molnar5a06a362006-07-30 03:04:11 -0700856 msg = (struct msg_msg *)msr_d.r_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
858
859 /* Lockless receive, part 3:
860 * If there is a message or an error then accept it without
861 * locking.
862 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700863 if (msg != ERR_PTR(-EAGAIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 rcu_read_unlock();
865 break;
866 }
867
868 /* Lockless receive, part 3:
869 * Acquire the queue spinlock.
870 */
871 ipc_lock_by_ptr(&msq->q_perm);
872 rcu_read_unlock();
873
874 /* Lockless receive, part 4:
875 * Repeat test after acquiring the spinlock.
876 */
877 msg = (struct msg_msg*)msr_d.r_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700878 if (msg != ERR_PTR(-EAGAIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 goto out_unlock;
880
881 list_del(&msr_d.r_list);
882 if (signal_pending(current)) {
883 msg = ERR_PTR(-ERESTARTNOHAND);
884out_unlock:
885 msg_unlock(msq);
886 break;
887 }
888 }
889 if (IS_ERR(msg))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700890 return PTR_ERR(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
suzuki651971c2006-12-06 20:37:48 -0800893 *pmtype = msg->m_type;
894 if (store_msg(mtext, msg, msgsz))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700895 msgsz = -EFAULT;
suzuki651971c2006-12-06 20:37:48 -0800896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 free_msg(msg);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return msgsz;
900}
901
suzuki651971c2006-12-06 20:37:48 -0800902asmlinkage long sys_msgrcv(int msqid, struct msgbuf __user *msgp, size_t msgsz,
903 long msgtyp, int msgflg)
904{
905 long err, mtype;
906
907 err = do_msgrcv(msqid, &mtype, msgp->mtext, msgsz, msgtyp, msgflg);
908 if (err < 0)
909 goto out;
910
911 if (put_user(mtype, &msgp->mtype))
912 err = -EFAULT;
913out:
914 return err;
915}
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -0700918static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Mike Waychison19b49462005-09-06 15:17:10 -0700920 struct msg_queue *msq = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Mike Waychison19b49462005-09-06 15:17:10 -0700922 return seq_printf(s,
Ingo Molnar5a06a362006-07-30 03:04:11 -0700923 "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
924 msq->q_perm.key,
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700925 msq->q_perm.id,
Ingo Molnar5a06a362006-07-30 03:04:11 -0700926 msq->q_perm.mode,
927 msq->q_cbytes,
928 msq->q_qnum,
929 msq->q_lspid,
930 msq->q_lrpid,
931 msq->q_perm.uid,
932 msq->q_perm.gid,
933 msq->q_perm.cuid,
934 msq->q_perm.cgid,
935 msq->q_stime,
936 msq->q_rtime,
937 msq->q_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939#endif