Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os: Add proper process termination in Linux #20671

Merged
merged 10 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions vlib/os/process.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ pub fn (mut p Process) signal_kill() {
}
p._signal_kill()
p.status = .aborted
return
}

// signal_term - terminate the process
pub fn (mut p Process) signal_term() {
if p.status !in [.running, .stopped] {
return
}
p._signal_term()
}

// signal_pgkill - kills the whole process group
Expand All @@ -23,7 +30,6 @@ pub fn (mut p Process) signal_pgkill() {
return
}
p._signal_pgkill()
return
}

// signal_stop - stops the process, you can resume it with p.signal_continue()
Expand All @@ -33,7 +39,6 @@ pub fn (mut p Process) signal_stop() {
}
p._signal_stop()
p.status = .stopped
return
}

// signal_continue - tell a stopped process to continue/resume its work
Expand All @@ -43,7 +48,6 @@ pub fn (mut p Process) signal_continue() {
}
p._signal_continue()
p.status = .running
return
}

// wait - wait for a process to finish.
Expand All @@ -60,7 +64,6 @@ pub fn (mut p Process) wait() {
return
}
p._wait()
return
}

// close - free the OS resources associated with the process.
Expand Down Expand Up @@ -132,7 +135,6 @@ pub fn (mut p Process) set_redirect_stdio() {
$if trace_process_pipes ? {
eprintln('${@LOCATION}, pid: ${p.pid}, status: ${p.status}')
}
return
}

// stdin_write will write the string `s`, to the stdin pipe of the child process.
Expand Down Expand Up @@ -290,6 +292,15 @@ fn (mut p Process) _signal_kill() {
}
}

// _signal_term - should not be called directly, except by p.signal_term
fn (mut p Process) _signal_term() {
$if windows {
p.win_term_process()
} $else {
p.unix_term_process()
}
}

// _signal_pgkill - should not be called directly, except by p.signal_pgkill
fn (mut p Process) _signal_pgkill() {
$if windows {
Expand Down Expand Up @@ -323,5 +334,4 @@ pub fn (mut p Process) run() {
return
}
p._spawn()
return
}
7 changes: 7 additions & 0 deletions vlib/os/process_nix.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ fn (mut p Process) unix_resume_process() {
C.kill(p.pid, C.SIGCONT)
}

fn (mut p Process) unix_term_process() {
C.kill(p.pid, C.SIGTERM)
}

fn (mut p Process) unix_kill_process() {
C.kill(p.pid, C.SIGKILL)
}
Expand Down Expand Up @@ -127,6 +131,9 @@ fn (mut p Process) win_stop_process() {
fn (mut p Process) win_resume_process() {
}

fn (mut p Process) win_term_process() {
}

fn (mut p Process) win_kill_process() {
}

Expand Down
7 changes: 7 additions & 0 deletions vlib/os/process_windows.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ fn (mut p Process) win_kill_process() {
C.TerminateProcess(wdata.proc_info.h_process, 3)
}

fn (mut p Process) win_term_process() {
p.win_kill_process()
}

fn (mut p Process) win_kill_pgroup() {
wdata := unsafe { &WProcess(p.wdata) }
C.GenerateConsoleCtrlEvent(C.CTRL_BREAK_EVENT, wdata.proc_info.dw_process_id)
Expand Down Expand Up @@ -279,6 +283,9 @@ fn (mut p Process) unix_stop_process() {
fn (mut p Process) unix_resume_process() {
}

fn (mut p Process) unix_term_process() {
}

fn (mut p Process) unix_kill_process() {
}

Expand Down
Loading