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