summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-02-05 21:17:41 +0100
committerAndreas Kling <kling@serenityos.org>2020-02-05 21:17:41 +0100
commitf2a087126c30c5c1525b8e6ba46af635e2fab2b4 (patch)
tree609bc5ff372f64925db9c09c606f5d47e1fea47e
parent6d1740e4be041e7f68c8e05f4f0cbcc7f91ae22b (diff)
downloadserenity-f2a087126c30c5c1525b8e6ba46af635e2fab2b4.zip
LibC: Add posix_openpt(), grantpt() and unlockpt()
This makes getting a pseudoterminal pair a little bit more portable. Note that grantpt() and unlockpt() are currently no-ops, since we've already granted the pseudoterminal slave to the calling user. We also accept O_CLOEXEC to posix_openpt(), unlike some systems. :^)
-rw-r--r--Applications/Terminal/main.cpp12
-rw-r--r--DevTools/HackStudio/TerminalWrapper.cpp12
-rw-r--r--Libraries/LibC/stdlib.cpp22
-rw-r--r--Libraries/LibC/stdlib.h4
-rw-r--r--Servers/TelnetServer/main.cpp14
5 files changed, 58 insertions, 6 deletions
diff --git a/Applications/Terminal/main.cpp b/Applications/Terminal/main.cpp
index 83070a9bb0..7a4f31ab45 100644
--- a/Applications/Terminal/main.cpp
+++ b/Applications/Terminal/main.cpp
@@ -202,9 +202,17 @@ int main(int argc, char** argv)
if (chdir(get_current_user_home_path().characters()) < 0)
perror("chdir");
- int ptm_fd = open("/dev/ptmx", O_RDWR | O_CLOEXEC);
+ int ptm_fd = posix_openpt(O_RDWR | O_CLOEXEC);
if (ptm_fd < 0) {
- perror("open(ptmx)");
+ perror("posix_openpt");
+ return 1;
+ }
+ if (grantpt(ptm_fd) < 0) {
+ perror("grantpt");
+ return 1;
+ }
+ if (unlockpt(ptm_fd) < 0) {
+ perror("unlockpt");
return 1;
}
diff --git a/DevTools/HackStudio/TerminalWrapper.cpp b/DevTools/HackStudio/TerminalWrapper.cpp
index e7c5e192f9..89106673ca 100644
--- a/DevTools/HackStudio/TerminalWrapper.cpp
+++ b/DevTools/HackStudio/TerminalWrapper.cpp
@@ -52,9 +52,17 @@ void TerminalWrapper::run_command(const String& command)
return;
}
- int ptm_fd = open("/dev/ptmx", O_RDWR | O_CLOEXEC);
+ int ptm_fd = posix_openpt(O_RDWR | O_CLOEXEC);
if (ptm_fd < 0) {
- perror("open(ptmx)");
+ perror("posix_openpt");
+ ASSERT_NOT_REACHED();
+ }
+ if (grantpt(ptm_fd) < 0) {
+ perror("grantpt");
+ ASSERT_NOT_REACHED();
+ }
+ if (unlockpt(ptm_fd) < 0) {
+ perror("unlockpt");
ASSERT_NOT_REACHED();
}
diff --git a/Libraries/LibC/stdlib.cpp b/Libraries/LibC/stdlib.cpp
index 43b6e83ba7..925e8882e5 100644
--- a/Libraries/LibC/stdlib.cpp
+++ b/Libraries/LibC/stdlib.cpp
@@ -746,4 +746,26 @@ char* realpath(const char* pathname, char* buffer)
errno = 0;
return buffer;
}
+
+int posix_openpt(int flags)
+{
+ if (flags & ~(O_RDWR | O_NOCTTY | O_CLOEXEC)) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ return open("/dev/ptmx", flags);
+}
+
+int grantpt(int fd)
+{
+ (void)fd;
+ return 0;
+}
+
+int unlockpt(int fd)
+{
+ (void)fd;
+ return 0;
+}
}
diff --git a/Libraries/LibC/stdlib.h b/Libraries/LibC/stdlib.h
index 69416a6df0..e9f987b3b0 100644
--- a/Libraries/LibC/stdlib.h
+++ b/Libraries/LibC/stdlib.h
@@ -100,4 +100,8 @@ typedef struct {
} ldiv_t;
ldiv_t ldiv(long, long);
+int posix_openpt(int flags);
+int grantpt(int fd);
+int unlockpt(int fd);
+
__END_DECLS
diff --git a/Servers/TelnetServer/main.cpp b/Servers/TelnetServer/main.cpp
index fbfddbae7d..efa8468454 100644
--- a/Servers/TelnetServer/main.cpp
+++ b/Servers/TelnetServer/main.cpp
@@ -143,9 +143,19 @@ int main(int argc, char** argv)
return;
}
- int ptm_fd = open("/dev/ptmx", O_RDWR);
+ int ptm_fd = posix_openpt(O_RDWR);
if (ptm_fd < 0) {
- perror("open(ptmx)");
+ perror("posix_openpt");
+ client_socket->close();
+ return;
+ }
+ if (grantpt(ptm_fd) < 0) {
+ perror("grantpt");
+ client_socket->close();
+ return;
+ }
+ if (unlockpt(ptm_fd) < 0) {
+ perror("unlockpt");
client_socket->close();
return;
}