1

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().

3
  • 1
    There are only about a hundred examples in the Linux kernel of how to use seq files to display the contents of linked list. fs/proc is a good start. Commented May 3, 2015 at 12:44
  • 1
    Have you read Documentation/filesystems/seq_file.txt? Particularly, pay attention to 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. Commented May 3, 2015 at 13:13
  • @SamProtsenko This is exactly what I was looking for, thanks for pointing in right direction Commented May 3, 2015 at 13:27

1 Answer 1

2
struct list_head test_head; //head of the link list static void *seq_start(struct seq_file *s, loff_t *pos) { return seq_list_start(&test_head, *pos); } static void *seq_next(struct seq_file *s, void *v, loff_t *pos) { return seq_list_next(v, &test_head, pos); } static int seq_show(struct seq_file *s, void *v) { struct my_data *entry; entry = list_entry(v, struct my_data, _list); seq_printf(seq, "id = %u and action = %u\n", entry->id, entry->action); return 0; } 

seq_list_start and seq_list_next functions have been implemented in fs\seq_file.c

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.