Compare commits

...

3 Commits

Author SHA1 Message Date
92ab403687
terminal: Use forkpty()
All checks were successful
continuous-integration/drone/push Build is passing
2023-11-25 12:18:48 +01:00
5bb4321134
libc: Add login_tty() and forkpty() 2023-11-25 12:18:25 +01:00
3a5924be64
kernel: Set the initial foreground process group when acquiring a controlling terminal 2023-11-25 12:18:04 +01:00
7 changed files with 71 additions and 22 deletions

View File

@ -99,6 +99,7 @@ Result<u64> SlavePTY::ioctl(int request, void* arg)
}); });
m_master->m_session = current->sid; m_master->m_session = current->sid;
m_master->m_foreground_process_group = current->pgid;
return 0; return 0;
} }

View File

@ -26,6 +26,7 @@ set(SOURCES
src/strtod.cpp src/strtod.cpp
src/math.cpp src/math.cpp
src/pty.cpp src/pty.cpp
src/utmp.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

View File

@ -14,6 +14,9 @@ extern "C"
/* Find an available pseudoterminal pair and initialize it, returning file descriptors for both sides. */ /* 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); int openpty(int* master, int* slave, char* name, const struct termios* term, const struct winsize* win);
/* Create a new process operating in a pseudoterminal. */
pid_t forkpty(int* master, char* name, const struct termios* term, const struct winsize* win);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

18
libc/include/utmp.h Normal file
View File

@ -0,0 +1,18 @@
/* utmp.h: Write entries to utmp files. */
#ifndef _UTMP_H
#define _UTMP_H
#ifdef __cplusplus
extern "C"
{
#endif
/* Prepare a new terminal session on a tty. */
int login_tty(int fd);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -5,6 +5,7 @@
#include <string.h> #include <string.h>
#include <termios.h> #include <termios.h>
#include <unistd.h> #include <unistd.h>
#include <utmp.h>
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
@ -44,4 +45,27 @@ extern "C"
return 0; return 0;
} }
pid_t forkpty(int* master, char* name, const struct termios* term, const struct winsize* win)
{
int slave;
if (openpty(master, &slave, name, term, win) < 0) return -1;
pid_t child = fork();
if (child < 0)
{
close(*master);
close(slave);
return -1;
}
if (!child)
{
close(*master);
login_tty(slave);
}
else
close(slave);
return child;
}
} }

22
libc/src/utmp.cpp Normal file
View File

@ -0,0 +1,22 @@
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#include <utmp.h>
extern "C"
{
int login_tty(int fd)
{
setsid();
if (ioctl(fd, TIOCSCTTY) < 0) return -1;
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
close(fd);
return 0;
}
}

View File

@ -54,34 +54,14 @@ Result<void> TerminalWidget::init(char* const* args)
signal(SIGCHLD, sigchld_handler); signal(SIGCHLD, sigchld_handler);
int master; int master;
int slave; pid_t child = forkpty(&master, nullptr, nullptr, nullptr);
if (child < 0) return err(errno);
int result = openpty(&master, &slave, nullptr, nullptr, nullptr);
if (result < 0) return err(errno);
pid_t child = TRY(os::Process::fork());
if (child == 0) if (child == 0)
{ {
setsid();
close(master);
ioctl(slave, TIOCSCTTY);
dup2(slave, STDIN_FILENO);
dup2(slave, STDOUT_FILENO);
dup2(slave, STDERR_FILENO);
tcsetpgrp(slave, getpid());
close(slave);
execv(args[0], args); execv(args[0], args);
_exit(127); _exit(127);
} }
close(slave);
m_pty = master; m_pty = master;
fcntl(master, F_SETFL, O_NONBLOCK); fcntl(master, F_SETFL, O_NONBLOCK);