blob: 413bf9c7aec37ebfec86fafc45c0a927587b35c7 [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
Ingo Molnar5a06a362006-07-30 03:04:11 -070069static atomic_t msg_bytes = ATOMIC_INIT(0);
70static atomic_t msg_hdrs = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Kirill Korotaev1e786932006-10-02 02:18:21 -070072static struct ipc_ids init_msg_ids;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Kirill Korotaev1e786932006-10-02 02:18:21 -070074#define msg_ids(ns) (*((ns)->ids[IPC_MSG_IDS]))
75
Ingo Molnar5a06a362006-07-30 03:04:11 -070076#define msg_unlock(msq) ipc_unlock(&(msq)->q_perm)
Kirill Korotaev1e786932006-10-02 02:18:21 -070077#define msg_buildid(ns, id, seq) \
78 ipc_buildid(&msg_ids(ns), id, seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Nadia Derbey7ca7e562007-10-18 23:40:48 -070080static void freeque(struct ipc_namespace *, struct msg_queue *);
Nadia Derbey7748dbf2007-10-18 23:40:49 -070081static int newque(struct ipc_namespace *, struct ipc_params *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -070083static int sysvipc_msg_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#endif
85
Cedric Le Goater7d69a1f2007-07-15 23:40:58 -070086static void __msg_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Kirill Korotaev1e786932006-10-02 02:18:21 -070088 ns->ids[IPC_MSG_IDS] = ids;
89 ns->msg_ctlmax = MSGMAX;
90 ns->msg_ctlmnb = MSGMNB;
91 ns->msg_ctlmni = MSGMNI;
Nadia Derbey7ca7e562007-10-18 23:40:48 -070092 ipc_init_ids(ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Kirill Korotaev1e786932006-10-02 02:18:21 -070095int msg_init_ns(struct ipc_namespace *ns)
96{
97 struct ipc_ids *ids;
98
99 ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
100 if (ids == NULL)
101 return -ENOMEM;
102
103 __msg_init_ns(ns, ids);
104 return 0;
105}
106
107void msg_exit_ns(struct ipc_namespace *ns)
108{
Kirill Korotaev1e786932006-10-02 02:18:21 -0700109 struct msg_queue *msq;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700110 int next_id;
111 int total, in_use;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700112
Nadia Derbey3e148c72007-10-18 23:40:54 -0700113 down_write(&msg_ids(ns).rw_mutex);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700114
115 in_use = msg_ids(ns).in_use;
116
117 for (total = 0, next_id = 0; total < in_use; next_id++) {
118 msq = idr_find(&msg_ids(ns).ipcs_idr, next_id);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700119 if (msq == NULL)
120 continue;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700121 ipc_lock_by_ptr(&msq->q_perm);
122 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
149 return container_of(ipcp, struct msg_queue, q_perm);
150}
151
152/*
153 * msg_lock_(check_) routines are called in the paths where the rw_mutex
154 * is not held.
155 */
Nadia Derbey023a5352007-10-18 23:40:51 -0700156static inline struct msg_queue *msg_lock(struct ipc_namespace *ns, int id)
157{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700158 struct kern_ipc_perm *ipcp = ipc_lock(&msg_ids(ns), id);
159
160 return container_of(ipcp, struct msg_queue, q_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700161}
162
163static inline struct msg_queue *msg_lock_check(struct ipc_namespace *ns,
164 int id)
165{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700166 struct kern_ipc_perm *ipcp = ipc_lock_check(&msg_ids(ns), id);
167
168 return container_of(ipcp, struct msg_queue, q_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700169}
170
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700171static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s)
172{
173 ipc_rmid(&msg_ids(ns), &s->q_perm);
174}
175
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700176/**
177 * newque - Create a new msg queue
178 * @ns: namespace
179 * @params: ptr to the structure that contains the key and msgflg
180 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700181 * Called with msg_ids.rw_mutex held (writer)
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700182 */
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700183static int newque(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 struct msg_queue *msq;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700186 int id, retval;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700187 key_t key = params->key;
188 int msgflg = params->flg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Ingo Molnar5a06a362006-07-30 03:04:11 -0700190 msq = ipc_rcu_alloc(sizeof(*msq));
191 if (!msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return -ENOMEM;
193
Ingo Molnar5a06a362006-07-30 03:04:11 -0700194 msq->q_perm.mode = msgflg & S_IRWXUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 msq->q_perm.key = key;
196
197 msq->q_perm.security = NULL;
198 retval = security_msg_queue_alloc(msq);
199 if (retval) {
200 ipc_rcu_putref(msq);
201 return retval;
202 }
203
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700204 /*
205 * ipc_addid() locks msq
206 */
Kirill Korotaev1e786932006-10-02 02:18:21 -0700207 id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700208 if (id == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 security_msg_queue_free(msq);
210 ipc_rcu_putref(msq);
211 return -ENOSPC;
212 }
213
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700214 msq->q_perm.id = msg_buildid(ns, id, msq->q_perm.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 msq->q_stime = msq->q_rtime = 0;
216 msq->q_ctime = get_seconds();
217 msq->q_cbytes = msq->q_qnum = 0;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700218 msq->q_qbytes = ns->msg_ctlmnb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 msq->q_lspid = msq->q_lrpid = 0;
220 INIT_LIST_HEAD(&msq->q_messages);
221 INIT_LIST_HEAD(&msq->q_receivers);
222 INIT_LIST_HEAD(&msq->q_senders);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 msg_unlock(msq);
225
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700226 return msq->q_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
Ingo Molnar5a06a362006-07-30 03:04:11 -0700229static inline void ss_add(struct msg_queue *msq, struct msg_sender *mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700231 mss->tsk = current;
232 current->state = TASK_INTERRUPTIBLE;
233 list_add_tail(&mss->list, &msq->q_senders);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
Ingo Molnar5a06a362006-07-30 03:04:11 -0700236static inline void ss_del(struct msg_sender *mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700238 if (mss->list.next != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 list_del(&mss->list);
240}
241
Ingo Molnar5a06a362006-07-30 03:04:11 -0700242static void ss_wakeup(struct list_head *h, int kill)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 struct list_head *tmp;
245
246 tmp = h->next;
247 while (tmp != h) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700248 struct msg_sender *mss;
249
250 mss = list_entry(tmp, struct msg_sender, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 tmp = tmp->next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700252 if (kill)
253 mss->list.next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 wake_up_process(mss->tsk);
255 }
256}
257
Ingo Molnar5a06a362006-07-30 03:04:11 -0700258static void expunge_all(struct msg_queue *msq, int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 struct list_head *tmp;
261
262 tmp = msq->q_receivers.next;
263 while (tmp != &msq->q_receivers) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700264 struct msg_receiver *msr;
265
266 msr = list_entry(tmp, struct msg_receiver, r_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 tmp = tmp->next;
268 msr->r_msg = NULL;
269 wake_up_process(msr->r_tsk);
270 smp_mb();
271 msr->r_msg = ERR_PTR(res);
272 }
273}
Ingo Molnar5a06a362006-07-30 03:04:11 -0700274
275/*
276 * freeque() wakes up waiters on the sender and receiver waiting queue,
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700277 * removes the message queue from message queue ID IDR, and cleans up all the
278 * messages associated with this queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700280 * msg_ids.rw_mutex (writer) and the spinlock for this message queue are held
281 * before freeque() is called. msg_ids.rw_mutex remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 */
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700283static void freeque(struct ipc_namespace *ns, struct msg_queue *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 struct list_head *tmp;
286
Ingo Molnar5a06a362006-07-30 03:04:11 -0700287 expunge_all(msq, -EIDRM);
288 ss_wakeup(&msq->q_senders, 1);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700289 msg_rmid(ns, msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 msg_unlock(msq);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 tmp = msq->q_messages.next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700293 while (tmp != &msq->q_messages) {
294 struct msg_msg *msg = list_entry(tmp, struct msg_msg, m_list);
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 tmp = tmp->next;
297 atomic_dec(&msg_hdrs);
298 free_msg(msg);
299 }
300 atomic_sub(msq->q_cbytes, &msg_bytes);
301 security_msg_queue_free(msq);
302 ipc_rcu_putref(msq);
303}
304
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700305/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700306 * Called with msg_ids.rw_mutex and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700307 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700308static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700309{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700310 struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
311
312 return security_msg_queue_associate(msq, msgflg);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700313}
314
Ingo Molnar5a06a362006-07-30 03:04:11 -0700315asmlinkage long sys_msgget(key_t key, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Kirill Korotaev1e786932006-10-02 02:18:21 -0700317 struct ipc_namespace *ns;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700318 struct ipc_ops msg_ops;
319 struct ipc_params msg_params;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700320
321 ns = current->nsproxy->ipc_ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700322
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700323 msg_ops.getnew = newque;
324 msg_ops.associate = msg_security;
325 msg_ops.more_checks = NULL;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700326
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700327 msg_params.key = key;
328 msg_params.flg = msgflg;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700329
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700330 return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Ingo Molnar5a06a362006-07-30 03:04:11 -0700333static inline unsigned long
334copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 switch(version) {
337 case IPC_64:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700338 return copy_to_user(buf, in, sizeof(*in));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 case IPC_OLD:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700340 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 struct msqid_ds out;
342
Ingo Molnar5a06a362006-07-30 03:04:11 -0700343 memset(&out, 0, sizeof(out));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
346
347 out.msg_stime = in->msg_stime;
348 out.msg_rtime = in->msg_rtime;
349 out.msg_ctime = in->msg_ctime;
350
Ingo Molnar5a06a362006-07-30 03:04:11 -0700351 if (in->msg_cbytes > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 out.msg_cbytes = USHRT_MAX;
353 else
354 out.msg_cbytes = in->msg_cbytes;
355 out.msg_lcbytes = in->msg_cbytes;
356
Ingo Molnar5a06a362006-07-30 03:04:11 -0700357 if (in->msg_qnum > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 out.msg_qnum = USHRT_MAX;
359 else
360 out.msg_qnum = in->msg_qnum;
361
Ingo Molnar5a06a362006-07-30 03:04:11 -0700362 if (in->msg_qbytes > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 out.msg_qbytes = USHRT_MAX;
364 else
365 out.msg_qbytes = in->msg_qbytes;
366 out.msg_lqbytes = in->msg_qbytes;
367
368 out.msg_lspid = in->msg_lspid;
369 out.msg_lrpid = in->msg_lrpid;
370
Ingo Molnar5a06a362006-07-30 03:04:11 -0700371 return copy_to_user(buf, &out, sizeof(out));
372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 default:
374 return -EINVAL;
375 }
376}
377
378struct msq_setbuf {
379 unsigned long qbytes;
380 uid_t uid;
381 gid_t gid;
382 mode_t mode;
383};
384
Ingo Molnar5a06a362006-07-30 03:04:11 -0700385static inline unsigned long
386copy_msqid_from_user(struct msq_setbuf *out, void __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 switch(version) {
389 case IPC_64:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700390 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 struct msqid64_ds tbuf;
392
Ingo Molnar5a06a362006-07-30 03:04:11 -0700393 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return -EFAULT;
395
396 out->qbytes = tbuf.msg_qbytes;
397 out->uid = tbuf.msg_perm.uid;
398 out->gid = tbuf.msg_perm.gid;
399 out->mode = tbuf.msg_perm.mode;
400
401 return 0;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 case IPC_OLD:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700404 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 struct msqid_ds tbuf_old;
406
Ingo Molnar5a06a362006-07-30 03:04:11 -0700407 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return -EFAULT;
409
410 out->uid = tbuf_old.msg_perm.uid;
411 out->gid = tbuf_old.msg_perm.gid;
412 out->mode = tbuf_old.msg_perm.mode;
413
Ingo Molnar5a06a362006-07-30 03:04:11 -0700414 if (tbuf_old.msg_qbytes == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 out->qbytes = tbuf_old.msg_lqbytes;
416 else
417 out->qbytes = tbuf_old.msg_qbytes;
418
419 return 0;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 default:
422 return -EINVAL;
423 }
424}
425
Ingo Molnar5a06a362006-07-30 03:04:11 -0700426asmlinkage long sys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 struct kern_ipc_perm *ipcp;
Jeff Garzik8e1c0912007-07-17 05:40:59 -0400429 struct msq_setbuf uninitialized_var(setbuf);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700430 struct msg_queue *msq;
431 int err, version;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700432 struct ipc_namespace *ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (msqid < 0 || cmd < 0)
435 return -EINVAL;
436
437 version = ipc_parse_version(&cmd);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700438 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 switch (cmd) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700441 case IPC_INFO:
442 case MSG_INFO:
443 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 struct msginfo msginfo;
445 int max_id;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (!buf)
448 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700449 /*
450 * We must not return kernel stack data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 * due to padding, it's not enough
452 * to set all member fields.
453 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 err = security_msg_queue_msgctl(NULL, cmd);
455 if (err)
456 return err;
457
Ingo Molnar5a06a362006-07-30 03:04:11 -0700458 memset(&msginfo, 0, sizeof(msginfo));
Kirill Korotaev1e786932006-10-02 02:18:21 -0700459 msginfo.msgmni = ns->msg_ctlmni;
460 msginfo.msgmax = ns->msg_ctlmax;
461 msginfo.msgmnb = ns->msg_ctlmnb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 msginfo.msgssz = MSGSSZ;
463 msginfo.msgseg = MSGSEG;
Nadia Derbey3e148c72007-10-18 23:40:54 -0700464 down_read(&msg_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (cmd == MSG_INFO) {
Kirill Korotaev1e786932006-10-02 02:18:21 -0700466 msginfo.msgpool = msg_ids(ns).in_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 msginfo.msgmap = atomic_read(&msg_hdrs);
468 msginfo.msgtql = atomic_read(&msg_bytes);
469 } else {
470 msginfo.msgmap = MSGMAP;
471 msginfo.msgpool = MSGPOOL;
472 msginfo.msgtql = MSGTQL;
473 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700474 max_id = ipc_get_maxid(&msg_ids(ns));
Nadia Derbey3e148c72007-10-18 23:40:54 -0700475 up_read(&msg_ids(ns).rw_mutex);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700476 if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700478 return (max_id < 0) ? 0 : max_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700480 case MSG_STAT: /* msqid is an index rather than a msg queue id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 case IPC_STAT:
482 {
483 struct msqid64_ds tbuf;
484 int success_return;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (!buf)
487 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Ingo Molnar5a06a362006-07-30 03:04:11 -0700489 if (cmd == MSG_STAT) {
Nadia Derbey023a5352007-10-18 23:40:51 -0700490 msq = msg_lock(ns, msqid);
491 if (IS_ERR(msq))
492 return PTR_ERR(msq);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700493 success_return = msq->q_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 } else {
Nadia Derbey023a5352007-10-18 23:40:51 -0700495 msq = msg_lock_check(ns, msqid);
496 if (IS_ERR(msq))
497 return PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 success_return = 0;
499 }
500 err = -EACCES;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700501 if (ipcperms(&msq->q_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 goto out_unlock;
503
504 err = security_msg_queue_msgctl(msq, cmd);
505 if (err)
506 goto out_unlock;
507
Nadia Derbey023a5352007-10-18 23:40:51 -0700508 memset(&tbuf, 0, sizeof(tbuf));
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
511 tbuf.msg_stime = msq->q_stime;
512 tbuf.msg_rtime = msq->q_rtime;
513 tbuf.msg_ctime = msq->q_ctime;
514 tbuf.msg_cbytes = msq->q_cbytes;
515 tbuf.msg_qnum = msq->q_qnum;
516 tbuf.msg_qbytes = msq->q_qbytes;
517 tbuf.msg_lspid = msq->q_lspid;
518 tbuf.msg_lrpid = msq->q_lrpid;
519 msg_unlock(msq);
520 if (copy_msqid_to_user(buf, &tbuf, version))
521 return -EFAULT;
522 return success_return;
523 }
524 case IPC_SET:
525 if (!buf)
526 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700527 if (copy_msqid_from_user(&setbuf, buf, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 break;
530 case IPC_RMID:
531 break;
532 default:
533 return -EINVAL;
534 }
535
Nadia Derbey3e148c72007-10-18 23:40:54 -0700536 down_write(&msg_ids(ns).rw_mutex);
537 msq = msg_lock_check_down(ns, msqid);
Nadia Derbey023a5352007-10-18 23:40:51 -0700538 if (IS_ERR(msq)) {
539 err = PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 goto out_up;
Nadia Derbey023a5352007-10-18 23:40:51 -0700541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 ipcp = &msq->q_perm;
Steve Grubb073115d2006-04-02 17:07:33 -0400544
545 err = audit_ipc_obj(ipcp);
546 if (err)
547 goto out_unlock_up;
Jeff Garzik8e1c0912007-07-17 05:40:59 -0400548 if (cmd == IPC_SET) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700549 err = audit_ipc_set_perm(setbuf.qbytes, setbuf.uid, setbuf.gid,
550 setbuf.mode);
Linda Knippersac032212006-05-16 22:03:48 -0400551 if (err)
552 goto out_unlock_up;
553 }
Steve Grubb073115d2006-04-02 17:07:33 -0400554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 err = -EPERM;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700556 if (current->euid != ipcp->cuid &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700558 /* We _could_ check for CAP_CHOWN above, but we don't */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 goto out_unlock_up;
560
561 err = security_msg_queue_msgctl(msq, cmd);
562 if (err)
563 goto out_unlock_up;
564
565 switch (cmd) {
566 case IPC_SET:
567 {
568 err = -EPERM;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700569 if (setbuf.qbytes > ns->msg_ctlmnb && !capable(CAP_SYS_RESOURCE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 goto out_unlock_up;
571
572 msq->q_qbytes = setbuf.qbytes;
573
574 ipcp->uid = setbuf.uid;
575 ipcp->gid = setbuf.gid;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700576 ipcp->mode = (ipcp->mode & ~S_IRWXUGO) |
577 (S_IRWXUGO & setbuf.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 msq->q_ctime = get_seconds();
579 /* sleeping receivers might be excluded by
580 * stricter permissions.
581 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700582 expunge_all(msq, -EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 /* sleeping senders might be able to send
584 * due to a larger queue size.
585 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700586 ss_wakeup(&msq->q_senders, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 msg_unlock(msq);
588 break;
589 }
590 case IPC_RMID:
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700591 freeque(ns, msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 break;
593 }
594 err = 0;
595out_up:
Nadia Derbey3e148c72007-10-18 23:40:54 -0700596 up_write(&msg_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return err;
598out_unlock_up:
599 msg_unlock(msq);
600 goto out_up;
601out_unlock:
602 msg_unlock(msq);
603 return err;
604}
605
Ingo Molnar5a06a362006-07-30 03:04:11 -0700606static int testmsg(struct msg_msg *msg, long type, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
608 switch(mode)
609 {
610 case SEARCH_ANY:
611 return 1;
612 case SEARCH_LESSEQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700613 if (msg->m_type <=type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return 1;
615 break;
616 case SEARCH_EQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700617 if (msg->m_type == type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return 1;
619 break;
620 case SEARCH_NOTEQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700621 if (msg->m_type != type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return 1;
623 break;
624 }
625 return 0;
626}
627
Ingo Molnar5a06a362006-07-30 03:04:11 -0700628static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700630 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 tmp = msq->q_receivers.next;
633 while (tmp != &msq->q_receivers) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700634 struct msg_receiver *msr;
635
636 msr = list_entry(tmp, struct msg_receiver, r_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 tmp = tmp->next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700638 if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
639 !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
640 msr->r_msgtype, msr->r_mode)) {
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 list_del(&msr->r_list);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700643 if (msr->r_maxsize < msg->m_ts) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 msr->r_msg = NULL;
645 wake_up_process(msr->r_tsk);
646 smp_mb();
647 msr->r_msg = ERR_PTR(-E2BIG);
648 } else {
649 msr->r_msg = NULL;
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700650 msq->q_lrpid = task_pid_vnr(msr->r_tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 msq->q_rtime = get_seconds();
652 wake_up_process(msr->r_tsk);
653 smp_mb();
654 msr->r_msg = msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return 1;
657 }
658 }
659 }
660 return 0;
661}
662
suzuki651971c2006-12-06 20:37:48 -0800663long do_msgsnd(int msqid, long mtype, void __user *mtext,
664 size_t msgsz, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 struct msg_queue *msq;
667 struct msg_msg *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 int err;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700669 struct ipc_namespace *ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700670
Kirill Korotaev1e786932006-10-02 02:18:21 -0700671 ns = current->nsproxy->ipc_ns;
672
673 if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (mtype < 1)
676 return -EINVAL;
677
suzuki651971c2006-12-06 20:37:48 -0800678 msg = load_msg(mtext, msgsz);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700679 if (IS_ERR(msg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return PTR_ERR(msg);
681
682 msg->m_type = mtype;
683 msg->m_ts = msgsz;
684
Nadia Derbey023a5352007-10-18 23:40:51 -0700685 msq = msg_lock_check(ns, msqid);
686 if (IS_ERR(msq)) {
687 err = PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 goto out_free;
Nadia Derbey023a5352007-10-18 23:40:51 -0700689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 for (;;) {
692 struct msg_sender s;
693
Ingo Molnar5a06a362006-07-30 03:04:11 -0700694 err = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (ipcperms(&msq->q_perm, S_IWUGO))
696 goto out_unlock_free;
697
698 err = security_msg_queue_msgsnd(msq, msg, msgflg);
699 if (err)
700 goto out_unlock_free;
701
Ingo Molnar5a06a362006-07-30 03:04:11 -0700702 if (msgsz + msq->q_cbytes <= msq->q_qbytes &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 1 + msq->q_qnum <= msq->q_qbytes) {
704 break;
705 }
706
707 /* queue full, wait: */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700708 if (msgflg & IPC_NOWAIT) {
709 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 goto out_unlock_free;
711 }
712 ss_add(msq, &s);
713 ipc_rcu_getref(msq);
714 msg_unlock(msq);
715 schedule();
716
717 ipc_lock_by_ptr(&msq->q_perm);
718 ipc_rcu_putref(msq);
719 if (msq->q_perm.deleted) {
720 err = -EIDRM;
721 goto out_unlock_free;
722 }
723 ss_del(&s);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (signal_pending(current)) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700726 err = -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 goto out_unlock_free;
728 }
729 }
730
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700731 msq->q_lspid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 msq->q_stime = get_seconds();
733
Ingo Molnar5a06a362006-07-30 03:04:11 -0700734 if (!pipelined_send(msq, msg)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 /* noone is waiting for this message, enqueue it */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700736 list_add_tail(&msg->m_list, &msq->q_messages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 msq->q_cbytes += msgsz;
738 msq->q_qnum++;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700739 atomic_add(msgsz, &msg_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 atomic_inc(&msg_hdrs);
741 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 err = 0;
744 msg = NULL;
745
746out_unlock_free:
747 msg_unlock(msq);
748out_free:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700749 if (msg != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 free_msg(msg);
751 return err;
752}
753
suzuki651971c2006-12-06 20:37:48 -0800754asmlinkage long
755sys_msgsnd(int msqid, struct msgbuf __user *msgp, size_t msgsz, int msgflg)
756{
757 long mtype;
758
759 if (get_user(mtype, &msgp->mtype))
760 return -EFAULT;
761 return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
762}
763
Ingo Molnar5a06a362006-07-30 03:04:11 -0700764static inline int convert_mode(long *msgtyp, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700766 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 * find message of correct type.
768 * msgtyp = 0 => get first.
769 * msgtyp > 0 => get first message of matching type.
Ingo Molnar5a06a362006-07-30 03:04:11 -0700770 * msgtyp < 0 => get message with least type must be < abs(msgtype).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700772 if (*msgtyp == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return SEARCH_ANY;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700774 if (*msgtyp < 0) {
775 *msgtyp = -*msgtyp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return SEARCH_LESSEQUAL;
777 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700778 if (msgflg & MSG_EXCEPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return SEARCH_NOTEQUAL;
780 return SEARCH_EQUAL;
781}
782
suzuki651971c2006-12-06 20:37:48 -0800783long do_msgrcv(int msqid, long *pmtype, void __user *mtext,
784 size_t msgsz, long msgtyp, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 struct msg_queue *msq;
787 struct msg_msg *msg;
788 int mode;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700789 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 if (msqid < 0 || (long) msgsz < 0)
792 return -EINVAL;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700793 mode = convert_mode(&msgtyp, msgflg);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700794 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Nadia Derbey023a5352007-10-18 23:40:51 -0700796 msq = msg_lock_check(ns, msqid);
797 if (IS_ERR(msq))
798 return PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 for (;;) {
801 struct msg_receiver msr_d;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700802 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 msg = ERR_PTR(-EACCES);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700805 if (ipcperms(&msq->q_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 goto out_unlock;
807
808 msg = ERR_PTR(-EAGAIN);
809 tmp = msq->q_messages.next;
810 while (tmp != &msq->q_messages) {
811 struct msg_msg *walk_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700812
813 walk_msg = list_entry(tmp, struct msg_msg, m_list);
814 if (testmsg(walk_msg, msgtyp, mode) &&
815 !security_msg_queue_msgrcv(msq, walk_msg, current,
816 msgtyp, mode)) {
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 msg = walk_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700819 if (mode == SEARCH_LESSEQUAL &&
820 walk_msg->m_type != 1) {
821 msg = walk_msg;
822 msgtyp = walk_msg->m_type - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 } else {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700824 msg = walk_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 break;
826 }
827 }
828 tmp = tmp->next;
829 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700830 if (!IS_ERR(msg)) {
831 /*
832 * Found a suitable message.
833 * Unlink it from the queue.
834 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
836 msg = ERR_PTR(-E2BIG);
837 goto out_unlock;
838 }
839 list_del(&msg->m_list);
840 msq->q_qnum--;
841 msq->q_rtime = get_seconds();
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700842 msq->q_lrpid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 msq->q_cbytes -= msg->m_ts;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700844 atomic_sub(msg->m_ts, &msg_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 atomic_dec(&msg_hdrs);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700846 ss_wakeup(&msq->q_senders, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 msg_unlock(msq);
848 break;
849 }
850 /* No message waiting. Wait for a message */
851 if (msgflg & IPC_NOWAIT) {
852 msg = ERR_PTR(-ENOMSG);
853 goto out_unlock;
854 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700855 list_add_tail(&msr_d.r_list, &msq->q_receivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 msr_d.r_tsk = current;
857 msr_d.r_msgtype = msgtyp;
858 msr_d.r_mode = mode;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700859 if (msgflg & MSG_NOERROR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 msr_d.r_maxsize = INT_MAX;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700861 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 msr_d.r_maxsize = msgsz;
863 msr_d.r_msg = ERR_PTR(-EAGAIN);
864 current->state = TASK_INTERRUPTIBLE;
865 msg_unlock(msq);
866
867 schedule();
868
869 /* Lockless receive, part 1:
870 * Disable preemption. We don't hold a reference to the queue
871 * and getting a reference would defeat the idea of a lockless
872 * operation, thus the code relies on rcu to guarantee the
873 * existance of msq:
874 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
875 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
876 * rcu_read_lock() prevents preemption between reading r_msg
877 * and the spin_lock() inside ipc_lock_by_ptr().
878 */
879 rcu_read_lock();
880
881 /* Lockless receive, part 2:
882 * Wait until pipelined_send or expunge_all are outside of
883 * wake_up_process(). There is a race with exit(), see
884 * ipc/mqueue.c for the details.
885 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700886 msg = (struct msg_msg*)msr_d.r_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 while (msg == NULL) {
888 cpu_relax();
Ingo Molnar5a06a362006-07-30 03:04:11 -0700889 msg = (struct msg_msg *)msr_d.r_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891
892 /* Lockless receive, part 3:
893 * If there is a message or an error then accept it without
894 * locking.
895 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700896 if (msg != ERR_PTR(-EAGAIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 rcu_read_unlock();
898 break;
899 }
900
901 /* Lockless receive, part 3:
902 * Acquire the queue spinlock.
903 */
904 ipc_lock_by_ptr(&msq->q_perm);
905 rcu_read_unlock();
906
907 /* Lockless receive, part 4:
908 * Repeat test after acquiring the spinlock.
909 */
910 msg = (struct msg_msg*)msr_d.r_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700911 if (msg != ERR_PTR(-EAGAIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 goto out_unlock;
913
914 list_del(&msr_d.r_list);
915 if (signal_pending(current)) {
916 msg = ERR_PTR(-ERESTARTNOHAND);
917out_unlock:
918 msg_unlock(msq);
919 break;
920 }
921 }
922 if (IS_ERR(msg))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700923 return PTR_ERR(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
suzuki651971c2006-12-06 20:37:48 -0800926 *pmtype = msg->m_type;
927 if (store_msg(mtext, msg, msgsz))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700928 msgsz = -EFAULT;
suzuki651971c2006-12-06 20:37:48 -0800929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 free_msg(msg);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return msgsz;
933}
934
suzuki651971c2006-12-06 20:37:48 -0800935asmlinkage long sys_msgrcv(int msqid, struct msgbuf __user *msgp, size_t msgsz,
936 long msgtyp, int msgflg)
937{
938 long err, mtype;
939
940 err = do_msgrcv(msqid, &mtype, msgp->mtext, msgsz, msgtyp, msgflg);
941 if (err < 0)
942 goto out;
943
944 if (put_user(mtype, &msgp->mtype))
945 err = -EFAULT;
946out:
947 return err;
948}
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -0700951static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Mike Waychison19b49462005-09-06 15:17:10 -0700953 struct msg_queue *msq = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Mike Waychison19b49462005-09-06 15:17:10 -0700955 return seq_printf(s,
Ingo Molnar5a06a362006-07-30 03:04:11 -0700956 "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
957 msq->q_perm.key,
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700958 msq->q_perm.id,
Ingo Molnar5a06a362006-07-30 03:04:11 -0700959 msq->q_perm.mode,
960 msq->q_cbytes,
961 msq->q_qnum,
962 msq->q_lspid,
963 msq->q_lrpid,
964 msq->q_perm.uid,
965 msq->q_perm.gid,
966 msq->q_perm.cuid,
967 msq->q_perm.cgid,
968 msq->q_stime,
969 msq->q_rtime,
970 msq->q_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972#endif