📄 17.html
字号:
<html><head><title>黄金书屋</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><link rel="stylesheet" href="/goldnets.css"></head><body bgcolor="#E4EBF1"><center><a href="http://ad.myrice.com/RealMedia/ads/click_nx.ads/goldnets.myrice.com/banner1@Top" target=_blanck ><script language=JavaScript><!---todayd = new Date();var seconds = todayd.getTime();document.write("<img src=\"http://ad.myrice.com/RealMedia/ads/adstream_nx.ads/goldnets.myrice.com/banner1@Top?dd=seconds\" border=0 width=468 height=60>");//--></script></a></center><br><table width="756" border="0" cellspacing="0" cellpadding="0" align="center" bgcolor="#E4EBF1"> <tr> <td colspan="2" valign="top" align="center"> <div align="center"> <table width="100%" border="0" cellspacing="0" cellpadding="0" height="52"> <tr> <td valign="top"><br> <div align="center"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td valign="bottom"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td><a href="/index.html">首页</a>>> <font color="#CC0000"><a href="/book/152/1015178.html">Linux内核编程</a></font></td> <td width="22%"> <a href="/index.html">[ 点此回首页 ]</a></td> </tr> <tr> <td colspan="2"><img src="/image/1x1.gif" width="1" height="2"></td> </tr> <tr bgcolor="#FFCC00"> <td colspan="2"><img src="/image/1x1.gif" width="1" height="1"></td> </tr> <tr> <td colspan="2"><img src="/image/1x1.gif" width="1" height="6"></td> </tr> </table> </td> </tr> </table> <br> <table width="590" border="0" cellspacing="0" cellpadding="0"> <tr> <td><center><a href='16.html'>上一页</a></center><br><hr><div style=font-size:12pt><pre>常见的错误
在我告诉你们进入实践写内核模块前,还有几个地方需要提醒一下。如果发生了不好
的事情而我没有警告,请把问题报告给我并且索取您为此书给我的报酬的全额赔偿。
1. 使用标准库。你不能那样做。在内核模块中你只能使用内核函数,它们就是在./proc/ksyms
中的那些。
2. 作废中断。也许你需要在短时间内这样做,这是可以的。但是如果你没有重新重新使它
们可用,那么你的系统就会死机,而你需要关机。
3. 冒险。也许我不需要警告你这一条,但是我认为还是需要,以防万一。
2.0和2.2版本的区别
以我对整个内核的模块的了解还不能写出它们所有的区别的文档。在转换这些例子(或
者实际的说,是使用了Emmanuel Papirakis 的改变)的过程中,我遇到了下面的区别。我
把它们列出来帮助内核模块程序员,特别是那些学习过本书前面各版熟悉我使用的技术的程
序员,来转化到新的版本。
1. Asm/success.h 如果你需要put_user或get_user你需要包含它。
2. Get_user 在2.2版中,get_user接到指向我们的内存的指针和指向用此信息设置内核中
的变量的指针。原因是get_user可以一次读2或4个字节,如果要读的变量有2或4个
字节长的话。
3. File_operations 这个结构体现在在open和close之间有一个flush函数。
4. Close in file_open 在2.2版中,close返回一个整型,所以它允许失败。
5. Read and write in file_operations 这个函数头改变了。它们现在返回一个ssize_t而不是
整数,它们的参数列表也改变了。索引节点不再是一个参数,而文件中的偏移地址成了
一个参数。
6. Proc_register_dynamic 这个函数已不存在。代替它的是,调用正常的proc_register,把
0写入这个结构体的索引节点域中。
7. Signals 任务结构中的信号量不再是一个32位整数,而是一个_NSIG_WORDS整数数
组。
8. Queue_task_irq 即使你要调度一个从中断处理程序中发生的任务,你也要使用
queue_task而不是queue_task_irq。
9. Module Parameters 你不需要把模块参数声明为全局变量了。在2.2中你必须要
MODULE_PARM来声明它们的类型。这是一个大的改进,因为它使得模块可以接受以
数字开始的字符串参数而不会混淆。
10. Symmetrical Multi-Processing 内核不再在一个大的连环锁内工作了,这意味着内核模
块必须注意到SMP。
除此以外
我可以很容易的在本书中加入几章。我可以增加一章关于创建新文件系统的内容,或
者增加新的协议栈。我也可以增加对我们从来没接触到的内核机制的解释,比如捆绑导入或
磁盘交互。
但是我没有。我写书的目的是要对神秘的内核编程进行启蒙,讲解最基本的技术。对
于对内核编程有浓厚兴趣的人,我可以向他们推荐一个资源网站:
http:.//jungla.dit.upm.es/~jmseyas/linux/kernel/hackers-docs.html。而且,正如Linus所说,学
习内核的最好方法就是自己读源代码。
如果你希望更多的短内核模块的代码,我推荐Phrack杂志。即使你对安全性不感兴趣,
尽管作为一个程序员你应该考虑安全性,那里有许多很好的例子可以告诉你可以在内核里做
什么,这些例子都很短小,容易看懂。
我希望我为你成为更好的程序员提供了帮助,或者至少在技术中显示出了乐趣。如果
你写出了有用的内核模块,我希望你能在GPL下发表,那么我也可以使用。
其他
Goods and Services
I hope nobody minds the shameless promotions here. They are all things which are likely to be of
use to beginning Linux Kernel Module programmers.
Getting this Book in Print
The Coriolis group is going to print this book sometimes in the summer of \'99. If this is already
summer, and you want this book in print, you can go easy on your printer and buy it in a nice,
bound form.
GNU GENERAL PUBLIC LICENSE
Printed below is the GNU General Public License (the GPL or copyleft), under which this book is
licensed.
Version 2, June 1991
Copyright ?1989, 1991 Free Software Foundation, Inc. 675 Mass Ave, Cambridge, MA
02139, USA Everyone is permitted to copy and distribute verbatim copies of this license
document, but changing it is not allowed.
PREAMBLE
The licenses for most software are designed to take away your freedom to share and change
it. By contrast, the GNU General Public License is intended to guarantee your freedom to
share and change free software-to make sure the software is free for all its users. This
General Public License applies to most of the Free Software Foundation\'s software and to
any other program whose authors commit to using it. (Some other Free Software Foundation
software is covered by the GNU Library General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not price. Our General Public
Licenses are designed to make sure that you have the freedom to distribute copies of free
software (and charge for this service if you wish), that you receive source code or can get it if
you want it, that you can change the software or use pieces of it in new free programs; and
that you know you can do these things.
To protect your rights, we need to make restrictions that forbid anyone to deny you these
rights or to ask you to surrender the rights. These restrictions translate to certain
responsibilities for you if you distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether gratis or for a fee, you
must give the recipients all the rights that you have. You must make sure that they, too,
receive or can get the source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and (2) offer you this
license which gives you legal permission to copy, distribute and/or modify the software.
Also, for each author\'s protection and ours, we want to make certain that everyone
understands that there is no warranty for this free software. If the software is modified by
someone else and passed on, we want its recipients to know that what they have is not the
original, so that any problems introduced by others will not reflect on the original authors\'
reputations.
Finally, any free program is threatened constantly by software patents. We wish to avoid the
danger that redistributors of a free program will individually obtain patent licenses, in effect
making the program proprietary. To prevent this, we have made it clear that any patent
must be licensed for everyone\'s free use or not licensed at all.
The precise terms and conditions for copying, distribution and modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0.
This License applies to any program or other work which contains a notice placed by the
copyright holder saying it may be distributed under the terms of this General Public License. The
`Program\', below, refers to any such program or work, and a `work based on the Program\' means
either the Program or any derivative work under copyright law: that is to say, a work containing
the Program or a portion of it, either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in the term `modification\'.) Each
licensee is addressed as `you\'.
Activities other than copying, distribution and modification are not covered by this License; they
are outside its scope. The act of running the Program is not restricted, and the output from the
Program is covered only if its contents constitute a work based on the Program (independent of
having been made by running the Program). Whether that is true depends on what the Program
does.
1.
You may copy and distribute verbatim copies of the Program\'s source code as you receive it, in
any medium, provided that you conspicuously and appropriately publish on each copy an
appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to
this License and to the absence of any warranty; and give any other recipients of the Program a
copy of this License along with the Program.
You may charge a fee for the physical act of transferring a copy, and you may at your option offer
warranty protection in exchange for a fee.
2.
You may modify your copy or copies of the Program or any portion of it, thus forming a work
based on the Program, and copy and distribute such modifications or work under the terms of
Section 1 above, provided that you also meet all of these conditions:
a.
You must cause the modified files to carry prominent notices stating that you changed the files and
the date of any change.
b.
You must cause any work that you distribute or publish, that in whole or in part contains or is
derived from the Program or any part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c.
If the modified program normally reads commands interactively when run, you must cause it,
when started running for such interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a notice that there is no warranty (or
else, saying that you provide a warranty) and that users may redistribute the program under these
conditions, and telling the user how to view a copy of this License. (Exception: if the Program
itself is interactive but does not normally print such an announcement, your work based on the
Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If identifiable sections of that work are
not derived from the Program, and can be reasonably considered independent and separate works
in themselves, then this License, and its terms, do not apply to those sections when you distribute
them as separate works. But when you distribute the same sections as part of a whole which is a
work based on the Program, the distribution of the whole must be on the terms of this License,
whose permissions for other licensees extend to the entire whole, and thus to each and every part
regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest your rights to work written
entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program with the Program (or
with a work based on the Program) on a volume of a storage or distribution medium does not
bring the other work under the scope of this License.
3.
You may copy and distribute the Program (or a work based on it, under Section 2) in object code
or executable form under the terms of Sections 1 and 2 above provided that you also do one of the
following:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -