10.1. Replacing printk

本文介绍了一种在Linux内核模块中实现打印输出的方法,通过获取当前任务的tty结构并利用其写函数将字符串输出到相应的终端。这种方式无论终端是通过X11、telnet还是其他方式连接都能适用。
n Section 1.2.1.2, I said that X and kernel module programming don't mix. That's true for developing kernel modules, but in actual use, you want to be able to send messages to whichever tty [1] the command to load the module came from.

The way this is done is by using current, a pointer to the currently running task, to get the current task's tty structure. Then, we look inside that tty structure to find a pointer to a string write function, which we use to write a string to the tty.

Example 10-1. print_string.c

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

Notes

[1]

Teletype, originally a combination keyboard-printer used to communicate with a Unix system, and today an abstraction for the text stream used for a Unix program, whether it's a physical terminal, an xterm on an X display, a network connection used with telnet, etc.

 
(base) unitree@ubuntu:~$ sudo apt-get install ffmpeg Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: apt-clone archdetect-deb bogl-bterm busybox-static cryptsetup-bin dctrl-tools dpkg-repack gir1.2-timezonemap-1.0 gir1.2-xkl-1.0 grub-common libavresample-dev libavresample4 libdc1394-22-dev libdebian-installer4 libexif-dev libgdcm-dev libgphoto2-dev libgtsam4 libilmbase-dev libmetis-dev libmetis5 libopencv4.2-java libopencv4.2-jni libopenexr-dev libraw1394-dev libtimezonemap-data libtimezonemap1 os-prober python3-icu python3-pam rdate tasksel tasksel-data Use 'sudo apt autoremove' to remove them. The following NEW packages will be installed: ffmpeg 0 upgraded, 1 newly installed, 0 to remove and 156 not upgraded. 1 not fully installed or removed. Need to get 0 B/14.3 MB of archives. After this operation, 52.4 MB of additional disk space will be used. debconf: delaying package configuration, since apt-utils is not installed (Reading database ... 264161 files and directories currently installed.) Preparing to unpack .../ffmpeg_7%3a4.2.7-nvidia_arm64.deb ... Unpacking ffmpeg (7:4.2.7-nvidia) ... Replacing files in old package libavcodec-dev:arm64 (7:4.2.7-0ubuntu0.1) ... Replacing files in old package libavdevice-dev:arm64 (7:4.2.7-0ubuntu0.1) ... Replacing files in old package libavfilter-dev:arm64 (7:4.2.7-0ubuntu0.1) ... Replacing files in old package libavformat-dev:arm64 (7:4.2.7-0ubuntu0.1) ... Replacing files in old package libavresample-dev:arm64 (7:4.2.7-0ubuntu0.1) ... Replacing files in old package libavutil-dev:arm64 (7:4.2.7-0ubuntu0.1) ... dpkg: error processing archive /var/cache/apt/archives/ffmpeg_7%3a4.2.7-nvidia_arm64.deb (--unpack): trying to overwrite '/usr/include/aarch64-linux-gnu/libpostproc/postprocess.h', which is also in package libpostproc-dev:arm64 7:4.2.7-0ubuntu0.1 怎么解决
04-01
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值