blob: ec0c724054b95ce733eee1510d044df7d95bea20 [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>
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
Kirill Korotaev1e786932006-10-02 02:18:21 -070069static struct ipc_ids init_msg_ids;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Kirill Korotaev1e786932006-10-02 02:18:21 -070071#define msg_ids(ns) (*((ns)->ids[IPC_MSG_IDS]))
72
Ingo Molnar5a06a362006-07-30 03:04:11 -070073#define msg_unlock(msq) ipc_unlock(&(msq)->q_perm)
Nadia Derbey1b531f22007-10-18 23:40:55 -070074#define msg_buildid(id, seq) ipc_buildid(id, seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Nadia Derbey7ca7e562007-10-18 23:40:48 -070076static void freeque(struct ipc_namespace *, struct msg_queue *);
Nadia Derbey7748dbf2007-10-18 23:40:49 -070077static int newque(struct ipc_namespace *, struct ipc_params *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -070079static int sysvipc_msg_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#endif
81
Cedric Le Goater7d69a1f2007-07-15 23:40:58 -070082static void __msg_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Kirill Korotaev1e786932006-10-02 02:18:21 -070084 ns->ids[IPC_MSG_IDS] = ids;
85 ns->msg_ctlmax = MSGMAX;
86 ns->msg_ctlmnb = MSGMNB;
87 ns->msg_ctlmni = MSGMNI;
Kirill Korotaev3ac88a42007-10-18 23:40:56 -070088 atomic_set(&ns->msg_bytes, 0);
89 atomic_set(&ns->msg_hdrs, 0);
Nadia Derbey7ca7e562007-10-18 23:40:48 -070090 ipc_init_ids(ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
Kirill Korotaev1e786932006-10-02 02:18:21 -070093int msg_init_ns(struct ipc_namespace *ns)
94{
95 struct ipc_ids *ids;
96
97 ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
98 if (ids == NULL)
99 return -ENOMEM;
100
101 __msg_init_ns(ns, ids);
102 return 0;
103}
104
105void msg_exit_ns(struct ipc_namespace *ns)
106{
Kirill Korotaev1e786932006-10-02 02:18:21 -0700107 struct msg_queue *msq;
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800108 struct kern_ipc_perm *perm;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700109 int next_id;
110 int total, in_use;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700111
Nadia Derbey3e148c72007-10-18 23:40:54 -0700112 down_write(&msg_ids(ns).rw_mutex);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700113
114 in_use = msg_ids(ns).in_use;
115
116 for (total = 0, next_id = 0; total < in_use; next_id++) {
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800117 perm = idr_find(&msg_ids(ns).ipcs_idr, next_id);
118 if (perm == NULL)
Kirill Korotaev1e786932006-10-02 02:18:21 -0700119 continue;
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800120 ipc_lock_by_ptr(perm);
121 msq = container_of(perm, struct msg_queue, q_perm);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700122 freeque(ns, msq);
123 total++;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700124 }
Nadia Derbey3e148c72007-10-18 23:40:54 -0700125
126 up_write(&msg_ids(ns).rw_mutex);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700127
128 kfree(ns->ids[IPC_MSG_IDS]);
129 ns->ids[IPC_MSG_IDS] = NULL;
130}
Kirill Korotaev1e786932006-10-02 02:18:21 -0700131
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
Nadia Derbey3e148c72007-10-18 23:40:54 -0700140/*
141 * This routine is called in the paths where the rw_mutex is held to protect
142 * access to the idr tree.
143 */
144static inline struct msg_queue *msg_lock_check_down(struct ipc_namespace *ns,
145 int id)
146{
147 struct kern_ipc_perm *ipcp = ipc_lock_check_down(&msg_ids(ns), id);
148
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800149 if (IS_ERR(ipcp))
150 return (struct msg_queue *)ipcp;
151
Nadia Derbey3e148c72007-10-18 23:40:54 -0700152 return container_of(ipcp, struct msg_queue, q_perm);
153}
154
155/*
156 * msg_lock_(check_) routines are called in the paths where the rw_mutex
157 * is not held.
158 */
Nadia Derbey023a5352007-10-18 23:40:51 -0700159static inline struct msg_queue *msg_lock(struct ipc_namespace *ns, int id)
160{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700161 struct kern_ipc_perm *ipcp = ipc_lock(&msg_ids(ns), id);
162
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800163 if (IS_ERR(ipcp))
164 return (struct msg_queue *)ipcp;
165
Nadia Derbey03f02c72007-10-18 23:40:51 -0700166 return container_of(ipcp, struct msg_queue, q_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700167}
168
169static inline struct msg_queue *msg_lock_check(struct ipc_namespace *ns,
170 int id)
171{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700172 struct kern_ipc_perm *ipcp = ipc_lock_check(&msg_ids(ns), id);
173
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800174 if (IS_ERR(ipcp))
175 return (struct msg_queue *)ipcp;
176
Nadia Derbey03f02c72007-10-18 23:40:51 -0700177 return container_of(ipcp, struct msg_queue, q_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700178}
179
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700180static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s)
181{
182 ipc_rmid(&msg_ids(ns), &s->q_perm);
183}
184
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700185/**
186 * newque - Create a new msg queue
187 * @ns: namespace
188 * @params: ptr to the structure that contains the key and msgflg
189 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700190 * Called with msg_ids.rw_mutex held (writer)
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700191 */
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700192static int newque(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 struct msg_queue *msq;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700195 int id, retval;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700196 key_t key = params->key;
197 int msgflg = params->flg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Ingo Molnar5a06a362006-07-30 03:04:11 -0700199 msq = ipc_rcu_alloc(sizeof(*msq));
200 if (!msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return -ENOMEM;
202
Ingo Molnar5a06a362006-07-30 03:04:11 -0700203 msq->q_perm.mode = msgflg & S_IRWXUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 msq->q_perm.key = key;
205
206 msq->q_perm.security = NULL;
207 retval = security_msg_queue_alloc(msq);
208 if (retval) {
209 ipc_rcu_putref(msq);
210 return retval;
211 }
212
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700213 /*
214 * ipc_addid() locks msq
215 */
Kirill Korotaev1e786932006-10-02 02:18:21 -0700216 id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700217 if (id < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 security_msg_queue_free(msq);
219 ipc_rcu_putref(msq);
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700220 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
222
Nadia Derbey1b531f22007-10-18 23:40:55 -0700223 msq->q_perm.id = msg_buildid(id, msq->q_perm.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 msq->q_stime = msq->q_rtime = 0;
225 msq->q_ctime = get_seconds();
226 msq->q_cbytes = msq->q_qnum = 0;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700227 msq->q_qbytes = ns->msg_ctlmnb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 msq->q_lspid = msq->q_lrpid = 0;
229 INIT_LIST_HEAD(&msq->q_messages);
230 INIT_LIST_HEAD(&msq->q_receivers);
231 INIT_LIST_HEAD(&msq->q_senders);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 msg_unlock(msq);
234
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700235 return msq->q_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
Ingo Molnar5a06a362006-07-30 03:04:11 -0700238static inline void ss_add(struct msg_queue *msq, struct msg_sender *mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700240 mss->tsk = current;
241 current->state = TASK_INTERRUPTIBLE;
242 list_add_tail(&mss->list, &msq->q_senders);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
Ingo Molnar5a06a362006-07-30 03:04:11 -0700245static inline void ss_del(struct msg_sender *mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700247 if (mss->list.next != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 list_del(&mss->list);
249}
250
Ingo Molnar5a06a362006-07-30 03:04:11 -0700251static void ss_wakeup(struct list_head *h, int kill)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 struct list_head *tmp;
254
255 tmp = h->next;
256 while (tmp != h) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700257 struct msg_sender *mss;
258
259 mss = list_entry(tmp, struct msg_sender, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 tmp = tmp->next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700261 if (kill)
262 mss->list.next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 wake_up_process(mss->tsk);
264 }
265}
266
Ingo Molnar5a06a362006-07-30 03:04:11 -0700267static void expunge_all(struct msg_queue *msq, int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 struct list_head *tmp;
270
271 tmp = msq->q_receivers.next;
272 while (tmp != &msq->q_receivers) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700273 struct msg_receiver *msr;
274
275 msr = list_entry(tmp, struct msg_receiver, r_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 tmp = tmp->next;
277 msr->r_msg = NULL;
278 wake_up_process(msr->r_tsk);
279 smp_mb();
280 msr->r_msg = ERR_PTR(res);
281 }
282}
Ingo Molnar5a06a362006-07-30 03:04:11 -0700283
284/*
285 * freeque() wakes up waiters on the sender and receiver waiting queue,
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700286 * removes the message queue from message queue ID IDR, and cleans up all the
287 * messages associated with this queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700289 * msg_ids.rw_mutex (writer) and the spinlock for this message queue are held
290 * before freeque() is called. msg_ids.rw_mutex remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 */
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700292static void freeque(struct ipc_namespace *ns, struct msg_queue *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 struct list_head *tmp;
295
Ingo Molnar5a06a362006-07-30 03:04:11 -0700296 expunge_all(msq, -EIDRM);
297 ss_wakeup(&msq->q_senders, 1);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700298 msg_rmid(ns, msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 msg_unlock(msq);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 tmp = msq->q_messages.next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700302 while (tmp != &msq->q_messages) {
303 struct msg_msg *msg = list_entry(tmp, struct msg_msg, m_list);
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 tmp = tmp->next;
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700306 atomic_dec(&ns->msg_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 free_msg(msg);
308 }
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700309 atomic_sub(msq->q_cbytes, &ns->msg_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 security_msg_queue_free(msq);
311 ipc_rcu_putref(msq);
312}
313
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700314/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700315 * Called with msg_ids.rw_mutex and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700316 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700317static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700318{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700319 struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
320
321 return security_msg_queue_associate(msq, msgflg);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700322}
323
Ingo Molnar5a06a362006-07-30 03:04:11 -0700324asmlinkage long sys_msgget(key_t key, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Kirill Korotaev1e786932006-10-02 02:18:21 -0700326 struct ipc_namespace *ns;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700327 struct ipc_ops msg_ops;
328 struct ipc_params msg_params;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700329
330 ns = current->nsproxy->ipc_ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700331
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700332 msg_ops.getnew = newque;
333 msg_ops.associate = msg_security;
334 msg_ops.more_checks = NULL;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700335
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700336 msg_params.key = key;
337 msg_params.flg = msgflg;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700338
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700339 return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Ingo Molnar5a06a362006-07-30 03:04:11 -0700342static inline unsigned long
343copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 switch(version) {
346 case IPC_64:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700347 return copy_to_user(buf, in, sizeof(*in));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 case IPC_OLD:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700349 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 struct msqid_ds out;
351
Ingo Molnar5a06a362006-07-30 03:04:11 -0700352 memset(&out, 0, sizeof(out));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
355
356 out.msg_stime = in->msg_stime;
357 out.msg_rtime = in->msg_rtime;
358 out.msg_ctime = in->msg_ctime;
359
Ingo Molnar5a06a362006-07-30 03:04:11 -0700360 if (in->msg_cbytes > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 out.msg_cbytes = USHRT_MAX;
362 else
363 out.msg_cbytes = in->msg_cbytes;
364 out.msg_lcbytes = in->msg_cbytes;
365
Ingo Molnar5a06a362006-07-30 03:04:11 -0700366 if (in->msg_qnum > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 out.msg_qnum = USHRT_MAX;
368 else
369 out.msg_qnum = in->msg_qnum;
370
Ingo Molnar5a06a362006-07-30 03:04:11 -0700371 if (in->msg_qbytes > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 out.msg_qbytes = USHRT_MAX;
373 else
374 out.msg_qbytes = in->msg_qbytes;
375 out.msg_lqbytes = in->msg_qbytes;
376
377 out.msg_lspid = in->msg_lspid;
378 out.msg_lrpid = in->msg_lrpid;
379
Ingo Molnar5a06a362006-07-30 03:04:11 -0700380 return copy_to_user(buf, &out, sizeof(out));
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 default:
383 return -EINVAL;
384 }
385}
386
387struct msq_setbuf {
388 unsigned long qbytes;
389 uid_t uid;
390 gid_t gid;
391 mode_t mode;
392};
393
Ingo Molnar5a06a362006-07-30 03:04:11 -0700394static inline unsigned long
395copy_msqid_from_user(struct msq_setbuf *out, void __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 switch(version) {
398 case IPC_64:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700399 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 struct msqid64_ds tbuf;
401
Ingo Molnar5a06a362006-07-30 03:04:11 -0700402 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return -EFAULT;
404
405 out->qbytes = tbuf.msg_qbytes;
406 out->uid = tbuf.msg_perm.uid;
407 out->gid = tbuf.msg_perm.gid;
408 out->mode = tbuf.msg_perm.mode;
409
410 return 0;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 case IPC_OLD:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700413 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 struct msqid_ds tbuf_old;
415
Ingo Molnar5a06a362006-07-30 03:04:11 -0700416 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return -EFAULT;
418
419 out->uid = tbuf_old.msg_perm.uid;
420 out->gid = tbuf_old.msg_perm.gid;
421 out->mode = tbuf_old.msg_perm.mode;
422
Ingo Molnar5a06a362006-07-30 03:04:11 -0700423 if (tbuf_old.msg_qbytes == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 out->qbytes = tbuf_old.msg_lqbytes;
425 else
426 out->qbytes = tbuf_old.msg_qbytes;
427
428 return 0;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 default:
431 return -EINVAL;
432 }
433}
434
Ingo Molnar5a06a362006-07-30 03:04:11 -0700435asmlinkage long sys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 struct kern_ipc_perm *ipcp;
Jeff Garzik8e1c0912007-07-17 05:40:59 -0400438 struct msq_setbuf uninitialized_var(setbuf);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700439 struct msg_queue *msq;
440 int err, version;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700441 struct ipc_namespace *ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (msqid < 0 || cmd < 0)
444 return -EINVAL;
445
446 version = ipc_parse_version(&cmd);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700447 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 switch (cmd) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700450 case IPC_INFO:
451 case MSG_INFO:
452 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 struct msginfo msginfo;
454 int max_id;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (!buf)
457 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700458 /*
459 * We must not return kernel stack data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 * due to padding, it's not enough
461 * to set all member fields.
462 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 err = security_msg_queue_msgctl(NULL, cmd);
464 if (err)
465 return err;
466
Ingo Molnar5a06a362006-07-30 03:04:11 -0700467 memset(&msginfo, 0, sizeof(msginfo));
Kirill Korotaev1e786932006-10-02 02:18:21 -0700468 msginfo.msgmni = ns->msg_ctlmni;
469 msginfo.msgmax = ns->msg_ctlmax;
470 msginfo.msgmnb = ns->msg_ctlmnb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 msginfo.msgssz = MSGSSZ;
472 msginfo.msgseg = MSGSEG;
Nadia Derbey3e148c72007-10-18 23:40:54 -0700473 down_read(&msg_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (cmd == MSG_INFO) {
Kirill Korotaev1e786932006-10-02 02:18:21 -0700475 msginfo.msgpool = msg_ids(ns).in_use;
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700476 msginfo.msgmap = atomic_read(&ns->msg_hdrs);
477 msginfo.msgtql = atomic_read(&ns->msg_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 } else {
479 msginfo.msgmap = MSGMAP;
480 msginfo.msgpool = MSGPOOL;
481 msginfo.msgtql = MSGTQL;
482 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700483 max_id = ipc_get_maxid(&msg_ids(ns));
Nadia Derbey3e148c72007-10-18 23:40:54 -0700484 up_read(&msg_ids(ns).rw_mutex);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700485 if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700487 return (max_id < 0) ? 0 : max_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700489 case MSG_STAT: /* msqid is an index rather than a msg queue id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 case IPC_STAT:
491 {
492 struct msqid64_ds tbuf;
493 int success_return;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (!buf)
496 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Ingo Molnar5a06a362006-07-30 03:04:11 -0700498 if (cmd == MSG_STAT) {
Nadia Derbey023a5352007-10-18 23:40:51 -0700499 msq = msg_lock(ns, msqid);
500 if (IS_ERR(msq))
501 return PTR_ERR(msq);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700502 success_return = msq->q_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 } else {
Nadia Derbey023a5352007-10-18 23:40:51 -0700504 msq = msg_lock_check(ns, msqid);
505 if (IS_ERR(msq))
506 return PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 success_return = 0;
508 }
509 err = -EACCES;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700510 if (ipcperms(&msq->q_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 goto out_unlock;
512
513 err = security_msg_queue_msgctl(msq, cmd);
514 if (err)
515 goto out_unlock;
516
Nadia Derbey023a5352007-10-18 23:40:51 -0700517 memset(&tbuf, 0, sizeof(tbuf));
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
520 tbuf.msg_stime = msq->q_stime;
521 tbuf.msg_rtime = msq->q_rtime;
522 tbuf.msg_ctime = msq->q_ctime;
523 tbuf.msg_cbytes = msq->q_cbytes;
524 tbuf.msg_qnum = msq->q_qnum;
525 tbuf.msg_qbytes = msq->q_qbytes;
526 tbuf.msg_lspid = msq->q_lspid;
527 tbuf.msg_lrpid = msq->q_lrpid;
528 msg_unlock(msq);
529 if (copy_msqid_to_user(buf, &tbuf, version))
530 return -EFAULT;
531 return success_return;
532 }
533 case IPC_SET:
534 if (!buf)
535 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700536 if (copy_msqid_from_user(&setbuf, buf, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 break;
539 case IPC_RMID:
540 break;
541 default:
542 return -EINVAL;
543 }
544
Nadia Derbey3e148c72007-10-18 23:40:54 -0700545 down_write(&msg_ids(ns).rw_mutex);
546 msq = msg_lock_check_down(ns, msqid);
Nadia Derbey023a5352007-10-18 23:40:51 -0700547 if (IS_ERR(msq)) {
548 err = PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 goto out_up;
Nadia Derbey023a5352007-10-18 23:40:51 -0700550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 ipcp = &msq->q_perm;
Steve Grubb073115d2006-04-02 17:07:33 -0400553
554 err = audit_ipc_obj(ipcp);
555 if (err)
556 goto out_unlock_up;
Jeff Garzik8e1c0912007-07-17 05:40:59 -0400557 if (cmd == IPC_SET) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700558 err = audit_ipc_set_perm(setbuf.qbytes, setbuf.uid, setbuf.gid,
559 setbuf.mode);
Linda Knippersac032212006-05-16 22:03:48 -0400560 if (err)
561 goto out_unlock_up;
562 }
Steve Grubb073115d2006-04-02 17:07:33 -0400563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 err = -EPERM;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700565 if (current->euid != ipcp->cuid &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700567 /* We _could_ check for CAP_CHOWN above, but we don't */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 goto out_unlock_up;
569
570 err = security_msg_queue_msgctl(msq, cmd);
571 if (err)
572 goto out_unlock_up;
573
574 switch (cmd) {
575 case IPC_SET:
576 {
577 err = -EPERM;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700578 if (setbuf.qbytes > ns->msg_ctlmnb && !capable(CAP_SYS_RESOURCE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 goto out_unlock_up;
580
581 msq->q_qbytes = setbuf.qbytes;
582
583 ipcp->uid = setbuf.uid;
584 ipcp->gid = setbuf.gid;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700585 ipcp->mode = (ipcp->mode & ~S_IRWXUGO) |
586 (S_IRWXUGO & setbuf.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 msq->q_ctime = get_seconds();
588 /* sleeping receivers might be excluded by
589 * stricter permissions.
590 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700591 expunge_all(msq, -EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 /* sleeping senders might be able to send
593 * due to a larger queue size.
594 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700595 ss_wakeup(&msq->q_senders, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 msg_unlock(msq);
597 break;
598 }
599 case IPC_RMID:
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700600 freeque(ns, msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 break;
602 }
603 err = 0;
604out_up:
Nadia Derbey3e148c72007-10-18 23:40:54 -0700605 up_write(&msg_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return err;
607out_unlock_up:
608 msg_unlock(msq);
609 goto out_up;
610out_unlock:
611 msg_unlock(msq);
612 return err;
613}
614
Ingo Molnar5a06a362006-07-30 03:04:11 -0700615static int testmsg(struct msg_msg *msg, long type, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 switch(mode)
618 {
619 case SEARCH_ANY:
620 return 1;
621 case SEARCH_LESSEQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700622 if (msg->m_type <=type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return 1;
624 break;
625 case SEARCH_EQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700626 if (msg->m_type == type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return 1;
628 break;
629 case SEARCH_NOTEQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700630 if (msg->m_type != type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return 1;
632 break;
633 }
634 return 0;
635}
636
Ingo Molnar5a06a362006-07-30 03:04:11 -0700637static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700639 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 tmp = msq->q_receivers.next;
642 while (tmp != &msq->q_receivers) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700643 struct msg_receiver *msr;
644
645 msr = list_entry(tmp, struct msg_receiver, r_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 tmp = tmp->next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700647 if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
648 !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
649 msr->r_msgtype, msr->r_mode)) {
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 list_del(&msr->r_list);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700652 if (msr->r_maxsize < msg->m_ts) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 msr->r_msg = NULL;
654 wake_up_process(msr->r_tsk);
655 smp_mb();
656 msr->r_msg = ERR_PTR(-E2BIG);
657 } else {
658 msr->r_msg = NULL;
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700659 msq->q_lrpid = task_pid_vnr(msr->r_tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 msq->q_rtime = get_seconds();
661 wake_up_process(msr->r_tsk);
662 smp_mb();
663 msr->r_msg = msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return 1;
666 }
667 }
668 }
669 return 0;
670}
671
suzuki651971c2006-12-06 20:37:48 -0800672long do_msgsnd(int msqid, long mtype, void __user *mtext,
673 size_t msgsz, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 struct msg_queue *msq;
676 struct msg_msg *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 int err;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700678 struct ipc_namespace *ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700679
Kirill Korotaev1e786932006-10-02 02:18:21 -0700680 ns = current->nsproxy->ipc_ns;
681
682 if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (mtype < 1)
685 return -EINVAL;
686
suzuki651971c2006-12-06 20:37:48 -0800687 msg = load_msg(mtext, msgsz);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700688 if (IS_ERR(msg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return PTR_ERR(msg);
690
691 msg->m_type = mtype;
692 msg->m_ts = msgsz;
693
Nadia Derbey023a5352007-10-18 23:40:51 -0700694 msq = msg_lock_check(ns, msqid);
695 if (IS_ERR(msq)) {
696 err = PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 goto out_free;
Nadia Derbey023a5352007-10-18 23:40:51 -0700698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 for (;;) {
701 struct msg_sender s;
702
Ingo Molnar5a06a362006-07-30 03:04:11 -0700703 err = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (ipcperms(&msq->q_perm, S_IWUGO))
705 goto out_unlock_free;
706
707 err = security_msg_queue_msgsnd(msq, msg, msgflg);
708 if (err)
709 goto out_unlock_free;
710
Ingo Molnar5a06a362006-07-30 03:04:11 -0700711 if (msgsz + msq->q_cbytes <= msq->q_qbytes &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 1 + msq->q_qnum <= msq->q_qbytes) {
713 break;
714 }
715
716 /* queue full, wait: */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700717 if (msgflg & IPC_NOWAIT) {
718 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 goto out_unlock_free;
720 }
721 ss_add(msq, &s);
722 ipc_rcu_getref(msq);
723 msg_unlock(msq);
724 schedule();
725
726 ipc_lock_by_ptr(&msq->q_perm);
727 ipc_rcu_putref(msq);
728 if (msq->q_perm.deleted) {
729 err = -EIDRM;
730 goto out_unlock_free;
731 }
732 ss_del(&s);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (signal_pending(current)) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700735 err = -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 goto out_unlock_free;
737 }
738 }
739
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700740 msq->q_lspid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 msq->q_stime = get_seconds();
742
Ingo Molnar5a06a362006-07-30 03:04:11 -0700743 if (!pipelined_send(msq, msg)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 /* noone is waiting for this message, enqueue it */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700745 list_add_tail(&msg->m_list, &msq->q_messages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 msq->q_cbytes += msgsz;
747 msq->q_qnum++;
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700748 atomic_add(msgsz, &ns->msg_bytes);
749 atomic_inc(&ns->msg_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 err = 0;
753 msg = NULL;
754
755out_unlock_free:
756 msg_unlock(msq);
757out_free:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700758 if (msg != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 free_msg(msg);
760 return err;
761}
762
suzuki651971c2006-12-06 20:37:48 -0800763asmlinkage long
764sys_msgsnd(int msqid, struct msgbuf __user *msgp, size_t msgsz, int msgflg)
765{
766 long mtype;
767
768 if (get_user(mtype, &msgp->mtype))
769 return -EFAULT;
770 return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
771}
772
Ingo Molnar5a06a362006-07-30 03:04:11 -0700773static inline int convert_mode(long *msgtyp, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700775 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 * find message of correct type.
777 * msgtyp = 0 => get first.
778 * msgtyp > 0 => get first message of matching type.
Ingo Molnar5a06a362006-07-30 03:04:11 -0700779 * msgtyp < 0 => get message with least type must be < abs(msgtype).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700781 if (*msgtyp == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return SEARCH_ANY;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700783 if (*msgtyp < 0) {
784 *msgtyp = -*msgtyp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return SEARCH_LESSEQUAL;
786 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700787 if (msgflg & MSG_EXCEPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return SEARCH_NOTEQUAL;
789 return SEARCH_EQUAL;
790}
791
suzuki651971c2006-12-06 20:37:48 -0800792long do_msgrcv(int msqid, long *pmtype, void __user *mtext,
793 size_t msgsz, long msgtyp, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
795 struct msg_queue *msq;
796 struct msg_msg *msg;
797 int mode;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700798 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 if (msqid < 0 || (long) msgsz < 0)
801 return -EINVAL;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700802 mode = convert_mode(&msgtyp, msgflg);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700803 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Nadia Derbey023a5352007-10-18 23:40:51 -0700805 msq = msg_lock_check(ns, msqid);
806 if (IS_ERR(msq))
807 return PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 for (;;) {
810 struct msg_receiver msr_d;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700811 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 msg = ERR_PTR(-EACCES);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700814 if (ipcperms(&msq->q_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 goto out_unlock;
816
817 msg = ERR_PTR(-EAGAIN);
818 tmp = msq->q_messages.next;
819 while (tmp != &msq->q_messages) {
820 struct msg_msg *walk_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700821
822 walk_msg = list_entry(tmp, struct msg_msg, m_list);
823 if (testmsg(walk_msg, msgtyp, mode) &&
824 !security_msg_queue_msgrcv(msq, walk_msg, current,
825 msgtyp, mode)) {
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 msg = walk_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700828 if (mode == SEARCH_LESSEQUAL &&
829 walk_msg->m_type != 1) {
830 msg = walk_msg;
831 msgtyp = walk_msg->m_type - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 } else {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700833 msg = walk_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 break;
835 }
836 }
837 tmp = tmp->next;
838 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700839 if (!IS_ERR(msg)) {
840 /*
841 * Found a suitable message.
842 * Unlink it from the queue.
843 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
845 msg = ERR_PTR(-E2BIG);
846 goto out_unlock;
847 }
848 list_del(&msg->m_list);
849 msq->q_qnum--;
850 msq->q_rtime = get_seconds();
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700851 msq->q_lrpid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 msq->q_cbytes -= msg->m_ts;
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700853 atomic_sub(msg->m_ts, &ns->msg_bytes);
854 atomic_dec(&ns->msg_hdrs);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700855 ss_wakeup(&msq->q_senders, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 msg_unlock(msq);
857 break;
858 }
859 /* No message waiting. Wait for a message */
860 if (msgflg & IPC_NOWAIT) {
861 msg = ERR_PTR(-ENOMSG);
862 goto out_unlock;
863 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700864 list_add_tail(&msr_d.r_list, &msq->q_receivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 msr_d.r_tsk = current;
866 msr_d.r_msgtype = msgtyp;
867 msr_d.r_mode = mode;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700868 if (msgflg & MSG_NOERROR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 msr_d.r_maxsize = INT_MAX;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700870 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 msr_d.r_maxsize = msgsz;
872 msr_d.r_msg = ERR_PTR(-EAGAIN);
873 current->state = TASK_INTERRUPTIBLE;
874 msg_unlock(msq);
875
876 schedule();
877
878 /* Lockless receive, part 1:
879 * Disable preemption. We don't hold a reference to the queue
880 * and getting a reference would defeat the idea of a lockless
881 * operation, thus the code relies on rcu to guarantee the
882 * existance of msq:
883 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
884 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
885 * rcu_read_lock() prevents preemption between reading r_msg
886 * and the spin_lock() inside ipc_lock_by_ptr().
887 */
888 rcu_read_lock();
889
890 /* Lockless receive, part 2:
891 * Wait until pipelined_send or expunge_all are outside of
892 * wake_up_process(). There is a race with exit(), see
893 * ipc/mqueue.c for the details.
894 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700895 msg = (struct msg_msg*)msr_d.r_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 while (msg == NULL) {
897 cpu_relax();
Ingo Molnar5a06a362006-07-30 03:04:11 -0700898 msg = (struct msg_msg *)msr_d.r_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
900
901 /* Lockless receive, part 3:
902 * If there is a message or an error then accept it without
903 * locking.
904 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700905 if (msg != ERR_PTR(-EAGAIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 rcu_read_unlock();
907 break;
908 }
909
910 /* Lockless receive, part 3:
911 * Acquire the queue spinlock.
912 */
913 ipc_lock_by_ptr(&msq->q_perm);
914 rcu_read_unlock();
915
916 /* Lockless receive, part 4:
917 * Repeat test after acquiring the spinlock.
918 */
919 msg = (struct msg_msg*)msr_d.r_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700920 if (msg != ERR_PTR(-EAGAIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 goto out_unlock;
922
923 list_del(&msr_d.r_list);
924 if (signal_pending(current)) {
925 msg = ERR_PTR(-ERESTARTNOHAND);
926out_unlock:
927 msg_unlock(msq);
928 break;
929 }
930 }
931 if (IS_ERR(msg))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700932 return PTR_ERR(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
suzuki651971c2006-12-06 20:37:48 -0800935 *pmtype = msg->m_type;
936 if (store_msg(mtext, msg, msgsz))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700937 msgsz = -EFAULT;
suzuki651971c2006-12-06 20:37:48 -0800938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 free_msg(msg);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return msgsz;
942}
943
suzuki651971c2006-12-06 20:37:48 -0800944asmlinkage long sys_msgrcv(int msqid, struct msgbuf __user *msgp, size_t msgsz,
945 long msgtyp, int msgflg)
946{
947 long err, mtype;
948
949 err = do_msgrcv(msqid, &mtype, msgp->mtext, msgsz, msgtyp, msgflg);
950 if (err < 0)
951 goto out;
952
953 if (put_user(mtype, &msgp->mtype))
954 err = -EFAULT;
955out:
956 return err;
957}
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -0700960static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Mike Waychison19b49462005-09-06 15:17:10 -0700962 struct msg_queue *msq = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Mike Waychison19b49462005-09-06 15:17:10 -0700964 return seq_printf(s,
Ingo Molnar5a06a362006-07-30 03:04:11 -0700965 "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
966 msq->q_perm.key,
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700967 msq->q_perm.id,
Ingo Molnar5a06a362006-07-30 03:04:11 -0700968 msq->q_perm.mode,
969 msq->q_cbytes,
970 msq->q_qnum,
971 msq->q_lspid,
972 msq->q_lrpid,
973 msq->q_perm.uid,
974 msq->q_perm.gid,
975 msq->q_perm.cuid,
976 msq->q_perm.cgid,
977 msq->q_stime,
978 msq->q_rtime,
979 msq->q_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981#endif