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