ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/ptytty.C
(Generate patch)

Comparing rxvt-unicode/src/ptytty.C (file contents):
Revision 1.33 by root, Wed Jan 11 23:08:54 2006 UTC vs.
Revision 1.39 by root, Tue Jan 17 12:22:59 2006 UTC

54#endif 54#endif
55 55
56#include <cstdio> 56#include <cstdio>
57#include <grp.h> 57#include <grp.h>
58 58
59#include "ptytty.h"
60
61/////////////////////////////////////////////////////////////////////////////
62
59/* ------------------------------------------------------------------------- * 63/* ------------------------------------------------------------------------- *
60 * GET PSEUDO TELETYPE - MASTER AND SLAVE * 64 * GET PSEUDO TELETYPE - MASTER AND SLAVE *
61 * ------------------------------------------------------------------------- */ 65 * ------------------------------------------------------------------------- */
62/* 66/*
63 * Returns pty file descriptor, or -1 on failure 67 * Returns pty file descriptor, or -1 on failure
88 if (*ttydev != NULL) 92 if (*ttydev != NULL)
89 return pfd; 93 return pfd;
90#endif 94#endif
91 95
92#if defined(HAVE_GRANTPT) && defined(HAVE_UNLOCKPT) 96#if defined(HAVE_GRANTPT) && defined(HAVE_UNLOCKPT)
93# if defined(PTYS_ARE_POSIX) || defined(PTYS_ARE_PTMX) 97# if defined(PTYS_ARE_GETPT) || defined(PTYS_ARE_POSIX) || defined(PTYS_ARE_PTMX)
94 98
95 { 99 {
100# ifdef PTYS_ARE_GETPT
101 pfd = getpt();
102# else
96# ifdef PTYS_ARE_POSIX 103# ifdef PTYS_ARE_POSIX
97 pfd = posix_openpt (O_RDWR); 104 pfd = posix_openpt (O_RDWR);
98# else 105# else
99 pfd = open ("/dev/ptmx", O_RDWR | O_NOCTTY, 0); 106 pfd = open ("/dev/ptmx", O_RDWR | O_NOCTTY, 0);
107# endif
100# endif 108# endif
101 109
102 if (pfd >= 0) 110 if (pfd >= 0)
103 { 111 {
104 if (grantpt (pfd) == 0 /* change slave permissions */ 112 if (grantpt (pfd) == 0 /* change slave permissions */
214/*----------------------------------------------------------------------*/ 222/*----------------------------------------------------------------------*/
215/* 223/*
216 * Make our tty a controlling tty so that /dev/tty points to us 224 * Make our tty a controlling tty so that /dev/tty points to us
217 */ 225 */
218static int 226static int
219control_tty (int fd_tty, const char *ttydev) 227control_tty (int fd_tty)
220{ 228{
221#ifndef __QNX__
222 int fd; 229 int fd;
223 230
224 /* ---------------------------------------- */ 231 /* ---------------------------------------- */
225# ifdef HAVE_SETSID
226 setsid (); 232 setsid ();
227# endif
228# if defined(HAVE_SETPGID)
229 setpgid (0, 0);
230# elif defined(HAVE_SETPGRP)
231 setpgrp (0, 0);
232# endif
233 233
234 /* ---------------------------------------- */ 234 /* ---------------------------------------- */
235# ifdef TIOCNOTTY
236 fd = open ("/dev/tty", O_RDWR | O_NOCTTY);
237 if (fd >= 0)
238 {
239 ioctl (fd, TIOCNOTTY, NULL); /* void tty associations */
240 close (fd);
241 }
242# endif
243
244 /* ---------------------------------------- */
245 fd = open ("/dev/tty", O_RDWR | O_NOCTTY);
246 if (fd >= 0)
247 close (fd); /* ouch: still have controlling tty */
248
249 /* ---------------------------------------- */
250#if defined(PTYS_ARE_PTMX) && defined(I_PUSH) 235# if defined(PTYS_ARE_PTMX) && defined(I_PUSH)
251 /* 236 /*
252 * Push STREAMS modules: 237 * Push STREAMS modules:
253 * ptem: pseudo-terminal hardware emulation module. 238 * ptem: pseudo-terminal hardware emulation module.
254 * ldterm: standard terminal line discipline. 239 * ldterm: standard terminal line discipline.
255 * ttcompat: V7, 4BSD and XENIX STREAMS compatibility module. 240 * ttcompat: V7, 4BSD and XENIX STREAMS compatibility module.
263 * documentation is really unclear about whether it is any close () on 248 * documentation is really unclear about whether it is any close () on
264 * the master side or the last close () - i.e. a proper STREAMS dismantling 249 * the master side or the last close () - i.e. a proper STREAMS dismantling
265 * close () - on the master side which causes a hang up to be sent 250 * close () - on the master side which causes a hang up to be sent
266 * through - Geoff Wing 251 * through - Geoff Wing
267 */ 252 */
268# ifdef HAVE_ISASTREAM 253# ifdef HAVE_ISASTREAM
269 if (isastream (fd_tty) == 1) 254 if (isastream (fd_tty) == 1)
270# endif 255# endif
271 { 256 {
272 ioctl (fd_tty, I_PUSH, "ptem"); 257 ioctl (fd_tty, I_PUSH, "ptem");
273 ioctl (fd_tty, I_PUSH, "ldterm"); 258 ioctl (fd_tty, I_PUSH, "ldterm");
274 ioctl (fd_tty, I_PUSH, "ttcompat"); 259 ioctl (fd_tty, I_PUSH, "ttcompat");
275 } 260 }
276#endif 261# endif
277 /* ---------------------------------------- */ 262 /* ---------------------------------------- */
278# if defined(TIOCSCTTY)
279 fd = ioctl (fd_tty, TIOCSCTTY, NULL); 263 fd = ioctl (fd_tty, TIOCSCTTY, NULL);
280# elif defined(TIOCSETCTTY)
281 fd = ioctl (fd_tty, TIOCSETCTTY, NULL);
282# else
283 fd = open (ttydev, O_RDWR);
284 if (fd >= 0)
285 close (fd);
286# endif
287 /* ---------------------------------------- */ 264 /* ---------------------------------------- */
288 fd = open ("/dev/tty", O_WRONLY); 265 fd = open ("/dev/tty", O_WRONLY);
289 if (fd < 0) 266 if (fd < 0)
290 return -1; /* fatal */ 267 return -1; /* fatal */
291 close (fd); 268 close (fd);
292 /* ---------------------------------------- */ 269 /* ---------------------------------------- */
293#endif /* ! __QNX__ */
294 270
295 return 0; 271 return 0;
296} 272}
273
274void
275rxvt_ptytty::close_tty ()
276{
277 if (tty < 0)
278 return;
279
280 close (tty);
281 tty = -1;
282}
283
284bool
285rxvt_ptytty::make_controlling_tty ()
286{
287 return control_tty (tty) >= 0;
288}
289
290void
291rxvt_ptytty::set_utf8_mode (bool on)
292{
293#ifdef IUTF8
294 if (pty < 0)
295 return;
296
297 struct termios tio;
298
299 if (tcgetattr (pty, &tio) != -1)
300 {
301 tcflag_t new_cflag = tio.c_iflag;
302
303 if (on)
304 new_cflag |= IUTF8;
305 else
306 new_cflag &= ~IUTF8;
307
308 if (new_cflag != tio.c_iflag)
309 {
310 tio.c_iflag = new_cflag;
311 tcsetattr (pty, TCSANOW, &tio);
312 }
313 }
314#endif
315}
316
317/////////////////////////////////////////////////////////////////////////////
297 318
298#ifndef NO_SETOWNER_TTYDEV 319#ifndef NO_SETOWNER_TTYDEV
299static struct ttyconf { 320static struct ttyconf {
300 gid_t gid; 321 gid_t gid;
301 mode_t mode; 322 mode_t mode;
317 gid = getgid (); 338 gid = getgid ();
318 } 339 }
319 } 340 }
320} ttyconf; 341} ttyconf;
321 342
343/////////////////////////////////////////////////////////////////////////////
344
322void 345void
323rxvt_ptytty::privileges (rxvt_privaction action) 346rxvt_ptytty_unix::privileges (rxvt_privaction action)
324{ 347{
325 if (!name || !*name) 348 if (!name || !*name)
326 return; 349 return;
327 350
328 rxvt_privileges (RESTORE); 351 rxvt_privileges (RESTORE);
361 384
362 rxvt_privileges (IGNORE); 385 rxvt_privileges (IGNORE);
363} 386}
364#endif 387#endif
365 388
366rxvt_ptytty::rxvt_ptytty () 389rxvt_ptytty_unix::rxvt_ptytty_unix ()
367{ 390{
368 pty = tty = -1; 391 pty = tty = -1;
369 name = 0; 392 name = 0;
370#ifndef NO_SETOWNER_TTYDEV 393#ifndef NO_SETOWNER_TTYDEV
371 saved = false; 394 saved = false;
372#endif 395#endif
396#if UTMP_SUPPORT
397 cmd_pid = 0;
398#endif
373} 399}
374 400
375rxvt_ptytty::~rxvt_ptytty () 401rxvt_ptytty_unix::~rxvt_ptytty_unix ()
376{ 402{
403 logout ();
377 put (); 404 put ();
378} 405}
379 406
380void 407void
381
382rxvt_ptytty::close_tty ()
383{
384 if (tty < 0)
385 return;
386
387 close (tty);
388 tty = -1;
389}
390
391void
392rxvt_ptytty::put () 408rxvt_ptytty_unix::put ()
393{ 409{
394#ifndef NO_SETOWNER_TTYDEV 410#ifndef NO_SETOWNER_TTYDEV
395 privileges (RESTORE); 411 privileges (RESTORE);
396#endif 412#endif
397 413
402 pty = tty = -1; 418 pty = tty = -1;
403 name = 0; 419 name = 0;
404} 420}
405 421
406bool 422bool
407rxvt_ptytty::make_controlling_tty ()
408{
409 return control_tty (tty, name) >= 0;
410}
411
412bool
413rxvt_ptytty::get () 423rxvt_ptytty_unix::get ()
414{ 424{
415 /* get master (pty) */ 425 /* get master (pty) */
416 if ((pty = get_pty (&tty, &name)) < 0) 426 if ((pty = get_pty (&tty, &name)) < 0)
417 return false; 427 return false;
418 428
433 } 443 }
434 444
435 return true; 445 return true;
436} 446}
437 447
438void 448// a "factory" *g*
439rxvt_ptytty::set_utf8_mode (bool on) 449rxvt_ptytty *rxvt_new_ptytty ()
440{ 450{
441#ifdef IUTF8 451 return new rxvt_ptytty_unix;
442 if (pty < 0)
443 return;
444
445 struct termios tio;
446
447 if (tcgetattr (pty, &tio) != -1)
448 {
449 tcflag_t new_cflag = tio.c_iflag;
450
451 if (on)
452 new_cflag |= IUTF8;
453 else
454 new_cflag &= ~IUTF8;
455
456 if (new_cflag != tio.c_iflag)
457 {
458 tio.c_iflag = new_cflag;
459 tcsetattr (pty, TCSANOW, &tio);
460 }
461 }
462#endif
463} 452}
464 453
465/*----------------------- end-of-file (C source) -----------------------*/ 454/*----------------------- end-of-file (C source) -----------------------*/
466 455

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines