blob: e3ea71bc56211f5f90daff532e79086a96228b4b [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 *
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrathec0c48c2002-09-16 03:16:06 +00009 */
10
Denis Vlasenko95891fc2008-03-27 16:26:35 +000011#include <linux/vt.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000012#include "libbb.h"
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000013
Denis Vlasenko95891fc2008-03-27 16:26:35 +000014/* "Standard" openvt's man page (we do not support all of this):
15
16openvt [-c NUM] [-fsulv] [--] [command [args]]
17
18Find the first available VT, and run command on it. Stdio is directed
19to 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
40bbox:
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? */
49static int not_vt_fd(int fd)
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000050{
Denis Vlasenko95891fc2008-03-27 16:26:35 +000051 struct vt_stat vtstat;
52 return ioctl(fd, VT_GETSTATE, &vtstat); /* !0: error, it's not VT fd */
53}
Glenn L McGrath0a3b0102003-05-13 16:31:15 +000054
Denis Vlasenko95891fc2008-03-27 16:26:35 +000055/* Helper: get a fd suitable for VT_xxx */
56static int get_vt_fd(void)
57{
58 int fd;
Denis Vlasenko53091ec2007-03-26 13:35:09 +000059
Denis Vlasenko95891fc2008-03-27 16:26:35 +000060 /* Do we, by chance, already have it? */
61 for (fd = 0; fd < 3; fd++)
62 if (!not_vt_fd(fd))
Denis Vlasenko1f228982008-04-22 00:16:29 +000063 return fd;
Denys Vlasenkoab19ede2009-11-11 21:05:42 +010064 fd = open(DEV_CONSOLE, O_RDONLY | O_NONBLOCK);
Denis Vlasenko95891fc2008-03-27 16:26:35 +000065 if (fd >= 0 && !not_vt_fd(fd))
66 return fd;
67 bb_error_msg_and_die("can't find open VT");
68}
Glenn L McGrathec0c48c2002-09-16 03:16:06 +000069
Denis Vlasenko95891fc2008-03-27 16:26:35 +000070static int find_free_vtno(void)
71{
72 int vtno;
73 int fd = get_vt_fd();
74
Denis Vlasenko1f228982008-04-22 00:16:29 +000075 errno = 0;
Denis Vlasenko95891fc2008-03-27 16:26:35 +000076 /*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 */
89static 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-Fischerae4342c2008-05-19 08:18:50 +000095 ioctl(STDIN_FILENO, TIOCSCTTY, 0 /* 0: don't forcibly steal */);
Denis Vlasenko95891fc2008-03-27 16:26:35 +000096 //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));
Pascal Bellard21e8e8d2010-07-04 00:57:03 +0200100 BB_EXECVP_or_die(argv);
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000101 }
102}
103
104int openvt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000105int openvt_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000106{
107 char vtname[sizeof(VC_FORMAT) + sizeof(int)*3];
Denis Vlasenko1b2d0b22008-04-19 03:44:45 +0000108 struct vt_stat vtstat;
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000109 char *str_c;
110 int vtno;
111 int flags;
112 enum {
113 OPT_c = (1 << 0),
114 OPT_w = (1 << 1),
115 OPT_s = (1 << 2),
116 OPT_l = (1 << 3),
117 OPT_f = (1 << 4),
118 OPT_v = (1 << 5),
119 };
120
121 /* "+" - stop on first non-option */
122 flags = getopt32(argv, "+c:wslfv", &str_c);
123 argv += optind;
124
125 if (flags & OPT_c) {
126 /* Check for illegal vt number: < 1 or > 63 */
127 vtno = xatou_range(str_c, 1, 63);
128 } else {
129 vtno = find_free_vtno();
130 }
131
132 /* Grab new VT */
133 sprintf(vtname, VC_FORMAT, vtno);
134 /* (Try to) clean up stray open fds above fd 2 */
135 bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS | DAEMON_ONLY_SANITIZE, NULL);
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +0000136 close(STDIN_FILENO);
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000137 /*setsid(); - BAD IDEA: after we exit, child is SIGHUPed... */
Denis Vlasenko53091ec2007-03-26 13:35:09 +0000138 xopen(vtname, O_RDWR);
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +0000139 xioctl(STDIN_FILENO, VT_GETSTATE, &vtstat);
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000140
141 if (flags & OPT_s) {
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +0000142 console_make_active(STDIN_FILENO, vtno);
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000143 }
144
145 if (!argv[0]) {
146 argv--;
147 argv[0] = getenv("SHELL");
148 if (!argv[0])
149 argv[0] = (char *) DEFAULT_SHELL;
150 /*argv[1] = NULL; - already is */
151 }
152
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +0000153 xdup2(STDIN_FILENO, STDOUT_FILENO);
154 xdup2(STDIN_FILENO, STDERR_FILENO);
Glenn L McGrathec0c48c2002-09-16 03:16:06 +0000155
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000156#ifdef BLOAT
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +0000157 {
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000158 /* Handle -l (login shell) option */
159 const char *prog = argv[0];
160 if (flags & OPT_l)
161 argv[0] = xasprintf("-%s", argv[0]);
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +0000162 }
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000163#endif
164
165 vfork_child(argv);
166 if (flags & OPT_w) {
Denis Vlasenko1b2d0b22008-04-19 03:44:45 +0000167 /* We have only one child, wait for it */
168 safe_waitpid(-1, NULL, 0); /* loops on EINTR */
169 if (flags & OPT_s) {
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +0000170 console_make_active(STDIN_FILENO, vtstat.v_active);
Denis Vlasenko1b2d0b22008-04-19 03:44:45 +0000171 // Compat: even with -c N (try to) disallocate:
172 // # /usr/app/kbd-1.12/bin/openvt -f -c 9 -ws sleep 5
173 // openvt: could not deallocate console 9
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +0000174 xioctl(STDIN_FILENO, VT_DISALLOCATE, (void*)(ptrdiff_t)vtno);
Denis Vlasenko1b2d0b22008-04-19 03:44:45 +0000175 }
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000176 }
Denis Vlasenko95891fc2008-03-27 16:26:35 +0000177 return EXIT_SUCCESS;
Glenn L McGrathec0c48c2002-09-16 03:16:06 +0000178}