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



Причина по которой мы используем proc_register_dynamic[2] в том, что мы не хотим определять inode номер, используемый для нашего файла заранее, но позволяем ядру определять его, чтобы предотвратить столкновения. В нормальных файловых системах размещенных на диске, а не только в памяти (как /proc) inode является указателем на то место, в котором на диске размещен индексный узел файла (кратко, inode). Inode содержит информацию относительно файла, например разрешения файла, вместе с указателем на то место, где могут быть найдены данные файла.

Поскольку никаких наших функций не вызывается когда файл открывается или закрывается, некуда поместить MOD_INC_USE_COUNT и MOD_DEC_USE_COUNT в этом модуле, и если файл открыт и затем модуль удален, не имеется никакого способа избежать проблем. В следующей главе мы рассмотрим более тяжелый в реализации, но более гибкий путь имеющий дело с файлами из /proc, который позволит нам защититься от этой проблемы.

procfs.c

>/* procfs.c - create a "file" in /proc

>* Copyright (C) 1998-1999 by Ori Pomerantz

>*/


>/* The necessary header files */

>/* Standard in kernel modules */

>#include /* We're doing kernel work */

>#include /* Specifically, a module */


>/* Deal with CONFIG_MODVERSIONS */

>#if CONFIG_MODVERSIONS==1

>#define MODVERSIONS

>#include

>#endif


>/* Necessary because we use the proc fs */

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


>/* 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 /fs/proc/array.c.