首页 > 其他分享 >通过IOCTL 进行内核态与用户态交互

通过IOCTL 进行内核态与用户态交互

时间:2023-09-25 11:56:17浏览次数:31  
标签:struct int IOC misc IOCTL hi 内核 include 交互

 

#include <linux/init.h>
#include <linux/printk.h>
#include <linux/module.h>

#include <linux/miscdevice.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
/*============================= 模块信息=========================================*/
#define HI_NAME "HelloWorld"
#define HI_MOD_VER "V1.0.0"

/*=============================MISC设备驱动 IOCTL=======================================*/
#define IOC_MAGIC 'x'
#define IOC_CMD_1 _IOWR(IOC_MAGIC, 1, __u32)

struct ioc_data_s{
	unsigned int size;
	char buf[10];
};

struct ioc_data_s mydata = {
	.size = 10,
	.buf  = "World",
};
/*=============================MISC设备驱动=======================================*/
#define HI_MISC_DEV_NAME "hi1"

static int hi_misc_open(struct inode *inode, struct file *file)
{
	return 0;
}
static long hi_misc_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
	struct ioc_data_s indata;
	switch(cmd){
		case IOC_CMD_1:
			copy_from_user(&indata, (struct ioc_data_s*)arg, sizeof(struct ioc_data_s));
			printk("from user: %s\n",indata.buf);
			copy_to_user((void*)arg, &mydata, sizeof(struct ioc_data_s));
			break;
	}

	return 0;
}
static const struct file_operations hi_misc_fops = {
	.owner = THIS_MODULE,
	//.open = hi_misc_open,
	.unlocked_ioctl = hi_misc_ioctl,
};

static struct miscdevice hi_misc_dev =
{
	.minor = MISC_DYNAMIC_MINOR,
	.name = HI_MISC_DEV_NAME,
	.fops = &hi_misc_fops,
};
/*=============================模块框架接口========================================*/
static int __init hi_mod_init(void)
{
#if 1
	int ret;
	ret = misc_register(&hi_misc_dev);
	if(ret < 0){
		printk("hi misc_register failed(ret=%d)!\n", ret);
	}
#endif
	return 0;
}

static void __exit hi_mod_exit(void)
{
#if 1
	misc_deregister(&hi_misc_dev);
#endif
}
/*=============================模块框架===========================================*/

module_init(hi_mod_init);
module_exit(hi_mod_exit);

MODULE_AUTHOR("momo");
MODULE_DESCRIPTION("HelloWorld driver using misc dev.");
MODULE_VERSION( HI_MOD_VER " (" __DATE__ " " __TIME__ ")");
MODULE_LICENSE("GPL");

 

用户态代码:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <linux/ioctl.h>
#define IOC_MAGIC 'x'
#define IOC_CMD_1 _IOWR(IOC_MAGIC, 1, unsigned int)

struct ioc_data_s{
	unsigned int size;
	char buf[10];
};

struct ioc_data_s mydata = {
    .size = 10,
    .buf  = "hello",
};
 
int main()
{
	int fd = 0;
	char buf[10] = "Hello";

	fd = open("/dev/hi1",O_RDWR);
	if (fd < 0)
	{
		printf("打开设备hi1失败!\n");
		return -1;
	}

	if (ioctl(fd, IOC_CMD_1, &mydata) < 0)
	{
		return -1;
	}

	printf("from kernel:%s\n",mydata.buf);
	close(fd);
	return 0;
}

 

标签:struct,int,IOC,misc,IOCTL,hi,内核,include,交互
From: https://www.cnblogs.com/riveruns/p/17727642.html

相关文章

  • Linux----内核运行
    内核运行的过程启动阶段:当计算机启动时,通常从硬盘、固态硬盘或其他存储设备中加载引导加载程序(BootLoader)的第一部分。这个引导加载程序负责加载操作系统内核。加载内核:引导加载程序会从存储设备中加载操作系统内核的镜像文件,通常是一个称为vmlinuxz或bzImage的文件。......
  • Uboot到底如何启动内核
    原文:https://blog.51cto.com/weidongshan/66115371.uboot启动内核的代码缩减如下:Uboot1.16/lib_arm/board.c中start_armboot()函数调用/common/main.c中main_loop()函数,在main_loop()中有uboot启动内核的代码:s=getenv("bootcmd");debug("###main_loop:bootcmd=\"%s\&quo......
  • Linux内核Makefile系统文件详解
    MakefileLinux内核的Makefile系统非常复杂,因为内核包含了许多不同的配置选项、架构和模块。以下是Linux内核Makefile系统中一些重要的文件和目录,以及它的作用。顶层Makefile(Makefile)顶层Makefile是整个内核编译过程的入口点它包含了全局选置选项,如目标体系结构、编译......
  • Linux内核裁剪移植
    目录前奏内核移植步骤制作可引导的LinuxISO文件对新机器配置网络前奏rambo@debian:~$cat/etc/issueDebianGNU/Linux12\n\lrambo@debian:~$free-htotalusedfreesharedbuff/cacheavailableMem:15Gi1.......
  • crash —— 内核符号和地址直接相互转换
    通过sym可以将内核地址转换成内核符号,或者将内核符号转换成内核地址。根据地址转换为符号函数地址crash>symffffffff8166f300ffffffff8166f300(T)blk_update_request+16/home/pengdl/x86_64/linux-6.2/block/blk-mq.c:896全局变量crash>sym-qpanic_on_offfff......
  • crash —— 获取所有内核符号
    查看所有的内核符号以及地址crash>sym-l0(D)__per_cpu_start0(D)irq_stack_union4000(D)cpu_debug_store5000(D)cpu_tss_rw8000(D)gdt_page9000(d)exception_stackse000(d)entry_stack_storagef000(D)espfix_waddrf008(D)espfix_stackf010(D)cpu......
  • 浏览器内核和js引擎
    浏览器内核是什么?简而言之,浏览器内核就是把我们编写的代码转换为页面的中控件。虽然现在大家谈起浏览器内核时,多指代渲染引擎(Renderingengine或layoutengineer)。但其实浏览器内核包括了两部分,渲染引擎和JS引擎,只是后来JS引擎用的越来越多所以就单独的把JS引擎拿了出来。 Ja......
  • crash —— 查看内核配置
    在编译内核时如果配置了CONFIG_IKCONFIG,那么内核配置文件会被内嵌到内核中,那么可以通过crash工具将其输出出来。crash>sysconfig##Automaticallygeneratedfile;DONOTEDIT.#Linux/x864.18.0KernelConfiguration###Compiler:gcc(Ubuntu5.4.0-6ubuntu1~16.04.......
  • 关于昨天测试结果--可视化的改进(也就是实现查询功能和ECharts的交互功能)
    今天我又看了一下自己可视化界面,不得不说,有点子low;并且,我除了实现了数据的可视化,并没有那种选择哪天的日期,然后就显示哪天的信息,可视化并不明确,很low!!!今天特意来改进一下:文本框输入日期数据:界面跳转:也就是通过将文本框数据和后端相连接实现,sql语句:select*fromtest1wher......
  • 基于事件的 JavaScript 编程:构建交互式 Web 应用程序
    了解事件 1.事件类型JavaScript支持多种事件类型。一些最常见的包括:鼠标事件:这些事件由用户与鼠标的交互触发,例如单击、悬停和拖动。键盘事件:这些事件在用户与键盘交互时发生,例如按下某个键或松开某个键。表单事件:与表单元素相关的事件,例如提交表单或更改输入字段的值。......