libc: Implement openpty()
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
ffdcc843eb
commit
a47321a228
@ -25,6 +25,7 @@ set(SOURCES
|
||||
src/utime.cpp
|
||||
src/strtod.cpp
|
||||
src/math.cpp
|
||||
src/pty.cpp
|
||||
src/sys/stat.cpp
|
||||
src/sys/mman.cpp
|
||||
src/sys/wait.cpp
|
||||
|
21
libc/include/pty.h
Normal file
21
libc/include/pty.h
Normal file
@ -0,0 +1,21 @@
|
||||
/* pty.h: Pseudoterminal handling functions. */
|
||||
|
||||
#ifndef _PTY_H
|
||||
#define _PTY_H
|
||||
|
||||
#include <bits/termios.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/* Find an available pseudoterminal pair and initialize it, returning file descriptors for both sides. */
|
||||
int openpty(int* master, int* slave, char* name, const struct termios* term, const struct winsize* win);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
47
libc/src/pty.cpp
Normal file
47
libc/src/pty.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
#include <fcntl.h>
|
||||
#include <pty.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
int openpty(int* master, int* slave, char* name, const struct termios* term, const struct winsize* win)
|
||||
{
|
||||
*master = posix_openpt(O_RDWR);
|
||||
if (*master < 0) return -1;
|
||||
|
||||
// No-ops on Luna, and since this is the Luna libc, we can skip error handling.
|
||||
grantpt(*master);
|
||||
unlockpt(*master);
|
||||
|
||||
char* pname = ptsname(*master);
|
||||
if (!pname)
|
||||
{
|
||||
close(*master);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (name)
|
||||
{
|
||||
fputs("warning: openpty() called with non-null name, this is insecure\n", stderr);
|
||||
strcpy(name, pname);
|
||||
}
|
||||
|
||||
*slave = open(pname, O_RDWR);
|
||||
if (*slave < 0)
|
||||
{
|
||||
close(*master);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (term) tcsetattr(*slave, TCSANOW, term);
|
||||
if (win) fputs("warning: openpty() called with non-null window size, this is not supported yet\n", stderr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@
|
||||
#include <luna/CType.h>
|
||||
#include <os/File.h>
|
||||
#include <os/Process.h>
|
||||
#include <pty.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
@ -51,33 +52,35 @@ Result<void> TerminalWidget::init(char* const* args)
|
||||
|
||||
signal(SIGCHLD, sigchld_handler);
|
||||
|
||||
int fd = posix_openpt(O_RDWR | O_CLOEXEC);
|
||||
if (fd < 0) return err(errno);
|
||||
int master;
|
||||
int slave;
|
||||
|
||||
grantpt(fd);
|
||||
unlockpt(fd);
|
||||
int result = openpty(&master, &slave, nullptr, nullptr, nullptr);
|
||||
if (result < 0) return err(errno);
|
||||
|
||||
pid_t child = TRY(os::Process::fork());
|
||||
if (child == 0)
|
||||
{
|
||||
int ptsfd = open(ptsname(fd), O_RDWR);
|
||||
close(master);
|
||||
|
||||
dup2(ptsfd, STDIN_FILENO);
|
||||
dup2(ptsfd, STDOUT_FILENO);
|
||||
dup2(ptsfd, STDERR_FILENO);
|
||||
dup2(slave, STDIN_FILENO);
|
||||
dup2(slave, STDOUT_FILENO);
|
||||
dup2(slave, STDERR_FILENO);
|
||||
|
||||
setpgid(0, 0);
|
||||
tcsetpgrp(ptsfd, getpid());
|
||||
tcsetpgrp(slave, getpid());
|
||||
|
||||
close(ptsfd);
|
||||
close(slave);
|
||||
|
||||
execv(args[0], args);
|
||||
_exit(127);
|
||||
}
|
||||
|
||||
m_pty = fd;
|
||||
close(slave);
|
||||
|
||||
fcntl(fd, F_SETFL, O_NONBLOCK);
|
||||
m_pty = master;
|
||||
|
||||
fcntl(master, F_SETFL, O_NONBLOCK);
|
||||
|
||||
m_child_pid = child;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user