blob: db2f073b2e40c84db5a88a8eb7eef4d6a668094f [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 Vlasenkob097a842018-12-28 03:20:17 +010011//config: bool "openvt (7.2 kb)"
Denys Vlasenko6d932992016-11-23 10:39:27 +010012//config: default y
Denys Vlasenko6d932992016-11-23 10:39:27 +010013//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020014//config: This program is used to start a command on an unused
15//config: virtual terminal.
Denys Vlasenko6d932992016-11-23 10:39:27 +010016
17//applet:IF_OPENVT(APPLET(openvt, BB_DIR_USR_BIN, BB_SUID_DROP))
18
19//kbuild:lib-$(CONFIG_OPENVT) += openvt.o
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000020
Pere Orga55068c42011-03-27 23:42:28 +020021//usage:#define openvt_trivial_usage
22//usage: "[-c N] [-sw] [PROG ARGS]"
23//usage:#define openvt_full_usage "\n\n"
24//usage: "Start PROG on a new virtual terminal\n"
Pere Orga55068c42011-03-27 23:42:28 +020025//usage: "\n -c N Use specified VT"
26//usage: "\n -s Switch to the VT"
27/* //usage: "\n -l Run PROG as login shell (by prepending '-')" */
28//usage: "\n -w Wait for PROG to exit"
29//usage:
30//usage:#define openvt_example_usage
31//usage: "openvt 2 /bin/ash\n"
32
Denis Vlasenko95891fc2008-03-27 16:26:35 +000033#include <linux/vt.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000034#include "libbb.h"
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000035
Denis Vlasenko95891fc2008-03-27 16:26:35 +000036/* "Standard" openvt's man page (we do not support all of this):
37
38openvt [-c NUM] [-fsulv] [--] [command [args]]
39
40Find the first available VT, and run command on it. Stdio is directed
41to that VT. If no command is specified then $SHELL is used.
42
43-c NUM
44 Use the given VT number, not the first free one.
45-f
46 Force opening a VT: don't try to check if VT is already in use.
47-s
48 Switch to the new VT when starting the command.
49 The VT of the new command will be made the new current VT.
50-u
51 Figure out the owner of the current VT, and run login as that user.
52 Suitable to be called by init. Shouldn't be used with -c or -l.
53-l
54 Make the command a login shell: a "-" is prepended to the argv[0]
55 when command is executed.
56-v
57 Verbose.
58-w
59 Wait for command to complete. If -w and -s are used together,
60 switch back to the controlling terminal when the command completes.
61
62bbox:
63-u: not implemented
64-f: always in effect
65-l: not implemented, ignored
66-v: ignored
67-ws: does NOT switch back
68*/
69
70/* Helper: does this fd understand VT_xxx? */
71static int not_vt_fd(int fd)
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000072{
Denis Vlasenko95891fc2008-03-27 16:26:35 +000073 struct vt_stat vtstat;
74 return ioctl(fd, VT_GETSTATE, &vtstat); /* !0: error, it's not VT fd */
75}
Glenn L McGrath0a3b0102003-05-13 16:31:15 +000076
Denis Vlasenko95891fc2008-03-27 16:26:35 +000077/* Helper: get a fd suitable for VT_xxx */
78static int get_vt_fd(void)
79{
80 int fd;
Denis Vlasenko53091ec2007-03-26 13:35:09 +000081
Denis Vlasenko95891fc2008-03-27 16:26:35 +000082 /* Do we, by chance, already have it? */
83 for (fd = 0; fd < 3; fd++)
84 if (!not_vt_fd(fd))
Denis Vlasenko1f228982008-04-22 00:16:29 +000085 return fd;
Denys Vlasenkoab19ede2009-11-11 21:05:42 +010086 fd = open(DEV_CONSOLE, O_RDONLY | O_NONBLOCK);
Denis Vlasenko95891fc2008-03-27 16:26:35 +000087 if (fd >= 0 && !not_vt_fd(fd))
88 return fd;
James Byrne69374872019-07-02 11:35:03 +020089 bb_simple_error_msg_and_die("can't find open VT");
Denis Vlasenko95891fc2008-03-27 16:26:35 +000090}
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000091
Denis Vlasenko95891fc2008-03-27 16:26:35 +000092static int find_free_vtno(void)
93{
94 int vtno;
95 int fd = get_vt_fd();
96
Denis Vlasenko1f228982008-04-22 00:16:29 +000097 errno = 0;
Denis Vlasenko95891fc2008-03-27 16:26:35 +000098 /*xfunc_error_retval = 3; - do we need compat? */
99 if (ioctl(fd, VT_OPENQRY, &vtno) != 0 || vtno <= 0)
James Byrne69374872019-07-02 11:35:03 +0200100 bb_simple_perror_msg_and_die("can't find open VT");
Denys Vlasenkob182e9a2017-08-04 23:04:17 +0200101// Not really needed, grep for DAEMON_CLOSE_EXTRA_FDS
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000102// if (fd > 2)
103// close(fd);
104 return vtno;
105}
106
107/* vfork scares gcc, it generates bigger code.
108 * Keep it away from main program.
109 * TODO: move to libbb; or adapt existing libbb's spawn().
110 */
111static NOINLINE void vfork_child(char **argv)
112{
113 if (vfork() == 0) {
114 /* CHILD */
115 /* Try to make this VT our controlling tty */
116 setsid(); /* lose old ctty */
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +0000117 ioctl(STDIN_FILENO, TIOCSCTTY, 0 /* 0: don't forcibly steal */);
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000118 //bb_error_msg("our sid %d", getsid(0));
119 //bb_error_msg("our pgrp %d", getpgrp());
120 //bb_error_msg("VT's sid %d", tcgetsid(0));
121 //bb_error_msg("VT's pgrp %d", tcgetpgrp(0));
Pascal Bellard21e8e8d2010-07-04 00:57:03 +0200122 BB_EXECVP_or_die(argv);
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000123 }
124}
125
126int openvt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000127int openvt_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000128{
129 char vtname[sizeof(VC_FORMAT) + sizeof(int)*3];
Denis Vlasenko1b2d0b22008-04-19 03:44:45 +0000130 struct vt_stat vtstat;
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000131 char *str_c;
132 int vtno;
133 int flags;
134 enum {
135 OPT_c = (1 << 0),
136 OPT_w = (1 << 1),
137 OPT_s = (1 << 2),
138 OPT_l = (1 << 3),
139 OPT_f = (1 << 4),
140 OPT_v = (1 << 5),
141 };
142
143 /* "+" - stop on first non-option */
144 flags = getopt32(argv, "+c:wslfv", &str_c);
145 argv += optind;
146
147 if (flags & OPT_c) {
148 /* Check for illegal vt number: < 1 or > 63 */
149 vtno = xatou_range(str_c, 1, 63);
150 } else {
151 vtno = find_free_vtno();
152 }
153
154 /* Grab new VT */
155 sprintf(vtname, VC_FORMAT, vtno);
156 /* (Try to) clean up stray open fds above fd 2 */
Denys Vlasenkob182e9a2017-08-04 23:04:17 +0200157 bb_daemon_helper(DAEMON_CLOSE_EXTRA_FDS);
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +0000158 close(STDIN_FILENO);
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000159 /*setsid(); - BAD IDEA: after we exit, child is SIGHUPed... */
Denis Vlasenko53091ec2007-03-26 13:35:09 +0000160 xopen(vtname, O_RDWR);
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +0000161 xioctl(STDIN_FILENO, VT_GETSTATE, &vtstat);
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000162
163 if (flags & OPT_s) {
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +0000164 console_make_active(STDIN_FILENO, vtno);
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000165 }
166
167 if (!argv[0]) {
168 argv--;
Denys Vlasenko681efe22011-03-08 21:00:36 +0100169 argv[0] = (char *) get_shell_name();
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000170 /*argv[1] = NULL; - already is */
171 }
172
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +0000173 xdup2(STDIN_FILENO, STDOUT_FILENO);
174 xdup2(STDIN_FILENO, STDERR_FILENO);
Glenn L McGrathec0c48c2002-09-16 03:16:06 +0000175
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000176#ifdef BLOAT
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +0000177 {
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000178 /* Handle -l (login shell) option */
179 const char *prog = argv[0];
180 if (flags & OPT_l)
181 argv[0] = xasprintf("-%s", argv[0]);
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +0000182 }
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000183#endif
184
185 vfork_child(argv);
186 if (flags & OPT_w) {
Denis Vlasenko1b2d0b22008-04-19 03:44:45 +0000187 /* We have only one child, wait for it */
188 safe_waitpid(-1, NULL, 0); /* loops on EINTR */
189 if (flags & OPT_s) {
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +0000190 console_make_active(STDIN_FILENO, vtstat.v_active);
Denis Vlasenko1b2d0b22008-04-19 03:44:45 +0000191 // Compat: even with -c N (try to) disallocate:
192 // # /usr/app/kbd-1.12/bin/openvt -f -c 9 -ws sleep 5
193 // openvt: could not deallocate console 9
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +0000194 xioctl(STDIN_FILENO, VT_DISALLOCATE, (void*)(ptrdiff_t)vtno);
Denis Vlasenko1b2d0b22008-04-19 03:44:45 +0000195 }
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000196 }
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000197 return EXIT_SUCCESS;
Glenn L McGrathec0c48c2002-09-16 03:16:06 +0000198}