blob: ab0c38b29533ca497b172edf301b24137e31d543 [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>
Nadia Derbey3e148c72007-10-18 23:40:54 -070037#include <linux/rwsem.h>
Kirill Korotaev1e786932006-10-02 02:18:21 -070038#include <linux/nsproxy.h>
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080039#include <linux/ipc_namespace.h>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/current.h>
42#include <asm/uaccess.h>
43#include "util.h"
44
Ingo Molnar5a06a362006-07-30 03:04:11 -070045/*
46 * one msg_receiver structure for each sleeping receiver:
47 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048struct msg_receiver {
Ingo Molnar5a06a362006-07-30 03:04:11 -070049 struct list_head r_list;
50 struct task_struct *r_tsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Ingo Molnar5a06a362006-07-30 03:04:11 -070052 int r_mode;
53 long r_msgtype;
54 long r_maxsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Linus Torvalds80491eb2006-11-04 09:55:00 -080056 struct msg_msg *volatile r_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057};
58
59/* one msg_sender for each sleeping sender */
60struct msg_sender {
Ingo Molnar5a06a362006-07-30 03:04:11 -070061 struct list_head list;
62 struct task_struct *tsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
65#define SEARCH_ANY 1
66#define SEARCH_EQUAL 2
67#define SEARCH_NOTEQUAL 3
68#define SEARCH_LESSEQUAL 4
69
Pierre Peiffered2ddbf2008-02-08 04:18:57 -080070#define msg_ids(ns) ((ns)->ids[IPC_MSG_IDS])
Kirill Korotaev1e786932006-10-02 02:18:21 -070071
Ingo Molnar5a06a362006-07-30 03:04:11 -070072#define msg_unlock(msq) ipc_unlock(&(msq)->q_perm)
Nadia Derbey1b531f22007-10-18 23:40:55 -070073#define msg_buildid(id, seq) ipc_buildid(id, seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Nadia Derbey7ca7e562007-10-18 23:40:48 -070075static void freeque(struct ipc_namespace *, struct msg_queue *);
Nadia Derbey7748dbf2007-10-18 23:40:49 -070076static int newque(struct ipc_namespace *, struct ipc_params *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -070078static int sysvipc_msg_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#endif
80
Pierre Peiffered2ddbf2008-02-08 04:18:57 -080081void msg_init_ns(struct ipc_namespace *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Kirill Korotaev1e786932006-10-02 02:18:21 -070083 ns->msg_ctlmax = MSGMAX;
84 ns->msg_ctlmnb = MSGMNB;
85 ns->msg_ctlmni = MSGMNI;
Kirill Korotaev3ac88a42007-10-18 23:40:56 -070086 atomic_set(&ns->msg_bytes, 0);
87 atomic_set(&ns->msg_hdrs, 0);
Pierre Peiffered2ddbf2008-02-08 04:18:57 -080088 ipc_init_ids(&ns->ids[IPC_MSG_IDS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080091#ifdef CONFIG_IPC_NS
Kirill Korotaev1e786932006-10-02 02:18:21 -070092void msg_exit_ns(struct ipc_namespace *ns)
93{
Kirill Korotaev1e786932006-10-02 02:18:21 -070094 struct msg_queue *msq;
Pierre Peifferb1ed88b2008-02-06 01:36:23 -080095 struct kern_ipc_perm *perm;
Nadia Derbey7ca7e562007-10-18 23:40:48 -070096 int next_id;
97 int total, in_use;
Kirill Korotaev1e786932006-10-02 02:18:21 -070098
Nadia Derbey3e148c72007-10-18 23:40:54 -070099 down_write(&msg_ids(ns).rw_mutex);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700100
101 in_use = msg_ids(ns).in_use;
102
103 for (total = 0, next_id = 0; total < in_use; next_id++) {
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800104 perm = idr_find(&msg_ids(ns).ipcs_idr, next_id);
105 if (perm == NULL)
Kirill Korotaev1e786932006-10-02 02:18:21 -0700106 continue;
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800107 ipc_lock_by_ptr(perm);
108 msq = container_of(perm, struct msg_queue, q_perm);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700109 freeque(ns, msq);
110 total++;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700111 }
Nadia Derbey3e148c72007-10-18 23:40:54 -0700112
113 up_write(&msg_ids(ns).rw_mutex);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700114}
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800115#endif
Kirill Korotaev1e786932006-10-02 02:18:21 -0700116
117void __init msg_init(void)
118{
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800119 msg_init_ns(&init_ipc_ns);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700120 ipc_init_proc_interface("sysvipc/msg",
121 " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
122 IPC_MSG_IDS, sysvipc_msg_proc_show);
123}
124
Nadia Derbey3e148c72007-10-18 23:40:54 -0700125/*
126 * This routine is called in the paths where the rw_mutex is held to protect
127 * access to the idr tree.
128 */
129static inline struct msg_queue *msg_lock_check_down(struct ipc_namespace *ns,
130 int id)
131{
132 struct kern_ipc_perm *ipcp = ipc_lock_check_down(&msg_ids(ns), id);
133
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800134 if (IS_ERR(ipcp))
135 return (struct msg_queue *)ipcp;
136
Nadia Derbey3e148c72007-10-18 23:40:54 -0700137 return container_of(ipcp, struct msg_queue, q_perm);
138}
139
140/*
141 * msg_lock_(check_) routines are called in the paths where the rw_mutex
142 * is not held.
143 */
Nadia Derbey023a5352007-10-18 23:40:51 -0700144static inline struct msg_queue *msg_lock(struct ipc_namespace *ns, int id)
145{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700146 struct kern_ipc_perm *ipcp = ipc_lock(&msg_ids(ns), id);
147
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800148 if (IS_ERR(ipcp))
149 return (struct msg_queue *)ipcp;
150
Nadia Derbey03f02c72007-10-18 23:40:51 -0700151 return container_of(ipcp, struct msg_queue, q_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700152}
153
154static inline struct msg_queue *msg_lock_check(struct ipc_namespace *ns,
155 int id)
156{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700157 struct kern_ipc_perm *ipcp = ipc_lock_check(&msg_ids(ns), id);
158
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800159 if (IS_ERR(ipcp))
160 return (struct msg_queue *)ipcp;
161
Nadia Derbey03f02c72007-10-18 23:40:51 -0700162 return container_of(ipcp, struct msg_queue, q_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700163}
164
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700165static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s)
166{
167 ipc_rmid(&msg_ids(ns), &s->q_perm);
168}
169
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700170/**
171 * newque - Create a new msg queue
172 * @ns: namespace
173 * @params: ptr to the structure that contains the key and msgflg
174 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700175 * Called with msg_ids.rw_mutex held (writer)
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700176 */
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700177static int newque(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 struct msg_queue *msq;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700180 int id, retval;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700181 key_t key = params->key;
182 int msgflg = params->flg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Ingo Molnar5a06a362006-07-30 03:04:11 -0700184 msq = ipc_rcu_alloc(sizeof(*msq));
185 if (!msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return -ENOMEM;
187
Ingo Molnar5a06a362006-07-30 03:04:11 -0700188 msq->q_perm.mode = msgflg & S_IRWXUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 msq->q_perm.key = key;
190
191 msq->q_perm.security = NULL;
192 retval = security_msg_queue_alloc(msq);
193 if (retval) {
194 ipc_rcu_putref(msq);
195 return retval;
196 }
197
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700198 /*
199 * ipc_addid() locks msq
200 */
Kirill Korotaev1e786932006-10-02 02:18:21 -0700201 id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700202 if (id < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 security_msg_queue_free(msq);
204 ipc_rcu_putref(msq);
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700205 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207
Nadia Derbey1b531f22007-10-18 23:40:55 -0700208 msq->q_perm.id = msg_buildid(id, msq->q_perm.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 msq->q_stime = msq->q_rtime = 0;
210 msq->q_ctime = get_seconds();
211 msq->q_cbytes = msq->q_qnum = 0;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700212 msq->q_qbytes = ns->msg_ctlmnb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 msq->q_lspid = msq->q_lrpid = 0;
214 INIT_LIST_HEAD(&msq->q_messages);
215 INIT_LIST_HEAD(&msq->q_receivers);
216 INIT_LIST_HEAD(&msq->q_senders);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 msg_unlock(msq);
219
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700220 return msq->q_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
Ingo Molnar5a06a362006-07-30 03:04:11 -0700223static inline void ss_add(struct msg_queue *msq, struct msg_sender *mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700225 mss->tsk = current;
226 current->state = TASK_INTERRUPTIBLE;
227 list_add_tail(&mss->list, &msq->q_senders);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
Ingo Molnar5a06a362006-07-30 03:04:11 -0700230static inline void ss_del(struct msg_sender *mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700232 if (mss->list.next != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 list_del(&mss->list);
234}
235
Ingo Molnar5a06a362006-07-30 03:04:11 -0700236static void ss_wakeup(struct list_head *h, int kill)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 struct list_head *tmp;
239
240 tmp = h->next;
241 while (tmp != h) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700242 struct msg_sender *mss;
243
244 mss = list_entry(tmp, struct msg_sender, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 tmp = tmp->next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700246 if (kill)
247 mss->list.next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 wake_up_process(mss->tsk);
249 }
250}
251
Ingo Molnar5a06a362006-07-30 03:04:11 -0700252static void expunge_all(struct msg_queue *msq, int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 struct list_head *tmp;
255
256 tmp = msq->q_receivers.next;
257 while (tmp != &msq->q_receivers) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700258 struct msg_receiver *msr;
259
260 msr = list_entry(tmp, struct msg_receiver, r_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 tmp = tmp->next;
262 msr->r_msg = NULL;
263 wake_up_process(msr->r_tsk);
264 smp_mb();
265 msr->r_msg = ERR_PTR(res);
266 }
267}
Ingo Molnar5a06a362006-07-30 03:04:11 -0700268
269/*
270 * freeque() wakes up waiters on the sender and receiver waiting queue,
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700271 * removes the message queue from message queue ID IDR, and cleans up all the
272 * messages associated with this queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700274 * msg_ids.rw_mutex (writer) and the spinlock for this message queue are held
275 * before freeque() is called. msg_ids.rw_mutex remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 */
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700277static void freeque(struct ipc_namespace *ns, struct msg_queue *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 struct list_head *tmp;
280
Ingo Molnar5a06a362006-07-30 03:04:11 -0700281 expunge_all(msq, -EIDRM);
282 ss_wakeup(&msq->q_senders, 1);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700283 msg_rmid(ns, msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 msg_unlock(msq);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 tmp = msq->q_messages.next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700287 while (tmp != &msq->q_messages) {
288 struct msg_msg *msg = list_entry(tmp, struct msg_msg, m_list);
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 tmp = tmp->next;
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700291 atomic_dec(&ns->msg_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 free_msg(msg);
293 }
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700294 atomic_sub(msq->q_cbytes, &ns->msg_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 security_msg_queue_free(msq);
296 ipc_rcu_putref(msq);
297}
298
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700299/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700300 * Called with msg_ids.rw_mutex and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700301 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700302static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700303{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700304 struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
305
306 return security_msg_queue_associate(msq, msgflg);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700307}
308
Ingo Molnar5a06a362006-07-30 03:04:11 -0700309asmlinkage long sys_msgget(key_t key, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Kirill Korotaev1e786932006-10-02 02:18:21 -0700311 struct ipc_namespace *ns;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700312 struct ipc_ops msg_ops;
313 struct ipc_params msg_params;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700314
315 ns = current->nsproxy->ipc_ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700316
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700317 msg_ops.getnew = newque;
318 msg_ops.associate = msg_security;
319 msg_ops.more_checks = NULL;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700320
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700321 msg_params.key = key;
322 msg_params.flg = msgflg;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700323
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700324 return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
Ingo Molnar5a06a362006-07-30 03:04:11 -0700327static inline unsigned long
328copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 switch(version) {
331 case IPC_64:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700332 return copy_to_user(buf, in, sizeof(*in));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 case IPC_OLD:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700334 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 struct msqid_ds out;
336
Ingo Molnar5a06a362006-07-30 03:04:11 -0700337 memset(&out, 0, sizeof(out));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
340
341 out.msg_stime = in->msg_stime;
342 out.msg_rtime = in->msg_rtime;
343 out.msg_ctime = in->msg_ctime;
344
Ingo Molnar5a06a362006-07-30 03:04:11 -0700345 if (in->msg_cbytes > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 out.msg_cbytes = USHRT_MAX;
347 else
348 out.msg_cbytes = in->msg_cbytes;
349 out.msg_lcbytes = in->msg_cbytes;
350
Ingo Molnar5a06a362006-07-30 03:04:11 -0700351 if (in->msg_qnum > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 out.msg_qnum = USHRT_MAX;
353 else
354 out.msg_qnum = in->msg_qnum;
355
Ingo Molnar5a06a362006-07-30 03:04:11 -0700356 if (in->msg_qbytes > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 out.msg_qbytes = USHRT_MAX;
358 else
359 out.msg_qbytes = in->msg_qbytes;
360 out.msg_lqbytes = in->msg_qbytes;
361
362 out.msg_lspid = in->msg_lspid;
363 out.msg_lrpid = in->msg_lrpid;
364
Ingo Molnar5a06a362006-07-30 03:04:11 -0700365 return copy_to_user(buf, &out, sizeof(out));
366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 default:
368 return -EINVAL;
369 }
370}
371
372struct msq_setbuf {
373 unsigned long qbytes;
374 uid_t uid;
375 gid_t gid;
376 mode_t mode;
377};
378
Ingo Molnar5a06a362006-07-30 03:04:11 -0700379static inline unsigned long
380copy_msqid_from_user(struct msq_setbuf *out, void __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 switch(version) {
383 case IPC_64:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700384 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 struct msqid64_ds tbuf;
386
Ingo Molnar5a06a362006-07-30 03:04:11 -0700387 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return -EFAULT;
389
390 out->qbytes = tbuf.msg_qbytes;
391 out->uid = tbuf.msg_perm.uid;
392 out->gid = tbuf.msg_perm.gid;
393 out->mode = tbuf.msg_perm.mode;
394
395 return 0;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 case IPC_OLD:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700398 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 struct msqid_ds tbuf_old;
400
Ingo Molnar5a06a362006-07-30 03:04:11 -0700401 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return -EFAULT;
403
404 out->uid = tbuf_old.msg_perm.uid;
405 out->gid = tbuf_old.msg_perm.gid;
406 out->mode = tbuf_old.msg_perm.mode;
407
Ingo Molnar5a06a362006-07-30 03:04:11 -0700408 if (tbuf_old.msg_qbytes == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 out->qbytes = tbuf_old.msg_lqbytes;
410 else
411 out->qbytes = tbuf_old.msg_qbytes;
412
413 return 0;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 default:
416 return -EINVAL;
417 }
418}
419
Ingo Molnar5a06a362006-07-30 03:04:11 -0700420asmlinkage long sys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 struct kern_ipc_perm *ipcp;
Jeff Garzik8e1c0912007-07-17 05:40:59 -0400423 struct msq_setbuf uninitialized_var(setbuf);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700424 struct msg_queue *msq;
425 int err, version;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700426 struct ipc_namespace *ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (msqid < 0 || cmd < 0)
429 return -EINVAL;
430
431 version = ipc_parse_version(&cmd);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700432 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 switch (cmd) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700435 case IPC_INFO:
436 case MSG_INFO:
437 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 struct msginfo msginfo;
439 int max_id;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (!buf)
442 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700443 /*
444 * We must not return kernel stack data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 * due to padding, it's not enough
446 * to set all member fields.
447 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 err = security_msg_queue_msgctl(NULL, cmd);
449 if (err)
450 return err;
451
Ingo Molnar5a06a362006-07-30 03:04:11 -0700452 memset(&msginfo, 0, sizeof(msginfo));
Kirill Korotaev1e786932006-10-02 02:18:21 -0700453 msginfo.msgmni = ns->msg_ctlmni;
454 msginfo.msgmax = ns->msg_ctlmax;
455 msginfo.msgmnb = ns->msg_ctlmnb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 msginfo.msgssz = MSGSSZ;
457 msginfo.msgseg = MSGSEG;
Nadia Derbey3e148c72007-10-18 23:40:54 -0700458 down_read(&msg_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (cmd == MSG_INFO) {
Kirill Korotaev1e786932006-10-02 02:18:21 -0700460 msginfo.msgpool = msg_ids(ns).in_use;
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700461 msginfo.msgmap = atomic_read(&ns->msg_hdrs);
462 msginfo.msgtql = atomic_read(&ns->msg_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 } else {
464 msginfo.msgmap = MSGMAP;
465 msginfo.msgpool = MSGPOOL;
466 msginfo.msgtql = MSGTQL;
467 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700468 max_id = ipc_get_maxid(&msg_ids(ns));
Nadia Derbey3e148c72007-10-18 23:40:54 -0700469 up_read(&msg_ids(ns).rw_mutex);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700470 if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700472 return (max_id < 0) ? 0 : max_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700474 case MSG_STAT: /* msqid is an index rather than a msg queue id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 case IPC_STAT:
476 {
477 struct msqid64_ds tbuf;
478 int success_return;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (!buf)
481 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Ingo Molnar5a06a362006-07-30 03:04:11 -0700483 if (cmd == MSG_STAT) {
Nadia Derbey023a5352007-10-18 23:40:51 -0700484 msq = msg_lock(ns, msqid);
485 if (IS_ERR(msq))
486 return PTR_ERR(msq);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700487 success_return = msq->q_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 } else {
Nadia Derbey023a5352007-10-18 23:40:51 -0700489 msq = msg_lock_check(ns, msqid);
490 if (IS_ERR(msq))
491 return PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 success_return = 0;
493 }
494 err = -EACCES;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700495 if (ipcperms(&msq->q_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 goto out_unlock;
497
498 err = security_msg_queue_msgctl(msq, cmd);
499 if (err)
500 goto out_unlock;
501
Nadia Derbey023a5352007-10-18 23:40:51 -0700502 memset(&tbuf, 0, sizeof(tbuf));
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
505 tbuf.msg_stime = msq->q_stime;
506 tbuf.msg_rtime = msq->q_rtime;
507 tbuf.msg_ctime = msq->q_ctime;
508 tbuf.msg_cbytes = msq->q_cbytes;
509 tbuf.msg_qnum = msq->q_qnum;
510 tbuf.msg_qbytes = msq->q_qbytes;
511 tbuf.msg_lspid = msq->q_lspid;
512 tbuf.msg_lrpid = msq->q_lrpid;
513 msg_unlock(msq);
514 if (copy_msqid_to_user(buf, &tbuf, version))
515 return -EFAULT;
516 return success_return;
517 }
518 case IPC_SET:
519 if (!buf)
520 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700521 if (copy_msqid_from_user(&setbuf, buf, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 break;
524 case IPC_RMID:
525 break;
526 default:
527 return -EINVAL;
528 }
529
Nadia Derbey3e148c72007-10-18 23:40:54 -0700530 down_write(&msg_ids(ns).rw_mutex);
531 msq = msg_lock_check_down(ns, msqid);
Nadia Derbey023a5352007-10-18 23:40:51 -0700532 if (IS_ERR(msq)) {
533 err = PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 goto out_up;
Nadia Derbey023a5352007-10-18 23:40:51 -0700535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 ipcp = &msq->q_perm;
Steve Grubb073115d2006-04-02 17:07:33 -0400538
539 err = audit_ipc_obj(ipcp);
540 if (err)
541 goto out_unlock_up;
Jeff Garzik8e1c0912007-07-17 05:40:59 -0400542 if (cmd == IPC_SET) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700543 err = audit_ipc_set_perm(setbuf.qbytes, setbuf.uid, setbuf.gid,
544 setbuf.mode);
Linda Knippersac032212006-05-16 22:03:48 -0400545 if (err)
546 goto out_unlock_up;
547 }
Steve Grubb073115d2006-04-02 17:07:33 -0400548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 err = -EPERM;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700550 if (current->euid != ipcp->cuid &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700552 /* We _could_ check for CAP_CHOWN above, but we don't */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 goto out_unlock_up;
554
555 err = security_msg_queue_msgctl(msq, cmd);
556 if (err)
557 goto out_unlock_up;
558
559 switch (cmd) {
560 case IPC_SET:
561 {
562 err = -EPERM;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700563 if (setbuf.qbytes > ns->msg_ctlmnb && !capable(CAP_SYS_RESOURCE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 goto out_unlock_up;
565
566 msq->q_qbytes = setbuf.qbytes;
567
568 ipcp->uid = setbuf.uid;
569 ipcp->gid = setbuf.gid;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700570 ipcp->mode = (ipcp->mode & ~S_IRWXUGO) |
571 (S_IRWXUGO & setbuf.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 msq->q_ctime = get_seconds();
573 /* sleeping receivers might be excluded by
574 * stricter permissions.
575 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700576 expunge_all(msq, -EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 /* sleeping senders might be able to send
578 * due to a larger queue size.
579 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700580 ss_wakeup(&msq->q_senders, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 msg_unlock(msq);
582 break;
583 }
584 case IPC_RMID:
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700585 freeque(ns, msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 break;
587 }
588 err = 0;
589out_up:
Nadia Derbey3e148c72007-10-18 23:40:54 -0700590 up_write(&msg_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return err;
592out_unlock_up:
593 msg_unlock(msq);
594 goto out_up;
595out_unlock:
596 msg_unlock(msq);
597 return err;
598}
599
Ingo Molnar5a06a362006-07-30 03:04:11 -0700600static int testmsg(struct msg_msg *msg, long type, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
602 switch(mode)
603 {
604 case SEARCH_ANY:
605 return 1;
606 case SEARCH_LESSEQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700607 if (msg->m_type <=type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return 1;
609 break;
610 case SEARCH_EQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700611 if (msg->m_type == type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return 1;
613 break;
614 case SEARCH_NOTEQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700615 if (msg->m_type != type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return 1;
617 break;
618 }
619 return 0;
620}
621
Ingo Molnar5a06a362006-07-30 03:04:11 -0700622static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700624 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 tmp = msq->q_receivers.next;
627 while (tmp != &msq->q_receivers) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700628 struct msg_receiver *msr;
629
630 msr = list_entry(tmp, struct msg_receiver, r_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 tmp = tmp->next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700632 if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
633 !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
634 msr->r_msgtype, msr->r_mode)) {
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 list_del(&msr->r_list);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700637 if (msr->r_maxsize < msg->m_ts) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 msr->r_msg = NULL;
639 wake_up_process(msr->r_tsk);
640 smp_mb();
641 msr->r_msg = ERR_PTR(-E2BIG);
642 } else {
643 msr->r_msg = NULL;
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700644 msq->q_lrpid = task_pid_vnr(msr->r_tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 msq->q_rtime = get_seconds();
646 wake_up_process(msr->r_tsk);
647 smp_mb();
648 msr->r_msg = msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return 1;
651 }
652 }
653 }
654 return 0;
655}
656
suzuki651971c2006-12-06 20:37:48 -0800657long do_msgsnd(int msqid, long mtype, void __user *mtext,
658 size_t msgsz, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 struct msg_queue *msq;
661 struct msg_msg *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 int err;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700663 struct ipc_namespace *ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700664
Kirill Korotaev1e786932006-10-02 02:18:21 -0700665 ns = current->nsproxy->ipc_ns;
666
667 if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (mtype < 1)
670 return -EINVAL;
671
suzuki651971c2006-12-06 20:37:48 -0800672 msg = load_msg(mtext, msgsz);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700673 if (IS_ERR(msg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return PTR_ERR(msg);
675
676 msg->m_type = mtype;
677 msg->m_ts = msgsz;
678
Nadia Derbey023a5352007-10-18 23:40:51 -0700679 msq = msg_lock_check(ns, msqid);
680 if (IS_ERR(msq)) {
681 err = PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 goto out_free;
Nadia Derbey023a5352007-10-18 23:40:51 -0700683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 for (;;) {
686 struct msg_sender s;
687
Ingo Molnar5a06a362006-07-30 03:04:11 -0700688 err = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (ipcperms(&msq->q_perm, S_IWUGO))
690 goto out_unlock_free;
691
692 err = security_msg_queue_msgsnd(msq, msg, msgflg);
693 if (err)
694 goto out_unlock_free;
695
Ingo Molnar5a06a362006-07-30 03:04:11 -0700696 if (msgsz + msq->q_cbytes <= msq->q_qbytes &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 1 + msq->q_qnum <= msq->q_qbytes) {
698 break;
699 }
700
701 /* queue full, wait: */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700702 if (msgflg & IPC_NOWAIT) {
703 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 goto out_unlock_free;
705 }
706 ss_add(msq, &s);
707 ipc_rcu_getref(msq);
708 msg_unlock(msq);
709 schedule();
710
711 ipc_lock_by_ptr(&msq->q_perm);
712 ipc_rcu_putref(msq);
713 if (msq->q_perm.deleted) {
714 err = -EIDRM;
715 goto out_unlock_free;
716 }
717 ss_del(&s);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (signal_pending(current)) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700720 err = -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 goto out_unlock_free;
722 }
723 }
724
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700725 msq->q_lspid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 msq->q_stime = get_seconds();
727
Ingo Molnar5a06a362006-07-30 03:04:11 -0700728 if (!pipelined_send(msq, msg)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 /* noone is waiting for this message, enqueue it */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700730 list_add_tail(&msg->m_list, &msq->q_messages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 msq->q_cbytes += msgsz;
732 msq->q_qnum++;
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700733 atomic_add(msgsz, &ns->msg_bytes);
734 atomic_inc(&ns->msg_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 err = 0;
738 msg = NULL;
739
740out_unlock_free:
741 msg_unlock(msq);
742out_free:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700743 if (msg != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 free_msg(msg);
745 return err;
746}
747
suzuki651971c2006-12-06 20:37:48 -0800748asmlinkage long
749sys_msgsnd(int msqid, struct msgbuf __user *msgp, size_t msgsz, int msgflg)
750{
751 long mtype;
752
753 if (get_user(mtype, &msgp->mtype))
754 return -EFAULT;
755 return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
756}
757
Ingo Molnar5a06a362006-07-30 03:04:11 -0700758static inline int convert_mode(long *msgtyp, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700760 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 * find message of correct type.
762 * msgtyp = 0 => get first.
763 * msgtyp > 0 => get first message of matching type.
Ingo Molnar5a06a362006-07-30 03:04:11 -0700764 * msgtyp < 0 => get message with least type must be < abs(msgtype).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700766 if (*msgtyp == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return SEARCH_ANY;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700768 if (*msgtyp < 0) {
769 *msgtyp = -*msgtyp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return SEARCH_LESSEQUAL;
771 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700772 if (msgflg & MSG_EXCEPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return SEARCH_NOTEQUAL;
774 return SEARCH_EQUAL;
775}
776
suzuki651971c2006-12-06 20:37:48 -0800777long do_msgrcv(int msqid, long *pmtype, void __user *mtext,
778 size_t msgsz, long msgtyp, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 struct msg_queue *msq;
781 struct msg_msg *msg;
782 int mode;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700783 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 if (msqid < 0 || (long) msgsz < 0)
786 return -EINVAL;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700787 mode = convert_mode(&msgtyp, msgflg);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700788 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Nadia Derbey023a5352007-10-18 23:40:51 -0700790 msq = msg_lock_check(ns, msqid);
791 if (IS_ERR(msq))
792 return PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 for (;;) {
795 struct msg_receiver msr_d;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700796 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 msg = ERR_PTR(-EACCES);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700799 if (ipcperms(&msq->q_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 goto out_unlock;
801
802 msg = ERR_PTR(-EAGAIN);
803 tmp = msq->q_messages.next;
804 while (tmp != &msq->q_messages) {
805 struct msg_msg *walk_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700806
807 walk_msg = list_entry(tmp, struct msg_msg, m_list);
808 if (testmsg(walk_msg, msgtyp, mode) &&
809 !security_msg_queue_msgrcv(msq, walk_msg, current,
810 msgtyp, mode)) {
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 msg = walk_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700813 if (mode == SEARCH_LESSEQUAL &&
814 walk_msg->m_type != 1) {
815 msg = walk_msg;
816 msgtyp = walk_msg->m_type - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 } else {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700818 msg = walk_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 break;
820 }
821 }
822 tmp = tmp->next;
823 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700824 if (!IS_ERR(msg)) {
825 /*
826 * Found a suitable message.
827 * Unlink it from the queue.
828 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
830 msg = ERR_PTR(-E2BIG);
831 goto out_unlock;
832 }
833 list_del(&msg->m_list);
834 msq->q_qnum--;
835 msq->q_rtime = get_seconds();
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700836 msq->q_lrpid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 msq->q_cbytes -= msg->m_ts;
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700838 atomic_sub(msg->m_ts, &ns->msg_bytes);
839 atomic_dec(&ns->msg_hdrs);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700840 ss_wakeup(&msq->q_senders, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 msg_unlock(msq);
842 break;
843 }
844 /* No message waiting. Wait for a message */
845 if (msgflg & IPC_NOWAIT) {
846 msg = ERR_PTR(-ENOMSG);
847 goto out_unlock;
848 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700849 list_add_tail(&msr_d.r_list, &msq->q_receivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 msr_d.r_tsk = current;
851 msr_d.r_msgtype = msgtyp;
852 msr_d.r_mode = mode;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700853 if (msgflg & MSG_NOERROR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 msr_d.r_maxsize = INT_MAX;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700855 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 msr_d.r_maxsize = msgsz;
857 msr_d.r_msg = ERR_PTR(-EAGAIN);
858 current->state = TASK_INTERRUPTIBLE;
859 msg_unlock(msq);
860
861 schedule();
862
863 /* Lockless receive, part 1:
864 * Disable preemption. We don't hold a reference to the queue
865 * and getting a reference would defeat the idea of a lockless
866 * operation, thus the code relies on rcu to guarantee the
867 * existance of msq:
868 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
869 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
870 * rcu_read_lock() prevents preemption between reading r_msg
871 * and the spin_lock() inside ipc_lock_by_ptr().
872 */
873 rcu_read_lock();
874
875 /* Lockless receive, part 2:
876 * Wait until pipelined_send or expunge_all are outside of
877 * wake_up_process(). There is a race with exit(), see
878 * ipc/mqueue.c for the details.
879 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700880 msg = (struct msg_msg*)msr_d.r_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 while (msg == NULL) {
882 cpu_relax();
Ingo Molnar5a06a362006-07-30 03:04:11 -0700883 msg = (struct msg_msg *)msr_d.r_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
885
886 /* Lockless receive, part 3:
887 * If there is a message or an error then accept it without
888 * locking.
889 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700890 if (msg != ERR_PTR(-EAGAIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 rcu_read_unlock();
892 break;
893 }
894
895 /* Lockless receive, part 3:
896 * Acquire the queue spinlock.
897 */
898 ipc_lock_by_ptr(&msq->q_perm);
899 rcu_read_unlock();
900
901 /* Lockless receive, part 4:
902 * Repeat test after acquiring the spinlock.
903 */
904 msg = (struct msg_msg*)msr_d.r_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700905 if (msg != ERR_PTR(-EAGAIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 goto out_unlock;
907
908 list_del(&msr_d.r_list);
909 if (signal_pending(current)) {
910 msg = ERR_PTR(-ERESTARTNOHAND);
911out_unlock:
912 msg_unlock(msq);
913 break;
914 }
915 }
916 if (IS_ERR(msg))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700917 return PTR_ERR(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
suzuki651971c2006-12-06 20:37:48 -0800920 *pmtype = msg->m_type;
921 if (store_msg(mtext, msg, msgsz))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700922 msgsz = -EFAULT;
suzuki651971c2006-12-06 20:37:48 -0800923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 free_msg(msg);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return msgsz;
927}
928
suzuki651971c2006-12-06 20:37:48 -0800929asmlinkage long sys_msgrcv(int msqid, struct msgbuf __user *msgp, size_t msgsz,
930 long msgtyp, int msgflg)
931{
932 long err, mtype;
933
934 err = do_msgrcv(msqid, &mtype, msgp->mtext, msgsz, msgtyp, msgflg);
935 if (err < 0)
936 goto out;
937
938 if (put_user(mtype, &msgp->mtype))
939 err = -EFAULT;
940out:
941 return err;
942}
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -0700945static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Mike Waychison19b49462005-09-06 15:17:10 -0700947 struct msg_queue *msq = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Mike Waychison19b49462005-09-06 15:17:10 -0700949 return seq_printf(s,
Ingo Molnar5a06a362006-07-30 03:04:11 -0700950 "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
951 msq->q_perm.key,
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700952 msq->q_perm.id,
Ingo Molnar5a06a362006-07-30 03:04:11 -0700953 msq->q_perm.mode,
954 msq->q_cbytes,
955 msq->q_qnum,
956 msq->q_lspid,
957 msq->q_lrpid,
958 msq->q_perm.uid,
959 msq->q_perm.gid,
960 msq->q_perm.cuid,
961 msq->q_perm.cgid,
962 msq->q_stime,
963 msq->q_rtime,
964 msq->q_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
966#endif