Skip to content

Commit

Permalink
os, v.builder: show more details, when a program ran by `v run file.v…
Browse files Browse the repository at this point in the history
…`, exits by a signal (fix #19412) (#19471)
  • Loading branch information
spytheman authored Sep 29, 2023
1 parent 1476daf commit 24278d8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 25 deletions.
7 changes: 4 additions & 3 deletions vlib/os/os.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -429,10 +429,11 @@ pub fn system(cmd string) int {
}
$if !windows {
pret, is_signaled := posix_wait4_to_exit_status(ret)
ret = pret
if is_signaled {
println('Terminated by signal ${ret:2d} (' + sigint_to_signal_name(pret) + ')')
eprintln('Terminated by signal ${pret:2d} (' + sigint_to_signal_name(pret) + ')')
ret = pret + 128
}
ret = pret
}
return ret
}
Expand Down Expand Up @@ -541,7 +542,7 @@ pub fn rmdir(path string) ! {
fn print_c_errno() {
e := C.errno
se := unsafe { tos_clone(&u8(C.strerror(e))) }
println('errno=${e} err=${se}')
eprintln('errno=${e} err=${se}')
}

// get_raw_line returns a one-line string from stdin along with '\n' if there is any.
Expand Down
34 changes: 12 additions & 22 deletions vlib/os/process_nix.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -83,42 +83,32 @@ fn (mut p Process) unix_kill_pgroup() {
}

fn (mut p Process) unix_wait() {
cstatus := 0
mut ret := -1
$if !emscripten ? {
ret = C.waitpid(p.pid, &cstatus, 0)
}
if ret == -1 {
p.err = posix_get_error_msg(C.errno)
return
}
pret, is_signaled := posix_wait4_to_exit_status(cstatus)
if is_signaled {
p.status = .aborted
p.err = 'Terminated by signal ${ret:2d} (${sigint_to_signal_name(pret)})'
} else {
p.status = .exited
}
p.code = pret
p.impl_check_pid_status(false, 0)
}

fn (mut p Process) unix_is_alive() bool {
cstatus := 0
return p.impl_check_pid_status(true, C.WNOHANG)
}

fn (mut p Process) impl_check_pid_status(exit_early_on_ret0 bool, waitpid_options int) bool {
mut cstatus := 0
mut ret := -1
$if !emscripten ? {
ret = C.waitpid(p.pid, &cstatus, C.WNOHANG)
ret = C.waitpid(p.pid, &cstatus, waitpid_options)
}
p.code = ret
if ret == -1 {
p.err = posix_get_error_msg(C.errno)
return false
}
if ret == 0 {
if exit_early_on_ret0 && ret == 0 {
return true
}
pret, is_signaled := posix_wait4_to_exit_status(cstatus)
mut pret, is_signaled := posix_wait4_to_exit_status(cstatus)
if is_signaled {
p.status = .aborted
p.err = 'Terminated by signal ${ret:2d} (${sigint_to_signal_name(pret)})'
p.err = 'Terminated by signal ${pret:2d} (${sigint_to_signal_name(pret)})'
pret += 128
} else {
p.status = .exited
}
Expand Down
3 changes: 3 additions & 0 deletions vlib/v/builder/compile.v
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ fn (mut b Builder) run_compiled_executable_and_exit() {
os.signal_opt(.quit, prev_quit_handler) or { serror('restore .quit', err) }
}
ret = run_process.code
if run_process.err != '' {
eprintln(run_process.err)
}
run_process.close()
}
b.cleanup_run_executable_after_exit(compiled_file)
Expand Down

0 comments on commit 24278d8

Please sign in to comment.