3
0

setconsole.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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: select PLATFORM_LINUX
  14. //config: help
  15. //config: Redirect writes to /dev/console to another device,
  16. //config: like the current tty while logged in via telnet.
  17. //config: This does not redirect kernel log, only writes
  18. //config: from user space.
  19. //config:
  20. //config:config FEATURE_SETCONSOLE_LONG_OPTIONS
  21. //config: bool "Enable long options"
  22. //config: default y
  23. //config: depends on SETCONSOLE && LONG_OPTS
  24. //applet:IF_SETCONSOLE(APPLET_NOEXEC(setconsole, setconsole, BB_DIR_SBIN, BB_SUID_DROP, setconsole))
  25. //kbuild:lib-$(CONFIG_SETCONSOLE) += setconsole.o
  26. //usage:#define setconsole_trivial_usage
  27. //usage: "[-r] [DEVICE]"
  28. //usage:#define setconsole_full_usage "\n\n"
  29. //usage: "Make writes to /dev/console appear on DEVICE (default: /dev/tty)."
  30. //usage: "\n""Does not redirect kernel log output or reads from /dev/console."
  31. //usage: "\n"
  32. //usage: "\n"" -r Reset: writes to /dev/console go to kernel log tty(s)"
  33. /* It was a bbox-specific invention, but SUSE does have a similar utility.
  34. * SUSE has no -r option, though.
  35. */
  36. #include "libbb.h"
  37. int setconsole_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  38. int setconsole_main(int argc UNUSED_PARAM, char **argv)
  39. {
  40. const char *device = CURRENT_TTY;
  41. int reset;
  42. /* at most one non-option argument */
  43. reset = getopt32(argv, "^" "r" "\0" "?1");
  44. argv += 1 + reset;
  45. if (*argv) {
  46. device = *argv;
  47. } else {
  48. if (reset)
  49. device = DEV_CONSOLE;
  50. }
  51. //TODO: fails if TIOCCONS redir is already active to some tty.
  52. //I think SUSE version first does TIOCCONS on /dev/console fd (iow: resets)
  53. //then TIOCCONS to new tty?
  54. xioctl(xopen(device, O_WRONLY), TIOCCONS, NULL);
  55. return EXIT_SUCCESS;
  56. }