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



>#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];


> /* We return 0 to indicate end of file, that we have

> * no more information. Otherwise, processes will

> * continue to read from us in an endless loop. */

> if (finished) {

>  finished = 0;

>  return 0;

> }


> /* We use put_user to copy the string from the kernel's

> * memory segment to the memory segment of the process

> * that called us. get_user, BTW, is

> * used for the reverse. */

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

> for(i=0; i


> /* Notice, we assume here that the size of the message

> * is below len, or it will be received cut. In a real

> * life situation, if the size of the message is less

> * than len then we'd return len and on the second call

> * start filling the buffer with the len+1'th byte of the message. */

> 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 */

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

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

> int length) /* The buffer's length */

>#endif

>{

> int i;

> /* Put the input into Message, where module_output will later be able to use it */

> for (i=0; i

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

>  get_user(Message[i], buf+i);

>  /* In version 2.2 the semantics of get_user changed,

>  * it not longer returns a character, but expects a

>  * variable to fill up as its first argument and a

>  * user segment pointer to fill it from as the its  second.

>  *

>  * The reason for this change is that the version 2.2

>  * get_user can also read an short or an int. The way

>  * it knows the type of the variable it should read

>  * is by using sizeof, and for that it needs the

>  * variable itself. */

>#else

>  Message[i] = get_user(buf+i);

>#endif

> Message[i] = '\0'; /* we want a standard, zero terminated string */