1
0

print_string.c 3.8 KB

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