… | |
… | |
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 : zero_initialized, ptytty |
390 | { |
390 | { |
391 | rxvt_ptytty *id; |
391 | ptytty *id; |
392 | |
392 | |
393 | ~rxvt_ptytty_proxy (); |
393 | ~ptytty_proxy (); |
394 | |
394 | |
395 | bool get (); |
395 | bool get (); |
396 | void login (int cmd_pid, bool login_shell, const char *hostname); |
396 | void login (int cmd_pid, bool login_shell, const char *hostname); |
397 | }; |
397 | }; |
398 | |
398 | |
399 | bool |
399 | bool |
400 | rxvt_ptytty_proxy::get () |
400 | ptytty_proxy::get () |
401 | { |
401 | { |
402 | command cmd; |
402 | command cmd; |
403 | |
403 | |
404 | cmd.type = command::get; |
404 | cmd.type = command::get; |
405 | |
405 | |
406 | write (sock_fd, &cmd, sizeof (cmd)); |
406 | write (sock_fd, &cmd, sizeof (cmd)); |
407 | |
407 | |
408 | if (read (sock_fd, &id, sizeof (id)) != sizeof (id)) |
408 | if (read (sock_fd, &id, sizeof (id)) != sizeof (id)) |
409 | rxvt_fatal ("protocol error while creating pty using helper process, aborting.\n"); |
409 | fatal ("protocol error while creating pty using helper process, aborting.\n"); |
410 | |
410 | |
411 | if (!id) |
411 | if (!id) |
412 | return false; |
412 | return false; |
413 | |
413 | |
414 | if ((pty = rxvt_recv_fd (sock_fd)) < 0 |
414 | if ((pty = ptytty_recv_fd (sock_fd)) < 0 |
415 | || (tty = rxvt_recv_fd (sock_fd)) < 0) |
415 | || (tty = ptytty_recv_fd (sock_fd)) < 0) |
416 | rxvt_fatal ("protocol error while reading pty/tty fds from helper process, aborting.\n"); |
416 | fatal ("protocol error while reading pty/tty fds from helper process, aborting.\n"); |
417 | |
417 | |
418 | return true; |
418 | return true; |
419 | } |
419 | } |
420 | |
420 | |
421 | void |
421 | void |
422 | rxvt_ptytty_proxy::login (int cmd_pid, bool login_shell, const char *hostname) |
422 | ptytty_proxy::login (int cmd_pid, bool login_shell, const char *hostname) |
423 | { |
423 | { |
424 | command cmd; |
424 | command cmd; |
425 | |
425 | |
426 | cmd.type = command::login; |
426 | cmd.type = command::login; |
427 | cmd.id = id; |
427 | cmd.id = id; |
… | |
… | |
430 | strncpy (cmd.hostname, hostname, sizeof (cmd.hostname)); |
430 | strncpy (cmd.hostname, hostname, sizeof (cmd.hostname)); |
431 | |
431 | |
432 | write (sock_fd, &cmd, sizeof (cmd)); |
432 | write (sock_fd, &cmd, sizeof (cmd)); |
433 | } |
433 | } |
434 | |
434 | |
435 | rxvt_ptytty_proxy::~rxvt_ptytty_proxy () |
435 | ptytty_proxy::~ptytty_proxy () |
436 | { |
436 | { |
437 | command cmd; |
437 | command cmd; |
438 | |
438 | |
439 | cmd.type = command::destroy; |
439 | cmd.type = command::destroy; |
440 | cmd.id = id; |
440 | cmd.id = id; |
… | |
… | |
444 | |
444 | |
445 | static |
445 | static |
446 | void serve () |
446 | void serve () |
447 | { |
447 | { |
448 | command cmd; |
448 | command cmd; |
449 | vector<rxvt_ptytty *> ptys; |
449 | vector<ptytty *> ptys; |
450 | |
450 | |
451 | while (read (sock_fd, &cmd, sizeof (command)) == sizeof (command)) |
451 | while (read (sock_fd, &cmd, sizeof (command)) == sizeof (command)) |
452 | { |
452 | { |
453 | if (cmd.type == command::get) |
453 | if (cmd.type == command::get) |
454 | { |
454 | { |
455 | // -> id ptyfd ttyfd |
455 | // -> id ptyfd ttyfd |
456 | cmd.id = new rxvt_ptytty_unix; |
456 | cmd.id = new ptytty_unix; |
457 | |
457 | |
458 | if (cmd.id->get ()) |
458 | if (cmd.id->get ()) |
459 | { |
459 | { |
460 | write (sock_fd, &cmd.id, sizeof (cmd.id)); |
460 | write (sock_fd, &cmd.id, sizeof (cmd.id)); |
461 | ptys.push_back (cmd.id); |
461 | ptys.push_back (cmd.id); |
462 | |
462 | |
463 | rxvt_send_fd (sock_fd, cmd.id->pty); |
463 | ptytty_send_fd (sock_fd, cmd.id->pty); |
464 | rxvt_send_fd (sock_fd, cmd.id->tty); |
464 | ptytty_send_fd (sock_fd, cmd.id->tty); |
465 | } |
465 | } |
466 | else |
466 | else |
467 | { |
467 | { |
468 | delete cmd.id; |
468 | delete cmd.id; |
469 | cmd.id = 0; |
469 | cmd.id = 0; |
… | |
… | |
480 | } |
480 | } |
481 | #endif |
481 | #endif |
482 | } |
482 | } |
483 | else if (cmd.type == command::destroy) |
483 | else if (cmd.type == command::destroy) |
484 | { |
484 | { |
485 | rxvt_ptytty **pty = find (ptys.begin (), ptys.end (), cmd.id); |
485 | ptytty **pty = find (ptys.begin (), ptys.end (), cmd.id); |
486 | |
486 | |
487 | if (pty) |
487 | if (pty) |
488 | { |
488 | { |
489 | delete *pty; |
489 | delete *pty; |
490 | ptys.erase (pty); |
490 | ptys.erase (pty); |
… | |
… | |
493 | else |
493 | else |
494 | break; |
494 | break; |
495 | } |
495 | } |
496 | |
496 | |
497 | // destroy all ptys |
497 | // destroy all ptys |
498 | for (rxvt_ptytty **i = ptys.end (); i-- > ptys.begin (); ) |
498 | for (ptytty **i = ptys.end (); i-- > ptys.begin (); ) |
499 | delete *i; |
499 | delete *i; |
500 | } |
500 | } |
501 | |
501 | |
502 | void rxvt_ptytty_server () |
502 | void ptytty_server () |
503 | { |
503 | { |
504 | int sv[2]; |
504 | int sv[2]; |
505 | |
505 | |
506 | if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv)) |
506 | if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv)) |
507 | rxvt_fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n"); |
507 | fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n"); |
508 | |
508 | |
509 | pid = fork (); |
509 | pid = fork (); |
510 | |
510 | |
511 | if (pid < 0) |
511 | if (pid < 0) |
512 | rxvt_fatal ("could not create pty/sessiondb helper process, aborting.\n"); |
512 | fatal ("could not create pty/sessiondb helper process, aborting.\n"); |
513 | |
513 | |
514 | if (pid) |
514 | if (pid) |
515 | { |
515 | { |
516 | // client, urxvt |
516 | // client, urxvt |
517 | sock_fd = sv[0]; |
517 | sock_fd = sv[0]; |
… | |
… | |
533 | } |
533 | } |
534 | |
534 | |
535 | #endif |
535 | #endif |
536 | |
536 | |
537 | // a "factory" *g* |
537 | // a "factory" *g* |
538 | rxvt_ptytty * |
538 | ptytty * |
539 | rxvt_new_ptytty () |
539 | new_ptytty () |
540 | { |
540 | { |
541 | #if PTYTTY_HELPER |
541 | #if PTYTTY_HELPER |
542 | if (pid > 0) |
542 | if (pid > 0) |
543 | // use helper process |
543 | // use helper process |
544 | return new rxvt_ptytty_proxy; |
544 | return new ptytty_proxy; |
545 | else |
545 | else |
546 | #endif |
546 | #endif |
547 | return new rxvt_ptytty_unix; |
547 | return new ptytty_unix; |
548 | } |
548 | } |
549 | |
549 | |
550 | /*----------------------- end-of-file (C source) -----------------------*/ |
550 | /*----------------------- end-of-file (C source) -----------------------*/ |
551 | |
551 | |