aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2022-04-01 15:10:16 -0500
committerRob Landley <rob@landley.net>2022-04-01 15:10:16 -0500
commitcfea8f012d6d755026e27297dca5293c4973b2d4 (patch)
tree7a4ffba51eca0699bc933d8d0e3a69fb7604b1cc
parentd040b01c2eab4e13efe20cf916e6bc80e7927a0d (diff)
downloadtoybox-cfea8f012d6d755026e27297dca5293c4973b2d4.tar.gz
Cleanup openvt/deallocvt and merge with chvt, promote out of pending.
-rw-r--r--toys/other/chvt.c33
-rw-r--r--toys/other/openvt.c115
-rw-r--r--toys/pending/openvt.c102
3 files changed, 115 insertions, 135 deletions
diff --git a/toys/other/chvt.c b/toys/other/chvt.c
deleted file mode 100644
index 7d69f9a4..00000000
--- a/toys/other/chvt.c
+++ /dev/null
@@ -1,33 +0,0 @@
-/* chvt.c - switch virtual terminals
- *
- * Copyright (C) 2008 David Anders <danders@amltd.com>
-
-USE_CHVT(NEWTOY(chvt, "<1", TOYFLAG_USR|TOYFLAG_BIN))
-
-config CHVT
- bool "chvt"
- default y
- help
- usage: chvt N
-
- Change to virtual terminal number N. (This only works in text mode.)
-
- Virtual terminals are the Linux VGA text mode displays, ordinarily
- switched between via alt-F1, alt-F2, etc. Use ctrl-alt-F1 to switch
- from X to a virtual terminal, and alt-F6 (or F7, or F8) to get back.
-*/
-
-#include "toys.h"
-#include <linux/vt.h>
-
-void chvt_main(void)
-{
- int vt, fd;
- char *consoles[]={"/dev/console", "/dev/vc/0", "/dev/tty", NULL}, **cc;
-
- vt = atoi(*toys.optargs);
- for (cc = consoles; *cc; cc++) if ((fd = open(*cc, O_RDWR)) != -1) break;
-
- if (fd == -1 || ioctl(fd, VT_ACTIVATE, vt) || ioctl(fd, VT_WAITACTIVE, vt))
- perror_exit(0);
-}
diff --git a/toys/other/openvt.c b/toys/other/openvt.c
new file mode 100644
index 00000000..8210587d
--- /dev/null
+++ b/toys/other/openvt.c
@@ -0,0 +1,115 @@
+/* openvt.c - Run a program on a new VT
+ *
+ * Copyright 2008 David Anders <danders@amltd.com>
+ * Copyright 2014 Vivek Kumar Bhagat <vivek.bhagat89@gmail.com>
+ *
+ * No Standard
+
+USE_OPENVT(NEWTOY(openvt, "^<1c#<1>63sw", TOYFLAG_BIN|TOYFLAG_NEEDROOT))
+USE_CHVT(NEWTOY(chvt, "<1", TOYFLAG_USR|TOYFLAG_BIN))
+USE_DEALLOCVT(NEWTOY(deallocvt, ">1", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_NEEDROOT))
+
+config OPENVT
+ bool "openvt"
+ default y
+ help
+ usage: openvt [-c NUM] [-sw] COMMAND...
+
+ Run COMMAND on a new virtual terminal.
+
+ -c NUM Use VT NUM
+ -s Switch to the new VT
+ -w Wait for command to exit (with -s, deallocates VT on exit)
+
+config CHVT
+ bool "chvt"
+ default y
+ help
+ usage: chvt NUM
+
+ Change to virtual terminal number NUM. (This only works in text mode.)
+
+ Virtual terminals are the Linux VGA text mode (or framebuffer) displays,
+ switched between via alt-F1, alt-F2, etc. Use ctrl-alt-F1 to switch
+ from X11 to a virtual terminal, and alt-F6 (or F7, or F8) to get back.
+
+config DEALLOCVT
+ bool "deallocvt"
+ default y
+ help
+ usage: deallocvt [NUM]
+
+ Deallocate unused virtual terminals, either a specific /dev/ttyNUM, or all.
+*/
+
+#define FOR_openvt
+#include "toys.h"
+#include <linux/vt.h>
+#include <linux/kd.h>
+
+GLOBALS(
+ long c;
+)
+
+static int open_console(void)
+{
+ char arg = 0, *console_name[] = {"/dev/tty", "/dev/tty0", "/dev/console"};
+ int i, fd;
+
+ for (i = 0; i < ARRAY_LEN(console_name); i++) {
+ if (0>(fd = open(console_name[i], O_RDWR))) continue;
+ if (!ioctl(fd, KDGKBTYPE, &arg)) return fd;
+ close(fd);
+ }
+ for (fd = 0; fd < 3; fd++) if (!ioctl(fd, KDGKBTYPE, &arg)) return fd;
+ error_exit("can't open console");
+}
+
+static int activate(int fd, int cc)
+{
+ return ioctl(fd, VT_ACTIVATE, cc) || ioctl(fd, VT_WAITACTIVE, cc);
+}
+
+void openvt_main(void)
+{
+ struct vt_stat vstate;
+ int fd, cc = (int)TT.c;
+ pid_t pid;
+
+ // find current console
+ if (-1 == (ioctl(fd = open_console(), VT_GETSTATE, &vstate)) ||
+ (!cc && 0>=(cc = xioctl(fd, VT_OPENQRY, &fd))))
+ perror_exit("can't find open VT");
+
+ sprintf(toybuf, "/dev/tty%d", cc);
+ if (!(pid = XVFORK())) {
+ close(0); //new vt becomes stdin
+ dup2(dup2(xopen_stdio(toybuf, O_RDWR), 1), 2);
+ if (FLAG(s)) activate(0, cc);
+ setsid();
+ ioctl(0, TIOCSCTTY, 0);
+ if (fd>2) close(fd);
+ xexec(toys.optargs);
+ }
+ if (FLAG(w)) {
+ while (-1 == waitpid(pid, NULL, 0) && errno == EINTR) errno = 0;
+ if (FLAG(s)) {
+ activate(fd, vstate.v_active);
+ dprintf(2, "%d\n", ioctl(fd, VT_DISALLOCATE, cc));
+ }
+ }
+}
+
+void chvt_main(void)
+{
+ if (activate(open_console(), atoi(*toys.optargs)))
+ perror_exit_raw(*toys.optargs);
+}
+
+void deallocvt_main(void)
+{
+ int fd = open_console(), vt_num = 0; // 0 = all
+
+ if (*toys.optargs) vt_num = atolx_range(*toys.optargs, 1, 63);
+ if (-1 == ioctl(fd, VT_DISALLOCATE, vt_num)) perror_exit("%d", vt_num);
+}
diff --git a/toys/pending/openvt.c b/toys/pending/openvt.c
deleted file mode 100644
index 3cc97daa..00000000
--- a/toys/pending/openvt.c
+++ /dev/null
@@ -1,102 +0,0 @@
-/* openvt.c - Run a program on a new VT
- *
- * Copyright 2014 Vivek Kumar Bhagat <vivek.bhagat89@gmail.com>
- *
- * No Standard
-
-USE_OPENVT(NEWTOY(openvt, "c#<1>63sw", TOYFLAG_BIN|TOYFLAG_NEEDROOT))
-USE_DEALLOCVT(NEWTOY(deallocvt, ">1", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_NEEDROOT))
-
-config OPENVT
- bool "openvt"
- default n
- depends on TOYBOX_FORK
- help
- usage: openvt [-c NUM] [-sw] [COMMAND...]
-
- Start a program on a new virtual terminal.
-
- -c NUM Use VT NUM
- -s Switch to new VT
- -w Wait for command to exit
-
- Together -sw switch back to originating VT when command completes.
-
-config DEALLOCVT
- bool "deallocvt"
- default n
- help
- usage: deallocvt [NUM]
-
- Deallocate unused virtual terminals, either a specific /dev/ttyNUM, or all.
-*/
-
-#define FOR_openvt
-#include "toys.h"
-#include <linux/vt.h>
-#include <linux/kd.h>
-
-GLOBALS(
- long c;
-)
-
-int open_console(void)
-{
- char arg = 0, *console_name[] = {"/dev/tty", "/dev/tty0", "/dev/console"};
- int i, fd;
-
- for (i = 0; i < ARRAY_LEN(console_name); i++) {
- if (0>(fd = open(console_name[i], O_RDWR))) continue;
- if (!ioctl(fd, KDGKBTYPE, &arg)) return fd;
- close(fd);
- }
- for (fd = 0; fd < 3; fd++) if (!ioctl(fd, KDGKBTYPE, &arg)) return fd;
- error_exit("can't open console");
-}
-
-void openvt_main(void)
-{
- struct vt_stat vstate;
- int fd;
- pid_t pid;
-
- // find current console
- if (-1 == (ioctl(fd = open_console(), VT_GETSTATE, &vstate)) ||
- (!TT.c && 0>=(TT.c = xioctl(fd, VT_OPENQRY, &fd))))
- perror_exit("can't find open VT");
-
- sprintf(toybuf, "/dev/tty%ld", TT.c);
- close(0); //new vt becomes stdin
- dup2(dup2(xopen_stdio(toybuf, O_RDWR), 1), 2);
- if (FLAG(s)) {
- ioctl(0, VT_ACTIVATE, (int)TT.c);
- ioctl(0, VT_WAITACTIVE, (int)TT.c);
- }
-
- if (!(pid = xfork())) {
- setsid();
- ioctl(0, TIOCSCTTY, 0);
- if (fd>2) close(fd);
- xexec(toys.optargs);
- }
-
- if (FLAG(w)) {
- while (-1 == waitpid(pid, NULL, 0) && errno == EINTR);
- if (FLAG(s)) {
- ioctl(fd, VT_ACTIVATE, vstate.v_active);
- ioctl(fd, VT_WAITACTIVE, vstate.v_active);
- ioctl(fd, VT_DISALLOCATE, (int)TT.c);
- }
- }
- close(fd);
-}
-
-void deallocvt_main(void)
-{
- int fd, vt_num = 0; // 0 = all
-
- if (*toys.optargs) vt_num = atolx_range(*toys.optargs, 1, 63);
- if (-1 == ioctl(fd = open_console(), VT_DISALLOCATE, vt_num))
- perror_exit("%d", vt_num);
- close(fd);
-}