简体   繁体   English

使用proc的内核模块程序

[英]kernel module program using proc

I have made the following kernel module to create a process "hello_proc" in /proc directory: 我已经制作了以下内核模块,以在/ proc目录中创建进程“ hello_proc”:

#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>

static int hello_proc_show(struct seq_file *m, void *v) 
{
    seq_printf(m, "P5 : Hello proc!\n");
    return 0;
}

static int hello_proc_open(struct inode *inode, struct  file *file) 
{
    return single_open(file, hello_proc_show, NULL);
}

static const struct file_operations hello_proc_fops = {
    .owner = THIS_MODULE,
    .open = hello_proc_open,
    .read = seq_read,
    //.write = seq_write,
    .llseek = seq_lseek,
    .release = single_release,
};

static int hello_proc_init(void) 
{
    proc_create("hello_proc", 0, NULL, &hello_proc_fops);
    //printk("P5 : Process hello proc created");
    return 0;
}

static void hello_proc_exit(void) 
{
    remove_proc_entry("hello_proc", NULL);
}

MODULE_LICENSE("GPL");
module_init(hello_proc_init);
module_exit(hello_proc_exit);

I now want to write (and read) contents of a command, say "ls -l /proc" to the proc file "hello_proc". 我现在想将(ls –l / proc)命令的内容写入(和读取)到proc文件“ hello_proc”中。

My question is, how to resolve the following error that I am getting while writing following data to proc file "hello_proc": 我的问题是,如何解决将以下数据写入proc文件“ hello_proc”时遇到的以下错误:

anubhav@anubhav-Inspiron-3421:~/Desktop/pro/p5$ sudo ls -l -t /proc | head -21 > /proc/hello_proc
bash: /proc/hello_proc: Permission denied 

anubhav@anubhav-Inspiron-3421:~/Desktop/pro/p5$ ls -l -t /proc | head -21 > sudo /proc/hello_proc
ls: cannot access /proc/4273: No such file or directory

anubhav@anubhav-Inspiron-3421:~/Desktop/pro/p5$ ls -l -t /proc | head -21 > /proc/hello_proc
bash: /proc/hello_proc: Permission denied

Firstly to resolve that Permission Denied Error 首先解决该Permission Denied Error

  1. Login as root (su) root (su)身份登录root (su)
  2. Run the command 运行命令

     $ ls -l -t /proc | head -21 > /proc/hello_proc 

Now you will face another issue as below. 现在您将面临另一个问题,如下所示。

head: write error: Input/output error 头:写错误:输入/输出错误
head: write error 头:写错误

The reason being that you not have written the write file operations fops for the proc file. 原因是,你不会写的写文件操作fops的proc文件。 In the code you have commented out the write operation. 在代码中,您已注释掉写操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM