如何内核数据发送到用户使用的procfs的空间? [英] how to send the kernel data to the user the space using procfs?
问题描述
我计算内核中的时间戳,后来我想从内核tmestamp转院到用户空间。所以我使用的procfs的内核和用户之间的通信。我使用的procfile_read功能,如下图所示,从发送内核中的数据。
I am calculating a timestamp in the kernel and later I want to tranfer the tmestamp from kernel to the user space. So I am using procfs for communication between kernel and user. I am using the procfile_read function for sending the data from kernel as shown below.
我修改和计算内核code的时间戳,如下所示。
I modified and calculated the timestamp of the kernel code as shown below.
//这个code是在网络设备驱动程序级别。
//this code is at network device driver level.
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 - 建立在/ proc一个文件
*
* /
/** * 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);
}
模块初始化建立与create_proc_entry一个procfs的条目()。功能procfile_write和procfile_read初始化处理写入和读取这个条目。该模块的函数cleanup_module(),当卸载模块调用,调用在cleanup_module()删除procfs的条目。
The module initialization establishes a procfs entry with create_proc_entry(). The functions procfile_write and procfile_read are initialized to handle writes and reads on this entry. The module's cleanup_module() function, called when the module is unloaded, removes the procfs entry by calling cleanup_module().
我的问题是如何添加计入procfile_read功能的时间戳,并将其发送到用户空间?
my question is how to add the timestamp calculated into the procfile_read function and send it to the user space ??
推荐答案
原油和简单:设定时间戳模块中的全局变量和使用的snprintf将它复制到你的profs_buffer。事实上,你可能会做的snprintf直接进入所提供的缓冲区和松procfs_buffer都在一起。
Crude and simple: set a global variable in your module with the timestamp and use snprintf to copy it into your profs_buffer. In fact, you might do snprintf directly into the provided buffer and loose the procfs_buffer all together.
有这种方法的多种问题(它不是原子,一个),但如果你只是想粗调试或日志接口它的工作原理。
There are multiple problems with this approach (it's not atomic, for one) but if you just want a crude debug or logging interface it works.
这篇关于如何内核数据发送到用户使用的procfs的空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!