I'm trying to implement linked list (or circular linked list as it is defined in <linux/list.h>) traversal in linux kernel using sequence files while I want to print data from all nodes to seq file and read them in user space application. I'm not a big fan of C programming so I don't understand how to achieve this. I know how to read/write to proc file using procfs (read/write) functions. Right now I'm using those, so when client application asks for data from linux kernel in proc read function I simply go through the list and store all data in buffer which I later copy to user. Problem is that this buffer may overflow, that's why I want to use seq files.
I understand basic concept of seq files, but I don't know how to exactly do list traversal and printing as I can't pass pointer to my node structure to iterating function.
This is struct which represents linked list's node:
struct my_data {
unsigned int id;
unsigned char action;
struct list_head list;
};
These are my definitions for seq_file operations
static void *seq_start(struct seq_file *s, loff_t *pos)
{
/* The entire data structure for this iterator is a single loff_t
* value holding the current position.
*/
loff_t *spos = kmalloc(sizeof(loff_t), GFP_KERNEL);
if (!spos)
return NULL;
*spos = *pos;
return spos;
}
static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
{
return pos;
}
static void seq_stop(struct seq_file *s, void *v)
{
}
static int seq_show(struct seq_file *s, void *v)
{
seq_printf(s, "print stuff\n");
return 0;
}
Now to the point. I would really appreciate if someone enlighten me how to iterate over linked list using seq_next() function and how to access data stored in nodes in seq_show().
seq_list_*()functions. You can find those functions usage in kernel and use found code as example. For example, see next files: drivers/input/input.c, drivers/char/misc.c.