diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-03-21 20:16:28 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-03-21 20:16:28 -0400 |
| commit | c9cb7ccb40d99f54e35ec5068cb3cba0514bb45f (patch) | |
| tree | 73c6b534a838dc25dc76e45006edc8a24a5ebc17 | |
| parent | c951ee59157ca351f3539a62c377890346e7194d (diff) | |
Clean up executable after running it
| -rw-r--r-- | tomo.c | 16 |
1 files changed, 14 insertions, 2 deletions
@@ -5,6 +5,7 @@ #include <gc/cord.h> #include <libgen.h> #include <printf.h> +#include <spawn.h> #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> @@ -198,8 +199,19 @@ int main(int argc, char *argv[]) for (int64_t i = 0; i < args.length; i++) prog_args[i + 1] = Text$as_c_string(*(Text_t*)(args.data + i*args.stride)); prog_args[1 + args.length] = NULL; - execv(prog_args[0], prog_args); - errx(1, "Failed to run compiled program"); + pid_t child; + if (posix_spawn(&child, prog_args[0], NULL, NULL, prog_args, NULL) != 0) + err(1, "Failed to run compiled program: %s", prog_args[0]); + assert(child); + int status; + while (waitpid(child, &status, 0) < 0 && errno == EINTR) { + if (WIFEXITED(status) || WIFSIGNALED(status)) + break; + else if (WIFSTOPPED(status)) + kill(child, SIGCONT); + } + unlink(prog_args[0]); + return WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE; } else { env_t *env = global_env(); Array_t object_files = {}, |
