blob: 7f0f9c711cc0a10b1e4fb085959f2db55c817005 [file] [log] [blame]
Paul Foxfc2256a2005-08-01 18:12:30 +00001/* vi: set sw=4 ts=4: */
2/*
Denys Vlasenkoebe6d9d2017-10-05 14:40:24 +02003 * setconsole.c - redirect system console output
Paul Foxfc2256a2005-08-01 18:12:30 +00004 *
Denys Vlasenkoebe6d9d2017-10-05 14:40:24 +02005 * Copyright (C) 2004,2005 Enrik Berkhan <Enrik.Berkhan@inka.de>
6 * Copyright (C) 2008 Bernhard Reutner-Fischer
Paul Foxfc2256a2005-08-01 18:12:30 +00007 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Paul Foxfc2256a2005-08-01 18:12:30 +00009 */
Denys Vlasenko6d932992016-11-23 10:39:27 +010010//config:config SETCONSOLE
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020011//config: bool "setconsole (3.7 kb)"
Denys Vlasenko6d932992016-11-23 10:39:27 +010012//config: default y
13//config: select PLATFORM_LINUX
14//config: help
Denys Vlasenko316d38e2017-08-07 17:36:41 +020015//config: Redirect writes to /dev/console to another device,
Denys Vlasenko72089cf2017-07-21 09:50:55 +020016//config: like the current tty while logged in via telnet.
Denys Vlasenko316d38e2017-08-07 17:36:41 +020017//config: This does not redirect kernel log, only writes
18//config: from user space.
Denys Vlasenko6d932992016-11-23 10:39:27 +010019//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
Denys Vlasenko6d932992016-11-23 10:39:27 +010024
Denys Vlasenko5cb907f2017-08-06 18:56:25 +020025//applet:IF_SETCONSOLE(APPLET_NOEXEC(setconsole, setconsole, BB_DIR_SBIN, BB_SUID_DROP, setconsole))
Denys Vlasenko6d932992016-11-23 10:39:27 +010026
27//kbuild:lib-$(CONFIG_SETCONSOLE) += setconsole.o
Paul Foxfc2256a2005-08-01 18:12:30 +000028
Pere Orga55068c42011-03-27 23:42:28 +020029//usage:#define setconsole_trivial_usage
Denys Vlasenkoab233812017-08-06 18:54:16 +020030//usage: "[-r] [DEVICE]"
Pere Orga55068c42011-03-27 23:42:28 +020031//usage:#define setconsole_full_usage "\n\n"
Denys Vlasenko316d38e2017-08-07 17:36:41 +020032//usage: "Make writes to /dev/console appear on DEVICE (default: /dev/tty)."
33//usage: "\n""Does not redirect kernel log output or reads from /dev/console."
34//usage: "\n"
35//usage: "\n"" -r Reset: writes to /dev/console go to kernel log tty(s)"
Pere Orga55068c42011-03-27 23:42:28 +020036
Denys Vlasenkoab233812017-08-06 18:54:16 +020037/* It was a bbox-specific invention, but SUSE does have a similar utility.
38 * SUSE has no -r option, though.
39 */
40
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000041#include "libbb.h"
Paul Foxfc2256a2005-08-01 18:12:30 +000042
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000043int setconsole_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000044int setconsole_main(int argc UNUSED_PARAM, char **argv)
Paul Foxfc2256a2005-08-01 18:12:30 +000045{
Rob Landley9f0e00f2005-09-08 03:27:06 +000046 const char *device = CURRENT_TTY;
Denys Vlasenko316d38e2017-08-07 17:36:41 +020047 int reset;
Paul Foxfc2256a2005-08-01 18:12:30 +000048
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000049 /* at most one non-option argument */
Denys Vlasenko22542ec2017-08-08 21:55:02 +020050 reset = getopt32(argv, "^" "r" "\0" "?1");
Paul Foxfc2256a2005-08-01 18:12:30 +000051
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000052 argv += 1 + reset;
53 if (*argv) {
54 device = *argv;
Paul Foxfc2256a2005-08-01 18:12:30 +000055 } else {
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000056 if (reset)
Denis Vlasenkoec27feb2007-02-17 15:52:02 +000057 device = DEV_CONSOLE;
Paul Foxfc2256a2005-08-01 18:12:30 +000058 }
59
Denys Vlasenko316d38e2017-08-07 17:36:41 +020060//TODO: fails if TIOCCONS redir is already active to some tty.
61//I think SUSE version first does TIOCCONS on /dev/console fd (iow: resets)
62//then TIOCCONS to new tty?
Peter Korsgaard8dc61952011-05-26 17:51:37 +020063 xioctl(xopen(device, O_WRONLY), TIOCCONS, NULL);
Paul Foxfc2256a2005-08-01 18:12:30 +000064 return EXIT_SUCCESS;
65}