Bootstrap

Linux用户态和内核态通信

linux环境下用户态和内核态通信机制主要有:ioctl、procFS和netlink,下面分别对各种实现进行简要分析。

1、ioctl

在内核态创建设备文件,并提供read、write和ioctl等操作接口,

1)内核态部分

#define DEV_NAME "usr"
static struct cdev usr_cdev;
static dev_t usr_dev_num;

static struct class *usr_class;
static struct device *usr_dev;

enum E_DEV_CMD
{
    ADD_USER	= 0x2201,
    DEL_USER	= 0x2202
};


static int usr_dev_open(struct inode *node, struct file * fd)
{
	return RET_OK;
}

static int usr_dev_close(struct inode *node, struct file * fd)
{
	return RET_OK;
}

static long usr_dev_ioctl(struct file *fd, unsigned int cmd, unsigned long arg)
{
	int ret = 0;

	switch (cmd)
	{
	case ADD_USER:
		{
			usr_info_t user_info;
			usr_info_t __user *UserSpaceInfo =  (usr_info_t __user *) arg;

			if (UserSpaceInfo == NULL)
			{
				ret = -EINVAL;
				break;
			}

			if (copy_from_user(&user_info, UserSpaceInfo
;