IO-Tty

 view release on metacpan or  search on metacpan

Tty.xs  view on Meta::CPAN

	if (print_debug)
	  fprintf(stderr, "trying ptsname_r()...\n");
#endif
	if(ptsname_r(*ptyfd, namebuf, namebuflen)) {
	    if (ckWARN(WARN_IO))
		warn("IO::Tty::open_slave(nonfatal): ptsname_r(): %.100s", strerror(errno));
	}
    }
#endif /* HAVE_PTSNAME_R */

#if defined (HAVE_PTSNAME)
    if (namebuf[0] == 0) {
	char * name;
#if PTY_DEBUG
	if (print_debug)
	  fprintf(stderr, "trying ptsname()...\n");
#endif
	name = ptsname(*ptyfd);
	if (name) {
	    if(strlcpy(namebuf, name, namebuflen) >= namebuflen) {
	      warn("ERROR: IO::Tty::open_slave: ttyname truncated");
	      close(*ptyfd);
	      *ptyfd = -1;
	      return 0;
	    }
	} else {
	    if (ckWARN(WARN_IO))
		warn("IO::Tty::open_slave(nonfatal): ptsname(): %.100s", strerror(errno));
	}
    }
#endif /* HAVE_PTSNAME */

    if (namebuf[0] == 0) {
	close(*ptyfd);
	*ptyfd = -1;
	return 0;		/* we failed to get the slave name */
    }

#if defined (__SVR4) && defined (__sun)
       #include <sys/types.h>
       #include <unistd.h>
       {
           uid_t euid = geteuid();
           uid_t uid  = getuid();

           /* root running as another user
            * grantpt() has done the wrong thing
             */
           if (euid != uid && uid == 0) {
#if PTY_DEBUG
		if (print_debug)
	  	    fprintf(stderr, "trying seteuid() from %d to %d...\n",
			euid, uid);
#endif
		if (setuid(uid)) {
		    warn("ERROR: IO::Tty::open_slave: couldn't seteuid to root: %d", errno);
		    close(*ptyfd);
		    *ptyfd = -1;
		    return 0;
		}
		if (chown(namebuf, euid, -1)) {
		    warn("ERROR: IO::Tty::open_slave: couldn't fchown the pty: %d", errno);
		    close(*ptyfd);
		    *ptyfd = -1;
		    return 0;
		}
		if (seteuid(euid)) {
		    warn("ERROR: IO::Tty::open_slave: couldn't seteuid back: %d", errno);
		    close(*ptyfd);
		    *ptyfd = -1;
		    return 0;
		}
           }
       }
#endif

    if (*ttyfd >= 0) {
      make_safe_fd(ptyfd);
      make_safe_fd(ttyfd);
      return 1;			/* we already have an open slave, so
                                   no more init is needed */
    }

    /*
     * Open the slave side.
     */
#if PTY_DEBUG
    if (print_debug)
      fprintf(stderr, "trying to open %s...\n", namebuf);
#endif

    *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
    if (*ttyfd < 0) {
      if (ckWARN(WARN_IO))
	warn("IO::Tty::open_slave(nonfatal): open(%.200s): %.100s",
	     namebuf, strerror(errno));
      close(*ptyfd);
      *ptyfd = -1;
      return 0;		/* too bad, couldn't open slave side */
    }

#if defined (I_PUSH)
    /*
     * Push appropriate streams modules for Solaris pty(7).
     * HP-UX pty(7) doesn't have ttcompat module.
     * We simply try to push all relevant modules but warn only on
     * those platforms we know these are required.
     */
#if PTY_DEBUG
    if (print_debug)
      fprintf(stderr, "trying to I_PUSH ptem...\n");
#endif
    if (ioctl(*ttyfd, I_PUSH, "ptem") < 0)
#if defined (__solaris) || defined(__hpux)
	if (ckWARN(WARN_IO))
	    warn("IO::Tty::pty_allocate: ioctl I_PUSH ptem: %.100s", strerror(errno))
#endif
	      ;

#if PTY_DEBUG
    if (print_debug)
      fprintf(stderr, "trying to I_PUSH ldterm...\n");



( run in 0.688 second using v1.01-cache-2.11-cpan-5511b514fd6 )