… | |
… | |
2 | * File: ptytty.C |
2 | * File: ptytty.C |
3 | *----------------------------------------------------------------------* |
3 | *----------------------------------------------------------------------* |
4 | * |
4 | * |
5 | * All portions of code are copyright by their respective author/s. |
5 | * All portions of code are copyright by their respective author/s. |
6 | * Copyright (c) 1999-2001 Geoff Wing <gcw@pobox.com> |
6 | * Copyright (c) 1999-2001 Geoff Wing <gcw@pobox.com> |
7 | * Copyright (c) 2004 Marc Lehmann <pcg@goof.com> |
7 | * Copyright (c) 2004-2006 Marc Lehmann <pcg@goof.com> |
8 | * |
8 | * |
9 | * This program is free software; you can redistribute it and/or modify |
9 | * This program is free software; you can redistribute it and/or modify |
10 | * it under the terms of the GNU General Public License as published by |
10 | * it under the terms of the GNU General Public License as published by |
11 | * the Free Software Foundation; either version 2 of the License, or |
11 | * the Free Software Foundation; either version 2 of the License, or |
12 | * (at your option) any later version. |
12 | * (at your option) any later version. |
… | |
… | |
46 | # include <sys/stropts.h> /* for I_PUSH */ |
46 | # include <sys/stropts.h> /* for I_PUSH */ |
47 | #endif |
47 | #endif |
48 | #ifdef HAVE_ISASTREAM |
48 | #ifdef HAVE_ISASTREAM |
49 | # include <stropts.h> |
49 | # include <stropts.h> |
50 | #endif |
50 | #endif |
|
|
51 | #ifdef HAVE_PTY_H |
|
|
52 | # include <pty.h> |
|
|
53 | #endif |
51 | |
54 | |
52 | // better do this via configure, but.... |
55 | // better do this via configure, but.... |
53 | #if defined(__FreeBSD__) |
56 | #if defined(__FreeBSD__) |
54 | # include <libutil.h> |
57 | # include <libutil.h> |
55 | #elif defined(__DARWIN__) |
58 | #elif defined(__DARWIN__) || (defined (__MACH__) && defined (__APPLE__)) |
56 | # include <util.h> |
59 | # include <util.h> |
57 | #endif |
60 | #endif |
58 | |
61 | |
59 | #include <cstdio> |
62 | #include <cstdio> |
60 | #include <grp.h> |
63 | #include <grp.h> |
… | |
… | |
73 | int pfd; |
76 | int pfd; |
74 | |
77 | |
75 | #ifdef PTYS_ARE_OPENPTY |
78 | #ifdef PTYS_ARE_OPENPTY |
76 | char tty_name[sizeof "/dev/pts/????\0"]; |
79 | char tty_name[sizeof "/dev/pts/????\0"]; |
77 | |
80 | |
|
|
81 | rxvt_privileges(RESTORE); |
78 | if (openpty (&pfd, fd_tty, tty_name, NULL, NULL) != -1) |
82 | int res = openpty (&pfd, fd_tty, tty_name, NULL, NULL); |
|
|
83 | rxvt_privileges(IGNORE); |
|
|
84 | |
|
|
85 | if (res != -1) |
79 | { |
86 | { |
80 | *ttydev = strdup (tty_name); |
87 | *ttydev = strdup (tty_name); |
81 | return pfd; |
88 | return pfd; |
82 | } |
89 | } |
83 | #endif |
90 | #endif |
… | |
… | |
87 | if (*ttydev != NULL) |
94 | if (*ttydev != NULL) |
88 | return pfd; |
95 | return pfd; |
89 | #endif |
96 | #endif |
90 | |
97 | |
91 | #ifdef PTYS_ARE_GETPTY |
98 | #ifdef PTYS_ARE_GETPTY |
92 | |
|
|
93 | char *ptydev; |
99 | char *ptydev; |
94 | |
100 | |
95 | while ((ptydev = getpty ()) != NULL) |
101 | while ((ptydev = getpty ()) != NULL) |
96 | if ((pfd = open (ptydev, O_RDWR | O_NOCTTY, 0)) >= 0) |
102 | if ((pfd = open (ptydev, O_RDWR | O_NOCTTY, 0)) >= 0) |
97 | { |
103 | { |
… | |
… | |
331 | } ttyconf; |
337 | } ttyconf; |
332 | |
338 | |
333 | void |
339 | void |
334 | rxvt_ptytty::privileges (rxvt_privaction action) |
340 | rxvt_ptytty::privileges (rxvt_privaction action) |
335 | { |
341 | { |
|
|
342 | if (!name || !*name) |
|
|
343 | return; |
|
|
344 | |
336 | rxvt_privileges (RESTORE); |
345 | rxvt_privileges (RESTORE); |
337 | |
346 | |
338 | if (action == SAVE) |
347 | if (action == SAVE) |
339 | { |
348 | { |
340 | //next_tty_action = RESTORE; |
|
|
341 | # ifndef RESET_TTY_TO_COMMON_DEFAULTS |
349 | # ifndef RESET_TTY_TO_COMMON_DEFAULTS |
342 | /* store original tty status for restoration rxvt_clean_exit () -- rgg 04/12/95 */ |
350 | /* store original tty status for restoration rxvt_clean_exit () -- rgg 04/12/95 */ |
343 | if (lstat (name, &savestat) < 0) /* you lose out */ |
351 | if (lstat (name, &savestat) < 0) /* you lose out */ |
344 | ;//next_tty_action = IGNORE; |
352 | ; |
345 | else |
353 | else |
346 | # endif |
354 | # endif |
347 | |
|
|
348 | { |
355 | { |
|
|
356 | saved = true; |
349 | chown (name, getuid (), ttyconf.gid); /* fail silently */ |
357 | chown (name, getuid (), ttyconf.gid); /* fail silently */ |
350 | chmod (name, ttyconf.mode); |
358 | chmod (name, ttyconf.mode); |
351 | # ifdef HAVE_REVOKE |
359 | # ifdef HAVE_REVOKE |
352 | revoke (name); |
360 | revoke (name); |
353 | # endif |
361 | # endif |
354 | |
|
|
355 | } |
362 | } |
356 | } |
363 | } |
357 | else |
364 | else |
358 | { /* action == RESTORE */ |
365 | { /* action == RESTORE */ |
359 | //next_tty_action = IGNORE; |
|
|
360 | # ifndef RESET_TTY_TO_COMMON_DEFAULTS |
366 | # ifndef RESET_TTY_TO_COMMON_DEFAULTS |
|
|
367 | if (saved) |
|
|
368 | { |
361 | chmod (name, savestat.st_mode); |
369 | chmod (name, savestat.st_mode); |
362 | chown (name, savestat.st_uid, savestat.st_gid); |
370 | chown (name, savestat.st_uid, savestat.st_gid); |
|
|
371 | } |
363 | # else |
372 | # else |
364 | chmod (name, (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)); |
373 | chmod (name, (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)); |
365 | chown (name, 0, 0); |
374 | chown (name, 0, 0); |
366 | # endif |
375 | # endif |
367 | |
376 | |
… | |
… | |
373 | |
382 | |
374 | rxvt_ptytty::rxvt_ptytty () |
383 | rxvt_ptytty::rxvt_ptytty () |
375 | { |
384 | { |
376 | pty = tty = -1; |
385 | pty = tty = -1; |
377 | name = 0; |
386 | name = 0; |
|
|
387 | #ifndef NO_SETOWNER_TTYDEV |
|
|
388 | saved = false; |
|
|
389 | #endif |
378 | } |
390 | } |
379 | |
391 | |
380 | rxvt_ptytty::~rxvt_ptytty () |
392 | rxvt_ptytty::~rxvt_ptytty () |
381 | { |
393 | { |
382 | put (); |
394 | put (); |
… | |
… | |
384 | |
396 | |
385 | void |
397 | void |
386 | |
398 | |
387 | rxvt_ptytty::close_tty () |
399 | rxvt_ptytty::close_tty () |
388 | { |
400 | { |
389 | if (tty >= 0) close (tty); |
401 | if (tty < 0) |
|
|
402 | return; |
|
|
403 | |
|
|
404 | close (tty); |
390 | tty = -1; |
405 | tty = -1; |
391 | } |
406 | } |
392 | |
407 | |
393 | void |
408 | void |
394 | rxvt_ptytty::put () |
409 | rxvt_ptytty::put () |
395 | { |
410 | { |
396 | #ifndef NO_SETOWNER_TTYDEV |
411 | #ifndef NO_SETOWNER_TTYDEV |
397 | if (tty >= 0) |
|
|
398 | privileges (RESTORE); |
412 | privileges (RESTORE); |
399 | #endif |
413 | #endif |
400 | |
414 | |
401 | if (pty >= 0) close (pty); |
415 | if (pty >= 0) close (pty); |
402 | close_tty (); |
416 | close_tty (); |
403 | free (name); |
417 | free (name); |
… | |
… | |
436 | } |
450 | } |
437 | |
451 | |
438 | return true; |
452 | return true; |
439 | } |
453 | } |
440 | |
454 | |
|
|
455 | void |
|
|
456 | rxvt_ptytty::set_utf8_mode (bool on) |
|
|
457 | { |
|
|
458 | #ifdef IUTF8 |
|
|
459 | if (pty < 0) |
|
|
460 | return; |
|
|
461 | |
|
|
462 | struct termios tio; |
|
|
463 | |
|
|
464 | if (tcgetattr (pty, &tio) != -1) |
|
|
465 | { |
|
|
466 | tcflag_t new_cflag = tio.c_iflag; |
|
|
467 | |
|
|
468 | if (on) |
|
|
469 | new_cflag |= IUTF8; |
|
|
470 | else |
|
|
471 | new_cflag &= ~IUTF8; |
|
|
472 | |
|
|
473 | if (new_cflag != tio.c_iflag) |
|
|
474 | { |
|
|
475 | tio.c_iflag = new_cflag; |
|
|
476 | tcsetattr (pty, TCSANOW, &tio); |
|
|
477 | } |
|
|
478 | } |
|
|
479 | #endif |
|
|
480 | } |
|
|
481 | |
441 | /*----------------------- end-of-file (C source) -----------------------*/ |
482 | /*----------------------- end-of-file (C source) -----------------------*/ |
442 | |
483 | |