summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibC/pty.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Userland/Libraries/LibC/pty.cpp')
-rw-r--r--Userland/Libraries/LibC/pty.cpp30
1 files changed, 19 insertions, 11 deletions
diff --git a/Userland/Libraries/LibC/pty.cpp b/Userland/Libraries/LibC/pty.cpp
index 0b13b2c8b1..0c72a31ef2 100644
--- a/Userland/Libraries/LibC/pty.cpp
+++ b/Userland/Libraries/LibC/pty.cpp
@@ -1,6 +1,7 @@
/*
* 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
*/
@@ -66,20 +67,27 @@ int openpty(int* amaster, int* aslave, char* name, const struct termios* termp,
return 0;
}
-pid_t forkpty(int* amaster, int* aslave, char* name, const struct termios* termp, const struct winsize* winp)
+pid_t forkpty(int* amaster, char* name, const struct termios* termp, const struct winsize* winp)
{
- int rc = openpty(amaster, aslave, name, termp, winp);
- if (rc < 0)
- return rc;
- rc = fork();
- if (rc < 0) {
- close(*amaster);
- close(*aslave);
+ 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;
+ }
+ if (pid == 0) {
+ close(master);
+ if (login_tty(slave) < 0)
+ _exit(1);
+ return 0;
}
- if (rc == 0)
- rc = login_tty(*aslave);
- return rc;
+ *amaster = master;
+ close(slave);
+ return pid;
}
int login_tty(int fd)