blob: c2ee26f01055c0314a4386629946bb5ce3511055 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/ipc/msg.c
Ingo Molnar5a06a362006-07-30 03:04:11 -07003 * Copyright (C) 1992 Krishna Balasubramanian
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Removed all the remaining kerneld mess
6 * Catch the -EFAULT stuff properly
7 * Use GFP_KERNEL for messages as in 1.2
8 * Fixed up the unchecked user space derefs
9 * Copyright (C) 1998 Alan Cox & Andi Kleen
10 *
11 * /proc/sysvipc/msg support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
12 *
13 * mostly rewritten, threaded and wake-one semantics added
14 * MSGMAX limit removed, sysctl's added
Christian Kujau624dffc2006-01-15 02:43:54 +010015 * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
Steve Grubb073115d2006-04-02 17:07:33 -040016 *
17 * support for audit of ipc object properties and permission changes
18 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
Kirill Korotaev1e786932006-10-02 02:18:21 -070019 *
20 * namespaces support
21 * OpenVZ, SWsoft Inc.
22 * Pavel Emelianov <xemul@openvz.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 */
24
Randy.Dunlapc59ede72006-01-11 12:17:46 -080025#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/slab.h>
27#include <linux/msg.h>
28#include <linux/spinlock.h>
29#include <linux/init.h>
30#include <linux/proc_fs.h>
31#include <linux/list.h>
32#include <linux/security.h>
33#include <linux/sched.h>
34#include <linux/syscalls.h>
35#include <linux/audit.h>
Mike Waychison19b49462005-09-06 15:17:10 -070036#include <linux/seq_file.h>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080037#include <linux/mutex.h>
Kirill Korotaev1e786932006-10-02 02:18:21 -070038#include <linux/nsproxy.h>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/current.h>
41#include <asm/uaccess.h>
42#include "util.h"
43
Ingo Molnar5a06a362006-07-30 03:04:11 -070044/*
45 * one msg_receiver structure for each sleeping receiver:
46 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070047struct msg_receiver {
Ingo Molnar5a06a362006-07-30 03:04:11 -070048 struct list_head r_list;
49 struct task_struct *r_tsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Ingo Molnar5a06a362006-07-30 03:04:11 -070051 int r_mode;
52 long r_msgtype;
53 long r_maxsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds80491eb2006-11-04 09:55:00 -080055 struct msg_msg *volatile r_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056};
57
58/* one msg_sender for each sleeping sender */
59struct msg_sender {
Ingo Molnar5a06a362006-07-30 03:04:11 -070060 struct list_head list;
61 struct task_struct *tsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
63
64#define SEARCH_ANY 1
65#define SEARCH_EQUAL 2
66#define SEARCH_NOTEQUAL 3
67#define SEARCH_LESSEQUAL 4
68
Ingo Molnar5a06a362006-07-30 03:04:11 -070069static atomic_t msg_bytes = ATOMIC_INIT(0);
70static atomic_t msg_hdrs = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Kirill Korotaev1e786932006-10-02 02:18:21 -070072static struct ipc_ids init_msg_ids;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Kirill Korotaev1e786932006-10-02 02:18:21 -070074#define msg_ids(ns) (*((ns)->ids[IPC_MSG_IDS]))
75
76#define msg_lock(ns, id) ((struct msg_queue*)ipc_lock(&msg_ids(ns), id))
Ingo Molnar5a06a362006-07-30 03:04:11 -070077#define msg_unlock(msq) ipc_unlock(&(msq)->q_perm)
Kirill Korotaev1e786932006-10-02 02:18:21 -070078#define msg_checkid(ns, msq, msgid) \
79 ipc_checkid(&msg_ids(ns), &msq->q_perm, msgid)
80#define msg_buildid(ns, id, seq) \
81 ipc_buildid(&msg_ids(ns), id, seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Nadia Derbey7ca7e562007-10-18 23:40:48 -070083static void freeque(struct ipc_namespace *, struct msg_queue *);
Nadia Derbey7748dbf2007-10-18 23:40:49 -070084static int newque(struct ipc_namespace *, struct ipc_params *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -070086static int sysvipc_msg_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#endif
88
Cedric Le Goater7d69a1f2007-07-15 23:40:58 -070089static void __msg_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Kirill Korotaev1e786932006-10-02 02:18:21 -070091 ns->ids[IPC_MSG_IDS] = ids;
92 ns->msg_ctlmax = MSGMAX;
93 ns->msg_ctlmnb = MSGMNB;
94 ns->msg_ctlmni = MSGMNI;
Nadia Derbey7ca7e562007-10-18 23:40:48 -070095 ipc_init_ids(ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Kirill Korotaev1e786932006-10-02 02:18:21 -070098int msg_init_ns(struct ipc_namespace *ns)
99{
100 struct ipc_ids *ids;
101
102 ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
103 if (ids == NULL)
104 return -ENOMEM;
105
106 __msg_init_ns(ns, ids);
107 return 0;
108}
109
110void msg_exit_ns(struct ipc_namespace *ns)
111{
Kirill Korotaev1e786932006-10-02 02:18:21 -0700112 struct msg_queue *msq;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700113 int next_id;
114 int total, in_use;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700115
116 mutex_lock(&msg_ids(ns).mutex);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700117
118 in_use = msg_ids(ns).in_use;
119
120 for (total = 0, next_id = 0; total < in_use; next_id++) {
121 msq = idr_find(&msg_ids(ns).ipcs_idr, next_id);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700122 if (msq == NULL)
123 continue;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700124 ipc_lock_by_ptr(&msq->q_perm);
125 freeque(ns, msq);
126 total++;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700127 }
128 mutex_unlock(&msg_ids(ns).mutex);
129
130 kfree(ns->ids[IPC_MSG_IDS]);
131 ns->ids[IPC_MSG_IDS] = NULL;
132}
Kirill Korotaev1e786932006-10-02 02:18:21 -0700133
134void __init msg_init(void)
135{
136 __msg_init_ns(&init_ipc_ns, &init_msg_ids);
137 ipc_init_proc_interface("sysvipc/msg",
138 " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
139 IPC_MSG_IDS, sysvipc_msg_proc_show);
140}
141
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700142static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s)
143{
144 ipc_rmid(&msg_ids(ns), &s->q_perm);
145}
146
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700147static int newque(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 struct msg_queue *msq;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700150 int id, retval;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700151 key_t key = params->key;
152 int msgflg = params->flg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Ingo Molnar5a06a362006-07-30 03:04:11 -0700154 msq = ipc_rcu_alloc(sizeof(*msq));
155 if (!msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return -ENOMEM;
157
Ingo Molnar5a06a362006-07-30 03:04:11 -0700158 msq->q_perm.mode = msgflg & S_IRWXUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 msq->q_perm.key = key;
160
161 msq->q_perm.security = NULL;
162 retval = security_msg_queue_alloc(msq);
163 if (retval) {
164 ipc_rcu_putref(msq);
165 return retval;
166 }
167
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700168 /*
169 * ipc_addid() locks msq
170 */
Kirill Korotaev1e786932006-10-02 02:18:21 -0700171 id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700172 if (id == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 security_msg_queue_free(msq);
174 ipc_rcu_putref(msq);
175 return -ENOSPC;
176 }
177
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700178 msq->q_perm.id = msg_buildid(ns, id, msq->q_perm.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 msq->q_stime = msq->q_rtime = 0;
180 msq->q_ctime = get_seconds();
181 msq->q_cbytes = msq->q_qnum = 0;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700182 msq->q_qbytes = ns->msg_ctlmnb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 msq->q_lspid = msq->q_lrpid = 0;
184 INIT_LIST_HEAD(&msq->q_messages);
185 INIT_LIST_HEAD(&msq->q_receivers);
186 INIT_LIST_HEAD(&msq->q_senders);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 msg_unlock(msq);
189
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700190 return msq->q_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Ingo Molnar5a06a362006-07-30 03:04:11 -0700193static inline void ss_add(struct msg_queue *msq, struct msg_sender *mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700195 mss->tsk = current;
196 current->state = TASK_INTERRUPTIBLE;
197 list_add_tail(&mss->list, &msq->q_senders);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
Ingo Molnar5a06a362006-07-30 03:04:11 -0700200static inline void ss_del(struct msg_sender *mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700202 if (mss->list.next != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 list_del(&mss->list);
204}
205
Ingo Molnar5a06a362006-07-30 03:04:11 -0700206static void ss_wakeup(struct list_head *h, int kill)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 struct list_head *tmp;
209
210 tmp = h->next;
211 while (tmp != h) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700212 struct msg_sender *mss;
213
214 mss = list_entry(tmp, struct msg_sender, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 tmp = tmp->next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700216 if (kill)
217 mss->list.next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 wake_up_process(mss->tsk);
219 }
220}
221
Ingo Molnar5a06a362006-07-30 03:04:11 -0700222static void expunge_all(struct msg_queue *msq, int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 struct list_head *tmp;
225
226 tmp = msq->q_receivers.next;
227 while (tmp != &msq->q_receivers) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700228 struct msg_receiver *msr;
229
230 msr = list_entry(tmp, struct msg_receiver, r_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 tmp = tmp->next;
232 msr->r_msg = NULL;
233 wake_up_process(msr->r_tsk);
234 smp_mb();
235 msr->r_msg = ERR_PTR(res);
236 }
237}
Ingo Molnar5a06a362006-07-30 03:04:11 -0700238
239/*
240 * freeque() wakes up waiters on the sender and receiver waiting queue,
241 * removes the message queue from message queue ID
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700242 * IDR, and cleans up all the messages associated with this queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 *
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700244 * msg_ids.mutex and the spinlock for this message queue are held
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800245 * before freeque() is called. msg_ids.mutex remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 */
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700247static void freeque(struct ipc_namespace *ns, struct msg_queue *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 struct list_head *tmp;
250
Ingo Molnar5a06a362006-07-30 03:04:11 -0700251 expunge_all(msq, -EIDRM);
252 ss_wakeup(&msq->q_senders, 1);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700253 msg_rmid(ns, msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 msg_unlock(msq);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 tmp = msq->q_messages.next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700257 while (tmp != &msq->q_messages) {
258 struct msg_msg *msg = list_entry(tmp, struct msg_msg, m_list);
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 tmp = tmp->next;
261 atomic_dec(&msg_hdrs);
262 free_msg(msg);
263 }
264 atomic_sub(msq->q_cbytes, &msg_bytes);
265 security_msg_queue_free(msq);
266 ipc_rcu_putref(msq);
267}
268
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700269static inline int msg_security(void *msq, int msgflg)
270{
271 return security_msg_queue_associate((struct msg_queue *) msq, msgflg);
272}
273
Ingo Molnar5a06a362006-07-30 03:04:11 -0700274asmlinkage long sys_msgget(key_t key, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Kirill Korotaev1e786932006-10-02 02:18:21 -0700276 struct ipc_namespace *ns;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700277 struct ipc_ops msg_ops;
278 struct ipc_params msg_params;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700279
280 ns = current->nsproxy->ipc_ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700281
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700282 msg_ops.getnew = newque;
283 msg_ops.associate = msg_security;
284 msg_ops.more_checks = NULL;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700285
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700286 msg_params.key = key;
287 msg_params.flg = msgflg;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700288
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700289 return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Ingo Molnar5a06a362006-07-30 03:04:11 -0700292static inline unsigned long
293copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 switch(version) {
296 case IPC_64:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700297 return copy_to_user(buf, in, sizeof(*in));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 case IPC_OLD:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700299 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 struct msqid_ds out;
301
Ingo Molnar5a06a362006-07-30 03:04:11 -0700302 memset(&out, 0, sizeof(out));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
305
306 out.msg_stime = in->msg_stime;
307 out.msg_rtime = in->msg_rtime;
308 out.msg_ctime = in->msg_ctime;
309
Ingo Molnar5a06a362006-07-30 03:04:11 -0700310 if (in->msg_cbytes > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 out.msg_cbytes = USHRT_MAX;
312 else
313 out.msg_cbytes = in->msg_cbytes;
314 out.msg_lcbytes = in->msg_cbytes;
315
Ingo Molnar5a06a362006-07-30 03:04:11 -0700316 if (in->msg_qnum > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 out.msg_qnum = USHRT_MAX;
318 else
319 out.msg_qnum = in->msg_qnum;
320
Ingo Molnar5a06a362006-07-30 03:04:11 -0700321 if (in->msg_qbytes > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 out.msg_qbytes = USHRT_MAX;
323 else
324 out.msg_qbytes = in->msg_qbytes;
325 out.msg_lqbytes = in->msg_qbytes;
326
327 out.msg_lspid = in->msg_lspid;
328 out.msg_lrpid = in->msg_lrpid;
329
Ingo Molnar5a06a362006-07-30 03:04:11 -0700330 return copy_to_user(buf, &out, sizeof(out));
331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 default:
333 return -EINVAL;
334 }
335}
336
337struct msq_setbuf {
338 unsigned long qbytes;
339 uid_t uid;
340 gid_t gid;
341 mode_t mode;
342};
343
Ingo Molnar5a06a362006-07-30 03:04:11 -0700344static inline unsigned long
345copy_msqid_from_user(struct msq_setbuf *out, void __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347 switch(version) {
348 case IPC_64:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700349 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 struct msqid64_ds tbuf;
351
Ingo Molnar5a06a362006-07-30 03:04:11 -0700352 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return -EFAULT;
354
355 out->qbytes = tbuf.msg_qbytes;
356 out->uid = tbuf.msg_perm.uid;
357 out->gid = tbuf.msg_perm.gid;
358 out->mode = tbuf.msg_perm.mode;
359
360 return 0;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 case IPC_OLD:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700363 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 struct msqid_ds tbuf_old;
365
Ingo Molnar5a06a362006-07-30 03:04:11 -0700366 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return -EFAULT;
368
369 out->uid = tbuf_old.msg_perm.uid;
370 out->gid = tbuf_old.msg_perm.gid;
371 out->mode = tbuf_old.msg_perm.mode;
372
Ingo Molnar5a06a362006-07-30 03:04:11 -0700373 if (tbuf_old.msg_qbytes == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 out->qbytes = tbuf_old.msg_lqbytes;
375 else
376 out->qbytes = tbuf_old.msg_qbytes;
377
378 return 0;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 default:
381 return -EINVAL;
382 }
383}
384
Ingo Molnar5a06a362006-07-30 03:04:11 -0700385asmlinkage long sys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 struct kern_ipc_perm *ipcp;
Jeff Garzik8e1c0912007-07-17 05:40:59 -0400388 struct msq_setbuf uninitialized_var(setbuf);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700389 struct msg_queue *msq;
390 int err, version;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700391 struct ipc_namespace *ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (msqid < 0 || cmd < 0)
394 return -EINVAL;
395
396 version = ipc_parse_version(&cmd);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700397 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 switch (cmd) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700400 case IPC_INFO:
401 case MSG_INFO:
402 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 struct msginfo msginfo;
404 int max_id;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (!buf)
407 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700408 /*
409 * We must not return kernel stack data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 * due to padding, it's not enough
411 * to set all member fields.
412 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 err = security_msg_queue_msgctl(NULL, cmd);
414 if (err)
415 return err;
416
Ingo Molnar5a06a362006-07-30 03:04:11 -0700417 memset(&msginfo, 0, sizeof(msginfo));
Kirill Korotaev1e786932006-10-02 02:18:21 -0700418 msginfo.msgmni = ns->msg_ctlmni;
419 msginfo.msgmax = ns->msg_ctlmax;
420 msginfo.msgmnb = ns->msg_ctlmnb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 msginfo.msgssz = MSGSSZ;
422 msginfo.msgseg = MSGSEG;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700423 mutex_lock(&msg_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (cmd == MSG_INFO) {
Kirill Korotaev1e786932006-10-02 02:18:21 -0700425 msginfo.msgpool = msg_ids(ns).in_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 msginfo.msgmap = atomic_read(&msg_hdrs);
427 msginfo.msgtql = atomic_read(&msg_bytes);
428 } else {
429 msginfo.msgmap = MSGMAP;
430 msginfo.msgpool = MSGPOOL;
431 msginfo.msgtql = MSGTQL;
432 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700433 max_id = ipc_get_maxid(&msg_ids(ns));
Kirill Korotaev1e786932006-10-02 02:18:21 -0700434 mutex_unlock(&msg_ids(ns).mutex);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700435 if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700437 return (max_id < 0) ? 0 : max_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700439 case MSG_STAT: /* msqid is an index rather than a msg queue id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 case IPC_STAT:
441 {
442 struct msqid64_ds tbuf;
443 int success_return;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (!buf)
446 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Ingo Molnar5a06a362006-07-30 03:04:11 -0700448 memset(&tbuf, 0, sizeof(tbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Kirill Korotaev1e786932006-10-02 02:18:21 -0700450 msq = msg_lock(ns, msqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (msq == NULL)
452 return -EINVAL;
453
Ingo Molnar5a06a362006-07-30 03:04:11 -0700454 if (cmd == MSG_STAT) {
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700455 success_return = msq->q_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 } else {
457 err = -EIDRM;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700458 if (msg_checkid(ns, msq, msqid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 goto out_unlock;
460 success_return = 0;
461 }
462 err = -EACCES;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700463 if (ipcperms(&msq->q_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 goto out_unlock;
465
466 err = security_msg_queue_msgctl(msq, cmd);
467 if (err)
468 goto out_unlock;
469
470 kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
471 tbuf.msg_stime = msq->q_stime;
472 tbuf.msg_rtime = msq->q_rtime;
473 tbuf.msg_ctime = msq->q_ctime;
474 tbuf.msg_cbytes = msq->q_cbytes;
475 tbuf.msg_qnum = msq->q_qnum;
476 tbuf.msg_qbytes = msq->q_qbytes;
477 tbuf.msg_lspid = msq->q_lspid;
478 tbuf.msg_lrpid = msq->q_lrpid;
479 msg_unlock(msq);
480 if (copy_msqid_to_user(buf, &tbuf, version))
481 return -EFAULT;
482 return success_return;
483 }
484 case IPC_SET:
485 if (!buf)
486 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700487 if (copy_msqid_from_user(&setbuf, buf, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 break;
490 case IPC_RMID:
491 break;
492 default:
493 return -EINVAL;
494 }
495
Kirill Korotaev1e786932006-10-02 02:18:21 -0700496 mutex_lock(&msg_ids(ns).mutex);
497 msq = msg_lock(ns, msqid);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700498 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (msq == NULL)
500 goto out_up;
501
502 err = -EIDRM;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700503 if (msg_checkid(ns, msq, msqid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 goto out_unlock_up;
505 ipcp = &msq->q_perm;
Steve Grubb073115d2006-04-02 17:07:33 -0400506
507 err = audit_ipc_obj(ipcp);
508 if (err)
509 goto out_unlock_up;
Jeff Garzik8e1c0912007-07-17 05:40:59 -0400510 if (cmd == IPC_SET) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700511 err = audit_ipc_set_perm(setbuf.qbytes, setbuf.uid, setbuf.gid,
512 setbuf.mode);
Linda Knippersac032212006-05-16 22:03:48 -0400513 if (err)
514 goto out_unlock_up;
515 }
Steve Grubb073115d2006-04-02 17:07:33 -0400516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 err = -EPERM;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700518 if (current->euid != ipcp->cuid &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700520 /* We _could_ check for CAP_CHOWN above, but we don't */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 goto out_unlock_up;
522
523 err = security_msg_queue_msgctl(msq, cmd);
524 if (err)
525 goto out_unlock_up;
526
527 switch (cmd) {
528 case IPC_SET:
529 {
530 err = -EPERM;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700531 if (setbuf.qbytes > ns->msg_ctlmnb && !capable(CAP_SYS_RESOURCE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 goto out_unlock_up;
533
534 msq->q_qbytes = setbuf.qbytes;
535
536 ipcp->uid = setbuf.uid;
537 ipcp->gid = setbuf.gid;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700538 ipcp->mode = (ipcp->mode & ~S_IRWXUGO) |
539 (S_IRWXUGO & setbuf.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 msq->q_ctime = get_seconds();
541 /* sleeping receivers might be excluded by
542 * stricter permissions.
543 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700544 expunge_all(msq, -EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 /* sleeping senders might be able to send
546 * due to a larger queue size.
547 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700548 ss_wakeup(&msq->q_senders, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 msg_unlock(msq);
550 break;
551 }
552 case IPC_RMID:
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700553 freeque(ns, msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 break;
555 }
556 err = 0;
557out_up:
Kirill Korotaev1e786932006-10-02 02:18:21 -0700558 mutex_unlock(&msg_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return err;
560out_unlock_up:
561 msg_unlock(msq);
562 goto out_up;
563out_unlock:
564 msg_unlock(msq);
565 return err;
566}
567
Ingo Molnar5a06a362006-07-30 03:04:11 -0700568static int testmsg(struct msg_msg *msg, long type, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 switch(mode)
571 {
572 case SEARCH_ANY:
573 return 1;
574 case SEARCH_LESSEQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700575 if (msg->m_type <=type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return 1;
577 break;
578 case SEARCH_EQUAL:
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_NOTEQUAL:
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 }
587 return 0;
588}
589
Ingo Molnar5a06a362006-07-30 03:04:11 -0700590static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700592 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 tmp = msq->q_receivers.next;
595 while (tmp != &msq->q_receivers) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700596 struct msg_receiver *msr;
597
598 msr = list_entry(tmp, struct msg_receiver, r_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 tmp = tmp->next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700600 if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
601 !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
602 msr->r_msgtype, msr->r_mode)) {
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 list_del(&msr->r_list);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700605 if (msr->r_maxsize < msg->m_ts) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 msr->r_msg = NULL;
607 wake_up_process(msr->r_tsk);
608 smp_mb();
609 msr->r_msg = ERR_PTR(-E2BIG);
610 } else {
611 msr->r_msg = NULL;
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700612 msq->q_lrpid = task_pid_vnr(msr->r_tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 msq->q_rtime = get_seconds();
614 wake_up_process(msr->r_tsk);
615 smp_mb();
616 msr->r_msg = msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return 1;
619 }
620 }
621 }
622 return 0;
623}
624
suzuki651971c2006-12-06 20:37:48 -0800625long do_msgsnd(int msqid, long mtype, void __user *mtext,
626 size_t msgsz, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
628 struct msg_queue *msq;
629 struct msg_msg *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 int err;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700631 struct ipc_namespace *ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700632
Kirill Korotaev1e786932006-10-02 02:18:21 -0700633 ns = current->nsproxy->ipc_ns;
634
635 if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (mtype < 1)
638 return -EINVAL;
639
suzuki651971c2006-12-06 20:37:48 -0800640 msg = load_msg(mtext, msgsz);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700641 if (IS_ERR(msg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return PTR_ERR(msg);
643
644 msg->m_type = mtype;
645 msg->m_ts = msgsz;
646
Kirill Korotaev1e786932006-10-02 02:18:21 -0700647 msq = msg_lock(ns, msqid);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700648 err = -EINVAL;
649 if (msq == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 goto out_free;
651
652 err= -EIDRM;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700653 if (msg_checkid(ns, msq, msqid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 goto out_unlock_free;
655
656 for (;;) {
657 struct msg_sender s;
658
Ingo Molnar5a06a362006-07-30 03:04:11 -0700659 err = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (ipcperms(&msq->q_perm, S_IWUGO))
661 goto out_unlock_free;
662
663 err = security_msg_queue_msgsnd(msq, msg, msgflg);
664 if (err)
665 goto out_unlock_free;
666
Ingo Molnar5a06a362006-07-30 03:04:11 -0700667 if (msgsz + msq->q_cbytes <= msq->q_qbytes &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 1 + msq->q_qnum <= msq->q_qbytes) {
669 break;
670 }
671
672 /* queue full, wait: */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700673 if (msgflg & IPC_NOWAIT) {
674 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 goto out_unlock_free;
676 }
677 ss_add(msq, &s);
678 ipc_rcu_getref(msq);
679 msg_unlock(msq);
680 schedule();
681
682 ipc_lock_by_ptr(&msq->q_perm);
683 ipc_rcu_putref(msq);
684 if (msq->q_perm.deleted) {
685 err = -EIDRM;
686 goto out_unlock_free;
687 }
688 ss_del(&s);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (signal_pending(current)) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700691 err = -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 goto out_unlock_free;
693 }
694 }
695
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700696 msq->q_lspid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 msq->q_stime = get_seconds();
698
Ingo Molnar5a06a362006-07-30 03:04:11 -0700699 if (!pipelined_send(msq, msg)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 /* noone is waiting for this message, enqueue it */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700701 list_add_tail(&msg->m_list, &msq->q_messages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 msq->q_cbytes += msgsz;
703 msq->q_qnum++;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700704 atomic_add(msgsz, &msg_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 atomic_inc(&msg_hdrs);
706 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 err = 0;
709 msg = NULL;
710
711out_unlock_free:
712 msg_unlock(msq);
713out_free:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700714 if (msg != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 free_msg(msg);
716 return err;
717}
718
suzuki651971c2006-12-06 20:37:48 -0800719asmlinkage long
720sys_msgsnd(int msqid, struct msgbuf __user *msgp, size_t msgsz, int msgflg)
721{
722 long mtype;
723
724 if (get_user(mtype, &msgp->mtype))
725 return -EFAULT;
726 return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
727}
728
Ingo Molnar5a06a362006-07-30 03:04:11 -0700729static inline int convert_mode(long *msgtyp, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700731 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 * find message of correct type.
733 * msgtyp = 0 => get first.
734 * msgtyp > 0 => get first message of matching type.
Ingo Molnar5a06a362006-07-30 03:04:11 -0700735 * msgtyp < 0 => get message with least type must be < abs(msgtype).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700737 if (*msgtyp == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return SEARCH_ANY;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700739 if (*msgtyp < 0) {
740 *msgtyp = -*msgtyp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return SEARCH_LESSEQUAL;
742 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700743 if (msgflg & MSG_EXCEPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return SEARCH_NOTEQUAL;
745 return SEARCH_EQUAL;
746}
747
suzuki651971c2006-12-06 20:37:48 -0800748long do_msgrcv(int msqid, long *pmtype, void __user *mtext,
749 size_t msgsz, long msgtyp, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
751 struct msg_queue *msq;
752 struct msg_msg *msg;
753 int mode;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700754 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 if (msqid < 0 || (long) msgsz < 0)
757 return -EINVAL;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700758 mode = convert_mode(&msgtyp, msgflg);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700759 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Kirill Korotaev1e786932006-10-02 02:18:21 -0700761 msq = msg_lock(ns, msqid);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700762 if (msq == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return -EINVAL;
764
765 msg = ERR_PTR(-EIDRM);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700766 if (msg_checkid(ns, msq, msqid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 goto out_unlock;
768
769 for (;;) {
770 struct msg_receiver msr_d;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700771 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 msg = ERR_PTR(-EACCES);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700774 if (ipcperms(&msq->q_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 goto out_unlock;
776
777 msg = ERR_PTR(-EAGAIN);
778 tmp = msq->q_messages.next;
779 while (tmp != &msq->q_messages) {
780 struct msg_msg *walk_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700781
782 walk_msg = list_entry(tmp, struct msg_msg, m_list);
783 if (testmsg(walk_msg, msgtyp, mode) &&
784 !security_msg_queue_msgrcv(msq, walk_msg, current,
785 msgtyp, mode)) {
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 msg = walk_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700788 if (mode == SEARCH_LESSEQUAL &&
789 walk_msg->m_type != 1) {
790 msg = walk_msg;
791 msgtyp = walk_msg->m_type - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 } else {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700793 msg = walk_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 break;
795 }
796 }
797 tmp = tmp->next;
798 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700799 if (!IS_ERR(msg)) {
800 /*
801 * Found a suitable message.
802 * Unlink it from the queue.
803 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
805 msg = ERR_PTR(-E2BIG);
806 goto out_unlock;
807 }
808 list_del(&msg->m_list);
809 msq->q_qnum--;
810 msq->q_rtime = get_seconds();
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700811 msq->q_lrpid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 msq->q_cbytes -= msg->m_ts;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700813 atomic_sub(msg->m_ts, &msg_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 atomic_dec(&msg_hdrs);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700815 ss_wakeup(&msq->q_senders, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 msg_unlock(msq);
817 break;
818 }
819 /* No message waiting. Wait for a message */
820 if (msgflg & IPC_NOWAIT) {
821 msg = ERR_PTR(-ENOMSG);
822 goto out_unlock;
823 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700824 list_add_tail(&msr_d.r_list, &msq->q_receivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 msr_d.r_tsk = current;
826 msr_d.r_msgtype = msgtyp;
827 msr_d.r_mode = mode;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700828 if (msgflg & MSG_NOERROR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 msr_d.r_maxsize = INT_MAX;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700830 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 msr_d.r_maxsize = msgsz;
832 msr_d.r_msg = ERR_PTR(-EAGAIN);
833 current->state = TASK_INTERRUPTIBLE;
834 msg_unlock(msq);
835
836 schedule();
837
838 /* Lockless receive, part 1:
839 * Disable preemption. We don't hold a reference to the queue
840 * and getting a reference would defeat the idea of a lockless
841 * operation, thus the code relies on rcu to guarantee the
842 * existance of msq:
843 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
844 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
845 * rcu_read_lock() prevents preemption between reading r_msg
846 * and the spin_lock() inside ipc_lock_by_ptr().
847 */
848 rcu_read_lock();
849
850 /* Lockless receive, part 2:
851 * Wait until pipelined_send or expunge_all are outside of
852 * wake_up_process(). There is a race with exit(), see
853 * ipc/mqueue.c for the details.
854 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700855 msg = (struct msg_msg*)msr_d.r_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 while (msg == NULL) {
857 cpu_relax();
Ingo Molnar5a06a362006-07-30 03:04:11 -0700858 msg = (struct msg_msg *)msr_d.r_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860
861 /* Lockless receive, part 3:
862 * If there is a message or an error then accept it without
863 * locking.
864 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700865 if (msg != ERR_PTR(-EAGAIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 rcu_read_unlock();
867 break;
868 }
869
870 /* Lockless receive, part 3:
871 * Acquire the queue spinlock.
872 */
873 ipc_lock_by_ptr(&msq->q_perm);
874 rcu_read_unlock();
875
876 /* Lockless receive, part 4:
877 * Repeat test after acquiring the spinlock.
878 */
879 msg = (struct msg_msg*)msr_d.r_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700880 if (msg != ERR_PTR(-EAGAIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 goto out_unlock;
882
883 list_del(&msr_d.r_list);
884 if (signal_pending(current)) {
885 msg = ERR_PTR(-ERESTARTNOHAND);
886out_unlock:
887 msg_unlock(msq);
888 break;
889 }
890 }
891 if (IS_ERR(msg))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700892 return PTR_ERR(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
suzuki651971c2006-12-06 20:37:48 -0800895 *pmtype = msg->m_type;
896 if (store_msg(mtext, msg, msgsz))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700897 msgsz = -EFAULT;
suzuki651971c2006-12-06 20:37:48 -0800898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 free_msg(msg);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return msgsz;
902}
903
suzuki651971c2006-12-06 20:37:48 -0800904asmlinkage long sys_msgrcv(int msqid, struct msgbuf __user *msgp, size_t msgsz,
905 long msgtyp, int msgflg)
906{
907 long err, mtype;
908
909 err = do_msgrcv(msqid, &mtype, msgp->mtext, msgsz, msgtyp, msgflg);
910 if (err < 0)
911 goto out;
912
913 if (put_user(mtype, &msgp->mtype))
914 err = -EFAULT;
915out:
916 return err;
917}
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -0700920static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Mike Waychison19b49462005-09-06 15:17:10 -0700922 struct msg_queue *msq = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Mike Waychison19b49462005-09-06 15:17:10 -0700924 return seq_printf(s,
Ingo Molnar5a06a362006-07-30 03:04:11 -0700925 "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
926 msq->q_perm.key,
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700927 msq->q_perm.id,
Ingo Molnar5a06a362006-07-30 03:04:11 -0700928 msq->q_perm.mode,
929 msq->q_cbytes,
930 msq->q_qnum,
931 msq->q_lspid,
932 msq->q_lrpid,
933 msq->q_perm.uid,
934 msq->q_perm.gid,
935 msq->q_perm.cuid,
936 msq->q_perm.cgid,
937 msq->q_stime,
938 msq->q_rtime,
939 msq->q_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941#endif