ladybird/Userland/Libraries/LibC/pty.cpp
Ben Wiederhake 6fd478b6ce Everywhere: Remove unused includes of AK/Format.h
These instances were detected by searching for files that include
AK/Format.h, but don't match the regex:

\\b(CheckedFormatString|critical_dmesgln|dbgln|dbgln_if|dmesgln|FormatBu
ilder|__FormatIfSupported|FormatIfSupported|FormatParser|FormatString|Fo
rmattable|Formatter|__format_value|HasFormatter|max_format_arguments|out
|outln|set_debug_enabled|StandardFormatter|TypeErasedFormatParams|TypeEr
asedParameter|VariadicFormatParams|v_critical_dmesgln|vdbgln|vdmesgln|vf
ormat|vout|warn|warnln|warnln_if)\\b

(Without the linebreaks.)

This regex is pessimistic, so there might be more files that don't
actually use any formatting functions.

Observe that this revealed that Userland/Libraries/LibC/signal.cpp is
missing an include.

In theory, one might use LibCPP to detect things like this
automatically, but let's do this one step after another.
2023-01-02 20:27:20 -05:00

126 lines
2.7 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Gunnar Beutner <gbeutner@serenityos.org>
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <errno.h>
#include <fcntl.h>
#include <pty.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
int openpty(int* amaster, int* aslave, char* name, const struct termios* termp, const struct winsize* winp)
{
*amaster = posix_openpt(O_RDWR);
if (*amaster < 0) {
return -1;
}
if (grantpt(*amaster) < 0) {
int error = errno;
close(*amaster);
errno = error;
return -1;
}
if (unlockpt(*amaster) < 0) {
int error = errno;
close(*amaster);
errno = error;
return -1;
}
char tty_name[32];
int rc = ptsname_r(*amaster, tty_name, sizeof(tty_name));
if (rc < 0) {
int error = errno;
close(*amaster);
errno = error;
return -1;
}
if (name) {
/* The spec doesn't say how large name has to be. Good luck. */
[[maybe_unused]] auto rc = strlcpy(name, tty_name, 128);
}
*aslave = open(tty_name, O_RDWR | O_NOCTTY);
if (*aslave < 0) {
int error = errno;
close(*amaster);
errno = error;
return -1;
}
if (termp) {
if (tcsetattr(*aslave, TCSAFLUSH, termp) == -1) {
int error = errno;
close(*aslave);
close(*amaster);
errno = error;
return -1;
}
}
if (winp) {
if (ioctl(*aslave, TIOCGWINSZ, winp) == -1) {
int error = errno;
close(*aslave);
close(*amaster);
errno = error;
return -1;
}
}
return 0;
}
pid_t forkpty(int* amaster, char* name, const struct termios* termp, const struct winsize* winp)
{
int master;
int slave;
if (openpty(&master, &slave, name, termp, winp) < 0)
return -1;
pid_t pid = fork();
if (pid < 0) {
close(master);
close(slave);
return -1;
}
*amaster = master;
if (pid == 0) {
close(master);
if (login_tty(slave) < 0)
_exit(1);
return 0;
}
close(slave);
return pid;
}
int login_tty(int fd)
{
setsid();
close(0);
close(1);
close(2);
int rc = dup2(fd, 0);
if (rc < 0)
return rc;
rc = dup2(fd, 1);
if (rc < 0)
return -1;
rc = dup2(fd, 2);
if (rc < 0)
return rc;
rc = close(fd);
if (rc < 0)
return rc;
rc = ioctl(0, TIOCSCTTY);
if (rc < 0)
return rc;
return 0;
}