Skip to content

Commit

Permalink
Implement OS.execute_with_pipe method to run process with redirecte…
Browse files Browse the repository at this point in the history
…d stdio.

Implement `pipe://*` path handling for creation of named pipes.
  • Loading branch information
bruvzg committed Mar 27, 2024
1 parent 7d151c8 commit 082b420
Show file tree
Hide file tree
Showing 16 changed files with 790 additions and 11 deletions.
21 changes: 15 additions & 6 deletions core/core_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ String OS::read_string_from_stdin() {

int OS::execute(const String &p_path, const Vector<String> &p_arguments, Array r_output, bool p_read_stderr, bool p_open_console) {
List<String> args;
for (int i = 0; i < p_arguments.size(); i++) {
args.push_back(p_arguments[i]);
for (const String &arg : p_arguments) {
args.push_back(arg);
}
String pipe;
int exitcode = 0;
Expand All @@ -296,10 +296,18 @@ int OS::execute(const String &p_path, const Vector<String> &p_arguments, Array r
return exitcode;
}

Dictionary OS::execute_with_pipe(const String &p_path, const Vector<String> &p_arguments) {
List<String> args;
for (const String &arg : p_arguments) {
args.push_back(arg);
}
return ::OS::get_singleton()->execute_with_pipe(p_path, args);
}

int OS::create_instance(const Vector<String> &p_arguments) {
List<String> args;
for (int i = 0; i < p_arguments.size(); i++) {
args.push_back(p_arguments[i]);
for (const String &arg : p_arguments) {
args.push_back(arg);
}
::OS::ProcessID pid = 0;
Error err = ::OS::get_singleton()->create_instance(args, &pid);
Expand All @@ -311,8 +319,8 @@ int OS::create_instance(const Vector<String> &p_arguments) {

int OS::create_process(const String &p_path, const Vector<String> &p_arguments, bool p_open_console) {
List<String> args;
for (int i = 0; i < p_arguments.size(); i++) {
args.push_back(p_arguments[i]);
for (const String &arg : p_arguments) {
args.push_back(arg);
}
::OS::ProcessID pid = 0;
Error err = ::OS::get_singleton()->create_process(p_path, args, &pid, p_open_console);
Expand Down Expand Up @@ -587,6 +595,7 @@ void OS::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_executable_path"), &OS::get_executable_path);
ClassDB::bind_method(D_METHOD("read_string_from_stdin"), &OS::read_string_from_stdin);
ClassDB::bind_method(D_METHOD("execute", "path", "arguments", "output", "read_stderr", "open_console"), &OS::execute, DEFVAL(Array()), DEFVAL(false), DEFVAL(false));
ClassDB::bind_method(D_METHOD("execute_with_pipe", "path", "arguments"), &OS::execute_with_pipe);
ClassDB::bind_method(D_METHOD("create_process", "path", "arguments", "open_console"), &OS::create_process, DEFVAL(false));
ClassDB::bind_method(D_METHOD("create_instance", "arguments"), &OS::create_instance);
ClassDB::bind_method(D_METHOD("kill", "pid"), &OS::kill);
Expand Down
1 change: 1 addition & 0 deletions core/core_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class OS : public Object {
String get_executable_path() const;
String read_string_from_stdin();
int execute(const String &p_path, const Vector<String> &p_arguments, Array r_output = Array(), bool p_read_stderr = false, bool p_open_console = false);
Dictionary execute_with_pipe(const String &p_path, const Vector<String> &p_arguments);
int create_process(const String &p_path, const Vector<String> &p_arguments, bool p_open_console = false);
int create_instance(const Vector<String> &p_arguments);
Error kill(int p_pid);
Expand Down
7 changes: 6 additions & 1 deletion core/io/file_access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ thread_local Error FileAccess::last_file_open_error = OK;

Ref<FileAccess> FileAccess::create(AccessType p_access) {
ERR_FAIL_INDEX_V(p_access, ACCESS_MAX, nullptr);
ERR_FAIL_NULL_V(create_func[p_access], nullptr);

Ref<FileAccess> ret = create_func[p_access]();
ret->_set_access_type(p_access);
Expand Down Expand Up @@ -75,7 +76,8 @@ Ref<FileAccess> FileAccess::create_for_path(const String &p_path) {
ret = create(ACCESS_RESOURCES);
} else if (p_path.begins_with("user://")) {
ret = create(ACCESS_USERDATA);

} else if (p_path.begins_with("pipe://")) {
ret = create(ACCESS_PIPE);
} else {
ret = create(ACCESS_FILESYSTEM);
}
Expand Down Expand Up @@ -209,6 +211,9 @@ String FileAccess::fix_path(const String &p_path) const {
}

} break;
case ACCESS_PIPE: {
return r_path;
} break;
case ACCESS_FILESYSTEM: {
return r_path;
} break;
Expand Down
1 change: 1 addition & 0 deletions core/io/file_access.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class FileAccess : public RefCounted {
ACCESS_RESOURCES,
ACCESS_USERDATA,
ACCESS_FILESYSTEM,
ACCESS_PIPE,
ACCESS_MAX
};

Expand Down
1 change: 1 addition & 0 deletions core/os/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ class OS {
virtual Vector<String> get_system_font_path_for_text(const String &p_font_name, const String &p_text, const String &p_locale = String(), const String &p_script = String(), int p_weight = 400, int p_stretch = 100, bool p_italic = false) const { return Vector<String>(); };
virtual String get_executable_path() const;
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) = 0;
virtual Dictionary execute_with_pipe(const String &p_path, const List<String> &p_arguments) { return Dictionary(); }
virtual Error create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id = nullptr, bool p_open_console = false) = 0;
virtual Error create_instance(const List<String> &p_arguments, ProcessID *r_child_id = nullptr) { return create_process(get_executable_path(), p_arguments, r_child_id); };
virtual Error kill(const ProcessID &p_pid) = 0;
Expand Down
25 changes: 21 additions & 4 deletions doc/classes/OS.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@
<param index="1" name="arguments" type="PackedStringArray" />
<param index="2" name="open_console" type="bool" default="false" />
<description>
Creates a new process that runs independently of Godot. It will not terminate when Godot terminates. The path specified in [param path] must exist and be executable file or macOS .app bundle. Platform path resolution will be used. The [param arguments] are used in the given order and separated by a space.
Creates a new process that runs independently of Godot. It will not terminate when Godot terminates. The path specified in [param path] must exist and be an executable file or macOS [code].app[/code] bundle. The path is resolved based on the current platform. The [param arguments] are used in the given order and separated by a space.
On Windows, if [param open_console] is [code]true[/code] and the process is a console app, a new terminal window will be opened.
If the process is successfully created, this method returns its process ID, which you can use to monitor the process (and potentially terminate it with [method kill]). Otherwise this method returns [code]-1[/code].
If the process is successfully created, this method returns its process ID, which you can use to monitor the process (and potentially terminate it with [method kill]). Otherwise, this method returns [code]-1[/code].
For example, running another instance of the project:
[codeblocks]
[gdscript]
Expand All @@ -63,7 +63,7 @@
[/csharp]
[/codeblocks]
See [method execute] if you wish to run an external command and retrieve the results.
[b]Note:[/b] This method is implemented on Android, iOS, Linux, macOS and Windows.
[b]Note:[/b] This method is implemented on Android, Linux, macOS, and Windows.
[b]Note:[/b] On macOS, sandboxed applications are limited to run only embedded helper executables, specified during export or system .app bundle, system .app bundles will ignore arguments.
</description>
</method>
Expand Down Expand Up @@ -120,14 +120,31 @@
OS.Execute("CMD.exe", new string[] {"/C", "cd %TEMP% &amp;&amp; dir"}, output);
[/csharp]
[/codeblocks]
[b]Note:[/b] This method is implemented on Android, iOS, Linux, macOS and Windows.
[b]Note:[/b] This method is implemented on Android, Linux, macOS, and Windows.
[b]Note:[/b] To execute a Windows command interpreter built-in command, specify [code]cmd.exe[/code] in [param path], [code]/c[/code] as the first argument, and the desired command as the second argument.
[b]Note:[/b] To execute a PowerShell built-in command, specify [code]powershell.exe[/code] in [param path], [code]-Command[/code] as the first argument, and the desired command as the second argument.
[b]Note:[/b] To execute a Unix shell built-in command, specify shell executable name in [param path], [code]-c[/code] as the first argument, and the desired command as the second argument.
[b]Note:[/b] On macOS, sandboxed applications are limited to run only embedded helper executables, specified during export.
[b]Note:[/b] On Android, system commands such as [code]dumpsys[/code] can only be run on a rooted device.
</description>
</method>
<method name="execute_with_pipe">
<return type="Dictionary" />
<param index="0" name="path" type="String" />
<param index="1" name="arguments" type="PackedStringArray" />
<description>
Creates a new process that runs independently of Godot with redirected IO. It will not terminate when Godot terminates. The path specified in [param path] must exist and be an executable file or macOS [code].app[/code] bundle. The path is resolved based on the current platform. The [param arguments] are used in the given order and separated by a space.
If the process cannot be created, this method returns an empty [Dictionary]. Otherwise, this method returns a [Dictionary] with the following keys:
- [code]"stdio"[/code] - [FileAccess] to access the process stdin and stdout pipes (read/write).
- [code]"stderr"[/code] - [FileAccess] to access the process stderr pipe (read only).
- [code]"pid"[/code] - Process ID as an [int], which you can use to monitor the process (and potentially terminate it with [method kill]).
[b]Note:[/b] This method is implemented on Android, Linux, macOS, and Windows.
[b]Note:[/b] To execute a Windows command interpreter built-in command, specify [code]cmd.exe[/code] in [param path], [code]/c[/code] as the first argument, and the desired command as the second argument.
[b]Note:[/b] To execute a PowerShell built-in command, specify [code]powershell.exe[/code] in [param path], [code]-Command[/code] as the first argument, and the desired command as the second argument.
[b]Note:[/b] To execute a Unix shell built-in command, specify shell executable name in [param path], [code]-c[/code] as the first argument, and the desired command as the second argument.
[b]Note:[/b] On macOS, sandboxed applications are limited to run only embedded helper executables, specified during export or system .app bundle, system .app bundles will ignore arguments.
</description>
</method>
<method name="find_keycode_from_string" qualifiers="const">
<return type="int" enum="Key" />
<param index="0" name="string" type="String" />
Expand Down
185 changes: 185 additions & 0 deletions drivers/unix/file_access_unix_pipe.cpp
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 082b420

Please sign in to comment.