Энциклопедия разработчика модулей ядра Linux | страница 32



>#include /* Specifically, a module */


>/* Deal with CONFIG_MODVERSIONS */

>#if CONFIG_MODVERSIONS==1

>#define MODVERSIONS

>#include

>#endif


>/* Necessary because we use proc fs */

>#include


>/* For putting processes to sleep and waking them up */

>#include

>#include


>/* In 2.2.3 /usr/include/linux/version.h includes a

>* macro for this, but 2.0.35 doesn't - so I add it

>* here if necessary. */

>#ifndef KERNEL_VERSION

>#define KERNEL_VERSION(a,b,c) ((a)*65536+(b)*256+(c))

>#endif


>#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)

>#include /* for get_user and put_user */

>#endif


>/* The module's file functions ********************** */

>/* Here we keep the last message received, to prove

>* that we can process our input */

>#define MESSAGE_LENGTH 80

>static char Message[MESSAGE_LENGTH];


>/* Since we use the file operations struct, we can't use

>* the special proc output provisions - we have to use

>* a standard read function, which is this function */

> #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)

>static size_t module_output(

> struct file *file, /* The file read */

> char *buf, /* The buffer to put data to (in the user segment) */

> size_t len, /* The length of the buffer */

> loff_t *offset) /* Offset in the file - ignore */

>#else

> static int module_output(

> struct inode *inode, /* The inode read */

> struct file *file, /* The file read */

> char *buf, /* The buffer to put data to (in the user segment) */

> int len) /* The length of the buffer */

>#endif

>{

> static int finished = 0;

> int i;

> char message[MESSAGE_LENGTH+30];

> /* Return 0 to signify end of file - that we have

> * nothing more to say at this point. */

> if (finished) {

>  finished = 0;

>  return 0;

> }

> /* If you don't understand this by now, you're

> * hopeless as a kernel programmer. */

> sprintf(message, "Last input:%s\n", Message);

> for(i=0; i

> finished = 1;

> return i; /* Return the number of bytes "read" */

>}


>/* This function receives input from the user when

>* the user writes to the /proc file. */

>#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)

>static ssize_t module_input(

> struct file *file, /* The file itself */

> const char *buf, /* The buffer with input */

> size_t length, /* The buffer's length */

> loff_t *offset) /* offset to file - ignore */

>#else

> static int module_input(

> struct inode *inode, /* The file's inode */