blob: 519513623e42b9ccf5696980474d0621976376f8 [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>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000020 */
21
22/* getopt not needed */
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <unistd.h>
27#include <fcntl.h>
28#include <string.h>
29#include <sys/types.h>
Glenn L McGratha6bbf792002-12-08 12:08:37 +000030#include <ctype.h>
31
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000032#include "busybox.h"
33
34#define VTNAME "/dev/tty%d"
35
36int openvt_main(int argc, char **argv)
37{
38 int pid;
39 int fd;
40 int vtno;
41 char vtname[sizeof VTNAME + 2];
42 char * cmd = NULL;
43 char * cmd_args = NULL;
44
45 if (argc < 3)
Manuel Novoa III cad53642003-03-19 09:13:01 +000046 bb_show_usage();
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000047
48 if (!isdigit(argv[1][0]))
Manuel Novoa III cad53642003-03-19 09:13:01 +000049 bb_show_usage();
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000050
51 vtno = (int) atol(argv[1]);
52
53 /* if (vtno <= 0 || vtno > 63) */
54 if (vtno <= 0 || vtno > 12)
Manuel Novoa III cad53642003-03-19 09:13:01 +000055 bb_error_msg_and_die("Illegal vt number (%d)", vtno);
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000056
57 sprintf(vtname, VTNAME, vtno);
58
59 cmd = argv[2];
60 cmd_args = xmalloc(80);
61 cmd_args[0] = '\0';
62
63 if((pid = fork()) == 0) {
64 /* leave current vt */
65
66#ifdef ESIX_5_3_2_D
67 if (setpgrp() < 0) {
68#else
69 if (setsid() < 0) {
70#endif
71
Manuel Novoa III cad53642003-03-19 09:13:01 +000072 bb_perror_msg_and_die("Unable to set new session");
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000073 }
74 close(0); /* so that new vt becomes stdin */
75
76 /* and grab new one */
77 if ((fd = open(vtname, O_RDWR)) == -1)
Manuel Novoa III cad53642003-03-19 09:13:01 +000078 bb_perror_msg_and_die("could not open %s", vtname);
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000079
80 /* Reassign stdout and sterr */
81 close(1);
82 close(2);
83 dup(fd);
84 dup(fd);
85
86 execvp(cmd, &argv[2]);
87 /*execlp(cmd, cmd_args);*/
88 _exit(1);
89 }
90 return EXIT_SUCCESS;
91}
92
93/*
94Local Variables:
95c-file-style: "linux"
96c-basic-offset: 4
97tab-width: 4
98End:
99*/