Linux Kernel programming (replace printk) 5-Linux general technology-Linux programming and kernel information. The following is a detailed description. 9. Replace printk's
At the beginning (Chapter 1), we said that X is not mixed with kernel module programming. This is correct when developing the kernel, but in actual applications we want to send messages to any tty sent by the module command (note 9.1 ). This is important when the kernel module is released because it will be used in all kernels.
This method uses the current concept, a pointer to the current running task, to obtain the tty structure of the current task. Then, we look for a pointer to the write string function in the tty structure. We use this function to write a string into tty.
Ex printk. c
/* Printk. c-send textual output to the tty youre
* Running on, regardless of whether its passed
* Through X11, telnet, etc .*/
/* Copy right (C) 1998 by Ori Pomerantz */
/* The necessary header files */
/* Standard in kernel modules */
# Include/* Were doing kernel work */
# Include/* Specifically, a module */
/* Deal with CONFIG_MODVERSIONS */
# If CONFIG_MODVERSIONS = 1
# Define MODVERSIONS
# Include
# Endif
/* Necessary here */
# Include/* For current */
# Include/* For the tty declarations */
/* Print the string to the appropriate tty, the one
* The current task uses */
Void print_string (char * str)
{
Struct tty_struct * my_tty;
/* The tty for the current task */
My_tty = current-> tty;
/* If my_tty is NULL, it means that the current task
* Has no tty you can print to (this is possible,
* Example, if its a daemon). In this case, theres
* Nothing we can do .*/
If (my_tty! = NULL ){
/* My_tty-> driver is a struct which holds the ttys
* Functions, one of which (write) is used
* Write strings to the tty. It can be used to take
* A string either from the users memory segment
* Or the kernels memory segment.
*
* The functions first parameter is the tty
* Write to, because the same function wocould
* Normally be used for all ttys of a certain type.
* The second parameter controls whether
* Function functions es a string from kernel memory
* (False, 0) or from user memory (true, non zero ).
* The third parameter is a pointer to a string,
* And the fourth parameter is the length
* The string.
*/
(* (My_tty-> driver). write )(
My_tty,/* The tty itself */
0,/* We dont take the string from user space */
Str,/* String */
Strlen (str);/* Length */
/* Ttys were originally hardware devices, which
* (Usually) adhered strictly to the ASCII standard.
* According to ASCII, to move to a new line you
* Need two characters, a carriage return and
* Line feed. In Unix, on the other hand,
* ASCII line feed is used for both purposes-so
* We cant just use, because it wouldnt have
* A carriage return and the next line will
* Start at the column right
* After the line feed.
*
* BTW, this is the reason why the text file
* Is different between Unix and Windows.
* In CP/M and its derivatives, such as MS-DOS and
* Windows, the ASCII standard was strictly
* Adhered to, and therefore a new line requires
* Both a line feed and a carriage return.
*/
(* (My_tty-> driver). write )(
My_tty,
0,
"" 1512 "",
2 );
}
}
/* Module initialization and cleanup *******************/
/* Initialize the module-register the proc file */
Int init_module ()
{
Print_string ("" Module Inserted "");
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.