libc: Implement openpty()
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-09-22 23:02:33 +02:00
parent ffdcc843eb
commit a47321a228
Signed by: apio
GPG Key ID: B8A7D06E42258954
4 changed files with 84 additions and 12 deletions

View File

@ -25,6 +25,7 @@ set(SOURCES
src/utime.cpp src/utime.cpp
src/strtod.cpp src/strtod.cpp
src/math.cpp src/math.cpp
src/pty.cpp
src/sys/stat.cpp src/sys/stat.cpp
src/sys/mman.cpp src/sys/mman.cpp
src/sys/wait.cpp src/sys/wait.cpp

21
libc/include/pty.h Normal file
View 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
View 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;
}
}

View File

@ -4,6 +4,7 @@
#include <luna/CType.h> #include <luna/CType.h>
#include <os/File.h> #include <os/File.h>
#include <os/Process.h> #include <os/Process.h>
#include <pty.h>
#include <signal.h> #include <signal.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
@ -51,33 +52,35 @@ Result<void> TerminalWidget::init(char* const* args)
signal(SIGCHLD, sigchld_handler); signal(SIGCHLD, sigchld_handler);
int fd = posix_openpt(O_RDWR | O_CLOEXEC); int master;
if (fd < 0) return err(errno); int slave;
grantpt(fd); int result = openpty(&master, &slave, nullptr, nullptr, nullptr);
unlockpt(fd); if (result < 0) return err(errno);
pid_t child = TRY(os::Process::fork()); pid_t child = TRY(os::Process::fork());
if (child == 0) if (child == 0)
{ {
int ptsfd = open(ptsname(fd), O_RDWR); close(master);
dup2(ptsfd, STDIN_FILENO); dup2(slave, STDIN_FILENO);
dup2(ptsfd, STDOUT_FILENO); dup2(slave, STDOUT_FILENO);
dup2(ptsfd, STDERR_FILENO); dup2(slave, STDERR_FILENO);
setpgid(0, 0); setpgid(0, 0);
tcsetpgrp(ptsfd, getpid()); tcsetpgrp(slave, getpid());
close(ptsfd); close(slave);
execv(args[0], args); execv(args[0], args);
_exit(127); _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; m_child_pid = child;