Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-release > by-pkgid > 265a7483afc48e27c236b36e810be507 > files > 94

lkmpg-1.1.0-23.mga7.noarch.rpm

<HTML
><HEAD
><TITLE
>Hello World</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="The Linux Kernel Module Programming Guide"
HREF="book1.htm"><LINK
REL="PREVIOUS"
TITLE="How Do Modules Get Into The Kernel?"
HREF="x49.htm"><LINK
REL="NEXT"
TITLE="Compiling Kernel Modules"
HREF="x209.htm"></HEAD
><BODY
CLASS="CHAPTER"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>The Linux Kernel Module Programming Guide</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="x49.htm"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="x209.htm"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="AEN147"
></A
>Chapter 2. Hello World</H1
><DIV
CLASS="TOC"
><DL
><DT
><B
>Table of Contents</B
></DT
><DT
><A
HREF="c147.htm#AEN149"
>Hello, World (part 1): The Simplest Module</A
></DT
><DT
><A
HREF="x209.htm"
>Compiling Kernel Modules</A
></DT
><DT
><A
HREF="x236.htm"
>Hello World (part 2)</A
></DT
><DT
><A
HREF="x264.htm"
>Hello World (part 3): The <TT
CLASS="LITERAL"
>__init</TT
> and <TT
CLASS="LITERAL"
>__exit</TT
> Macros</A
></DT
><DT
><A
HREF="x298.htm"
>Hello World (part 4): Licensing and Module Documentation</A
></DT
><DT
><A
HREF="x333.htm"
>Passing Command Line Arguments to a Module</A
></DT
><DT
><A
HREF="x365.htm"
>Modules Spanning Multiple Files</A
></DT
><DT
><A
HREF="x419.htm"
>Building modules for a precompiled kernel</A
></DT
></DL
></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="AEN149"
></A
>Hello, World (part 1): The Simplest Module</H1
><P
>When the first caveman programmer chiseled the first program on the walls of the first cave computer, it was a program
	to paint the string `Hello, world' in Antelope pictures.  Roman programming textbooks began with the `Salut, Mundi' program.
	I don't know what happens to people who break with this tradition, but I think it's safer not to find out.  We'll start with a
	series of hello world programs that demonstrate the different aspects of the basics of writing a kernel module.</P
><P
>Here's the simplest module possible.  Don't compile it yet; we'll cover module compilation in the next section.</P
><A
NAME="AEN153"
></A
><DIV
CLASS="EXAMPLE"
><A
NAME="AEN156"
></A
><P
><B
>Example 2-1. hello-1.c</B
></P
><PRE
CLASS="PROGRAMLISTING"
>/*  
 *  hello-1.c - The simplest kernel module.
 */
#include &#60;linux/module.h&#62;	/* Needed by all modules */
#include &#60;linux/kernel.h&#62;	/* Needed for KERN_ALERT */

int init_module(void)
{
	printk("&#60;1&#62;Hello world 1.\n");

	/* 
	 * A non 0 return means init_module failed; module can't be loaded. 
	 */
	return 0;
}

void cleanup_module(void)
{
	printk(KERN_ALERT "Goodbye world 1.\n");
}</PRE
></DIV
><A
NAME="AEN160"
></A
><A
NAME="AEN163"
></A
><P
>Kernel modules must have at least two functions: a "start" (initialization) function called
	<TT
CLASS="FUNCTION"
>init_module()</TT
> which is called when the module is insmoded into the kernel, and an "end" (cleanup)
	function called <TT
CLASS="FUNCTION"
>cleanup_module()</TT
> which is called just before it is rmmoded.  Actually, things have
	changed starting with kernel 2.3.13.  You can now use whatever name you like for the start and end functions of a module, and
	you'll learn how to do this in <A
HREF="x236.htm"
>the Section called <I
>Hello World (part 2)</I
></A
>.  In fact, the new method is the preferred method.  However, many
	people still use <TT
CLASS="FUNCTION"
>init_module()</TT
> and <TT
CLASS="FUNCTION"
>cleanup_module()</TT
> for their start and end
	functions.</P
><P
>Typically, <TT
CLASS="FUNCTION"
>init_module()</TT
> either registers a handler for something with the kernel, or it replaces
	one of the kernel functions with its own code (usually code to do something and then call the original function).  The
	<TT
CLASS="FUNCTION"
>cleanup_module()</TT
> function is supposed to undo whatever <TT
CLASS="FUNCTION"
>init_module()</TT
> did, so the
	module can be unloaded safely.</P
><P
>Lastly, every kernel module needs to include <TT
CLASS="FILENAME"
>linux/module.h</TT
>.  We needed to
	include <TT
CLASS="FILENAME"
>linux/kernel.h</TT
> only for the macro expansion for the
	<TT
CLASS="FUNCTION"
>printk()</TT
> log level, <TT
CLASS="VARNAME"
>KERN_ALERT</TT
>, which you'll learn about in <A
HREF="c147.htm#INTRODUCINGPRINTK"
>the Section called <I
>Introducing <TT
CLASS="FUNCTION"
>printk()</TT
></I
></A
>.</P
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="INTRODUCINGPRINTK"
></A
>Introducing <TT
CLASS="FUNCTION"
>printk()</TT
></H2
><A
NAME="AEN185"
></A
><A
NAME="AEN188"
></A
><P
>Despite what you might think, <TT
CLASS="FUNCTION"
>printk()</TT
> was not meant to communicate information to the user,
			even though we used it for exactly this purpose in <SPAN
CLASS="APPLICATION"
>hello-1</SPAN
>!  It happens to be a logging
			mechanism for the kernel, and is used to log information or give warnings.  Therefore, each <TT
CLASS="FUNCTION"
>printk()</TT
>
			statement comes with a priority, which is the <TT
CLASS="VARNAME"
>&lt;1&gt;</TT
> and <TT
CLASS="VARNAME"
>KERN_ALERT</TT
> you see.
			There are 8 priorities and the kernel has macros for them, so you don't have to use cryptic numbers, and you can view them
			(and their meanings) in <TT
CLASS="FILENAME"
>linux/kernel.h</TT
>.  If you don't specify a priority level, the
			default priority, <TT
CLASS="LITERAL"
>DEFAULT_MESSAGE_LOGLEVEL</TT
>, will be used.</P
><P
>Take time to read through the priority macros.  The header file also describes what each priority means.  In
			practise, don't use number, like <TT
CLASS="LITERAL"
>&lt;4&gt;</TT
>.  Always use the macro, like
			<TT
CLASS="LITERAL"
>KERN_WARNING</TT
>.</P
><P
>If the priority is less than <TT
CLASS="VARNAME"
>int console_loglevel</TT
>, the message is printed on your current
			terminal.  If both <B
CLASS="COMMAND"
>syslogd</B
> and <SPAN
CLASS="APPLICATION"
>klogd</SPAN
> are running, then the message will also
			get appended to <TT
CLASS="FILENAME"
>/var/log/messages</TT
>, whether it got printed to the console or not.  We use a high
			priority, like <TT
CLASS="LITERAL"
>KERN_ALERT</TT
>, to make sure the <TT
CLASS="FUNCTION"
>printk()</TT
> messages get printed to your
			console rather than just logged to your logfile.  When you write real modules, you'll want to use priorities that are
			meaningful for the situation at hand.</P
></DIV
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="x49.htm"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="book1.htm"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="x209.htm"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>How Do Modules Get Into The Kernel?</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
>&nbsp;</TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Compiling Kernel Modules</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>