1
0

print_string.c 2.8 KB

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