[英]Trying to write a struct to a proc file from kernel space
我正在嘗試將結構寫入proc
文件(我知道我不應該這樣做)。 我能夠使用seq_print
方法成功寫出struct成員,但是我想嘗試編寫該struct,這樣就不必在用戶空間中對其進行解析。 我不確定如何使用seq_file
實現對其進行寫入。 下面是我正在嘗試的代碼。 該seq_show
功能是我的一部分.read,
file_operation結構回調機制( file_operations
, seq_file
, .start
, .next
, .stop
, .show
)時/proc/info
是從用戶空間打開。
typedef struct tmp_t{
int number;
uint_32 info;
char name[10];
} tmp_t;
static int seq_show(struct seq_file *s, void *v)
{
ssize_t proc_write;
mm_segment_t old_fs;
struct file *file;
loff_t offset = 0;
old_fs = get_fs();
set_fs(get_ds());
file = filp_open("/proc/info", O_WRONLY, 0777);
if(file){
//tmp is a global pointer to the struct I want to write
proc_write = vfs_write(file, tmp, sizeof(tmp_t), &offset);
// I've also tried this using the v parameter
//proc_write = vfs_write(file, tmp, sizeof(tmp_t), v);
fput(file);
printk(KERN_ALERT "proc_write: %zd.\n");
}
filp_close(file, NULL);
set_fs(old_fs);
return 0;
}
static void *info_start(struct seq_file *s, loff_t *pos)
{
}
static void *info_next(struct seq_file *s, void *v, loff_t *pos)
{
}
static void info_stop(struct seq_file *s, void *v)
{
}
static int open_info(struct inode *inode, struct file *file)
{
return seq_open(file, &seq_ops_info);
}
static struct seq_operations seq_ops_info = {
.start = info_start,
.next = info_next,
.stop = info_stop,
.show = info_show,
};
static const struct file_operations fops_info = {
.owner = THIS_MODULE,
.open = open_info,
.read = seq_read,
.llseek = seq_lseek,
.release = seq_release,
};
static int __init app_init(void)
{
info_file = proc_create("info", 0777, NULL, &fops_info);
}
printk語句給出proc_write: -5.
我將這些結構存儲在鏈接列表中,因此通過seq_file的每次迭代我都想將該結構寫入/proc/info
。 我正在嘗試處理在內核源代碼樹中找到的該示例 。 任何幫助表示贊賞。
據我所知,您無法輕松地在/proc
中創建和寫入文件。 最有可能的是,對filp_open()
調用失敗,並且您沒有檢查它的返回值是否正確。 您必須使用IS_ERR()
函數檢查返回值。
但我要再次提及,這個想法對我來說似乎是錯誤的。 如果要在/proc
目錄中創建條目,請考慮使用標准方式 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.