nhmk/examples/print_string.c

84 lines
3.0 KiB
C
Raw Normal View History

2021-07-22 06:35:24 +08:00
/*
* 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/init.h>
2021-07-22 06:35:24 +08:00
#include <linux/kernel.h>
#include <linux/module.h>
2021-07-22 07:33:27 +08:00
#include <linux/sched.h> /* For current */
#include <linux/tty.h> /* For the tty declarations */
2021-07-22 06:35:24 +08:00
MODULE_LICENSE("GPL");
static void print_string(char *str)
{
struct tty_struct *my_tty;
const struct tty_operations *ttyops;
/*
* The tty for the current task, for 2.6.6+ kernels
*/
my_tty = get_current_tty();
ttyops = my_tty->driver->ops;
/*
* 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
2021-07-22 07:33:27 +08:00
* tty's of a certain type.
* The 2nd parameter is a pointer to a string.
* The 3rd parameter is the length of the string.
2021-07-22 06:35:24 +08:00
*
* 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
*/
2021-07-22 07:33:27 +08:00
(ttyops->write)(my_tty, /* The tty itself */
str, /* String */
strlen(str)); /* Length */
2021-07-22 06:35:24 +08:00
/*
* 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.
*/
(ttyops->write)(my_tty, "\015\012", 2);
2021-07-22 06:35:24 +08:00
}
}
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!");
}
module_init(print_string_init);
module_exit(print_string_exit);