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