blob: 0584584df91910be8e577d604ea2c82eea8e0331 [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
Denis Vlasenko06af2162007-02-03 17:28:39 +000015int openvt_main(int argc, char **argv);
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000016int openvt_main(int argc, char **argv)
17{
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000018 int fd;
Rob Landleyead19302006-03-10 23:16:25 +000019 char vtname[sizeof(VC_FORMAT) + 2];
Glenn L McGrath0a3b0102003-05-13 16:31:15 +000020
Rob Landleyead19302006-03-10 23:16:25 +000021 if (argc < 3) {
22 bb_show_usage();
23 }
Denis Vlasenko13858992006-10-08 12:49:22 +000024 /* check for illegal vt number: < 1 or > 63 */
Denis Vlasenkocad04ef2007-03-25 23:21:05 +000025 sprintf(vtname, VC_FORMAT, (int)xatou_range(argv[1], 1, 63));
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000026
Denis Vlasenkocad04ef2007-03-25 23:21:05 +000027//FIXME NOMMU
Rob Landleyead19302006-03-10 23:16:25 +000028 if (fork() == 0) {
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +000029 /* child */
30 /* leave current vt (controlling tty) */
31 setsid();
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000032 /* and grab new one */
Rob Landleyd921b2e2006-08-03 15:41:12 +000033 fd = xopen(vtname, O_RDWR);
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +000034 /* Reassign stdin, stdout and sterr */
35 dup2(fd, STDIN_FILENO);
Rob Landley9f0e00f2005-09-08 03:27:06 +000036 dup2(fd, STDOUT_FILENO);
37 dup2(fd, STDERR_FILENO);
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +000038 while (fd > 2) close(fd--);
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000039
Denis Vlasenko1d76f432007-02-06 01:20:12 +000040 BB_EXECVP(argv[2], &argv[2]);
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000041 _exit(1);
42 }
43 return EXIT_SUCCESS;
44}