… | |
… | |
22 | *---------------------------------------------------------------------*/ |
22 | *---------------------------------------------------------------------*/ |
23 | |
23 | |
24 | #include "../config.h" /* NECESSARY */ |
24 | #include "../config.h" /* NECESSARY */ |
25 | #include "rxvt.h" |
25 | #include "rxvt.h" |
26 | |
26 | |
27 | # include <cstdlib> |
27 | #include <cstdlib> |
28 | # include <cstring> |
28 | #include <cstring> |
29 | |
29 | |
30 | #ifdef HAVE_SYS_TYPES_H |
|
|
31 | # include <sys/types.h> |
30 | #include <sys/types.h> |
32 | #endif |
31 | #include <sys/socket.h> |
33 | #ifdef HAVE_UNISTD_H |
|
|
34 | # include <unistd.h> |
32 | #include <unistd.h> |
35 | #endif |
|
|
36 | #ifdef HAVE_FCNTL_H |
|
|
37 | # include <fcntl.h> |
33 | #include <fcntl.h> |
38 | #endif |
34 | |
39 | #ifdef HAVE_SYS_IOCTL_H |
35 | #ifdef HAVE_SYS_IOCTL_H |
40 | # include <sys/ioctl.h> |
36 | # include <sys/ioctl.h> |
41 | #endif |
37 | #endif |
42 | #if defined(PTYS_ARE_PTMX) && defined(HAVE_SYS_STROPTS_H) |
38 | #if defined(PTYS_ARE_PTMX) && defined(HAVE_SYS_STROPTS_H) |
43 | # include <sys/stropts.h> /* for I_PUSH */ |
39 | # include <sys/stropts.h> /* for I_PUSH */ |
… | |
… | |
53 | # include <util.h> |
49 | # include <util.h> |
54 | #endif |
50 | #endif |
55 | |
51 | |
56 | #include <cstdio> |
52 | #include <cstdio> |
57 | #include <grp.h> |
53 | #include <grp.h> |
|
|
54 | |
|
|
55 | #include "rxvtutil.h" |
|
|
56 | #include "fdpass.h" |
|
|
57 | #include "ptytty.h" |
58 | |
58 | |
59 | ///////////////////////////////////////////////////////////////////////////// |
59 | ///////////////////////////////////////////////////////////////////////////// |
60 | |
60 | |
61 | /* ------------------------------------------------------------------------- * |
61 | /* ------------------------------------------------------------------------- * |
62 | * GET PSEUDO TELETYPE - MASTER AND SLAVE * |
62 | * GET PSEUDO TELETYPE - MASTER AND SLAVE * |
… | |
… | |
222 | * Make our tty a controlling tty so that /dev/tty points to us |
222 | * Make our tty a controlling tty so that /dev/tty points to us |
223 | */ |
223 | */ |
224 | static int |
224 | static int |
225 | control_tty (int fd_tty) |
225 | control_tty (int fd_tty) |
226 | { |
226 | { |
227 | #ifndef __QNX__ |
|
|
228 | int fd; |
227 | int fd; |
229 | |
228 | |
230 | /* ---------------------------------------- */ |
229 | /* ---------------------------------------- */ |
231 | # ifdef HAVE_SETSID |
|
|
232 | setsid (); |
230 | setsid (); |
233 | # endif |
|
|
234 | # if defined(HAVE_SETPGID) |
|
|
235 | setpgid (0, 0); |
|
|
236 | # elif defined(HAVE_SETPGRP) |
|
|
237 | setpgrp (0, 0); |
|
|
238 | # endif |
|
|
239 | |
|
|
240 | /* ---------------------------------------- */ |
|
|
241 | # ifdef TIOCNOTTY |
|
|
242 | fd = open ("/dev/tty", O_RDWR | O_NOCTTY); |
|
|
243 | if (fd >= 0) |
|
|
244 | { |
|
|
245 | ioctl (fd, TIOCNOTTY, NULL); /* void tty associations */ |
|
|
246 | close (fd); |
|
|
247 | } |
|
|
248 | # endif |
|
|
249 | |
|
|
250 | /* ---------------------------------------- */ |
|
|
251 | fd = open ("/dev/tty", O_RDWR | O_NOCTTY); |
|
|
252 | if (fd >= 0) |
|
|
253 | close (fd); /* ouch: still have controlling tty */ |
|
|
254 | |
231 | |
255 | /* ---------------------------------------- */ |
232 | /* ---------------------------------------- */ |
256 | # if defined(PTYS_ARE_PTMX) && defined(I_PUSH) |
233 | # if defined(PTYS_ARE_PTMX) && defined(I_PUSH) |
257 | /* |
234 | /* |
258 | * Push STREAMS modules: |
235 | * Push STREAMS modules: |
… | |
… | |
286 | fd = open ("/dev/tty", O_WRONLY); |
263 | fd = open ("/dev/tty", O_WRONLY); |
287 | if (fd < 0) |
264 | if (fd < 0) |
288 | return -1; /* fatal */ |
265 | return -1; /* fatal */ |
289 | close (fd); |
266 | close (fd); |
290 | /* ---------------------------------------- */ |
267 | /* ---------------------------------------- */ |
291 | #endif /* ! __QNX__ */ |
|
|
292 | |
268 | |
293 | return 0; |
269 | return 0; |
294 | } |
270 | } |
295 | |
271 | |
296 | void |
272 | void |
… | |
… | |
465 | } |
441 | } |
466 | |
442 | |
467 | return true; |
443 | return true; |
468 | } |
444 | } |
469 | |
445 | |
|
|
446 | #if PTYTTY_HELPER |
|
|
447 | |
|
|
448 | static int sock_fd; |
|
|
449 | static int pid; |
|
|
450 | |
|
|
451 | struct command |
|
|
452 | { |
|
|
453 | enum { get, login, destroy } type; |
|
|
454 | |
|
|
455 | rxvt_ptytty *id; |
|
|
456 | |
|
|
457 | bool login_shell; |
|
|
458 | int cmd_pid; |
|
|
459 | char hostname[512]; // arbitrary, but should be plenty |
|
|
460 | }; |
|
|
461 | |
|
|
462 | struct rxvt_ptytty_proxy : zero_initialized, rxvt_ptytty |
|
|
463 | { |
|
|
464 | rxvt_ptytty *id; |
|
|
465 | |
|
|
466 | rxvt_ptytty_proxy (); |
|
|
467 | ~rxvt_ptytty_proxy (); |
|
|
468 | |
|
|
469 | bool get (); |
|
|
470 | void login (int cmd_pid, bool login_shell, const char *hostname); |
|
|
471 | }; |
|
|
472 | |
|
|
473 | bool |
|
|
474 | rxvt_ptytty_proxy::get () |
|
|
475 | { |
|
|
476 | command cmd; |
|
|
477 | |
|
|
478 | cmd.type = command::get; |
|
|
479 | |
|
|
480 | write (sock_fd, &cmd, sizeof (cmd)); |
|
|
481 | |
|
|
482 | if (read (sock_fd, &id, sizeof (id)) != sizeof (id)) |
|
|
483 | rxvt_fatal ("protocol error while creating pty using helper process, aborting.\n"); |
|
|
484 | |
|
|
485 | if (!id) |
|
|
486 | return false; |
|
|
487 | |
|
|
488 | if ((pty = rxvt_recv_fd (sock_fd)) < 0 |
|
|
489 | || (tty = rxvt_recv_fd (sock_fd)) < 0) |
|
|
490 | rxvt_fatal ("protocol error while reading pty/tty fds from helper process, aborting.\n"); |
|
|
491 | |
|
|
492 | return true; |
|
|
493 | } |
|
|
494 | |
|
|
495 | void |
|
|
496 | rxvt_ptytty_proxy::login (int cmd_pid, bool login_shell, const char *hostname) |
|
|
497 | { |
|
|
498 | command cmd; |
|
|
499 | |
|
|
500 | cmd.type = command::login; |
|
|
501 | cmd.id = id; |
|
|
502 | cmd.cmd_pid = cmd_pid; |
|
|
503 | cmd.login_shell = login_shell; |
|
|
504 | strncpy (cmd.hostname, hostname, sizeof (cmd.hostname)); |
|
|
505 | |
|
|
506 | write (sock_fd, &cmd, sizeof (cmd)); |
|
|
507 | } |
|
|
508 | |
|
|
509 | rxvt_ptytty_proxy::~rxvt_ptytty_proxy () |
|
|
510 | { |
|
|
511 | command cmd; |
|
|
512 | |
|
|
513 | cmd.type = command::destroy; |
|
|
514 | cmd.id = id; |
|
|
515 | |
|
|
516 | write (sock_fd, &cmd, sizeof (cmd)); |
|
|
517 | } |
|
|
518 | |
|
|
519 | static |
|
|
520 | void serve () |
|
|
521 | { |
|
|
522 | command cmd; |
|
|
523 | vector<rxvt_ptytty *> ptys; |
|
|
524 | |
|
|
525 | while (read (sock_fd, &cmd, sizeof (command)) == sizeof (command)) |
|
|
526 | { |
|
|
527 | if (cmd.type == command::get) |
|
|
528 | { |
|
|
529 | // -> id ptyfd ttyfd |
|
|
530 | cmd.id = new rxvt_ptytty_unix; |
|
|
531 | |
|
|
532 | if (cmd.id->get ()) |
|
|
533 | { |
|
|
534 | write (sock_fd, &cmd.id, sizeof (cmd.id)); |
|
|
535 | ptys.push_back (cmd.id); |
|
|
536 | |
|
|
537 | rxvt_send_fd (sock_fd, cmd.id->pty); |
|
|
538 | rxvt_send_fd (sock_fd, cmd.id->tty); |
|
|
539 | } |
|
|
540 | else |
|
|
541 | { |
|
|
542 | delete cmd.id; |
|
|
543 | cmd.id = 0; |
|
|
544 | write (sock_fd, &cmd.id, sizeof (cmd.id)); |
|
|
545 | } |
|
|
546 | } |
|
|
547 | else if (cmd.type == command::login) |
|
|
548 | { |
|
|
549 | if (find (ptys.begin (), ptys.end (), cmd.id)) |
|
|
550 | { |
|
|
551 | cmd.hostname[sizeof (cmd.hostname) - 1] = 0; |
|
|
552 | cmd.id->login (cmd.cmd_pid, cmd.login_shell, cmd.hostname); |
|
|
553 | } |
|
|
554 | else printf ("xxx hiya login no match %p\n", cmd.id); |
|
|
555 | } |
|
|
556 | else if (cmd.type == command::destroy) |
|
|
557 | { |
|
|
558 | rxvt_ptytty **pty = find (ptys.begin (), ptys.end (), cmd.id); |
|
|
559 | |
|
|
560 | if (*pty) |
|
|
561 | { |
|
|
562 | ptys.erase (pty); |
|
|
563 | delete *pty; |
|
|
564 | } |
|
|
565 | else printf ("xxx hiya destroy no match %p\n", cmd.id); |
|
|
566 | } |
|
|
567 | else |
|
|
568 | break; |
|
|
569 | } |
|
|
570 | |
|
|
571 | // destroy all ptys |
|
|
572 | for (rxvt_ptytty **i = ptys.end (); i-- > ptys.begin (); ) |
|
|
573 | delete *i; |
|
|
574 | } |
|
|
575 | |
|
|
576 | void rxvt_ptytty_server () |
|
|
577 | { |
|
|
578 | int sv[2]; |
|
|
579 | |
|
|
580 | if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv)) |
|
|
581 | rxvt_fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n"); |
|
|
582 | |
|
|
583 | pid = fork (); |
|
|
584 | |
|
|
585 | if (pid < 0) |
|
|
586 | rxvt_fatal ("could not create pty/sessiondb helper process, aborting.\n"); |
|
|
587 | |
|
|
588 | if (pid) |
|
|
589 | { |
|
|
590 | // client, urxvt |
|
|
591 | sock_fd = sv[0]; |
|
|
592 | close (sv[1]); |
|
|
593 | fcntl (sock_fd, F_SETFD, FD_CLOEXEC); |
|
|
594 | } |
|
|
595 | else |
|
|
596 | { |
|
|
597 | // server, pty-helper |
|
|
598 | sock_fd = sv[1]; |
|
|
599 | |
|
|
600 | close (sv[0]);//D |
|
|
601 | // for (int fd = 0; fd < 1023; fd++) |
|
|
602 | // if (fd != sock_fd) |
|
|
603 | // close (fd); |
|
|
604 | |
|
|
605 | serve (); |
|
|
606 | _exit (EXIT_SUCCESS); |
|
|
607 | } |
|
|
608 | } |
|
|
609 | #endif |
|
|
610 | |
|
|
611 | // a "factory" *g* |
|
|
612 | rxvt_ptytty * |
|
|
613 | rxvt_new_ptytty () |
|
|
614 | { |
|
|
615 | #if PTYTTY_HELPER |
|
|
616 | if (pid > 0) |
|
|
617 | { |
|
|
618 | // use helper process |
|
|
619 | return new rxvt_ptytty_proxy; |
|
|
620 | } |
|
|
621 | else |
|
|
622 | #endif |
|
|
623 | return new rxvt_ptytty_unix; |
|
|
624 | } |
|
|
625 | |
470 | /*----------------------- end-of-file (C source) -----------------------*/ |
626 | /*----------------------- end-of-file (C source) -----------------------*/ |
471 | |
627 | |