2008 May 19th Monday (五月 十九日 月曜日)

本文介绍了一个简单的Linux内核模块,该模块能够在加载和卸载时向终端打印消息。通过直接操作tty_struct来实现这一功能,展示了不同Linux内核版本中tty结构的变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

To print message to a tty.   #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/tty.h> #include <linux/version.h> static void print_string(char *str) {     struct tty_struct *my_tty;     /*     * tty struct went into signal struct in 2.6.6     */ #if(LINUX_VERSION_CODE<=KERNEL_VERSION(2,6,5))     /*     * The tty for the current task     */     my_tty=current->tty; #else     /*     * The tty for the current task, for 2.6.6+ kernels     */     my_tty=current->signal->tty; #endif     /*     * If my_tty is NULL, the current task has no tty you can print to     * (ie, if it's a daemon). Ifso,there'snothingwecando.     */         if (my_tty != NULL){         /*         * my_tty->driveris 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 or         * 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, /*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. InASCII,tomoveto         * 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.         *         * This is why text files are different between Unix and         * MS Windows. In CP/Mand 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);         } } static int __init print_string_init(void) {     print_string("The module has been inserted. Hello world!");     return 0; } static void __exit print_string_exit(void) {     print_string("The module has been removed. Fare well world!"); } module_init(print_string_init); module_exit(print_string_exit);   The great earth in Shichuang province almost caught the eyes of all country people. Donation from all leveles, from oversea, from other countries, is not stoped.   It is said that about three million people died from this great earth.  Many teacher and students died suddenly in this earth.  The great earth is not any forecast before and is too sudden to people lost their home, lost their family, lost their relatives.   Luckly, the hope is always kept in their mind.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值