print_string.c 2.8 KB

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