I'm running some experiments with seq_file
s and have some confusion regarding it.
I analyzed implementation of common functions from seq_file.c and judging by seq_printf
implementation the internal char *buf
of the struct seq_file
is used entirely to store a formatted string to copy to a user in seq_read
. But there is seq_write
function defined in in seq_file.c which can write to the buffer.
QUESTION: Is it possible to reuse the struct seq_file
's internal buffer and use it for writing data coming from user or it is for data formatting only?
I currently used another buffer for writing data and struct seq_file
for data formatting only:
static char buf[4096];
static char *limit = buf;
void *pfsw_seq_start(struct seq_file *m, loff_t *pos){
if(*pos >= limit - buf) {
return NULL;
}
char *data = buf + *pos;
*pos = limit - buf;
return data;
}
void pfsw_seq_stop(struct seq_file *m, void *v){ }
void *pfsw_seq_next(struct seq_file *m, void *v, loff_t *pos){ return NULL; }
int pfsw_seq_show(struct seq_file *m, void *v){
seq_printf(m, "Data: %s\n", (char *) v);
return 0;
}
ssize_t pfsw_seq_write(struct file *filp, const char __user * user_data, size_t sz, loff_t *off){
if(*off < 0 || *off > sizeof buf){
return -EINVAL;
}
size_t space_left_from_off = sizeof buf - (size_t) *off;
size_t bytes_to_write = space_left_from_off <= sz ? space_left_from_off : sz;
if(copy_from_user(buf + *off, user_data, bytes_to_write)){
return -EAGAIN;
}
*off += bytes_to_write;
if(*off > limit - buf){
limit = buf + *off;
}
return bytes_to_write;
}
So I defined struct file_operations
as
static const struct seq_operations seq_ops = {
.start = pfsw_seq_start,
.stop = pfsw_seq_stop,
.next = pfsw_seq_next,
.show = pfsw_seq_show
};
int pfsw_seq_open(struct inode *ino, struct file *filp){
return seq_open(filp, &seq_ops);
}
static const struct file_operations fops = {
.open = pfsw_seq_open,
.read = seq_read,
.write = pfsw_seq_write,
.release = seq_release,
};