diff options
| author | Alexander Shishkin <virtuoso@slind.org> | 2011-10-31 13:18:44 +0100 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2011-10-31 13:18:44 +0100 |
| commit | 156840c249ddf761d9e60b06ca6d8aaa1bf88402 (patch) | |
| tree | 927112b5656f671980e8db99d11cbb74a6457118 /shell | |
| parent | bcdae638752d5eac9185f69b88f49ed136cf39af (diff) | |
| download | busybox-w32-156840c249ddf761d9e60b06ca6d8aaa1bf88402.tar.gz busybox-w32-156840c249ddf761d9e60b06ca6d8aaa1bf88402.tar.bz2 busybox-w32-156840c249ddf761d9e60b06ca6d8aaa1bf88402.zip | |
cttyhack: print detected ctty name when called without parameters
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 283 327 +44
packed_usage 28911 28915 +4
Signed-off-by: Alexander Shishkin <virtuoso@slind.org>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'shell')
| -rw-r--r-- | shell/cttyhack.c | 76 |
1 files changed, 42 insertions, 34 deletions
diff --git a/shell/cttyhack.c b/shell/cttyhack.c index 6241c76a9..640f5b1fb 100644 --- a/shell/cttyhack.c +++ b/shell/cttyhack.c | |||
| @@ -50,9 +50,12 @@ | |||
| 50 | //config: | 50 | //config: |
| 51 | //config: # exec setsid sh -c 'exec sh </dev/tty1 >/dev/tty1 2>&1' | 51 | //config: # exec setsid sh -c 'exec sh </dev/tty1 >/dev/tty1 2>&1' |
| 52 | //config: | 52 | //config: |
| 53 | //config: Starting getty on a controlling tty from a shell script: | ||
| 54 | //config: | ||
| 55 | //config: # getty 115200 $(cttyhack) | ||
| 53 | 56 | ||
| 54 | //usage:#define cttyhack_trivial_usage | 57 | //usage:#define cttyhack_trivial_usage |
| 55 | //usage: "PROG ARGS" | 58 | //usage: "[PROG ARGS]" |
| 56 | //usage:#define cttyhack_full_usage "\n\n" | 59 | //usage:#define cttyhack_full_usage "\n\n" |
| 57 | //usage: "Give PROG a controlling tty if possible." | 60 | //usage: "Give PROG a controlling tty if possible." |
| 58 | //usage: "\nExample for /etc/inittab (for busybox init):" | 61 | //usage: "\nExample for /etc/inittab (for busybox init):" |
| @@ -108,61 +111,66 @@ int cttyhack_main(int argc UNUSED_PARAM, char **argv) | |||
| 108 | char paranoia[sizeof(struct serial_struct) * 3]; | 111 | char paranoia[sizeof(struct serial_struct) * 3]; |
| 109 | } u; | 112 | } u; |
| 110 | 113 | ||
| 111 | if (!*++argv) { | ||
| 112 | bb_show_usage(); | ||
| 113 | } | ||
| 114 | |||
| 115 | strcpy(console, "/dev/tty"); | 114 | strcpy(console, "/dev/tty"); |
| 116 | fd = open(console, O_RDWR); | 115 | fd = open(console, O_RDWR); |
| 117 | if (fd >= 0) { | 116 | if (fd < 0) { |
| 118 | /* We already have ctty, nothing to do */ | ||
| 119 | close(fd); | ||
| 120 | } else { | ||
| 121 | /* We don't have ctty (or don't have "/dev/tty" node...) */ | 117 | /* We don't have ctty (or don't have "/dev/tty" node...) */ |
| 122 | do { | 118 | do { |
| 123 | #ifdef __linux__ | 119 | #ifdef __linux__ |
| 124 | int s = open_read_close("/sys/class/tty/console/active", | 120 | int s; |
| 125 | console + 5, sizeof(console) - 5); | ||
| 126 | if (s > 0) { | ||
| 127 | /* found active console via sysfs (Linux 2.6.38+) | ||
| 128 | * sysfs string looks like "ttyS0\n" so zap the newline: | ||
| 129 | */ | ||
| 130 | console[4 + s] = '\0'; | ||
| 131 | break; | ||
| 132 | } | ||
| 133 | |||
| 134 | if (ioctl(0, VT_GETSTATE, &u.vt) == 0) { | 121 | if (ioctl(0, VT_GETSTATE, &u.vt) == 0) { |
| 135 | /* this is linux virtual tty */ | 122 | /* this is linux virtual tty */ |
| 136 | sprintf(console + 8, "S%d" + 1, u.vt.v_active); | 123 | sprintf(console + 8, "S%u" + 1, (int)u.vt.v_active); |
| 137 | break; | 124 | break; |
| 138 | } | 125 | } |
| 139 | #endif | 126 | #endif |
| 140 | #ifdef TIOCGSERIAL | 127 | #ifdef TIOCGSERIAL |
| 141 | if (ioctl(0, TIOCGSERIAL, &u.sr) == 0) { | 128 | if (ioctl(0, TIOCGSERIAL, &u.sr) == 0) { |
| 142 | /* this is a serial console; assuming it is named /dev/ttySn */ | 129 | /* this is a serial console; assuming it is named /dev/ttySn */ |
| 143 | sprintf(console + 8, "S%d", u.sr.line); | 130 | sprintf(console + 8, "S%u", (int)u.sr.line); |
| 131 | break; | ||
| 132 | } | ||
| 133 | #endif | ||
| 134 | #ifdef __linux__ | ||
| 135 | /* Note that this method is not related to _stdin_ */ | ||
| 136 | s = open_read_close("/sys/class/tty/console/active", | ||
| 137 | console + 5, sizeof(console) - 5); | ||
| 138 | if (s > 0) { | ||
| 139 | /* found active console via sysfs (Linux 2.6.38+) | ||
| 140 | * sysfs string looks like "ttyS0\n" so zap the newline: | ||
| 141 | */ | ||
| 142 | console[4 + s] = '\0'; | ||
| 144 | break; | 143 | break; |
| 145 | } | 144 | } |
| 146 | #endif | 145 | #endif |
| 147 | /* nope, could not find it */ | 146 | /* nope, could not find it */ |
| 148 | goto ret; | 147 | console[0] = '\0'; |
| 149 | } while (0); | 148 | } while (0); |
| 149 | } | ||
| 150 | 150 | ||
| 151 | argv++; | ||
| 152 | if (!argv[0]) { | ||
| 153 | if (!console[0]) | ||
| 154 | return EXIT_FAILURE; | ||
| 155 | puts(console); | ||
| 156 | return EXIT_SUCCESS; | ||
| 157 | } | ||
| 158 | |||
| 159 | if (fd < 0) { | ||
| 151 | fd = open_or_warn(console, O_RDWR); | 160 | fd = open_or_warn(console, O_RDWR); |
| 152 | if (fd < 0) | 161 | if (fd < 0) |
| 153 | goto ret; | 162 | goto ret; |
| 154 | //bb_error_msg("switching to '%s'", console); | ||
| 155 | dup2(fd, 0); | ||
| 156 | dup2(fd, 1); | ||
| 157 | dup2(fd, 2); | ||
| 158 | while (fd > 2) | ||
| 159 | close(fd--); | ||
| 160 | /* Some other session may have it as ctty, | ||
| 161 | * steal it from them: | ||
| 162 | */ | ||
| 163 | ioctl(0, TIOCSCTTY, 1); | ||
| 164 | } | 163 | } |
| 165 | 164 | //bb_error_msg("switching to '%s'", console); | |
| 166 | ret: | 165 | dup2(fd, 0); |
| 166 | dup2(fd, 1); | ||
| 167 | dup2(fd, 2); | ||
| 168 | while (fd > 2) | ||
| 169 | close(fd--); | ||
| 170 | /* Some other session may have it as ctty, | ||
| 171 | * try to steal it from them: | ||
| 172 | */ | ||
| 173 | ioctl(0, TIOCSCTTY, 1); | ||
| 174 | ret: | ||
| 167 | BB_EXECVP_or_die(argv); | 175 | BB_EXECVP_or_die(argv); |
| 168 | } | 176 | } |
