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
- Linux内核:写proc文件的返回值问题
- Linux内核学习笔记之seq_file接口创建可读写proc文件
- Linux内核proc文件系统的冰山一角--源自对/proc/net/dev文件中各网卡参数的疑问
- Linux内核proc文件系统的冰山一角--源自对/proc/net/dev文件中各网卡参数的疑问
- Linux内核proc文件系统的冰山一角 源自对/proc/net/dev文件中各网卡参数的疑问
- Linux内核proc文件系统的冰山一角--源自对/proc/net/dev文件中各网卡参数的疑问
- 写一个自己的proc文件
- [Linux内核参数]proc
- Linux内核参数-proc
- Linux内核参数-proc
- linux内核——创建自己的/proc文件——processinfo
- Linux内核和用户空间通信的方式— proc文件和mmap共享内存
- Linux内核中bool值的返回
- Linux内核源代码情景分析-文件的写
- linux系统下proc文件的含义
- 嵌入式 linux下proc文件的读写
- linux下proc文件的读写
- linux /proc目录下的文件详解
- 0000001_java学习之路
- Linux下更改转移mysql数据库目录
- iOS安全系列之 HTTPS
- 批量添加删除EC2资源的TAG
- *+html功能
- Linux内核:写proc文件的返回值问题
- 可空类型(C Sharp)
- Android中对日期进行排序
- 用Properties加载“ .properties "文件产生的java.lang.NullPointerException的解决之道
- DoubleLinkedList独立实现,不依赖任何包
- Problem E: 二维数组中的查找
- java虚拟机
- C++对象模型——"无继承"情况下的对象构造(第五章)
- Web安全测试之跨站请求伪造(CSRF)