📄 x1111.html
字号:
<HTML
><HEAD
><TITLE
>Replacing printk</TITLE
><META
NAME="GENERATOR"
CONTENT="Microsoft FrontPage 4.0"><LINK
REL="HOME"
TITLE="The Linux Kernel Module Programming Guide"
HREF="index.html"><LINK
REL="UP"
TITLE="Replacing Printks"
HREF="c1109.html"><LINK
REL="PREVIOUS"
TITLE="Replacing Printks"
HREF="c1109.html"><LINK
REL="NEXT"
TITLE="Scheduling Tasks"
HREF="c1143.html"></HEAD
><BODY
CLASS="SECT1"
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"
>Linux内核驱动模块编程指南 (内核版本2.2, 2.4)</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="c1109.html"
ACCESSKEY="P"
>返回</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>第十章. 替换 Printk</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="c1143.html"
ACCESSKEY="N"
>继续</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="AEN1111"
></A
>10.1. 替换 <TT
CLASS="FUNCTION"
>printk</TT
></H1
><P
>在 <A
HREF="x45.html#USINGX"
>节</A
><A
HREF="x45.html#USINGX"
> 1.2.1.2</A
> 中,
我说过最好不要在X中进行内核模块编程。在真正的内核模块开发中的确是这样。但在实际应用中,你想在任何加载模块的tty<A
NAME="AEN1119"
HREF="#FTN.AEN1119"
><SPAN
CLASS="footnote"
>[1]</SPAN
></A
>终端中显示信息。</P
><P
>实现的方法是使用 <TT
CLASS="VARNAME"
>current</TT
>
指针,一个指向当前运行进程的指针,来获取当前任务的tty终端的结构体。然后,我们找到在该tty结构体中用来向tty写入字符信息的函数的指针。通过指针我们使用该函数来向终端写入信息。</P
><DIV
CLASS="EXAMPLE"
><A
NAME="AEN1140"
></A
><P
><B
>Example 10-1. print_string.c</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="PROGRAMLISTING"
>/* print_string.c - Send output to the tty you're running on, regardless of whether it's
* through X11, telnet, etc. We do this by printing the string to the tty associated
* with the current task.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sched.h> // For current
#include <linux/tty.h> // For the tty declarations
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Peter Jay Salzman");
void print_string(char *str)
{
struct tty_struct *my_tty;
my_tty = current->tty; // The tty for the current task
/* If my_tty is NULL, the current task has no tty you can print to (this is possible,
* for example, if it's a daemon). If so, there's nothing we can do.
*/
if (my_tty != NULL) {
/* my_tty->driver is a struct which holds the tty's functions, one of which (write)
* is used to write strings to the tty. It can be used to take a string either
* from the user's memory segment or the kernel's memory segment.
*
* The function's 1st parameter is the tty to write to, because the same function
* would normally be used for all tty's of a certain type. The 2nd parameter
* controls whether the function receives a string from kernel memory (false, 0) or
* from user memory (true, non zero). The 3rd parameter is a pointer to a string.
* The 4th parameter is the length of the string.
*/
(*(my_tty->driver).write)(
my_tty, // The tty itself
0, // We don't take the string from user space
str, // String
strlen(str)); // Length
/* ttys were originally hardware devices, which (usually) strictly followed the
* ASCII standard. In ASCII, to move to a new line you need two characters, a
* carriage return and a line feed. On Unix, the ASCII line feed is used for both
* purposes - so we can't just use \n, because it wouldn't have a carriage return
* and the next line will start at the column right after the line feed.
*
* BTW, this is why text files are different between Unix and MS Windows. In CP/M
* and its derivatives, like MS-DOS and MS Windows, the ASCII standard was strictly
* adhered to, and therefore a newline requirs both a LF and a CR.
*/
(*(my_tty->driver).write)(my_tty, 0, "\015\012", 2);
}
}
int print_string_init(void)
{
print_string("The module has been inserted. Hello world!");
return 0;
}
void print_string_exit(void)
{
print_string("The module has been removed. Farewell world!");
}
module_init(print_string_init);
module_exit(print_string_exit);</PRE
></FONT
></TD
></TR
></TABLE
></DIV
></DIV
><H3
CLASS="FOOTNOTES"
>注意</H3
><TABLE
BORDER="0"
CLASS="FOOTNOTES"
WIDTH="100%"
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN1119"
HREF="x1111.html#AEN1119"
><SPAN
CLASS="footnote"
>[1]</SPAN
></A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
><EM
>T</EM
>ele<EM
>ty</EM
>pe(tty),
原先是一种用来和Unix系统交互的键盘和打印机结合起来的装置。现在,它只是一个用来同Unix或类似的系统交流文字流的抽象的设备,而不管它具体是显示器,X中的xterm,还是一个通过telnet的网络连接。</P
></TD
></TR
></TABLE
><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="c1109.html"
ACCESSKEY="P"
>返回</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>返回首页</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="c1143.html"
ACCESSKEY="N"
>继续</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>替换Printk</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="c1109.html"
ACCESSKEY="U"
>返回本章开始</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>任务调度</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -