print_string.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * print_string.c - Send output to the tty we're running on, regardless if it's
  3. * through X11, telnet, etc. We do this by printing the string to the tty
  4. * associated with the current task.
  5. */
  6. #include <linux/init.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/sched.h> /* For current */
  10. #include <linux/tty.h> /* For the tty declarations */
  11. MODULE_LICENSE("GPL");
  12. static void print_string(char *str)
  13. {
  14. struct tty_struct *my_tty;
  15. const struct tty_operations *ttyops;
  16. /*
  17. * The tty for the current task, for 2.6.6+ kernels
  18. */
  19. my_tty = get_current_tty();
  20. ttyops = my_tty->driver->ops;
  21. /*
  22. * If my_tty is NULL, the current task has no tty you can print to
  23. * (ie, if it's a daemon). If so, there's nothing we can do.
  24. */
  25. if (my_tty != NULL) {
  26. /*
  27. * my_tty->driver is a struct which holds the tty's functions,
  28. * one of which (write) is used to write strings to the tty.
  29. * It can be used to take a string either from the user's or
  30. * kernel's memory segment.
  31. *
  32. * The function's 1st parameter is the tty to write to,
  33. * because the same function would normally be used for all
  34. * tty's of a certain type.
  35. * The 2nd parameter is a pointer to a string.
  36. * The 3rd parameter is the length of the string.
  37. *
  38. * As you will see below, sometimes it's necessary to use
  39. * preprocessor stuff to create code that works for different
  40. * kernel versions. The (naive) approach we've taken here
  41. * does not scale well. The right way to deal with this
  42. * is described in section 2 of
  43. * linux/Documentation/SubmittingPatches
  44. */
  45. (ttyops->write)(my_tty, /* The tty itself */
  46. str, /* String */
  47. strlen(str)); /* Length */
  48. /*
  49. * ttys were originally hardware devices, which (usually)
  50. * strictly followed the ASCII standard. In ASCII, to move to
  51. * a new line you need two characters, a carriage return and a
  52. * line feed. On Unix, the ASCII line feed is used for both
  53. * purposes - so we can't just use \n, because it wouldn't have
  54. * a carriage return and the next line will start at the
  55. * column right after the line feed.
  56. *
  57. * This is why text files are different between Unix and
  58. * MS Windows. In CP/M and derivatives, like MS-DOS and
  59. * MS Windows, the ASCII standard was strictly adhered to,
  60. * and therefore a newline requirs both a LF and a CR.
  61. */
  62. (ttyops->write)(my_tty, "\015\012", 2);
  63. }
  64. }
  65. static int __init print_string_init(void)
  66. {
  67. print_string("The module has been inserted. Hello world!");
  68. return 0;
  69. }
  70. static void __exit print_string_exit(void)
  71. {
  72. print_string("The module has been removed. Farewell world!");
  73. }
  74. module_init(print_string_init);
  75. module_exit(print_string_exit);