Reputation: 1513
I'm trying to write a structure to a proc
file (I know I'm not suppose to). I'm able to successfully write out the struct members using the seq_print
method but I want to try writing the struct so I don't have to parse it in user space. I'm not sure how to write to it using the seq_file
implementation. Below is the code I'm trying. The seq_show
function is part of my .read,
file_operation struct callback mechanism (file_operations
, seq_file
, .start
, .next
, .stop
, .show
) when the /proc/info
is opened from user space.
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);
}
The printk statement gives proc_write: -5.
I have those structs stored in a link list so each iteration through the seq_file stuff I'd like to write the struct to the /proc/info
. I'm trying to work off of this example that I found in the kernel source tree. Any help is appreciated.
Upvotes: 0
Views: 1628
Reputation: 3768
As far, as I know, you can't create and write file in /proc
that easily. Most likely, call to filp_open()
has failed and you don't check it's return value properly. You have to check return value with IS_ERR()
function.
But I should mention again that this idea seems wrong for me. If you want create an entry in /proc
directory, consider using standard way.
Upvotes: 1