omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / src / omega_clean_clone.c
| 1 | #include "omega_clean_clone.h" |
| 2 | |
| 3 | #include <errno.h> |
| 4 | #include <fcntl.h> |
| 5 | #include <ftw.h> |
| 6 | #include <signal.h> |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
| 10 | #include <sys/stat.h> |
| 11 | #include <sys/types.h> |
| 12 | #include <sys/wait.h> |
| 13 | #include <unistd.h> |
| 14 | |
| 15 | bool omega_cc_path_is_plain(const char *s) { |
| 16 | if (!s || !*s) return false; |
| 17 | for (const char *p = s; *p; p++) { |
| 18 | char c = *p; |
| 19 | bool ok = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || |
| 20 | c == '/' || c == '.' || c == '_' || c == '+' || c == '-'; |
| 21 | if (!ok) return false; |
| 22 | } |
| 23 | return true; |
| 24 | } |
| 25 | |
| 26 | /* Child side: environment, directory, output, then exec. Never returns. */ |
| 27 | static void cc_child(const char *dir, const char *prog, char *const argv[], const char *out_rel, bool in_clean_clone) { |
| 28 | if (unsetenv("OUT_DIR") != 0) _exit(127); |
| 29 | if (in_clean_clone && setenv("OMEGA_IN_CLEAN_CLONE", "1", 1) != 0) _exit(127); |
| 30 | if (dir && chdir(dir) != 0) _exit(127); |
| 31 | /* Opened after chdir: the name is fixed by the caller, never built from a path. */ |
| 32 | int out_fd = out_rel ? open(out_rel, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW, 0644) |
| 33 | : open("/dev/null", O_WRONLY | O_CLOEXEC); |
| 34 | if (out_fd < 0) _exit(126); |
| 35 | if (dup2(out_fd, STDOUT_FILENO) < 0) _exit(127); |
| 36 | if (dup2(out_fd, STDERR_FILENO) < 0) _exit(127); |
| 37 | if (out_fd != STDOUT_FILENO && out_fd != STDERR_FILENO) close(out_fd); |
| 38 | execvp(prog, argv); |
| 39 | _exit(127); |
| 40 | } |
| 41 | |
| 42 | static int cc_wait(pid_t pid) { |
| 43 | int status = 0; |
| 44 | while (waitpid(pid, &status, 0) < 0) { |
| 45 | if (errno != EINTR) return -1; |
| 46 | } |
| 47 | if (WIFEXITED(status)) return WEXITSTATUS(status); |
| 48 | if (WIFSIGNALED(status)) return 128 + WTERMSIG(status); |
| 49 | return -1; |
| 50 | } |
| 51 | |
| 52 | int omega_cc_run(const char *dir, const char *prog, char *const argv[], const char *out_rel, bool in_clean_clone) { |
| 53 | if (!prog || !argv || !argv[0]) return -1; |
| 54 | fflush(NULL); |
| 55 | pid_t pid = fork(); |
| 56 | if (pid < 0) return -1; |
| 57 | if (pid == 0) cc_child(dir, prog, argv, out_rel, in_clean_clone); |
| 58 | return cc_wait(pid); |
| 59 | } |
| 60 | |
| 61 | bool omega_cc_capture_line(const char *dir, const char *prog, char *const argv[], char *out, size_t out_size) { |
| 62 | if (!prog || !argv || !argv[0] || !out || out_size == 0) return false; |
| 63 | out[0] = '\0'; |
| 64 | int fds[2]; |
| 65 | if (pipe2(fds, O_CLOEXEC) != 0) return false; |
| 66 | int err_fd = open("/dev/null", O_WRONLY | O_CLOEXEC); |
| 67 | if (err_fd < 0) { |
| 68 | close(fds[0]); |
| 69 | close(fds[1]); |
| 70 | return false; |
| 71 | } |
| 72 | fflush(NULL); |
| 73 | pid_t pid = fork(); |
| 74 | if (pid < 0) { |
| 75 | close(fds[0]); |
| 76 | close(fds[1]); |
| 77 | close(err_fd); |
| 78 | return false; |
| 79 | } |
| 80 | if (pid == 0) { |
| 81 | if (dir && chdir(dir) != 0) _exit(127); |
| 82 | if (dup2(fds[1], STDOUT_FILENO) < 0) _exit(127); |
| 83 | if (dup2(err_fd, STDERR_FILENO) < 0) _exit(127); |
| 84 | execvp(prog, argv); |
| 85 | _exit(127); |
| 86 | } |
| 87 | close(fds[1]); |
| 88 | close(err_fd); |
| 89 | size_t used = 0; |
| 90 | bool overflow = false; |
| 91 | char buf[256]; |
| 92 | for (;;) { |
| 93 | ssize_t n = read(fds[0], buf, sizeof(buf)); |
| 94 | if (n < 0 && errno == EINTR) continue; |
| 95 | if (n <= 0) break; |
| 96 | for (ssize_t i = 0; i < n; i++) { |
| 97 | if (used + 1 < out_size) out[used++] = buf[i]; |
| 98 | else overflow = true; |
| 99 | } |
| 100 | } |
| 101 | close(fds[0]); |
| 102 | out[used] = '\0'; |
| 103 | int rc = cc_wait(pid); |
| 104 | char *nl = strpbrk(out, "\r\n"); |
| 105 | if (nl) *nl = '\0'; |
| 106 | else if (overflow) return false; |
| 107 | return rc == 0 && out[0] != '\0'; |
| 108 | } |
| 109 | |
| 110 | static int cc_remove_entry(const char *path, const struct stat *sb, int type, struct FTW *ftw) { |
| 111 | (void)sb; |
| 112 | (void)ftw; |
| 113 | int rc = (type == FTW_DP) ? rmdir(path) : unlink(path); |
| 114 | if (rc != 0 && errno != ENOENT) { |
| 115 | fprintf(stderr, "clean clone: cannot remove %s: %s\n", path, strerror(errno)); |
| 116 | return -1; |
| 117 | } |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | int omega_cc_remove_tree(const char *path) { |
| 122 | if (!omega_cc_path_is_plain(path) || path[0] != '/' || strcmp(path, "/") == 0) { |
| 123 | fprintf(stderr, "clean clone: refusing to remove '%s' (not a plain absolute path)\n", |
| 124 | path ? path : "(null)"); |
| 125 | return -1; |
| 126 | } |
| 127 | struct stat st; |
| 128 | if (lstat(path, &st) != 0) return (errno == ENOENT) ? 0 : -1; |
| 129 | /* FTW_DEPTH: children before their directory. FTW_PHYS: never follow a symlink, |
| 130 | * so a link inside the copy cannot reach outside it. FTW_MOUNT: stay on one |
| 131 | * file system. */ |
| 132 | return nftw(path, cc_remove_entry, 32, FTW_DEPTH | FTW_PHYS | FTW_MOUNT) == 0 ? 0 : -1; |
| 133 | } |