blob: 423122fe9e1ecf152122f551b839f82efa8e004f [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrathec0c48c2002-09-16 03:16:06 +00009 */
Denys Vlasenko6d932992016-11-23 10:39:27 +010010//config:config OPENVT
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020011//config: bool "openvt (7 kb)"
Denys Vlasenko6d932992016-11-23 10:39:27 +010012//config: default y
13//config: select PLATFORM_LINUX
14//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020015//config: This program is used to start a command on an unused
16//config: virtual terminal.
Denys Vlasenko6d932992016-11-23 10:39:27 +010017
18//applet:IF_OPENVT(APPLET(openvt, BB_DIR_USR_BIN, BB_SUID_DROP))
19
20//kbuild:lib-$(CONFIG_OPENVT) += openvt.o
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000021
Pere Orga55068c42011-03-27 23:42:28 +020022//usage:#define openvt_trivial_usage
23//usage: "[-c N] [-sw] [PROG ARGS]"
24//usage:#define openvt_full_usage "\n\n"
25//usage: "Start PROG on a new virtual terminal\n"
Pere Orga55068c42011-03-27 23:42:28 +020026//usage: "\n -c N Use specified VT"
27//usage: "\n -s Switch to the VT"
28/* //usage: "\n -l Run PROG as login shell (by prepending '-')" */
29//usage: "\n -w Wait for PROG to exit"
30//usage:
31//usage:#define openvt_example_usage
32//usage: "openvt 2 /bin/ash\n"
33
Denis Vlasenko95891fc2008-03-27 16:26:35 +000034#include <linux/vt.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000035#include "libbb.h"
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000036
Denis Vlasenko95891fc2008-03-27 16:26:35 +000037/* "Standard" openvt's man page (we do not support all of this):
38
39openvt [-c NUM] [-fsulv] [--] [command [args]]
40
41Find the first available VT, and run command on it. Stdio is directed
42to that VT. If no command is specified then $SHELL is used.
43
44-c NUM
45 Use the given VT number, not the first free one.
46-f
47 Force opening a VT: don't try to check if VT is already in use.
48-s
49 Switch to the new VT when starting the command.
50 The VT of the new command will be made the new current VT.
51-u
52 Figure out the owner of the current VT, and run login as that user.
53 Suitable to be called by init. Shouldn't be used with -c or -l.
54-l
55 Make the command a login shell: a "-" is prepended to the argv[0]
56 when command is executed.
57-v
58 Verbose.
59-w
60 Wait for command to complete. If -w and -s are used together,
61 switch back to the controlling terminal when the command completes.
62
63bbox:
64-u: not implemented
65-f: always in effect
66-l: not implemented, ignored
67-v: ignored
68-ws: does NOT switch back
69*/
70
71/* Helper: does this fd understand VT_xxx? */
72static int not_vt_fd(int fd)
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000073{
Denis Vlasenko95891fc2008-03-27 16:26:35 +000074 struct vt_stat vtstat;
75 return ioctl(fd, VT_GETSTATE, &vtstat); /* !0: error, it's not VT fd */
76}
Glenn L McGrath0a3b0102003-05-13 16:31:15 +000077
Denis Vlasenko95891fc2008-03-27 16:26:35 +000078/* Helper: get a fd suitable for VT_xxx */
79static int get_vt_fd(void)
80{
81 int fd;
Denis Vlasenko53091ec2007-03-26 13:35:09 +000082
Denis Vlasenko95891fc2008-03-27 16:26:35 +000083 /* Do we, by chance, already have it? */
84 for (fd = 0; fd < 3; fd++)
85 if (!not_vt_fd(fd))
Denis Vlasenko1f228982008-04-22 00:16:29 +000086 return fd;
Denys Vlasenkoab19ede2009-11-11 21:05:42 +010087 fd = open(DEV_CONSOLE, O_RDONLY | O_NONBLOCK);
Denis Vlasenko95891fc2008-03-27 16:26:35 +000088 if (fd >= 0 && !not_vt_fd(fd))
89 return fd;
90 bb_error_msg_and_die("can't find open VT");
91}
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000092
Denis Vlasenko95891fc2008-03-27 16:26:35 +000093static int find_free_vtno(void)
94{
95 int vtno;
96 int fd = get_vt_fd();
97
Denis Vlasenko1f228982008-04-22 00:16:29 +000098 errno = 0;
Denis Vlasenko95891fc2008-03-27 16:26:35 +000099 /*xfunc_error_retval = 3; - do we need compat? */
100 if (ioctl(fd, VT_OPENQRY, &vtno) != 0 || vtno <= 0)
101 bb_perror_msg_and_die("can't find open VT");
Denys Vlasenkob182e9a2017-08-04 23:04:17 +0200102// Not really needed, grep for DAEMON_CLOSE_EXTRA_FDS
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000103// if (fd > 2)
104// close(fd);
105 return vtno;
106}
107
108/* vfork scares gcc, it generates bigger code.
109 * Keep it away from main program.
110 * TODO: move to libbb; or adapt existing libbb's spawn().
111 */
112static NOINLINE void vfork_child(char **argv)
113{
114 if (vfork() == 0) {
115 /* CHILD */
116 /* Try to make this VT our controlling tty */
117 setsid(); /* lose old ctty */
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +0000118 ioctl(STDIN_FILENO, TIOCSCTTY, 0 /* 0: don't forcibly steal */);
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000119 //bb_error_msg("our sid %d", getsid(0));
120 //bb_error_msg("our pgrp %d", getpgrp());
121 //bb_error_msg("VT's sid %d", tcgetsid(0));
122 //bb_error_msg("VT's pgrp %d", tcgetpgrp(0));
Pascal Bellard21e8e8d2010-07-04 00:57:03 +0200123 BB_EXECVP_or_die(argv);
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000124 }
125}
126
127int openvt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000128int openvt_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000129{
130 char vtname[sizeof(VC_FORMAT) + sizeof(int)*3];
Denis Vlasenko1b2d0b22008-04-19 03:44:45 +0000131 struct vt_stat vtstat;
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000132 char *str_c;
133 int vtno;
134 int flags;
135 enum {
136 OPT_c = (1 << 0),
137 OPT_w = (1 << 1),
138 OPT_s = (1 << 2),
139 OPT_l = (1 << 3),
140 OPT_f = (1 << 4),
141 OPT_v = (1 << 5),
142 };
143
144 /* "+" - stop on first non-option */
145 flags = getopt32(argv, "+c:wslfv", &str_c);
146 argv += optind;
147
148 if (flags & OPT_c) {
149 /* Check for illegal vt number: < 1 or > 63 */
150 vtno = xatou_range(str_c, 1, 63);
151 } else {
152 vtno = find_free_vtno();
153 }
154
155 /* Grab new VT */
156 sprintf(vtname, VC_FORMAT, vtno);
157 /* (Try to) clean up stray open fds above fd 2 */
Denys Vlasenkob182e9a2017-08-04 23:04:17 +0200158 bb_daemon_helper(DAEMON_CLOSE_EXTRA_FDS);
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +0000159 close(STDIN_FILENO);
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000160 /*setsid(); - BAD IDEA: after we exit, child is SIGHUPed... */
Denis Vlasenko53091ec2007-03-26 13:35:09 +0000161 xopen(vtname, O_RDWR);
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +0000162 xioctl(STDIN_FILENO, VT_GETSTATE, &vtstat);
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000163
164 if (flags & OPT_s) {
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +0000165 console_make_active(STDIN_FILENO, vtno);
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000166 }
167
168 if (!argv[0]) {
169 argv--;
Denys Vlasenko681efe22011-03-08 21:00:36 +0100170 argv[0] = (char *) get_shell_name();
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000171 /*argv[1] = NULL; - already is */
172 }
173
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +0000174 xdup2(STDIN_FILENO, STDOUT_FILENO);
175 xdup2(STDIN_FILENO, STDERR_FILENO);
Glenn L McGrathec0c48c2002-09-16 03:16:06 +0000176
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000177#ifdef BLOAT
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +0000178 {
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000179 /* Handle -l (login shell) option */
180 const char *prog = argv[0];
181 if (flags & OPT_l)
182 argv[0] = xasprintf("-%s", argv[0]);
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +0000183 }
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000184#endif
185
186 vfork_child(argv);
187 if (flags & OPT_w) {
Denis Vlasenko1b2d0b22008-04-19 03:44:45 +0000188 /* We have only one child, wait for it */
189 safe_waitpid(-1, NULL, 0); /* loops on EINTR */
190 if (flags & OPT_s) {
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +0000191 console_make_active(STDIN_FILENO, vtstat.v_active);
Denis Vlasenko1b2d0b22008-04-19 03:44:45 +0000192 // Compat: even with -c N (try to) disallocate:
193 // # /usr/app/kbd-1.12/bin/openvt -f -c 9 -ws sleep 5
194 // openvt: could not deallocate console 9
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +0000195 xioctl(STDIN_FILENO, VT_DISALLOCATE, (void*)(ptrdiff_t)vtno);
Denis Vlasenko1b2d0b22008-04-19 03:44:45 +0000196 }
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000197 }
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000198 return EXIT_SUCCESS;
Glenn L McGrathec0c48c2002-09-16 03:16:06 +0000199}