82 lines
2.3 KiB
GDScript3
82 lines
2.3 KiB
GDScript3
|
|
extends RefCounted
|
||
|
|
class_name ProcessTree
|
||
|
|
|
||
|
|
class ProcessInfo:
|
||
|
|
extends RefCounted
|
||
|
|
## Class representing a shallow base of Process information for a process running on the system.
|
||
|
|
|
||
|
|
## User ID that ran this process
|
||
|
|
var uid: int
|
||
|
|
## ID for the System processs
|
||
|
|
var pid: int
|
||
|
|
## Parent process that executed this ID.
|
||
|
|
var ppid: int
|
||
|
|
## Executable that was used to run this process.
|
||
|
|
var executable: String
|
||
|
|
|
||
|
|
func _init(_user: String, _pid: String, _ppid: String, name: String) -> void:
|
||
|
|
uid = _user.to_int()
|
||
|
|
pid = _pid.to_int()
|
||
|
|
ppid = _ppid.to_int()
|
||
|
|
executable = name
|
||
|
|
|
||
|
|
func _to_string() -> String:
|
||
|
|
return "<ProcessInfo: pid:%d, ppid:%d, user:%d, exec:%s>" % [pid, ppid, uid, executable]
|
||
|
|
|
||
|
|
## Returns true/false if the process is currently running
|
||
|
|
func is_running() -> bool:
|
||
|
|
return OS.is_process_running(pid)
|
||
|
|
|
||
|
|
## Kills the process on the system.
|
||
|
|
func kill() -> Error:
|
||
|
|
return OS.kill(pid)
|
||
|
|
|
||
|
|
## Kills the parent process that ran this process. (NOTE: May kill this process, or leave this process in a Zombie State)
|
||
|
|
func kill_parent() -> Error:
|
||
|
|
return OS.kill(ppid)
|
||
|
|
|
||
|
|
var processes: Array[ProcessInfo] = []
|
||
|
|
|
||
|
|
func _init() -> void:
|
||
|
|
refresh()
|
||
|
|
|
||
|
|
func refresh() -> void:
|
||
|
|
processes.clear()
|
||
|
|
var output := []
|
||
|
|
OS.execute("/usr/bin/ps", ["-eo", "uid,pid,ppid,exe"], output)
|
||
|
|
var lines: PackedStringArray = output[0].split("\n")
|
||
|
|
for line in lines:
|
||
|
|
var parts = line.split(" ",false,4)
|
||
|
|
if parts.is_empty(): continue
|
||
|
|
if parts[0] == "UID":
|
||
|
|
continue
|
||
|
|
if parts[3] == "-":
|
||
|
|
continue
|
||
|
|
var proc: ProcessInfo = ProcessInfo.new(parts[0],parts[1],parts[2],parts[3])
|
||
|
|
processes.append(proc)
|
||
|
|
|
||
|
|
func has_process_id(id: int) -> bool:
|
||
|
|
return processes.any(func(x: ProcessInfo): return x.pid == id)
|
||
|
|
|
||
|
|
func has_process_name(name: String) -> bool:
|
||
|
|
return processes.any(func(x: ProcessInfo): return x.executable == name or name in x.executable)
|
||
|
|
|
||
|
|
func get_process(id: int) -> ProcessInfo:
|
||
|
|
for proc: ProcessInfo in processes:
|
||
|
|
if proc.pid == id:
|
||
|
|
return proc
|
||
|
|
return null
|
||
|
|
|
||
|
|
func get_process_by_name(name: String) -> ProcessInfo:
|
||
|
|
for proc: ProcessInfo in processes:
|
||
|
|
if proc.executable == name or name in proc.executable:
|
||
|
|
return proc
|
||
|
|
return null
|
||
|
|
|
||
|
|
func get_all_processes_by_name(name: String) -> Array[ProcessInfo]:
|
||
|
|
var procs: Array[ProcessInfo] = []
|
||
|
|
for proc: ProcessInfo in processes:
|
||
|
|
if proc.executable == name or name in proc.executable:
|
||
|
|
procs.append(proc)
|
||
|
|
return procs
|