// SPDX-License-Identifier: GPL-2.0-or-later
/*
* linux/ipc/msgutil.c
* Copyright (C) 1999, 2004 Manfred Spraul
*/ #include <linux/spinlock.h>
#include <linux/init.h>
#include <linux/security.h>
#include <linux/slab.h>
#include <linux/ipc.h>
#include <linux/msg.h>
#include <linux/ipc_namespace.h>
#include <linux/utsname.h>
#include <linux/proc_ns.h>
#include <linux/uaccess.h>
#include <linux/sched.h> #include "util.h" DEFINE_SPINLOCK(mq_lock); /*
* 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 = {
.count = REFCOUNT_INIT(1),
.user_ns = &init_user_ns,
.ns.inum = PROC_IPC_INIT_INO,
#ifdef CONFIG_IPC_NS
.ns.ops = &ipcns_operations,
#endif
}; struct msg_msgseg {
struct msg_msgseg *next;
/* the next part of the message follows immediately */
}; #define DATALEN_MSG ((size_t)PAGE_SIZE-sizeof(struct msg_msg))
#define DATALEN_SEG ((size_t)PAGE_SIZE-sizeof(struct msg_msgseg)) static struct msg_msg *alloc_msg(size_t len)
{
struct msg_msg *msg;
struct msg_msgseg **pseg;
size_t alen; alen = min(len, DATALEN_MSG);
msg = kmalloc(sizeof(*msg) + alen, GFP_KERNEL_ACCOUNT);
if (msg == NULL)
return NULL; msg->next = NULL;
msg->security = NULL; len -= alen;
pseg = &msg->next;
while (len > 0) {
struct msg_msgseg *seg; cond_resched(); alen = min(len, DATALEN_SEG);
seg = kmalloc(sizeof(*seg) + alen, GFP_KERNEL_ACCOUNT);
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, size_t len)
{
struct msg_msg *msg;
struct msg_msgseg *seg;
int err = -EFAULT;
size_t alen; msg = alloc_msg(len);
if (msg == NULL)
return ERR_PTR(-ENOMEM); alen = min(len, DATALEN_MSG);
if (copy_from_user(msg + 1, src, alen))
goto out_err; for (seg = msg->next; seg != NULL; seg = seg->next) {
len -= alen;
src = (char __user *)src + alen;
alen = min(len, DATALEN_SEG);
if (copy_from_user(seg + 1, src, alen))
goto out_err;
} err = security_msg_msg_alloc(msg);
if (err)
goto out_err; return msg; out_err:
free_msg(msg);
return ERR_PTR(err);
}
#ifdef CONFIG_CHECKPOINT_RESTORE
struct msg_msg *copy_msg(struct msg_msg *src, struct msg_msg *dst)
{
struct msg_msgseg *dst_pseg, *src_pseg;
size_t len = src->m_ts;
size_t alen; if (src->m_ts > dst->m_ts)
return ERR_PTR(-EINVAL); alen = min(len, DATALEN_MSG);
memcpy(dst + 1, src + 1, alen); for (dst_pseg = dst->next, src_pseg = src->next;
src_pseg != NULL;
dst_pseg = dst_pseg->next, src_pseg = src_pseg->next) { len -= alen;
alen = min(len, DATALEN_SEG);
memcpy(dst_pseg + 1, src_pseg + 1, alen);
} dst->m_type = src->m_type;
dst->m_ts = src->m_ts; return dst;
}
#else
struct msg_msg *copy_msg(struct msg_msg *src, struct msg_msg *dst)
{
return ERR_PTR(-ENOSYS);
}
#endif
int store_msg(void __user *dest, struct msg_msg *msg, size_t len)
{
size_t alen;
struct msg_msgseg *seg; alen = min(len, DATALEN_MSG);
if (copy_to_user(dest, msg + 1, alen))
return -1; for (seg = msg->next; seg != NULL; seg = seg->next) {
len -= alen;
dest = (char __user *)dest + alen;
alen = min(len, DATALEN_SEG);
if (copy_to_user(dest, seg + 1, alen))
return -1;
}
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; cond_resched();
kfree(seg);
seg = tmp;
}
}

  

