int create_pts(void) { int pts = posix_openpt(O_RDWR | O_NONBLOCK); struct termios termios; if (pts < 0) err(1, "failed to create pseudo terminal"); if (grantpt(pts)) err(1, "failed to grant psuedo terminal access"); if (unlockpt(pts)) err(1, "failed to unlock pseudo terminal"); if (tcgetattr(pts, &termios)) err(1, "failed to get termios"); cfmakeraw(&termios); if (tcsetattr(pts, TCSANOW, &termios)) err(1, "failed to set termios"); if (sim_is_interactive()) printf("pts: %s\n", ptsname(pts)); return pts; }
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; }
/* 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, 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 */ }
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 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); }
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; }
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 open(const char *pathname, int flags, ...) { int fd = 99; /* Ugly, but fine. */ if (!strncmp(pathname,LOG_PATH,strlen(LOG_PATH))) { fd = alloc_fd(FTYPE_CONSOLE); } else if (!strncmp(pathname, "/dev/mem", strlen("/dev/mem"))) { fd = alloc_fd(FTYPE_MEM); } else if (!strncmp(pathname, "/dev/ptmx", strlen("/dev/ptmx"))) { fd = posix_openpt(flags); } else if (!strncmp(pathname,SAVE_PATH,strlen(SAVE_PATH))) { fd = open_savefile(pathname, flags & O_WRONLY); } else if (!strcmp(pathname, "/etc/nsd/nsd.conf")) { fd = open_compiled_file(CONFIGURATION, sizeof(CONFIGURATION) - 1, flags); } else if (!strcmp(pathname, "/var/db/nsd/nsd.db")) { fd = open_compiled_file(__nsd_database, sizeof(__nsd_database) - 1, flags); } else if (!strcmp(pathname, "/var/run/nsd.pid")) { errno = ENOENT; fd = -1; } else { errno = EIO; fd = -1; } printk("open(%s, %d) -> %d\n", pathname, flags, fd); return fd; }
ATF_TC_BODY(posix_openpt_failure, tc) { const char *regex = "posix_openpt.*return,failure : Invalid argument"; FILE *pipefd = setup(fds, auclass); ATF_REQUIRE_EQ(-1, posix_openpt(-1)); check_audit(fds, regex, pipefd); }
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; }
/* * Check that the given PTY index, which is in use for an old-style PTY, is not * allocated through Unix98 PTY allocation. This test is not foolproof, but it * does the job well enough. */ static void test_overlap(int m) { char *tname; size_t len; int i, n, fd[MIN_PTYS]; for (i = 0; i < MIN_PTYS; i++) { if ((fd[i] = posix_openpt(O_RDWR | O_NOCTTY)) < 0) break; /* out of PTYs */ if (grantpt(fd[i]) < 0) e(0); if (unlockpt(fd[i]) < 0) e(0); if ((tname = ptsname(fd[i])) == NULL) e(0); len = strlen(_PATH_DEV_PTS); if (strncmp(tname, _PATH_DEV_PTS, strlen(_PATH_DEV_PTS))) e(0); n = atoi(&tname[len]); if (n < 0 || n > 9) e(0); if (m == n) e(0); } for (i--; i >= 0; i--) if (close(fd[i]) < 0) e(0); }
/* allocate one pty/tty pair (Unix 98 way) */ static int get_pty(char *tty_buf, int tty_buf_size) { #ifdef ZIPIT_Z2 return get_pty_old(tty_buf, tty_buf_size); #else int fd; char *str; fd = posix_openpt(O_RDWR | O_NOCTTY); if (fd < 0) { return get_pty_old(tty_buf, tty_buf_size); } if (grantpt(fd) < 0) goto fail; if (unlockpt(fd) < 0) goto fail; str = ptsname(fd); if (!str) goto fail; pstrcpy(tty_buf, tty_buf_size, str); return fd; fail: close(fd); return -1; #endif }
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 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); }
int get_pty(int *master, int *slave, char *name, size_t namesz, uid_t ttyuid) { char *line; *master = posix_openpt(O_RDWR|O_NOCTTY); if (*master == -1) return 0; (void) grantpt(*master); /* may fork */ if (unlockpt(*master) != 0) { close(*master); return 0; } line = ptsname(*master); if (line == NULL) { close(*master); return 0; } *slave = open(line, O_RDWR|O_NOCTTY, 0); if (*slave == -1) { close(*master); return 0; } # 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); 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 open_pty() { int fd = check("posix_openpt", posix_openpt(O_RDWR | O_NOCTTY)); check("grantpt", grantpt(fd)); check("unlockpt", unlockpt(fd)); return fd; }
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 */ }
pseudoterminal(int options): master_fd(posix_openpt(options)) { if (grantpt(master_fd.getfd()) == -1) { throw errno_exception("pseudoterminal(), grantpt"); } if (unlockpt(master_fd.getfd()) == -1) { throw errno_exception("pseudoterminal(), unlockpt"); } }
int open_and_setup_pty_master() { int pty_master = posix_openpt(O_RDWR | O_CLOEXEC); ensure(pty_master != -1, "Coundn't open pty master"); ensure(grantpt(pty_master) != -1, "Coundn't grantpt"); ensure(unlockpt(pty_master) != -1, "Coundn't unlockpt"); return pty_master; }
LinuxProcess::LinuxProcess(Screen *scr, Font *f, const char *cmdline) : Process(scr, f, cmdline) { fd_master=posix_openpt(O_RDWR|O_NOCTTY); ppid=getpid(); // Process ID of parent cpid=-1; // Get child PID later s = scr; assert( fd_master >= 0 ); // Did master terminal open? assert( unlockpt(fd_master) >= 0 ); // Unlock PTY assert( grantpt(fd_master) >= 0 ); // Grant PTY assert( (pts=ptsname(fd_master)) != NULL ); // Get slave name // Create a child process to use the slave pty assert( (cpid=fork()) >= 0); // set size... struct winsize size; int ret; memset(&size, 0, sizeof(size)); size.ws_row = scr->sy; size.ws_col = scr->sx; ret = ioctl(fd_master, TIOCSWINSZ, &size); if (ret != 0) { logfile && fprintf(logfile, "failed to set window size\n"); } if(cpid == 0) { int fd_slave=-1; // Slave PTY assert( close(fd_master) >= 0 ); // Ditch master PTY fd_slave=open(pts,O_RDWR); assert( fd_slave >= 0); // This will dup fd over stdin,out,err then close fd // This function needs compilation with -lutil assert( login_tty(fd_slave) >= 0 ); // we are simulating the 'linux' terminal setenv("TERM", "linux", 1); // todo: should also disable UTF8 int s = system(cmdline); exit(WEXITSTATUS(s)); } isActive = true; resetConsole(); }
static int start_process(Channel * c, char ** envp, char * dir, char * exe, char ** args, int attach, int * pid, int * selfattach, ChildProcess ** prs) { int err = 0; int fd_tty_master = -1; char * tty_slave_name = NULL; fd_tty_master = posix_openpt(O_RDWR|O_NOCTTY); if (fd_tty_master < 0 || grantpt(fd_tty_master) < 0 || unlockpt(fd_tty_master) < 0 || (tty_slave_name = ptsname(fd_tty_master)) == NULL) err = errno; if (err == 0 && fd_tty_master < 3) { int fd0 = fd_tty_master; if ((fd_tty_master = dup(fd_tty_master)) < 0 || close(fd0)) err = errno; } if (!err) { *pid = fork(); if (*pid < 0) err = errno; if (*pid == 0) { int fd = -1; int fd_tty_slave = -1; setsid(); if (!err && (fd = sysconf(_SC_OPEN_MAX)) < 0) err = errno; if (!err && (fd_tty_slave = open(tty_slave_name, O_RDWR)) < 0) err = errno; if (!err && dup2(fd_tty_slave, 0) < 0) err = errno; if (!err && dup2(fd_tty_slave, 1) < 0) err = errno; if (!err && dup2(fd_tty_slave, 2) < 0) err = errno; while (!err && fd > 3) close(--fd); if (!err && attach && context_attach_self() < 0) err = errno; if (!err) { execve(exe, args, envp); err = errno; } if (!attach) err = 1; else if (err < 1) err = EINVAL; else if (err > 0xff) err = EINVAL; exit(err); } } if (!err) { *prs = loc_alloc_zero(sizeof(ChildProcess)); (*prs)->inp = fd_tty_master; (*prs)->out = fd_tty_master; (*prs)->err = fd_tty_master; (*prs)->pid = *pid; (*prs)->bcg = c->bcg; list_add_first(&(*prs)->link, &prs_list); } *selfattach = 1; if (!err) return 0; errno = err; return -1; }
CAMLprim value caml_extunix_posix_openpt(value flags) { CAMLparam1(flags); int ret, cv_flags; cv_flags = extunix_open_flags(flags); ret = posix_openpt(cv_flags); if(ret == -1) uerror("posix_openpt", Nothing); CAMLreturn(Val_int(ret)); }
ATF_TC_BODY(posix_openpt_success, tc) { int filedesc; FILE *pipefd = setup(fds, auclass); ATF_REQUIRE((filedesc = posix_openpt(O_RDWR | O_NOCTTY)) != -1); /* Check for the presence of filedesc in the audit record */ snprintf(ipcregex, sizeof(ipcregex), "posix_openpt.*return,success,%d", filedesc); check_audit(fds, ipcregex, pipefd); close(filedesc); }
void passphrase_read_prompts_to_tty() { static const int read_nbyte = 11; int masterfd; char* slavedevice = NULL; char read_buf[read_nbyte]; fd_set fd_set_write; 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"); switch (fork()) { case -1: CU_FAIL_FATAL("Could not fork"); case 0: { static const int password_size = 512; char buffer[password_size]; int slavefd; if (setsid() == (pid_t) -1) CU_FAIL_FATAL("Could not create new session"); if ((slavefd = open(slavedevice, O_RDWR)) == 0) CU_FAIL_FATAL("Could not open slave end of pty"); close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); close(masterfd); freerdp_passphrase_read("Password: "******"Master end of pty not writeable"); if (read(masterfd, read_buf, read_nbyte) == (ssize_t) -1) CU_FAIL_FATAL("Nothing written to slave end of pty"); CU_ASSERT_STRING_EQUAL(read_buf, "Password: "******"\n", (size_t) 2); close(masterfd); return; }
int ptm_open(void) { int masterfd; if ((masterfd = posix_openpt(O_RDWR | O_NOCTTY)) == -1) { return -1; } if (grantpt(masterfd) == -1 || unlockpt(masterfd) == -1) { close(masterfd); return -1; } return masterfd; }
/* * _vte_pty_getpt: * @error: a location to store a #GError, or %NULL * * Opens a file descriptor for the next available PTY master. * Sets the descriptor to blocking mode! * * Returns: a new file descriptor, or %-1 on failure */ static int _vte_pty_getpt(GError **error) { int fd, flags, rv; #if defined(HAVE_POSIX_OPENPT) fd = posix_openpt(O_RDWR | O_NOCTTY); #elif defined(HAVE_GETPT) /* Call the system's function for allocating a pty. */ fd = getpt(); #else /* Try to allocate a pty by accessing the pty master multiplex. */ fd = open("/dev/ptmx", O_RDWR | O_NOCTTY); if ((fd == -1) && (errno == ENOENT)) { fd = open("/dev/ptc", O_RDWR | O_NOCTTY); /* AIX */ } #endif if (fd == -1) { g_set_error (error, VTE_PTY_ERROR, VTE_PTY_ERROR_PTY98_FAILED, "%s failed: %s", "getpt", g_strerror(errno)); return -1; } rv = fcntl(fd, F_GETFL, 0); if (rv < 0) { int errsv = errno; g_set_error(error, VTE_PTY_ERROR, VTE_PTY_ERROR_PTY98_FAILED, "%s failed: %s", "fcntl(F_GETFL)", g_strerror(errno)); close(fd); errno = errsv; return -1; } /* Set it to blocking. */ /* FIXMEchpe: why?? vte_terminal_set_pty does the inverse... */ flags = rv & ~(O_NONBLOCK); rv = fcntl(fd, F_SETFL, flags); if (rv < 0) { int errsv = errno; g_set_error(error, VTE_PTY_ERROR, VTE_PTY_ERROR_PTY98_FAILED, "%s failed: %s", "fcntl(F_SETFL)", g_strerror(errno)); close(fd); errno = errsv; return -1; } return fd; }
CAMLprim value unix_posix_openpt(value flags) { CAMLparam1(flags); int fd, cv_flags; cv_flags = caml_convert_flag_list(flags, posix_openpt_flag_table); caml_enter_blocking_section(); fd = posix_openpt(cv_flags); caml_leave_blocking_section(); if (fd == -1) uerror("posix_openpt", Nothing); CAMLreturn (Val_int(fd)); }
static int alloc_func_pty(struct ptydev *pty) { FILE *master, *slave; int master_fd, slave_fd; #if defined(HAVE_OPENPTY) || defined(HAVE_UNIX98_FUNCS) #if defined(HAVE_UNIX98_FUNCS) char *pts_name; #endif int ret = 1; #endif #if defined(HAVE_OPENPTY) || defined(HAVE_UNIX98_FUNCS) # if defined(HAVE_UNIX98_FUNCS) # if defined(HAVE_POSIX_OPENPT) master_fd = posix_openpt(O_RDWR|O_NOCTTY); if(master_fd == -1) SYS_ERR("posix_openpt", NULL, error); # elif defined(HAVE_GETPT) master_fd = getpt(); if(master_fd == -1) SYS_ERR("getpt", NULL, error); # else master_fd = open("/dev/ptmx", O_RDWR|O_NOCTTY); if(master_fd == -1) SYS_ERR("open", "/dev/ptmx", error); # endif if(grantpt(master_fd) == -1) SYS_ERR("grantpt", NULL, error); if(unlockpt(master_fd) == -1) SYS_ERR("unlockpt", NULL, error); pts_name = ptsname(master_fd); if(!pts_name) SYS_ERR("ptsname", NULL, error); slave_fd = open(pts_name, O_RDWR|O_NOCTTY); if(slave_fd == -1) SYS_ERR("open", pts_name, error); # else if(openpty(&master_fd, &slave_fd, NULL, NULL, NULL) == -1) SYS_ERR("openpty", NULL, error); # endif master = fdopen(master_fd, "r+"); if(!master) SYS_ERR("fdopen", "fdopening master_fd", error); slave = fdopen(slave_fd, "r+"); if(!slave) SYS_ERR("fdopen", "fdopening slave_fd", error); pty->type = FUNC_PTY; pty->master = master; pty->slave = slave; error: return ret; #else return 0; #endif }