setconsole.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * setconsole.c - redirect system console output
  4. *
  5. * Copyright (C) 2004,2005 Enrik Berkhan <Enrik.Berkhan@inka.de>
  6. * Copyright (C) 2008 Bernhard Reutner-Fischer
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  9. */
  10. //config:config SETCONSOLE
  11. //config: bool "setconsole (3.6 kb)"
  12. //config: default y
  13. //config: help
  14. //config: Redirect writes to /dev/console to another device,
  15. //config: like the current tty while logged in via telnet.
  16. //config: This does not redirect kernel log, only writes
  17. //config: from user space.
  18. //config:
  19. //config:config FEATURE_SETCONSOLE_LONG_OPTIONS
  20. //config: bool "Enable long options"
  21. //config: default y
  22. //config: depends on SETCONSOLE && LONG_OPTS
  23. //applet:IF_SETCONSOLE(APPLET_NOEXEC(setconsole, setconsole, BB_DIR_SBIN, BB_SUID_DROP, setconsole))
  24. //kbuild:lib-$(CONFIG_SETCONSOLE) += setconsole.o
  25. //usage:#define setconsole_trivial_usage
  26. //usage: "[-r] [DEVICE]"
  27. //usage:#define setconsole_full_usage "\n\n"
  28. //usage: "Make writes to /dev/console appear on DEVICE (default: /dev/tty)."
  29. //usage: "\n""Does not redirect kernel log output or reads from /dev/console."
  30. //usage: "\n"
  31. //usage: "\n"" -r Reset: writes to /dev/console go to kernel log tty(s)"
  32. /* It was a bbox-specific invention, but SUSE does have a similar utility.
  33. * SUSE has no -r option, though.
  34. */
  35. #include "libbb.h"
  36. int setconsole_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  37. int setconsole_main(int argc UNUSED_PARAM, char **argv)
  38. {
  39. const char *device = CURRENT_TTY;
  40. int reset;
  41. /* at most one non-option argument */
  42. reset = getopt32(argv, "^" "r" "\0" "?1");
  43. argv += 1 + reset;
  44. if (*argv) {
  45. device = *argv;
  46. } else {
  47. if (reset)
  48. device = DEV_CONSOLE;
  49. }
  50. //TODO: fails if TIOCCONS redir is already active to some tty.
  51. //I think SUSE version first does TIOCCONS on /dev/console fd (iow: resets)
  52. //then TIOCCONS to new tty?
  53. xioctl(xopen(device, O_WRONLY), TIOCCONS, NULL);
  54. return EXIT_SUCCESS;
  55. }