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