linux内核探索--基于proc实现内核和用户态通信

简介: 基于proc实现内核和用户态通信

目录

1、编写proc文件

2、编写配套的Makefile

3、测试


要求:实现一个proc文件,一个只读文件output,一个可写文件input;功能是:向input文件写入字符串后,可以从output文件中,读出input文件中反转后的字符串


1、编写proc文件

vim my_proc.c

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h> 
#include <linux/proc_fs.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#define BUFSIZE  1024
static struct proc_dir_entry *input, *output;
static char *buf;
static unsigned int len;
void reverse(void)
{
  int i = 0;
  int j = len - 2;
  printk("in reverse = %u\n", len);
  while(i < j)
  {
    char c;
    c = buf[i];
    buf[i++] = buf[j];
    buf[j--] = c;
  }
}
static ssize_t mywrite(struct file *file, const char __user *ubuf,size_t count, loff_t *ppos) 
{
  if(count <= 0)
    return -EFAULT;
  printk("---start write---\n");
  len = count > BUFSIZE ? BUFSIZE : count;
  // kfree memory by kmalloc before
  if(buf != NULL)
    kfree(buf);
  buf = (char*)kmalloc(len+1, GFP_KERNEL);
  if(buf == NULL)
  {
    printk("kmalloc failure\n");
    return -EFAULT;
  }
  //memset(buf, 0, sizeof(buf));
  memset(buf, 0, len+1);
  if(copy_from_user(buf, ubuf, len))
    return -EFAULT;
  printk("writing :%s",buf);
  return len;
}
static ssize_t myread(struct file *file, char __user *ubuf,size_t count, loff_t *ppos) 
{
  if(*ppos > 0)
    return 0;
  printk("---start read---\n");
  printk("origin = %s\n", buf);
  reverse();
  printk("after = %s\n", buf);
  if(copy_to_user(ubuf, buf, len))
    return -EFAULT;
  *ppos = *ppos + len;
  return len;
}
static struct file_operations fo_input = 
{
  .owner = THIS_MODULE,
  .write = mywrite,
};
static struct file_operations fo_output = 
{
  .owner = THIS_MODULE,
  .read = myread,
};
static int proc_init(void)
{
  input=proc_create("input",0660,NULL,&fo_input);
  if(input == NULL)
    return -ENOMEM;
  output=proc_create("output",0660,NULL,&fo_output);
  if(output == NULL)
    return -ENOMEM;
  printk("input & ouput init success!\n");
  return 0;
}
static void proc_cleanup(void)
{
  proc_remove(input);
  proc_remove(output);
  printk("input & output cleanup\n");
}
module_init(proc_init);
module_exit(proc_cleanup);
MODULE_LICENSE("GPL");

image.gif

2、编写配套的Makefile

vim Makefile:

obj-m += my_proc.o
all:
  make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
  make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

image.gif

3、测试

make 编译一下

如下

make -C /lib/modules/4.18.11/build M=/home/zj/neihe/proc_module_making/my_proc clean

make[1]: 正在进入目录 `/usr/src/linux-4.18.11'

 CLEAN   /home/zj/neihe/proc_module_making/my_proc/.tmp_versions

 CLEAN   /home/zj/neihe/proc_module_making/my_proc/Module.symvers

make[1]:正在离开目录 `/usr/src/linux-4.18.11'

如果觉得文章对你有帮助,欢迎关注微信公众号:小牛呼噜噜

然后sudo su 切换的root用户下

ls /proc/

发现多了2个文件:input ,output

ehco "test" >> /proc/input

cat /proc/input

打印:tset

ps:dmesg

image.png

内核探索暂时先到这里,先告一段落= =

相关文章
|
7天前
|
存储 安全 Linux
探索Linux操作系统的心脏:内核
在这篇文章中,我们将深入探讨Linux操作系统的核心—内核。通过简单易懂的语言和比喻,我们会发现内核是如何像心脏一样为系统提供动力,处理数据,并保持一切顺畅运行。从文件系统的管理到进程调度,再到设备驱动,我们将一探究竟,看看内核是怎样支撑起整个操作系统的大厦。无论你是计算机新手还是资深用户,这篇文章都将带你领略Linux内核的魅力,让你对这台复杂机器的内部运作有一个清晰的认识。
23 3
|
17天前
|
缓存 安全 Unix
Linux 内核黑客不可靠指南【ChatGPT】
Linux 内核黑客不可靠指南【ChatGPT】
|
17天前
|
Linux 开发者
Linux内核贡献成熟度模型 【ChatGPT】
Linux内核贡献成熟度模型 【ChatGPT】
|
17天前
|
网络协议 Ubuntu Linux
用Qemu模拟vexpress-a9 (三)--- 实现用u-boot引导Linux内核
用Qemu模拟vexpress-a9 (三)--- 实现用u-boot引导Linux内核
|
17天前
|
Linux
用clang编译Linux内核
用clang编译Linux内核
|
17天前
|
Linux API C语言
Linux 内核补丁提交的清单 【ChatGPT】
Linux 内核补丁提交的清单 【ChatGPT】
|
17天前
|
安全 Linux 开发者
Linux内核管理风格 【ChatGPT】
Linux内核管理风格 【ChatGPT】
|
18天前
|
Linux 程序员 编译器
Linux内核驱动程序接口 【ChatGPT】
Linux内核驱动程序接口 【ChatGPT】
|
18天前
|
存储 Linux 开发工具
如何进行Linux内核开发【ChatGPT】
如何进行Linux内核开发【ChatGPT】
|
17天前
|
Linux API 调度
关于在Linux内核中使用不同延迟/休眠机制 【ChatGPT】
关于在Linux内核中使用不同延迟/休眠机制 【ChatGPT】