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