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



3. Объедините все объектные файлы в один. Под x86 это делается командой:

>ld -m elf_i386 -r -o <имя_модуля>.o <1-ый исходный файл>.o <2-ой исходный файл>.o.

Пример такого модуля:

start.c

>/* start.c

>* Copyright (C) 1999 by Ori Pomerantz

>*

>* "Hello, world" - the kernel module version.

>* This file includes just the start routine

>*/


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


>/* Initialize the module */

>int init_module() {

> printk("Hello, world - this is the kernel speaking\n");


> /* If we return a non zero value, it means that

>  * init_module failed and the kernel module

>  * can't be loaded */

> return 0;

>}

stop.c   

>/* stop.c

>* Copyright (C) 1999 by Ori Pomerantz

>*

>* "Hello, world" - the kernel module version. This

>* file includes just the stop routine.

>*/


>/* The necessary header files */

>/* Standard in kernel modules */

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


>#define __NO_VERSION__ /* This isn't "the" file of the kernel module */

>#include /* Specifically, a module */

>#include /* Not included by module.h because of the __NO_VERSION__ */


>/* Deal with CONFIG_MODVERSIONS */

>#if CONFIG_MODVERSIONS==1

>#define MODVERSIONS

>#include

>#endif


>/* Cleanup - undid whatever init_module did */

>void cleanup_module(){

> printk("Short is the life of a kernel module\n");

>}

Makefile

># Makefile for a multifile kernel module

>CC=gcc

>MODCFLAGS := -Wall -DMODULE -D__KERNEL__ -DLINUX


>hello.o:        start.o stop.o

>                ld -m elf_i386 -r -o hello.o start.o stop.o


>start.o:        start.c /usr/include/linux/version.h

>                $(CC) $(MODCFLAGS) -c start.c


>stop.o:         stop.c /usr/include/linux/version.h

>                $(CC) $(MODCFLAGS) -c stop.c

Файлы символьных устройств

Имеются два главных пути для общения модуля разговаривать с процессами. Первый идет через файлы устройства (подобно файлам в каталоге /dev), другой должен использовать файловую систему proc. Поскольку одной из главных причин написания модуля ядра, является поддержка некоего аппаратного устройства, мы начнем с файлов устройства. 

Первоначальная цель файлов устройства состоит в том, чтобы позволить процессам связываться с драйверами устройства в ядре, и через них с физическими устройствами (модемы, терминалы, и т.д.).