Sometimes there's a need to figure out the controlling tty from a shell script, for example, to obtain a line for getty. In this case it's easier to call cttyhack than trying to repeat some of the cttyhack's logic.
function old new delta cttyhack_main 324 320 -4 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-4) Total: -4 bytes Signed-off-by: Alexander Shishkin <[email protected]> --- shell/cttyhack.c | 40 +++++++++++++++++++++------------------- 1 files changed, 21 insertions(+), 19 deletions(-) diff --git a/shell/cttyhack.c b/shell/cttyhack.c index 6241c76..772f8b8 100644 --- a/shell/cttyhack.c +++ b/shell/cttyhack.c @@ -50,6 +50,9 @@ //config: //config: # exec setsid sh -c 'exec sh </dev/tty1 >/dev/tty1 2>&1' //config: +//config: Starting getty on a controlling tty from a shell script: +//config: +//config: # getty $(cttyhack) 115200 //usage:#define cttyhack_trivial_usage //usage: "PROG ARGS" @@ -108,14 +111,10 @@ int cttyhack_main(int argc UNUSED_PARAM, char **argv) char paranoia[sizeof(struct serial_struct) * 3]; } u; - if (!*++argv) { - bb_show_usage(); - } - strcpy(console, "/dev/tty"); fd = open(console, O_RDWR); if (fd >= 0) { - /* We already have ctty, nothing to do */ + /* We already have ctty, don't try to detect it. */ close(fd); } else { /* We don't have ctty (or don't have "/dev/tty" node...) */ @@ -145,24 +144,27 @@ int cttyhack_main(int argc UNUSED_PARAM, char **argv) } #endif /* nope, could not find it */ - goto ret; + console[0] = '\0'; } while (0); + } - fd = open_or_warn(console, O_RDWR); - if (fd < 0) - goto ret; - //bb_error_msg("switching to '%s'", console); - dup2(fd, 0); - dup2(fd, 1); - dup2(fd, 2); - while (fd > 2) - close(fd--); - /* Some other session may have it as ctty, - * steal it from them: - */ - ioctl(0, TIOCSCTTY, 1); + if (!*++argv) { + puts(console); + return EXIT_SUCCESS; } + /* If it's a /dev/tty, still make sure it's wired to std{in,out,err} */ + fd = open_or_warn(console, O_RDWR); + if (fd < 0) + goto ret; + //bb_error_msg("switching to '%s'", console); + dup2(fd, STDIN_FILENO); + dup2(fd, STDOUT_FILENO); + xmove_fd(fd, STDERR_FILENO); + /* Some other session may have it as ctty, + * steal it from them: + */ + ioctl(0, TIOCSCTTY, 1); ret: BB_EXECVP_or_die(argv); } -- 1.7.2.1.45.gb66c2 _______________________________________________ busybox mailing list [email protected] http://lists.busybox.net/mailman/listinfo/busybox
