1 /* vi: set sw=4 ts=4: */
3 * A text-mode VNC like program for Linux virtual terminals.
5 * pascal.bellard@ads-lu.com
7 * Based on Russell Stuart's conspy.c
8 * http://ace-host.stuart.id.au/russell/files/conspy.c
10 * Licensed under GPLv2 or later, see file License in this tarball for details.
12 * example : conspy num shared access to console num
13 * or conspy -d num screenshot of console num
14 * or conspy -cs num poor man's GNU screen like
17 //applet:IF_CONSPY(APPLET(conspy, _BB_DIR_BIN, _BB_SUID_DROP))
19 //kbuild:lib-$(CONFIG_CONSPY) += conspy.o
21 //config:config CONSPY
22 //config: bool "conspy"
25 //config: A text-mode VNC like program for Linux virtual terminals.
26 //config: example : conspy num shared access to console num
27 //config: or conspy -d num screenshot of console num
28 //config: or conspy -cs num poor man's GNU screen like
30 //usage:#define conspy_trivial_usage
31 //usage: "[-vcsndf] [-x ROW] [-y LINE] [CONSOLE_NO]"
32 //usage:#define conspy_full_usage "\n\n"
33 //usage: "A text-mode VNC like program for Linux virtual consoles."
34 //usage: "\nTo exit, quickly press ESC 3 times."
37 //usage: "\n -v Don't send keystrokes to the console"
38 //usage: "\n -c Create missing devices in /dev"
39 //usage: "\n -s Open a SHELL session"
40 //usage: "\n -n Black & white"
41 //usage: "\n -d Dump console to stdout"
42 //usage: "\n -f Follow cursor"
43 //usage: "\n -x ROW Starting row"
44 //usage: "\n -y LINE Starting line"
50 unsigned char lines, rows, cursor_x, cursor_y;
53 #define CHAR(x) ((x)[0])
54 #define ATTR(x) ((x)[1])
55 #define NEXT(x) ((x)+=2)
56 #define DATA(x) (* (short *) (x))
66 struct screen_info info;
67 struct termios term_orig;
70 #define G (*ptr_to_globals)
71 #define INIT_G() do { \
72 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
77 FLAG_c, // create device if need
80 FLAG_d, // dump screen
81 FLAG_f, // follow cursor
83 #define FLAG(x) (1 << FLAG_##x)
84 #define BW (option_mask32 & FLAG(n))
86 static void screen_read_close(int fd, char *data)
90 xread(fd, data, G.size);
92 for (i = 0; i < G.info.lines; i++) {
93 for (j = 0; j < G.info.rows; j++, NEXT(data)) {
94 unsigned x = j - G.x; // if will catch j < G.x too
95 unsigned y = i - G.y; // if will catch i < G.y too
99 if (y >= G.height || x >= G.width)
106 static void screen_char(char *data)
108 if (!BW && G.last_attr != ATTR(data)) {
110 static const char color[8] = "04261537";
112 printf("\033[%c;4%c;3%cm",
113 (ATTR(data) & 8) ? '1' // bold
115 color[(ATTR(data) >> 4) & 7], color[ATTR(data) & 7]);
116 G.last_attr = ATTR(data);
118 bb_putchar(CHAR(data));
121 #define clrscr() printf("\033[1;1H" "\033[0J")
122 #define curoff() printf("\033[?25l")
124 static void curon(void)
129 static void gotoxy(int row, int line)
131 printf("\033[%u;%uH", line + 1, row + 1);
134 static void screen_dump(char *data)
138 int linecnt = G.info.lines - G.y;
140 data += 2 * G.y * G.info.rows;
142 for (line = 0; line < linecnt && line < G.height; line++) {
144 for (row = 0; row < G.info.rows; row++, NEXT(data)) {
145 unsigned tty_row = row - G.x; // if will catch row < G.x too
147 if (tty_row >= G.width)
150 if (BW && (CHAR(data) | ' ') == ' ')
152 while (linefeed_cnt != 0) {
165 static void curmove(void)
167 unsigned cx = G.info.cursor_x - G.x;
168 unsigned cy = G.info.cursor_y - G.y;
170 if (cx >= G.width || cy >= G.height) {
179 static void cleanup(int code)
183 tcsetattr(G.kbd_fd, TCSANOW, &G.term_orig);
184 if (ENABLE_FEATURE_CLEAN_UP) {
185 free(ptr_to_globals);
193 kill_myself_with_sig(code); // does not return
197 static void get_initial_data(const char* vcsa_name)
200 int fd = xopen(vcsa_name, O_RDONLY);
201 xread(fd, &G.info, 4);
202 G.size = size = G.info.rows * G.info.lines * 2;
203 G.width = G.height = UINT_MAX;
204 G.data = xzalloc(2 * size);
205 screen_read_close(fd, G.data);
208 static void create_cdev_if_doesnt_exist(const char* name, dev_t dev)
210 int fd = open(name, O_RDONLY);
213 else if (errno == ENOENT)
214 mknod(name, S_IFCHR | 0660, dev);
217 static NOINLINE void start_shell_in_child(const char* tty_name)
221 bb_perror_msg_and_die("vfork");
224 struct termios termchild;
225 char *shell = getenv("SHELL");
228 shell = (char *) DEFAULT_SHELL;
229 signal(SIGHUP, SIG_IGN);
230 // set tty as a controlling tty
232 // make tty to be input, output, error
234 xopen(tty_name, O_RDWR); // uses fd 0
237 ioctl(0, TIOCSCTTY, 1);
238 tcsetpgrp(0, getpid());
239 tcgetattr(0, &termchild);
240 termchild.c_lflag |= ECHO;
241 termchild.c_oflag |= ONLCR | XTABS;
242 termchild.c_iflag |= ICRNL;
243 termchild.c_iflag &= ~IXOFF;
244 tcsetattr_stdin_TCSANOW(&termchild);
245 execl(shell, shell, "-i", (char *) NULL);
246 bb_simple_perror_msg_and_die(shell);
250 int conspy_main(int argc UNUSED_PARAM, char **argv) MAIN_EXTERNALLY_VISIBLE;
251 int conspy_main(int argc UNUSED_PARAM, char **argv)
254 char vcsa_name[sizeof("/dev/vcsa") + 2];
255 char tty_name[sizeof("/dev/tty") + 2];
256 #define keybuf bb_common_bufsiz1
257 struct termios termbuf;
267 static const char getopt_longopts[] ALIGN1 =
268 "viewonly\0" No_argument "v"
269 "createdevice\0" No_argument "c"
270 "session\0" No_argument "s"
271 "nocolors\0" No_argument "n"
272 "dump\0" No_argument "d"
273 "follow\0" No_argument "f"
276 applet_long_options = getopt_longopts;
279 strcpy(vcsa_name, "/dev/vcsa");
281 opt_complementary = "x+:y+"; // numeric params
282 opts = getopt32(argv, "vcsndfx:y:", &G.x, &G.y);
286 ttynum = xatou_range(argv[0], 0, 63);
287 sprintf(vcsa_name + sizeof("/dev/vcsa")-1, "%u", ttynum);
289 sprintf(tty_name, "%s%u", "/dev/tty", ttynum);
290 if (opts & FLAG(c)) {
291 if ((opts & (FLAG(s)|FLAG(v))) != FLAG(v))
292 create_cdev_if_doesnt_exist(tty_name, makedev(4, ttynum));
293 create_cdev_if_doesnt_exist(vcsa_name, makedev(7, 128 + ttynum));
295 if ((opts & FLAG(s)) && ttynum) {
296 start_shell_in_child(tty_name);
299 get_initial_data(vcsa_name);
300 G.kbd_fd = xopen(CURRENT_TTY, O_RDONLY);
301 if (opts & FLAG(d)) {
304 if (ENABLE_FEATURE_CLEAN_UP) {
305 free(ptr_to_globals);
311 bb_signals(BB_FATAL_SIGS, cleanup);
312 // All characters must be passed through to us unaltered
313 tcgetattr(G.kbd_fd, &G.term_orig);
314 termbuf = G.term_orig;
315 termbuf.c_iflag &= ~(BRKINT|INLCR|ICRNL|IXON|IXOFF|IUCLC|IXANY|IMAXBEL);
316 termbuf.c_oflag &= ~(OPOST);
317 termbuf.c_lflag &= ~(ISIG|ICANON|ECHO);
318 termbuf.c_cc[VMIN] = 1;
319 termbuf.c_cc[VTIME] = 0;
320 tcsetattr(G.kbd_fd, TCSANOW, &termbuf);
322 buffer[1] = G.data + G.size;
323 poll_timeout_ms = 250;
324 ioerror_count = key_count = escape_count = nokeys = current = 0;
330 int next = 1 - current;
331 char *data = buffer[next];
332 char *old = buffer[current];
334 // Close & re-open vcsa in case they have
335 // swapped virtual consoles
336 vcsa_handle = xopen(vcsa_name, O_RDONLY);
337 xread(vcsa_handle, &G.info, 4);
338 if (G.size != (G.info.rows * G.info.lines * 2)) {
343 get_terminal_width_height(G.kbd_fd, &G.width, &G.height);
344 if ((option_mask32 & FLAG(f))) {
345 int nx = G.info.cursor_x - G.width + 1;
346 int ny = G.info.cursor_y - G.height + 1;
348 if (G.info.cursor_x < G.x) {
349 G.x = G.info.cursor_x;
350 i = 0; // force refresh
354 i = 0; // force refresh
356 if (G.info.cursor_y < G.y) {
357 G.y = G.info.cursor_y;
358 i = 0; // force refresh
362 i = 0; // force refresh
366 // Scan console data and redraw our tty where needed
367 screen_read_close(vcsa_handle, data);
368 if (i != G.width || j != G.height) {
372 else for (i = 0; i < G.info.lines; i++) {
377 if (iy >= (int) G.height)
379 for (j = 0; j < G.info.rows; j++) {
381 if (DATA(data) != DATA(old) && iy >= 0) {
382 unsigned jx = j - G.x;
385 if (first == NULL && jx < G.width) {
398 // Write the data to the screen
399 for (; first < last; NEXT(first))
405 // Wait for local user keypresses
409 switch (poll(&pfd, 1, poll_timeout_ms)) {
416 nokeys = escape_count = 0;
419 // Read the keys pressed
420 bytes_read = read(G.kbd_fd, keybuf + key_count,
421 sizeof(keybuf) - key_count);
425 // Do exit processing
426 for (i = 0; i < bytes_read; i++) {
427 if (keybuf[key_count + i] != '\033')
429 else if (++escape_count >= 3)
433 poll_timeout_ms = 250;
435 // Insert all keys pressed into the virtual console's input
436 // buffer. Don't do this if the virtual console is in scan
437 // code mode - giving ASCII characters to a program expecting
438 // scan codes will confuse it.
439 if (!(option_mask32 & FLAG(v)) && escape_count == 0) {
443 key_count += bytes_read;
444 handle = xopen(tty_name, O_WRONLY);
445 result = ioctl(handle, KDGKBMODE, &kbd_mode);
448 else if (kbd_mode != K_XLATE && kbd_mode != K_UNICODE)
449 key_count = 0; // scan code mode
451 for (i = 0; i < key_count && result != -1; i++)
452 result = ioctl(handle, TIOCSTI, keybuf + i);
455 memmove(keybuf, keybuf + i, key_count);
456 // If there is an application on console which reacts
457 // to keypresses, we need to make our first sleep
458 // shorter to quickly redraw whatever it printed there.
459 poll_timeout_ms = 20;
461 // Close & re-open tty in case they have
462 // swapped virtual consoles
465 // We sometimes get spurious IO errors on the TTY
466 // as programs close and re-open it
469 else if (errno != EIO || ++ioerror_count > 4)