blob: f1cf5645b3fb396224f7bfd6b03466dd2d3b2f3b [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
Glenn L McGrathec0c48c2002-09-16 03:16:06 +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) {
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000028 /* leave current vt */
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000029 if (setsid() < 0) {
Rob Landleyead19302006-03-10 23:16:25 +000030 bb_perror_msg_and_die("setsid");
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000031 }
32 close(0); /* so that new vt becomes stdin */
33
34 /* and grab new one */
Rob Landleyd921b2e2006-08-03 15:41:12 +000035 fd = xopen(vtname, O_RDWR);
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000036
37 /* Reassign stdout and sterr */
Rob Landley9f0e00f2005-09-08 03:27:06 +000038 dup2(fd, STDOUT_FILENO);
39 dup2(fd, STDERR_FILENO);
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000040
Rob Landleyead19302006-03-10 23:16:25 +000041 execvp(argv[2], &argv[2]);
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000042 _exit(1);
43 }
44 return EXIT_SUCCESS;
45}