如何使用procfs将内核数据发送到用户空间?

如何使用procfs将内核数据发送到用户空间?,c,linux,linux-kernel,procfs,C,Linux,Linux Kernel,Procfs,我正在计算内核中的时间戳,稍后我想将时间戳从内核传输到用户空间。所以我使用procfs在内核和用户之间进行通信。我使用procfile\u read函数从内核发送数据,如下所示 我修改并计算了内核代码的时间戳,如下所示 此代码位于网络设备驱动程序级别 int netif_rx(struct sk_buff *skb) { __net_timestamp(skb);//I modify the code in kernel to get the timestamp and store i

我正在计算内核中的时间戳,稍后我想将时间戳从内核传输到用户空间。所以我使用procfs在内核和用户之间进行通信。我使用procfile\u read函数从内核发送数据,如下所示

我修改并计算了内核代码的时间戳,如下所示

此代码位于网络设备驱动程序级别

int netif_rx(struct sk_buff *skb) 
{
    __net_timestamp(skb);//I modify the code in kernel to get the timestamp and store in buffer
    // or I can use : skb->tstamp = ktime_get_real(); //to get the timestamp
}

/**
 *  procfs2.c -  create a "file" in /proc
 *
 */

#include <linux/module.h>   /* Specifically, a module */
#include <linux/kernel.h>   /* We're doing kernel work */
#include <linux/proc_fs.h>  /* Necessary because we use the proc fs */
#include <asm/uaccess.h>    /* for copy_from_user */

#define PROCFS_MAX_SIZE     1024
#define PROCFS_NAME         "buffer1k"

/**
 * This structure hold information about the /proc file
 *
 */
static struct proc_dir_entry *Our_Proc_File;

/**
 * The buffer used to store character for this module
 *
 */
static char procfs_buffer[PROCFS_MAX_SIZE];

/**
 * The size of the buffer
 *
 */
static unsigned long procfs_buffer_size = 0;

/** 
 * This function is called then the /proc file is read
 *
 */
int 
procfile_read(char *buffer,
          char **buffer_location,
          off_t offset, int buffer_length, int *eof, void *data)
{
    int ret;

    printk(KERN_INFO "procfile_read (/proc/%s) called\n", PROCFS_NAME);

    if (offset > 0) {
        /* we have finished to read, return 0 */
        ret  = 0;
    } else {
        /* fill the buffer, return the buffer size */
        memcpy(buffer, procfs_buffer, procfs_buffer_size);
        ret = procfs_buffer_size;
    }

    return ret;
}

/**
 *This function is called when the module is loaded
 *
 */
int init_module()
{
    /* create the /proc file */
    Our_Proc_File = create_proc_entry(PROCFS_NAME, 0644, NULL);

    if (Our_Proc_File == NULL) {
        remove_proc_entry(PROCFS_NAME, &proc_root);
        printk(KERN_ALERT "Error: Could not initialize /proc/%s\n",
            PROCFS_NAME);
        return -ENOMEM;
    }

    Our_Proc_File->read_proc  = procfile_read;
    Our_Proc_File->owner      = THIS_MODULE;
    Our_Proc_File->mode       = S_IFREG | S_IRUGO;
    Our_Proc_File->uid    = 0;
    Our_Proc_File->gid    = 0;
    Our_Proc_File->size       = 37;

    printk(KERN_INFO "/proc/%s created\n", PROCFS_NAME);    
    return 0;   /* everything is ok */
}

/**
 *This function is called when the module is unloaded
 *
 */
void cleanup_module()
{
    remove_proc_entry(PROCFS_NAME, &proc_root);
    printk(KERN_INFO "/proc/%s removed\n", PROCFS_NAME);
}
int netif_rx(结构sk_buff*skb)
{
__net_timestamp(skb);//我修改内核中的代码以获取时间戳并存储在缓冲区中
//或者我可以使用:skb->tstamp=ktime_get_real();//来获取时间戳
}
/**
*c-在/proc中创建一个“文件”
*
*/
#包括/*特别是模块*/
#include/*我们正在做内核工作*/
#包括/*必要,因为我们使用proc fs*/
#包含/*用于从用户处复制*/
#定义PROCFS_MAX_SIZE 1024
#定义PROCFS_名称“buffer1k”
/**
*此结构保存有关/proc文件的信息
*
*/
静态结构程序目录项*我们的程序文件;
/**
*用于存储此模块的字符的缓冲区
*
*/
静态字符procfs_缓冲区[procfs_MAX_SIZE];
/**
*缓冲区的大小
*
*/
静态无符号长procfs\u buffer\u size=0;
/** 
*调用此函数,然后读取/proc文件
*
*/
int
procfile_读取(字符*缓冲区,
字符**缓冲区位置,
off_t offset,int buffer_length,int*eof,void*data)
{
int ret;
printk(KERN\u INFO“procfile\u read(/proc/%s)调用\n”,PROCFS\u NAME);
如果(偏移量>0){
/*我们已完成读取,返回0*/
ret=0;
}否则{
/*填充缓冲区,返回缓冲区大小*/
memcpy(缓冲区、procfs\u缓冲区、procfs\u缓冲区大小);
ret=程序缓冲区大小;
}
返回ret;
}
/**
*加载模块时调用此函数
*
*/
int init_模块()
{
/*创建/proc文件*/
我们的\u Proc\u文件=创建\u Proc\u条目(PROCFS\u NAME,0644,NULL);
if(我们的_Proc_文件==NULL){
删除进程项(进程名和进程根);
printk(内核警报“错误:无法初始化/proc/%s\n”,
PROCFS_NAME);
return-ENOMEM;
}
我们的程序文件->读取程序=程序文件读取;
我们的\u Proc\u File->owner=此\u模块;
我们的程序文件->模式=S|IFREG | S|IRUGO;
我们的程序文件->uid=0;
我们的程序文件->gid=0;
我们的程序文件->大小=37;
printk(KERN\u INFO)/proc/%s已创建\n“,PROCFS\u名称);
返回0;/*一切正常*/
}
/**
*卸载模块时调用此函数
*
*/
void cleanup_模块()
{
删除进程项(进程名和进程根);
printk(内核信息“/proc/%s已删除\n”,PROCFS\u名称);
}
模块初始化使用create_proc_entry()建立一个procfs条目。函数procfile\u write和procfile\u read被初始化以处理对该条目的写入和读取。卸载模块时调用的模块的cleanup_module()函数通过调用cleanup_module()删除procfs条目


我的问题是如何将计算出的时间戳添加到procfile_read函数中,并将其发送到用户空间?

粗糙而简单:使用时间戳在模块中设置一个全局变量,并使用snprintf将其复制到profs_缓冲区中。事实上,您可以直接在提供的缓冲区中执行snprintf,然后一起释放procfs\u缓冲区


这种方法存在多个问题(例如,它不是原子的),但如果您只是想要一个粗略的调试或日志记录界面,它就可以工作。

非常感谢您,请给我一个小例子??对于粗略的调试或日志记录界面,
debugfs
可能是一个更好的选择。它具有方便的函数,可以访问典型的变量类型,例如64位int(应该可以存储时间戳)??