blob: a388824740e7e9c1e770dde9d3082609c0e8f017 [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_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 Torvalds1da177e2005-04-16 15:20:36 -070083
Kirill Korotaev1e786932006-10-02 02:18:21 -070084static void freeque (struct ipc_namespace *ns, struct msg_queue *msq, int id);
85static int newque (struct ipc_namespace *ns, key_t key, int msgflg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -070087static int sysvipc_msg_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#endif
89
Kirill Korotaev1e786932006-10-02 02:18:21 -070090static void __ipc_init __msg_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Kirill Korotaev1e786932006-10-02 02:18:21 -070092 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 Torvalds1da177e2005-04-16 15:20:36 -070097}
98
Kirill Korotaev1e786932006-10-02 02:18:21 -070099#ifdef CONFIG_IPC_NS
100int msg_init_ns(struct ipc_namespace *ns)
101{
102 struct ipc_ids *ids;
103
104 ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
105 if (ids == NULL)
106 return -ENOMEM;
107
108 __msg_init_ns(ns, ids);
109 return 0;
110}
111
112void msg_exit_ns(struct ipc_namespace *ns)
113{
114 int i;
115 struct msg_queue *msq;
116
117 mutex_lock(&msg_ids(ns).mutex);
118 for (i = 0; i <= msg_ids(ns).max_id; i++) {
119 msq = msg_lock(ns, i);
120 if (msq == NULL)
121 continue;
122
123 freeque(ns, msq, i);
124 }
125 mutex_unlock(&msg_ids(ns).mutex);
126
Pavel Emelianovc7e12b82006-11-02 22:07:03 -0800127 ipc_fini_ids(ns->ids[IPC_MSG_IDS]);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700128 kfree(ns->ids[IPC_MSG_IDS]);
129 ns->ids[IPC_MSG_IDS] = NULL;
130}
131#endif
132
133void __init msg_init(void)
134{
135 __msg_init_ns(&init_ipc_ns, &init_msg_ids);
136 ipc_init_proc_interface("sysvipc/msg",
137 " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
138 IPC_MSG_IDS, sysvipc_msg_proc_show);
139}
140
141static int newque (struct ipc_namespace *ns, key_t key, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 struct msg_queue *msq;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700144 int id, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Ingo Molnar5a06a362006-07-30 03:04:11 -0700146 msq = ipc_rcu_alloc(sizeof(*msq));
147 if (!msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return -ENOMEM;
149
Ingo Molnar5a06a362006-07-30 03:04:11 -0700150 msq->q_perm.mode = msgflg & S_IRWXUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 msq->q_perm.key = key;
152
153 msq->q_perm.security = NULL;
154 retval = security_msg_queue_alloc(msq);
155 if (retval) {
156 ipc_rcu_putref(msq);
157 return retval;
158 }
159
Kirill Korotaev1e786932006-10-02 02:18:21 -0700160 id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700161 if (id == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 security_msg_queue_free(msq);
163 ipc_rcu_putref(msq);
164 return -ENOSPC;
165 }
166
Kirill Korotaev1e786932006-10-02 02:18:21 -0700167 msq->q_id = msg_buildid(ns, id, msq->q_perm.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 msq->q_stime = msq->q_rtime = 0;
169 msq->q_ctime = get_seconds();
170 msq->q_cbytes = msq->q_qnum = 0;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700171 msq->q_qbytes = ns->msg_ctlmnb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 msq->q_lspid = msq->q_lrpid = 0;
173 INIT_LIST_HEAD(&msq->q_messages);
174 INIT_LIST_HEAD(&msq->q_receivers);
175 INIT_LIST_HEAD(&msq->q_senders);
176 msg_unlock(msq);
177
Mike Waychison19b49462005-09-06 15:17:10 -0700178 return msq->q_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Ingo Molnar5a06a362006-07-30 03:04:11 -0700181static inline void ss_add(struct msg_queue *msq, struct msg_sender *mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700183 mss->tsk = current;
184 current->state = TASK_INTERRUPTIBLE;
185 list_add_tail(&mss->list, &msq->q_senders);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
Ingo Molnar5a06a362006-07-30 03:04:11 -0700188static inline void ss_del(struct msg_sender *mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700190 if (mss->list.next != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 list_del(&mss->list);
192}
193
Ingo Molnar5a06a362006-07-30 03:04:11 -0700194static void ss_wakeup(struct list_head *h, int kill)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 struct list_head *tmp;
197
198 tmp = h->next;
199 while (tmp != h) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700200 struct msg_sender *mss;
201
202 mss = list_entry(tmp, struct msg_sender, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 tmp = tmp->next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700204 if (kill)
205 mss->list.next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 wake_up_process(mss->tsk);
207 }
208}
209
Ingo Molnar5a06a362006-07-30 03:04:11 -0700210static void expunge_all(struct msg_queue *msq, int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 struct list_head *tmp;
213
214 tmp = msq->q_receivers.next;
215 while (tmp != &msq->q_receivers) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700216 struct msg_receiver *msr;
217
218 msr = list_entry(tmp, struct msg_receiver, r_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 tmp = tmp->next;
220 msr->r_msg = NULL;
221 wake_up_process(msr->r_tsk);
222 smp_mb();
223 msr->r_msg = ERR_PTR(res);
224 }
225}
Ingo Molnar5a06a362006-07-30 03:04:11 -0700226
227/*
228 * freeque() wakes up waiters on the sender and receiver waiting queue,
229 * removes the message queue from message queue ID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 * array, and cleans up all the messages associated with this queue.
231 *
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800232 * msg_ids.mutex and the spinlock for this message queue is hold
233 * before freeque() is called. msg_ids.mutex remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 */
Kirill Korotaev1e786932006-10-02 02:18:21 -0700235static void freeque(struct ipc_namespace *ns, struct msg_queue *msq, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 struct list_head *tmp;
238
Ingo Molnar5a06a362006-07-30 03:04:11 -0700239 expunge_all(msq, -EIDRM);
240 ss_wakeup(&msq->q_senders, 1);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700241 msq = msg_rmid(ns, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 msg_unlock(msq);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 tmp = msq->q_messages.next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700245 while (tmp != &msq->q_messages) {
246 struct msg_msg *msg = list_entry(tmp, struct msg_msg, m_list);
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 tmp = tmp->next;
249 atomic_dec(&msg_hdrs);
250 free_msg(msg);
251 }
252 atomic_sub(msq->q_cbytes, &msg_bytes);
253 security_msg_queue_free(msq);
254 ipc_rcu_putref(msq);
255}
256
Ingo Molnar5a06a362006-07-30 03:04:11 -0700257asmlinkage long sys_msgget(key_t key, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 struct msg_queue *msq;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700260 int id, ret = -EPERM;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700261 struct ipc_namespace *ns;
262
263 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Kirill Korotaev1e786932006-10-02 02:18:21 -0700265 mutex_lock(&msg_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (key == IPC_PRIVATE)
Kirill Korotaev1e786932006-10-02 02:18:21 -0700267 ret = newque(ns, key, msgflg);
268 else if ((id = ipc_findkey(&msg_ids(ns), key)) == -1) { /* key not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (!(msgflg & IPC_CREAT))
270 ret = -ENOENT;
271 else
Kirill Korotaev1e786932006-10-02 02:18:21 -0700272 ret = newque(ns, key, msgflg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 } else if (msgflg & IPC_CREAT && msgflg & IPC_EXCL) {
274 ret = -EEXIST;
275 } else {
Kirill Korotaev1e786932006-10-02 02:18:21 -0700276 msq = msg_lock(ns, id);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700277 BUG_ON(msq == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (ipcperms(&msq->q_perm, msgflg))
279 ret = -EACCES;
280 else {
Kirill Korotaev1e786932006-10-02 02:18:21 -0700281 int qid = msg_buildid(ns, id, msq->q_perm.seq);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700282
283 ret = security_msg_queue_associate(msq, msgflg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (!ret)
285 ret = qid;
286 }
287 msg_unlock(msq);
288 }
Kirill Korotaev1e786932006-10-02 02:18:21 -0700289 mutex_unlock(&msg_ids(ns).mutex);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return ret;
292}
293
Ingo Molnar5a06a362006-07-30 03:04:11 -0700294static inline unsigned long
295copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 switch(version) {
298 case IPC_64:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700299 return copy_to_user(buf, in, sizeof(*in));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 case IPC_OLD:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700301 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 struct msqid_ds out;
303
Ingo Molnar5a06a362006-07-30 03:04:11 -0700304 memset(&out, 0, sizeof(out));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
307
308 out.msg_stime = in->msg_stime;
309 out.msg_rtime = in->msg_rtime;
310 out.msg_ctime = in->msg_ctime;
311
Ingo Molnar5a06a362006-07-30 03:04:11 -0700312 if (in->msg_cbytes > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 out.msg_cbytes = USHRT_MAX;
314 else
315 out.msg_cbytes = in->msg_cbytes;
316 out.msg_lcbytes = in->msg_cbytes;
317
Ingo Molnar5a06a362006-07-30 03:04:11 -0700318 if (in->msg_qnum > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 out.msg_qnum = USHRT_MAX;
320 else
321 out.msg_qnum = in->msg_qnum;
322
Ingo Molnar5a06a362006-07-30 03:04:11 -0700323 if (in->msg_qbytes > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 out.msg_qbytes = USHRT_MAX;
325 else
326 out.msg_qbytes = in->msg_qbytes;
327 out.msg_lqbytes = in->msg_qbytes;
328
329 out.msg_lspid = in->msg_lspid;
330 out.msg_lrpid = in->msg_lrpid;
331
Ingo Molnar5a06a362006-07-30 03:04:11 -0700332 return copy_to_user(buf, &out, sizeof(out));
333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 default:
335 return -EINVAL;
336 }
337}
338
339struct msq_setbuf {
340 unsigned long qbytes;
341 uid_t uid;
342 gid_t gid;
343 mode_t mode;
344};
345
Ingo Molnar5a06a362006-07-30 03:04:11 -0700346static inline unsigned long
347copy_msqid_from_user(struct msq_setbuf *out, void __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 switch(version) {
350 case IPC_64:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700351 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 struct msqid64_ds tbuf;
353
Ingo Molnar5a06a362006-07-30 03:04:11 -0700354 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return -EFAULT;
356
357 out->qbytes = tbuf.msg_qbytes;
358 out->uid = tbuf.msg_perm.uid;
359 out->gid = tbuf.msg_perm.gid;
360 out->mode = tbuf.msg_perm.mode;
361
362 return 0;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 case IPC_OLD:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700365 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 struct msqid_ds tbuf_old;
367
Ingo Molnar5a06a362006-07-30 03:04:11 -0700368 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return -EFAULT;
370
371 out->uid = tbuf_old.msg_perm.uid;
372 out->gid = tbuf_old.msg_perm.gid;
373 out->mode = tbuf_old.msg_perm.mode;
374
Ingo Molnar5a06a362006-07-30 03:04:11 -0700375 if (tbuf_old.msg_qbytes == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 out->qbytes = tbuf_old.msg_lqbytes;
377 else
378 out->qbytes = tbuf_old.msg_qbytes;
379
380 return 0;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 default:
383 return -EINVAL;
384 }
385}
386
Ingo Molnar5a06a362006-07-30 03:04:11 -0700387asmlinkage long sys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 struct kern_ipc_perm *ipcp;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700390 struct msq_setbuf setbuf;
391 struct msg_queue *msq;
392 int err, version;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700393 struct ipc_namespace *ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (msqid < 0 || cmd < 0)
396 return -EINVAL;
397
398 version = ipc_parse_version(&cmd);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700399 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 switch (cmd) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700402 case IPC_INFO:
403 case MSG_INFO:
404 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 struct msginfo msginfo;
406 int max_id;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (!buf)
409 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700410 /*
411 * We must not return kernel stack data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 * due to padding, it's not enough
413 * to set all member fields.
414 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 err = security_msg_queue_msgctl(NULL, cmd);
416 if (err)
417 return err;
418
Ingo Molnar5a06a362006-07-30 03:04:11 -0700419 memset(&msginfo, 0, sizeof(msginfo));
Kirill Korotaev1e786932006-10-02 02:18:21 -0700420 msginfo.msgmni = ns->msg_ctlmni;
421 msginfo.msgmax = ns->msg_ctlmax;
422 msginfo.msgmnb = ns->msg_ctlmnb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 msginfo.msgssz = MSGSSZ;
424 msginfo.msgseg = MSGSEG;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700425 mutex_lock(&msg_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (cmd == MSG_INFO) {
Kirill Korotaev1e786932006-10-02 02:18:21 -0700427 msginfo.msgpool = msg_ids(ns).in_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 msginfo.msgmap = atomic_read(&msg_hdrs);
429 msginfo.msgtql = atomic_read(&msg_bytes);
430 } else {
431 msginfo.msgmap = MSGMAP;
432 msginfo.msgpool = MSGPOOL;
433 msginfo.msgtql = MSGTQL;
434 }
Kirill Korotaev1e786932006-10-02 02:18:21 -0700435 max_id = msg_ids(ns).max_id;
436 mutex_unlock(&msg_ids(ns).mutex);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700437 if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700439 return (max_id < 0) ? 0 : max_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441 case MSG_STAT:
442 case IPC_STAT:
443 {
444 struct msqid64_ds tbuf;
445 int success_return;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (!buf)
448 return -EFAULT;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700449 if (cmd == MSG_STAT && msqid >= msg_ids(ns).entries->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return -EINVAL;
451
Ingo Molnar5a06a362006-07-30 03:04:11 -0700452 memset(&tbuf, 0, sizeof(tbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Kirill Korotaev1e786932006-10-02 02:18:21 -0700454 msq = msg_lock(ns, msqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (msq == NULL)
456 return -EINVAL;
457
Ingo Molnar5a06a362006-07-30 03:04:11 -0700458 if (cmd == MSG_STAT) {
Kirill Korotaev1e786932006-10-02 02:18:21 -0700459 success_return = msg_buildid(ns, msqid, msq->q_perm.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 } else {
461 err = -EIDRM;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700462 if (msg_checkid(ns, msq, msqid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 goto out_unlock;
464 success_return = 0;
465 }
466 err = -EACCES;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700467 if (ipcperms(&msq->q_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 goto out_unlock;
469
470 err = security_msg_queue_msgctl(msq, cmd);
471 if (err)
472 goto out_unlock;
473
474 kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
475 tbuf.msg_stime = msq->q_stime;
476 tbuf.msg_rtime = msq->q_rtime;
477 tbuf.msg_ctime = msq->q_ctime;
478 tbuf.msg_cbytes = msq->q_cbytes;
479 tbuf.msg_qnum = msq->q_qnum;
480 tbuf.msg_qbytes = msq->q_qbytes;
481 tbuf.msg_lspid = msq->q_lspid;
482 tbuf.msg_lrpid = msq->q_lrpid;
483 msg_unlock(msq);
484 if (copy_msqid_to_user(buf, &tbuf, version))
485 return -EFAULT;
486 return success_return;
487 }
488 case IPC_SET:
489 if (!buf)
490 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700491 if (copy_msqid_from_user(&setbuf, buf, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 break;
494 case IPC_RMID:
495 break;
496 default:
497 return -EINVAL;
498 }
499
Kirill Korotaev1e786932006-10-02 02:18:21 -0700500 mutex_lock(&msg_ids(ns).mutex);
501 msq = msg_lock(ns, msqid);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700502 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (msq == NULL)
504 goto out_up;
505
506 err = -EIDRM;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700507 if (msg_checkid(ns, msq, msqid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 goto out_unlock_up;
509 ipcp = &msq->q_perm;
Steve Grubb073115d2006-04-02 17:07:33 -0400510
511 err = audit_ipc_obj(ipcp);
512 if (err)
513 goto out_unlock_up;
Linda Knippersac032212006-05-16 22:03:48 -0400514 if (cmd==IPC_SET) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700515 err = audit_ipc_set_perm(setbuf.qbytes, setbuf.uid, setbuf.gid,
516 setbuf.mode);
Linda Knippersac032212006-05-16 22:03:48 -0400517 if (err)
518 goto out_unlock_up;
519 }
Steve Grubb073115d2006-04-02 17:07:33 -0400520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 err = -EPERM;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700522 if (current->euid != ipcp->cuid &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700524 /* We _could_ check for CAP_CHOWN above, but we don't */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 goto out_unlock_up;
526
527 err = security_msg_queue_msgctl(msq, cmd);
528 if (err)
529 goto out_unlock_up;
530
531 switch (cmd) {
532 case IPC_SET:
533 {
534 err = -EPERM;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700535 if (setbuf.qbytes > ns->msg_ctlmnb && !capable(CAP_SYS_RESOURCE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 goto out_unlock_up;
537
538 msq->q_qbytes = setbuf.qbytes;
539
540 ipcp->uid = setbuf.uid;
541 ipcp->gid = setbuf.gid;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700542 ipcp->mode = (ipcp->mode & ~S_IRWXUGO) |
543 (S_IRWXUGO & setbuf.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 msq->q_ctime = get_seconds();
545 /* sleeping receivers might be excluded by
546 * stricter permissions.
547 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700548 expunge_all(msq, -EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 /* sleeping senders might be able to send
550 * due to a larger queue size.
551 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700552 ss_wakeup(&msq->q_senders, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 msg_unlock(msq);
554 break;
555 }
556 case IPC_RMID:
Kirill Korotaev1e786932006-10-02 02:18:21 -0700557 freeque(ns, msq, msqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 break;
559 }
560 err = 0;
561out_up:
Kirill Korotaev1e786932006-10-02 02:18:21 -0700562 mutex_unlock(&msg_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return err;
564out_unlock_up:
565 msg_unlock(msq);
566 goto out_up;
567out_unlock:
568 msg_unlock(msq);
569 return err;
570}
571
Ingo Molnar5a06a362006-07-30 03:04:11 -0700572static int testmsg(struct msg_msg *msg, long type, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 switch(mode)
575 {
576 case SEARCH_ANY:
577 return 1;
578 case SEARCH_LESSEQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700579 if (msg->m_type <=type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return 1;
581 break;
582 case SEARCH_EQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700583 if (msg->m_type == type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return 1;
585 break;
586 case SEARCH_NOTEQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700587 if (msg->m_type != type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return 1;
589 break;
590 }
591 return 0;
592}
593
Ingo Molnar5a06a362006-07-30 03:04:11 -0700594static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700596 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 tmp = msq->q_receivers.next;
599 while (tmp != &msq->q_receivers) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700600 struct msg_receiver *msr;
601
602 msr = list_entry(tmp, struct msg_receiver, r_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 tmp = tmp->next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700604 if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
605 !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
606 msr->r_msgtype, msr->r_mode)) {
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 list_del(&msr->r_list);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700609 if (msr->r_maxsize < msg->m_ts) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 msr->r_msg = NULL;
611 wake_up_process(msr->r_tsk);
612 smp_mb();
613 msr->r_msg = ERR_PTR(-E2BIG);
614 } else {
615 msr->r_msg = NULL;
616 msq->q_lrpid = msr->r_tsk->pid;
617 msq->q_rtime = get_seconds();
618 wake_up_process(msr->r_tsk);
619 smp_mb();
620 msr->r_msg = msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return 1;
623 }
624 }
625 }
626 return 0;
627}
628
suzuki651971c2006-12-06 20:37:48 -0800629long do_msgsnd(int msqid, long mtype, void __user *mtext,
630 size_t msgsz, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
632 struct msg_queue *msq;
633 struct msg_msg *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 int err;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700635 struct ipc_namespace *ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700636
Kirill Korotaev1e786932006-10-02 02:18:21 -0700637 ns = current->nsproxy->ipc_ns;
638
639 if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (mtype < 1)
642 return -EINVAL;
643
suzuki651971c2006-12-06 20:37:48 -0800644 msg = load_msg(mtext, msgsz);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700645 if (IS_ERR(msg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return PTR_ERR(msg);
647
648 msg->m_type = mtype;
649 msg->m_ts = msgsz;
650
Kirill Korotaev1e786932006-10-02 02:18:21 -0700651 msq = msg_lock(ns, msqid);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700652 err = -EINVAL;
653 if (msq == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 goto out_free;
655
656 err= -EIDRM;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700657 if (msg_checkid(ns, msq, msqid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 goto out_unlock_free;
659
660 for (;;) {
661 struct msg_sender s;
662
Ingo Molnar5a06a362006-07-30 03:04:11 -0700663 err = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (ipcperms(&msq->q_perm, S_IWUGO))
665 goto out_unlock_free;
666
667 err = security_msg_queue_msgsnd(msq, msg, msgflg);
668 if (err)
669 goto out_unlock_free;
670
Ingo Molnar5a06a362006-07-30 03:04:11 -0700671 if (msgsz + msq->q_cbytes <= msq->q_qbytes &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 1 + msq->q_qnum <= msq->q_qbytes) {
673 break;
674 }
675
676 /* queue full, wait: */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700677 if (msgflg & IPC_NOWAIT) {
678 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 goto out_unlock_free;
680 }
681 ss_add(msq, &s);
682 ipc_rcu_getref(msq);
683 msg_unlock(msq);
684 schedule();
685
686 ipc_lock_by_ptr(&msq->q_perm);
687 ipc_rcu_putref(msq);
688 if (msq->q_perm.deleted) {
689 err = -EIDRM;
690 goto out_unlock_free;
691 }
692 ss_del(&s);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (signal_pending(current)) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700695 err = -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 goto out_unlock_free;
697 }
698 }
699
700 msq->q_lspid = current->tgid;
701 msq->q_stime = get_seconds();
702
Ingo Molnar5a06a362006-07-30 03:04:11 -0700703 if (!pipelined_send(msq, msg)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 /* noone is waiting for this message, enqueue it */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700705 list_add_tail(&msg->m_list, &msq->q_messages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 msq->q_cbytes += msgsz;
707 msq->q_qnum++;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700708 atomic_add(msgsz, &msg_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 atomic_inc(&msg_hdrs);
710 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 err = 0;
713 msg = NULL;
714
715out_unlock_free:
716 msg_unlock(msq);
717out_free:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700718 if (msg != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 free_msg(msg);
720 return err;
721}
722
suzuki651971c2006-12-06 20:37:48 -0800723asmlinkage long
724sys_msgsnd(int msqid, struct msgbuf __user *msgp, size_t msgsz, int msgflg)
725{
726 long mtype;
727
728 if (get_user(mtype, &msgp->mtype))
729 return -EFAULT;
730 return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
731}
732
Ingo Molnar5a06a362006-07-30 03:04:11 -0700733static inline int convert_mode(long *msgtyp, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700735 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 * find message of correct type.
737 * msgtyp = 0 => get first.
738 * msgtyp > 0 => get first message of matching type.
Ingo Molnar5a06a362006-07-30 03:04:11 -0700739 * msgtyp < 0 => get message with least type must be < abs(msgtype).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700741 if (*msgtyp == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return SEARCH_ANY;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700743 if (*msgtyp < 0) {
744 *msgtyp = -*msgtyp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return SEARCH_LESSEQUAL;
746 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700747 if (msgflg & MSG_EXCEPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return SEARCH_NOTEQUAL;
749 return SEARCH_EQUAL;
750}
751
suzuki651971c2006-12-06 20:37:48 -0800752long do_msgrcv(int msqid, long *pmtype, void __user *mtext,
753 size_t msgsz, long msgtyp, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
755 struct msg_queue *msq;
756 struct msg_msg *msg;
757 int mode;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700758 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 if (msqid < 0 || (long) msgsz < 0)
761 return -EINVAL;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700762 mode = convert_mode(&msgtyp, msgflg);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700763 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Kirill Korotaev1e786932006-10-02 02:18:21 -0700765 msq = msg_lock(ns, msqid);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700766 if (msq == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return -EINVAL;
768
769 msg = ERR_PTR(-EIDRM);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700770 if (msg_checkid(ns, msq, msqid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 goto out_unlock;
772
773 for (;;) {
774 struct msg_receiver msr_d;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700775 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 msg = ERR_PTR(-EACCES);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700778 if (ipcperms(&msq->q_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 goto out_unlock;
780
781 msg = ERR_PTR(-EAGAIN);
782 tmp = msq->q_messages.next;
783 while (tmp != &msq->q_messages) {
784 struct msg_msg *walk_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700785
786 walk_msg = list_entry(tmp, struct msg_msg, m_list);
787 if (testmsg(walk_msg, msgtyp, mode) &&
788 !security_msg_queue_msgrcv(msq, walk_msg, current,
789 msgtyp, mode)) {
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 msg = walk_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700792 if (mode == SEARCH_LESSEQUAL &&
793 walk_msg->m_type != 1) {
794 msg = walk_msg;
795 msgtyp = walk_msg->m_type - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 } else {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700797 msg = walk_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 break;
799 }
800 }
801 tmp = tmp->next;
802 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700803 if (!IS_ERR(msg)) {
804 /*
805 * Found a suitable message.
806 * Unlink it from the queue.
807 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
809 msg = ERR_PTR(-E2BIG);
810 goto out_unlock;
811 }
812 list_del(&msg->m_list);
813 msq->q_qnum--;
814 msq->q_rtime = get_seconds();
815 msq->q_lrpid = current->tgid;
816 msq->q_cbytes -= msg->m_ts;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700817 atomic_sub(msg->m_ts, &msg_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 atomic_dec(&msg_hdrs);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700819 ss_wakeup(&msq->q_senders, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 msg_unlock(msq);
821 break;
822 }
823 /* No message waiting. Wait for a message */
824 if (msgflg & IPC_NOWAIT) {
825 msg = ERR_PTR(-ENOMSG);
826 goto out_unlock;
827 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700828 list_add_tail(&msr_d.r_list, &msq->q_receivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 msr_d.r_tsk = current;
830 msr_d.r_msgtype = msgtyp;
831 msr_d.r_mode = mode;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700832 if (msgflg & MSG_NOERROR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 msr_d.r_maxsize = INT_MAX;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700834 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 msr_d.r_maxsize = msgsz;
836 msr_d.r_msg = ERR_PTR(-EAGAIN);
837 current->state = TASK_INTERRUPTIBLE;
838 msg_unlock(msq);
839
840 schedule();
841
842 /* Lockless receive, part 1:
843 * Disable preemption. We don't hold a reference to the queue
844 * and getting a reference would defeat the idea of a lockless
845 * operation, thus the code relies on rcu to guarantee the
846 * existance of msq:
847 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
848 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
849 * rcu_read_lock() prevents preemption between reading r_msg
850 * and the spin_lock() inside ipc_lock_by_ptr().
851 */
852 rcu_read_lock();
853
854 /* Lockless receive, part 2:
855 * Wait until pipelined_send or expunge_all are outside of
856 * wake_up_process(). There is a race with exit(), see
857 * ipc/mqueue.c for the details.
858 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700859 msg = (struct msg_msg*)msr_d.r_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 while (msg == NULL) {
861 cpu_relax();
Ingo Molnar5a06a362006-07-30 03:04:11 -0700862 msg = (struct msg_msg *)msr_d.r_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864
865 /* Lockless receive, part 3:
866 * If there is a message or an error then accept it without
867 * locking.
868 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700869 if (msg != ERR_PTR(-EAGAIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 rcu_read_unlock();
871 break;
872 }
873
874 /* Lockless receive, part 3:
875 * Acquire the queue spinlock.
876 */
877 ipc_lock_by_ptr(&msq->q_perm);
878 rcu_read_unlock();
879
880 /* Lockless receive, part 4:
881 * Repeat test after acquiring the spinlock.
882 */
883 msg = (struct msg_msg*)msr_d.r_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700884 if (msg != ERR_PTR(-EAGAIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 goto out_unlock;
886
887 list_del(&msr_d.r_list);
888 if (signal_pending(current)) {
889 msg = ERR_PTR(-ERESTARTNOHAND);
890out_unlock:
891 msg_unlock(msq);
892 break;
893 }
894 }
895 if (IS_ERR(msg))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700896 return PTR_ERR(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
suzuki651971c2006-12-06 20:37:48 -0800899 *pmtype = msg->m_type;
900 if (store_msg(mtext, msg, msgsz))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700901 msgsz = -EFAULT;
suzuki651971c2006-12-06 20:37:48 -0800902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 free_msg(msg);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return msgsz;
906}
907
suzuki651971c2006-12-06 20:37:48 -0800908asmlinkage long sys_msgrcv(int msqid, struct msgbuf __user *msgp, size_t msgsz,
909 long msgtyp, int msgflg)
910{
911 long err, mtype;
912
913 err = do_msgrcv(msqid, &mtype, msgp->mtext, msgsz, msgtyp, msgflg);
914 if (err < 0)
915 goto out;
916
917 if (put_user(mtype, &msgp->mtype))
918 err = -EFAULT;
919out:
920 return err;
921}
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -0700924static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Mike Waychison19b49462005-09-06 15:17:10 -0700926 struct msg_queue *msq = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Mike Waychison19b49462005-09-06 15:17:10 -0700928 return seq_printf(s,
Ingo Molnar5a06a362006-07-30 03:04:11 -0700929 "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
930 msq->q_perm.key,
931 msq->q_id,
932 msq->q_perm.mode,
933 msq->q_cbytes,
934 msq->q_qnum,
935 msq->q_lspid,
936 msq->q_lrpid,
937 msq->q_perm.uid,
938 msq->q_perm.gid,
939 msq->q_perm.cuid,
940 msq->q_perm.cgid,
941 msq->q_stime,
942 msq->q_rtime,
943 msq->q_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945#endif