Glenn L McGrath | ec0c48c | 2002-09-16 03:16:06 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | ec0c48c | 2002-09-16 03:16:06 +0000 | [diff] [blame] | 2 | /* |
Glenn L McGrath | ebdc8b4 | 2002-09-16 03:47:48 +0000 | [diff] [blame] | 3 | * openvt.c - open a vt to run a command. |
Glenn L McGrath | ec0c48c | 2002-09-16 03:16:06 +0000 | [diff] [blame] | 4 | * |
Glenn L McGrath | ebdc8b4 | 2002-09-16 03:47:48 +0000 | [diff] [blame] | 5 | * 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 McGrath | ec0c48c | 2002-09-16 03:16:06 +0000 | [diff] [blame] | 20 | */ |
| 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 McGrath | a6bbf79 | 2002-12-08 12:08:37 +0000 | [diff] [blame] | 30 | #include <ctype.h> |
| 31 | |
Glenn L McGrath | ec0c48c | 2002-09-16 03:16:06 +0000 | [diff] [blame] | 32 | #include "busybox.h" |
| 33 | |
| 34 | #define VTNAME "/dev/tty%d" |
| 35 | |
| 36 | int 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 | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 46 | bb_show_usage(); |
Glenn L McGrath | ec0c48c | 2002-09-16 03:16:06 +0000 | [diff] [blame] | 47 | |
| 48 | if (!isdigit(argv[1][0])) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 49 | bb_show_usage(); |
Glenn L McGrath | ec0c48c | 2002-09-16 03:16:06 +0000 | [diff] [blame] | 50 | |
| 51 | vtno = (int) atol(argv[1]); |
| 52 | |
| 53 | /* if (vtno <= 0 || vtno > 63) */ |
| 54 | if (vtno <= 0 || vtno > 12) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 55 | bb_error_msg_and_die("Illegal vt number (%d)", vtno); |
Glenn L McGrath | ec0c48c | 2002-09-16 03:16:06 +0000 | [diff] [blame] | 56 | |
| 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 | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 72 | bb_perror_msg_and_die("Unable to set new session"); |
Glenn L McGrath | ec0c48c | 2002-09-16 03:16:06 +0000 | [diff] [blame] | 73 | } |
| 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 | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 78 | bb_perror_msg_and_die("could not open %s", vtname); |
Glenn L McGrath | ec0c48c | 2002-09-16 03:16:06 +0000 | [diff] [blame] | 79 | |
| 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 | /* |
| 94 | Local Variables: |
| 95 | c-file-style: "linux" |
| 96 | c-basic-offset: 4 |
| 97 | tab-width: 4 |
| 98 | End: |
| 99 | */ |