blob: 46585a05473eceb84f23c456e0f7dc96ab0bfbf6 [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
Pierre Peiffer01b8b072008-02-08 04:18:57 -080075static void freeque(struct ipc_namespace *, struct kern_ipc_perm *);
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{
Pierre Peiffer01b8b072008-02-08 04:18:57 -080094 free_ipcs(ns, &msg_ids(ns), freeque);
Kirill Korotaev1e786932006-10-02 02:18:21 -070095}
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080096#endif
Kirill Korotaev1e786932006-10-02 02:18:21 -070097
98void __init msg_init(void)
99{
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800100 msg_init_ns(&init_ipc_ns);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700101 ipc_init_proc_interface("sysvipc/msg",
102 " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
103 IPC_MSG_IDS, sysvipc_msg_proc_show);
104}
105
Nadia Derbey3e148c72007-10-18 23:40:54 -0700106/*
107 * This routine is called in the paths where the rw_mutex is held to protect
108 * access to the idr tree.
109 */
110static inline struct msg_queue *msg_lock_check_down(struct ipc_namespace *ns,
111 int id)
112{
113 struct kern_ipc_perm *ipcp = ipc_lock_check_down(&msg_ids(ns), id);
114
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800115 if (IS_ERR(ipcp))
116 return (struct msg_queue *)ipcp;
117
Nadia Derbey3e148c72007-10-18 23:40:54 -0700118 return container_of(ipcp, struct msg_queue, q_perm);
119}
120
121/*
122 * msg_lock_(check_) routines are called in the paths where the rw_mutex
123 * is not held.
124 */
Nadia Derbey023a5352007-10-18 23:40:51 -0700125static inline struct msg_queue *msg_lock(struct ipc_namespace *ns, int id)
126{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700127 struct kern_ipc_perm *ipcp = ipc_lock(&msg_ids(ns), id);
128
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800129 if (IS_ERR(ipcp))
130 return (struct msg_queue *)ipcp;
131
Nadia Derbey03f02c72007-10-18 23:40:51 -0700132 return container_of(ipcp, struct msg_queue, q_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700133}
134
135static inline struct msg_queue *msg_lock_check(struct ipc_namespace *ns,
136 int id)
137{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700138 struct kern_ipc_perm *ipcp = ipc_lock_check(&msg_ids(ns), id);
139
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800140 if (IS_ERR(ipcp))
141 return (struct msg_queue *)ipcp;
142
Nadia Derbey03f02c72007-10-18 23:40:51 -0700143 return container_of(ipcp, struct msg_queue, q_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700144}
145
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700146static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s)
147{
148 ipc_rmid(&msg_ids(ns), &s->q_perm);
149}
150
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700151/**
152 * newque - Create a new msg queue
153 * @ns: namespace
154 * @params: ptr to the structure that contains the key and msgflg
155 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700156 * Called with msg_ids.rw_mutex held (writer)
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700157 */
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700158static int newque(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 struct msg_queue *msq;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700161 int id, retval;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700162 key_t key = params->key;
163 int msgflg = params->flg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Ingo Molnar5a06a362006-07-30 03:04:11 -0700165 msq = ipc_rcu_alloc(sizeof(*msq));
166 if (!msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return -ENOMEM;
168
Ingo Molnar5a06a362006-07-30 03:04:11 -0700169 msq->q_perm.mode = msgflg & S_IRWXUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 msq->q_perm.key = key;
171
172 msq->q_perm.security = NULL;
173 retval = security_msg_queue_alloc(msq);
174 if (retval) {
175 ipc_rcu_putref(msq);
176 return retval;
177 }
178
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700179 /*
180 * ipc_addid() locks msq
181 */
Kirill Korotaev1e786932006-10-02 02:18:21 -0700182 id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700183 if (id < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 security_msg_queue_free(msq);
185 ipc_rcu_putref(msq);
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700186 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188
Nadia Derbey1b531f22007-10-18 23:40:55 -0700189 msq->q_perm.id = msg_buildid(id, msq->q_perm.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 msq->q_stime = msq->q_rtime = 0;
191 msq->q_ctime = get_seconds();
192 msq->q_cbytes = msq->q_qnum = 0;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700193 msq->q_qbytes = ns->msg_ctlmnb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 msq->q_lspid = msq->q_lrpid = 0;
195 INIT_LIST_HEAD(&msq->q_messages);
196 INIT_LIST_HEAD(&msq->q_receivers);
197 INIT_LIST_HEAD(&msq->q_senders);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 msg_unlock(msq);
200
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700201 return msq->q_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
Ingo Molnar5a06a362006-07-30 03:04:11 -0700204static inline void ss_add(struct msg_queue *msq, struct msg_sender *mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700206 mss->tsk = current;
207 current->state = TASK_INTERRUPTIBLE;
208 list_add_tail(&mss->list, &msq->q_senders);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Ingo Molnar5a06a362006-07-30 03:04:11 -0700211static inline void ss_del(struct msg_sender *mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700213 if (mss->list.next != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 list_del(&mss->list);
215}
216
Ingo Molnar5a06a362006-07-30 03:04:11 -0700217static void ss_wakeup(struct list_head *h, int kill)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 struct list_head *tmp;
220
221 tmp = h->next;
222 while (tmp != h) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700223 struct msg_sender *mss;
224
225 mss = list_entry(tmp, struct msg_sender, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 tmp = tmp->next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700227 if (kill)
228 mss->list.next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 wake_up_process(mss->tsk);
230 }
231}
232
Ingo Molnar5a06a362006-07-30 03:04:11 -0700233static void expunge_all(struct msg_queue *msq, int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 struct list_head *tmp;
236
237 tmp = msq->q_receivers.next;
238 while (tmp != &msq->q_receivers) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700239 struct msg_receiver *msr;
240
241 msr = list_entry(tmp, struct msg_receiver, r_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 tmp = tmp->next;
243 msr->r_msg = NULL;
244 wake_up_process(msr->r_tsk);
245 smp_mb();
246 msr->r_msg = ERR_PTR(res);
247 }
248}
Ingo Molnar5a06a362006-07-30 03:04:11 -0700249
250/*
251 * freeque() wakes up waiters on the sender and receiver waiting queue,
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700252 * removes the message queue from message queue ID IDR, and cleans up all the
253 * messages associated with this queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700255 * msg_ids.rw_mutex (writer) and the spinlock for this message queue are held
256 * before freeque() is called. msg_ids.rw_mutex remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 */
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800258static void freeque(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 struct list_head *tmp;
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800261 struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Ingo Molnar5a06a362006-07-30 03:04:11 -0700263 expunge_all(msq, -EIDRM);
264 ss_wakeup(&msq->q_senders, 1);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700265 msg_rmid(ns, msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 msg_unlock(msq);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 tmp = msq->q_messages.next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700269 while (tmp != &msq->q_messages) {
270 struct msg_msg *msg = list_entry(tmp, struct msg_msg, m_list);
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 tmp = tmp->next;
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700273 atomic_dec(&ns->msg_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 free_msg(msg);
275 }
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700276 atomic_sub(msq->q_cbytes, &ns->msg_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 security_msg_queue_free(msq);
278 ipc_rcu_putref(msq);
279}
280
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700281/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700282 * Called with msg_ids.rw_mutex and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700283 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700284static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700285{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700286 struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
287
288 return security_msg_queue_associate(msq, msgflg);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700289}
290
Ingo Molnar5a06a362006-07-30 03:04:11 -0700291asmlinkage long sys_msgget(key_t key, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Kirill Korotaev1e786932006-10-02 02:18:21 -0700293 struct ipc_namespace *ns;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700294 struct ipc_ops msg_ops;
295 struct ipc_params msg_params;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700296
297 ns = current->nsproxy->ipc_ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700298
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700299 msg_ops.getnew = newque;
300 msg_ops.associate = msg_security;
301 msg_ops.more_checks = NULL;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700302
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700303 msg_params.key = key;
304 msg_params.flg = msgflg;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700305
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700306 return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Ingo Molnar5a06a362006-07-30 03:04:11 -0700309static inline unsigned long
310copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 switch(version) {
313 case IPC_64:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700314 return copy_to_user(buf, in, sizeof(*in));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 case IPC_OLD:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700316 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 struct msqid_ds out;
318
Ingo Molnar5a06a362006-07-30 03:04:11 -0700319 memset(&out, 0, sizeof(out));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
322
323 out.msg_stime = in->msg_stime;
324 out.msg_rtime = in->msg_rtime;
325 out.msg_ctime = in->msg_ctime;
326
Ingo Molnar5a06a362006-07-30 03:04:11 -0700327 if (in->msg_cbytes > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 out.msg_cbytes = USHRT_MAX;
329 else
330 out.msg_cbytes = in->msg_cbytes;
331 out.msg_lcbytes = in->msg_cbytes;
332
Ingo Molnar5a06a362006-07-30 03:04:11 -0700333 if (in->msg_qnum > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 out.msg_qnum = USHRT_MAX;
335 else
336 out.msg_qnum = in->msg_qnum;
337
Ingo Molnar5a06a362006-07-30 03:04:11 -0700338 if (in->msg_qbytes > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 out.msg_qbytes = USHRT_MAX;
340 else
341 out.msg_qbytes = in->msg_qbytes;
342 out.msg_lqbytes = in->msg_qbytes;
343
344 out.msg_lspid = in->msg_lspid;
345 out.msg_lrpid = in->msg_lrpid;
346
Ingo Molnar5a06a362006-07-30 03:04:11 -0700347 return copy_to_user(buf, &out, sizeof(out));
348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 default:
350 return -EINVAL;
351 }
352}
353
354struct msq_setbuf {
355 unsigned long qbytes;
356 uid_t uid;
357 gid_t gid;
358 mode_t mode;
359};
360
Ingo Molnar5a06a362006-07-30 03:04:11 -0700361static inline unsigned long
362copy_msqid_from_user(struct msq_setbuf *out, void __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 switch(version) {
365 case IPC_64:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700366 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 struct msqid64_ds tbuf;
368
Ingo Molnar5a06a362006-07-30 03:04:11 -0700369 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return -EFAULT;
371
372 out->qbytes = tbuf.msg_qbytes;
373 out->uid = tbuf.msg_perm.uid;
374 out->gid = tbuf.msg_perm.gid;
375 out->mode = tbuf.msg_perm.mode;
376
377 return 0;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 case IPC_OLD:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700380 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 struct msqid_ds tbuf_old;
382
Ingo Molnar5a06a362006-07-30 03:04:11 -0700383 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return -EFAULT;
385
386 out->uid = tbuf_old.msg_perm.uid;
387 out->gid = tbuf_old.msg_perm.gid;
388 out->mode = tbuf_old.msg_perm.mode;
389
Ingo Molnar5a06a362006-07-30 03:04:11 -0700390 if (tbuf_old.msg_qbytes == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 out->qbytes = tbuf_old.msg_lqbytes;
392 else
393 out->qbytes = tbuf_old.msg_qbytes;
394
395 return 0;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 default:
398 return -EINVAL;
399 }
400}
401
Ingo Molnar5a06a362006-07-30 03:04:11 -0700402asmlinkage long sys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 struct kern_ipc_perm *ipcp;
Jeff Garzik8e1c0912007-07-17 05:40:59 -0400405 struct msq_setbuf uninitialized_var(setbuf);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700406 struct msg_queue *msq;
407 int err, version;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700408 struct ipc_namespace *ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (msqid < 0 || cmd < 0)
411 return -EINVAL;
412
413 version = ipc_parse_version(&cmd);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700414 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 switch (cmd) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700417 case IPC_INFO:
418 case MSG_INFO:
419 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 struct msginfo msginfo;
421 int max_id;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (!buf)
424 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700425 /*
426 * We must not return kernel stack data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 * due to padding, it's not enough
428 * to set all member fields.
429 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 err = security_msg_queue_msgctl(NULL, cmd);
431 if (err)
432 return err;
433
Ingo Molnar5a06a362006-07-30 03:04:11 -0700434 memset(&msginfo, 0, sizeof(msginfo));
Kirill Korotaev1e786932006-10-02 02:18:21 -0700435 msginfo.msgmni = ns->msg_ctlmni;
436 msginfo.msgmax = ns->msg_ctlmax;
437 msginfo.msgmnb = ns->msg_ctlmnb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 msginfo.msgssz = MSGSSZ;
439 msginfo.msgseg = MSGSEG;
Nadia Derbey3e148c72007-10-18 23:40:54 -0700440 down_read(&msg_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (cmd == MSG_INFO) {
Kirill Korotaev1e786932006-10-02 02:18:21 -0700442 msginfo.msgpool = msg_ids(ns).in_use;
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700443 msginfo.msgmap = atomic_read(&ns->msg_hdrs);
444 msginfo.msgtql = atomic_read(&ns->msg_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 } else {
446 msginfo.msgmap = MSGMAP;
447 msginfo.msgpool = MSGPOOL;
448 msginfo.msgtql = MSGTQL;
449 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700450 max_id = ipc_get_maxid(&msg_ids(ns));
Nadia Derbey3e148c72007-10-18 23:40:54 -0700451 up_read(&msg_ids(ns).rw_mutex);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700452 if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700454 return (max_id < 0) ? 0 : max_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700456 case MSG_STAT: /* msqid is an index rather than a msg queue id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 case IPC_STAT:
458 {
459 struct msqid64_ds tbuf;
460 int success_return;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (!buf)
463 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Ingo Molnar5a06a362006-07-30 03:04:11 -0700465 if (cmd == MSG_STAT) {
Nadia Derbey023a5352007-10-18 23:40:51 -0700466 msq = msg_lock(ns, msqid);
467 if (IS_ERR(msq))
468 return PTR_ERR(msq);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700469 success_return = msq->q_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 } else {
Nadia Derbey023a5352007-10-18 23:40:51 -0700471 msq = msg_lock_check(ns, msqid);
472 if (IS_ERR(msq))
473 return PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 success_return = 0;
475 }
476 err = -EACCES;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700477 if (ipcperms(&msq->q_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 goto out_unlock;
479
480 err = security_msg_queue_msgctl(msq, cmd);
481 if (err)
482 goto out_unlock;
483
Nadia Derbey023a5352007-10-18 23:40:51 -0700484 memset(&tbuf, 0, sizeof(tbuf));
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
487 tbuf.msg_stime = msq->q_stime;
488 tbuf.msg_rtime = msq->q_rtime;
489 tbuf.msg_ctime = msq->q_ctime;
490 tbuf.msg_cbytes = msq->q_cbytes;
491 tbuf.msg_qnum = msq->q_qnum;
492 tbuf.msg_qbytes = msq->q_qbytes;
493 tbuf.msg_lspid = msq->q_lspid;
494 tbuf.msg_lrpid = msq->q_lrpid;
495 msg_unlock(msq);
496 if (copy_msqid_to_user(buf, &tbuf, version))
497 return -EFAULT;
498 return success_return;
499 }
500 case IPC_SET:
501 if (!buf)
502 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700503 if (copy_msqid_from_user(&setbuf, buf, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 break;
506 case IPC_RMID:
507 break;
508 default:
509 return -EINVAL;
510 }
511
Nadia Derbey3e148c72007-10-18 23:40:54 -0700512 down_write(&msg_ids(ns).rw_mutex);
513 msq = msg_lock_check_down(ns, msqid);
Nadia Derbey023a5352007-10-18 23:40:51 -0700514 if (IS_ERR(msq)) {
515 err = PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 goto out_up;
Nadia Derbey023a5352007-10-18 23:40:51 -0700517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 ipcp = &msq->q_perm;
Steve Grubb073115d2006-04-02 17:07:33 -0400520
521 err = audit_ipc_obj(ipcp);
522 if (err)
523 goto out_unlock_up;
Jeff Garzik8e1c0912007-07-17 05:40:59 -0400524 if (cmd == IPC_SET) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700525 err = audit_ipc_set_perm(setbuf.qbytes, setbuf.uid, setbuf.gid,
526 setbuf.mode);
Linda Knippersac032212006-05-16 22:03:48 -0400527 if (err)
528 goto out_unlock_up;
529 }
Steve Grubb073115d2006-04-02 17:07:33 -0400530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 err = -EPERM;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700532 if (current->euid != ipcp->cuid &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700534 /* We _could_ check for CAP_CHOWN above, but we don't */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 goto out_unlock_up;
536
537 err = security_msg_queue_msgctl(msq, cmd);
538 if (err)
539 goto out_unlock_up;
540
541 switch (cmd) {
542 case IPC_SET:
543 {
544 err = -EPERM;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700545 if (setbuf.qbytes > ns->msg_ctlmnb && !capable(CAP_SYS_RESOURCE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 goto out_unlock_up;
547
548 msq->q_qbytes = setbuf.qbytes;
549
550 ipcp->uid = setbuf.uid;
551 ipcp->gid = setbuf.gid;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700552 ipcp->mode = (ipcp->mode & ~S_IRWXUGO) |
553 (S_IRWXUGO & setbuf.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 msq->q_ctime = get_seconds();
555 /* sleeping receivers might be excluded by
556 * stricter permissions.
557 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700558 expunge_all(msq, -EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 /* sleeping senders might be able to send
560 * due to a larger queue size.
561 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700562 ss_wakeup(&msq->q_senders, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 msg_unlock(msq);
564 break;
565 }
566 case IPC_RMID:
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800567 freeque(ns, &msq->q_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 break;
569 }
570 err = 0;
571out_up:
Nadia Derbey3e148c72007-10-18 23:40:54 -0700572 up_write(&msg_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return err;
574out_unlock_up:
575 msg_unlock(msq);
576 goto out_up;
577out_unlock:
578 msg_unlock(msq);
579 return err;
580}
581
Ingo Molnar5a06a362006-07-30 03:04:11 -0700582static int testmsg(struct msg_msg *msg, long type, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 switch(mode)
585 {
586 case SEARCH_ANY:
587 return 1;
588 case SEARCH_LESSEQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700589 if (msg->m_type <=type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return 1;
591 break;
592 case SEARCH_EQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700593 if (msg->m_type == type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return 1;
595 break;
596 case SEARCH_NOTEQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700597 if (msg->m_type != type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return 1;
599 break;
600 }
601 return 0;
602}
603
Ingo Molnar5a06a362006-07-30 03:04:11 -0700604static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700606 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 tmp = msq->q_receivers.next;
609 while (tmp != &msq->q_receivers) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700610 struct msg_receiver *msr;
611
612 msr = list_entry(tmp, struct msg_receiver, r_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 tmp = tmp->next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700614 if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
615 !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
616 msr->r_msgtype, msr->r_mode)) {
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 list_del(&msr->r_list);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700619 if (msr->r_maxsize < msg->m_ts) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 msr->r_msg = NULL;
621 wake_up_process(msr->r_tsk);
622 smp_mb();
623 msr->r_msg = ERR_PTR(-E2BIG);
624 } else {
625 msr->r_msg = NULL;
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700626 msq->q_lrpid = task_pid_vnr(msr->r_tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 msq->q_rtime = get_seconds();
628 wake_up_process(msr->r_tsk);
629 smp_mb();
630 msr->r_msg = msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return 1;
633 }
634 }
635 }
636 return 0;
637}
638
suzuki651971c2006-12-06 20:37:48 -0800639long do_msgsnd(int msqid, long mtype, void __user *mtext,
640 size_t msgsz, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
642 struct msg_queue *msq;
643 struct msg_msg *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 int err;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700645 struct ipc_namespace *ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700646
Kirill Korotaev1e786932006-10-02 02:18:21 -0700647 ns = current->nsproxy->ipc_ns;
648
649 if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (mtype < 1)
652 return -EINVAL;
653
suzuki651971c2006-12-06 20:37:48 -0800654 msg = load_msg(mtext, msgsz);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700655 if (IS_ERR(msg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return PTR_ERR(msg);
657
658 msg->m_type = mtype;
659 msg->m_ts = msgsz;
660
Nadia Derbey023a5352007-10-18 23:40:51 -0700661 msq = msg_lock_check(ns, msqid);
662 if (IS_ERR(msq)) {
663 err = PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 goto out_free;
Nadia Derbey023a5352007-10-18 23:40:51 -0700665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 for (;;) {
668 struct msg_sender s;
669
Ingo Molnar5a06a362006-07-30 03:04:11 -0700670 err = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (ipcperms(&msq->q_perm, S_IWUGO))
672 goto out_unlock_free;
673
674 err = security_msg_queue_msgsnd(msq, msg, msgflg);
675 if (err)
676 goto out_unlock_free;
677
Ingo Molnar5a06a362006-07-30 03:04:11 -0700678 if (msgsz + msq->q_cbytes <= msq->q_qbytes &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 1 + msq->q_qnum <= msq->q_qbytes) {
680 break;
681 }
682
683 /* queue full, wait: */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700684 if (msgflg & IPC_NOWAIT) {
685 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 goto out_unlock_free;
687 }
688 ss_add(msq, &s);
689 ipc_rcu_getref(msq);
690 msg_unlock(msq);
691 schedule();
692
693 ipc_lock_by_ptr(&msq->q_perm);
694 ipc_rcu_putref(msq);
695 if (msq->q_perm.deleted) {
696 err = -EIDRM;
697 goto out_unlock_free;
698 }
699 ss_del(&s);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (signal_pending(current)) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700702 err = -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 goto out_unlock_free;
704 }
705 }
706
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700707 msq->q_lspid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 msq->q_stime = get_seconds();
709
Ingo Molnar5a06a362006-07-30 03:04:11 -0700710 if (!pipelined_send(msq, msg)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 /* noone is waiting for this message, enqueue it */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700712 list_add_tail(&msg->m_list, &msq->q_messages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 msq->q_cbytes += msgsz;
714 msq->q_qnum++;
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700715 atomic_add(msgsz, &ns->msg_bytes);
716 atomic_inc(&ns->msg_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 err = 0;
720 msg = NULL;
721
722out_unlock_free:
723 msg_unlock(msq);
724out_free:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700725 if (msg != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 free_msg(msg);
727 return err;
728}
729
suzuki651971c2006-12-06 20:37:48 -0800730asmlinkage long
731sys_msgsnd(int msqid, struct msgbuf __user *msgp, size_t msgsz, int msgflg)
732{
733 long mtype;
734
735 if (get_user(mtype, &msgp->mtype))
736 return -EFAULT;
737 return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
738}
739
Ingo Molnar5a06a362006-07-30 03:04:11 -0700740static inline int convert_mode(long *msgtyp, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700742 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 * find message of correct type.
744 * msgtyp = 0 => get first.
745 * msgtyp > 0 => get first message of matching type.
Ingo Molnar5a06a362006-07-30 03:04:11 -0700746 * msgtyp < 0 => get message with least type must be < abs(msgtype).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700748 if (*msgtyp == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return SEARCH_ANY;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700750 if (*msgtyp < 0) {
751 *msgtyp = -*msgtyp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return SEARCH_LESSEQUAL;
753 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700754 if (msgflg & MSG_EXCEPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return SEARCH_NOTEQUAL;
756 return SEARCH_EQUAL;
757}
758
suzuki651971c2006-12-06 20:37:48 -0800759long do_msgrcv(int msqid, long *pmtype, void __user *mtext,
760 size_t msgsz, long msgtyp, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 struct msg_queue *msq;
763 struct msg_msg *msg;
764 int mode;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700765 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 if (msqid < 0 || (long) msgsz < 0)
768 return -EINVAL;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700769 mode = convert_mode(&msgtyp, msgflg);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700770 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Nadia Derbey023a5352007-10-18 23:40:51 -0700772 msq = msg_lock_check(ns, msqid);
773 if (IS_ERR(msq))
774 return PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 for (;;) {
777 struct msg_receiver msr_d;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700778 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 msg = ERR_PTR(-EACCES);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700781 if (ipcperms(&msq->q_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 goto out_unlock;
783
784 msg = ERR_PTR(-EAGAIN);
785 tmp = msq->q_messages.next;
786 while (tmp != &msq->q_messages) {
787 struct msg_msg *walk_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700788
789 walk_msg = list_entry(tmp, struct msg_msg, m_list);
790 if (testmsg(walk_msg, msgtyp, mode) &&
791 !security_msg_queue_msgrcv(msq, walk_msg, current,
792 msgtyp, mode)) {
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 msg = walk_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700795 if (mode == SEARCH_LESSEQUAL &&
796 walk_msg->m_type != 1) {
797 msg = walk_msg;
798 msgtyp = walk_msg->m_type - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 } else {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700800 msg = walk_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 break;
802 }
803 }
804 tmp = tmp->next;
805 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700806 if (!IS_ERR(msg)) {
807 /*
808 * Found a suitable message.
809 * Unlink it from the queue.
810 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
812 msg = ERR_PTR(-E2BIG);
813 goto out_unlock;
814 }
815 list_del(&msg->m_list);
816 msq->q_qnum--;
817 msq->q_rtime = get_seconds();
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700818 msq->q_lrpid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 msq->q_cbytes -= msg->m_ts;
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700820 atomic_sub(msg->m_ts, &ns->msg_bytes);
821 atomic_dec(&ns->msg_hdrs);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700822 ss_wakeup(&msq->q_senders, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 msg_unlock(msq);
824 break;
825 }
826 /* No message waiting. Wait for a message */
827 if (msgflg & IPC_NOWAIT) {
828 msg = ERR_PTR(-ENOMSG);
829 goto out_unlock;
830 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700831 list_add_tail(&msr_d.r_list, &msq->q_receivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 msr_d.r_tsk = current;
833 msr_d.r_msgtype = msgtyp;
834 msr_d.r_mode = mode;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700835 if (msgflg & MSG_NOERROR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 msr_d.r_maxsize = INT_MAX;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700837 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 msr_d.r_maxsize = msgsz;
839 msr_d.r_msg = ERR_PTR(-EAGAIN);
840 current->state = TASK_INTERRUPTIBLE;
841 msg_unlock(msq);
842
843 schedule();
844
845 /* Lockless receive, part 1:
846 * Disable preemption. We don't hold a reference to the queue
847 * and getting a reference would defeat the idea of a lockless
848 * operation, thus the code relies on rcu to guarantee the
849 * existance of msq:
850 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
851 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
852 * rcu_read_lock() prevents preemption between reading r_msg
853 * and the spin_lock() inside ipc_lock_by_ptr().
854 */
855 rcu_read_lock();
856
857 /* Lockless receive, part 2:
858 * Wait until pipelined_send or expunge_all are outside of
859 * wake_up_process(). There is a race with exit(), see
860 * ipc/mqueue.c for the details.
861 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700862 msg = (struct msg_msg*)msr_d.r_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 while (msg == NULL) {
864 cpu_relax();
Ingo Molnar5a06a362006-07-30 03:04:11 -0700865 msg = (struct msg_msg *)msr_d.r_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867
868 /* Lockless receive, part 3:
869 * If there is a message or an error then accept it without
870 * locking.
871 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700872 if (msg != ERR_PTR(-EAGAIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 rcu_read_unlock();
874 break;
875 }
876
877 /* Lockless receive, part 3:
878 * Acquire the queue spinlock.
879 */
880 ipc_lock_by_ptr(&msq->q_perm);
881 rcu_read_unlock();
882
883 /* Lockless receive, part 4:
884 * Repeat test after acquiring the spinlock.
885 */
886 msg = (struct msg_msg*)msr_d.r_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700887 if (msg != ERR_PTR(-EAGAIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 goto out_unlock;
889
890 list_del(&msr_d.r_list);
891 if (signal_pending(current)) {
892 msg = ERR_PTR(-ERESTARTNOHAND);
893out_unlock:
894 msg_unlock(msq);
895 break;
896 }
897 }
898 if (IS_ERR(msg))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700899 return PTR_ERR(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
suzuki651971c2006-12-06 20:37:48 -0800902 *pmtype = msg->m_type;
903 if (store_msg(mtext, msg, msgsz))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700904 msgsz = -EFAULT;
suzuki651971c2006-12-06 20:37:48 -0800905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 free_msg(msg);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return msgsz;
909}
910
suzuki651971c2006-12-06 20:37:48 -0800911asmlinkage long sys_msgrcv(int msqid, struct msgbuf __user *msgp, size_t msgsz,
912 long msgtyp, int msgflg)
913{
914 long err, mtype;
915
916 err = do_msgrcv(msqid, &mtype, msgp->mtext, msgsz, msgtyp, msgflg);
917 if (err < 0)
918 goto out;
919
920 if (put_user(mtype, &msgp->mtype))
921 err = -EFAULT;
922out:
923 return err;
924}
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -0700927static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Mike Waychison19b49462005-09-06 15:17:10 -0700929 struct msg_queue *msq = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Mike Waychison19b49462005-09-06 15:17:10 -0700931 return seq_printf(s,
Ingo Molnar5a06a362006-07-30 03:04:11 -0700932 "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
933 msq->q_perm.key,
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700934 msq->q_perm.id,
Ingo Molnar5a06a362006-07-30 03:04:11 -0700935 msq->q_perm.mode,
936 msq->q_cbytes,
937 msq->q_qnum,
938 msq->q_lspid,
939 msq->q_lrpid,
940 msq->q_perm.uid,
941 msq->q_perm.gid,
942 msq->q_perm.cuid,
943 msq->q_perm.cgid,
944 msq->q_stime,
945 msq->q_rtime,
946 msq->q_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948#endif