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