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> |
Glenn L McGrath | 0a3b010 | 2003-05-13 16:31:15 +0000 | [diff] [blame] | 6 | * hacked by Tito <farmatito@tiscali.it> |
Glenn L McGrath | ebdc8b4 | 2002-09-16 03:47:48 +0000 | [diff] [blame] | 7 | * |
Bernhard Reutner-Fischer | b1629b1 | 2006-05-19 19:29:19 +0000 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Glenn L McGrath | ec0c48c | 2002-09-16 03:16:06 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Denis Vlasenko | 95891fc | 2008-03-27 16:26:35 +0000 | [diff] [blame] | 11 | #include <linux/vt.h> |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 12 | #include "libbb.h" |
Glenn L McGrath | ec0c48c | 2002-09-16 03:16:06 +0000 | [diff] [blame] | 13 | |
Denis Vlasenko | 95891fc | 2008-03-27 16:26:35 +0000 | [diff] [blame] | 14 | /* "Standard" openvt's man page (we do not support all of this): |
| 15 | |
| 16 | openvt [-c NUM] [-fsulv] [--] [command [args]] |
| 17 | |
| 18 | Find the first available VT, and run command on it. Stdio is directed |
| 19 | to that VT. If no command is specified then $SHELL is used. |
| 20 | |
| 21 | -c NUM |
| 22 | Use the given VT number, not the first free one. |
| 23 | -f |
| 24 | Force opening a VT: don't try to check if VT is already in use. |
| 25 | -s |
| 26 | Switch to the new VT when starting the command. |
| 27 | The VT of the new command will be made the new current VT. |
| 28 | -u |
| 29 | Figure out the owner of the current VT, and run login as that user. |
| 30 | Suitable to be called by init. Shouldn't be used with -c or -l. |
| 31 | -l |
| 32 | Make the command a login shell: a "-" is prepended to the argv[0] |
| 33 | when command is executed. |
| 34 | -v |
| 35 | Verbose. |
| 36 | -w |
| 37 | Wait for command to complete. If -w and -s are used together, |
| 38 | switch back to the controlling terminal when the command completes. |
| 39 | |
| 40 | bbox: |
| 41 | -u: not implemented |
| 42 | -f: always in effect |
| 43 | -l: not implemented, ignored |
| 44 | -v: ignored |
| 45 | -ws: does NOT switch back |
| 46 | */ |
| 47 | |
| 48 | /* Helper: does this fd understand VT_xxx? */ |
| 49 | static int not_vt_fd(int fd) |
Glenn L McGrath | ec0c48c | 2002-09-16 03:16:06 +0000 | [diff] [blame] | 50 | { |
Denis Vlasenko | 95891fc | 2008-03-27 16:26:35 +0000 | [diff] [blame] | 51 | struct vt_stat vtstat; |
| 52 | return ioctl(fd, VT_GETSTATE, &vtstat); /* !0: error, it's not VT fd */ |
| 53 | } |
Glenn L McGrath | 0a3b010 | 2003-05-13 16:31:15 +0000 | [diff] [blame] | 54 | |
Denis Vlasenko | 95891fc | 2008-03-27 16:26:35 +0000 | [diff] [blame] | 55 | /* Helper: get a fd suitable for VT_xxx */ |
| 56 | static int get_vt_fd(void) |
| 57 | { |
| 58 | int fd; |
Denis Vlasenko | 53091ec | 2007-03-26 13:35:09 +0000 | [diff] [blame] | 59 | |
Denis Vlasenko | 95891fc | 2008-03-27 16:26:35 +0000 | [diff] [blame] | 60 | /* Do we, by chance, already have it? */ |
| 61 | for (fd = 0; fd < 3; fd++) |
| 62 | if (!not_vt_fd(fd)) |
Denis Vlasenko | 1f22898 | 2008-04-22 00:16:29 +0000 | [diff] [blame] | 63 | return fd; |
Denys Vlasenko | ab19ede | 2009-11-11 21:05:42 +0100 | [diff] [blame] | 64 | fd = open(DEV_CONSOLE, O_RDONLY | O_NONBLOCK); |
Denis Vlasenko | 95891fc | 2008-03-27 16:26:35 +0000 | [diff] [blame] | 65 | if (fd >= 0 && !not_vt_fd(fd)) |
| 66 | return fd; |
| 67 | bb_error_msg_and_die("can't find open VT"); |
| 68 | } |
Glenn L McGrath | ec0c48c | 2002-09-16 03:16:06 +0000 | [diff] [blame] | 69 | |
Denis Vlasenko | 95891fc | 2008-03-27 16:26:35 +0000 | [diff] [blame] | 70 | static int find_free_vtno(void) |
| 71 | { |
| 72 | int vtno; |
| 73 | int fd = get_vt_fd(); |
| 74 | |
Denis Vlasenko | 1f22898 | 2008-04-22 00:16:29 +0000 | [diff] [blame] | 75 | errno = 0; |
Denis Vlasenko | 95891fc | 2008-03-27 16:26:35 +0000 | [diff] [blame] | 76 | /*xfunc_error_retval = 3; - do we need compat? */ |
| 77 | if (ioctl(fd, VT_OPENQRY, &vtno) != 0 || vtno <= 0) |
| 78 | bb_perror_msg_and_die("can't find open VT"); |
| 79 | // Not really needed, grep for DAEMON_ONLY_SANITIZE |
| 80 | // if (fd > 2) |
| 81 | // close(fd); |
| 82 | return vtno; |
| 83 | } |
| 84 | |
| 85 | /* vfork scares gcc, it generates bigger code. |
| 86 | * Keep it away from main program. |
| 87 | * TODO: move to libbb; or adapt existing libbb's spawn(). |
| 88 | */ |
| 89 | static NOINLINE void vfork_child(char **argv) |
| 90 | { |
| 91 | if (vfork() == 0) { |
| 92 | /* CHILD */ |
| 93 | /* Try to make this VT our controlling tty */ |
| 94 | setsid(); /* lose old ctty */ |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 95 | ioctl(STDIN_FILENO, TIOCSCTTY, 0 /* 0: don't forcibly steal */); |
Denis Vlasenko | 95891fc | 2008-03-27 16:26:35 +0000 | [diff] [blame] | 96 | //bb_error_msg("our sid %d", getsid(0)); |
| 97 | //bb_error_msg("our pgrp %d", getpgrp()); |
| 98 | //bb_error_msg("VT's sid %d", tcgetsid(0)); |
| 99 | //bb_error_msg("VT's pgrp %d", tcgetpgrp(0)); |
| 100 | BB_EXECVP(argv[0], argv); |
| 101 | bb_perror_msg_and_die("exec %s", argv[0]); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | int openvt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 106 | int openvt_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 95891fc | 2008-03-27 16:26:35 +0000 | [diff] [blame] | 107 | { |
| 108 | char vtname[sizeof(VC_FORMAT) + sizeof(int)*3]; |
Denis Vlasenko | 1b2d0b2 | 2008-04-19 03:44:45 +0000 | [diff] [blame] | 109 | struct vt_stat vtstat; |
Denis Vlasenko | 95891fc | 2008-03-27 16:26:35 +0000 | [diff] [blame] | 110 | char *str_c; |
| 111 | int vtno; |
| 112 | int flags; |
| 113 | enum { |
| 114 | OPT_c = (1 << 0), |
| 115 | OPT_w = (1 << 1), |
| 116 | OPT_s = (1 << 2), |
| 117 | OPT_l = (1 << 3), |
| 118 | OPT_f = (1 << 4), |
| 119 | OPT_v = (1 << 5), |
| 120 | }; |
| 121 | |
| 122 | /* "+" - stop on first non-option */ |
| 123 | flags = getopt32(argv, "+c:wslfv", &str_c); |
| 124 | argv += optind; |
| 125 | |
| 126 | if (flags & OPT_c) { |
| 127 | /* Check for illegal vt number: < 1 or > 63 */ |
| 128 | vtno = xatou_range(str_c, 1, 63); |
| 129 | } else { |
| 130 | vtno = find_free_vtno(); |
| 131 | } |
| 132 | |
| 133 | /* Grab new VT */ |
| 134 | sprintf(vtname, VC_FORMAT, vtno); |
| 135 | /* (Try to) clean up stray open fds above fd 2 */ |
| 136 | bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS | DAEMON_ONLY_SANITIZE, NULL); |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 137 | close(STDIN_FILENO); |
Denis Vlasenko | 95891fc | 2008-03-27 16:26:35 +0000 | [diff] [blame] | 138 | /*setsid(); - BAD IDEA: after we exit, child is SIGHUPed... */ |
Denis Vlasenko | 53091ec | 2007-03-26 13:35:09 +0000 | [diff] [blame] | 139 | xopen(vtname, O_RDWR); |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 140 | xioctl(STDIN_FILENO, VT_GETSTATE, &vtstat); |
Denis Vlasenko | 95891fc | 2008-03-27 16:26:35 +0000 | [diff] [blame] | 141 | |
| 142 | if (flags & OPT_s) { |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 143 | console_make_active(STDIN_FILENO, vtno); |
Denis Vlasenko | 95891fc | 2008-03-27 16:26:35 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | if (!argv[0]) { |
| 147 | argv--; |
| 148 | argv[0] = getenv("SHELL"); |
| 149 | if (!argv[0]) |
| 150 | argv[0] = (char *) DEFAULT_SHELL; |
| 151 | /*argv[1] = NULL; - already is */ |
| 152 | } |
| 153 | |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 154 | xdup2(STDIN_FILENO, STDOUT_FILENO); |
| 155 | xdup2(STDIN_FILENO, STDERR_FILENO); |
Glenn L McGrath | ec0c48c | 2002-09-16 03:16:06 +0000 | [diff] [blame] | 156 | |
Denis Vlasenko | 95891fc | 2008-03-27 16:26:35 +0000 | [diff] [blame] | 157 | #ifdef BLOAT |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 158 | { |
Denis Vlasenko | 95891fc | 2008-03-27 16:26:35 +0000 | [diff] [blame] | 159 | /* Handle -l (login shell) option */ |
| 160 | const char *prog = argv[0]; |
| 161 | if (flags & OPT_l) |
| 162 | argv[0] = xasprintf("-%s", argv[0]); |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 163 | } |
Denis Vlasenko | 95891fc | 2008-03-27 16:26:35 +0000 | [diff] [blame] | 164 | #endif |
| 165 | |
| 166 | vfork_child(argv); |
| 167 | if (flags & OPT_w) { |
Denis Vlasenko | 1b2d0b2 | 2008-04-19 03:44:45 +0000 | [diff] [blame] | 168 | /* We have only one child, wait for it */ |
| 169 | safe_waitpid(-1, NULL, 0); /* loops on EINTR */ |
| 170 | if (flags & OPT_s) { |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 171 | console_make_active(STDIN_FILENO, vtstat.v_active); |
Denis Vlasenko | 1b2d0b2 | 2008-04-19 03:44:45 +0000 | [diff] [blame] | 172 | // Compat: even with -c N (try to) disallocate: |
| 173 | // # /usr/app/kbd-1.12/bin/openvt -f -c 9 -ws sleep 5 |
| 174 | // openvt: could not deallocate console 9 |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 175 | xioctl(STDIN_FILENO, VT_DISALLOCATE, (void*)(ptrdiff_t)vtno); |
Denis Vlasenko | 1b2d0b2 | 2008-04-19 03:44:45 +0000 | [diff] [blame] | 176 | } |
Denis Vlasenko | 95891fc | 2008-03-27 16:26:35 +0000 | [diff] [blame] | 177 | } |
Denis Vlasenko | 95891fc | 2008-03-27 16:26:35 +0000 | [diff] [blame] | 178 | return EXIT_SUCCESS; |
Glenn L McGrath | ec0c48c | 2002-09-16 03:16:06 +0000 | [diff] [blame] | 179 | } |