Skip to content

Commit aebc657

Browse files
committed
fix(Reaper): kill asyncronously and add method for getting process env
1 parent e951b1a commit aebc657

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

core/systems/launcher/reaper.gd

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ static func reap(pid: int, sig: SIG = SIG.TERM) -> void:
7878

7979
# Kill PGID
8080
var cmd := "kill"
81-
var args := [sig_arg, "--", "-{0}".format([p])]
81+
var args: Array[String] = [sig_arg, "--", "-{0}".format([p])]
8282
logger.info(cmd + " " + " ".join(args))
83-
OS.execute(cmd, args)
83+
Command.create(cmd, args).execute()
8484

8585
# Kill PIDs
8686
args = [sig_arg, "--", "{0}".format([p])]
8787
logger.info(cmd + " " + " ".join(args))
88-
OS.execute(cmd, args)
88+
Command.create(cmd, args).execute()
8989

9090
var verb := "Reaped"
9191
if sig == SIG.STOP:
@@ -175,6 +175,51 @@ static func get_pid_status(pid: int) -> Dictionary:
175175
return status
176176

177177

178+
## Returns the parsed environment for the given PID. Returns an empty dictionary
179+
## if the PID is not found or we do not have permission to read the environment.
180+
static func get_pid_environment(pid: int) -> Dictionary[String, String]:
181+
var env: Dictionary[String, String] = {}
182+
183+
# Open the environment file for the given process
184+
var env_path := "/".join(["/proc", str(pid), "environ"])
185+
var env_file := FileAccess.open(env_path, FileAccess.READ)
186+
if not env_file:
187+
return env
188+
189+
# Read from the environment until no data is left
190+
var env_data := PackedByteArray()
191+
while not env_file.eof_reached():
192+
env_data.append_array(env_file.get_buffer(8128))
193+
194+
# The environment data is a null-terminated list of strings. Loop
195+
# over the bytes to find slices between the null bytes and decode each
196+
# found slice as a string.
197+
var current_position := 0
198+
while true:
199+
var next_position := env_data.find(0, current_position)
200+
if next_position < 0:
201+
break
202+
var entry := env_data.slice(current_position, next_position)
203+
var string := entry.get_string_from_utf8()
204+
var key_value := string.split("=", true, 1)
205+
if key_value.size() > 1:
206+
env[key_value[0]] = key_value[1]
207+
current_position = next_position + 1
208+
209+
return env
210+
211+
212+
## Returns a list of all currently running processes
213+
static func get_pids() -> PackedInt64Array:
214+
var pids := PackedInt64Array()
215+
for proc in DirAccess.get_directories_at("/proc"):
216+
if not (proc as String).is_valid_int():
217+
continue
218+
var process_id := proc.to_int()
219+
pids.push_back(process_id)
220+
return pids
221+
222+
178223
# Recursively finds all descendant processes and returns it as an array of PIDs
179224
static func pstree(pid: int) -> Array:
180225
var logger := Log.get_logger("Reaper")

0 commit comments

Comments
 (0)