blob: 461a98c6addfd4f5450aeaca0944ba6a9930f893 [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 Vlasenkob097a842018-12-28 03:20:17 +010011//config: bool "setconsole (3.6 kb)"
Denys Vlasenko6d932992016-11-23 10:39:27 +010012//config: default y
Denys Vlasenko6d932992016-11-23 10:39:27 +010013//config: help
Denys Vlasenko316d38e2017-08-07 17:36:41 +020014//config: Redirect writes to /dev/console to another device,
Denys Vlasenko72089cf2017-07-21 09:50:55 +020015//config: like the current tty while logged in via telnet.
Denys Vlasenko316d38e2017-08-07 17:36:41 +020016//config: This does not redirect kernel log, only writes
17//config: from user space.
Denys Vlasenko6d932992016-11-23 10:39:27 +010018//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 Vlasenko6d932992016-11-23 10:39:27 +010023
Denys Vlasenko5cb907f2017-08-06 18:56:25 +020024//applet:IF_SETCONSOLE(APPLET_NOEXEC(setconsole, setconsole, BB_DIR_SBIN, BB_SUID_DROP, setconsole))
Denys Vlasenko6d932992016-11-23 10:39:27 +010025
26//kbuild:lib-$(CONFIG_SETCONSOLE) += setconsole.o
Paul Foxfc2256a2005-08-01 18:12:30 +000027
Pere Orga55068c42011-03-27 23:42:28 +020028//usage:#define setconsole_trivial_usage
Denys Vlasenkoab233812017-08-06 18:54:16 +020029//usage: "[-r] [DEVICE]"
Pere Orga55068c42011-03-27 23:42:28 +020030//usage:#define setconsole_full_usage "\n\n"
Denys Vlasenko316d38e2017-08-07 17:36:41 +020031//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 Orga55068c42011-03-27 23:42:28 +020035
Denys Vlasenkoab233812017-08-06 18:54:16 +020036/* It was a bbox-specific invention, but SUSE does have a similar utility.
37 * SUSE has no -r option, though.
38 */
39
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000040#include "libbb.h"
Paul Foxfc2256a2005-08-01 18:12:30 +000041
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000042int setconsole_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000043int setconsole_main(int argc UNUSED_PARAM, char **argv)
Paul Foxfc2256a2005-08-01 18:12:30 +000044{
Rob Landley9f0e00f2005-09-08 03:27:06 +000045 const char *device = CURRENT_TTY;
Denys Vlasenko316d38e2017-08-07 17:36:41 +020046 int reset;
Paul Foxfc2256a2005-08-01 18:12:30 +000047
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000048 /* at most one non-option argument */
Denys Vlasenko22542ec2017-08-08 21:55:02 +020049 reset = getopt32(argv, "^" "r" "\0" "?1");
Paul Foxfc2256a2005-08-01 18:12:30 +000050
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000051 argv += 1 + reset;
52 if (*argv) {
53 device = *argv;
Paul Foxfc2256a2005-08-01 18:12:30 +000054 } else {
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000055 if (reset)
Denis Vlasenkoec27feb2007-02-17 15:52:02 +000056 device = DEV_CONSOLE;
Paul Foxfc2256a2005-08-01 18:12:30 +000057 }
58
Denys Vlasenko316d38e2017-08-07 17:36:41 +020059//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 Korsgaard8dc61952011-05-26 17:51:37 +020062 xioctl(xopen(device, O_WRONLY), TIOCCONS, NULL);
Paul Foxfc2256a2005-08-01 18:12:30 +000063 return EXIT_SUCCESS;
64}