| // SPDX-License-Identifier: GPL-2.0-only | |
| // Copyright (c) 2026 Jarkko Sakkinen | |
| #include <signal.h> | |
| #include <sys/wait.h> | |
| #include "base.h" | |
| #include "tracee.h" | |
| void tracee_kill(struct tracee *tracee) | |
| { | |
| if (tracee->pid > 0) | |
| kill(tracee->pid, SIGKILL); | |
| } | |
| void tracee_destroy(struct tracee *tracee) | |
| { | |
| if (tracee->pid <= 0) | |
| return; | |
| tracee_kill(tracee); | |
| while (waitpid(tracee->pid, NULL, 0) < 0 && errno == EINTR) | |
| ; | |
| tracee->pid = 0; | |
| } | |
| int tracee_poll_exit(struct tracee *tracee, int *exit_code) | |
| { | |
| int status; | |
| pid_t waited; | |
| if (tracee->pid <= 0) | |
| return 0; | |
| do { | |
| waited = waitpid(tracee->pid, &status, WNOHANG); | |
| } while (waited < 0 && errno == EINTR); | |
| if (waited < 0) { | |
| int err = errno; | |
| if (err == ECHILD) | |
| tracee->pid = 0; | |
| return err_ctx(-err, "waitpid tracee"); | |
| } | |
| if (!waited) | |
| return 0; | |
| if (WIFEXITED(status)) | |
| *exit_code = WEXITSTATUS(status); | |
| else if (WIFSIGNALED(status)) | |
| *exit_code = 128 + WTERMSIG(status); | |
| else | |
| return 0; | |
| tracee->pid = 0; | |
| return 1; | |
| } |