Artifact 7cea46358138cb9e6c5e27effd0f95b094722d4d8b939bb6359586386d78b46b:
- File
src/attention.c
— part of check-in
[79385a1b35]
at
2026-11-14 11:41:21
on branch trunk
— suit install retries a load that failed, and says so until it works
When the loader fails, suit install now writes one item to the attention file: timers load: written but not loaded, with the loader's name and exit status, or could not run when it never started. suit status and suit status --line show it. Running suit install again, with the files unchanged and that item present, loads again in place of saying already installed. A success removes the item; suit uninstall removes it too. If writing the item fails as well, install still exits 3 and reports both.
make quick now rebuilds the dev binaries before it runs, so it cannot test a stale build.
49 new shell cases. 1269 unit checks, 477 shell cases, 669 store cases, 612 placement cases and 378 project cases, under clang, under gcc 16, with no user name set, and under a strict umask, in one run of make gate (31 minutes). 100% coverage in all 23 source files. No suite has fewer cases than at the last check-in. (user: dmestas, size: 5283) [annotate] [blame] [check-ins using]
#include "attention.h" #include "platform.h" #include <errno.h> #include <fcntl.h> #include <stdio.h> #include <string.h> #include <unistd.h> static char cur[ATTENTION_FILE_MAX + 1]; static char out[ATTENTION_FILE_MAX + 1]; // Read the file into cur. A file that is not there is empty. static int load(const char *path, size_t *len) { *len = 0; int fd = sys_open(path, O_RDONLY, 0); if (fd < 0 && errno == ENOENT) return 0; if (fd < 0) { say(STDERR_FILENO, "suit: cannot open %s: %s\n", path, strerror(errno)); return 1; } ssize_t n = 1; while (*len <= ATTENTION_FILE_MAX && n > 0) { n = sys_read(fd, cur + *len, ATTENTION_FILE_MAX + 1 - *len); if (n < 0 && errno == EINTR) { n = 1; continue; } *len += n > 0 ? (size_t)n : 0; } int err = errno; (void)sys_close(fd); if (n < 0) { say(STDERR_FILENO, "suit: cannot read %s: %s\n", path, strerror(err)); return 1; } if (*len > ATTENTION_FILE_MAX) { say(STDERR_FILENO, "suit: %s is over %zu bytes\n", path, ATTENTION_FILE_MAX); return 1; } return 0; } // Whether the header line LINE ("KIND NAME" or "KIND NAME: ...") is the item // of that kind (any if KIND is null) for NAME. static bool item_is(const char *line, size_t n, const char *kind, const char *name) { const char *sp = memchr(line, ' ', n); if (sp == nullptr) return false; size_t kl = (size_t)(sp - line); if (kind != nullptr && (strlen(kind) != kl || memcmp(line, kind, kl) != 0)) return false; const char *w = sp + 1; size_t room = n - kl - 1; size_t wl = 0; while (wl < room && w[wl] != ':') wl++; return wl == strlen(name) && memcmp(w, name, wl) == 0; } // Copy cur to out without the items of KIND and NAME. Returns the length of // out, or SIZE_MAX... cannot overflow: out is as big as cur. static size_t filter(size_t len, const char *kind, const char *name) { size_t o = 0; size_t p = 0; bool keep = true; while (p < len) { const char *nl = memchr(cur + p, '\n', len - p); size_t end = nl != nullptr ? (size_t)(nl - cur) : len; if (cur[p] != ' ') keep = !item_is(cur + p, end - p, kind, name); if (keep) { memcpy(out + o, cur + p, end - p); o += end - p; out[o++] = '\n'; } p = end + 1; } return o; } // Write OUT, or remove the file when OUT is empty. CHANGED says whether OUT // differs from what was read. static int store_out(const char *path, size_t o, size_t len) { if (o == len && memcmp(out, cur, o) == 0) return 0; if (o == 0) { if (sys_unlink(path) != 0 && errno != ENOENT) { say(STDERR_FILENO, "suit: cannot remove %s: %s\n", path, strerror(errno)); return 1; } return 0; } int err = sys_write_atomic(path, out, o); if (err != 0) say(STDERR_FILENO, "suit: cannot write %s: %s\n", path, strerror(err)); return err == 0 ? 0 : 1; } int attention_remove(const char *path, const char *kind, const char *name) { size_t len; if (load(path, &len) != 0) return 1; return store_out(path, filter(len, kind, name), len); } int attention_add(const char *path, const char *kind, const char *name, const char *header, const char *details) { size_t len; if (load(path, &len) != 0) return 1; size_t o = filter(len, kind, name); size_t more = strlen(header) + 1 + (details != nullptr ? strlen(details) * 3 : 0); if (o + more > ATTENTION_FILE_MAX) { say(STDERR_FILENO, "suit: %s would be over %zu bytes\n", path, ATTENTION_FILE_MAX); return 1; } o += (size_t)snprintf(out + o, sizeof out - o, "%s\n", header); for (const char *d = details; d != nullptr && *d != '\0';) { size_t n = strcspn(d, "\n"); o += (size_t)snprintf(out + o, sizeof out - o, " %.*s\n", (int)n, d); ensure(d[n] == '\n'); d += n + 1; } return store_out(path, o, len); } int attention_print(const char *path) { size_t len; if (load(path, &len) != 0) return 1; if (len == 0) return 0; say(STDOUT_FILENO, "needs you:\n"); for (size_t p = 0; p < len;) { const char *nl = memchr(cur + p, '\n', len - p); size_t end = nl != nullptr ? (size_t)(nl - cur) : len; say(STDOUT_FILENO, "%.*s\n", (int)(end - p), cur + p); p = end + 1; } return 0; } int attention_count(const char *path, unsigned *count) { size_t len; *count = 0; if (load(path, &len) != 0) return 1; for (size_t p = 0; p < len;) { const char *nl = memchr(cur + p, '\n', len - p); size_t end = nl != nullptr ? (size_t)(nl - cur) : len; if (end > p && cur[p] != ' ') (*count)++; p = end + 1; } return 0; } int attention_has(const char *path, const char *kind, const char *name, bool *has) { size_t len; *has = false; if (load(path, &len) != 0) return 1; for (size_t p = 0; p < len && !*has; p++) { const char *nl = memchr(cur + p, '\n', len - p); size_t end = nl != nullptr ? (size_t)(nl - cur) : len; *has = cur[p] != ' ' && item_is(cur + p, end - p, kind, name); p = end; } return 0; }