blob: 0c0cef23ca435d2a3cf12dca908fcc83126a1669 [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
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <fcntl.h>
17#include <string.h>
18#include <sys/types.h>
Glenn L McGratha6bbf792002-12-08 12:08:37 +000019#include <ctype.h>
20
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000021#include "busybox.h"
22
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000023int openvt_main(int argc, char **argv)
24{
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000025 int fd;
Rob Landleyead19302006-03-10 23:16:25 +000026 char vtname[sizeof(VC_FORMAT) + 2];
Glenn L McGrath0a3b0102003-05-13 16:31:15 +000027
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000028
Rob Landleyead19302006-03-10 23:16:25 +000029 if (argc < 3) {
30 bb_show_usage();
31 }
Glenn L McGrathdfb62112004-01-13 10:12:16 +000032 /* check for Illegal vt number: < 1 or > 12 */
Rob Landleyead19302006-03-10 23:16:25 +000033 sprintf(vtname, VC_FORMAT, (int)bb_xgetlarg(argv[1], 10, 1, 12));
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000034
Rob Landleyead19302006-03-10 23:16:25 +000035 if (fork() == 0) {
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000036 /* leave current vt */
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000037 if (setsid() < 0) {
Rob Landleyead19302006-03-10 23:16:25 +000038 bb_perror_msg_and_die("setsid");
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000039 }
40 close(0); /* so that new vt becomes stdin */
41
42 /* and grab new one */
Rob Landleyd921b2e2006-08-03 15:41:12 +000043 fd = xopen(vtname, O_RDWR);
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000044
45 /* Reassign stdout and sterr */
Rob Landley9f0e00f2005-09-08 03:27:06 +000046 dup2(fd, STDOUT_FILENO);
47 dup2(fd, STDERR_FILENO);
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000048
Rob Landleyead19302006-03-10 23:16:25 +000049 execvp(argv[2], &argv[2]);
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000050 _exit(1);
51 }
52 return EXIT_SUCCESS;
53}