shell: split read builtin from ash
[oweals/busybox.git] / console-tools / openvt.c
index 6bdf608c7f4197c4877b64869548eb8a775a44ce..7bd6072a4675f4d584e8ca051e218ecfb1361f65 100644 (file)
  *  busyboxed by Quy Tonthat <quy@signal3.com>
  *  hacked by Tito <farmatito@tiscali.it>
  *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
-/* getopt not needed */
+#include <linux/vt.h>
+#include "libbb.h"
+
+/* "Standard" openvt's man page (we do not support all of this):
+
+openvt [-c NUM] [-fsulv] [--] [command [args]]
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <string.h>
-#include <sys/types.h>
-#include <ctype.h>
+Find the first available VT, and run command on it. Stdio is directed
+to that VT. If no command is specified then $SHELL is used.
 
-#include "busybox.h"
+-c NUM
+    Use the given VT number, not the first free one.
+-f
+    Force opening a VT: don't try to check if VT is already in use.
+-s
+    Switch to the new VT when starting the command.
+    The VT of the new command will be made the new current VT.
+-u
+    Figure out the owner of the current VT, and run login as that user.
+    Suitable to be called by init. Shouldn't be used with -c or -l.
+-l
+    Make the command a login shell: a "-" is prepended to the argv[0]
+    when command is executed.
+-v
+    Verbose.
+-w
+    Wait for command to complete. If -w and -s are used together,
+    switch back to the controlling terminal when the command completes.
+
+bbox:
+-u: not implemented
+-f: always in effect
+-l: not implemented, ignored
+-v: ignored
+-ws: does NOT switch back
+*/
+
+/* Helper: does this fd understand VT_xxx? */
+static int not_vt_fd(int fd)
+{
+       struct vt_stat vtstat;
+       return ioctl(fd, VT_GETSTATE, &vtstat); /* !0: error, it's not VT fd */
+}
 
-int openvt_main(int argc, char **argv)
+/* Helper: get a fd suitable for VT_xxx */
+static int get_vt_fd(void)
 {
        int fd;
-       char vtname[sizeof VC_FORMAT + 2];
 
+       /* Do we, by chance, already have it? */
+       for (fd = 0; fd < 3; fd++)
+               if (!not_vt_fd(fd))
+                       return fd;
+       fd = open(DEV_CONSOLE, O_RDONLY | O_NONBLOCK);
+       if (fd >= 0 && !not_vt_fd(fd))
+               return fd;
+       bb_error_msg_and_die("can't find open VT");
+}
 
-       if (argc < 3)
-       bb_show_usage();
+static int find_free_vtno(void)
+{
+       int vtno;
+       int fd = get_vt_fd();
 
-       /* check for Illegal vt number: < 1 or > 12 */
-       sprintf(vtname, VC_FORMAT,(int)bb_xgetlarg(argv[1], 10, 1, 12));
+       errno = 0;
+       /*xfunc_error_retval = 3; - do we need compat? */
+       if (ioctl(fd, VT_OPENQRY, &vtno) != 0 || vtno <= 0)
+               bb_perror_msg_and_die("can't find open VT");
+// Not really needed, grep for DAEMON_ONLY_SANITIZE
+//     if (fd > 2)
+//             close(fd);
+       return vtno;
+}
 
-       argv+=2;
-       argc-=2;
+/* vfork scares gcc, it generates bigger code.
+ * Keep it away from main program.
+ * TODO: move to libbb; or adapt existing libbb's spawn().
+ */
+static NOINLINE void vfork_child(char **argv)
+{
+       if (vfork() == 0) {
+               /* CHILD */
+               /* Try to make this VT our controlling tty */
+               setsid(); /* lose old ctty */
+               ioctl(STDIN_FILENO, TIOCSCTTY, 0 /* 0: don't forcibly steal */);
+               //bb_error_msg("our sid %d", getsid(0));
+               //bb_error_msg("our pgrp %d", getpgrp());
+               //bb_error_msg("VT's sid %d", tcgetsid(0));
+               //bb_error_msg("VT's pgrp %d", tcgetpgrp(0));
+               BB_EXECVP(argv[0], argv);
+               bb_perror_msg_and_die("exec %s", argv[0]);
+       }
+}
 
