blob: 79a4313f0313a4412c1fa4e2186fcf9f030a6439 [file] [log] [blame]
Paul Foxfc2256a2005-08-01 18:12:30 +00001/* 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 *
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Paul Foxfc2256a2005-08-01 18:12:30 +00008 */
9
10#include <sys/ioctl.h>
11#include <sys/stat.h>
12#include <sys/types.h>
13#include <fcntl.h>
14#include <stdio.h>
Rob Landleyb7128c62005-09-11 01:05:30 +000015#include <getopt.h> /* struct option */
Paul Foxfc2256a2005-08-01 18:12:30 +000016
17#include "busybox.h"
18
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000019#if ENABLE_FEATURE_SETCONSOLE_LONG_OPTIONS
Paul Foxfc2256a2005-08-01 18:12:30 +000020static const struct option setconsole_long_options[] = {
21 { "reset", 0, NULL, 'r' },
22 { 0, 0, 0, 0 }
23};
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000024#endif
Paul Foxfc2256a2005-08-01 18:12:30 +000025
26#define OPT_SETCONS_RESET 1
27
28int setconsole_main(int argc, char **argv)
29{
Paul Foxfc2256a2005-08-01 18:12:30 +000030 unsigned long flags;
Rob Landley9f0e00f2005-09-08 03:27:06 +000031 const char *device = CURRENT_TTY;
Paul Foxfc2256a2005-08-01 18:12:30 +000032
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000033#if ENABLE_FEATURE_SETCONSOLE_LONG_OPTIONS
Paul Foxfc2256a2005-08-01 18:12:30 +000034 bb_applet_long_options = setconsole_long_options;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000035#endif
Paul Foxfc2256a2005-08-01 18:12:30 +000036 flags = bb_getopt_ulflags(argc, argv, "r");
37
38 if (argc - optind > 1)
39 bb_show_usage();
40
41 if (argc - optind == 1) {
42 if (flags & OPT_SETCONS_RESET)
43 bb_show_usage();
44 device = argv[optind];
45 } else {
46 if (flags & OPT_SETCONS_RESET)
Rob Landley9f0e00f2005-09-08 03:27:06 +000047 device = CONSOLE_DEV;
Paul Foxfc2256a2005-08-01 18:12:30 +000048 }
49
Rob Landley9f0e00f2005-09-08 03:27:06 +000050 if (-1 == ioctl(bb_xopen(device, O_RDONLY), TIOCCONS)) {
51 bb_perror_msg_and_die("TIOCCONS");
Paul Foxfc2256a2005-08-01 18:12:30 +000052 }
53 return EXIT_SUCCESS;
54}