blob: c7b3e4fa4519fd1029879178a95a0a2115e89a39 [file] [log] [blame]
Glenn L McGrathec0c48c2002-09-16 03:16:06 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrathec0c48c2002-09-16 03:16:06 +00002/*
Glenn L McGrathebdc8b42002-09-16 03:47:48 +00003 * openvt.c - open a vt to run a command.
Glenn L McGrathec0c48c2002-09-16 03:16:06 +00004 *
Glenn L McGrathebdc8b42002-09-16 03:47:48 +00005 * busyboxed by Quy Tonthat <quy@signal3.com>
Glenn L McGrath0a3b0102003-05-13 16:31:15 +00006 * hacked by Tito <farmatito@tiscali.it>
Glenn L McGrathebdc8b42002-09-16 03:47:48 +00007 *
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrathec0c48c2002-09-16 03:16:06 +00009 */
10
11/* getopt not needed */
12
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000013#include "busybox.h"
14
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000015int openvt_main(int argc, char **argv)
16{
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000017 int fd;
Rob Landleyead19302006-03-10 23:16:25 +000018 char vtname[sizeof(VC_FORMAT) + 2];
Glenn L McGrath0a3b0102003-05-13 16:31:15 +000019
Rob Landleyead19302006-03-10 23:16:25 +000020 if (argc < 3) {
21 bb_show_usage();
22 }
Denis Vlasenko13858992006-10-08 12:49:22 +000023 /* check for illegal vt number: < 1 or > 63 */
24 sprintf(vtname, VC_FORMAT, (int)xatoul_range(argv[1], 1, 63));
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000025
Rob Landleyead19302006-03-10 23:16:25 +000026 if (fork() == 0) {
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +000027 /* child */
28 /* leave current vt (controlling tty) */
29 setsid();
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000030 /* and grab new one */
Rob Landleyd921b2e2006-08-03 15:41:12 +000031 fd = xopen(vtname, O_RDWR);
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +000032 /* Reassign stdin, stdout and sterr */
33 dup2(fd, STDIN_FILENO);
Rob Landley9f0e00f2005-09-08 03:27:06 +000034 dup2(fd, STDOUT_FILENO);
35 dup2(fd, STDERR_FILENO);
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +000036 while (fd > 2) close(fd--);
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000037
Rob Landleyead19302006-03-10 23:16:25 +000038 execvp(argv[2], &argv[2]);
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000039 _exit(1);
40 }
41 return EXIT_SUCCESS;
42}