2020-11-16 19:32:50 +03:00
|
|
|
module os
|
|
|
|
|
2021-10-21 11:19:01 +03:00
|
|
|
// - ProcessState.not_started - the process has not yet started
|
|
|
|
// - ProcessState.running - the process is currently running
|
|
|
|
// - ProcessState.stopped - the process was running, but was stopped temporarily
|
|
|
|
// - ProcessState.exited - the process has finished/exited
|
|
|
|
// - ProcessState.aborted - the process was terminated by a signal
|
|
|
|
// - ProcessState.closed - the process resources like opened file descriptors were freed/discarded, final state.
|
2020-11-16 19:32:50 +03:00
|
|
|
pub enum ProcessState {
|
|
|
|
not_started
|
|
|
|
running
|
|
|
|
stopped
|
|
|
|
exited
|
|
|
|
aborted
|
2021-05-09 21:31:04 +03:00
|
|
|
closed
|
2020-11-16 19:32:50 +03:00
|
|
|
}
|
|
|
|
|
2021-02-13 17:52:01 +03:00
|
|
|
[heap]
|
2020-11-16 19:32:50 +03:00
|
|
|
pub struct Process {
|
|
|
|
pub:
|
2021-01-12 06:38:43 +03:00
|
|
|
filename string // the process's command file path
|
2020-11-16 19:32:50 +03:00
|
|
|
pub mut:
|
2021-01-12 06:38:43 +03:00
|
|
|
pid int // the PID of the process
|
|
|
|
code int = -1
|
2020-11-16 19:32:50 +03:00
|
|
|
// the exit code of the process, != -1 *only* when status is .exited *and* the process was not aborted
|
2021-01-12 06:38:43 +03:00
|
|
|
status ProcessState = .not_started
|
2020-11-16 19:32:50 +03:00
|
|
|
// the current status of the process
|
2021-01-03 23:10:25 +03:00
|
|
|
err string // if the process fails, contains the reason why
|
2020-11-16 19:32:50 +03:00
|
|
|
args []string // the arguments that the command takes
|
2021-01-03 23:10:25 +03:00
|
|
|
env_is_custom bool // true, when the environment was customized with .set_environment
|
2021-02-07 07:19:05 +03:00
|
|
|
env []string // the environment with which the process was started (list of 'var=val')
|
2021-01-03 23:10:25 +03:00
|
|
|
use_stdio_ctl bool // when true, then you can use p.stdin_write(), p.stdout_slurp() and p.stderr_slurp()
|
2021-04-04 17:05:06 +03:00
|
|
|
use_pgroup bool // when true, the process will create a new process group, enabling .signal_pgkill()
|
|
|
|
stdio_fd [3]int // the stdio file descriptors for the child process, used only by the nix implementation
|
|
|
|
wdata voidptr // the WProcess; used only by the windows implementation
|
2020-11-16 19:32:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// new_process - create a new process descriptor
|
2022-03-06 20:01:22 +03:00
|
|
|
// Note: new does NOT start the new process.
|
2020-11-16 19:32:50 +03:00
|
|
|
// That is done because you may want to customize it first,
|
|
|
|
// by calling different set_ methods on it.
|
|
|
|
// In order to start it, call p.run() or p.wait()
|
|
|
|
pub fn new_process(filename string) &Process {
|
|
|
|
return &Process{
|
|
|
|
filename: filename
|
2021-04-04 17:05:06 +03:00
|
|
|
stdio_fd: [-1, -1, -1]!
|
2020-11-16 19:32:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set_args - set the arguments for the new process
|
2021-03-15 10:23:43 +03:00
|
|
|
pub fn (mut p Process) set_args(pargs []string) {
|
2020-11-16 19:32:50 +03:00
|
|
|
if p.status != .not_started {
|
2021-03-15 10:23:43 +03:00
|
|
|
return
|
2020-11-16 19:32:50 +03:00
|
|
|
}
|
|
|
|
p.args = pargs
|
2021-03-15 10:23:43 +03:00
|
|
|
return
|
2020-11-16 19:32:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// set_environment - set a custom environment variable mapping for the new process
|
2021-03-15 10:23:43 +03:00
|
|
|
pub fn (mut p Process) set_environment(envs map[string]string) {
|
2020-11-16 19:32:50 +03:00
|
|
|
if p.status != .not_started {
|
2021-03-15 10:23:43 +03:00
|
|
|
return
|
2020-11-16 19:32:50 +03:00
|
|
|
}
|
|
|
|
p.env_is_custom = true
|
|
|
|
p.env = []string{}
|
|
|
|
for k, v in envs {
|
|
|
|
p.env << '$k=$v'
|
|
|
|
}
|
2021-03-15 10:23:43 +03:00
|
|
|
return
|
2020-11-16 19:32:50 +03:00
|
|
|
}
|