-       if(fork() == 0) {
-               /* leave current vt */
+int openvt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int openvt_main(int argc UNUSED_PARAM, char **argv)
+{
+       char vtname[sizeof(VC_FORMAT) + sizeof(int)*3];
+       struct vt_stat vtstat;
+       char *str_c;
+       int vtno;
+       int flags;
+       enum {
+               OPT_c = (1 << 0),
+               OPT_w = (1 << 1),
+               OPT_s = (1 << 2),
+               OPT_l = (1 << 3),
+               OPT_f = (1 << 4),
+               OPT_v = (1 << 5),
+       };
 
-#ifdef   ESIX_5_3_2_D
-               if (setpgrp() < 0) {
-#else
-               if (setsid() < 0) {
-#endif
+       /* "+" - stop on first non-option */
+       flags = getopt32(argv, "+c:wslfv", &str_c);
+       argv += optind;
 
-                       bb_perror_msg_and_die("Unable to set new session");
-               }
-               close(0);                       /* so that new vt becomes stdin */
+       if (flags & OPT_c) {
+               /* Check for illegal vt number: < 1 or > 63 */
+               vtno = xatou_range(str_c, 1, 63);
+       } else {
+               vtno = find_free_vtno();
+       }
 
-               /* and grab new one */
-               fd = bb_xopen(vtname, O_RDWR);
+       /* Grab new VT */
+       sprintf(vtname, VC_FORMAT, vtno);
+       /* (Try to) clean up stray open fds above fd 2 */
+       bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS | DAEMON_ONLY_SANITIZE, NULL);
+       close(STDIN_FILENO);
+       /*setsid(); - BAD IDEA: after we exit, child is SIGHUPed... */
+       xopen(vtname, O_RDWR);
+       xioctl(STDIN_FILENO, VT_GETSTATE, &vtstat);
+
+       if (flags & OPT_s) {
+               console_make_active(STDIN_FILENO, vtno);
+       }
+
+       if (!argv[0]) {
+               argv--;
+               argv[0] = getenv("SHELL");
+               if (!argv[0])
+                       argv[0] = (char *) DEFAULT_SHELL;
+               /*argv[1] = NULL; - already is */
+       }
 
-               /* Reassign stdout and sterr */
-               dup2(fd, STDOUT_FILENO);
-               dup2(fd, STDERR_FILENO);
+       xdup2(STDIN_FILENO, STDOUT_FILENO);
+       xdup2(STDIN_FILENO, STDERR_FILENO);
 
-               execvp(argv[0], argv);
-               _exit(1);
+#ifdef BLOAT
+       {
+       /* Handle -l (login shell) option */
+       const char *prog = argv[0];
+       if (flags & OPT_l)
+               argv[0] = xasprintf("-%s", argv[0]);
+       }
+#endif
+
+       vfork_child(argv);
+       if (flags & OPT_w) {
+               /* We have only one child, wait for it */
+               safe_waitpid(-1, NULL, 0); /* loops on EINTR */
+               if (flags & OPT_s) {
+                       console_make_active(STDIN_FILENO, vtstat.v_active);
+                       // Compat: even with -c N (try to) disallocate:
+                       // # /usr/app/kbd-1.12/bin/openvt -f -c 9 -ws sleep 5
+                       // openvt: could not deallocate console 9
+                       xioctl(STDIN_FILENO, VT_DISALLOCATE, (void*)(ptrdiff_t)vtno);
+               }
        }
        return EXIT_SUCCESS;
 }
-
-/*
-Local Variables:
-c-file-style: "linux"
-c-basic-offset: 4
-tab-width: 4
-End:
-*/