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