summaryrefslogtreecommitdiffstats
path: root/drivers/unix
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/unix')
-rw-r--r--drivers/unix/file_access_unix_pipe.cpp185
-rw-r--r--drivers/unix/file_access_unix_pipe.h96
-rw-r--r--drivers/unix/os_unix.cpp102
-rw-r--r--drivers/unix/os_unix.h1
4 files changed, 384 insertions, 0 deletions
diff --git a/drivers/unix/file_access_unix_pipe.cpp b/drivers/unix/file_access_unix_pipe.cpp
new file mode 100644
index 0000000000..5d9a27ad05
--- /dev/null
+++ b/drivers/unix/file_access_unix_pipe.cpp
@@ -0,0 +1,185 @@
+/**************************************************************************/
+/* file_access_unix_pipe.cpp */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/**************************************************************************/
+
+#include "file_access_unix_pipe.h"
+
+#if defined(UNIX_ENABLED)
+
+#include "core/os/os.h"
+#include "core/string/print_string.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+Error FileAccessUnixPipe::open_existing(int p_rfd, int p_wfd) {
+ // Open pipe using handles created by pipe(fd) call in the OS.execute_with_pipe.
+ _close();
+
+ path_src = String();
+ unlink_on_close = false;
+ ERR_FAIL_COND_V_MSG(fd[0] >= 0 || fd[1] >= 0, ERR_ALREADY_IN_USE, "Pipe is already in use.");
+ fd[0] = p_rfd;
+ fd[1] = p_wfd;
+
+ last_error = OK;
+ return OK;
+}
+
+Error FileAccessUnixPipe::open_internal(const String &p_path, int p_mode_flags) {
+ _close();
+
+ path_src = p_path;
+ ERR_FAIL_COND_V_MSG(fd[0] >= 0 || fd[1] >= 0, ERR_ALREADY_IN_USE, "Pipe is already in use.");
+
+ path = String("/tmp/") + p_path.replace("pipe://", "").replace("/", "_");
+ struct stat st = {};
+ int err = stat(path.utf8().get_data(), &st);
+ if (err) {
+ if (mkfifo(path.utf8().get_data(), 0666) != 0) {
+ last_error = ERR_FILE_CANT_OPEN;
+ return last_error;
+ }
+ unlink_on_close = true;
+ } else {
+ ERR_FAIL_COND_V_MSG(!S_ISFIFO(st.st_mode), ERR_ALREADY_IN_USE, "Pipe name is already used by file.");
+ }
+
+ int f = ::open(path.utf8().get_data(), O_RDWR | O_CLOEXEC);
+ if (f < 0) {
+ switch (errno) {
+ case ENOENT: {
+ last_error = ERR_FILE_NOT_FOUND;
+ } break;
+ default: {
+ last_error = ERR_FILE_CANT_OPEN;
+ } break;
+ }
+ return last_error;
+ }
+
+ // Set close on exec to avoid leaking it to subprocesses.
+ fd[0] = f;
+ fd[1] = f;
+
+ last_error = OK;
+ return OK;
+}
+
+void FileAccessUnixPipe::_close() {
+ if (fd[0] < 0) {
+ return;
+ }
+
+ if (fd[1] != fd[0]) {
+ ::close(fd[1]);
+ }
+ ::close(fd[0]);
+ fd[0] = -1;
+ fd[1] = -1;
+
+ if (unlink_on_close) {
+ ::unlink(path.utf8().ptr());
+ }
+ unlink_on_close = false;
+}
+
+bool FileAccessUnixPipe::is_open() const {
+ return (fd[0] >= 0 || fd[1] >= 0);
+}
+
+String FileAccessUnixPipe::get_path() const {
+ return path_src;
+}
+
+String FileAccessUnixPipe::get_path_absolute() const {
+ return path_src;
+}
+
+uint8_t FileAccessUnixPipe::get_8() const {
+ ERR_FAIL_COND_V_MSG(fd[0] < 0, 0, "Pipe must be opened before use.");
+
+ uint8_t b;
+ if (::read(fd[0], &b, 1) == 0) {
+ last_error = ERR_FILE_CANT_READ;
+ b = '\0';
+ } else {
+ last_error = OK;
+ }
+ return b;
+}
+
+uint64_t FileAccessUnixPipe::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
+ ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
+ ERR_FAIL_COND_V_MSG(fd[0] < 0, -1, "Pipe must be opened before use.");
+
+ uint64_t read = ::read(fd[0], p_dst, p_length);
+ if (read == p_length) {
+ last_error = ERR_FILE_CANT_READ;
+ } else {
+ last_error = OK;
+ }
+ return read;
+}
+
+Error FileAccessUnixPipe::get_error() const {
+ return last_error;
+}
+
+void FileAccessUnixPipe::store_8(uint8_t p_src) {
+ ERR_FAIL_COND_MSG(fd[1] < 0, "Pipe must be opened before use.");
+ if (::write(fd[1], &p_src, 1) != 1) {
+ last_error = ERR_FILE_CANT_WRITE;
+ } else {
+ last_error = OK;
+ }
+}
+
+void FileAccessUnixPipe::store_buffer(const uint8_t *p_src, uint64_t p_length) {
+ ERR_FAIL_COND_MSG(fd[1] < 0, "Pipe must be opened before use.");
+ ERR_FAIL_COND(!p_src && p_length > 0);
+ if (::write(fd[1], p_src, p_length) != (ssize_t)p_length) {
+ last_error = ERR_FILE_CANT_WRITE;
+ } else {
+ last_error = OK;
+ }
+}
+
+void FileAccessUnixPipe::close() {
+ _close();
+}
+
+FileAccessUnixPipe::~FileAccessUnixPipe() {
+ _close();
+}
+
+#endif // UNIX_ENABLED
diff --git a/drivers/unix/file_access_unix_pipe.h b/drivers/unix/file_access_unix_pipe.h
new file mode 100644
index 0000000000..d14f897d8f
--- /dev/null
+++ b/drivers/unix/file_access_unix_pipe.h
@@ -0,0 +1,96 @@
+/**************************************************************************/
+/* file_access_unix_pipe.h */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/**************************************************************************/
+
+#ifndef FILE_ACCESS_UNIX_PIPE_H
+#define FILE_ACCESS_UNIX_PIPE_H
+
+#include "core/io/file_access.h"
+#include "core/os/memory.h"
+
+#include <stdio.h>
+
+#if defined(UNIX_ENABLED)
+
+class FileAccessUnixPipe : public FileAccess {
+ bool unlink_on_close = false;
+
+ int fd[2] = { -1, -1 };
+
+ mutable Error last_error = OK;
+ String path;
+ String path_src;
+
+ void _close();
+
+public:
+ Error open_existing(int p_rfd, int p_wfd);
+ virtual Error open_internal(const String &p_path, int p_mode_flags) override; ///< open a file
+
+ virtual bool is_open() const override; ///< true when file is open
+
+ virtual String get_path() const override; /// returns the path for the current open file
+ virtual String get_path_absolute() const override; /// returns the absolute path for the current open file
+
+ virtual void seek(uint64_t p_position) override {}
+ virtual void seek_end(int64_t p_position = 0) override {}
+ virtual uint64_t get_position() const override { return 0; }
+ virtual uint64_t get_length() const override { return 0; }
+
+ virtual bool eof_reached() const override { return false; }
+
+ virtual uint8_t get_8() const override; ///< get a byte
+ virtual uint64_t get_buffer(uint8_t *p_dst, uint64_t p_length) const override;
+
+ virtual Error get_error() const override; ///< get last error
+
+ virtual void flush() override {}
+ virtual void store_8(uint8_t p_src) override; ///< store a byte
+ virtual void store_buffer(const uint8_t *p_src, uint64_t p_length) override; ///< store an array of bytes
+
+ virtual bool file_exists(const String &p_path) override { return false; }
+
+ virtual uint64_t _get_modified_time(const String &p_file) override { return 0; }
+ virtual BitField<FileAccess::UnixPermissionFlags> _get_unix_permissions(const String &p_file) override { return 0; }
+ virtual Error _set_unix_permissions(const String &p_file, BitField<FileAccess::UnixPermissionFlags> p_permissions) override { return ERR_UNAVAILABLE; }
+
+ virtual bool _get_hidden_attribute(const String &p_file) override { return false; }
+ virtual Error _set_hidden_attribute(const String &p_file, bool p_hidden) override { return ERR_UNAVAILABLE; }
+ virtual bool _get_read_only_attribute(const String &p_file) override { return false; }
+ virtual Error _set_read_only_attribute(const String &p_file, bool p_ro) override { return ERR_UNAVAILABLE; }
+
+ virtual void close() override;
+
+ FileAccessUnixPipe() {}
+ virtual ~FileAccessUnixPipe();
+};
+
+#endif // UNIX_ENABLED
+
+#endif // FILE_ACCESS_UNIX_PIPE_H
diff --git a/drivers/unix/os_unix.cpp b/drivers/unix/os_unix.cpp
index 74b703b09e..597e41fd22 100644
--- a/drivers/unix/os_unix.cpp
+++ b/drivers/unix/os_unix.cpp
@@ -37,6 +37,7 @@
#include "core/debugger/script_debugger.h"
#include "drivers/unix/dir_access_unix.h"
#include "drivers/unix/file_access_unix.h"
+#include "drivers/unix/file_access_unix_pipe.h"
#include "drivers/unix/net_socket_posix.h"
#include "drivers/unix/thread_posix.h"
#include "servers/rendering_server.h"
@@ -160,6 +161,7 @@ void OS_Unix::initialize_core() {
FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_RESOURCES);
FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_USERDATA);
FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_FILESYSTEM);
+ FileAccess::make_default<FileAccessUnixPipe>(FileAccess::ACCESS_PIPE);
DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_RESOURCES);
DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_USERDATA);
DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_FILESYSTEM);
@@ -489,6 +491,106 @@ Dictionary OS_Unix::get_memory_info() const {
return meminfo;
}
+Dictionary OS_Unix::execute_with_pipe(const String &p_path, const List<String> &p_arguments) {
+#define CLEAN_PIPES \
+ if (pipe_in[0] >= 0) { \
+ ::close(pipe_in[0]); \
+ } \
+ if (pipe_in[1] >= 0) { \
+ ::close(pipe_in[1]); \
+ } \
+ if (pipe_out[0] >= 0) { \
+ ::close(pipe_out[0]); \
+ } \
+ if (pipe_out[1] >= 0) { \
+ ::close(pipe_out[1]); \
+ } \
+ if (pipe_err[0] >= 0) { \
+ ::close(pipe_err[0]); \
+ } \
+ if (pipe_err[1] >= 0) { \
+ ::close(pipe_err[1]); \
+ }
+
+ Dictionary ret;
+#ifdef __EMSCRIPTEN__
+ // Don't compile this code at all to avoid undefined references.
+ // Actual virtual call goes to OS_Web.
+ ERR_FAIL_V(ret);
+#else
+ // Create pipes.
+ int pipe_in[2] = { -1, -1 };
+ int pipe_out[2] = { -1, -1 };
+ int pipe_err[2] = { -1, -1 };
+
+ ERR_FAIL_COND_V(pipe(pipe_in) != 0, ret);
+ if (pipe(pipe_out) != 0) {
+ CLEAN_PIPES
+ ERR_FAIL_V(ret);
+ }
+ if (pipe(pipe_err) != 0) {
+ CLEAN_PIPES
+ ERR_FAIL_V(ret);
+ }
+
+ // Create process.
+ pid_t pid = fork();
+ if (pid < 0) {
+ CLEAN_PIPES
+ ERR_FAIL_V(ret);
+ }
+
+ if (pid == 0) {
+ // The child process.
+ Vector<CharString> cs;
+ cs.push_back(p_path.utf8());
+ for (int i = 0; i < p_arguments.size(); i++) {
+ cs.push_back(p_arguments[i].utf8());
+ }
+
+ Vector<char *> args;
+ for (int i = 0; i < cs.size(); i++) {
+ args.push_back((char *)cs[i].get_data());
+ }
+ args.push_back(0);
+
+ ::close(STDIN_FILENO);
+ ::dup2(pipe_in[0], STDIN_FILENO);
+
+ ::close(STDOUT_FILENO);
+ ::dup2(pipe_out[1], STDOUT_FILENO);
+
+ ::close(STDERR_FILENO);
+ ::dup2(pipe_err[1], STDERR_FILENO);
+
+ CLEAN_PIPES
+
+ execvp(p_path.utf8().get_data(), &args[0]);
+ // The execvp() function only returns if an error occurs.
+ ERR_PRINT("Could not create child process: " + p_path);
+ raise(SIGKILL);
+ }
+ ::close(pipe_in[0]);
+ ::close(pipe_out[1]);
+ ::close(pipe_err[1]);
+
+ Ref<FileAccessUnixPipe> main_pipe;
+ main_pipe.instantiate();
+ main_pipe->open_existing(pipe_out[0], pipe_in[1]);
+
+ Ref<FileAccessUnixPipe> err_pipe;
+ err_pipe.instantiate();
+ err_pipe->open_existing(pipe_err[0], 0);
+
+ ret["stdio"] = main_pipe;
+ ret["stderr"] = err_pipe;
+ ret["pid"] = pid;
+
+#undef CLEAN_PIPES
+ return ret;
+#endif
+}
+
Error OS_Unix::execute(const String &p_path, const List<String> &p_arguments, String *r_pipe, int *r_exitcode, bool read_stderr, Mutex *p_pipe_mutex, bool p_open_console) {
#ifdef __EMSCRIPTEN__
// Don't compile this code at all to avoid undefined references.
diff --git a/drivers/unix/os_unix.h b/drivers/unix/os_unix.h
index d3393c98ec..abbaf9b8cd 100644
--- a/drivers/unix/os_unix.h
+++ b/drivers/unix/os_unix.h
@@ -76,6 +76,7 @@ public:
virtual Dictionary get_memory_info() const override;
virtual Error execute(const String &p_path, const List<String> &p_arguments, String *r_pipe = nullptr, int *r_exitcode = nullptr, bool read_stderr = false, Mutex *p_pipe_mutex = nullptr, bool p_open_console = false) override;
+ virtual Dictionary execute_with_pipe(const String &p_path, const List<String> &p_arguments) override;
virtual Error create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id = nullptr, bool p_open_console = false) override;
virtual Error kill(const ProcessID &p_pid) override;
virtual int get_process_id() const override;