IPC之msgutil.c源码解读的更多相关文章

  1. IPC之mqueue.c源码解读

    队列的意思应该大家都清楚,不过还有有一些细节的地方不知道,下面是一个队列的源码,因该说这是队列的一部分,不是全部.而且是linux中队列,其他各种OS中队列大同小异. /* * POSIX messa ...

  2. IPC之ipc_sysctl.c源码解读

    // SPDX-License-Identifier: GPL-2.0-only /* * Copyright (C) 2007 * * Author: Eric Biederman <ebie ...

  3. IPC之util.h源码解读

    /* SPDX-License-Identifier: GPL-2.0 */ /* * linux/ipc/util.h * Copyright (C) 1999 Christoph Rohland ...

  4. IPC之util.c源码解读

    // SPDX-License-Identifier: GPL-2.0 /* * linux/ipc/util.c * Copyright (C) 1992 Krishna Balasubramani ...

  5. IPC之syscall.c源码解读

    // SPDX-License-Identifier: GPL-2.0 /* * sys_ipc() is the old de-multiplexer for the SysV IPC calls. ...

  6. IPC之shm.c源码解读

    // SPDX-License-Identifier: GPL-2.0 /* * linux/ipc/shm.c * Copyright (C) 1992, 1993 Krishna Balasubr ...

  7. IPC之sem.c源码解读

    // SPDX-License-Identifier: GPL-2.0 /* * linux/ipc/sem.c * Copyright (C) 1992 Krishna Balasubramania ...

  8. IPC之namespace.c源码解读

    // SPDX-License-Identifier: GPL-2.0 /* * linux/ipc/namespace.c * Copyright (C) 2006 Pavel Emelyanov ...

  9. IPC之msg.c源码解读

    // SPDX-License-Identifier: GPL-2.0 /* * linux/ipc/msg.c * Copyright (C) 1992 Krishna Balasubramania ...

随机推荐

  1. 安装php的sphinx扩展模块

    转自 http://blog.csdn.net/fenglailea/article/details/38115821 首先你必须已经安装过了sphinx 如何安装sphinx请看:http://bl ...

  2. Python os 使用

    python os 使用 1. 获取文件所在路径 import os os.path.dirname(__file__)  获取当前文件的所在路径 print (os.path.dirname(os. ...

  3. 【ARM-Linux开发】linux下Eclipse进行C编程时动态链接库的生成和使用

    linux下Eclipse进行C编程时动态链接库的生成和使用 引用 http://linux.chinaitlab.com/soft/864157.html 欢迎进入Linux社区论坛,与200万技术 ...

  4. 【图像处理】H.264流媒体播放

    相信有不少人和我一样,希望实现H264格式视频的流媒体播放.但是对于一个新手来说,往往不知道从何入手.利用百度,GOOGLE等搜索资料真是沙里淘金.在琢磨了N周之后,才弄出来了点成果,其中费了很多无用 ...

  5. csv文件的读取写法 from Udacity

    长版本 import unicodecsv enrollments_filename = 'C:\\Users\\xxxxx\\Desktop\\try.csv' enrollments = [] f ...

  6. [bzoj1892][bzoj2384][bzoj1461][Ceoi2011]Match/字符串的匹配_KMP_树状数组

    2384: [Ceoi2011]Match 1892: Match 1461: 字符串的匹配 题目大意: 数据范围: 题解: 很巧妙的一道题呀. 需要对$KMP$算法有很深的理解才行. 首先我们需要发 ...

  7. 深拷贝 & 浅拷贝

    浅拷贝: class Professor { String name; int age; public Professor(String name, int age) { this.name = na ...

  8. Linux系列(5):入门之文件类型与扩展名

    通过本章你会了解到: 文件类型有哪些? 文件扩展名的意义是什么? 1.文件类型 任何设备在Linux系统中都是文件,不仅如此,连数据沟通的接口也有专属的文件在负责,所以Linux的文件种类真的很多,除 ...

  9. Spring4学习回顾之路04—引用其他Bean,集合数据注入,内部Bean

    引用其他Bean 组件应用程序的Bean经常需要相互协作以完成应用程序的功能,所以要求Bean能够相互访问,就必须在Bean配置文件中指定Bean的引用.在Bean的配置文件中可以用过<ref& ...

  10. 数值优化(Numerical Optimization)学习系列-目录

    数值优化(Numerical Optimization)学习系列-目录 置顶 2015年12月27日 19:07:11 下一步 阅读数 12291更多 分类专栏: 数值优化   版权声明:本文为博主原 ...