blob: 5879bfeb79ca77527b48b1e074ebe2aad0e2b8fa [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
Kirill Korotaev1e786932006-10-02 02:18:21 -070070static struct ipc_ids init_msg_ids;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Kirill Korotaev1e786932006-10-02 02:18:21 -070072#define msg_ids(ns) (*((ns)->ids[IPC_MSG_IDS]))
73
Ingo Molnar5a06a362006-07-30 03:04:11 -070074#define msg_unlock(msq) ipc_unlock(&(msq)->q_perm)
Nadia Derbey1b531f22007-10-18 23:40:55 -070075#define msg_buildid(id, seq) ipc_buildid(id, seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Nadia Derbey7ca7e562007-10-18 23:40:48 -070077static void freeque(struct ipc_namespace *, struct msg_queue *);
Nadia Derbey7748dbf2007-10-18 23:40:49 -070078static int newque(struct ipc_namespace *, struct ipc_params *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -070080static int sysvipc_msg_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#endif
82
Cedric Le Goater7d69a1f2007-07-15 23:40:58 -070083static void __msg_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Kirill Korotaev1e786932006-10-02 02:18:21 -070085 ns->ids[IPC_MSG_IDS] = ids;
86 ns->msg_ctlmax = MSGMAX;
87 ns->msg_ctlmnb = MSGMNB;
88 ns->msg_ctlmni = MSGMNI;
Kirill Korotaev3ac88a42007-10-18 23:40:56 -070089 atomic_set(&ns->msg_bytes, 0);
90 atomic_set(&ns->msg_hdrs, 0);
Nadia Derbey7ca7e562007-10-18 23:40:48 -070091 ipc_init_ids(ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080094#ifdef CONFIG_IPC_NS
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;
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800110 struct kern_ipc_perm *perm;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700111 int next_id;
112 int total, in_use;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700113
Nadia Derbey3e148c72007-10-18 23:40:54 -0700114 down_write(&msg_ids(ns).rw_mutex);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700115
116 in_use = msg_ids(ns).in_use;
117
118 for (total = 0, next_id = 0; total < in_use; next_id++) {
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800119 perm = idr_find(&msg_ids(ns).ipcs_idr, next_id);
120 if (perm == NULL)
Kirill Korotaev1e786932006-10-02 02:18:21 -0700121 continue;
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800122 ipc_lock_by_ptr(perm);
123 msq = container_of(perm, struct msg_queue, q_perm);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700124 freeque(ns, msq);
125 total++;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700126 }
Nadia Derbey3e148c72007-10-18 23:40:54 -0700127
128 up_write(&msg_ids(ns).rw_mutex);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700129
130 kfree(ns->ids[IPC_MSG_IDS]);
131 ns->ids[IPC_MSG_IDS] = NULL;
132}
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800133#endif
Kirill Korotaev1e786932006-10-02 02:18:21 -0700134
135void __init msg_init(void)
136{
137 __msg_init_ns(&init_ipc_ns, &init_msg_ids);
138 ipc_init_proc_interface("sysvipc/msg",
139 " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
140 IPC_MSG_IDS, sysvipc_msg_proc_show);
141}
142
Nadia Derbey3e148c72007-10-18 23:40:54 -0700143/*
144 * This routine is called in the paths where the rw_mutex is held to protect
145 * access to the idr tree.
146 */
147static inline struct msg_queue *msg_lock_check_down(struct ipc_namespace *ns,
148 int id)
149{
150 struct kern_ipc_perm *ipcp = ipc_lock_check_down(&msg_ids(ns), id);
151
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800152 if (IS_ERR(ipcp))
153 return (struct msg_queue *)ipcp;
154
Nadia Derbey3e148c72007-10-18 23:40:54 -0700155 return container_of(ipcp, struct msg_queue, q_perm);
156}
157
158/*
159 * msg_lock_(check_) routines are called in the paths where the rw_mutex
160 * is not held.
161 */
Nadia Derbey023a5352007-10-18 23:40:51 -0700162static inline struct msg_queue *msg_lock(struct ipc_namespace *ns, int id)
163{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700164 struct kern_ipc_perm *ipcp = ipc_lock(&msg_ids(ns), id);
165
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800166 if (IS_ERR(ipcp))
167 return (struct msg_queue *)ipcp;
168
Nadia Derbey03f02c72007-10-18 23:40:51 -0700169 return container_of(ipcp, struct msg_queue, q_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700170}
171
172static inline struct msg_queue *msg_lock_check(struct ipc_namespace *ns,
173 int id)
174{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700175 struct kern_ipc_perm *ipcp = ipc_lock_check(&msg_ids(ns), id);
176
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800177 if (IS_ERR(ipcp))
178 return (struct msg_queue *)ipcp;
179
Nadia Derbey03f02c72007-10-18 23:40:51 -0700180 return container_of(ipcp, struct msg_queue, q_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700181}
182
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700183static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s)
184{
185 ipc_rmid(&msg_ids(ns), &s->q_perm);
186}
187
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700188/**
189 * newque - Create a new msg queue
190 * @ns: namespace
191 * @params: ptr to the structure that contains the key and msgflg
192 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700193 * Called with msg_ids.rw_mutex held (writer)
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700194 */
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700195static int newque(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 struct msg_queue *msq;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700198 int id, retval;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700199 key_t key = params->key;
200 int msgflg = params->flg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Ingo Molnar5a06a362006-07-30 03:04:11 -0700202 msq = ipc_rcu_alloc(sizeof(*msq));
203 if (!msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return -ENOMEM;
205
Ingo Molnar5a06a362006-07-30 03:04:11 -0700206 msq->q_perm.mode = msgflg & S_IRWXUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 msq->q_perm.key = key;
208
209 msq->q_perm.security = NULL;
210 retval = security_msg_queue_alloc(msq);
211 if (retval) {
212 ipc_rcu_putref(msq);
213 return retval;
214 }
215
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700216 /*
217 * ipc_addid() locks msq
218 */
Kirill Korotaev1e786932006-10-02 02:18:21 -0700219 id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700220 if (id < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 security_msg_queue_free(msq);
222 ipc_rcu_putref(msq);
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700223 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225
Nadia Derbey1b531f22007-10-18 23:40:55 -0700226 msq->q_perm.id = msg_buildid(id, msq->q_perm.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 msq->q_stime = msq->q_rtime = 0;
228 msq->q_ctime = get_seconds();
229 msq->q_cbytes = msq->q_qnum = 0;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700230 msq->q_qbytes = ns->msg_ctlmnb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 msq->q_lspid = msq->q_lrpid = 0;
232 INIT_LIST_HEAD(&msq->q_messages);
233 INIT_LIST_HEAD(&msq->q_receivers);
234 INIT_LIST_HEAD(&msq->q_senders);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 msg_unlock(msq);
237
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700238 return msq->q_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
Ingo Molnar5a06a362006-07-30 03:04:11 -0700241static inline void ss_add(struct msg_queue *msq, struct msg_sender *mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700243 mss->tsk = current;
244 current->state = TASK_INTERRUPTIBLE;
245 list_add_tail(&mss->list, &msq->q_senders);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
Ingo Molnar5a06a362006-07-30 03:04:11 -0700248static inline void ss_del(struct msg_sender *mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700250 if (mss->list.next != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 list_del(&mss->list);
252}
253
Ingo Molnar5a06a362006-07-30 03:04:11 -0700254static void ss_wakeup(struct list_head *h, int kill)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 struct list_head *tmp;
257
258 tmp = h->next;
259 while (tmp != h) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700260 struct msg_sender *mss;
261
262 mss = list_entry(tmp, struct msg_sender, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 tmp = tmp->next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700264 if (kill)
265 mss->list.next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 wake_up_process(mss->tsk);
267 }
268}
269
Ingo Molnar5a06a362006-07-30 03:04:11 -0700270static void expunge_all(struct msg_queue *msq, int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 struct list_head *tmp;
273
274 tmp = msq->q_receivers.next;
275 while (tmp != &msq->q_receivers) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700276 struct msg_receiver *msr;
277
278 msr = list_entry(tmp, struct msg_receiver, r_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 tmp = tmp->next;
280 msr->r_msg = NULL;
281 wake_up_process(msr->r_tsk);
282 smp_mb();
283 msr->r_msg = ERR_PTR(res);
284 }
285}
Ingo Molnar5a06a362006-07-30 03:04:11 -0700286
287/*
288 * freeque() wakes up waiters on the sender and receiver waiting queue,
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700289 * removes the message queue from message queue ID IDR, and cleans up all the
290 * messages associated with this queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700292 * msg_ids.rw_mutex (writer) and the spinlock for this message queue are held
293 * before freeque() is called. msg_ids.rw_mutex remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 */
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700295static void freeque(struct ipc_namespace *ns, struct msg_queue *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 struct list_head *tmp;
298
Ingo Molnar5a06a362006-07-30 03:04:11 -0700299 expunge_all(msq, -EIDRM);
300 ss_wakeup(&msq->q_senders, 1);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700301 msg_rmid(ns, msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 msg_unlock(msq);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 tmp = msq->q_messages.next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700305 while (tmp != &msq->q_messages) {
306 struct msg_msg *msg = list_entry(tmp, struct msg_msg, m_list);
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 tmp = tmp->next;
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700309 atomic_dec(&ns->msg_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 free_msg(msg);
311 }
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700312 atomic_sub(msq->q_cbytes, &ns->msg_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 security_msg_queue_free(msq);
314 ipc_rcu_putref(msq);
315}
316
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700317/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700318 * Called with msg_ids.rw_mutex and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700319 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700320static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700321{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700322 struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
323
324 return security_msg_queue_associate(msq, msgflg);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700325}
326
Ingo Molnar5a06a362006-07-30 03:04:11 -0700327asmlinkage long sys_msgget(key_t key, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Kirill Korotaev1e786932006-10-02 02:18:21 -0700329 struct ipc_namespace *ns;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700330 struct ipc_ops msg_ops;
331 struct ipc_params msg_params;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700332
333 ns = current->nsproxy->ipc_ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700334
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700335 msg_ops.getnew = newque;
336 msg_ops.associate = msg_security;
337 msg_ops.more_checks = NULL;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700338
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700339 msg_params.key = key;
340 msg_params.flg = msgflg;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700341
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700342 return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Ingo Molnar5a06a362006-07-30 03:04:11 -0700345static inline unsigned long
346copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 switch(version) {
349 case IPC_64:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700350 return copy_to_user(buf, in, sizeof(*in));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 case IPC_OLD:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700352 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 struct msqid_ds out;
354
Ingo Molnar5a06a362006-07-30 03:04:11 -0700355 memset(&out, 0, sizeof(out));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
358
359 out.msg_stime = in->msg_stime;
360 out.msg_rtime = in->msg_rtime;
361 out.msg_ctime = in->msg_ctime;
362
Ingo Molnar5a06a362006-07-30 03:04:11 -0700363 if (in->msg_cbytes > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 out.msg_cbytes = USHRT_MAX;
365 else
366 out.msg_cbytes = in->msg_cbytes;
367 out.msg_lcbytes = in->msg_cbytes;
368
Ingo Molnar5a06a362006-07-30 03:04:11 -0700369 if (in->msg_qnum > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 out.msg_qnum = USHRT_MAX;
371 else
372 out.msg_qnum = in->msg_qnum;
373
Ingo Molnar5a06a362006-07-30 03:04:11 -0700374 if (in->msg_qbytes > USHRT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 out.msg_qbytes = USHRT_MAX;
376 else
377 out.msg_qbytes = in->msg_qbytes;
378 out.msg_lqbytes = in->msg_qbytes;
379
380 out.msg_lspid = in->msg_lspid;
381 out.msg_lrpid = in->msg_lrpid;
382
Ingo Molnar5a06a362006-07-30 03:04:11 -0700383 return copy_to_user(buf, &out, sizeof(out));
384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 default:
386 return -EINVAL;
387 }
388}
389
390struct msq_setbuf {
391 unsigned long qbytes;
392 uid_t uid;
393 gid_t gid;
394 mode_t mode;
395};
396
Ingo Molnar5a06a362006-07-30 03:04:11 -0700397static inline unsigned long
398copy_msqid_from_user(struct msq_setbuf *out, void __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 switch(version) {
401 case IPC_64:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700402 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 struct msqid64_ds tbuf;
404
Ingo Molnar5a06a362006-07-30 03:04:11 -0700405 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return -EFAULT;
407
408 out->qbytes = tbuf.msg_qbytes;
409 out->uid = tbuf.msg_perm.uid;
410 out->gid = tbuf.msg_perm.gid;
411 out->mode = tbuf.msg_perm.mode;
412
413 return 0;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 case IPC_OLD:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700416 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 struct msqid_ds tbuf_old;
418
Ingo Molnar5a06a362006-07-30 03:04:11 -0700419 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return -EFAULT;
421
422 out->uid = tbuf_old.msg_perm.uid;
423 out->gid = tbuf_old.msg_perm.gid;
424 out->mode = tbuf_old.msg_perm.mode;
425
Ingo Molnar5a06a362006-07-30 03:04:11 -0700426 if (tbuf_old.msg_qbytes == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 out->qbytes = tbuf_old.msg_lqbytes;
428 else
429 out->qbytes = tbuf_old.msg_qbytes;
430
431 return 0;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 default:
434 return -EINVAL;
435 }
436}
437
Ingo Molnar5a06a362006-07-30 03:04:11 -0700438asmlinkage long sys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 struct kern_ipc_perm *ipcp;
Jeff Garzik8e1c0912007-07-17 05:40:59 -0400441 struct msq_setbuf uninitialized_var(setbuf);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700442 struct msg_queue *msq;
443 int err, version;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700444 struct ipc_namespace *ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (msqid < 0 || cmd < 0)
447 return -EINVAL;
448
449 version = ipc_parse_version(&cmd);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700450 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 switch (cmd) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700453 case IPC_INFO:
454 case MSG_INFO:
455 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 struct msginfo msginfo;
457 int max_id;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (!buf)
460 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700461 /*
462 * We must not return kernel stack data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 * due to padding, it's not enough
464 * to set all member fields.
465 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 err = security_msg_queue_msgctl(NULL, cmd);
467 if (err)
468 return err;
469
Ingo Molnar5a06a362006-07-30 03:04:11 -0700470 memset(&msginfo, 0, sizeof(msginfo));
Kirill Korotaev1e786932006-10-02 02:18:21 -0700471 msginfo.msgmni = ns->msg_ctlmni;
472 msginfo.msgmax = ns->msg_ctlmax;
473 msginfo.msgmnb = ns->msg_ctlmnb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 msginfo.msgssz = MSGSSZ;
475 msginfo.msgseg = MSGSEG;
Nadia Derbey3e148c72007-10-18 23:40:54 -0700476 down_read(&msg_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (cmd == MSG_INFO) {
Kirill Korotaev1e786932006-10-02 02:18:21 -0700478 msginfo.msgpool = msg_ids(ns).in_use;
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700479 msginfo.msgmap = atomic_read(&ns->msg_hdrs);
480 msginfo.msgtql = atomic_read(&ns->msg_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 } else {
482 msginfo.msgmap = MSGMAP;
483 msginfo.msgpool = MSGPOOL;
484 msginfo.msgtql = MSGTQL;
485 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700486 max_id = ipc_get_maxid(&msg_ids(ns));
Nadia Derbey3e148c72007-10-18 23:40:54 -0700487 up_read(&msg_ids(ns).rw_mutex);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700488 if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700490 return (max_id < 0) ? 0 : max_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700492 case MSG_STAT: /* msqid is an index rather than a msg queue id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 case IPC_STAT:
494 {
495 struct msqid64_ds tbuf;
496 int success_return;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (!buf)
499 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Ingo Molnar5a06a362006-07-30 03:04:11 -0700501 if (cmd == MSG_STAT) {
Nadia Derbey023a5352007-10-18 23:40:51 -0700502 msq = msg_lock(ns, msqid);
503 if (IS_ERR(msq))
504 return PTR_ERR(msq);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700505 success_return = msq->q_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 } else {
Nadia Derbey023a5352007-10-18 23:40:51 -0700507 msq = msg_lock_check(ns, msqid);
508 if (IS_ERR(msq))
509 return PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 success_return = 0;
511 }
512 err = -EACCES;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700513 if (ipcperms(&msq->q_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 goto out_unlock;
515
516 err = security_msg_queue_msgctl(msq, cmd);
517 if (err)
518 goto out_unlock;
519
Nadia Derbey023a5352007-10-18 23:40:51 -0700520 memset(&tbuf, 0, sizeof(tbuf));
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
523 tbuf.msg_stime = msq->q_stime;
524 tbuf.msg_rtime = msq->q_rtime;
525 tbuf.msg_ctime = msq->q_ctime;
526 tbuf.msg_cbytes = msq->q_cbytes;
527 tbuf.msg_qnum = msq->q_qnum;
528 tbuf.msg_qbytes = msq->q_qbytes;
529 tbuf.msg_lspid = msq->q_lspid;
530 tbuf.msg_lrpid = msq->q_lrpid;
531 msg_unlock(msq);
532 if (copy_msqid_to_user(buf, &tbuf, version))
533 return -EFAULT;
534 return success_return;
535 }
536 case IPC_SET:
537 if (!buf)
538 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700539 if (copy_msqid_from_user(&setbuf, buf, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 break;
542 case IPC_RMID:
543 break;
544 default:
545 return -EINVAL;
546 }
547
Nadia Derbey3e148c72007-10-18 23:40:54 -0700548 down_write(&msg_ids(ns).rw_mutex);
549 msq = msg_lock_check_down(ns, msqid);
Nadia Derbey023a5352007-10-18 23:40:51 -0700550 if (IS_ERR(msq)) {
551 err = PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 goto out_up;
Nadia Derbey023a5352007-10-18 23:40:51 -0700553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 ipcp = &msq->q_perm;
Steve Grubb073115d2006-04-02 17:07:33 -0400556
557 err = audit_ipc_obj(ipcp);
558 if (err)
559 goto out_unlock_up;
Jeff Garzik8e1c0912007-07-17 05:40:59 -0400560 if (cmd == IPC_SET) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700561 err = audit_ipc_set_perm(setbuf.qbytes, setbuf.uid, setbuf.gid,
562 setbuf.mode);
Linda Knippersac032212006-05-16 22:03:48 -0400563 if (err)
564 goto out_unlock_up;
565 }
Steve Grubb073115d2006-04-02 17:07:33 -0400566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 err = -EPERM;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700568 if (current->euid != ipcp->cuid &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700570 /* We _could_ check for CAP_CHOWN above, but we don't */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 goto out_unlock_up;
572
573 err = security_msg_queue_msgctl(msq, cmd);
574 if (err)
575 goto out_unlock_up;
576
577 switch (cmd) {
578 case IPC_SET:
579 {
580 err = -EPERM;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700581 if (setbuf.qbytes > ns->msg_ctlmnb && !capable(CAP_SYS_RESOURCE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 goto out_unlock_up;
583
584 msq->q_qbytes = setbuf.qbytes;
585
586 ipcp->uid = setbuf.uid;
587 ipcp->gid = setbuf.gid;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700588 ipcp->mode = (ipcp->mode & ~S_IRWXUGO) |
589 (S_IRWXUGO & setbuf.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 msq->q_ctime = get_seconds();
591 /* sleeping receivers might be excluded by
592 * stricter permissions.
593 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700594 expunge_all(msq, -EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 /* sleeping senders might be able to send
596 * due to a larger queue size.
597 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700598 ss_wakeup(&msq->q_senders, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 msg_unlock(msq);
600 break;
601 }
602 case IPC_RMID:
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700603 freeque(ns, msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 break;
605 }
606 err = 0;
607out_up:
Nadia Derbey3e148c72007-10-18 23:40:54 -0700608 up_write(&msg_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return err;
610out_unlock_up:
611 msg_unlock(msq);
612 goto out_up;
613out_unlock:
614 msg_unlock(msq);
615 return err;
616}
617
Ingo Molnar5a06a362006-07-30 03:04:11 -0700618static int testmsg(struct msg_msg *msg, long type, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
620 switch(mode)
621 {
622 case SEARCH_ANY:
623 return 1;
624 case SEARCH_LESSEQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700625 if (msg->m_type <=type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return 1;
627 break;
628 case SEARCH_EQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700629 if (msg->m_type == type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return 1;
631 break;
632 case SEARCH_NOTEQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700633 if (msg->m_type != type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return 1;
635 break;
636 }
637 return 0;
638}
639
Ingo Molnar5a06a362006-07-30 03:04:11 -0700640static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700642 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 tmp = msq->q_receivers.next;
645 while (tmp != &msq->q_receivers) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700646 struct msg_receiver *msr;
647
648 msr = list_entry(tmp, struct msg_receiver, r_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 tmp = tmp->next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700650 if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
651 !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
652 msr->r_msgtype, msr->r_mode)) {
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 list_del(&msr->r_list);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700655 if (msr->r_maxsize < msg->m_ts) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 msr->r_msg = NULL;
657 wake_up_process(msr->r_tsk);
658 smp_mb();
659 msr->r_msg = ERR_PTR(-E2BIG);
660 } else {
661 msr->r_msg = NULL;
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700662 msq->q_lrpid = task_pid_vnr(msr->r_tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 msq->q_rtime = get_seconds();
664 wake_up_process(msr->r_tsk);
665 smp_mb();
666 msr->r_msg = msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return 1;
669 }
670 }
671 }
672 return 0;
673}
674
suzuki651971c2006-12-06 20:37:48 -0800675long do_msgsnd(int msqid, long mtype, void __user *mtext,
676 size_t msgsz, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
678 struct msg_queue *msq;
679 struct msg_msg *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 int err;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700681 struct ipc_namespace *ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700682
Kirill Korotaev1e786932006-10-02 02:18:21 -0700683 ns = current->nsproxy->ipc_ns;
684
685 if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (mtype < 1)
688 return -EINVAL;
689
suzuki651971c2006-12-06 20:37:48 -0800690 msg = load_msg(mtext, msgsz);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700691 if (IS_ERR(msg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return PTR_ERR(msg);
693
694 msg->m_type = mtype;
695 msg->m_ts = msgsz;
696
Nadia Derbey023a5352007-10-18 23:40:51 -0700697 msq = msg_lock_check(ns, msqid);
698 if (IS_ERR(msq)) {
699 err = PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 goto out_free;
Nadia Derbey023a5352007-10-18 23:40:51 -0700701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 for (;;) {
704 struct msg_sender s;
705
Ingo Molnar5a06a362006-07-30 03:04:11 -0700706 err = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (ipcperms(&msq->q_perm, S_IWUGO))
708 goto out_unlock_free;
709
710 err = security_msg_queue_msgsnd(msq, msg, msgflg);
711 if (err)
712 goto out_unlock_free;
713
Ingo Molnar5a06a362006-07-30 03:04:11 -0700714 if (msgsz + msq->q_cbytes <= msq->q_qbytes &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 1 + msq->q_qnum <= msq->q_qbytes) {
716 break;
717 }
718
719 /* queue full, wait: */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700720 if (msgflg & IPC_NOWAIT) {
721 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 goto out_unlock_free;
723 }
724 ss_add(msq, &s);
725 ipc_rcu_getref(msq);
726 msg_unlock(msq);
727 schedule();
728
729 ipc_lock_by_ptr(&msq->q_perm);
730 ipc_rcu_putref(msq);
731 if (msq->q_perm.deleted) {
732 err = -EIDRM;
733 goto out_unlock_free;
734 }
735 ss_del(&s);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (signal_pending(current)) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700738 err = -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 goto out_unlock_free;
740 }
741 }
742
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700743 msq->q_lspid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 msq->q_stime = get_seconds();
745
Ingo Molnar5a06a362006-07-30 03:04:11 -0700746 if (!pipelined_send(msq, msg)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 /* noone is waiting for this message, enqueue it */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700748 list_add_tail(&msg->m_list, &msq->q_messages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 msq->q_cbytes += msgsz;
750 msq->q_qnum++;
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700751 atomic_add(msgsz, &ns->msg_bytes);
752 atomic_inc(&ns->msg_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 err = 0;
756 msg = NULL;
757
758out_unlock_free:
759 msg_unlock(msq);
760out_free:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700761 if (msg != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 free_msg(msg);
763 return err;
764}
765
suzuki651971c2006-12-06 20:37:48 -0800766asmlinkage long
767sys_msgsnd(int msqid, struct msgbuf __user *msgp, size_t msgsz, int msgflg)
768{
769 long mtype;
770
771 if (get_user(mtype, &msgp->mtype))
772 return -EFAULT;
773 return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
774}
775
Ingo Molnar5a06a362006-07-30 03:04:11 -0700776static inline int convert_mode(long *msgtyp, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700778 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 * find message of correct type.
780 * msgtyp = 0 => get first.
781 * msgtyp > 0 => get first message of matching type.
Ingo Molnar5a06a362006-07-30 03:04:11 -0700782 * msgtyp < 0 => get message with least type must be < abs(msgtype).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700784 if (*msgtyp == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return SEARCH_ANY;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700786 if (*msgtyp < 0) {
787 *msgtyp = -*msgtyp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return SEARCH_LESSEQUAL;
789 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700790 if (msgflg & MSG_EXCEPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return SEARCH_NOTEQUAL;
792 return SEARCH_EQUAL;
793}
794
suzuki651971c2006-12-06 20:37:48 -0800795long do_msgrcv(int msqid, long *pmtype, void __user *mtext,
796 size_t msgsz, long msgtyp, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
798 struct msg_queue *msq;
799 struct msg_msg *msg;
800 int mode;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700801 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 if (msqid < 0 || (long) msgsz < 0)
804 return -EINVAL;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700805 mode = convert_mode(&msgtyp, msgflg);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700806 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Nadia Derbey023a5352007-10-18 23:40:51 -0700808 msq = msg_lock_check(ns, msqid);
809 if (IS_ERR(msq))
810 return PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 for (;;) {
813 struct msg_receiver msr_d;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700814 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 msg = ERR_PTR(-EACCES);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700817 if (ipcperms(&msq->q_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 goto out_unlock;
819
820 msg = ERR_PTR(-EAGAIN);
821 tmp = msq->q_messages.next;
822 while (tmp != &msq->q_messages) {
823 struct msg_msg *walk_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700824
825 walk_msg = list_entry(tmp, struct msg_msg, m_list);
826 if (testmsg(walk_msg, msgtyp, mode) &&
827 !security_msg_queue_msgrcv(msq, walk_msg, current,
828 msgtyp, mode)) {
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 msg = walk_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700831 if (mode == SEARCH_LESSEQUAL &&
832 walk_msg->m_type != 1) {
833 msg = walk_msg;
834 msgtyp = walk_msg->m_type - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 } else {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700836 msg = walk_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 break;
838 }
839 }
840 tmp = tmp->next;
841 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700842 if (!IS_ERR(msg)) {
843 /*
844 * Found a suitable message.
845 * Unlink it from the queue.
846 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
848 msg = ERR_PTR(-E2BIG);
849 goto out_unlock;
850 }
851 list_del(&msg->m_list);
852 msq->q_qnum--;
853 msq->q_rtime = get_seconds();
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700854 msq->q_lrpid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 msq->q_cbytes -= msg->m_ts;
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700856 atomic_sub(msg->m_ts, &ns->msg_bytes);
857 atomic_dec(&ns->msg_hdrs);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700858 ss_wakeup(&msq->q_senders, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 msg_unlock(msq);
860 break;
861 }
862 /* No message waiting. Wait for a message */
863 if (msgflg & IPC_NOWAIT) {
864 msg = ERR_PTR(-ENOMSG);
865 goto out_unlock;
866 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700867 list_add_tail(&msr_d.r_list, &msq->q_receivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 msr_d.r_tsk = current;
869 msr_d.r_msgtype = msgtyp;
870 msr_d.r_mode = mode;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700871 if (msgflg & MSG_NOERROR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 msr_d.r_maxsize = INT_MAX;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700873 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 msr_d.r_maxsize = msgsz;
875 msr_d.r_msg = ERR_PTR(-EAGAIN);
876 current->state = TASK_INTERRUPTIBLE;
877 msg_unlock(msq);
878
879 schedule();
880
881 /* Lockless receive, part 1:
882 * Disable preemption. We don't hold a reference to the queue
883 * and getting a reference would defeat the idea of a lockless
884 * operation, thus the code relies on rcu to guarantee the
885 * existance of msq:
886 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
887 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
888 * rcu_read_lock() prevents preemption between reading r_msg
889 * and the spin_lock() inside ipc_lock_by_ptr().
890 */
891 rcu_read_lock();
892
893 /* Lockless receive, part 2:
894 * Wait until pipelined_send or expunge_all are outside of
895 * wake_up_process(). There is a race with exit(), see
896 * ipc/mqueue.c for the details.
897 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700898 msg = (struct msg_msg*)msr_d.r_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 while (msg == NULL) {
900 cpu_relax();
Ingo Molnar5a06a362006-07-30 03:04:11 -0700901 msg = (struct msg_msg *)msr_d.r_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903
904 /* Lockless receive, part 3:
905 * If there is a message or an error then accept it without
906 * locking.
907 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700908 if (msg != ERR_PTR(-EAGAIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 rcu_read_unlock();
910 break;
911 }
912
913 /* Lockless receive, part 3:
914 * Acquire the queue spinlock.
915 */
916 ipc_lock_by_ptr(&msq->q_perm);
917 rcu_read_unlock();
918
919 /* Lockless receive, part 4:
920 * Repeat test after acquiring the spinlock.
921 */
922 msg = (struct msg_msg*)msr_d.r_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700923 if (msg != ERR_PTR(-EAGAIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 goto out_unlock;
925
926 list_del(&msr_d.r_list);
927 if (signal_pending(current)) {
928 msg = ERR_PTR(-ERESTARTNOHAND);
929out_unlock:
930 msg_unlock(msq);
931 break;
932 }
933 }
934 if (IS_ERR(msg))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700935 return PTR_ERR(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
suzuki651971c2006-12-06 20:37:48 -0800938 *pmtype = msg->m_type;
939 if (store_msg(mtext, msg, msgsz))
Ingo Molnar5a06a362006-07-30 03:04:11 -0700940 msgsz = -EFAULT;
suzuki651971c2006-12-06 20:37:48 -0800941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 free_msg(msg);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return msgsz;
945}
946
suzuki651971c2006-12-06 20:37:48 -0800947asmlinkage long sys_msgrcv(int msqid, struct msgbuf __user *msgp, size_t msgsz,
948 long msgtyp, int msgflg)
949{
950 long err, mtype;
951
952 err = do_msgrcv(msqid, &mtype, msgp->mtext, msgsz, msgtyp, msgflg);
953 if (err < 0)
954 goto out;
955
956 if (put_user(mtype, &msgp->mtype))
957 err = -EFAULT;
958out:
959 return err;
960}
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -0700963static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Mike Waychison19b49462005-09-06 15:17:10 -0700965 struct msg_queue *msq = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Mike Waychison19b49462005-09-06 15:17:10 -0700967 return seq_printf(s,
Ingo Molnar5a06a362006-07-30 03:04:11 -0700968 "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
969 msq->q_perm.key,
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700970 msq->q_perm.id,
Ingo Molnar5a06a362006-07-30 03:04:11 -0700971 msq->q_perm.mode,
972 msq->q_cbytes,
973 msq->q_qnum,
974 msq->q_lspid,
975 msq->q_lrpid,
976 msq->q_perm.uid,
977 msq->q_perm.gid,
978 msq->q_perm.cuid,
979 msq->q_perm.cgid,
980 msq->q_stime,
981 msq->q_rtime,
982 msq->q_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984#endif