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

на главную

Жанры

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

Померанц Ори

Шрифт:

* Copyright (C) 1998-1999 by Ori Pomerantz

*/

/* The necessary header files */

/* Standard in kernel modules */

#include <linux/kernel.h> /* We're doing kernel work */

#include <linux/module.h> /* Specifically, a module */

/* Deal with CONFIG_MODVERSIONS */

#if CONFIG_MODVERSIONS==1

#define MODVERSIONS

#include <linux/modversions.h>

#endif

/* Necessary because we use the proc fs */

#include <linux/proc_fs.h>

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

/* Put data into the proc fs file.

 Arguments

 =========

 1. The buffer where the data is to be inserted, if you decide to use it.

 2. A pointer to a pointer to characters. This is useful if you don't want to use the buffer allocated by the kernel.

 3. The current position in the file.

 4. The size of the buffer in the first argument.

 5. Zero (for future use?).

Usage and Return Value

 ======================

 If you use your own buffer, like I do, put its location in the second argument and return the number of bytes used in the buffer.

 A return value of zero means you have no further information at this time (end of file). A negative return value is an error condition.

 For More Information

 ====================

 The way I discovered what to do with this function wasn't by reading documentation, but by reading the code which used it. I just looked to see what uses the get_info field of proc_dir_entry struct (I used a combination of find and grep, if you're interested), and I saw that it is used in <kernel source directory>/fs/proc/array.c.

 If something is unknown about the kernel, this is usually the way to go. In Linux we have the great advantage of having the kernel source code for free - use it.

*/

int procfile_read(char *buffer, char **buffer_location, off_t offset, int buffer_length, int zero) {

 int len; /* The number of bytes actually used */

 /* This is static so it will still be in memory
 when we leave this function */

 static char my_buffer[80];

 static int count = 1;

 /* We give all of our information in one go, so if the

 * user asks us if we have more information the

 * answer should always be no.

 *

 * This is important because the standard read

 * function from the library would continue to issue

 * the read system call until the kernel replies

 * that it has no more information, or until its * buffer is filled. */

 if (offset > 0) return 0;

 /* Fill the buffer and get its length */

 len = sprintf(my_buffer, "For the %d%s time, go away!\n", count,

(count % 100 > 10 && count % 100 < 14) ? "th" :

(count % 10 == 1) ? "st" : (count % 10 == 2) ? "nd" :

(count % 10 == 3) ? "rd" : "th" );

 count++;

 /* Tell the function which called us where the buffer is */

 *buffer_location = my_buffer;

 /* Return the length */

 return len;

}

struct proc_dir_entry Our_Proc_File = {

 0, /* Inode number - ignore, it will be filled by proc_register[_dynamic] */

 4, /* Length of the file name */

 "test", /* The file name */

 S_IFREG | S_IRUGO, /* File mode - this is a regular file which can be read by its owner, its group, and everybody else */

 1, /* Number of links (directories where the file is referenced) */

 0, 0, /* The uid and gid for the file - we give it * to root */

 80, /* The size of the file reported by ls. */

 NULL, /* functions which can be done on the inode (linking, removing, etc.) - we don't support any. */

 procfile_read, /* The read function for this file, the function called when somebody tries to read something from it. */

 NULL /* We could have here a function to fill the file's inode, to enable us to play with permissions, ownership, etc. */

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

Темный Лекарь 13

Токсик Саша
13. Темный Лекарь
Фантастика:
попаданцы
аниме
фэнтези
5.00
рейтинг книги
Темный Лекарь 13

Сердце Зверя. Том 1. Правда стали, ложь зеркал

Камша Вера Викторовна
7. Отблески Этерны
Фантастика:
фэнтези
8.50
рейтинг книги
Сердце Зверя. Том 1. Правда стали, ложь зеркал

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

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

Локки 7. Потомок бога

Решетов Евгений Валерьевич
7. Локки
Фантастика:
аниме
эпическая фантастика
фэнтези
5.00
рейтинг книги
Локки 7. Потомок бога

Мастер 6

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

Ларь

Билик Дмитрий Александрович
10. Бедовый
Фантастика:
городское фэнтези
мистика
5.75
рейтинг книги
Ларь

Газлайтер. Том 4

Володин Григорий
4. История Телепата
Фантастика:
попаданцы
альтернативная история
аниме
5.00
рейтинг книги
Газлайтер. Том 4

Мастер 8

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

Проект «Оборотень». Пенталогия

Земляной Андрей Борисович
Фантастика:
боевая фантастика
8.31
рейтинг книги
Проект «Оборотень». Пенталогия

Фиктивный брак

Завгородняя Анна Александровна
Фантастика:
фэнтези
6.71
рейтинг книги
Фиктивный брак

Страж Кодекса. Книга VII

Романов Илья Николаевич
7. КО: Страж Кодекса
Фантастика:
фэнтези
попаданцы
аниме
5.00
рейтинг книги
Страж Кодекса. Книга VII

Я – Орк. Том 2

Лисицин Евгений
2. Я — Орк
Фантастика:
юмористическое фэнтези
попаданцы
аниме
5.00
рейтинг книги
Я – Орк. Том 2

Мастер 9

Чащин Валерий
9. Мастер
Фантастика:
боевая фантастика
попаданцы
технофэнтези
аниме
фэнтези
5.00
рейтинг книги
Мастер 9

Архил...? 4

Кожевников Павел
4. Архил...?
Фантастика:
фэнтези
попаданцы
альтернативная история
5.50
рейтинг книги
Архил...? 4