blob: eb9f49fe25931091dccbe42faf81d62030186022 [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 */
25 sprintf(vtname, VC_FORMAT, (int)xatoul_range(argv[1], 1, 63));
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000026
Rob Landleyead19302006-03-10 23:16:25 +000027 if (fork() == 0) {
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +000028 /* child */
29 /* leave current vt (controlling tty) */
30 setsid();
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000031 /* and grab new one */
Rob Landleyd921b2e2006-08-03 15:41:12 +000032 fd = xopen(vtname, O_RDWR);
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +000033 /* Reassign stdin, stdout and sterr */
34 dup2(fd, STDIN_FILENO);
Rob Landley9f0e00f2005-09-08 03:27:06 +000035 dup2(fd, STDOUT_FILENO);
36 dup2(fd, STDERR_FILENO);
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +000037 while (fd > 2) close(fd--);
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000038
Denis Vlasenko1d76f432007-02-06 01:20:12 +000039 BB_EXECVP(argv[2], &argv[2]);
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000040 _exit(1);
41 }
42 return EXIT_SUCCESS;
43}