Linux Proc文件系统

/proc文件系统一中虚拟文件系统,提供了一种内核空间和内核空间通信新的方式,虚拟文件系统相对其它文件系统,文件时动态生成的,起初,/proc文件系统就是为了提供内核运行的一些基本信息,下面将做一个简单/proc文件读写操作。
proc_test.c 源码如下

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/string.h>
#include <asm/uaccess.h>
#include <linux/seq_file.h>
#include <linux/slab.h>
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("proc write and read");
MODULE_AUTHOR("lchj");
#define PROC_TEST_FILE_NAME "proc_test_file"
#define PROC_TEST_PARENT_FILE_NAME NULL
static int proc_test_read(struct file *flip,char *buf,size_t count,loff_t *f_ops);
static int proc_test_write(struct file *flip,char *buf,size_t count,loff_t *f_ops);
static struct proc_dir_entry *proc_entry;

int len,temp;
static char *str;
static char test[100];
struct file_operations hello_proc_fops = {
    read :proc_test_read,
    .write = proc_test_write
};
int init_proc_module(void){
    printk("proc_test_modlue_init\n");
    proc_entry = proc_create( PROC_TEST_FILE_NAME, 0466, PROC_TEST_PARENT_FILE_NAME ,&hello_proc_fops);
    str ="hello proc test";
    len = strlen(str);
    temp = len;
    printk("%d",len);
    return 0;
}

void exit_proc_module(void){
    printk("proc_test module exit\n");
    remove_proc_entry(PROC_TEST_FILE_NAME,PROC_TEST_PARENT_FILE_NAME);
}

static int proc_test_read(struct file *flip,char *buf,size_t count,loff_t *f_ops){
    printk("test\n");
    printk("%d\n",count);
    if(count>temp){
        count=temp;
    }
    printk("%d\n",count);
    temp=temp-count;
    copy_to_user(buf,str, count);
    if(count==0){
        temp=len;
    }
    printk("%d\n",count);
    return count;
}
static int proc_test_write(struct file *flip,char *buf,size_t count,loff_t *f_ops){
    printk("write  count%zu",count);
    str = kmalloc(100,GFP_KERNEL);
    copy_from_user(str,buf,count);
    len = strlen(str);
    temp = len;
    return count;
}

module_init(init_proc_module);
module_exit(exit_proc_module);

MakeFile文件如下

obj-m += proc_test.o
CURRENT_PATH := $(shell pwd)
LINUX_KERNEL := $(shell uname -r)
LINUX_KERNEL_PATH := /usr/src/linux-headers-$(LINUX_KERNEL)
all:
    make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules
clean:uninstall
    make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean
install:
    sudo insmod proc_test.ko
uninstall:
    sudo rmmod proc_test
log:
    dmesg
read:
    cat /proc/proc_test_file
write:
    echo ${str} > /proc/proc_test_file

运行效果如下

lchj@lchj-VirtualBox:~/Documents/proc_test$ make install
sudo insmod proc_test.ko
[sudo] password for lchj: 
lchj@lchj-VirtualBox:~/Documents/proc_test$ make read
cat /proc/proc_test_file
hello proc testlchj@lchj-VirtualBox:~/Documents/proc_test$ make write str=hi,lchj!
echo hi,lchj! > /proc/proc_test_file
lchj@lchj-VirtualBox:~/Documents/proc_test$ make read
cat /proc/proc_test_file
hi,lchj!
lchj@lchj-VirtualBox:~/Documents/proc_test$ make write str=hi,proc
echo hi,proc > /proc/proc_test_file
lchj@lchj-VirtualBox:~/Documents/proc_test$ make read
cat /proc/proc_test_file
hi,proc

代码很简单,如没有Linux driver相关经验的话,需要注意一下代码中的read方法会掉用多次。
代码github地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值