diff options
author | Isaac Dunham <ibid.ag@gmail.com> | 2014-04-03 22:43:28 -0500 |
---|---|---|
committer | Isaac Dunham <ibid.ag@gmail.com> | 2014-04-03 22:43:28 -0500 |
commit | 6f9d481062e171fb0bd1aa5cdcf043c0e4f9d733 (patch) | |
tree | 2b50fcc1648c7b92e0e3659f0793642230f9d411 | |
parent | 547c8d980bb5ae37f8002aead4e155cc5554dfca (diff) | |
download | toybox-6f9d481062e171fb0bd1aa5cdcf043c0e4f9d733.tar.gz |
openvt tries opening several devices to get an fd that points to the current console, without a need for read or write permissions. O_RDWR implies that both O_RDONLY and O_WRONLY would work, so skip it. Reindent.
-rw-r--r-- | toys/pending/deallocvt.c | 24 | ||||
-rw-r--r-- | toys/pending/openvt.c | 177 |
2 files changed, 99 insertions, 102 deletions
diff --git a/toys/pending/deallocvt.c b/toys/pending/deallocvt.c index 8ac67016..a8067d59 100644 --- a/toys/pending/deallocvt.c +++ b/toys/pending/deallocvt.c @@ -8,13 +8,13 @@ USE_DEALLOCVT(NEWTOY(deallocvt, ">1", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_NEEDROOT)) config DEALLOCVT bool "deallocvt" - depends on OPENVT + depends on OPENVT default n help - usage: deallocvt [N] + usage: deallocvt [N] - Deallocate unused virtual terminal /dev/ttyN - default value of N is 0, deallocate all unused consoles + Deallocate unused virtual terminal /dev/ttyN + default value of N is 0, deallocate all unused consoles */ #include "toys.h" @@ -22,16 +22,16 @@ config DEALLOCVT void deallocvt_main(void) { - int fd; + int fd; - // 0 : deallocate all unused consoles - int vt_num = 0; + // 0 : deallocate all unused consoles + int vt_num = 0; - if (toys.optargs[0]) - vt_num = atolx_range(toys.optargs[0], 1, 63); + if (toys.optargs[0]) + vt_num = atolx_range(toys.optargs[0], 1, 63); - fd = find_console_fd(); - if (fd < 0) error_exit("can't open console"); + fd = find_console_fd(); + if (fd < 0) error_exit("can't open console"); - xioctl(fd, VT_DISALLOCATE, (void *)(ptrdiff_t)vt_num); + xioctl(fd, VT_DISALLOCATE, (void *)(ptrdiff_t)vt_num); } diff --git a/toys/pending/openvt.c b/toys/pending/openvt.c index 5aecd945..f136b1b7 100644 --- a/toys/pending/openvt.c +++ b/toys/pending/openvt.c @@ -27,108 +27,105 @@ config OPENVT #include <linux/kd.h> GLOBALS( - unsigned long vt_num; + unsigned long vt_num; ) int find_console_fd(void) { - char *console_name[] = {"/dev/tty", "/dev/tty0", "/dev/console"}; - int i; - int fd; - char arg; - - for (i = 0; i < 3; i++) { - fd = open(console_name[i], O_RDWR); - if (fd < 0 && errno == EACCES) - fd = open(console_name[i], O_RDONLY); - - if (fd < 0 && errno == EACCES) - fd = open(console_name[i], O_WRONLY); - - if (fd >= 0) { - arg = 0; - if (0 == ioctl(fd, KDGKBTYPE, &arg)) - return fd; - else - close(fd); - } - } - - /* check std fd 0, 1 and 2 */ - for (fd = 0; fd < 3; fd++) { - arg = 0; - if (0 == ioctl(fd, KDGKBTYPE, &arg)) - return fd; - } - - return -1; + char *console_name[] = {"/dev/tty", "/dev/tty0", "/dev/console"}; + int i; + int fd; + char arg; + + for (i = 0; i < 3; i++) { + fd = open(console_name[i], O_RDONLY); + if (fd < 0 && errno == EACCES) + fd = open(console_name[i], O_WRONLY); + + if (fd >= 0) { + arg = 0; + if (0 == ioctl(fd, KDGKBTYPE, &arg)) + return fd; + else + close(fd); + } + } + + /* check std fd 0, 1 and 2 */ + for (fd = 0; fd < 3; fd++) { + arg = 0; + if (0 == ioctl(fd, KDGKBTYPE, &arg)) + return fd; + } + + return -1; } int xvtnum(int fd) { - int ret; + int ret; - ret = ioctl(fd, VT_OPENQRY, (int *)&TT.vt_num); - if (ret != 0 || TT.vt_num <= 0) perror_exit("can't find open VT"); + ret = ioctl(fd, VT_OPENQRY, (int *)&TT.vt_num); + if (ret != 0 || TT.vt_num <= 0) perror_exit("can't find open VT"); - return TT.vt_num; + return TT.vt_num; } void openvt_main(void) { - int fd = -1, vt_fd = -1, pid, ret = 0; - struct vt_stat vstate; - - if (!(toys.optflags & FLAG_c)) { - // check if fd 0,1 or 2 is already opened - for (fd = 0; fd < 3; fd++) - if (!ioctl(fd, VT_GETSTATE, &vstate)) { - ret = xvtnum(fd); - break; - } - - // find VT number using /dev/console - if (!ret) { - fd = xopen("/dev/console", O_RDONLY | O_NONBLOCK); - xioctl(fd, VT_GETSTATE, &vstate); - xvtnum(fd); - } - } - - sprintf(toybuf, "/dev/tty%lu", TT.vt_num); - fd = find_console_fd(); - xioctl(fd, VT_GETSTATE, &vstate); - - close(0); //new vt becomes stdin - vt_fd = xopen(toybuf, O_RDWR); - if (toys.optflags & FLAG_s) { - ioctl(vt_fd, VT_ACTIVATE, TT.vt_num); - ioctl(vt_fd, VT_WAITACTIVE, TT.vt_num); - } - - close(1); - close(2); - dup2(vt_fd, 1); - dup2(vt_fd, 2); - while (vt_fd > 2) - close(vt_fd--); - - pid = vfork(); - if (pid < 0) perror_exit("Fork failed"); - else if (!pid) { - setsid(); - ioctl(vt_fd, TIOCSCTTY, 0); - xexec(toys.optargs); - } - - if (toys.optflags & FLAG_w) { - while (-1 == waitpid(pid, NULL, 0) && errno == EINTR) - ; - if (toys.optflags & FLAG_s) { - ioctl(fd, VT_ACTIVATE, vstate.v_active); - ioctl(fd, VT_WAITACTIVE, vstate.v_active); - //check why deallocate isn't working here - xioctl(fd, VT_DISALLOCATE, (void *)(ptrdiff_t)TT.vt_num); - } - } + int fd = -1, vt_fd = -1, pid, ret = 0; + struct vt_stat vstate; + + if (!(toys.optflags & FLAG_c)) { + // check if fd 0,1 or 2 is already opened + for (fd = 0; fd < 3; fd++) + if (!ioctl(fd, VT_GETSTATE, &vstate)) { + ret = xvtnum(fd); + break; + } + + // find VT number using /dev/console + if (!ret) { + fd = xopen("/dev/console", O_RDONLY | O_NONBLOCK); + xioctl(fd, VT_GETSTATE, &vstate); + xvtnum(fd); + } + } + + sprintf(toybuf, "/dev/tty%lu", TT.vt_num); + fd = find_console_fd(); + xioctl(fd, VT_GETSTATE, &vstate); + + close(0); //new vt becomes stdin + vt_fd = xopen(toybuf, O_RDWR); + if (toys.optflags & FLAG_s) { + ioctl(vt_fd, VT_ACTIVATE, TT.vt_num); + ioctl(vt_fd, VT_WAITACTIVE, TT.vt_num); + } + + close(1); + close(2); + dup2(vt_fd, 1); + dup2(vt_fd, 2); + while (vt_fd > 2) + close(vt_fd--); + + pid = vfork(); + if (pid < 0) perror_exit("Fork failed"); + else if (!pid) { + setsid(); + ioctl(vt_fd, TIOCSCTTY, 0); + xexec(toys.optargs); + } + + if (toys.optflags & FLAG_w) { + while (-1 == waitpid(pid, NULL, 0) && errno == EINTR) + ; + if (toys.optflags & FLAG_s) { + ioctl(fd, VT_ACTIVATE, vstate.v_active); + ioctl(fd, VT_WAITACTIVE, vstate.v_active); + //check why deallocate isn't working here + xioctl(fd, VT_DISALLOCATE, (void *)(ptrdiff_t)TT.vt_num); + } + } } |