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

 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);

#else

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

#endif

 /* we want a standard, zero terminated string */

 Message[i] = '\0';

 /* We need to return the number of input characters used */

 return i;

}

/* 1 if the file is currently open by somebody */

int Already_Open = 0;

 /* Queue of processes who want our file */

Перейти на страницу:

Все книги серии Проект russian ldp

Похожие книги