static int ptym_open(char *pts_name) { char *ptr; int fdm; strcpy(pts_name, "/dev/ptmx"); if ((fdm = open(pts_name, O_RDWR)) < 0) return(-1); if (grantpt(fdm) < 0) { close(fdm); return(-1); } if (unlockpt(fdm) < 0) { close(fdm); return(-1); } if ((ptr = ptsname(fdm)) == NULL) { close(fdm); return(-1); } strcpy(pts_name, ptr); return(fdm); }
int ptym_open(char *pts_name, int pts_namesz) { char *ptr; int fdm, err; if ((fdm = posix_openpt(O_RDWR)) < 0) return(-1); if (grantpt(fdm) < 0) /* grant access to slave */ goto errout; if (unlockpt(fdm) < 0) /* clear slave's lock flag */ goto errout; if ((ptr = ptsname(fdm)) == NULL) /* get slave's name */ goto errout; /* * Return name of slave. Null terminate to handle * case where strlen(ptr) > pts_namesz. */ strncpy(pts_name, ptr, pts_namesz); pts_name[pts_namesz - 1] = '\0'; return(fdm); /* return fd of master */ errout: err = errno; close(fdm); errno = err; return(-1); }
void handle_signals_resets_terminal(void) { int status, masterfd; char* slavedevice = NULL; struct termios test_flags; pid_t child_pid; masterfd = posix_openpt(O_RDWR|O_NOCTTY); if (masterfd == -1 || grantpt (masterfd) == -1 || unlockpt (masterfd) == -1 || (slavedevice = ptsname (masterfd)) == NULL) CU_FAIL_FATAL("Could not create pty"); terminal_fildes = open(slavedevice, O_RDWR|O_NOCTTY); tcgetattr(terminal_fildes, &orig_flags); new_flags = orig_flags; new_flags.c_lflag &= ~ECHO; tcsetattr(terminal_fildes, TCSANOW, &new_flags); terminal_needs_reset = 1; if((child_pid = fork()) == 0) { freerdp_handle_signals(); raise(SIGINT); } while(wait(&status) != -1); tcgetattr(terminal_fildes, &test_flags); CU_ASSERT_EQUAL(orig_flags.c_lflag, test_flags.c_lflag); close(masterfd); close(terminal_fildes); }
int ptym_open(char *pts_name, char *pts_name_s , int pts_namesz) { char *ptr; int fdm; strncpy(pts_name, "/dev/ptmx", pts_namesz); pts_name[pts_namesz - 1] = '\0'; fdm = posix_openpt(O_RDWR | O_NONBLOCK); if (fdm < 0) return(-1); if (grantpt(fdm) < 0) { close(fdm); return(-2); } if (unlockpt(fdm) < 0) { close(fdm); return(-3); } if ((ptr = ptsname(fdm)) == NULL) { close(fdm); return(-4); } strncpy(pts_name_s, ptr, pts_namesz); pts_name[pts_namesz - 1] = '\0'; return(fdm); }
// We would like to use "openpty", but it isn't in libc on some systems. static bool createPseudoTty(int& masterFd, int& slaveFd, char *ttyname) { // try opening Unix98 pseudo tty if ((masterFd = ::open("/dev/ptmx", O_RDWR | O_NONBLOCK, 0)) >= 0) { if (grantpt(masterFd) == 0) { if (unlockpt(masterFd) == 0) { ptsname_r(masterFd, ttyname, BUFSIZ); if ((slaveFd = ::open(ttyname, O_RDWR | O_NOCTTY, 0)) >= 0) return true; } } ::close(masterFd); } // opening Unix98 pseudo tty failed, fall back to BSD style pseudo tty static char const firstChars[] = "pqrstuvwxyzabcde"; static char const secondChars[] = "0123456789abcdef"; const char *first; const char *second; char ptyname[16]; for ( first = firstChars; *first != '\0'; ++first ) { for ( second = secondChars; *second != '\0'; ++second ) { sprintf( ptyname, "/dev/pty%c%c", *first, *second ); sprintf( ttyname, "/dev/tty%c%c", *first, *second ); if ( ( masterFd = ::open( ptyname, O_RDWR | O_NONBLOCK, 0 ) ) >= 0 ) { if ( ( slaveFd = ::open( ttyname, O_RDWR | O_NOCTTY, 0 ) ) >= 0 ) { return true; } ::close( masterFd ); } } } return false; }
int main(void) { int fdm; int rc; // initial system("ls -l /dev/pts"); fdm = posix_openpt(O_RDWR); if (fdm < 0) { perror("posix_openpt"); return 1; } rc = grantpt(fdm); if (rc != 0) { perror("grantpt"); return 1; } rc = unlockpt(fdm); if (rc != 0) { perror("unlockpt"); return 1; } // final system("ls -l /dev/pts"); return 0; }
int allocate_pty(int *master, int *slave) { #if defined(HAVE_OPENPTY) || (defined(HAVE_DECL_OPENPTY) && HAVE_DECL_OPENPTY != 0) if(openpty(master, slave, NULL, NULL, NULL)) return -1; #else /* STREAMS... sigh */ char *slavename; extern char *ptsname(); *master = open("/dev/ptmx", O_RDWR); /* open master */ if(*master < 0) return -1; grantpt(*master); /* change permission of slave */ unlockpt(*master); /* unlock slave */ slavename = ptsname(*master); /* get name of slave */ *slave = open(slavename, O_RDWR); /* open slave */ if(*slave < 0) { close(*master); *master = -1; return -1; } /* This is a bit backwards as we using the PTY backwards. * We want to make the master a tty instead of the slave... odd, I know. */ ioctl(*master, I_PUSH, "ptem"); /* push ptem */ ioctl(*master, I_PUSH, "ldterm"); /* push ldterm*/ #endif if(eventer_set_fd_nonblocking(*master)) return -1; noitL(noit_debug, "allocate_pty -> %d,%d\n", *master, *slave); return 0; }
static int test_ebadf (void) { int fd, ret, err; fd = posix_openpt (O_RDWR); if (fd == -1) { printf ("posix_openpt(O_RDWR) failed\nerrno %d (%s)\n", errno, strerror (errno)); /* We don't fail because of this; maybe the system does not have SUS pseudo terminals. */ return 0; } unlockpt (fd); close (fd); ret = grantpt (fd); err = errno; if (ret != -1 || err != EBADF) { printf ("grantpt(): expected: return = %d, errno = %d\n", -1, EBADF); printf (" got: return = %d, errno = %d\n", ret, err); return 1; } return 0; }
int ptym_open(char *pts_name) { char *ptr; int fdm; #if defined(AIX43) || defined(AIX51) char default_pts_name[] = "/dev/ptc"; #else char default_pts_name[] = "/dev/ptmx"; #endif strcpy(pts_name, default_pts_name); /* in case open fails */ if ((fdm = open(pts_name, O_RDWR)) < 0) { return -1; } if (grantpt(fdm) < 0) { /* grant access to slave */ close(fdm); return -2; } if (unlockpt(fdm) < 0) { /* clear slave's lock flag */ close(fdm); return -3; } if ((ptr = ptsname(fdm)) == NULL) { /* get slave's name */ close(fdm); return -4; } strcpy(pts_name, ptr); /* return name of slave */ return fdm; /* return fd of master */ }
void try_tty() { int master, slave; char name[128] = "123"; struct termios *term; struct winsize *win; if ( ! openpty( &master, &slave, name, term, win ) ) { printf( "name: %s\n", name ); printf( "master: %d\n", master ); printf( "slave: %d\n", slave ); printf( "window row: %u\n", win->ws_row ); printf( "window column: %u\n", win->ws_col ); printf( "window xpixel: %u\n", win->ws_xpixel ); printf( "window ypixel: %u\n", win->ws_ypixel ); // getchar(); } ELSE_PRINT_ERROR int fd_pt; char buf[128]; if ( 0 < ( fd_pt = getpt() ) ) { grantpt( fd_pt ); printf( "ptsname==>name: %s\n", ptsname( fd_pt ) ); ptsname_r( fd_pt, buf, sizeof(buf) ); printf( "ptsname_r==>name: %s\n", buf ); write( fd_pt, "Hello tty !!", 13 ); // getchar(); } ELSE_PRINT_ERROR }
static int test_einval (void) { int fd, ret, err; const char file[] = "./grantpt-einval"; fd = open (file, O_RDWR | O_CREAT, 0600); if (fd == -1) { printf ("open(\"%s\", O_RDWR) failed\nerrno %d (%s)\n", file, errno, strerror (errno)); return 0; } unlink (file); ret = grantpt (fd); err = errno; if (ret != -1 || err != EINVAL) { printf ("grantpt(): expected: return = %d, errno = %d\n", -1, EINVAL); printf (" got: return = %d, errno = %d\n", ret, err); ret = 1; } else ret = 0; close (fd); return ret; }
/*** Grant access to a slave pseudoterminal @function grantpt @int fd descriptor returned by openpt @treturn[1] int `0`, if successful @return[2] nil @treturn[2] string error message @treturn[2] int errnum @see grantpt(3) @see openpt @see ptsname @see unlockpt */ static int Pgrantpt(lua_State *L) { int fd=checkint(L, 1); checknargs(L, 1); return pushresult(L, grantpt(fd), "grantpt"); }
int ptym_open(char * pts_name) { int fdm; char *ptr; strcpy(pts_name, "/dev/ptmx"); fdm = posix_openpt(O_RDWR); if (fdm < 0) return -1; if (grantpt(fdm) < 0) { /* grant access to slave */ close(fdm); return -2; } if (unlockpt(fdm) < 0) { /* clear slave's lock flag */ close(fdm); return -3; } ptr = ptsname(fdm); if (ptr == NULL) { /* get slave's name */ close (fdm); return -4; } strcpy(pts_name, ptr); /* return name of slave */ return fdm; /* return fd of master */ }
int ptym_open(char *pts_name, int pts_namesz) { char *ptr; int fdm; /* * Return the name of the master device so that on failure * the caller can print an error message. Null terminate * to handle case where string length > pts_namesz. */ strncpy(pts_name, "/dev/ptyXY", pts_namesz); pts_name[pts_namesz - 1] = '\0'; if ((fdm = posix_openpt(O_RDWR)) < 0) return(-1); if (grantpt(fdm) < 0) { /* grant access to slave */ close(fdm); return(-2); } if (unlockpt(fdm) < 0) { /* clear slave's lock flag */ close(fdm); return(-3); } if ((ptr = ptsname(fdm)) == NULL) { /* get slave's name */ close(fdm); return(-4); } /* * Return name of slave. Null terminate to handle * case where strlen(ptr) > pts_namesz. */ strncpy(pts_name, ptr, pts_namesz); pts_name[pts_namesz - 1] = '\0'; return(fdm); /* return fd of master */ }
int open_master(char *name, int sz) { char *sname; int fd; strncpy(name, DEV_PTMX, sz); name[sz - 1] = '\0'; fd = open(name, O_RDWR); if (fd < 0) return EPTMX_OPEN; if (grantpt(fd) < 0) { close(fd); return EPTMX_GRANT; } if (unlockpt(fd) < 0) { close(fd); return EPTMX_UNLOCK; } sname = ptsname(fd); if (sname == NULL) { close(fd); return EPTMX_NAME; } strncpy(name, sname, sz); name[sz - 1] = '\0'; return fd; }
int open_pty() { int fd = check("posix_openpt", posix_openpt(O_RDWR | O_NOCTTY)); check("grantpt", grantpt(fd)); check("unlockpt", unlockpt(fd)); return fd; }
gint ide_vte_pty_create_slave (VtePty *pty) { gint master_fd; #ifdef HAVE_PTSNAME_R char name[PATH_MAX + 1]; #else const char *name; #endif g_assert (VTE_IS_PTY (pty)); if (-1 == (master_fd = vte_pty_get_fd (pty))) return -1; if (grantpt (master_fd) != 0) return -1; if (unlockpt (master_fd) != 0) return -1; #ifdef HAVE_PTSNAME_R if (ptsname_r (master_fd, name, sizeof name - 1) != 0) return -1; name[sizeof name - 1] = '\0'; #else if (NULL == (name = ptsname (master_fd))) return -1; #endif return open (name, O_RDWR | O_CLOEXEC); }
int open_pty_pair(int *masterp, int *slavep) { int master, slave; char name[1024]; master = getpt(); if (master < 0) { return 0; } if (grantpt(master) < 0 || unlockpt(master) < 0) { close(master); return 0; } if (ptsname_r(master, name, sizeof(name)) < 0) { close(master); return 0; } slave = open(name, O_RDWR); if (slave < 0) { close(master); return 0; } *masterp = master; *slavep = slave; return 1; }
CAMLprim value caml_extunix_grantpt(value fd) { CAMLparam1(fd); if(grantpt(Int_val(fd)) == -1) uerror("grantpt", Nothing); CAMLreturn(Val_unit); }
int openpty(int* master, int* slave, char* name, const termios* t, const winsize* ws) { *master = getpt(); if (*master == -1) { return -1; } if (grantpt(*master) == -1 || unlockpt(*master) == -1) { close(*master); return -1; } char buf[32]; if (name == NULL) { name = buf; } if (ptsname_r(*master, name, sizeof(buf)) != 0) { close(*master); return -1; } *slave = open(name, O_RDWR|O_NOCTTY); if (*slave == -1) { close(*master); return -1; } if (t != NULL) { tcsetattr(*slave, TCSAFLUSH, t); } if (ws != NULL) { ioctl(*slave, TIOCSWINSZ, ws); } return 0; }
/* * main test driver */ int main(int argc, char **argv) { int masterfd; /* master pty fd */ char *slavename; pid_t childpid; /*--------------------------------------------------------------------*/ masterfd = open(MASTERCLONE, O_RDWR); if (masterfd < 0) tst_brkm(TBROK | TERRNO, NULL, "open %s", MASTERCLONE); slavename = ptsname(masterfd); if (slavename == NULL) tst_brkm(TBROK | TERRNO, NULL, "ptsname"); if (grantpt(masterfd) != 0) tst_brkm(TBROK | TERRNO, NULL, "grantpt"); if (unlockpt(masterfd) != 0) tst_brkm(TBROK | TERRNO, NULL, "unlockpt"); childpid = fork(); if (childpid == -1) tst_brkm(TBROK | TERRNO, NULL, "fork"); else if (childpid == 0) exit(child(masterfd)); else parent(masterfd, childpid); /*--------------------------------------------------------------------*/ cleanup(); tst_exit(); }
/* * ptym_open -- * This function opens a master pty and returns the file descriptor * to it. pts_name is also returned which is the name of the slave. */ static int ptym_open(char *pts_name) { int fdm; char *ptr; strcpy(pts_name, _PATH_SYSV_PTY); if ((fdm = open(pts_name, O_RDWR)) < 0 ) return (-1); if (grantpt(fdm) < 0) { close(fdm); return (-2); } if (unlockpt(fdm) < 0) { close(fdm); return (-3); } if (unlockpt(fdm) < 0) { close(fdm); return (-3); } /* get slave's name */ if ((ptr = ptsname(fdm)) == NULL) { close(fdm); return (-3); } strcpy(pts_name, ptr); return (fdm); }
/* To open and start new * python shell process */ gboolean ptyFork (ChildProcessData *python_shell_data, GError **error) { int mfd, slaveFd, savedErrno; mfd = posix_openpt (O_RDWR | O_NOCTTY | O_NONBLOCK); grantpt (mfd); unlockpt (mfd); python_shell_data->master_fd = mfd; python_shell_data->slave_name = g_strdup (ptsname (mfd)); python_shell_data->sh_argv = g_malloc0 (sizeof (gchar *)); python_shell_data->sh_argv[0] = g_strdup ("/bin/sh"); /*if (!g_spawn_async (python_shell_data->current_dir, python_shell_data->argv, NULL, 0, child_func, (gpointer)python_shell_data, &(python_shell_data->pid), error)) return FALSE;*/ pid_t childPid = fork (); if (childPid == 0) { child_func ((gpointer)python_shell_data); execv (python_shell_data->sh_argv [0], python_shell_data->sh_argv); } python_shell_data->pid = childPid; return TRUE; }
int ptym_open(char *pts_name) { char *ptr; int fdm; strcpy(pts_name, "/dev/ptmx"); /* in case open fails */ if ( (fdm = open(pts_name, O_RDWR)) < 0) return(-1); if (grantpt(fdm) < 0) { /* grant access to slave */ close(fdm); return(-2); } if (unlockpt(fdm) < 0) { /* clear slave's lock flag */ close(fdm); return(-3); } if ( (ptr = ptsname(fdm)) == NULL) { /* get slave's name */ close(fdm); return(-4); } strcpy(pts_name, ptr); /* return name of slave */ return(fdm); /* return fd of master */ }
static int pty_open_master (char *pty_name) { char *slave_name; int pty_master; #ifdef HAVE_POSIX_OPENPT pty_master = posix_openpt (O_RDWR); #elif HAVE_GETPT /* getpt () is a GNU extension (glibc 2.1.x) */ pty_master = getpt (); #elif IS_AIX strcpy (pty_name, "/dev/ptc"); pty_master = open (pty_name, O_RDWR); #else strcpy (pty_name, "/dev/ptmx"); pty_master = open (pty_name, O_RDWR); #endif if (pty_master == -1) return -1; if (grantpt (pty_master) == -1 /* Grant access to slave */ || unlockpt (pty_master) == -1 /* Clear slave's lock flag */ || !(slave_name = ptsname (pty_master))) /* Get slave's name */ { close (pty_master); return -1; } strcpy (pty_name, slave_name); return pty_master; }
/* * Allocate pty master (APUE, p. 638). */ int ptym_open(char *pts_name, size_t namsz) { char *ptr; int fdm; strcpy(pts_name, "/dev/ptmx"); if ((fdm = open(pts_name, O_RDWR)) < 0) return -1; if (grantpt(fdm) < 0) { close(fdm); return -2; } if (unlockpt(fdm) < 0) { close(fdm); return -3; } if ((ptr = ptsname(fdm)) == NULL) { close(fdm); return -4; } strncpy(pts_name, ptr, namsz); #ifndef __linux__ if (ioctl(fdm, I_PUSH, "pckt") < 0) { close(fdm); return -5; } #endif /* !__linux__ */ return fdm; }
int right_side::create_ps_term() { int master; if ((master = posix_openpt(O_RDWR)) == -1) { throw_error("posix_openpt()"); } if (grantpt(master) == -1) { throw_error("grantpt()"); } if (unlockpt(master) == -1) { throw_error("inlockpt()"); } int slave = open(ptsname(master), O_RDWR); if (slave == -1) { throw_error("open(ptsname(master)..."); } child = fork(); if (child == -1) { throw_error("fork()"); } if (!child) { struct termios settings; tcgetattr(slave, &settings); cfmakeraw(&settings); tcsetattr(slave, TCSANOW, &settings); close(master); if (dup2(slave, STDIN_FILENO) == -1) {throw_error("dup2(slave, STDIN_FILENO)");} if (dup2(slave, STDOUT_FILENO) == -1) {throw_error("dup2(slave, STDOUT_FILENO)");} if (dup2(slave, STDERR_FILENO) == -1) {throw_error("dup2(slave, STDERR_FILENO)");} close(slave); setsid(); ioctl(0, TIOCSCTTY, 1); if (execlp("/bin/sh", "sh", NULL) == -1) {throw_error("execlp()");} } else { close(slave); } return master; }
/* * test hangup semantics */ int test6(void) { static int masterfd; static int slavefd; char *slavename; struct termios termios; masterfd = open(MASTERCLONE, O_RDWR); if (masterfd < 0) { tst_resm(TBROK,"%s",MASTERCLONE); tst_exit(); } slavename = ptsname(masterfd); if (slavename == NULL) { tst_resm(TBROK|TERRNO, "ptsname() call failed"); tst_exit(); } if (grantpt(masterfd) != 0) { tst_resm(TBROK|TERRNO, "grantpt() call failed"); tst_exit(); } if (unlockpt(masterfd) != 0) { tst_resm(TBROK,"unlockpt() call failed"); tst_exit(); } if ((slavefd = open(slavename, O_RDWR)) < 0) { tst_resm(TBROK,"Could not open %s",slavename); tst_exit(); } if (ioctl(slavefd, TCGETS, &termios) != 0) { tst_resm(TFAIL,"TCGETS"); tst_exit(); } termios.c_cflag &= ~CBAUD; termios.c_cflag |= B0&CBAUD; if (ioctl(slavefd, TCSETS, &termios) != 0) { tst_resm(TFAIL,"TCGETS"); tst_exit(); } if (close(slavefd) != 0) { tst_resm(TBROK,"close"); tst_exit(); } if (close(masterfd) != 0) { tst_resm(TBROK,"close"); tst_exit(); } tst_resm(TPASS,"test6"); /** NOT REACHED **/ return 0; }
bool get_pty(int *master, int *slave, char *name, size_t namesz, uid_t ttyuid) { char *line; bool rval = false; debug_decl(get_pty, SUDO_DEBUG_PTY) *master = posix_openpt(O_RDWR|O_NOCTTY); if (*master != -1) { (void) grantpt(*master); /* may fork */ if (unlockpt(*master) != 0) { close(*master); goto done; } line = ptsname(*master); if (line == NULL) { close(*master); goto done; } *slave = open(line, O_RDWR|O_NOCTTY, 0); if (*slave == -1) { close(*master); goto done; } # if defined(I_PUSH) && !defined(_AIX) ioctl(*slave, I_PUSH, "ptem"); /* pseudo tty emulation module */ ioctl(*slave, I_PUSH, "ldterm"); /* line discipline module */ # endif (void) chown(line, ttyuid, -1); strlcpy(name, line, namesz); rval = true; } done: debug_return_bool(rval); }
int GUCEF_pty_open( int* fdm, int* fds ) { int masterfd, slavefd; char *slavedevice; /* * O_RDWR = Open the device for both reading and writing. It is usual to specify this flag. * O_NOCTTY = Do not make this device the controlling terminal for the process. */ masterfd = posix_openpt( O_RDWR | O_NOCTTY ); if ( masterfd == -1 || grantpt (masterfd) == -1 || unlockpt (masterfd) == -1 || (slavedevice = ptsname (masterfd)) == NULL) return 1; slavefd = open(slavedevice, O_RDWR|O_NOCTTY); if (slavefd < 0) return 1; /* success */ *fdm = masterfd; *fds = slavefd; return 0; }