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