Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




10.1. Replacing printk

In Section, 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 we're running on, regardless if 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/init.h>
#include <linux/sched.h>	/* For current */
#include <linux/tty.h>		/* For the tty declarations */
#include <linux/version.h>	/* For LINUX_VERSION_CODE */

MODULE_AUTHOR("Peter Jay Salzman");

static void print_string(char *str)
	struct tty_struct *my_tty;

	 * tty struct went into signal struct in 2.6.6 
	 * The tty for the current task 
	my_tty = current->tty;
	 * The tty for the current task, for 2.6.6+ kernels 
	my_tty = current->signal->tty;

	 * If my_tty is NULL, the current task has no tty you can print to 
	 * (ie, 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 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). 
		 * BTW: this param has been removed in Kernels > 2.6.9
		 * The (2nd) 3rd parameter is a pointer to a string.
		 * The (3rd) 4th parameter is the length of the string.
		 * As you will see below, sometimes it's necessary to use
		 * preprocessor stuff to create code that works for different
		 * kernel versions. The (naive) approach we've taken here 
		 * does not scale well. The right way to deal with this 
		 * is described in section 2 of 
		 * linux/Documentation/SubmittingPatches
		((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.  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.
		 * This is why text files are different between Unix and 
		 * MS Windows.  In CP/M and 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);
		((my_tty->driver)->write) (my_tty, "\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.  Farewell world!");




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.

  Published under the terms of the GNU General Public License Design by Interspire