… | |
… | |
67 | * fd_tty _may_ also be set to an open fd to the slave device |
67 | * fd_tty _may_ also be set to an open fd to the slave device |
68 | */ |
68 | */ |
69 | static inline int |
69 | static inline int |
70 | get_pty_streams (int *fd_tty, char **ttydev) |
70 | get_pty_streams (int *fd_tty, char **ttydev) |
71 | { |
71 | { |
72 | #ifdef NO_SETOWNER_TTYDEV |
72 | #if defined(HAVE_GRANTPT) && defined(HAVE_UNLOCKPT) |
|
|
73 | # if defined(PTYS_ARE_GETPT) || defined(PTYS_ARE_POSIX) || defined(PTYS_ARE_PTMX) |
73 | int pfd; |
74 | int pfd; |
74 | |
75 | |
75 | # if defined(PTYS_ARE_GETPT) |
76 | # if defined(PTYS_ARE_GETPT) |
76 | pfd = getpt(); |
77 | pfd = getpt(); |
77 | # elif defined(PTYS_ARE_POSIX) |
78 | # elif defined(PTYS_ARE_POSIX) |
… | |
… | |
88 | return pfd; |
89 | return pfd; |
89 | } |
90 | } |
90 | |
91 | |
91 | close (pfd); |
92 | close (pfd); |
92 | } |
93 | } |
|
|
94 | # endif |
93 | #endif |
95 | #endif |
94 | |
96 | |
95 | return -1; |
97 | return -1; |
96 | } |
98 | } |
97 | |
99 | |
… | |
… | |
99 | get_pty_openpty (int *fd_tty, char **ttydev) |
101 | get_pty_openpty (int *fd_tty, char **ttydev) |
100 | { |
102 | { |
101 | #ifdef PTYS_ARE_OPENPTY |
103 | #ifdef PTYS_ARE_OPENPTY |
102 | int pfd; |
104 | int pfd; |
103 | int res; |
105 | int res; |
104 | char tty_name[sizeof "/dev/pts/????\0"]; |
106 | char tty_name[32]; |
105 | |
107 | |
106 | res = openpty (&pfd, fd_tty, tty_name, NULL, NULL); |
108 | res = openpty (&pfd, fd_tty, tty_name, NULL, NULL); |
107 | if (res != -1) |
109 | if (res != -1) |
108 | { |
110 | { |
109 | *ttydev = strdup (tty_name); |
111 | *ttydev = strdup (tty_name); |
… | |
… | |
358 | } |
360 | } |
359 | } |
361 | } |
360 | #endif |
362 | #endif |
361 | } |
363 | } |
362 | |
364 | |
363 | ///////////////////////////////////////////////////////////////////////////// |
|
|
364 | |
|
|
365 | #ifndef NO_SETOWNER_TTYDEV |
|
|
366 | static struct ttyconf { |
365 | static struct ttyconf { |
367 | gid_t gid; |
366 | gid_t gid; |
368 | mode_t mode; |
367 | mode_t mode; |
369 | |
368 | |
370 | ttyconf () |
369 | ttyconf () |
… | |
… | |
384 | gid = 0; |
383 | gid = 0; |
385 | } |
384 | } |
386 | } |
385 | } |
387 | } ttyconf; |
386 | } ttyconf; |
388 | |
387 | |
389 | ///////////////////////////////////////////////////////////////////////////// |
388 | rxvt_ptytty_unix::rxvt_ptytty_unix () |
|
|
389 | { |
|
|
390 | pty = tty = -1; |
|
|
391 | name = 0; |
|
|
392 | #if UTMP_SUPPORT |
|
|
393 | cmd_pid = 0; |
|
|
394 | #endif |
|
|
395 | } |
|
|
396 | |
|
|
397 | rxvt_ptytty_unix::~rxvt_ptytty_unix () |
|
|
398 | { |
|
|
399 | #if UTMP_SUPPORT |
|
|
400 | logout (); |
|
|
401 | #endif |
|
|
402 | put (); |
|
|
403 | } |
390 | |
404 | |
391 | void |
405 | void |
392 | rxvt_ptytty_unix::privileges (rxvt_privaction action) |
406 | rxvt_ptytty_unix::put () |
393 | { |
407 | { |
394 | if (!name || !*name) |
408 | chmod (name, RESTORE_TTY_MODE); |
|
|
409 | chown (name, 0, ttyconf.gid); |
|
|
410 | |
|
|
411 | close_tty (); |
|
|
412 | |
|
|
413 | if (pty >= 0) |
|
|
414 | close (pty); |
|
|
415 | |
|
|
416 | free (name); |
|
|
417 | |
|
|
418 | pty = tty = -1; |
|
|
419 | name = 0; |
|
|
420 | } |
|
|
421 | |
|
|
422 | bool |
|
|
423 | rxvt_ptytty_unix::get () |
|
|
424 | { |
|
|
425 | /* get master (pty) */ |
|
|
426 | if ((pty = get_pty (&tty, &name)) < 0) |
395 | return; |
427 | return false; |
396 | |
428 | |
397 | if (action == SAVE) |
429 | fcntl (pty, F_SETFL, O_NONBLOCK); |
|
|
430 | |
|
|
431 | /* get slave (tty) */ |
|
|
432 | if (tty < 0) |
398 | { |
433 | { |
|
|
434 | #ifndef NO_SETOWNER_TTYDEV |
399 | chown (name, getuid (), ttyconf.gid); /* fail silently */ |
435 | chown (name, getuid (), ttyconf.gid); /* fail silently */ |
400 | chmod (name, ttyconf.mode); |
436 | chmod (name, ttyconf.mode); |
401 | # ifdef HAVE_REVOKE |
437 | # ifdef HAVE_REVOKE |
402 | revoke (name); |
438 | revoke (name); |
403 | # endif |
439 | # endif |
404 | } |
|
|
405 | else |
|
|
406 | { /* action == RESTORE */ |
|
|
407 | chmod (name, RESTORE_TTY_MODE); |
|
|
408 | chown (name, 0, ttyconf.gid); |
|
|
409 | } |
|
|
410 | } |
|
|
411 | #endif |
|
|
412 | |
|
|
413 | rxvt_ptytty_unix::rxvt_ptytty_unix () |
|
|
414 | { |
|
|
415 | pty = tty = -1; |
|
|
416 | name = 0; |
|
|
417 | #if UTMP_SUPPORT |
|
|
418 | cmd_pid = 0; |
|
|
419 | #endif |
|
|
420 | } |
|
|
421 | |
|
|
422 | rxvt_ptytty_unix::~rxvt_ptytty_unix () |
|
|
423 | { |
|
|
424 | #if UTMP_SUPPORT |
|
|
425 | logout (); |
|
|
426 | #endif |
|
|
427 | put (); |
|
|
428 | } |
|
|
429 | |
|
|
430 | void |
|
|
431 | rxvt_ptytty_unix::put () |
|
|
432 | { |
|
|
433 | #ifndef NO_SETOWNER_TTYDEV |
|
|
434 | privileges (RESTORE); |
|
|
435 | #endif |
|
|
436 | |
|
|
437 | if (pty >= 0) close (pty); |
|
|
438 | close_tty (); |
|
|
439 | free (name); |
|
|
440 | |
|
|
441 | pty = tty = -1; |
|
|
442 | name = 0; |
|
|
443 | } |
|
|
444 | |
|
|
445 | bool |
|
|
446 | rxvt_ptytty_unix::get () |
|
|
447 | { |
|
|
448 | /* get master (pty) */ |
|
|
449 | if ((pty = get_pty (&tty, &name)) < 0) |
|
|
450 | return false; |
|
|
451 | |
|
|
452 | fcntl (pty, F_SETFL, O_NONBLOCK); |
|
|
453 | |
|
|
454 | /* get slave (tty) */ |
|
|
455 | if (tty < 0) |
|
|
456 | { |
|
|
457 | #ifndef NO_SETOWNER_TTYDEV |
|
|
458 | privileges (SAVE); |
|
|
459 | #endif |
440 | #endif |
460 | |
441 | |
461 | if ((tty = get_tty (name)) < 0) |
442 | if ((tty = get_tty (name)) < 0) |
462 | { |
443 | { |
463 | put (); |
444 | put (); |
… | |
… | |
627 | |
608 | |
628 | serve (); |
609 | serve (); |
629 | _exit (EXIT_SUCCESS); |
610 | _exit (EXIT_SUCCESS); |
630 | } |
611 | } |
631 | } |
612 | } |
|
|
613 | |
632 | #endif |
614 | #endif |
633 | |
615 | |
634 | // a "factory" *g* |
616 | // a "factory" *g* |
635 | rxvt_ptytty * |
617 | rxvt_ptytty * |
636 | rxvt_new_ptytty () |
618 | rxvt_new_ptytty () |