Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * A text-mode VNC like program for Linux virtual terminals. |
| 4 | * |
| 5 | * pascal.bellard@ads-lu.com |
| 6 | * |
| 7 | * Based on Russell Stuart's conspy.c |
| 8 | * http://ace-host.stuart.id.au/russell/files/conspy.c |
| 9 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 10 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 11 | */ |
| 12 | |
Denys Vlasenko | b9f2d9f | 2011-01-18 13:58:01 +0100 | [diff] [blame^] | 13 | //applet:IF_CONSPY(APPLET(conspy, BB_DIR_BIN, BB_SUID_DROP)) |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 14 | |
| 15 | //kbuild:lib-$(CONFIG_CONSPY) += conspy.o |
| 16 | |
| 17 | //config:config CONSPY |
| 18 | //config: bool "conspy" |
| 19 | //config: default n |
Jeremie Koenig | 1d7266d | 2010-07-19 00:44:56 +0200 | [diff] [blame] | 20 | //config: depends on PLATFORM_LINUX |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 21 | //config: help |
| 22 | //config: A text-mode VNC like program for Linux virtual terminals. |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 23 | //config: example: conspy NUM shared access to console num |
| 24 | //config: or conspy -nd NUM screenshot of console num |
| 25 | //config: or conspy -cs NUM poor man's GNU screen like |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 26 | |
| 27 | //usage:#define conspy_trivial_usage |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 28 | //usage: "[-vcsndf] [-x COL] [-y LINE] [CONSOLE_NO]" |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 29 | //usage:#define conspy_full_usage "\n\n" |
| 30 | //usage: "A text-mode VNC like program for Linux virtual consoles." |
| 31 | //usage: "\nTo exit, quickly press ESC 3 times." |
| 32 | //usage: "\n" |
| 33 | //usage: "\nOptions:" |
| 34 | //usage: "\n -v Don't send keystrokes to the console" |
| 35 | //usage: "\n -c Create missing devices in /dev" |
| 36 | //usage: "\n -s Open a SHELL session" |
| 37 | //usage: "\n -n Black & white" |
| 38 | //usage: "\n -d Dump console to stdout" |
| 39 | //usage: "\n -f Follow cursor" |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 40 | //usage: "\n -x COL Starting column" |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 41 | //usage: "\n -y LINE Starting line" |
| 42 | |
| 43 | #include "libbb.h" |
| 44 | #include <sys/kd.h> |
| 45 | |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 46 | |
| 47 | #define ESC "\033" |
| 48 | |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 49 | struct screen_info { |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 50 | unsigned char lines, cols, cursor_x, cursor_y; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 51 | }; |
| 52 | |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 53 | #define CHAR(x) (*(uint8_t*)(x)) |
| 54 | #define ATTR(x) (((uint8_t*)(x))[1]) |
| 55 | #define NEXT(x) ((x) += 2) |
Denys Vlasenko | 51fa147 | 2010-06-25 00:57:57 +0200 | [diff] [blame] | 56 | #define DATA(x) (*(uint16_t*)(x)) |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 57 | |
| 58 | struct globals { |
| 59 | char* data; |
| 60 | int size; |
| 61 | int x, y; |
| 62 | int kbd_fd; |
Pascal Bellard | 1b8e2b0 | 2010-06-23 20:25:00 +0200 | [diff] [blame] | 63 | int ioerror_count; |
| 64 | int key_count; |
| 65 | int escape_count; |
| 66 | int nokeys; |
| 67 | int current; |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 68 | int first_line_offset; |
| 69 | int last_attr; |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 70 | // cached local tty parameters |
| 71 | unsigned width; |
| 72 | unsigned height; |
| 73 | unsigned col; |
| 74 | unsigned line; |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 75 | smallint curoff; // unknown:0 cursor on:-1 cursor off:1 |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 76 | char attrbuf[sizeof(ESC"[0;1;5;30;40m")]; |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 77 | // remote console |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 78 | struct screen_info remote; |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 79 | // saved local tty terminfo |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 80 | struct termios term_orig; |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 81 | char vcsa_name[sizeof("/dev/vcsaNN")]; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | #define G (*ptr_to_globals) |
| 85 | #define INIT_G() do { \ |
| 86 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 87 | G.attrbuf[0] = '\033'; \ |
| 88 | G.attrbuf[1] = '['; \ |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 89 | G.width = G.height = UINT_MAX; \ |
| 90 | G.last_attr--; \ |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 91 | } while (0) |
| 92 | |
Denys Vlasenko | 05a550b | 2010-06-21 04:00:16 +0200 | [diff] [blame] | 93 | enum { |
| 94 | FLAG_v, // view only |
| 95 | FLAG_c, // create device if need |
| 96 | FLAG_s, // session |
| 97 | FLAG_n, // no colors |
| 98 | FLAG_d, // dump screen |
| 99 | FLAG_f, // follow cursor |
| 100 | }; |
| 101 | #define FLAG(x) (1 << FLAG_##x) |
| 102 | #define BW (option_mask32 & FLAG(n)) |
| 103 | |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 104 | static void clrscr(void) |
| 105 | { |
| 106 | // Home, clear till end of screen |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 107 | fputs(ESC"[1;1H" ESC"[J", stdout); |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 108 | G.col = G.line = 0; |
| 109 | } |
| 110 | |
| 111 | static void set_cursor(int state) |
| 112 | { |
| 113 | if (G.curoff != state) { |
| 114 | G.curoff = state; |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 115 | fputs(ESC"[?25", stdout); |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 116 | bb_putchar("h?l"[1 + state]); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | static void gotoxy(int col, int line) |
| 121 | { |
| 122 | if (G.col != col || G.line != line) { |
| 123 | G.col = col; |
| 124 | G.line = line; |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 125 | printf(ESC"[%u;%uH", line + 1, col + 1); |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | static void cleanup(int code) |
| 130 | { |
| 131 | set_cursor(-1); // cursor on |
| 132 | tcsetattr(G.kbd_fd, TCSANOW, &G.term_orig); |
| 133 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 134 | close(G.kbd_fd); |
| 135 | } |
| 136 | // Reset attributes |
| 137 | if (!BW) |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 138 | fputs(ESC"[0m", stdout); |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 139 | bb_putchar('\n'); |
| 140 | if (code > 1) |
| 141 | kill_myself_with_sig(code); |
| 142 | exit(code); |
| 143 | } |
| 144 | |
Pascal Bellard | 1b8e2b0 | 2010-06-23 20:25:00 +0200 | [diff] [blame] | 145 | static void screen_read_close(void) |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 146 | { |
| 147 | unsigned i, j; |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 148 | int vcsa_fd; |
| 149 | char *data; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 150 | |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 151 | // Close & re-open vcsa in case they have swapped virtual consoles |
| 152 | vcsa_fd = xopen(G.vcsa_name, O_RDONLY); |
| 153 | xread(vcsa_fd, &G.remote, 4); |
| 154 | i = G.remote.cols * 2; |
| 155 | G.first_line_offset = G.y * i; |
| 156 | i *= G.remote.lines; |
| 157 | if (G.data == NULL) { |
| 158 | G.size = i; |
| 159 | G.data = xzalloc(2 * i); |
| 160 | } |
| 161 | else if (G.size != i) { |
| 162 | cleanup(1); |
| 163 | } |
| 164 | data = G.data + G.current; |
| 165 | xread(vcsa_fd, data, G.size); |
| 166 | close(vcsa_fd); |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 167 | for (i = 0; i < G.remote.lines; i++) { |
| 168 | for (j = 0; j < G.remote.cols; j++, NEXT(data)) { |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 169 | unsigned x = j - G.x; // if will catch j < G.x too |
| 170 | unsigned y = i - G.y; // if will catch i < G.y too |
| 171 | |
| 172 | if (CHAR(data) < ' ') |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 173 | CHAR(data) = ' '; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 174 | if (y >= G.height || x >= G.width) |
| 175 | DATA(data) = 0; |
| 176 | } |
| 177 | } |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static void screen_char(char *data) |
| 181 | { |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 182 | if (!BW) { |
| 183 | uint8_t attr = ATTR(data); |
| 184 | //uint8_t attr = ATTR(data) >> 1; // for framebuffer console |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 185 | uint8_t attr_diff = G.last_attr ^ attr; |
Denys Vlasenko | a54985b | 2010-06-24 17:50:00 +0200 | [diff] [blame] | 186 | |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 187 | if (attr_diff) { |
Denys Vlasenko | a54985b | 2010-06-24 17:50:00 +0200 | [diff] [blame] | 188 | // Attribute layout for VGA compatible text videobuffer: |
| 189 | // blinking text |
| 190 | // |red bkgd |
| 191 | // ||green bkgd |
| 192 | // |||blue bkgd |
| 193 | // vvvv |
| 194 | // 00000000 <- lsb bit on the right |
| 195 | // bold text / text 8th bit |
| 196 | // red text |
| 197 | // green text |
| 198 | // blue text |
| 199 | // TODO: apparently framebuffer-based console uses different layout |
| 200 | // (bug? attempt to get 8th text bit in better position?) |
| 201 | // red bkgd |
| 202 | // |green bkgd |
| 203 | // ||blue bkgd |
| 204 | // vvv |
| 205 | // 00000000 <- lsb bit on the right |
| 206 | // bold text |
| 207 | // red text |
| 208 | // green text |
| 209 | // blue text |
| 210 | // text 8th bit |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 211 | // converting RGB color bit triad to BGR: |
| 212 | static const char color[8] = "04261537"; |
| 213 | const uint8_t fg_mask = 0x07, bold_mask = 0x08; |
| 214 | const uint8_t bg_mask = 0x70, blink_mask = 0x80; |
| 215 | char *ptr; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 216 | |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 217 | ptr = G.attrbuf + 2; // skip "ESC [" |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 218 | |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 219 | // (G.last_attr & ~attr) has 1 only where |
| 220 | // G.last_attr has 1 but attr has 0. |
| 221 | // Here we check whether we have transition |
| 222 | // bold->non-bold or blink->non-blink: |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 223 | if (G.last_attr < 0 // initial value |
| 224 | || ((G.last_attr & ~attr) & (bold_mask | blink_mask)) != 0 |
| 225 | ) { |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 226 | *ptr++ = '0'; // "reset all attrs" |
| 227 | *ptr++ = ';'; |
| 228 | // must set fg & bg, maybe need to set bold or blink: |
| 229 | attr_diff = attr | ~(bold_mask | blink_mask); |
| 230 | } |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 231 | G.last_attr = attr; |
| 232 | if (attr_diff & bold_mask) { |
| 233 | *ptr++ = '1'; |
| 234 | *ptr++ = ';'; |
| 235 | } |
| 236 | if (attr_diff & blink_mask) { |
| 237 | *ptr++ = '5'; |
| 238 | *ptr++ = ';'; |
| 239 | } |
| 240 | if (attr_diff & fg_mask) { |
| 241 | *ptr++ = '3'; |
| 242 | *ptr++ = color[attr & fg_mask]; |
| 243 | *ptr++ = ';'; |
| 244 | } |
| 245 | if (attr_diff & bg_mask) { |
| 246 | *ptr++ = '4'; |
| 247 | *ptr++ = color[(attr & bg_mask) >> 4]; |
| 248 | *ptr++ = ';'; |
| 249 | } |
| 250 | if (ptr != G.attrbuf + 2) { |
| 251 | ptr[-1] = 'm'; |
| 252 | *ptr = '\0'; |
| 253 | fputs(G.attrbuf, stdout); |
| 254 | } |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 255 | } |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 256 | } |
Denys Vlasenko | a54985b | 2010-06-24 17:50:00 +0200 | [diff] [blame] | 257 | putchar(CHAR(data)); |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 258 | G.col++; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 259 | } |
| 260 | |
Pascal Bellard | 1b8e2b0 | 2010-06-23 20:25:00 +0200 | [diff] [blame] | 261 | static void screen_dump(void) |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 262 | { |
Denys Vlasenko | 05a550b | 2010-06-21 04:00:16 +0200 | [diff] [blame] | 263 | int linefeed_cnt; |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 264 | int line, col; |
| 265 | int linecnt = G.remote.lines - G.y; |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 266 | char *data = G.data + G.current + G.first_line_offset; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 267 | |
Denys Vlasenko | 05a550b | 2010-06-21 04:00:16 +0200 | [diff] [blame] | 268 | linefeed_cnt = 0; |
| 269 | for (line = 0; line < linecnt && line < G.height; line++) { |
| 270 | int space_cnt = 0; |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 271 | for (col = 0; col < G.remote.cols; col++, NEXT(data)) { |
| 272 | unsigned tty_col = col - G.x; // if will catch col < G.x too |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 273 | |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 274 | if (tty_col >= G.width) |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 275 | continue; |
Denys Vlasenko | 05a550b | 2010-06-21 04:00:16 +0200 | [diff] [blame] | 276 | space_cnt++; |
Denys Vlasenko | 51fa147 | 2010-06-25 00:57:57 +0200 | [diff] [blame] | 277 | if (BW && CHAR(data) == ' ') |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 278 | continue; |
Denys Vlasenko | 05a550b | 2010-06-21 04:00:16 +0200 | [diff] [blame] | 279 | while (linefeed_cnt != 0) { |
Denys Vlasenko | 51fa147 | 2010-06-25 00:57:57 +0200 | [diff] [blame] | 280 | //bb_putchar('\r'); - tty driver does it for us |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 281 | bb_putchar('\n'); |
Denys Vlasenko | 05a550b | 2010-06-21 04:00:16 +0200 | [diff] [blame] | 282 | linefeed_cnt--; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 283 | } |
Denys Vlasenko | 05a550b | 2010-06-21 04:00:16 +0200 | [diff] [blame] | 284 | while (--space_cnt) |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 285 | bb_putchar(' '); |
| 286 | screen_char(data); |
| 287 | } |
Denys Vlasenko | 05a550b | 2010-06-21 04:00:16 +0200 | [diff] [blame] | 288 | linefeed_cnt++; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
| 292 | static void curmove(void) |
| 293 | { |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 294 | unsigned cx = G.remote.cursor_x - G.x; |
| 295 | unsigned cy = G.remote.cursor_y - G.y; |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 296 | int cursor = 1; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 297 | |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 298 | if (cx < G.width && cy < G.height) { |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 299 | gotoxy(cx, cy); |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 300 | cursor = -1; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 301 | } |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 302 | set_cursor(cursor); |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | static void create_cdev_if_doesnt_exist(const char* name, dev_t dev) |
| 306 | { |
| 307 | int fd = open(name, O_RDONLY); |
| 308 | if (fd != -1) |
| 309 | close(fd); |
| 310 | else if (errno == ENOENT) |
| 311 | mknod(name, S_IFCHR | 0660, dev); |
| 312 | } |
| 313 | |
| 314 | static NOINLINE void start_shell_in_child(const char* tty_name) |
| 315 | { |
Pascal Bellard | 926031b | 2010-07-04 15:32:38 +0200 | [diff] [blame] | 316 | int pid = xvfork(); |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 317 | if (pid == 0) { |
| 318 | struct termios termchild; |
| 319 | char *shell = getenv("SHELL"); |
| 320 | |
| 321 | if (!shell) |
| 322 | shell = (char *) DEFAULT_SHELL; |
| 323 | signal(SIGHUP, SIG_IGN); |
| 324 | // set tty as a controlling tty |
| 325 | setsid(); |
| 326 | // make tty to be input, output, error |
| 327 | close(0); |
| 328 | xopen(tty_name, O_RDWR); // uses fd 0 |
| 329 | xdup2(0, 1); |
| 330 | xdup2(0, 2); |
| 331 | ioctl(0, TIOCSCTTY, 1); |
| 332 | tcsetpgrp(0, getpid()); |
| 333 | tcgetattr(0, &termchild); |
| 334 | termchild.c_lflag |= ECHO; |
| 335 | termchild.c_oflag |= ONLCR | XTABS; |
| 336 | termchild.c_iflag |= ICRNL; |
| 337 | termchild.c_iflag &= ~IXOFF; |
| 338 | tcsetattr_stdin_TCSANOW(&termchild); |
| 339 | execl(shell, shell, "-i", (char *) NULL); |
| 340 | bb_simple_perror_msg_and_die(shell); |
| 341 | } |
| 342 | } |
| 343 | |
Denys Vlasenko | 51fa147 | 2010-06-25 00:57:57 +0200 | [diff] [blame] | 344 | int conspy_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 345 | int conspy_main(int argc UNUSED_PARAM, char **argv) |
| 346 | { |
Denys Vlasenko | 51fa147 | 2010-06-25 00:57:57 +0200 | [diff] [blame] | 347 | char tty_name[sizeof("/dev/ttyNN")]; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 348 | #define keybuf bb_common_bufsiz1 |
| 349 | struct termios termbuf; |
| 350 | unsigned opts; |
| 351 | unsigned ttynum; |
| 352 | int poll_timeout_ms; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 353 | #if ENABLE_LONG_OPTS |
| 354 | static const char getopt_longopts[] ALIGN1 = |
| 355 | "viewonly\0" No_argument "v" |
| 356 | "createdevice\0" No_argument "c" |
| 357 | "session\0" No_argument "s" |
| 358 | "nocolors\0" No_argument "n" |
| 359 | "dump\0" No_argument "d" |
| 360 | "follow\0" No_argument "f" |
| 361 | ; |
| 362 | |
| 363 | applet_long_options = getopt_longopts; |
| 364 | #endif |
| 365 | INIT_G(); |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 366 | strcpy(G.vcsa_name, "/dev/vcsa"); |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 367 | |
| 368 | opt_complementary = "x+:y+"; // numeric params |
| 369 | opts = getopt32(argv, "vcsndfx:y:", &G.x, &G.y); |
| 370 | argv += optind; |
| 371 | ttynum = 0; |
| 372 | if (argv[0]) { |
| 373 | ttynum = xatou_range(argv[0], 0, 63); |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 374 | sprintf(G.vcsa_name + sizeof("/dev/vcsa")-1, "%u", ttynum); |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 375 | } |
| 376 | sprintf(tty_name, "%s%u", "/dev/tty", ttynum); |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 377 | if (opts & FLAG(c)) { |
| 378 | if ((opts & (FLAG(s)|FLAG(v))) != FLAG(v)) |
| 379 | create_cdev_if_doesnt_exist(tty_name, makedev(4, ttynum)); |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 380 | create_cdev_if_doesnt_exist(G.vcsa_name, makedev(7, 128 + ttynum)); |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 381 | } |
| 382 | if ((opts & FLAG(s)) && ttynum) { |
| 383 | start_shell_in_child(tty_name); |
| 384 | } |
| 385 | |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 386 | screen_read_close(); |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 387 | if (opts & FLAG(d)) { |
Pascal Bellard | 1b8e2b0 | 2010-06-23 20:25:00 +0200 | [diff] [blame] | 388 | screen_dump(); |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 389 | bb_putchar('\n'); |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | bb_signals(BB_FATAL_SIGS, cleanup); |
Denys Vlasenko | 918f444 | 2010-06-25 14:40:25 +0200 | [diff] [blame] | 394 | |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 395 | // All characters must be passed through to us unaltered |
Denys Vlasenko | 918f444 | 2010-06-25 14:40:25 +0200 | [diff] [blame] | 396 | G.kbd_fd = xopen(CURRENT_TTY, O_RDONLY); |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 397 | tcgetattr(G.kbd_fd, &G.term_orig); |
| 398 | termbuf = G.term_orig; |
| 399 | termbuf.c_iflag &= ~(BRKINT|INLCR|ICRNL|IXON|IXOFF|IUCLC|IXANY|IMAXBEL); |
Denys Vlasenko | 51fa147 | 2010-06-25 00:57:57 +0200 | [diff] [blame] | 400 | //termbuf.c_oflag &= ~(OPOST); - no, we still want \n -> \r\n |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 401 | termbuf.c_lflag &= ~(ISIG|ICANON|ECHO); |
| 402 | termbuf.c_cc[VMIN] = 1; |
| 403 | termbuf.c_cc[VTIME] = 0; |
| 404 | tcsetattr(G.kbd_fd, TCSANOW, &termbuf); |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 405 | |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 406 | poll_timeout_ms = 250; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 407 | while (1) { |
| 408 | struct pollfd pfd; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 409 | int bytes_read; |
| 410 | int i, j; |
Pascal Bellard | 1b8e2b0 | 2010-06-23 20:25:00 +0200 | [diff] [blame] | 411 | char *data, *old; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 412 | |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 413 | // in the first loop G.width = G.height = 0: refresh |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 414 | i = G.width; |
| 415 | j = G.height; |
| 416 | get_terminal_width_height(G.kbd_fd, &G.width, &G.height); |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 417 | if (option_mask32 & FLAG(f)) { |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 418 | int nx = G.remote.cursor_x - G.width + 1; |
| 419 | int ny = G.remote.cursor_y - G.height + 1; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 420 | |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 421 | if (G.remote.cursor_x < G.x) { |
| 422 | G.x = G.remote.cursor_x; |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 423 | i = 0; // force refresh |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 424 | } |
| 425 | if (nx > G.x) { |
| 426 | G.x = nx; |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 427 | i = 0; // force refresh |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 428 | } |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 429 | if (G.remote.cursor_y < G.y) { |
| 430 | G.y = G.remote.cursor_y; |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 431 | i = 0; // force refresh |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 432 | } |
| 433 | if (ny > G.y) { |
| 434 | G.y = ny; |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 435 | i = 0; // force refresh |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
| 439 | // Scan console data and redraw our tty where needed |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 440 | old = G.data + G.current; |
| 441 | G.current = G.size - G.current; |
| 442 | data = G.data + G.current; |
Pascal Bellard | 1b8e2b0 | 2010-06-23 20:25:00 +0200 | [diff] [blame] | 443 | screen_read_close(); |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 444 | if (i != G.width || j != G.height) { |
| 445 | clrscr(); |
Pascal Bellard | 1b8e2b0 | 2010-06-23 20:25:00 +0200 | [diff] [blame] | 446 | screen_dump(); |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 447 | } else { |
| 448 | // For each remote line |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 449 | old += G.first_line_offset; |
| 450 | data += G.first_line_offset; |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 451 | for (i = G.y; i < G.remote.lines; i++) { |
| 452 | char *first = NULL; // first char which needs updating |
| 453 | char *last = last; // last char which needs updating |
| 454 | unsigned iy = i - G.y; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 455 | |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 456 | if (iy >= G.height) |
| 457 | break; |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 458 | for (j = 0; j < G.remote.cols; j++, NEXT(old), NEXT(data)) { |
| 459 | unsigned jx = j - G.x; // if will catch j >= G.x too |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 460 | |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 461 | if (jx < G.width && DATA(data) != DATA(old)) { |
| 462 | last = data; |
| 463 | if (!first) { |
| 464 | first = data; |
| 465 | gotoxy(jx, iy); |
| 466 | } |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 467 | } |
| 468 | } |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 469 | if (first) { |
| 470 | // Rewrite updated data on the local screen |
| 471 | for (; first <= last; NEXT(first)) |
| 472 | screen_char(first); |
| 473 | } |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 474 | } |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 475 | } |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 476 | curmove(); |
| 477 | |
| 478 | // Wait for local user keypresses |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 479 | fflush_all(); |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 480 | pfd.fd = G.kbd_fd; |
| 481 | pfd.events = POLLIN; |
| 482 | bytes_read = 0; |
| 483 | switch (poll(&pfd, 1, poll_timeout_ms)) { |
Pascal Bellard | 1b8e2b0 | 2010-06-23 20:25:00 +0200 | [diff] [blame] | 484 | char *k; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 485 | case -1: |
| 486 | if (errno != EINTR) |
| 487 | cleanup(1); |
| 488 | break; |
| 489 | case 0: |
Pascal Bellard | 1b8e2b0 | 2010-06-23 20:25:00 +0200 | [diff] [blame] | 490 | if (++G.nokeys >= 4) |
| 491 | G.nokeys = G.escape_count = 0; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 492 | break; |
| 493 | default: |
| 494 | // Read the keys pressed |
Pascal Bellard | 1b8e2b0 | 2010-06-23 20:25:00 +0200 | [diff] [blame] | 495 | k = keybuf + G.key_count; |
| 496 | bytes_read = read(G.kbd_fd, k, sizeof(keybuf) - G.key_count); |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 497 | if (bytes_read < 0) |
| 498 | cleanup(1); |
| 499 | |
| 500 | // Do exit processing |
| 501 | for (i = 0; i < bytes_read; i++) { |
Denys Vlasenko | 51fa147 | 2010-06-25 00:57:57 +0200 | [diff] [blame] | 502 | if (k[i] != '\033') |
| 503 | G.escape_count = 0; |
Pascal Bellard | 1b8e2b0 | 2010-06-23 20:25:00 +0200 | [diff] [blame] | 504 | else if (++G.escape_count >= 3) |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 505 | cleanup(0); |
| 506 | } |
| 507 | } |
| 508 | poll_timeout_ms = 250; |
| 509 | |
| 510 | // Insert all keys pressed into the virtual console's input |
| 511 | // buffer. Don't do this if the virtual console is in scan |
| 512 | // code mode - giving ASCII characters to a program expecting |
| 513 | // scan codes will confuse it. |
Pascal Bellard | 1b8e2b0 | 2010-06-23 20:25:00 +0200 | [diff] [blame] | 514 | if (!(option_mask32 & FLAG(v)) && G.escape_count == 0) { |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 515 | int handle, result; |
| 516 | long kbd_mode; |
| 517 | |
Pascal Bellard | 1b8e2b0 | 2010-06-23 20:25:00 +0200 | [diff] [blame] | 518 | G.key_count += bytes_read; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 519 | handle = xopen(tty_name, O_WRONLY); |
| 520 | result = ioctl(handle, KDGKBMODE, &kbd_mode); |
Pascal Bellard | 8119967 | 2010-07-01 07:18:41 +0200 | [diff] [blame] | 521 | if (result >= 0) { |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 522 | char *p = keybuf; |
Pascal Bellard | 8119967 | 2010-07-01 07:18:41 +0200 | [diff] [blame] | 523 | |
| 524 | if (kbd_mode != K_XLATE && kbd_mode != K_UNICODE) { |
| 525 | G.key_count = 0; // scan code mode |
| 526 | } |
| 527 | for (; G.key_count != 0; p++, G.key_count--) { |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 528 | result = ioctl(handle, TIOCSTI, p); |
Pascal Bellard | 8119967 | 2010-07-01 07:18:41 +0200 | [diff] [blame] | 529 | if (result < 0) { |
| 530 | memmove(keybuf, p, G.key_count); |
| 531 | break; |
| 532 | } |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 533 | // If there is an application on console which reacts |
| 534 | // to keypresses, we need to make our first sleep |
| 535 | // shorter to quickly redraw whatever it printed there. |
| 536 | poll_timeout_ms = 20; |
| 537 | } |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 538 | } |
| 539 | // Close & re-open tty in case they have |
| 540 | // swapped virtual consoles |
| 541 | close(handle); |
| 542 | |
| 543 | // We sometimes get spurious IO errors on the TTY |
| 544 | // as programs close and re-open it |
Pascal Bellard | 8119967 | 2010-07-01 07:18:41 +0200 | [diff] [blame] | 545 | if (result >= 0) |
Pascal Bellard | 1b8e2b0 | 2010-06-23 20:25:00 +0200 | [diff] [blame] | 546 | G.ioerror_count = 0; |
| 547 | else if (errno != EIO || ++G.ioerror_count > 4) |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 548 | cleanup(1); |
| 549 | } |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 550 | } /* while (1) */ |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 551 | } |