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