print_string.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #include <linux/version.h> /* For LINUX_VERSION_CODE */
  12. MODULE_LICENSE("GPL");
  13. static void print_string(char *str)
  14. {
  15. struct tty_struct *my_tty;
  16. const struct tty_operations *ttyops;
  17. /*
  18. * tty struct went into signal struct in 2.6.6
  19. */
  20. #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 5))
  21. /*
  22. * The tty for the current task
  23. */
  24. my_tty = current->tty;
  25. #else
  26. /*
  27. * The tty for the current task, for 2.6.6+ kernels
  28. */
  29. my_tty = get_current_tty();
  30. #endif
  31. ttyops = my_tty->driver->ops;
  32. /*
  33. * If my_tty is NULL, the current task has no tty you can print to
  34. * (ie, if it's a daemon). If so, there's nothing we can do.
  35. */
  36. if (my_tty != NULL) {
  37. /*
  38. * my_tty->driver is a struct which holds the tty's functions,
  39. * one of which (write) is used to write strings to the tty.
  40. * It can be used to take a string either from the user's or
  41. * kernel's memory segment.
  42. *
  43. * The function's 1st parameter is the tty to write to,
  44. * because the same function would normally be used for all
  45. * tty's of a certain type. The 2nd parameter controls
  46. * whether the function receives a string from kernel
  47. * memory (false, 0) or from user memory (true, non zero).
  48. * BTW: this param has been removed in Kernels > 2.6.9
  49. * The (2nd) 3rd parameter is a pointer to a string.
  50. * The (3rd) 4th parameter is the length of the string.
  51. *
  52. * As you will see below, sometimes it's necessary to use
  53. * preprocessor stuff to create code that works for different
  54. * kernel versions. The (naive) approach we've taken here
  55. * does not scale well. The right way to deal with this
  56. * is described in section 2 of
  57. * linux/Documentation/SubmittingPatches
  58. */
  59. (ttyops->write)(my_tty, /* The tty itself */
  60. #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 9))
  61. 0, /* Don't take the string
  62. from user space */
  63. #endif
  64. str, /* String */
  65. strlen(str)); /* Length */
  66. /*
  67. * ttys were originally hardware devices, which (usually)
  68. * strictly followed the ASCII standard. In ASCII, to move to
  69. * a new line you need two characters, a carriage return and a
  70. * line feed. On Unix, the ASCII line feed is used for both
  71. * purposes - so we can't just use \n, because it wouldn't have
  72. * a carriage return and the next line will start at the
  73. * column right after the line feed.
  74. *
  75. * This is why text files are different between Unix and
  76. * MS Windows. In CP/M and derivatives, like MS-DOS and
  77. * MS Windows, the ASCII standard was strictly adhered to,
  78. * and therefore a newline requirs both a LF and a CR.
  79. */
  80. #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 9))
  81. (ttyops->write)(my_tty, 0, "\015\012", 2);
  82. #else
  83. (ttyops->write)(my_tty, "\015\012", 2);
  84. #endif
  85. }
  86. }
  87. static int __init print_string_init(void)
  88. {
  89. print_string("The module has been inserted. Hello world!");
  90. return 0;
  91. }
  92. static void __exit print_string_exit(void)
  93. {
  94. print_string("The module has been removed. Farewell world!");
  95. }
  96. module_init(print_string_init);
  97. module_exit(print_string_exit);