msgutil.c 3.7 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * linux/ipc/msgutil.c
L
Linus Torvalds 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15
 * Copyright (C) 1999, 2004 Manfred Spraul
 *
 * This file is released under GNU General Public Licence version 2 or
 * (at your option) any later version.
 *
 * See the file COPYING for more details.
 */

#include <linux/spinlock.h>
#include <linux/init.h>
#include <linux/security.h>
#include <linux/slab.h>
#include <linux/ipc.h>
A
Al Viro 已提交
16
#include <linux/msg.h>
17
#include <linux/ipc_namespace.h>
A
Al Viro 已提交
18
#include <linux/utsname.h>
19
#include <linux/proc_fs.h>
L
Linus Torvalds 已提交
20 21 22 23
#include <asm/uaccess.h>

#include "util.h"

24 25
DEFINE_SPINLOCK(mq_lock);

26 27 28 29 30 31
/*
 * The next 2 defines are here bc this is the only file
 * compiled when either CONFIG_SYSVIPC and CONFIG_POSIX_MQUEUE
 * and not CONFIG_IPC_NS.
 */
struct ipc_namespace init_ipc_ns = {
32
	.count		= ATOMIC_INIT(1),
33
	.user_ns = &init_user_ns,
34
	.proc_inum = PROC_IPC_INIT_INO,
35 36 37 38
};

atomic_t nr_ipc_ns = ATOMIC_INIT(1);

L
Linus Torvalds 已提交
39 40 41 42 43
struct msg_msgseg {
	struct msg_msgseg* next;
	/* the next part of the message follows immediately */
};

P
Peter Hurley 已提交
44 45
#define DATALEN_MSG	(int)(PAGE_SIZE-sizeof(struct msg_msg))
#define DATALEN_SEG	(int)(PAGE_SIZE-sizeof(struct msg_msgseg))
L
Linus Torvalds 已提交
46

47 48

static struct msg_msg *alloc_msg(int len)
L
Linus Torvalds 已提交
49 50 51 52 53
{
	struct msg_msg *msg;
	struct msg_msgseg **pseg;
	int alen;

P
Peter Hurley 已提交
54
	alen = min(len, DATALEN_MSG);
55
	msg = kmalloc(sizeof(*msg) + alen, GFP_KERNEL);
L
Linus Torvalds 已提交
56
	if (msg == NULL)
57
		return NULL;
L
Linus Torvalds 已提交
58 59 60 61

	msg->next = NULL;
	msg->security = NULL;

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	len -= alen;
	pseg = &msg->next;
	while (len > 0) {
		struct msg_msgseg *seg;
		alen = min(len, DATALEN_SEG);
		seg = kmalloc(sizeof(*seg) + alen, GFP_KERNEL);
		if (seg == NULL)
			goto out_err;
		*pseg = seg;
		seg->next = NULL;
		pseg = &seg->next;
		len -= alen;
	}

	return msg;

out_err:
	free_msg(msg);
	return NULL;
}

struct msg_msg *load_msg(const void __user *src, int len)
{
	struct msg_msg *msg;
	struct msg_msgseg *seg;
	int err;
	int alen;

	msg = alloc_msg(len);
	if (msg == NULL)
		return ERR_PTR(-ENOMEM);

	alen = min(len, DATALEN_MSG);
L
Linus Torvalds 已提交
95 96 97 98 99 100 101
	if (copy_from_user(msg + 1, src, alen)) {
		err = -EFAULT;
		goto out_err;
	}

	len -= alen;
	src = ((char __user *)src) + alen;
102
	seg = msg->next;
L
Linus Torvalds 已提交
103
	while (len > 0) {
P
Peter Hurley 已提交
104
		alen = min(len, DATALEN_SEG);
L
Linus Torvalds 已提交
105 106 107 108
		if (copy_from_user(seg + 1, src, alen)) {
			err = -EFAULT;
			goto out_err;
		}
109
		seg = seg->next;
L
Linus Torvalds 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123
		len -= alen;
		src = ((char __user *)src) + alen;
	}

	err = security_msg_msg_alloc(msg);
	if (err)
		goto out_err;

	return msg;

out_err:
	free_msg(msg);
	return ERR_PTR(err);
}
124 125 126 127 128 129 130 131 132 133 134
#ifdef CONFIG_CHECKPOINT_RESTORE
struct msg_msg *copy_msg(struct msg_msg *src, struct msg_msg *dst)
{
	struct msg_msgseg *dst_pseg, *src_pseg;
	int len = src->m_ts;
	int alen;

	BUG_ON(dst == NULL);
	if (src->m_ts > dst->m_ts)
		return ERR_PTR(-EINVAL);

P
Peter Hurley 已提交
135
	alen = min(len, DATALEN_MSG);
136 137 138 139 140 141
	memcpy(dst + 1, src + 1, alen);

	len -= alen;
	dst_pseg = dst->next;
	src_pseg = src->next;
	while (len > 0) {
P
Peter Hurley 已提交
142
		alen = min(len, DATALEN_SEG);
143 144 145 146 147 148 149 150 151 152 153
		memcpy(dst_pseg + 1, src_pseg + 1, alen);
		dst_pseg = dst_pseg->next;
		len -= alen;
		src_pseg = src_pseg->next;
	}

	dst->m_type = src->m_type;
	dst->m_ts = src->m_ts;

	return dst;
}
154 155 156 157 158
#else
struct msg_msg *copy_msg(struct msg_msg *src, struct msg_msg *dst)
{
	return ERR_PTR(-ENOSYS);
}
159
#endif
L
Linus Torvalds 已提交
160 161 162 163 164
int store_msg(void __user *dest, struct msg_msg *msg, int len)
{
	int alen;
	struct msg_msgseg *seg;

P
Peter Hurley 已提交
165
	alen = min(len, DATALEN_MSG);
L
Linus Torvalds 已提交
166 167 168 169 170 171 172
	if (copy_to_user(dest, msg + 1, alen))
		return -1;

	len -= alen;
	dest = ((char __user *)dest) + alen;
	seg = msg->next;
	while (len > 0) {
P
Peter Hurley 已提交
173
		alen = min(len, DATALEN_SEG);
L
Linus Torvalds 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
		if (copy_to_user(dest, seg + 1, alen))
			return -1;
		len -= alen;
		dest = ((char __user *)dest) + alen;
		seg = seg->next;
	}
	return 0;
}

void free_msg(struct msg_msg *msg)
{
	struct msg_msgseg *seg;

	security_msg_msg_free(msg);

	seg = msg->next;
	kfree(msg);
	while (seg != NULL) {
		struct msg_msgseg *tmp = seg->next;
		kfree(seg);
		seg = tmp;
	}
}