… | |
… | |
21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
22 | *---------------------------------------------------------------------*/ |
22 | *---------------------------------------------------------------------*/ |
23 | |
23 | |
24 | #include "../config.h" /* NECESSARY */ |
24 | #include "../config.h" /* NECESSARY */ |
25 | |
25 | |
|
|
26 | #include "fdpass.h" |
|
|
27 | #include "ptytty.h" |
|
|
28 | |
26 | #include <cstdlib> |
29 | #include <cstdlib> |
27 | #include <cstring> |
30 | #include <cstring> |
28 | |
31 | |
29 | #include <sys/types.h> |
32 | #include <sys/types.h> |
30 | #include <sys/socket.h> |
33 | #include <sys/socket.h> |
… | |
… | |
50 | #ifdef TTY_GID_SUPPORT |
53 | #ifdef TTY_GID_SUPPORT |
51 | #include <grp.h> |
54 | #include <grp.h> |
52 | #endif |
55 | #endif |
53 | |
56 | |
54 | #include <cstdio> |
57 | #include <cstdio> |
55 | |
|
|
56 | #include "rxvtutil.h" |
|
|
57 | #include "fdpass.h" |
|
|
58 | #include "ptytty.h" |
|
|
59 | |
58 | |
60 | ///////////////////////////////////////////////////////////////////////////// |
59 | ///////////////////////////////////////////////////////////////////////////// |
61 | |
60 | |
62 | /* ------------------------------------------------------------------------- * |
61 | /* ------------------------------------------------------------------------- * |
63 | * GET PSEUDO TELETYPE - MASTER AND SLAVE * |
62 | * GET PSEUDO TELETYPE - MASTER AND SLAVE * |
… | |
… | |
241 | |
240 | |
242 | return 0; |
241 | return 0; |
243 | } |
242 | } |
244 | |
243 | |
245 | void |
244 | void |
246 | rxvt_ptytty::close_tty () |
245 | ptytty::close_tty () |
247 | { |
246 | { |
248 | if (tty < 0) |
247 | if (tty < 0) |
249 | return; |
248 | return; |
250 | |
249 | |
251 | close (tty); |
250 | close (tty); |
252 | tty = -1; |
251 | tty = -1; |
253 | } |
252 | } |
254 | |
253 | |
255 | bool |
254 | bool |
256 | rxvt_ptytty::make_controlling_tty () |
255 | ptytty::make_controlling_tty () |
257 | { |
256 | { |
258 | return control_tty (tty) >= 0; |
257 | return control_tty (tty) >= 0; |
259 | } |
258 | } |
260 | |
259 | |
261 | void |
260 | void |
262 | rxvt_ptytty::set_utf8_mode (bool on) |
261 | ptytty::set_utf8_mode (bool on) |
263 | { |
262 | { |
264 | #ifdef IUTF8 |
263 | #ifdef IUTF8 |
265 | if (pty < 0) |
264 | if (pty < 0) |
266 | return; |
265 | return; |
267 | |
266 | |
… | |
… | |
306 | gid = 0; |
305 | gid = 0; |
307 | } |
306 | } |
308 | } |
307 | } |
309 | } ttyconf; |
308 | } ttyconf; |
310 | |
309 | |
311 | rxvt_ptytty_unix::rxvt_ptytty_unix () |
310 | ptytty_unix::ptytty_unix () |
312 | { |
311 | { |
313 | name = 0; |
312 | name = 0; |
314 | #if UTMP_SUPPORT |
313 | #if UTMP_SUPPORT |
315 | cmd_pid = 0; |
314 | cmd_pid = 0; |
316 | #endif |
315 | #endif |
317 | } |
316 | } |
318 | |
317 | |
319 | rxvt_ptytty_unix::~rxvt_ptytty_unix () |
318 | ptytty_unix::~ptytty_unix () |
320 | { |
319 | { |
321 | #if UTMP_SUPPORT |
320 | #if UTMP_SUPPORT |
322 | logout (); |
321 | logout (); |
323 | #endif |
322 | #endif |
324 | put (); |
323 | put (); |
325 | } |
324 | } |
326 | |
325 | |
327 | void |
326 | void |
328 | rxvt_ptytty_unix::put () |
327 | ptytty_unix::put () |
329 | { |
328 | { |
330 | chmod (name, RESTORE_TTY_MODE); |
329 | chmod (name, RESTORE_TTY_MODE); |
331 | chown (name, 0, ttyconf.gid); |
330 | chown (name, 0, ttyconf.gid); |
332 | |
331 | |
333 | close_tty (); |
332 | close_tty (); |
… | |
… | |
340 | pty = tty = -1; |
339 | pty = tty = -1; |
341 | name = 0; |
340 | name = 0; |
342 | } |
341 | } |
343 | |
342 | |
344 | bool |
343 | bool |
345 | rxvt_ptytty_unix::get () |
344 | ptytty_unix::get () |
346 | { |
345 | { |
347 | /* get master (pty) */ |
346 | /* get master (pty) */ |
348 | if ((pty = get_pty (&tty, &name)) < 0) |
347 | if ((pty = get_pty (&tty, &name)) < 0) |
349 | return false; |
348 | return false; |
350 | |
349 | |
… | |
… | |
378 | |
377 | |
379 | struct command |
378 | struct command |
380 | { |
379 | { |
381 | enum { get, login, destroy } type; |
380 | enum { get, login, destroy } type; |
382 | |
381 | |
383 | rxvt_ptytty *id; |
382 | ptytty *id; |
384 | |
383 | |
385 | bool login_shell; |
384 | bool login_shell; |
386 | int cmd_pid; |
385 | int cmd_pid; |
387 | char hostname[512]; // arbitrary, but should be plenty |
386 | char hostname[512]; // arbitrary, but should be plenty |
388 | }; |
387 | }; |
389 | |
388 | |
390 | struct rxvt_ptytty_proxy : zero_initialized, rxvt_ptytty |
389 | struct ptytty_proxy : ptytty |
391 | { |
390 | { |
392 | rxvt_ptytty *id; |
391 | ptytty *id; |
393 | |
392 | |
|
|
393 | ~ptytty_proxy () |
|
|
394 | : id(0) |
|
|
395 | { |
|
|
396 | } |
|
|
397 | |
394 | ~rxvt_ptytty_proxy (); |
398 | ~ptytty_proxy (); |
395 | |
399 | |
396 | bool get (); |
400 | bool get (); |
397 | void login (int cmd_pid, bool login_shell, const char *hostname); |
401 | void login (int cmd_pid, bool login_shell, const char *hostname); |
398 | }; |
402 | }; |
399 | |
403 | |
400 | bool |
404 | bool |
401 | rxvt_ptytty_proxy::get () |
405 | ptytty_proxy::get () |
402 | { |
406 | { |
403 | command cmd; |
407 | command cmd; |
404 | |
408 | |
405 | cmd.type = command::get; |
409 | cmd.type = command::get; |
406 | |
410 | |
407 | write (sock_fd, &cmd, sizeof (cmd)); |
411 | write (sock_fd, &cmd, sizeof (cmd)); |
408 | |
412 | |
409 | if (read (sock_fd, &id, sizeof (id)) != sizeof (id)) |
413 | if (read (sock_fd, &id, sizeof (id)) != sizeof (id)) |
410 | rxvt_fatal ("protocol error while creating pty using helper process, aborting.\n"); |
414 | fatal ("protocol error while creating pty using helper process, aborting.\n"); |
411 | |
415 | |
412 | if (!id) |
416 | if (!id) |
413 | return false; |
417 | return false; |
414 | |
418 | |
415 | if ((pty = rxvt_recv_fd (sock_fd)) < 0 |
419 | if ((pty = ptytty_recv_fd (sock_fd)) < 0 |
416 | || (tty = rxvt_recv_fd (sock_fd)) < 0) |
420 | || (tty = ptytty_recv_fd (sock_fd)) < 0) |
417 | rxvt_fatal ("protocol error while reading pty/tty fds from helper process, aborting.\n"); |
421 | fatal ("protocol error while reading pty/tty fds from helper process, aborting.\n"); |
418 | |
422 | |
419 | return true; |
423 | return true; |
420 | } |
424 | } |
421 | |
425 | |
422 | void |
426 | void |
423 | rxvt_ptytty_proxy::login (int cmd_pid, bool login_shell, const char *hostname) |
427 | ptytty_proxy::login (int cmd_pid, bool login_shell, const char *hostname) |
424 | { |
428 | { |
425 | command cmd; |
429 | command cmd; |
426 | |
430 | |
427 | cmd.type = command::login; |
431 | cmd.type = command::login; |
428 | cmd.id = id; |
432 | cmd.id = id; |
… | |
… | |
431 | strncpy (cmd.hostname, hostname, sizeof (cmd.hostname)); |
435 | strncpy (cmd.hostname, hostname, sizeof (cmd.hostname)); |
432 | |
436 | |
433 | write (sock_fd, &cmd, sizeof (cmd)); |
437 | write (sock_fd, &cmd, sizeof (cmd)); |
434 | } |
438 | } |
435 | |
439 | |
436 | rxvt_ptytty_proxy::~rxvt_ptytty_proxy () |
440 | ptytty_proxy::~ptytty_proxy () |
437 | { |
441 | { |
|
|
442 | if (id) |
|
|
443 | { |
438 | command cmd; |
444 | command cmd; |
439 | |
445 | |
440 | cmd.type = command::destroy; |
446 | cmd.type = command::destroy; |
441 | cmd.id = id; |
447 | cmd.id = id; |
442 | |
448 | |
443 | write (sock_fd, &cmd, sizeof (cmd)); |
449 | write (sock_fd, &cmd, sizeof (cmd)); |
|
|
450 | } |
444 | } |
451 | } |
445 | |
452 | |
446 | static |
453 | static |
447 | void serve () |
454 | void serve () |
448 | { |
455 | { |
449 | command cmd; |
456 | command cmd; |
450 | vector<rxvt_ptytty *> ptys; |
457 | vector<ptytty *> ptys; |
451 | |
458 | |
452 | while (read (sock_fd, &cmd, sizeof (command)) == sizeof (command)) |
459 | while (read (sock_fd, &cmd, sizeof (command)) == sizeof (command)) |
453 | { |
460 | { |
454 | if (cmd.type == command::get) |
461 | if (cmd.type == command::get) |
455 | { |
462 | { |
456 | // -> id ptyfd ttyfd |
463 | // -> id ptyfd ttyfd |
457 | cmd.id = new rxvt_ptytty_unix; |
464 | cmd.id = new ptytty_unix; |
458 | |
465 | |
459 | if (cmd.id->get ()) |
466 | if (cmd.id->get ()) |
460 | { |
467 | { |
461 | write (sock_fd, &cmd.id, sizeof (cmd.id)); |
468 | write (sock_fd, &cmd.id, sizeof (cmd.id)); |
462 | ptys.push_back (cmd.id); |
469 | ptys.push_back (cmd.id); |
463 | |
470 | |
464 | rxvt_send_fd (sock_fd, cmd.id->pty); |
471 | ptytty_send_fd (sock_fd, cmd.id->pty); |
465 | rxvt_send_fd (sock_fd, cmd.id->tty); |
472 | ptytty_send_fd (sock_fd, cmd.id->tty); |
466 | } |
473 | } |
467 | else |
474 | else |
468 | { |
475 | { |
469 | delete cmd.id; |
476 | delete cmd.id; |
470 | cmd.id = 0; |
477 | cmd.id = 0; |
… | |
… | |
481 | } |
488 | } |
482 | #endif |
489 | #endif |
483 | } |
490 | } |
484 | else if (cmd.type == command::destroy) |
491 | else if (cmd.type == command::destroy) |
485 | { |
492 | { |
486 | rxvt_ptytty **pty = find (ptys.begin (), ptys.end (), cmd.id); |
493 | ptytty **pty = find (ptys.begin (), ptys.end (), cmd.id); |
487 | |
494 | |
488 | if (pty) |
495 | if (pty) |
489 | { |
496 | { |
490 | delete *pty; |
497 | delete *pty; |
491 | ptys.erase (pty); |
498 | ptys.erase (pty); |
… | |
… | |
494 | else |
501 | else |
495 | break; |
502 | break; |
496 | } |
503 | } |
497 | |
504 | |
498 | // destroy all ptys |
505 | // destroy all ptys |
499 | for (rxvt_ptytty **i = ptys.end (); i-- > ptys.begin (); ) |
506 | for (ptytty **i = ptys.end (); i-- > ptys.begin (); ) |
500 | delete *i; |
507 | delete *i; |
501 | } |
508 | } |
502 | |
509 | |
503 | void rxvt_ptytty_server () |
510 | void ptytty_server () |
504 | { |
511 | { |
505 | int sv[2]; |
512 | int sv[2]; |
506 | |
513 | |
507 | if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv)) |
514 | if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv)) |
508 | rxvt_fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n"); |
515 | fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n"); |
509 | |
516 | |
510 | pid = fork (); |
517 | pid = fork (); |
511 | |
518 | |
512 | if (pid < 0) |
519 | if (pid < 0) |
513 | rxvt_fatal ("could not create pty/sessiondb helper process, aborting.\n"); |
520 | fatal ("could not create pty/sessiondb helper process, aborting.\n"); |
514 | |
521 | |
515 | if (pid) |
522 | if (pid) |
516 | { |
523 | { |
517 | // client, urxvt |
524 | // client, process |
518 | sock_fd = sv[0]; |
525 | sock_fd = sv[0]; |
519 | close (sv[1]); |
526 | close (sv[1]); |
520 | fcntl (sock_fd, F_SETFD, FD_CLOEXEC); |
527 | fcntl (sock_fd, F_SETFD, FD_CLOEXEC); |
521 | } |
528 | } |
522 | else |
529 | else |
… | |
… | |
534 | } |
541 | } |
535 | |
542 | |
536 | #endif |
543 | #endif |
537 | |
544 | |
538 | // a "factory" *g* |
545 | // a "factory" *g* |
539 | rxvt_ptytty * |
546 | ptytty * |
540 | rxvt_new_ptytty () |
547 | new_ptytty () |
541 | { |
548 | { |
542 | #if PTYTTY_HELPER |
549 | #if PTYTTY_HELPER |
543 | if (pid > 0) |
550 | if (pid > 0) |
544 | // use helper process |
551 | // use helper process |
545 | return new rxvt_ptytty_proxy; |
552 | return new ptytty_proxy; |
546 | else |
553 | else |
547 | #endif |
554 | #endif |
548 | return new rxvt_ptytty_unix; |
555 | return new ptytty_unix; |
549 | } |
556 | } |
550 | |
557 | |
551 | /*----------------------- end-of-file (C source) -----------------------*/ |
558 | /*----------------------- end-of-file (C source) -----------------------*/ |
552 | |
559 | |