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