Kill child process and close pty on exit

- Adds kill() method to LibuvUtils.
- Adds close() method to Pipe.
This commit is contained in:
Leroy Hopson 2021-07-22 12:17:23 +07:00
parent 55b0a0577d
commit c81da3820b
No known key found for this signature in database
GPG key ID: D2747312A6DB51AA
10 changed files with 53 additions and 9 deletions

View file

@ -9,6 +9,8 @@ void LibuvUtils::_register_methods() {
register_method("get_os_environ", &LibuvUtils::get_os_environ);
register_method("get_os_release", &LibuvUtils::get_os_release);
register_method("get_cwd", &LibuvUtils::get_cwd);
register_method("kill", &LibuvUtils::kill);
}
LibuvUtils::LibuvUtils() {}
@ -61,6 +63,10 @@ String LibuvUtils::get_cwd() {
return result;
}
godot_error LibuvUtils::kill(int pid, int signum) {
RETURN_UV_ERR(uv_kill(pid, signum));
}
godot_error LibuvUtils::translate_uv_errno(int uv_err) {
if (uv_err >= 0)
return GODOT_OK;

View file

@ -9,7 +9,9 @@
String(uv_strerror(uv_err)));
#define RETURN_UV_ERR(uv_err) \
UV_ERR_PRINT(uv_err); \
if (uv_err < 0) { \
UV_ERR_PRINT(uv_err); \
} \
return LibuvUtils::translate_uv_errno(uv_err);
#define RETURN_IF_UV_ERR(uv_err) \
@ -34,6 +36,8 @@ public:
String get_os_release();
String get_cwd();
godot_error kill(int pid, int signum);
public:
static godot_error translate_uv_errno(int uv_err);
};

View file

@ -31,7 +31,7 @@ void Pipe::_register_methods() {
}
Pipe::Pipe() {}
Pipe::~Pipe() {}
Pipe::~Pipe() { close(); }
void Pipe::_init() {}
@ -39,6 +39,8 @@ void _poll_connection();
void _read_cb(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf);
void _close_cb(uv_handle_t *handle);
void _write_cb(uv_write_t *req, int status);
void _alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf);
@ -56,6 +58,11 @@ godot_error Pipe::open(int fd, bool ipc = false) {
return GODOT_OK;
}
void Pipe::close() {
uv_close((uv_handle_t *)&handle, NULL);
uv_run(uv_default_loop(), UV_RUN_NOWAIT);
}
godot_error Pipe::write(String p_data) {
char *s = p_data.alloc_c_string();
ULONG len = strlen(s);
@ -88,10 +95,10 @@ void _read_cb(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf) {
switch (nread) {
case UV_EOF:
// Normal after shell exits.
return;
case UV_EIO:
// Can happen when the process exits.
// As long as PTY has caught it, we should be fine.
uv_read_stop(handle);
return;
default:
UV_ERR_PRINT(nread);

View file

@ -23,6 +23,7 @@ public:
void _init();
godot_error open(int fd, bool ipc);
void close();
int get_status();
godot_error write(String p_data);