Paul Fox | fc2256a | 2005-08-01 18:12:30 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Denys Vlasenko | ebe6d9d | 2017-10-05 14:40:24 +0200 | [diff] [blame] | 3 | * setconsole.c - redirect system console output |
Paul Fox | fc2256a | 2005-08-01 18:12:30 +0000 | [diff] [blame] | 4 | * |
Denys Vlasenko | ebe6d9d | 2017-10-05 14:40:24 +0200 | [diff] [blame] | 5 | * Copyright (C) 2004,2005 Enrik Berkhan <Enrik.Berkhan@inka.de> |
| 6 | * Copyright (C) 2008 Bernhard Reutner-Fischer |
Paul Fox | fc2256a | 2005-08-01 18:12:30 +0000 | [diff] [blame] | 7 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Paul Fox | fc2256a | 2005-08-01 18:12:30 +0000 | [diff] [blame] | 9 | */ |
Denys Vlasenko | 6d93299 | 2016-11-23 10:39:27 +0100 | [diff] [blame] | 10 | //config:config SETCONSOLE |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 11 | //config: bool "setconsole (3.6 kb)" |
Denys Vlasenko | 6d93299 | 2016-11-23 10:39:27 +0100 | [diff] [blame] | 12 | //config: default y |
Denys Vlasenko | 6d93299 | 2016-11-23 10:39:27 +0100 | [diff] [blame] | 13 | //config: help |
Denys Vlasenko | 316d38e | 2017-08-07 17:36:41 +0200 | [diff] [blame] | 14 | //config: Redirect writes to /dev/console to another device, |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 15 | //config: like the current tty while logged in via telnet. |
Denys Vlasenko | 316d38e | 2017-08-07 17:36:41 +0200 | [diff] [blame] | 16 | //config: This does not redirect kernel log, only writes |
| 17 | //config: from user space. |
Denys Vlasenko | 6d93299 | 2016-11-23 10:39:27 +0100 | [diff] [blame] | 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 |
Denys Vlasenko | 6d93299 | 2016-11-23 10:39:27 +0100 | [diff] [blame] | 23 | |
Denys Vlasenko | 5cb907f | 2017-08-06 18:56:25 +0200 | [diff] [blame] | 24 | //applet:IF_SETCONSOLE(APPLET_NOEXEC(setconsole, setconsole, BB_DIR_SBIN, BB_SUID_DROP, setconsole)) |
Denys Vlasenko | 6d93299 | 2016-11-23 10:39:27 +0100 | [diff] [blame] | 25 | |
| 26 | //kbuild:lib-$(CONFIG_SETCONSOLE) += setconsole.o |
Paul Fox | fc2256a | 2005-08-01 18:12:30 +0000 | [diff] [blame] | 27 | |
Pere Orga | 55068c4 | 2011-03-27 23:42:28 +0200 | [diff] [blame] | 28 | //usage:#define setconsole_trivial_usage |
Denys Vlasenko | ab23381 | 2017-08-06 18:54:16 +0200 | [diff] [blame] | 29 | //usage: "[-r] [DEVICE]" |
Pere Orga | 55068c4 | 2011-03-27 23:42:28 +0200 | [diff] [blame] | 30 | //usage:#define setconsole_full_usage "\n\n" |
Denys Vlasenko | 316d38e | 2017-08-07 17:36:41 +0200 | [diff] [blame] | 31 | //usage: "Make writes to /dev/console appear on DEVICE (default: /dev/tty)." |
| 32 | //usage: "\n""Does not redirect kernel log output or reads from /dev/console." |
| 33 | //usage: "\n" |
| 34 | //usage: "\n"" -r Reset: writes to /dev/console go to kernel log tty(s)" |
Pere Orga | 55068c4 | 2011-03-27 23:42:28 +0200 | [diff] [blame] | 35 | |
Denys Vlasenko | ab23381 | 2017-08-06 18:54:16 +0200 | [diff] [blame] | 36 | /* It was a bbox-specific invention, but SUSE does have a similar utility. |
| 37 | * SUSE has no -r option, though. |
| 38 | */ |
| 39 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 40 | #include "libbb.h" |
Paul Fox | fc2256a | 2005-08-01 18:12:30 +0000 | [diff] [blame] | 41 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 42 | int setconsole_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 43 | int setconsole_main(int argc UNUSED_PARAM, char **argv) |
Paul Fox | fc2256a | 2005-08-01 18:12:30 +0000 | [diff] [blame] | 44 | { |
Rob Landley | 9f0e00f | 2005-09-08 03:27:06 +0000 | [diff] [blame] | 45 | const char *device = CURRENT_TTY; |
Denys Vlasenko | 316d38e | 2017-08-07 17:36:41 +0200 | [diff] [blame] | 46 | int reset; |
Paul Fox | fc2256a | 2005-08-01 18:12:30 +0000 | [diff] [blame] | 47 | |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 48 | /* at most one non-option argument */ |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 49 | reset = getopt32(argv, "^" "r" "\0" "?1"); |
Paul Fox | fc2256a | 2005-08-01 18:12:30 +0000 | [diff] [blame] | 50 | |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 51 | argv += 1 + reset; |
| 52 | if (*argv) { |
| 53 | device = *argv; |
Paul Fox | fc2256a | 2005-08-01 18:12:30 +0000 | [diff] [blame] | 54 | } else { |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 55 | if (reset) |
Denis Vlasenko | ec27feb | 2007-02-17 15:52:02 +0000 | [diff] [blame] | 56 | device = DEV_CONSOLE; |
Paul Fox | fc2256a | 2005-08-01 18:12:30 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Denys Vlasenko | 316d38e | 2017-08-07 17:36:41 +0200 | [diff] [blame] | 59 | //TODO: fails if TIOCCONS redir is already active to some tty. |
| 60 | //I think SUSE version first does TIOCCONS on /dev/console fd (iow: resets) |
| 61 | //then TIOCCONS to new tty? |
Peter Korsgaard | 8dc6195 | 2011-05-26 17:51:37 +0200 | [diff] [blame] | 62 | xioctl(xopen(device, O_WRONLY), TIOCCONS, NULL); |
Paul Fox | fc2256a | 2005-08-01 18:12:30 +0000 | [diff] [blame] | 63 | return EXIT_SUCCESS; |
| 64 | } |