Allow script loading when compiled with PTY_DISABLED

This commit is contained in:
Leroy Hopson 2024-01-06 22:37:47 +13:00
parent 893c768f75
commit 52a89827c9
No known key found for this signature in database
GPG key ID: D2747312A6DB51AA
2 changed files with 21 additions and 10 deletions

View file

@ -36,6 +36,14 @@ enum IPCSignal {
SIGTERM = 15, # Termination signal SIGTERM = 15, # Termination signal
} }
var _LibuvUtils = (
ClassDB.instantiate("LibuvUtils").get_class() if ClassDB.class_exists("LibuvUtils") else null
)
var _Pipe = ClassDB.instantiate("Pipe").get_class() if ClassDB.class_exists("Pipe") else null
var _PTYUnix = (
ClassDB.instantiate("PTYUnix").get_class() if ClassDB.class_exists("PTYUnix") else null
)
# The name of the process. # The name of the process.
#var process: String #var process: String
@ -50,7 +58,7 @@ var env := DEFAULT_ENV
# former taking precedence in the case of conflicts. # former taking precedence in the case of conflicts.
var use_os_env := true var use_os_env := true
var _pipe: Pipe var _pipe = _Pipe
# Security warning: use this option with great caution, as opened file descriptors # Security warning: use this option with great caution, as opened file descriptors
# with higher privileges might leak to the child program. # with higher privileges might leak to the child program.
@ -74,14 +82,14 @@ func write(data) -> void:
func resize(cols: int, rows: int) -> void: func resize(cols: int, rows: int) -> void:
if _fd >= 0: if _fd >= 0:
PTYUnix.new().resize(_fd, cols, rows) _PTYUnix.new().resize(_fd, cols, rows)
func kill(signum: int = IPCSignal.SIGHUP) -> void: func kill(signum: int = IPCSignal.SIGHUP) -> void:
if _pipe: if _pipe:
_pipe.close() _pipe.close()
if _pid > 0: if _pid > 0:
LibuvUtils.kill(_pid, signum) _LibuvUtils.kill(_pid, signum)
func _parse_env(env: Dictionary = {}) -> PackedStringArray: func _parse_env(env: Dictionary = {}) -> PackedStringArray:
@ -110,7 +118,7 @@ func _process(_delta):
func fork( func fork(
file: String = OS.get_environment("SHELL"), file: String = OS.get_environment("SHELL"),
args: PackedStringArray = PackedStringArray(), args: PackedStringArray = PackedStringArray(),
cwd = LibuvUtils.get_cwd(), cwd = _LibuvUtils.get_cwd(),
cols: int = DEFAULT_COLS, cols: int = DEFAULT_COLS,
rows: int = DEFAULT_ROWS, rows: int = DEFAULT_ROWS,
uid: int = -1, uid: int = -1,
@ -123,7 +131,7 @@ func fork(
# Environment variables. # Environment variables.
# If we are using OS env vars, sanitize them to remove variables that might confuse our terminal. # If we are using OS env vars, sanitize them to remove variables that might confuse our terminal.
var final_env := _sanitize_env(LibuvUtils.get_os_environ()) if use_os_env else {} var final_env := _sanitize_env(_LibuvUtils.get_os_environ()) if use_os_env else {}
for key in env.keys(): for key in env.keys():
final_env[key] = env[key] final_env[key] = env[key]
var parsed_env: PackedStringArray = _parse_env(final_env) var parsed_env: PackedStringArray = _parse_env(final_env)
@ -133,7 +141,7 @@ func fork(
# Actual fork. # Actual fork.
var result = ( var result = (
PTYUnix _PTYUnix
. new() . new()
. fork( . fork(
# VERY IMPORTANT: The second argument must be 0, otherwise will get an ENOTSOCK error after connecting our pipe to the fd. # VERY IMPORTANT: The second argument must be 0, otherwise will get an ENOTSOCK error after connecting our pipe to the fd.
@ -162,7 +170,7 @@ func fork(
_pid = result[1].pid _pid = result[1].pid
_pipe = Pipe.new() _pipe = _Pipe.new()
_pipe.open(_fd, false) _pipe.open(_fd, false)
# Must connect to signal AFTER opening, otherwise we will get error ENOTSOCK. # Must connect to signal AFTER opening, otherwise we will get error ENOTSOCK.
@ -172,13 +180,13 @@ func fork(
func open(cols: int = DEFAULT_COLS, rows: int = DEFAULT_ROWS) -> Array: func open(cols: int = DEFAULT_COLS, rows: int = DEFAULT_ROWS) -> Array:
return PTYUnix.new().open(cols, rows) return _PTYUnix.new().open(cols, rows)
func _exit_tree(): func _exit_tree():
_exit_cb = Callable() _exit_cb = Callable()
if _pid > 1: if _pid > 1:
LibuvUtils.kill(_pid, IPCSignal.SIGHUP) _LibuvUtils.kill(_pid, IPCSignal.SIGHUP)
if _pipe: if _pipe:
while _pipe.get_status() != 0: while _pipe.get_status() != 0:
continue continue

View file

@ -6,7 +6,10 @@
@tool @tool
extends Node extends Node
var _LibuvUtils := LibuvUtils var _LibuvUtils = (
ClassDB.instantiate("LibuvUtils").get_class() if ClassDB.class_exists("LibuvUtils") else null
)
const _PTYNative := preload("./nodes/pty/pty_native.gd") const _PTYNative := preload("./nodes/pty/pty_native.gd")
const _PTYUnix := preload("./nodes/pty/unix/pty_unix.gd") const _PTYUnix := preload("./nodes/pty/unix/pty_unix.gd")