fix: improve error handling in Process

This commit is contained in:
ge
2026-01-08 05:39:19 +03:00
parent 04faf33542
commit 21ebb3dbfa

View File

@@ -98,24 +98,31 @@ pub fn (s ProcessState) str() string {
// [execve(3p)](https://man7.org/linux/man-pages/man3/exec.3p.html) // [execve(3p)](https://man7.org/linux/man-pages/man3/exec.3p.html)
// calls. Return value is the child process identifier. // calls. Return value is the child process identifier.
pub fn (mut p Process) start() !int { pub fn (mut p Process) start() !int {
if p.pid != -1 {
return error('runcmd: process already started')
}
printdbg('${@METHOD}: current pid before fork() = ${v_getpid()}') printdbg('${@METHOD}: current pid before fork() = ${v_getpid()}')
printdbg('${@METHOD}: executing pre-fork callback') printdbg('${@METHOD}: executing pre-fork callback')
p.pre_fork_cb(mut p)! p.pre_fork_cb(mut p)!
pid := os.fork() pid := os.fork()
p.pid = pid p.pid = pid
if pid == -1 {
return os.last_error()
}
printdbg('${@METHOD}: pid after fork() = ${pid}') printdbg('${@METHOD}: pid after fork() = ${pid}')
if pid != 0 { if pid != 0 {
// //
// This is the parent process after the fork // This is the parent process
// //
printdbg('${@METHOD}: executing post-fork parent callback') printdbg('${@METHOD}: executing post-fork parent callback')
p.post_fork_parent_cb(mut p)! p.post_fork_parent_cb(mut p)!
return pid } else {
}
// //
// This is the child process // This is the child process
// //
printdbg('${@METHOD}: executing post-fork child callback') printdbg('${@METHOD}: executing post-fork child callback')
p.post_fork_child_cb(mut p)! p.post_fork_child_cb(mut p)!
if p.dir != '' { if p.dir != '' {
@@ -126,6 +133,8 @@ pub fn (mut p Process) start() !int {
env << k + '=' + v env << k + '=' + v
} }
os.execve(p.path, p.argv, env)! os.execve(p.path, p.argv, env)!
}
return pid return pid
} }