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

一、使用说明

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_path
struct ctl_path test_path[] = {
    {
        .ctl_name = CTL_NET,
        .procname = "net",
    },
    {
        .ctl_name = NET_IPV4,
        .procname = "ipv4",
    },
    {
        .ctl_name = 100,
        .procname = "test",
    },
    { }
};

//5. struct ctl_table
struct 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_header
struct 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
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面给出一段 C 语言代码,用于获取进程的uss、pss、rss和vss等内存数据,保证准确性。 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define BUF_SIZE 1024 int main(int argc, char *argv[]) { if (argc < 2) { printf("Usage: %s <pid>\n", argv[0]); exit(EXIT_FAILURE); } char filename[BUF_SIZE]; snprintf(filename, BUF_SIZE, "/proc/%s/smaps", argv[1]); FILE *fp = fopen(filename, "r"); if (!fp) { printf("Failed to open file %s\n", filename); exit(EXIT_FAILURE); } char line[BUF_SIZE]; unsigned long uss = 0, pss = 0, rss = 0, vss = 0; while (fgets(line, BUF_SIZE, fp)) { unsigned long start, end, size, rss_val; char tmp[BUF_SIZE], permissions[5], dev[6], mapname[BUF_SIZE]; if (sscanf(line, "%lx-%lx %4s %lx %5s %lu %s", &start, &end, permissions, &size, dev, &rss_val, mapname) < 7) { continue; } if (strstr(line, "Private")) { uss += size; pss += rss_val; } rss += rss_val; vss += size; } fclose(fp); printf("USS: %lu KB\n", uss / 1024); printf("PSS: %lu KB\n", pss / 1024); printf("RSS: %lu KB\n", rss / 1024); printf("VSS: %lu KB\n", vss / 1024); return 0; } ``` 这段代码可以通过读取 /proc/PID/smaps 文件来获取进程的uss、pss、rss和vss等内存数据。它会逐行读取 smaps 文件中的内容,解析每个内存映射区域的详细信息,并根据计算公式计算出相应的内存数据。具体来说,它会统计每个映射区域的uss、pss、rss和vss等数据,然后累加得到进程的总的uss、pss、rss和vss等数据。最后,它会输出这些内存数据到控制台上。 需要注意的是,这段代码只能获取当前进程的内存数据,需要传入要查询的进程的 PID 作为参数。同时,由于计算公式和内存映射区域的解析方式可能会因操作系统版本的不同而有所区别,因此需要测试和验证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值