Saturday, August 30, 2008

Life update

I should start practicing for my driving license test...(breakevide... :-P)

A question to 8-year olds!!:-o

Find the smallest number that when divided by n yields the remainder (n-1) for 2 <= n <= 10 (ie. dividing n by 10 yields 9, by 9 yields 8...).

Believe it or not, this was one of the questions asked for a 3rd standard annual exam (state syllabus).

Design documents

Code and design are maintained as separate files in almost all software projects. A major task faced by all IT firms is ensuring the consistency between the source code and the corresponding design document. Code maintainers confused by inconsistent design documents are commonplace. Literate programming might be the first step towards a solution.

Exposing module internals using seq_file interface

Learn all about seq_file interface here
http://www.xenotime.net/linux/doc/seq_file_howto.txt

The seq_file mechanism provides a more straightforward, non-iterator style, interface. A driver writer may simply define show() with operations that output data to proc file.

First of all, we have to create a file in /proc. For this, we have to call create_proc_entry in our module initialization function.

struct proc_dir_entry *foo = create_proc_entry("foo", 0, NULL); /* This will create "/proc/foo" */

Then initialize the proc file operations structure
foo->proc_fops = &foo_proc_operations;

where foo_proc_operations is defined as,

static struct file_operations foo_proc_operations = {
.open = foo_proc_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
seq_read, seq_lseek, and single_release are functions defined by Linux seq_file core.

Within foo_proc_open we have to call single_open() and pass it "show()", the function performing actual display.

return single_open(file, foo_proc_show, NULL);

Within foo_proc_show, we can use seq_{putc|puts|printf} to output data to "/proc/foo". These functions work like normal putc|puts|printf.

static int foo_proc_show(struct seq_file *m, void *v)
{
seq_puts(m, "Hello from foo\n"); /* write to our proc file */
return 0;
}

And finally don't forget to remove the proc file by calling,

remove_proc_entry("foo", NULL);