]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - ipc/msg.c
ipc: inline ipc_buildid()
[mirror_ubuntu-bionic-kernel.git] / ipc / msg.c
CommitLineData
1da177e4
LT
1/*
2 * linux/ipc/msg.c
5a06a363 3 * Copyright (C) 1992 Krishna Balasubramanian
1da177e4
LT
4 *
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
624dffcb 15 * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
073115d6
SG
16 *
17 * support for audit of ipc object properties and permission changes
18 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
1e786937
KK
19 *
20 * namespaces support
21 * OpenVZ, SWsoft Inc.
22 * Pavel Emelianov <xemul@openvz.org>
1da177e4
LT
23 */
24
c59ede7b 25#include <linux/capability.h>
1da177e4
LT
26#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>
19b4946c 36#include <linux/seq_file.h>
5f921ae9 37#include <linux/mutex.h>
1e786937 38#include <linux/nsproxy.h>
5f921ae9 39
1da177e4
LT
40#include <asm/current.h>
41#include <asm/uaccess.h>
42#include "util.h"
43
5a06a363
IM
44/*
45 * one msg_receiver structure for each sleeping receiver:
46 */
1da177e4 47struct msg_receiver {
5a06a363
IM
48 struct list_head r_list;
49 struct task_struct *r_tsk;
1da177e4 50
5a06a363
IM
51 int r_mode;
52 long r_msgtype;
53 long r_maxsize;
1da177e4 54
80491eb9 55 struct msg_msg *volatile r_msg;
1da177e4
LT
56};
57
58/* one msg_sender for each sleeping sender */
59struct msg_sender {
5a06a363
IM
60 struct list_head list;
61 struct task_struct *tsk;
1da177e4
LT
62};
63
64#define SEARCH_ANY 1
65#define SEARCH_EQUAL 2
66#define SEARCH_NOTEQUAL 3
67#define SEARCH_LESSEQUAL 4
68
5a06a363
IM
69static atomic_t msg_bytes = ATOMIC_INIT(0);
70static atomic_t msg_hdrs = ATOMIC_INIT(0);
1da177e4 71
1e786937 72static struct ipc_ids init_msg_ids;
1da177e4 73
1e786937 74#define msg_ids(ns) (*((ns)->ids[IPC_MSG_IDS]))
1da177e4 75
1e786937 76#define msg_unlock(msq) ipc_unlock(&(msq)->q_perm)
1e786937
KK
77#define msg_buildid(ns, id, seq) \
78 ipc_buildid(&msg_ids(ns), id, seq)
79
7ca7e564 80static void freeque(struct ipc_namespace *, struct msg_queue *);
7748dbfa 81static int newque(struct ipc_namespace *, struct ipc_params *);
1da177e4 82#ifdef CONFIG_PROC_FS
19b4946c 83static int sysvipc_msg_proc_show(struct seq_file *s, void *it);
1da177e4
LT
84#endif
85
7d69a1f4 86static void __msg_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
1e786937
KK
87{
88 ns->ids[IPC_MSG_IDS] = ids;
89 ns->msg_ctlmax = MSGMAX;
90 ns->msg_ctlmnb = MSGMNB;
91 ns->msg_ctlmni = MSGMNI;
7ca7e564 92 ipc_init_ids(ids);
1e786937
KK
93}
94
1e786937
KK
95int 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{
1e786937 109 struct msg_queue *msq;
7ca7e564
ND
110 int next_id;
111 int total, in_use;
1e786937
KK
112
113 mutex_lock(&msg_ids(ns).mutex);
7ca7e564
ND
114
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);
1e786937
KK
119 if (msq == NULL)
120 continue;
7ca7e564
ND
121 ipc_lock_by_ptr(&msq->q_perm);
122 freeque(ns, msq);
123 total++;
1e786937
KK
124 }
125 mutex_unlock(&msg_ids(ns).mutex);
126
127 kfree(ns->ids[IPC_MSG_IDS]);
128 ns->ids[IPC_MSG_IDS] = NULL;
129}
1e786937 130
5a06a363 131void __init msg_init(void)
1da177e4 132{
1e786937 133 __msg_init_ns(&init_ipc_ns, &init_msg_ids);
19b4946c
MW
134 ipc_init_proc_interface("sysvipc/msg",
135 " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
1e786937 136 IPC_MSG_IDS, sysvipc_msg_proc_show);
1da177e4
LT
137}
138
023a5355
ND
139static inline struct msg_queue *msg_lock(struct ipc_namespace *ns, int id)
140{
03f02c76
ND
141 struct kern_ipc_perm *ipcp = ipc_lock(&msg_ids(ns), id);
142
143 return container_of(ipcp, struct msg_queue, q_perm);
023a5355
ND
144}
145
146static inline struct msg_queue *msg_lock_check(struct ipc_namespace *ns,
147 int id)
148{
03f02c76
ND
149 struct kern_ipc_perm *ipcp = ipc_lock_check(&msg_ids(ns), id);
150
151 return container_of(ipcp, struct msg_queue, q_perm);
023a5355
ND
152}
153
7ca7e564
ND
154static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s)
155{
156 ipc_rmid(&msg_ids(ns), &s->q_perm);
157}
158
7748dbfa 159static int newque(struct ipc_namespace *ns, struct ipc_params *params)
1da177e4 160{
1da177e4 161 struct msg_queue *msq;
5a06a363 162 int id, retval;
7748dbfa
ND
163 key_t key = params->key;
164 int msgflg = params->flg;
1da177e4 165
5a06a363
IM
166 msq = ipc_rcu_alloc(sizeof(*msq));
167 if (!msq)
1da177e4
LT
168 return -ENOMEM;
169
5a06a363 170 msq->q_perm.mode = msgflg & S_IRWXUGO;
1da177e4
LT
171 msq->q_perm.key = key;
172
173 msq->q_perm.security = NULL;
174 retval = security_msg_queue_alloc(msq);
175 if (retval) {
176 ipc_rcu_putref(msq);
177 return retval;
178 }
179
7ca7e564
ND
180 /*
181 * ipc_addid() locks msq
182 */
1e786937 183 id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
5a06a363 184 if (id == -1) {
1da177e4
LT
185 security_msg_queue_free(msq);
186 ipc_rcu_putref(msq);
187 return -ENOSPC;
188 }
189
7ca7e564 190 msq->q_perm.id = msg_buildid(ns, id, msq->q_perm.seq);
1da177e4
LT
191 msq->q_stime = msq->q_rtime = 0;
192 msq->q_ctime = get_seconds();
193 msq->q_cbytes = msq->q_qnum = 0;
1e786937 194 msq->q_qbytes = ns->msg_ctlmnb;
1da177e4
LT
195 msq->q_lspid = msq->q_lrpid = 0;
196 INIT_LIST_HEAD(&msq->q_messages);
197 INIT_LIST_HEAD(&msq->q_receivers);
198 INIT_LIST_HEAD(&msq->q_senders);
7ca7e564 199
1da177e4
LT
200 msg_unlock(msq);
201
7ca7e564 202 return msq->q_perm.id;
1da177e4
LT
203}
204
5a06a363 205static inline void ss_add(struct msg_queue *msq, struct msg_sender *mss)
1da177e4 206{
5a06a363
IM
207 mss->tsk = current;
208 current->state = TASK_INTERRUPTIBLE;
209 list_add_tail(&mss->list, &msq->q_senders);
1da177e4
LT
210}
211
5a06a363 212static inline void ss_del(struct msg_sender *mss)
1da177e4 213{
5a06a363 214 if (mss->list.next != NULL)
1da177e4
LT
215 list_del(&mss->list);
216}
217
5a06a363 218static void ss_wakeup(struct list_head *h, int kill)
1da177e4
LT
219{
220 struct list_head *tmp;
221
222 tmp = h->next;
223 while (tmp != h) {
5a06a363
IM
224 struct msg_sender *mss;
225
226 mss = list_entry(tmp, struct msg_sender, list);
1da177e4 227 tmp = tmp->next;
5a06a363
IM
228 if (kill)
229 mss->list.next = NULL;
1da177e4
LT
230 wake_up_process(mss->tsk);
231 }
232}
233
5a06a363 234static void expunge_all(struct msg_queue *msq, int res)
1da177e4
LT
235{
236 struct list_head *tmp;
237
238 tmp = msq->q_receivers.next;
239 while (tmp != &msq->q_receivers) {
5a06a363
IM
240 struct msg_receiver *msr;
241
242 msr = list_entry(tmp, struct msg_receiver, r_list);
1da177e4
LT
243 tmp = tmp->next;
244 msr->r_msg = NULL;
245 wake_up_process(msr->r_tsk);
246 smp_mb();
247 msr->r_msg = ERR_PTR(res);
248 }
249}
5a06a363
IM
250
251/*
252 * freeque() wakes up waiters on the sender and receiver waiting queue,
253 * removes the message queue from message queue ID
7ca7e564 254 * IDR, and cleans up all the messages associated with this queue.
1da177e4 255 *
7ca7e564 256 * msg_ids.mutex and the spinlock for this message queue are held
5f921ae9 257 * before freeque() is called. msg_ids.mutex remains locked on exit.
1da177e4 258 */
7ca7e564 259static void freeque(struct ipc_namespace *ns, struct msg_queue *msq)
1da177e4
LT
260{
261 struct list_head *tmp;
262
5a06a363
IM
263 expunge_all(msq, -EIDRM);
264 ss_wakeup(&msq->q_senders, 1);
7ca7e564 265 msg_rmid(ns, msq);
1da177e4 266 msg_unlock(msq);
5a06a363 267
1da177e4 268 tmp = msq->q_messages.next;
5a06a363
IM
269 while (tmp != &msq->q_messages) {
270 struct msg_msg *msg = list_entry(tmp, struct msg_msg, m_list);
271
1da177e4
LT
272 tmp = tmp->next;
273 atomic_dec(&msg_hdrs);
274 free_msg(msg);
275 }
276 atomic_sub(msq->q_cbytes, &msg_bytes);
277 security_msg_queue_free(msq);
278 ipc_rcu_putref(msq);
279}
280
03f02c76 281static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg)
7748dbfa 282{
03f02c76
ND
283 struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
284
285 return security_msg_queue_associate(msq, msgflg);
7748dbfa
ND
286}
287
5a06a363 288asmlinkage long sys_msgget(key_t key, int msgflg)
1da177e4 289{
1e786937 290 struct ipc_namespace *ns;
7748dbfa
ND
291 struct ipc_ops msg_ops;
292 struct ipc_params msg_params;
1e786937
KK
293
294 ns = current->nsproxy->ipc_ns;
7ca7e564 295
7748dbfa
ND
296 msg_ops.getnew = newque;
297 msg_ops.associate = msg_security;
298 msg_ops.more_checks = NULL;
299
300 msg_params.key = key;
301 msg_params.flg = msgflg;
5a06a363 302
7748dbfa 303 return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params);
1da177e4
LT
304}
305
5a06a363
IM
306static inline unsigned long
307copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
1da177e4
LT
308{
309 switch(version) {
310 case IPC_64:
5a06a363 311 return copy_to_user(buf, in, sizeof(*in));
1da177e4 312 case IPC_OLD:
5a06a363 313 {
1da177e4
LT
314 struct msqid_ds out;
315
5a06a363 316 memset(&out, 0, sizeof(out));
1da177e4
LT
317
318 ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
319
320 out.msg_stime = in->msg_stime;
321 out.msg_rtime = in->msg_rtime;
322 out.msg_ctime = in->msg_ctime;
323
5a06a363 324 if (in->msg_cbytes > USHRT_MAX)
1da177e4
LT
325 out.msg_cbytes = USHRT_MAX;
326 else
327 out.msg_cbytes = in->msg_cbytes;
328 out.msg_lcbytes = in->msg_cbytes;
329
5a06a363 330 if (in->msg_qnum > USHRT_MAX)
1da177e4
LT
331 out.msg_qnum = USHRT_MAX;
332 else
333 out.msg_qnum = in->msg_qnum;
334
5a06a363 335 if (in->msg_qbytes > USHRT_MAX)
1da177e4
LT
336 out.msg_qbytes = USHRT_MAX;
337 else
338 out.msg_qbytes = in->msg_qbytes;
339 out.msg_lqbytes = in->msg_qbytes;
340
341 out.msg_lspid = in->msg_lspid;
342 out.msg_lrpid = in->msg_lrpid;
343
5a06a363
IM
344 return copy_to_user(buf, &out, sizeof(out));
345 }
1da177e4
LT
346 default:
347 return -EINVAL;
348 }
349}
350
351struct msq_setbuf {
352 unsigned long qbytes;
353 uid_t uid;
354 gid_t gid;
355 mode_t mode;
356};
357
5a06a363
IM
358static inline unsigned long
359copy_msqid_from_user(struct msq_setbuf *out, void __user *buf, int version)
1da177e4
LT
360{
361 switch(version) {
362 case IPC_64:
5a06a363 363 {
1da177e4
LT
364 struct msqid64_ds tbuf;
365
5a06a363 366 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
1da177e4
LT
367 return -EFAULT;
368
369 out->qbytes = tbuf.msg_qbytes;
370 out->uid = tbuf.msg_perm.uid;
371 out->gid = tbuf.msg_perm.gid;
372 out->mode = tbuf.msg_perm.mode;
373
374 return 0;
5a06a363 375 }
1da177e4 376 case IPC_OLD:
5a06a363 377 {
1da177e4
LT
378 struct msqid_ds tbuf_old;
379
5a06a363 380 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
1da177e4
LT
381 return -EFAULT;
382
383 out->uid = tbuf_old.msg_perm.uid;
384 out->gid = tbuf_old.msg_perm.gid;
385 out->mode = tbuf_old.msg_perm.mode;
386
5a06a363 387 if (tbuf_old.msg_qbytes == 0)
1da177e4
LT
388 out->qbytes = tbuf_old.msg_lqbytes;
389 else
390 out->qbytes = tbuf_old.msg_qbytes;
391
392 return 0;
5a06a363 393 }
1da177e4
LT
394 default:
395 return -EINVAL;
396 }
397}
398
5a06a363 399asmlinkage long sys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf)
1da177e4 400{
1da177e4 401 struct kern_ipc_perm *ipcp;
8e1c091c 402 struct msq_setbuf uninitialized_var(setbuf);
5a06a363
IM
403 struct msg_queue *msq;
404 int err, version;
1e786937 405 struct ipc_namespace *ns;
5a06a363 406
1da177e4
LT
407 if (msqid < 0 || cmd < 0)
408 return -EINVAL;
409
410 version = ipc_parse_version(&cmd);
1e786937 411 ns = current->nsproxy->ipc_ns;
1da177e4
LT
412
413 switch (cmd) {
5a06a363
IM
414 case IPC_INFO:
415 case MSG_INFO:
416 {
1da177e4
LT
417 struct msginfo msginfo;
418 int max_id;
5a06a363 419
1da177e4
LT
420 if (!buf)
421 return -EFAULT;
5a06a363
IM
422 /*
423 * We must not return kernel stack data.
1da177e4
LT
424 * due to padding, it's not enough
425 * to set all member fields.
426 */
1da177e4
LT
427 err = security_msg_queue_msgctl(NULL, cmd);
428 if (err)
429 return err;
430
5a06a363 431 memset(&msginfo, 0, sizeof(msginfo));
1e786937
KK
432 msginfo.msgmni = ns->msg_ctlmni;
433 msginfo.msgmax = ns->msg_ctlmax;
434 msginfo.msgmnb = ns->msg_ctlmnb;
1da177e4
LT
435 msginfo.msgssz = MSGSSZ;
436 msginfo.msgseg = MSGSEG;
1e786937 437 mutex_lock(&msg_ids(ns).mutex);
1da177e4 438 if (cmd == MSG_INFO) {
1e786937 439 msginfo.msgpool = msg_ids(ns).in_use;
1da177e4
LT
440 msginfo.msgmap = atomic_read(&msg_hdrs);
441 msginfo.msgtql = atomic_read(&msg_bytes);
442 } else {
443 msginfo.msgmap = MSGMAP;
444 msginfo.msgpool = MSGPOOL;
445 msginfo.msgtql = MSGTQL;
446 }
7ca7e564 447 max_id = ipc_get_maxid(&msg_ids(ns));
1e786937 448 mutex_unlock(&msg_ids(ns).mutex);
5a06a363 449 if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
1da177e4 450 return -EFAULT;
5a06a363 451 return (max_id < 0) ? 0 : max_id;
1da177e4 452 }
7ca7e564 453 case MSG_STAT: /* msqid is an index rather than a msg queue id */
1da177e4
LT
454 case IPC_STAT:
455 {
456 struct msqid64_ds tbuf;
457 int success_return;
5a06a363 458
1da177e4
LT
459 if (!buf)
460 return -EFAULT;
1da177e4 461
5a06a363 462 if (cmd == MSG_STAT) {
023a5355
ND
463 msq = msg_lock(ns, msqid);
464 if (IS_ERR(msq))
465 return PTR_ERR(msq);
7ca7e564 466 success_return = msq->q_perm.id;
1da177e4 467 } else {
023a5355
ND
468 msq = msg_lock_check(ns, msqid);
469 if (IS_ERR(msq))
470 return PTR_ERR(msq);
1da177e4
LT
471 success_return = 0;
472 }
473 err = -EACCES;
5a06a363 474 if (ipcperms(&msq->q_perm, S_IRUGO))
1da177e4
LT
475 goto out_unlock;
476
477 err = security_msg_queue_msgctl(msq, cmd);
478 if (err)
479 goto out_unlock;
480
023a5355
ND
481 memset(&tbuf, 0, sizeof(tbuf));
482
1da177e4
LT
483 kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
484 tbuf.msg_stime = msq->q_stime;
485 tbuf.msg_rtime = msq->q_rtime;
486 tbuf.msg_ctime = msq->q_ctime;
487 tbuf.msg_cbytes = msq->q_cbytes;
488 tbuf.msg_qnum = msq->q_qnum;
489 tbuf.msg_qbytes = msq->q_qbytes;
490 tbuf.msg_lspid = msq->q_lspid;
491 tbuf.msg_lrpid = msq->q_lrpid;
492 msg_unlock(msq);
493 if (copy_msqid_to_user(buf, &tbuf, version))
494 return -EFAULT;
495 return success_return;
496 }
497 case IPC_SET:
498 if (!buf)
499 return -EFAULT;
5a06a363 500 if (copy_msqid_from_user(&setbuf, buf, version))
1da177e4 501 return -EFAULT;
1da177e4
LT
502 break;
503 case IPC_RMID:
504 break;
505 default:
506 return -EINVAL;
507 }
508
1e786937 509 mutex_lock(&msg_ids(ns).mutex);
023a5355
ND
510 msq = msg_lock_check(ns, msqid);
511 if (IS_ERR(msq)) {
512 err = PTR_ERR(msq);
1da177e4 513 goto out_up;
023a5355 514 }
1da177e4 515
1da177e4 516 ipcp = &msq->q_perm;
073115d6
SG
517
518 err = audit_ipc_obj(ipcp);
519 if (err)
520 goto out_unlock_up;
8e1c091c 521 if (cmd == IPC_SET) {
5a06a363
IM
522 err = audit_ipc_set_perm(setbuf.qbytes, setbuf.uid, setbuf.gid,
523 setbuf.mode);
ac03221a
LK
524 if (err)
525 goto out_unlock_up;
526 }
073115d6 527
1da177e4 528 err = -EPERM;
5a06a363 529 if (current->euid != ipcp->cuid &&
1da177e4 530 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN))
5a06a363 531 /* We _could_ check for CAP_CHOWN above, but we don't */
1da177e4
LT
532 goto out_unlock_up;
533
534 err = security_msg_queue_msgctl(msq, cmd);
535 if (err)
536 goto out_unlock_up;
537
538 switch (cmd) {
539 case IPC_SET:
540 {
541 err = -EPERM;
1e786937 542 if (setbuf.qbytes > ns->msg_ctlmnb && !capable(CAP_SYS_RESOURCE))
1da177e4
LT
543 goto out_unlock_up;
544
545 msq->q_qbytes = setbuf.qbytes;
546
547 ipcp->uid = setbuf.uid;
548 ipcp->gid = setbuf.gid;
5a06a363
IM
549 ipcp->mode = (ipcp->mode & ~S_IRWXUGO) |
550 (S_IRWXUGO & setbuf.mode);
1da177e4
LT
551 msq->q_ctime = get_seconds();
552 /* sleeping receivers might be excluded by
553 * stricter permissions.
554 */
5a06a363 555 expunge_all(msq, -EAGAIN);
1da177e4
LT
556 /* sleeping senders might be able to send
557 * due to a larger queue size.
558 */
5a06a363 559 ss_wakeup(&msq->q_senders, 0);
1da177e4
LT
560 msg_unlock(msq);
561 break;
562 }
563 case IPC_RMID:
7ca7e564 564 freeque(ns, msq);
1da177e4
LT
565 break;
566 }
567 err = 0;
568out_up:
1e786937 569 mutex_unlock(&msg_ids(ns).mutex);
1da177e4
LT
570 return err;
571out_unlock_up:
572 msg_unlock(msq);
573 goto out_up;
574out_unlock:
575 msg_unlock(msq);
576 return err;
577}
578
5a06a363 579static int testmsg(struct msg_msg *msg, long type, int mode)
1da177e4
LT
580{
581 switch(mode)
582 {
583 case SEARCH_ANY:
584 return 1;
585 case SEARCH_LESSEQUAL:
5a06a363 586 if (msg->m_type <=type)
1da177e4
LT
587 return 1;
588 break;
589 case SEARCH_EQUAL:
5a06a363 590 if (msg->m_type == type)
1da177e4
LT
591 return 1;
592 break;
593 case SEARCH_NOTEQUAL:
5a06a363 594 if (msg->m_type != type)
1da177e4
LT
595 return 1;
596 break;
597 }
598 return 0;
599}
600
5a06a363 601static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
1da177e4 602{
5a06a363 603 struct list_head *tmp;
1da177e4
LT
604
605 tmp = msq->q_receivers.next;
606 while (tmp != &msq->q_receivers) {
5a06a363
IM
607 struct msg_receiver *msr;
608
609 msr = list_entry(tmp, struct msg_receiver, r_list);
1da177e4 610 tmp = tmp->next;
5a06a363
IM
611 if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
612 !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
613 msr->r_msgtype, msr->r_mode)) {
614
1da177e4 615 list_del(&msr->r_list);
5a06a363 616 if (msr->r_maxsize < msg->m_ts) {
1da177e4
LT
617 msr->r_msg = NULL;
618 wake_up_process(msr->r_tsk);
619 smp_mb();
620 msr->r_msg = ERR_PTR(-E2BIG);
621 } else {
622 msr->r_msg = NULL;
b488893a 623 msq->q_lrpid = task_pid_vnr(msr->r_tsk);
1da177e4
LT
624 msq->q_rtime = get_seconds();
625 wake_up_process(msr->r_tsk);
626 smp_mb();
627 msr->r_msg = msg;
5a06a363 628
1da177e4
LT
629 return 1;
630 }
631 }
632 }
633 return 0;
634}
635
651971cb 636long do_msgsnd(int msqid, long mtype, void __user *mtext,
637 size_t msgsz, int msgflg)
1da177e4
LT
638{
639 struct msg_queue *msq;
640 struct msg_msg *msg;
1da177e4 641 int err;
1e786937
KK
642 struct ipc_namespace *ns;
643
644 ns = current->nsproxy->ipc_ns;
5a06a363 645
1e786937 646 if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
1da177e4 647 return -EINVAL;
1da177e4
LT
648 if (mtype < 1)
649 return -EINVAL;
650
651971cb 651 msg = load_msg(mtext, msgsz);
5a06a363 652 if (IS_ERR(msg))
1da177e4
LT
653 return PTR_ERR(msg);
654
655 msg->m_type = mtype;
656 msg->m_ts = msgsz;
657
023a5355
ND
658 msq = msg_lock_check(ns, msqid);
659 if (IS_ERR(msq)) {
660 err = PTR_ERR(msq);
1da177e4 661 goto out_free;
023a5355 662 }
1da177e4
LT
663
664 for (;;) {
665 struct msg_sender s;
666
5a06a363 667 err = -EACCES;
1da177e4
LT
668 if (ipcperms(&msq->q_perm, S_IWUGO))
669 goto out_unlock_free;
670
671 err = security_msg_queue_msgsnd(msq, msg, msgflg);
672 if (err)
673 goto out_unlock_free;
674
5a06a363 675 if (msgsz + msq->q_cbytes <= msq->q_qbytes &&
1da177e4
LT
676 1 + msq->q_qnum <= msq->q_qbytes) {
677 break;
678 }
679
680 /* queue full, wait: */
5a06a363
IM
681 if (msgflg & IPC_NOWAIT) {
682 err = -EAGAIN;
1da177e4
LT
683 goto out_unlock_free;
684 }
685 ss_add(msq, &s);
686 ipc_rcu_getref(msq);
687 msg_unlock(msq);
688 schedule();
689
690 ipc_lock_by_ptr(&msq->q_perm);
691 ipc_rcu_putref(msq);
692 if (msq->q_perm.deleted) {
693 err = -EIDRM;
694 goto out_unlock_free;
695 }
696 ss_del(&s);
5a06a363 697
1da177e4 698 if (signal_pending(current)) {
5a06a363 699 err = -ERESTARTNOHAND;
1da177e4
LT
700 goto out_unlock_free;
701 }
702 }
703
b488893a 704 msq->q_lspid = task_tgid_vnr(current);
1da177e4
LT
705 msq->q_stime = get_seconds();
706
5a06a363 707 if (!pipelined_send(msq, msg)) {
1da177e4 708 /* noone is waiting for this message, enqueue it */
5a06a363 709 list_add_tail(&msg->m_list, &msq->q_messages);
1da177e4
LT
710 msq->q_cbytes += msgsz;
711 msq->q_qnum++;
5a06a363 712 atomic_add(msgsz, &msg_bytes);
1da177e4
LT
713 atomic_inc(&msg_hdrs);
714 }
5a06a363 715
1da177e4
LT
716 err = 0;
717 msg = NULL;
718
719out_unlock_free:
720 msg_unlock(msq);
721out_free:
5a06a363 722 if (msg != NULL)
1da177e4
LT
723 free_msg(msg);
724 return err;
725}
726
651971cb 727asmlinkage long
728sys_msgsnd(int msqid, struct msgbuf __user *msgp, size_t msgsz, int msgflg)
729{
730 long mtype;
731
732 if (get_user(mtype, &msgp->mtype))
733 return -EFAULT;
734 return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
735}
736
5a06a363 737static inline int convert_mode(long *msgtyp, int msgflg)
1da177e4 738{
5a06a363 739 /*
1da177e4
LT
740 * find message of correct type.
741 * msgtyp = 0 => get first.
742 * msgtyp > 0 => get first message of matching type.
5a06a363 743 * msgtyp < 0 => get message with least type must be < abs(msgtype).
1da177e4 744 */
5a06a363 745 if (*msgtyp == 0)
1da177e4 746 return SEARCH_ANY;
5a06a363
IM
747 if (*msgtyp < 0) {
748 *msgtyp = -*msgtyp;
1da177e4
LT
749 return SEARCH_LESSEQUAL;
750 }
5a06a363 751 if (msgflg & MSG_EXCEPT)
1da177e4
LT
752 return SEARCH_NOTEQUAL;
753 return SEARCH_EQUAL;
754}
755
651971cb 756long do_msgrcv(int msqid, long *pmtype, void __user *mtext,
757 size_t msgsz, long msgtyp, int msgflg)
1da177e4
LT
758{
759 struct msg_queue *msq;
760 struct msg_msg *msg;
761 int mode;
1e786937 762 struct ipc_namespace *ns;
1da177e4
LT
763
764 if (msqid < 0 || (long) msgsz < 0)
765 return -EINVAL;
5a06a363 766 mode = convert_mode(&msgtyp, msgflg);
1e786937 767 ns = current->nsproxy->ipc_ns;
1da177e4 768
023a5355
ND
769 msq = msg_lock_check(ns, msqid);
770 if (IS_ERR(msq))
771 return PTR_ERR(msq);
1da177e4
LT
772
773 for (;;) {
774 struct msg_receiver msr_d;
5a06a363 775 struct list_head *tmp;
1da177e4
LT
776
777 msg = ERR_PTR(-EACCES);
5a06a363 778 if (ipcperms(&msq->q_perm, S_IRUGO))
1da177e4
LT
779 goto out_unlock;
780
781 msg = ERR_PTR(-EAGAIN);
782 tmp = msq->q_messages.next;
783 while (tmp != &msq->q_messages) {
784 struct msg_msg *walk_msg;
5a06a363
IM
785
786 walk_msg = list_entry(tmp, struct msg_msg, m_list);
787 if (testmsg(walk_msg, msgtyp, mode) &&
788 !security_msg_queue_msgrcv(msq, walk_msg, current,
789 msgtyp, mode)) {
790
1da177e4 791 msg = walk_msg;
5a06a363
IM
792 if (mode == SEARCH_LESSEQUAL &&
793 walk_msg->m_type != 1) {
794 msg = walk_msg;
795 msgtyp = walk_msg->m_type - 1;
1da177e4 796 } else {
5a06a363 797 msg = walk_msg;
1da177e4
LT
798 break;
799 }
800 }
801 tmp = tmp->next;
802 }
5a06a363
IM
803 if (!IS_ERR(msg)) {
804 /*
805 * Found a suitable message.
806 * Unlink it from the queue.
807 */
1da177e4
LT
808 if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
809 msg = ERR_PTR(-E2BIG);
810 goto out_unlock;
811 }
812 list_del(&msg->m_list);
813 msq->q_qnum--;
814 msq->q_rtime = get_seconds();
b488893a 815 msq->q_lrpid = task_tgid_vnr(current);
1da177e4 816 msq->q_cbytes -= msg->m_ts;
5a06a363 817 atomic_sub(msg->m_ts, &msg_bytes);
1da177e4 818 atomic_dec(&msg_hdrs);
5a06a363 819 ss_wakeup(&msq->q_senders, 0);
1da177e4
LT
820 msg_unlock(msq);
821 break;
822 }
823 /* No message waiting. Wait for a message */
824 if (msgflg & IPC_NOWAIT) {
825 msg = ERR_PTR(-ENOMSG);
826 goto out_unlock;
827 }
5a06a363 828 list_add_tail(&msr_d.r_list, &msq->q_receivers);
1da177e4
LT
829 msr_d.r_tsk = current;
830 msr_d.r_msgtype = msgtyp;
831 msr_d.r_mode = mode;
5a06a363 832 if (msgflg & MSG_NOERROR)
1da177e4 833 msr_d.r_maxsize = INT_MAX;
5a06a363 834 else
1da177e4
LT
835 msr_d.r_maxsize = msgsz;
836 msr_d.r_msg = ERR_PTR(-EAGAIN);
837 current->state = TASK_INTERRUPTIBLE;
838 msg_unlock(msq);
839
840 schedule();
841
842 /* Lockless receive, part 1:
843 * Disable preemption. We don't hold a reference to the queue
844 * and getting a reference would defeat the idea of a lockless
845 * operation, thus the code relies on rcu to guarantee the
846 * existance of msq:
847 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
848 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
849 * rcu_read_lock() prevents preemption between reading r_msg
850 * and the spin_lock() inside ipc_lock_by_ptr().
851 */
852 rcu_read_lock();
853
854 /* Lockless receive, part 2:
855 * Wait until pipelined_send or expunge_all are outside of
856 * wake_up_process(). There is a race with exit(), see
857 * ipc/mqueue.c for the details.
858 */
5a06a363 859 msg = (struct msg_msg*)msr_d.r_msg;
1da177e4
LT
860 while (msg == NULL) {
861 cpu_relax();
5a06a363 862 msg = (struct msg_msg *)msr_d.r_msg;
1da177e4
LT
863 }
864
865 /* Lockless receive, part 3:
866 * If there is a message or an error then accept it without
867 * locking.
868 */
5a06a363 869 if (msg != ERR_PTR(-EAGAIN)) {
1da177e4
LT
870 rcu_read_unlock();
871 break;
872 }
873
874 /* Lockless receive, part 3:
875 * Acquire the queue spinlock.
876 */
877 ipc_lock_by_ptr(&msq->q_perm);
878 rcu_read_unlock();
879
880 /* Lockless receive, part 4:
881 * Repeat test after acquiring the spinlock.
882 */
883 msg = (struct msg_msg*)msr_d.r_msg;
5a06a363 884 if (msg != ERR_PTR(-EAGAIN))
1da177e4
LT
885 goto out_unlock;
886
887 list_del(&msr_d.r_list);
888 if (signal_pending(current)) {
889 msg = ERR_PTR(-ERESTARTNOHAND);
890out_unlock:
891 msg_unlock(msq);
892 break;
893 }
894 }
895 if (IS_ERR(msg))
5a06a363 896 return PTR_ERR(msg);
1da177e4
LT
897
898 msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
651971cb 899 *pmtype = msg->m_type;
900 if (store_msg(mtext, msg, msgsz))
5a06a363 901 msgsz = -EFAULT;
651971cb 902
1da177e4 903 free_msg(msg);
5a06a363 904
1da177e4
LT
905 return msgsz;
906}
907
651971cb 908asmlinkage long sys_msgrcv(int msqid, struct msgbuf __user *msgp, size_t msgsz,
909 long msgtyp, int msgflg)
910{
911 long err, mtype;
912
913 err = do_msgrcv(msqid, &mtype, msgp->mtext, msgsz, msgtyp, msgflg);
914 if (err < 0)
915 goto out;
916
917 if (put_user(mtype, &msgp->mtype))
918 err = -EFAULT;
919out:
920 return err;
921}
922
1da177e4 923#ifdef CONFIG_PROC_FS
19b4946c 924static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
1da177e4 925{
19b4946c
MW
926 struct msg_queue *msq = it;
927
928 return seq_printf(s,
5a06a363
IM
929 "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
930 msq->q_perm.key,
7ca7e564 931 msq->q_perm.id,
5a06a363
IM
932 msq->q_perm.mode,
933 msq->q_cbytes,
934 msq->q_qnum,
935 msq->q_lspid,
936 msq->q_lrpid,
937 msq->q_perm.uid,
938 msq->q_perm.gid,
939 msq->q_perm.cuid,
940 msq->q_perm.cgid,
941 msq->q_stime,
942 msq->q_rtime,
943 msq->q_ctime);
1da177e4
LT
944}
945#endif