Linux内核:写proc文件的返回值问题

来源:互联网 发布:百度一下淘宝商城 编辑:程序博客网 时间:2024/05/22 09:07

一、使用说明

1. 用户态写(echo或write())内核/proc文件时,返回值,就是内核的函数提供的返回值。

2. echo数据时的返回情况:

        a. echo写int数据,可以用以下2种方式:

                echo "6" > /proc/sys/net/ipv4/test/testdata

                echo 7 > /proc/sys/net/ipv4/test/testdata

        b. 当写入的类型不对时,返回“echo: write error: Invalid argument”错误。

                echo "abc" > /proc/sys/net/ipv4/test/testdata

        c. 当内核返回值为-1时,会提示“echo: write error: Operation not permitted”错误.

3. 内核/proc文件的写法。

4. 用户态write()如何写入int数据类型。

二、代码

2.1 内核代码

#include <linux/netfilter.h>#include <linux/netfilter_ipv4.h>#include <linux/module.h>#include <linux/moduleparam.h>#include <linux/kernel.h>#include <linux/inetdevice.h>#include <linux/cpumask.h>#include <linux/string.h>#include <net/route.h>#include <linux/inet.h>#include <linux/ip.h>#include <linux/udp.h>#include <net/checksum.h>#include <net/udp.h>#include <net/ip.h>#include <net/route.h>u32 testdata = 0;//6. function()static int proc_testdata(struct ctl_table *table, int write,void __user *buffer, size_t *lenp, loff_t *ppos){    int ret;    ret = proc_dointvec(table,write,buffer,lenp,ppos);    if(ret == 0 && write)    {         return 100;    }    printk("ret:%d\n", ret);    return ret;}//4. struct ctl_pathstruct ctl_path test_path[] = {    {        .ctl_name = CTL_NET,        .procname = "net",    },    {        .ctl_name = NET_IPV4,        .procname = "ipv4",    },    {        .ctl_name = 100,        .procname = "test",    },    { }};//5. struct ctl_tablestruct ctl_table test_table[] = {    {        .ctl_name   = 101,        .procname   = "testdata",        .data       = &testdata,        .maxlen     = sizeof(testdata),        .mode       = 0644,        .proc_handler   = proc_testdata,    },    {.ctl_name = 0 }};//1. struct ctl_table_headerstruct ctl_table_header * th = NULL;static int init_marker(void){    printk("init_marker ok\n");    //2. register_sysctl_paths()    th = register_sysctl_paths(test_path, test_table);    if(!th)    {        printk("register_sysctl_paths error\n");        return -1;    }    return 0;}static void exit_marker(void){    printk("exit_marker ok\n");    //3. unregister_sysctl_table()    if(th)    {        unregister_sysctl_table(th);    }}module_init(init_marker);module_exit(exit_marker);

2.2 用户态代码

#include <stdio.h>#include <stdlib.h>#include <fcntl.h>int main(int argc, char*argv[]){        int fd = open("/proc/sys/net/ipv4/test/testdata", O_RDWR);        if (fd == -1)        {                printf("open error.\n");                return -1;        }        char sz[10];        int p = 5;
        //sprintf() int -> char sz[] 用write()写int类型数据        sprintf((char*)sz, "%u", p);        //wirte() int        int n = write(fd, sz, 1);        printf("n:%d\n", n);        close(fd);        return 0;}

三、输出结果

3.1 proc_testdata()返回100时

        write()写:


        echo写:


3.2 proc_testdata()返回-1时

        write()写:


        echo写:



0 0
原创粉丝点击