blob: 4f1f263935010cb2782a10a4311fc585305d89b8 [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)
Nadia Derbey1b531f22007-10-18 23:40:55 -070077#define msg_buildid(id, seq) ipc_buildid(id, seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Nadia Derbey7ca7e562007-10-18 23:40:48 -070079static void freeque(struct ipc_namespace *, struct msg_queue *);
Nadia Derbey7748dbf2007-10-18 23:40:49 -070080static int newque(struct ipc_namespace *, struct ipc_params *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -070082static int sysvipc_msg_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#endif
84
Cedric Le Goater7d69a1f2007-07-15 23:40:58 -070085static void __msg_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Kirill Korotaev1e786932006-10-02 02:18:21 -070087 ns->ids[IPC_MSG_IDS] = ids;
88 ns->msg_ctlmax = MSGMAX;
89 ns->msg_ctlmnb = MSGMNB;
90 ns->msg_ctlmni = MSGMNI;
Nadia Derbey7ca7e562007-10-18 23:40:48 -070091 ipc_init_ids(ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Kirill Korotaev1e786932006-10-02 02:18:21 -070094int msg_init_ns(struct ipc_namespace *ns)
95{
96 struct ipc_ids *ids;
97
98 ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
99 if (ids == NULL)
100 return -ENOMEM;
101
102 __msg_init_ns(ns, ids);
103 return 0;
104}
105
106void msg_exit_ns(struct ipc_namespace *ns)
107{
Kirill Korotaev1e786932006-10-02 02:18:21 -0700108 struct msg_queue *msq;
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++) {
117 msq = idr_find(&msg_ids(ns).ipcs_idr, next_id);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700118 if (msq == NULL)
119 continue;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700120 ipc_lock_by_ptr(&msq->q_perm);
121 freeque(ns, msq);
122 total++;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700123 }
Nadia Derbey3e148c72007-10-18 23:40:54 -0700124
125 up_write(&msg_ids(ns).rw_mutex);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700126
127 kfree(ns->ids[IPC_MSG_IDS]);
128 ns->ids[IPC_MSG_IDS] = NULL;
129}
Kirill Korotaev1e786932006-10-02 02:18:21 -0700130
131void __init msg_init(void)
132{
133 __msg_init_ns(&init_ipc_ns, &init_msg_ids);
134 ipc_init_proc_interface("sysvipc/msg",
135 " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
136 IPC_MSG_IDS, sysvipc_msg_proc_show);
137}
138
Nadia Derbey3e148c72007-10-18 23:40:54 -0700139/*
140 * This routine is called in the paths where the rw_mutex is held to protect
141 * access to the idr tree.
142 */
143static inline struct msg_queue *msg_lock_check_down(struct ipc_namespace *ns,
144 int id)
145{
146 struct kern_ipc_perm *ipcp = ipc_lock_check_down(&msg_ids(ns), id);
147
148 return container_of(ipcp, struct msg_queue, q_perm);
149}
150
151/*
152 * msg_lock_(check_) routines are called in the paths where the rw_mutex
153 * is not held.
154 */
Nadia Derbey023a5352007-10-18 23:40:51 -0700155static inline struct msg_queue *msg_lock(struct ipc_namespace *ns, int id)
156{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700157 struct kern_ipc_perm *ipcp = ipc_lock(&msg_ids(ns), id);
158
159 return container_of(ipcp, struct msg_queue, q_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700160}
161
162static inline struct msg_queue *msg_lock_check(struct ipc_namespace *ns,
163 int id)
164{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700165 struct kern_ipc_perm *ipcp = ipc_lock_check(&msg_ids(ns), id);
166
167 return container_of(ipcp, struct msg_queue, q_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700168}
169
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700170static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s)
171{
172 ipc_rmid(&msg_ids(ns), &s->q_perm);
173}
174
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700175/**
176 * newque - Create a new msg queue
177 * @ns: namespace
178 * @params: ptr to the structure that contains the key and msgflg
179 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700180 * Called with msg_ids.rw_mutex held (writer)
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700181 */
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700182static int newque(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 struct msg_queue *msq;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700185 int id, retval;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700186 key_t key = params->key;
187 int msgflg = params->flg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Ingo Molnar5a06a362006-07-30 03:04:11 -0700189 msq = ipc_rcu_alloc(sizeof(*msq));
190 if (!msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return -ENOMEM;
192
Ingo Molnar5a06a362006-07-30 03:04:11 -0700193 msq->q_perm.mode = msgflg & S_IRWXUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 msq->q_perm.key = key;
195
196 msq->q_perm.security = NULL;
197 retval = security_msg_queue_alloc(msq);
198 if (retval) {
199 ipc_rcu_putref(msq);
200 return retval;
201 }
202
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700203 /*
204 * ipc_addid() locks msq
205 */
Kirill Korotaev1e786932006-10-02 02:18:21 -0700206 id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700207 if (id == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 security_msg_queue_free(msq);
209 ipc_rcu_putref(msq);
210 return -ENOSPC;
211 }
212
Nadia Derbey1b531f22007-10-18 23:40:55 -0700213 msq->q_perm.id = msg_buildid(id, msq->q_perm.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 msq->q_stime = msq->q_rtime = 0;
215 msq->q_ctime = get_seconds();
216 msq->q_cbytes = msq->q_qnum = 0;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700217 msq->q_qbytes = ns->msg_ctlmnb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 msq->q_lspid = msq->q_lrpid = 0;
219 INIT_LIST_HEAD(&msq->q_messages);
220 INIT_LIST_HEAD(&msq->q_receivers);
221 INIT_LIST_HEAD(&msq->q_senders);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 msg_unlock(msq);
224
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700225 return msq->q_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Ingo Molnar5a06a362006-07-30 03:04:11 -0700228static inline void ss_add(struct msg_queue *msq, struct msg_sender *mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700230 mss->tsk = current;
231 current->state = TASK_INTERRUPTIBLE;
232 list_add_tail(&mss->list, &msq->q_senders);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Ingo Molnar5a06a362006-07-30 03:04:11 -0700235static inline void ss_del(struct msg_sender *mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700237 if (mss->list.next != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 list_del(&mss->list);
239}
240
Ingo Molnar5a06a362006-07-30 03:04:11 -0700241static void ss_wakeup(struct list_head *h, int kill)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
243 struct list_head *tmp;
244
245 tmp = h->next;
246 while (tmp != h) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700247 struct msg_sender *mss;
248
249 mss = list_entry(tmp, struct msg_sender, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 tmp = tmp->next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700251 if (kill)
252 mss->list.next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 wake_up_process(mss->tsk);
254 }
255}
256
Ingo Molnar5a06a362006-07-30 03:04:11 -0700257static void expunge_all(struct msg_queue *msq, int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 struct list_head *tmp;
260
261 tmp = msq->q_receivers.next;
262 while (tmp != &msq->q_receivers) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700263 struct msg_receiver *msr;
264
265 msr = list_entry(tmp, struct msg_receiver, r_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 tmp = tmp->next;
267 msr->r_msg = NULL;
268 wake_up_process(msr->r_tsk);
269 smp_mb();
270 msr->r_msg = ERR_PTR(res);
271 }
272}
Ingo Molnar5a06a362006-07-30 03:04:11 -0700273
274/*
275 * freeque() wakes up waiters on the sender and receiver waiting queue,
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700276 * removes the message queue from message queue ID IDR, and cleans up all the
277 * messages associated with this queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700279 * msg_ids.rw_mutex (writer) and the spinlock for this message queue are held
280 * before freeque() is called. msg_ids.rw_mutex remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 */
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700282static void freeque(struct ipc_namespace *ns, struct msg_queue *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 struct list_head *tmp;
285
Ingo Molnar5a06a362006-07-30 03:04:11 -0700286 expunge_all(msq, -EIDRM);
287 ss_wakeup(&msq->q_senders, 1);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700288 msg_rmid(ns, msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 msg_unlock(msq);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 tmp = msq->q_messages.next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700292 while (tmp != &msq->q_messages) {
293 struct msg_msg *msg = list_entry(tmp, struct msg_msg, m_list);
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 tmp = tmp->next;
296 atomic_dec(&msg_hdrs);
297 free_msg(msg);
298 }
299 atomic_sub(msq->q_cbytes, &msg_bytes);
300 security_msg_queue_free(msq);
301 ipc_rcu_putref(msq);
302}
303
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700304/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700305 * Called with msg_ids.rw_mutex and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700306 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700307static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700308{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700309 struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
310
311 return security_msg_queue_associate(msq, msgflg);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700312}
313
Ingo Molnar5a06a362006-07-30 03:04:11 -0700314asmlinkage long sys_msgget(key_t key, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Kirill Korotaev1e786932006-10-02 02:18:21 -0700316 struct ipc_namespace *ns;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700317 struct ipc_ops msg_ops;
318 struct ipc_params msg_params;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700319
320 ns = current->nsproxy->ipc_ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700321
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700322 msg_ops.getnew = newque;
323 msg_ops.associate = msg_security;
324 msg_ops.more_checks = NULL;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700325
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700326 msg_params.key = key;
327 msg_params.flg = msgflg;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700328
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700329 return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Ingo Molnar5a06a362006-07-30 03:04:11 -0700332static inline unsigned long
333copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 switch(version) {
336 case IPC_64:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700337 return copy_to_user(buf, in, sizeof(*in));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 case IPC_OLD:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700339 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct msqid_ds out;
341
Ingo Molnar5a06a362006-07-30 03:04:11 -0700342 memset(&out, 0, sizeof(out));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
345
346 out.msg_stime = in->msg_stime;
347 out.msg_rtime = in->msg_rtime;
348 out.msg_ctime = in->msg_ctime;
349
Ingo Molnar5a06a362006-07-30 03:04:11 -0700350 if (in->msg_cbytes > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 out.msg_cbytes = USHRT_MAX;
352 else
353 out.msg_cbytes = in->msg_cbytes;
354 out.msg_lcbytes = in->msg_cbytes;
355
Ingo Molnar5a06a362006-07-30 03:04:11 -0700356 if (in->msg_qnum > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 out.msg_qnum = USHRT_MAX;
358 else
359 out.msg_qnum = in->msg_qnum;
360
Ingo Molnar5a06a362006-07-30 03:04:11 -0700361 if (in->msg_qbytes > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 out.msg_qbytes = USHRT_MAX;
363 else
364 out.msg_qbytes = in->msg_qbytes;
365 out.msg_lqbytes = in->msg_qbytes;
366
367 out.msg_lspid = in->msg_lspid;
368 out.msg_lrpid = in->msg_lrpid;
369
Ingo Molnar5a06a362006-07-30 03:04:11 -0700370 return copy_to_user(buf, &out, sizeof(out));
371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 default:
373 return -EINVAL;
374 }
375}
376
377struct msq_setbuf {
378 unsigned long qbytes;
379 uid_t uid;
380 gid_t gid;
381 mode_t mode;
382};
383
Ingo Molnar5a06a362006-07-30 03:04:11 -0700384static inline unsigned long
385copy_msqid_from_user(struct msq_setbuf *out, void __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 switch(version) {
388 case IPC_64:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700389 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 struct msqid64_ds tbuf;
391
Ingo Molnar5a06a362006-07-30 03:04:11 -0700392 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return -EFAULT;
394
395 out->qbytes = tbuf.msg_qbytes;
396 out->uid = tbuf.msg_perm.uid;
397 out->gid = tbuf.msg_perm.gid;
398 out->mode = tbuf.msg_perm.mode;
399
400 return 0;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 case IPC_OLD:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700403 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 struct msqid_ds tbuf_old;
405
Ingo Molnar5a06a362006-07-30 03:04:11 -0700406 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return -EFAULT;
408
409 out->uid = tbuf_old.msg_perm.uid;
410 out->gid = tbuf_old.msg_perm.gid;
411 out->mode = tbuf_old.msg_perm.mode;
412
Ingo Molnar5a06a362006-07-30 03:04:11 -0700413 if (tbuf_old.msg_qbytes == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 out->qbytes = tbuf_old.msg_lqbytes;
415 else
416 out->qbytes = tbuf_old.msg_qbytes;
417
418 return 0;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 default:
421 return -EINVAL;
422 }
423}
424
Ingo Molnar5a06a362006-07-30 03:04:11 -0700425asmlinkage long sys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 struct kern_ipc_perm *ipcp;
Jeff Garzik8e1c0912007-07-17 05:40:59 -0400428 struct msq_setbuf uninitialized_var(setbuf);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700429 struct msg_queue *msq;
430 int err, version;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700431 struct ipc_namespace *ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (msqid < 0 || cmd < 0)
434 return -EINVAL;
435
436 version = ipc_parse_version(&cmd);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700437 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 switch (cmd) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700440 case IPC_INFO:
441 case MSG_INFO:
442 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 struct msginfo msginfo;
444 int max_id;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (!buf)
447 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700448 /*
449 * We must not return kernel stack data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 * due to padding, it's not enough
451 * to set all member fields.
452 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 err = security_msg_queue_msgctl(NULL, cmd);
454 if (err)
455 return err;
456
Ingo Molnar5a06a362006-07-30 03:04:11 -0700457 memset(&msginfo, 0, sizeof(msginfo));
Kirill Korotaev1e786932006-10-02 02:18:21 -0700458 msginfo.msgmni = ns->msg_ctlmni;
459 msginfo.msgmax = ns->msg_ctlmax;
460 msginfo.msgmnb = ns->msg_ctlmnb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 msginfo.msgssz = MSGSSZ;
462 msginfo.msgseg = MSGSEG;
Nadia Derbey3e148c72007-10-18 23:40:54 -0700463 down_read(&msg_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (cmd == MSG_INFO) {
Kirill Korotaev1e786932006-10-02 02:18:21 -0700465 msginfo.msgpool = msg_ids(ns).in_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 msginfo.msgmap = atomic_read(&msg_hdrs);
467 msginfo.msgtql = atomic_read(&msg_bytes);
468 } else {
469 msginfo.msgmap = MSGMAP;
470 msginfo.msgpool = MSGPOOL;
471 msginfo.msgtql = MSGTQL;
472 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700473 max_id = ipc_get_maxid(&msg_ids(ns));
Nadia Derbey3e148c72007-10-18 23:40:54 -0700474 up_read(&msg_ids(ns).rw_mutex);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700475 if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700477 return (max_id < 0) ? 0 : max_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700479 case MSG_STAT: /* msqid is an index rather than a msg queue id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 case IPC_STAT:
481 {
482 struct msqid64_ds tbuf;
483 int success_return;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (!buf)
486 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Ingo Molnar5a06a362006-07-30 03:04:11 -0700488 if (cmd == MSG_STAT) {
Nadia Derbey023a5352007-10-18 23:40:51 -0700489 msq = msg_lock(ns, msqid);
490 if (IS_ERR(msq))
491 return PTR_ERR(msq);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700492 success_return = msq->q_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 } else {
Nadia Derbey023a5352007-10-18 23:40:51 -0700494 msq = msg_lock_check(ns, msqid);
495 if (IS_ERR(msq))
496 return PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 success_return = 0;
498 }
499 err = -EACCES;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700500 if (ipcperms(&msq->q_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 goto out_unlock;
502
503 err = security_msg_queue_msgctl(msq, cmd);
504 if (err)
505 goto out_unlock;
506
Nadia Derbey023a5352007-10-18 23:40:51 -0700507 memset(&tbuf, 0, sizeof(tbuf));
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
510 tbuf.msg_stime = msq->q_stime;
511 tbuf.msg_rtime = msq->q_rtime;
512 tbuf.msg_ctime = msq->q_ctime;
513 tbuf.msg_cbytes = msq->q_cbytes;
514 tbuf.msg_qnum = msq->q_qnum;
515 tbuf.msg_qbytes = msq->q_qbytes;
516 tbuf.msg_lspid = msq->q_lspid;
517 tbuf.msg_lrpid = msq->q_lrpid;
518 msg_unlock(msq);
519 if (copy_msqid_to_user(buf, &tbuf, version))
520 return -EFAULT;
521 return success_return;
522 }
523 case IPC_SET:
524 if (!buf)
525 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700526 if (copy_msqid_from_user(&setbuf, buf, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 break;
529 case IPC_RMID:
530 break;
531 default:
532 return -EINVAL;
533 }
534
Nadia Derbey3e148c72007-10-18 23:40:54 -0700535 down_write(&msg_ids(ns).rw_mutex);
536 msq = msg_lock_check_down(ns, msqid);
Nadia Derbey023a5352007-10-18 23:40:51 -0700537 if (IS_ERR(msq)) {
538 err = PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 goto out_up;
Nadia Derbey023a5352007-10-18 23:40:51 -0700540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 ipcp = &msq->q_perm;
Steve Grubb073115d2006-04-02 17:07:33 -0400543
544 err = audit_ipc_obj(ipcp);
545 if (err)
546 goto out_unlock_up;
Jeff Garzik8e1c0912007-07-17 05:40:59 -0400547 if (cmd == IPC_SET) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700548 err = audit_ipc_set_perm(setbuf.qbytes, setbuf.uid, setbuf.gid,
549 setbuf.mode);
Linda Knippersac032212006-05-16 22:03:48 -0400550 if (err)
551 goto out_unlock_up;
552 }
Steve Grubb073115d2006-04-02 17:07:33 -0400553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 err = -EPERM;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700555 if (current->euid != ipcp->cuid &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700557 /* We _could_ check for CAP_CHOWN above, but we don't */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 goto out_unlock_up;
559
560 err = security_msg_queue_msgctl(msq, cmd);
561 if (err)
562 goto out_unlock_up;
563
564 switch (cmd) {
565 case IPC_SET:
566 {
567 err = -EPERM;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700568 if (setbuf.qbytes > ns->msg_ctlmnb && !capable(CAP_SYS_RESOURCE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 goto out_unlock_up;
570
571 msq->q_qbytes = setbuf.qbytes;
572
573 ipcp->uid = setbuf.uid;
574 ipcp->gid = setbuf.gid;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700575 ipcp->mode = (ipcp->mode & ~S_IRWXUGO) |
576 (S_IRWXUGO & setbuf.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 msq->q_ctime = get_seconds();
578 /* sleeping receivers might be excluded by
579 * stricter permissions.
580 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700581 expunge_all(msq, -EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /* sleeping senders might be able to send
583 * due to a larger queue size.
584 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700585 ss_wakeup(&msq->q_senders, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 msg_unlock(msq);
587 break;
588 }
589 case IPC_RMID:
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700590 freeque(ns, msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 break;
592 }
593 err = 0;
594out_up:
Nadia Derbey3e148c72007-10-18 23:40:54 -0700595 up_write(&msg_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return err;
597out_unlock_up:
598 msg_unlock(msq);
599 goto out_up;
600out_unlock:
601 msg_unlock(msq);
602 return err;
603}
604
Ingo Molnar5a06a362006-07-30 03:04:11 -0700605static int testmsg(struct msg_msg *msg, long type, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 switch(mode)
608 {
609 case SEARCH_ANY:
610 return 1;
611 case SEARCH_LESSEQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700612 if (msg->m_type <=type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return 1;
614 break;
615 case SEARCH_EQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700616 if (msg->m_type == type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return 1;
618 break;
619 case SEARCH_NOTEQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700620 if (msg->m_type != type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return 1;
622 break;
623 }
624 return 0;
625}
626
Ingo Molnar5a06a362006-07-30 03:04:11 -0700627static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700629 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 tmp = msq->q_receivers.next;
632 while (tmp != &msq->q_receivers) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700633 struct msg_receiver *msr;
634
635 msr = list_entry(tmp, struct msg_receiver, r_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 tmp = tmp->next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700637 if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
638 !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
639 msr->r_msgtype, msr->r_mode)) {
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 list_del(&msr->r_list);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700642 if (msr->r_maxsize < msg->m_ts) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 msr->r_msg = NULL;
644 wake_up_process(msr->r_tsk);
645 smp_mb();
646 msr->r_msg = ERR_PTR(-E2BIG);
647 } else {
648 msr->r_msg = NULL;
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700649 msq->q_lrpid = task_pid_vnr(msr->r_tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 msq->q_rtime = get_seconds();
651 wake_up_process(msr->r_tsk);
652 smp_mb();
653 msr->r_msg = msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return 1;
656 }
657 }
658 }
659 return 0;
660}
661
suzuki651971c2006-12-06 20:37:48 -0800662long do_msgsnd(int msqid, long mtype, void __user *mtext,
663 size_t msgsz, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 struct msg_queue *msq;
666 struct msg_msg *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 int err;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700668 struct ipc_namespace *ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700669
Kirill Korotaev1e786932006-10-02 02:18:21 -0700670 ns = current->nsproxy->ipc_ns;
671
672 if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (mtype < 1)
675 return -EINVAL;
676
suzuki651971c2006-12-06 20:37:48 -0800677 msg = load_msg(mtext, msgsz);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700678 if (IS_ERR(msg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return PTR_ERR(msg);
680
681 msg->m_type = mtype;
682 msg->m_ts = msgsz;
683
Nadia Derbey023a5352007-10-18 23:40:51 -0700684 msq = msg_lock_check(ns, msqid);
685 if (IS_ERR(msq)) {
686 err = PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 goto out_free;
Nadia Derbey023a5352007-10-18 23:40:51 -0700688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 for (;;) {
691 struct msg_sender s;
692
Ingo Molnar5a06a362006-07-30 03:04:11 -0700693 err = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (ipcperms(&msq->q_perm, S_IWUGO))
695 goto out_unlock_free;
696
697 err = security_msg_queue_msgsnd(msq, msg, msgflg);
698 if (err)
699 goto out_unlock_free;
700
Ingo Molnar5a06a362006-07-30 03:04:11 -0700701 if (msgsz + msq->q_cbytes <= msq->q_qbytes &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 1 + msq->q_qnum <= msq->q_qbytes) {
703 break;
704 }
705
706 /* queue full, wait: */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700707 if (msgflg & IPC_NOWAIT) {
708 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 goto out_unlock_free;
710 }
711 ss_add(msq, &s);
712 ipc_rcu_getref(msq);
713 msg_unlock(msq);
714 schedule();
715
716 ipc_lock_by_ptr(&msq->q_perm);
717 ipc_rcu_putref(msq);
718 if (msq->q_perm.deleted) {
719 err = -EIDRM;
720 goto out_unlock_free;
721 }
722 ss_del(&s);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (signal_pending(current)) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700725 err = -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 goto out_unlock_free;
727 }
728 }
729
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700730 msq->q_lspid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 msq->q_stime = get_seconds();
732
Ingo Molnar5a06a362006-07-30 03:04:11 -0700733 if (!pipelined_send(msq, msg)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 /* noone is waiting for this message, enqueue it */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700735 list_add_tail(&msg->m_list, &msq->q_messages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 msq->q_cbytes += msgsz;
737 msq->q_qnum++;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700738 atomic_add(msgsz, &msg_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 atomic_inc(&msg_hdrs);
740 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 err = 0;
743 msg = NULL;
744
745out_unlock_free:
746 msg_unlock(msq);
747out_free:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700748 if (msg != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 free_msg(msg);
750 return err;
751}
752
suzuki651971c2006-12-06 20:37:48 -0800753asmlinkage long
754sys_msgsnd(int msqid, struct msgbuf __user *msgp, size_t msgsz, int msgflg)
755{
756 long mtype;
757
758 if (get_user(mtype, &msgp->mtype))
759 return -EFAULT;
760 return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
761}
762
Ingo Molnar5a06a362006-07-30 03:04:11 -0700763static inline int convert_mode(long *msgtyp, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700765 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 * find message of correct type.
767 * msgtyp = 0 => get first.
768 * msgtyp > 0 => get first message of matching type.
Ingo Molnar5a06a362006-07-30 03:04:11 -0700769 * msgtyp < 0 => get message with least type must be < abs(msgtype).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700771 if (*msgtyp == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return SEARCH_ANY;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700773 if (*msgtyp < 0) {
774 *msgtyp = -*msgtyp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return SEARCH_LESSEQUAL;
776 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700777 if (msgflg & MSG_EXCEPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return SEARCH_NOTEQUAL;
779 return SEARCH_EQUAL;
780}
781
suzuki651971c2006-12-06 20:37:48 -0800782long do_msgrcv(int msqid, long *pmtype, void __user *mtext,
783 size_t msgsz, long msgtyp, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 struct msg_queue *msq;
786 struct msg_msg *msg;
787 int mode;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700788 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 if (msqid < 0 || (long) msgsz < 0)
791 return -EINVAL;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700792 mode = convert_mode(&msgtyp, msgflg);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700793 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Nadia Derbey023a5352007-10-18 23:40:51 -0700795 msq = msg_lock_check(ns, msqid);
796 if (IS_ERR(msq))
797 return PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 for (;;) {
800 struct msg_receiver msr_d;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700801 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 msg = ERR_PTR(-EACCES);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700804 if (ipcperms(&msq->q_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 goto out_unlock;
806
807 msg = ERR_PTR(-EAGAIN);
808 tmp = msq->q_messages.next;
809 while (tmp != &msq->q_messages) {
810 struct msg_msg *walk_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700811
812 walk_msg = list_entry(tmp, struct msg_msg, m_list);
813 if (testmsg(walk_msg, msgtyp, mode) &&
814 !security_msg_queue_msgrcv(msq, walk_msg, current,
815 msgtyp, mode)) {
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 msg = walk_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700818 if (mode == SEARCH_LESSEQUAL &&
819 walk_msg->m_type != 1) {
820 msg = walk_msg;
821 msgtyp = walk_msg->m_type - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 } else {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700823 msg = walk_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 break;
825 }
826 }
827 tmp = tmp->next;
828 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700829 if (!IS_ERR(msg)) {
830 /*
831 * Found a suitable message.
832 * Unlink it from the queue.
833 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
835 msg = ERR_PTR(-E2BIG);
836 goto out_unlock;
837 }
838 list_del(&msg->m_list);
839 msq->q_qnum--;
840 msq->q_rtime = get_seconds();
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700841 msq->q_lrpid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 msq->q_cbytes -= msg->m_ts;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700843 atomic_sub(msg->m_ts, &msg_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 atomic_dec(&msg_hdrs);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700845 ss_wakeup(&msq->q_senders, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 msg_unlock(msq);
847 break;
848 }
849 /* No message waiting. Wait for a message */
850 if (msgflg & IPC_NOWAIT) {
851 msg = ERR_PTR(-ENOMSG);
852 goto out_unlock;
853 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700854 list_add_tail(&msr_d.r_list, &msq->q_receivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 msr_d.r_tsk = current;
856 msr_d.r_msgtype = msgtyp;
857 msr_d.r_mode = mode;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700858 if (msgflg & MSG_NOERROR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 msr_d.r_maxsize = INT_MAX;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700860 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 msr_d.r_maxsize = msgsz;
862 msr_d.r_msg = ERR_PTR(-EAGAIN);
863 current->state = TASK_INTERRUPTIBLE;
864 msg_unlock(msq);
865
866 schedule();
867
868 /* Lockless receive, part 1:
869 * Disable preemption. We don't hold a reference to the queue
870 * and getting a reference would defeat the idea of a lockless
871 * operation, thus the code relies on rcu to guarantee the
872 * existance of msq:
873 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
874 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
875 * rcu_read_lock() prevents preemption between reading r_msg
876 * and the spin_lock() inside ipc_lock_by_ptr().
877 */
878 rcu_read_lock();
879
880 /* Lockless receive, part 2:
881 * Wait until pipelined_send or expunge_all are outside of
882 * wake_up_process(). There is a race with exit(), see
883 * ipc/mqueue.c for the details.
884 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700885 msg = (struct msg_msg*)msr_d.r_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 while (msg == NULL) {
887 cpu_relax();
Ingo Molnar5a06a362006-07-30 03:04:11 -0700888 msg = (struct msg_msg *)msr_d.r_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
890
891 /* Lockless receive, part 3:
892 * If there is a message or an error then accept it without
893 * locking.
894 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700895 if (msg != ERR_PTR(-EAGAIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 rcu_read_unlock();
897 break;
898 }
899
900 /* Lockless receive, part 3:
901 * Acquire the queue spinlock.
902 */
903 ipc_lock_by_ptr(&msq->q_perm);
904 rcu_read_unlock();
905
906 /* Lockless receive, part 4:
907 * Repeat test after acquiring the spinlock.
908 */
909 msg = (struct msg_msg*)msr_d.r_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700910 if (msg != ERR_PTR(-EAGAIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 goto out_unlock;
912
913 list_del(&msr_d.r_list);
914 if (signal_pending(current)) {
915 msg = ERR_PTR(-ERESTARTNOHAND);
916out_unlock:
917 msg_unlock(msq);
918 break;
919 }
920 }
921 if (IS_ERR(msg))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700922 return PTR_ERR(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
suzuki651971c2006-12-06 20:37:48 -0800925 *pmtype = msg->m_type;
926 if (store_msg(mtext, msg, msgsz))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700927 msgsz = -EFAULT;
suzuki651971c2006-12-06 20:37:48 -0800928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 free_msg(msg);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return msgsz;
932}
933
suzuki651971c2006-12-06 20:37:48 -0800934asmlinkage long sys_msgrcv(int msqid, struct msgbuf __user *msgp, size_t msgsz,
935 long msgtyp, int msgflg)
936{
937 long err, mtype;
938
939 err = do_msgrcv(msqid, &mtype, msgp->mtext, msgsz, msgtyp, msgflg);
940 if (err < 0)
941 goto out;
942
943 if (put_user(mtype, &msgp->mtype))
944 err = -EFAULT;
945out:
946 return err;
947}
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -0700950static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Mike Waychison19b49462005-09-06 15:17:10 -0700952 struct msg_queue *msq = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Mike Waychison19b49462005-09-06 15:17:10 -0700954 return seq_printf(s,
Ingo Molnar5a06a362006-07-30 03:04:11 -0700955 "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
956 msq->q_perm.key,
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700957 msq->q_perm.id,
Ingo Molnar5a06a362006-07-30 03:04:11 -0700958 msq->q_perm.mode,
959 msq->q_cbytes,
960 msq->q_qnum,
961 msq->q_lspid,
962 msq->q_lrpid,
963 msq->q_perm.uid,
964 msq->q_perm.gid,
965 msq->q_perm.cuid,
966 msq->q_perm.cgid,
967 msq->q_stime,
968 msq->q_rtime,
969 msq->q_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971#endif