Чтение онлайн

на главную - закладки

Жанры

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

Померанц Ори

Шрифт:

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

 /* 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<len && message[i]; i++) put_user(message[i], buf+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<MESSAGE_LENGTH-1 && i<length; 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 */

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

 return i;

}

/* This function decides whether to allow an operation

* (return zero) or not allow it (return a non-zero

* which indicates why it is not allowed).

*

* The operation can be one of the following values:

* 0 - Execute (run the "file" - meaningless in our case)

* 2 - Write (input to the kernel module)

* 4 - Read (output from the kernel module)

*

* This is the real function that checks file

* permissions. The permissions returned by ls -l are

* for reference only, and can be overridden here. */

static int module_permission(struct inode *inode, int op) {

 /* We allow everybody to read from our module, but only root (uid 0) may write to it */

Поделиться:
Популярные книги

Ищу жену с прицепом

Рам Янка
2. Спасатели
Любовные романы:
современные любовные романы
6.25
рейтинг книги
Ищу жену с прицепом

Неудержимый. Книга XXVI

Боярский Андрей
26. Неудержимый
Фантастика:
фэнтези
попаданцы
5.00
рейтинг книги
Неудержимый. Книга XXVI

Враг из прошлого тысячелетия

Еслер Андрей
4. Соприкосновение миров
Фантастика:
фэнтези
попаданцы
аниме
5.00
рейтинг книги
Враг из прошлого тысячелетия

Седьмой Рубеж IV

Бор Жорж
4. 5000 лет темноты
Фантастика:
фэнтези
попаданцы
5.00
рейтинг книги
Седьмой Рубеж IV

Неучтенный. Дилогия

Муравьёв Константин Николаевич
Неучтенный
Фантастика:
боевая фантастика
попаданцы
7.98
рейтинг книги
Неучтенный. Дилогия

Идеальный мир для Лекаря 15

Сапфир Олег
15. Лекарь
Фантастика:
боевая фантастика
юмористическая фантастика
аниме
5.00
рейтинг книги
Идеальный мир для Лекаря 15

Назад в ссср 6

Дамиров Рафаэль
6. Курсант
Фантастика:
попаданцы
альтернативная история
6.00
рейтинг книги
Назад в ссср 6

Черный дембель. Часть 5

Федин Андрей Анатольевич
5. Черный дембель
Фантастика:
попаданцы
альтернативная история
5.00
рейтинг книги
Черный дембель. Часть 5

Княжна попаданка. Последняя из рода

Семина Дия
1. Княжна попаданка. Магическая управа
Фантастика:
попаданцы
альтернативная история
историческое фэнтези
аниме
сказочная фантастика
фэнтези
5.00
рейтинг книги
Княжна попаданка. Последняя из рода

Отверженный IX: Большой проигрыш

Опсокополос Алексис
9. Отверженный
Фантастика:
попаданцы
альтернативная история
аниме
5.00
рейтинг книги
Отверженный IX: Большой проигрыш

Город Богов 3

Парсиев Дмитрий
3. Профсоюз водителей грузовых драконов
Фантастика:
юмористическое фэнтези
городское фэнтези
попаданцы
5.00
рейтинг книги
Город Богов 3

Найди меня Шерхан

Тоцка Тала
3. Ямпольские-Демидовы
Любовные романы:
современные любовные романы
короткие любовные романы
7.70
рейтинг книги
Найди меня Шерхан

Мастер 5

Чащин Валерий
5. Мастер
Фантастика:
попаданцы
аниме
5.00
рейтинг книги
Мастер 5

По осколкам твоего сердца

Джейн Анна
2. Хулиган и новенькая
Любовные романы:
современные любовные романы
5.56
рейтинг книги
По осколкам твоего сердца