looking for simple kernel source examples of, well, everything

Robert P. J. Day rpjday at crashcourse.ca
Tue Oct 23 09:38:20 EDT 2012


  while writing/updating a pile of kernel courseware over the next
couple of months, i'm interested in bolstering everything in the
courseware with actual examples ripped from the source code, so
students can see all of the concepts in action.

  for example, to show how to use seq_file to write a trivial
read-only proc file, i always point at fs/proc/version.c:

===== start =====

#include <linux/fs.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/utsname.h>

static int version_proc_show(struct seq_file *m, void *v)
{
        seq_printf(m, linux_proc_banner,
                utsname()->sysname,
                utsname()->release,
                utsname()->version);
        return 0;
}

static int version_proc_open(struct inode *inode, struct file *file)
{
        return single_open(file, version_proc_show, NULL);
}

static const struct file_operations version_proc_fops = {
        .open           = version_proc_open,
        .read           = seq_read,
        .llseek         = seq_lseek,
        .release        = single_release,
};

static int __init proc_version_init(void)
{
        proc_create("version", 0, NULL, &version_proc_fops);
        return 0;
}
module_init(proc_version_init);

===== end =====

  not surprisingly, students can *immediately* see how to use that as
the basis to write their own.

  if i want to show a seq_file that is actually being used
sequentially, i typically like to show how /proc/devices is
implemented (see fs/proc/devices.c).

  anyway, you get the idea.  it's a wide open invitation -- i'm just
trying to collect really elegant examples from the kernel source code
to explain various concepts.  if you have any favourites, by all
means, post them and i'll start collecting them somewhere public.

rday

-- 

========================================================================
Robert P. J. Day                                 Ottawa, Ontario, CANADA
                        http://crashcourse.ca

Twitter:                                       http://twitter.com/rpjday
LinkedIn:                               http://ca.linkedin.com/in/rpjday
========================================================================



More information about the Kernelnewbies mailing list