blob: 5b213d952545e82684dc1d3f212f311ad3348790 [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
Ingo Molnar5a06a362006-07-30 03:04:11 -070055 volatile struct msg_msg *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
127 kfree(ns->ids[IPC_MSG_IDS]);
128 ns->ids[IPC_MSG_IDS] = NULL;
129}
130#endif
131
132void __init msg_init(void)
133{
134 __msg_init_ns(&init_ipc_ns, &init_msg_ids);
135 ipc_init_proc_interface("sysvipc/msg",
136 " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
137 IPC_MSG_IDS, sysvipc_msg_proc_show);
138}
139
140static int newque (struct ipc_namespace *ns, key_t key, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 struct msg_queue *msq;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700143 int id, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Ingo Molnar5a06a362006-07-30 03:04:11 -0700145 msq = ipc_rcu_alloc(sizeof(*msq));
146 if (!msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return -ENOMEM;
148
Ingo Molnar5a06a362006-07-30 03:04:11 -0700149 msq->q_perm.mode = msgflg & S_IRWXUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 msq->q_perm.key = key;
151
152 msq->q_perm.security = NULL;
153 retval = security_msg_queue_alloc(msq);
154 if (retval) {
155 ipc_rcu_putref(msq);
156 return retval;
157 }
158
Kirill Korotaev1e786932006-10-02 02:18:21 -0700159 id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700160 if (id == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 security_msg_queue_free(msq);
162 ipc_rcu_putref(msq);
163 return -ENOSPC;
164 }
165
Kirill Korotaev1e786932006-10-02 02:18:21 -0700166 msq->q_id = msg_buildid(ns, id, msq->q_perm.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 msq->q_stime = msq->q_rtime = 0;
168 msq->q_ctime = get_seconds();
169 msq->q_cbytes = msq->q_qnum = 0;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700170 msq->q_qbytes = ns->msg_ctlmnb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 msq->q_lspid = msq->q_lrpid = 0;
172 INIT_LIST_HEAD(&msq->q_messages);
173 INIT_LIST_HEAD(&msq->q_receivers);
174 INIT_LIST_HEAD(&msq->q_senders);
175 msg_unlock(msq);
176
Mike Waychison19b49462005-09-06 15:17:10 -0700177 return msq->q_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Ingo Molnar5a06a362006-07-30 03:04:11 -0700180static inline void ss_add(struct msg_queue *msq, struct msg_sender *mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700182 mss->tsk = current;
183 current->state = TASK_INTERRUPTIBLE;
184 list_add_tail(&mss->list, &msq->q_senders);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Ingo Molnar5a06a362006-07-30 03:04:11 -0700187static inline void ss_del(struct msg_sender *mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700189 if (mss->list.next != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 list_del(&mss->list);
191}
192
Ingo Molnar5a06a362006-07-30 03:04:11 -0700193static void ss_wakeup(struct list_head *h, int kill)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 struct list_head *tmp;
196
197 tmp = h->next;
198 while (tmp != h) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700199 struct msg_sender *mss;
200
201 mss = list_entry(tmp, struct msg_sender, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 tmp = tmp->next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700203 if (kill)
204 mss->list.next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 wake_up_process(mss->tsk);
206 }
207}
208
Ingo Molnar5a06a362006-07-30 03:04:11 -0700209static void expunge_all(struct msg_queue *msq, int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 struct list_head *tmp;
212
213 tmp = msq->q_receivers.next;
214 while (tmp != &msq->q_receivers) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700215 struct msg_receiver *msr;
216
217 msr = list_entry(tmp, struct msg_receiver, r_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 tmp = tmp->next;
219 msr->r_msg = NULL;
220 wake_up_process(msr->r_tsk);
221 smp_mb();
222 msr->r_msg = ERR_PTR(res);
223 }
224}
Ingo Molnar5a06a362006-07-30 03:04:11 -0700225
226/*
227 * freeque() wakes up waiters on the sender and receiver waiting queue,
228 * removes the message queue from message queue ID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 * array, and cleans up all the messages associated with this queue.
230 *
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800231 * msg_ids.mutex and the spinlock for this message queue is hold
232 * before freeque() is called. msg_ids.mutex remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 */
Kirill Korotaev1e786932006-10-02 02:18:21 -0700234static void freeque(struct ipc_namespace *ns, struct msg_queue *msq, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 struct list_head *tmp;
237
Ingo Molnar5a06a362006-07-30 03:04:11 -0700238 expunge_all(msq, -EIDRM);
239 ss_wakeup(&msq->q_senders, 1);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700240 msq = msg_rmid(ns, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 msg_unlock(msq);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 tmp = msq->q_messages.next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700244 while (tmp != &msq->q_messages) {
245 struct msg_msg *msg = list_entry(tmp, struct msg_msg, m_list);
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 tmp = tmp->next;
248 atomic_dec(&msg_hdrs);
249 free_msg(msg);
250 }
251 atomic_sub(msq->q_cbytes, &msg_bytes);
252 security_msg_queue_free(msq);
253 ipc_rcu_putref(msq);
254}
255
Ingo Molnar5a06a362006-07-30 03:04:11 -0700256asmlinkage long sys_msgget(key_t key, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 struct msg_queue *msq;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700259 int id, ret = -EPERM;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700260 struct ipc_namespace *ns;
261
262 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Kirill Korotaev1e786932006-10-02 02:18:21 -0700264 mutex_lock(&msg_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (key == IPC_PRIVATE)
Kirill Korotaev1e786932006-10-02 02:18:21 -0700266 ret = newque(ns, key, msgflg);
267 else if ((id = ipc_findkey(&msg_ids(ns), key)) == -1) { /* key not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (!(msgflg & IPC_CREAT))
269 ret = -ENOENT;
270 else
Kirill Korotaev1e786932006-10-02 02:18:21 -0700271 ret = newque(ns, key, msgflg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 } else if (msgflg & IPC_CREAT && msgflg & IPC_EXCL) {
273 ret = -EEXIST;
274 } else {
Kirill Korotaev1e786932006-10-02 02:18:21 -0700275 msq = msg_lock(ns, id);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700276 BUG_ON(msq == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (ipcperms(&msq->q_perm, msgflg))
278 ret = -EACCES;
279 else {
Kirill Korotaev1e786932006-10-02 02:18:21 -0700280 int qid = msg_buildid(ns, id, msq->q_perm.seq);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700281
282 ret = security_msg_queue_associate(msq, msgflg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (!ret)
284 ret = qid;
285 }
286 msg_unlock(msq);
287 }
Kirill Korotaev1e786932006-10-02 02:18:21 -0700288 mutex_unlock(&msg_ids(ns).mutex);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return ret;
291}
292
Ingo Molnar5a06a362006-07-30 03:04:11 -0700293static inline unsigned long
294copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 switch(version) {
297 case IPC_64:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700298 return copy_to_user(buf, in, sizeof(*in));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 case IPC_OLD:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700300 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 struct msqid_ds out;
302
Ingo Molnar5a06a362006-07-30 03:04:11 -0700303 memset(&out, 0, sizeof(out));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
306
307 out.msg_stime = in->msg_stime;
308 out.msg_rtime = in->msg_rtime;
309 out.msg_ctime = in->msg_ctime;
310
Ingo Molnar5a06a362006-07-30 03:04:11 -0700311 if (in->msg_cbytes > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 out.msg_cbytes = USHRT_MAX;
313 else
314 out.msg_cbytes = in->msg_cbytes;
315 out.msg_lcbytes = in->msg_cbytes;
316
Ingo Molnar5a06a362006-07-30 03:04:11 -0700317 if (in->msg_qnum > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 out.msg_qnum = USHRT_MAX;
319 else
320 out.msg_qnum = in->msg_qnum;
321
Ingo Molnar5a06a362006-07-30 03:04:11 -0700322 if (in->msg_qbytes > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 out.msg_qbytes = USHRT_MAX;
324 else
325 out.msg_qbytes = in->msg_qbytes;
326 out.msg_lqbytes = in->msg_qbytes;
327
328 out.msg_lspid = in->msg_lspid;
329 out.msg_lrpid = in->msg_lrpid;
330
Ingo Molnar5a06a362006-07-30 03:04:11 -0700331 return copy_to_user(buf, &out, sizeof(out));
332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 default:
334 return -EINVAL;
335 }
336}
337
338struct msq_setbuf {
339 unsigned long qbytes;
340 uid_t uid;
341 gid_t gid;
342 mode_t mode;
343};
344
Ingo Molnar5a06a362006-07-30 03:04:11 -0700345static inline unsigned long
346copy_msqid_from_user(struct msq_setbuf *out, void __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 switch(version) {
349 case IPC_64:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700350 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 struct msqid64_ds tbuf;
352
Ingo Molnar5a06a362006-07-30 03:04:11 -0700353 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return -EFAULT;
355
356 out->qbytes = tbuf.msg_qbytes;
357 out->uid = tbuf.msg_perm.uid;
358 out->gid = tbuf.msg_perm.gid;
359 out->mode = tbuf.msg_perm.mode;
360
361 return 0;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 case IPC_OLD:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700364 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 struct msqid_ds tbuf_old;
366
Ingo Molnar5a06a362006-07-30 03:04:11 -0700367 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return -EFAULT;
369
370 out->uid = tbuf_old.msg_perm.uid;
371 out->gid = tbuf_old.msg_perm.gid;
372 out->mode = tbuf_old.msg_perm.mode;
373
Ingo Molnar5a06a362006-07-30 03:04:11 -0700374 if (tbuf_old.msg_qbytes == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 out->qbytes = tbuf_old.msg_lqbytes;
376 else
377 out->qbytes = tbuf_old.msg_qbytes;
378
379 return 0;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 default:
382 return -EINVAL;
383 }
384}
385
Ingo Molnar5a06a362006-07-30 03:04:11 -0700386asmlinkage long sys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 struct kern_ipc_perm *ipcp;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700389 struct msq_setbuf setbuf;
390 struct msg_queue *msq;
391 int err, version;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700392 struct ipc_namespace *ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (msqid < 0 || cmd < 0)
395 return -EINVAL;
396
397 version = ipc_parse_version(&cmd);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700398 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 switch (cmd) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700401 case IPC_INFO:
402 case MSG_INFO:
403 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 struct msginfo msginfo;
405 int max_id;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (!buf)
408 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700409 /*
410 * We must not return kernel stack data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 * due to padding, it's not enough
412 * to set all member fields.
413 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 err = security_msg_queue_msgctl(NULL, cmd);
415 if (err)
416 return err;
417
Ingo Molnar5a06a362006-07-30 03:04:11 -0700418 memset(&msginfo, 0, sizeof(msginfo));
Kirill Korotaev1e786932006-10-02 02:18:21 -0700419 msginfo.msgmni = ns->msg_ctlmni;
420 msginfo.msgmax = ns->msg_ctlmax;
421 msginfo.msgmnb = ns->msg_ctlmnb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 msginfo.msgssz = MSGSSZ;
423 msginfo.msgseg = MSGSEG;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700424 mutex_lock(&msg_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (cmd == MSG_INFO) {
Kirill Korotaev1e786932006-10-02 02:18:21 -0700426 msginfo.msgpool = msg_ids(ns).in_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 msginfo.msgmap = atomic_read(&msg_hdrs);
428 msginfo.msgtql = atomic_read(&msg_bytes);
429 } else {
430 msginfo.msgmap = MSGMAP;
431 msginfo.msgpool = MSGPOOL;
432 msginfo.msgtql = MSGTQL;
433 }
Kirill Korotaev1e786932006-10-02 02:18:21 -0700434 max_id = msg_ids(ns).max_id;
435 mutex_unlock(&msg_ids(ns).mutex);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700436 if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700438 return (max_id < 0) ? 0 : max_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440 case MSG_STAT:
441 case IPC_STAT:
442 {
443 struct msqid64_ds tbuf;
444 int success_return;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (!buf)
447 return -EFAULT;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700448 if (cmd == MSG_STAT && msqid >= msg_ids(ns).entries->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return -EINVAL;
450
Ingo Molnar5a06a362006-07-30 03:04:11 -0700451 memset(&tbuf, 0, sizeof(tbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Kirill Korotaev1e786932006-10-02 02:18:21 -0700453 msq = msg_lock(ns, msqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (msq == NULL)
455 return -EINVAL;
456
Ingo Molnar5a06a362006-07-30 03:04:11 -0700457 if (cmd == MSG_STAT) {
Kirill Korotaev1e786932006-10-02 02:18:21 -0700458 success_return = msg_buildid(ns, msqid, msq->q_perm.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 } else {
460 err = -EIDRM;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700461 if (msg_checkid(ns, msq, msqid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 goto out_unlock;
463 success_return = 0;
464 }
465 err = -EACCES;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700466 if (ipcperms(&msq->q_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 goto out_unlock;
468
469 err = security_msg_queue_msgctl(msq, cmd);
470 if (err)
471 goto out_unlock;
472
473 kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
474 tbuf.msg_stime = msq->q_stime;
475 tbuf.msg_rtime = msq->q_rtime;
476 tbuf.msg_ctime = msq->q_ctime;
477 tbuf.msg_cbytes = msq->q_cbytes;
478 tbuf.msg_qnum = msq->q_qnum;
479 tbuf.msg_qbytes = msq->q_qbytes;
480 tbuf.msg_lspid = msq->q_lspid;
481 tbuf.msg_lrpid = msq->q_lrpid;
482 msg_unlock(msq);
483 if (copy_msqid_to_user(buf, &tbuf, version))
484 return -EFAULT;
485 return success_return;
486 }
487 case IPC_SET:
488 if (!buf)
489 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700490 if (copy_msqid_from_user(&setbuf, buf, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 break;
493 case IPC_RMID:
494 break;
495 default:
496 return -EINVAL;
497 }
498
Kirill Korotaev1e786932006-10-02 02:18:21 -0700499 mutex_lock(&msg_ids(ns).mutex);
500 msq = msg_lock(ns, msqid);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700501 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (msq == NULL)
503 goto out_up;
504
505 err = -EIDRM;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700506 if (msg_checkid(ns, msq, msqid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 goto out_unlock_up;
508 ipcp = &msq->q_perm;
Steve Grubb073115d2006-04-02 17:07:33 -0400509
510 err = audit_ipc_obj(ipcp);
511 if (err)
512 goto out_unlock_up;
Linda Knippersac032212006-05-16 22:03:48 -0400513 if (cmd==IPC_SET) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700514 err = audit_ipc_set_perm(setbuf.qbytes, setbuf.uid, setbuf.gid,
515 setbuf.mode);
Linda Knippersac032212006-05-16 22:03:48 -0400516 if (err)
517 goto out_unlock_up;
518 }
Steve Grubb073115d2006-04-02 17:07:33 -0400519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 err = -EPERM;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700521 if (current->euid != ipcp->cuid &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700523 /* We _could_ check for CAP_CHOWN above, but we don't */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 goto out_unlock_up;
525
526 err = security_msg_queue_msgctl(msq, cmd);
527 if (err)
528 goto out_unlock_up;
529
530 switch (cmd) {
531 case IPC_SET:
532 {
533 err = -EPERM;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700534 if (setbuf.qbytes > ns->msg_ctlmnb && !capable(CAP_SYS_RESOURCE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 goto out_unlock_up;
536
537 msq->q_qbytes = setbuf.qbytes;
538
539 ipcp->uid = setbuf.uid;
540 ipcp->gid = setbuf.gid;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700541 ipcp->mode = (ipcp->mode & ~S_IRWXUGO) |
542 (S_IRWXUGO & setbuf.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 msq->q_ctime = get_seconds();
544 /* sleeping receivers might be excluded by
545 * stricter permissions.
546 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700547 expunge_all(msq, -EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* sleeping senders might be able to send
549 * due to a larger queue size.
550 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700551 ss_wakeup(&msq->q_senders, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 msg_unlock(msq);
553 break;
554 }
555 case IPC_RMID:
Kirill Korotaev1e786932006-10-02 02:18:21 -0700556 freeque(ns, msq, msqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 break;
558 }
559 err = 0;
560out_up:
Kirill Korotaev1e786932006-10-02 02:18:21 -0700561 mutex_unlock(&msg_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return err;
563out_unlock_up:
564 msg_unlock(msq);
565 goto out_up;
566out_unlock:
567 msg_unlock(msq);
568 return err;
569}
570
Ingo Molnar5a06a362006-07-30 03:04:11 -0700571static int testmsg(struct msg_msg *msg, long type, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
573 switch(mode)
574 {
575 case SEARCH_ANY:
576 return 1;
577 case SEARCH_LESSEQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700578 if (msg->m_type <=type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return 1;
580 break;
581 case SEARCH_EQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700582 if (msg->m_type == type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return 1;
584 break;
585 case SEARCH_NOTEQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700586 if (msg->m_type != type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return 1;
588 break;
589 }
590 return 0;
591}
592
Ingo Molnar5a06a362006-07-30 03:04:11 -0700593static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700595 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 tmp = msq->q_receivers.next;
598 while (tmp != &msq->q_receivers) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700599 struct msg_receiver *msr;
600
601 msr = list_entry(tmp, struct msg_receiver, r_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 tmp = tmp->next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700603 if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
604 !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
605 msr->r_msgtype, msr->r_mode)) {
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 list_del(&msr->r_list);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700608 if (msr->r_maxsize < msg->m_ts) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 msr->r_msg = NULL;
610 wake_up_process(msr->r_tsk);
611 smp_mb();
612 msr->r_msg = ERR_PTR(-E2BIG);
613 } else {
614 msr->r_msg = NULL;
615 msq->q_lrpid = msr->r_tsk->pid;
616 msq->q_rtime = get_seconds();
617 wake_up_process(msr->r_tsk);
618 smp_mb();
619 msr->r_msg = msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return 1;
622 }
623 }
624 }
625 return 0;
626}
627
Ingo Molnar5a06a362006-07-30 03:04:11 -0700628asmlinkage long
629sys_msgsnd(int msqid, struct msgbuf __user *msgp, size_t msgsz, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 struct msg_queue *msq;
632 struct msg_msg *msg;
633 long mtype;
634 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;
641 if (get_user(mtype, &msgp->mtype))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700642 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (mtype < 1)
644 return -EINVAL;
645
646 msg = load_msg(msgp->mtext, msgsz);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700647 if (IS_ERR(msg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return PTR_ERR(msg);
649
650 msg->m_type = mtype;
651 msg->m_ts = msgsz;
652
Kirill Korotaev1e786932006-10-02 02:18:21 -0700653 msq = msg_lock(ns, msqid);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700654 err = -EINVAL;
655 if (msq == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 goto out_free;
657
658 err= -EIDRM;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700659 if (msg_checkid(ns, msq, msqid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 goto out_unlock_free;
661
662 for (;;) {
663 struct msg_sender s;
664
Ingo Molnar5a06a362006-07-30 03:04:11 -0700665 err = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (ipcperms(&msq->q_perm, S_IWUGO))
667 goto out_unlock_free;
668
669 err = security_msg_queue_msgsnd(msq, msg, msgflg);
670 if (err)
671 goto out_unlock_free;
672
Ingo Molnar5a06a362006-07-30 03:04:11 -0700673 if (msgsz + msq->q_cbytes <= msq->q_qbytes &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 1 + msq->q_qnum <= msq->q_qbytes) {
675 break;
676 }
677
678 /* queue full, wait: */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700679 if (msgflg & IPC_NOWAIT) {
680 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 goto out_unlock_free;
682 }
683 ss_add(msq, &s);
684 ipc_rcu_getref(msq);
685 msg_unlock(msq);
686 schedule();
687
688 ipc_lock_by_ptr(&msq->q_perm);
689 ipc_rcu_putref(msq);
690 if (msq->q_perm.deleted) {
691 err = -EIDRM;
692 goto out_unlock_free;
693 }
694 ss_del(&s);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (signal_pending(current)) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700697 err = -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 goto out_unlock_free;
699 }
700 }
701
702 msq->q_lspid = current->tgid;
703 msq->q_stime = get_seconds();
704
Ingo Molnar5a06a362006-07-30 03:04:11 -0700705 if (!pipelined_send(msq, msg)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 /* noone is waiting for this message, enqueue it */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700707 list_add_tail(&msg->m_list, &msq->q_messages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 msq->q_cbytes += msgsz;
709 msq->q_qnum++;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700710 atomic_add(msgsz, &msg_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 atomic_inc(&msg_hdrs);
712 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 err = 0;
715 msg = NULL;
716
717out_unlock_free:
718 msg_unlock(msq);
719out_free:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700720 if (msg != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 free_msg(msg);
722 return err;
723}
724
Ingo Molnar5a06a362006-07-30 03:04:11 -0700725static inline int convert_mode(long *msgtyp, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700727 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 * find message of correct type.
729 * msgtyp = 0 => get first.
730 * msgtyp > 0 => get first message of matching type.
Ingo Molnar5a06a362006-07-30 03:04:11 -0700731 * msgtyp < 0 => get message with least type must be < abs(msgtype).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700733 if (*msgtyp == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return SEARCH_ANY;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700735 if (*msgtyp < 0) {
736 *msgtyp = -*msgtyp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return SEARCH_LESSEQUAL;
738 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700739 if (msgflg & MSG_EXCEPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return SEARCH_NOTEQUAL;
741 return SEARCH_EQUAL;
742}
743
Ingo Molnar5a06a362006-07-30 03:04:11 -0700744asmlinkage long sys_msgrcv(int msqid, struct msgbuf __user *msgp, size_t msgsz,
745 long msgtyp, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 struct msg_queue *msq;
748 struct msg_msg *msg;
749 int mode;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700750 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 if (msqid < 0 || (long) msgsz < 0)
753 return -EINVAL;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700754 mode = convert_mode(&msgtyp, msgflg);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700755 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Kirill Korotaev1e786932006-10-02 02:18:21 -0700757 msq = msg_lock(ns, msqid);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700758 if (msq == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return -EINVAL;
760
761 msg = ERR_PTR(-EIDRM);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700762 if (msg_checkid(ns, msq, msqid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 goto out_unlock;
764
765 for (;;) {
766 struct msg_receiver msr_d;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700767 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 msg = ERR_PTR(-EACCES);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700770 if (ipcperms(&msq->q_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 goto out_unlock;
772
773 msg = ERR_PTR(-EAGAIN);
774 tmp = msq->q_messages.next;
775 while (tmp != &msq->q_messages) {
776 struct msg_msg *walk_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700777
778 walk_msg = list_entry(tmp, struct msg_msg, m_list);
779 if (testmsg(walk_msg, msgtyp, mode) &&
780 !security_msg_queue_msgrcv(msq, walk_msg, current,
781 msgtyp, mode)) {
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 msg = walk_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700784 if (mode == SEARCH_LESSEQUAL &&
785 walk_msg->m_type != 1) {
786 msg = walk_msg;
787 msgtyp = walk_msg->m_type - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 } else {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700789 msg = walk_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 break;
791 }
792 }
793 tmp = tmp->next;
794 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700795 if (!IS_ERR(msg)) {
796 /*
797 * Found a suitable message.
798 * Unlink it from the queue.
799 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
801 msg = ERR_PTR(-E2BIG);
802 goto out_unlock;
803 }
804 list_del(&msg->m_list);
805 msq->q_qnum--;
806 msq->q_rtime = get_seconds();
807 msq->q_lrpid = current->tgid;
808 msq->q_cbytes -= msg->m_ts;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700809 atomic_sub(msg->m_ts, &msg_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 atomic_dec(&msg_hdrs);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700811 ss_wakeup(&msq->q_senders, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 msg_unlock(msq);
813 break;
814 }
815 /* No message waiting. Wait for a message */
816 if (msgflg & IPC_NOWAIT) {
817 msg = ERR_PTR(-ENOMSG);
818 goto out_unlock;
819 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700820 list_add_tail(&msr_d.r_list, &msq->q_receivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 msr_d.r_tsk = current;
822 msr_d.r_msgtype = msgtyp;
823 msr_d.r_mode = mode;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700824 if (msgflg & MSG_NOERROR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 msr_d.r_maxsize = INT_MAX;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700826 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 msr_d.r_maxsize = msgsz;
828 msr_d.r_msg = ERR_PTR(-EAGAIN);
829 current->state = TASK_INTERRUPTIBLE;
830 msg_unlock(msq);
831
832 schedule();
833
834 /* Lockless receive, part 1:
835 * Disable preemption. We don't hold a reference to the queue
836 * and getting a reference would defeat the idea of a lockless
837 * operation, thus the code relies on rcu to guarantee the
838 * existance of msq:
839 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
840 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
841 * rcu_read_lock() prevents preemption between reading r_msg
842 * and the spin_lock() inside ipc_lock_by_ptr().
843 */
844 rcu_read_lock();
845
846 /* Lockless receive, part 2:
847 * Wait until pipelined_send or expunge_all are outside of
848 * wake_up_process(). There is a race with exit(), see
849 * ipc/mqueue.c for the details.
850 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700851 msg = (struct msg_msg*)msr_d.r_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 while (msg == NULL) {
853 cpu_relax();
Ingo Molnar5a06a362006-07-30 03:04:11 -0700854 msg = (struct msg_msg *)msr_d.r_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856
857 /* Lockless receive, part 3:
858 * If there is a message or an error then accept it without
859 * locking.
860 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700861 if (msg != ERR_PTR(-EAGAIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 rcu_read_unlock();
863 break;
864 }
865
866 /* Lockless receive, part 3:
867 * Acquire the queue spinlock.
868 */
869 ipc_lock_by_ptr(&msq->q_perm);
870 rcu_read_unlock();
871
872 /* Lockless receive, part 4:
873 * Repeat test after acquiring the spinlock.
874 */
875 msg = (struct msg_msg*)msr_d.r_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700876 if (msg != ERR_PTR(-EAGAIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 goto out_unlock;
878
879 list_del(&msr_d.r_list);
880 if (signal_pending(current)) {
881 msg = ERR_PTR(-ERESTARTNOHAND);
882out_unlock:
883 msg_unlock(msq);
884 break;
885 }
886 }
887 if (IS_ERR(msg))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700888 return PTR_ERR(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
891 if (put_user (msg->m_type, &msgp->mtype) ||
892 store_msg(msgp->mtext, msg, msgsz)) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700893 msgsz = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895 free_msg(msg);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return msgsz;
898}
899
900#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -0700901static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Mike Waychison19b49462005-09-06 15:17:10 -0700903 struct msg_queue *msq = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Mike Waychison19b49462005-09-06 15:17:10 -0700905 return seq_printf(s,
Ingo Molnar5a06a362006-07-30 03:04:11 -0700906 "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
907 msq->q_perm.key,
908 msq->q_id,
909 msq->q_perm.mode,
910 msq->q_cbytes,
911 msq->q_qnum,
912 msq->q_lspid,
913 msq->q_lrpid,
914 msq->q_perm.uid,
915 msq->q_perm.gid,
916 msq->q_perm.cuid,
917 msq->q_perm.cgid,
918 msq->q_stime,
919 msq->q_rtime,
920 msq->q_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922#endif