Artifact 9b1f7fe75eca11fee14d87d3fe4e98a11cd7a4186addc950d0eee9d2c8331d0a:
- File
src/attention.c
— part of check-in
[7ecc8d52ea]
at
2026-11-14 06:52:19
on branch trunk
— Phase 7c-2: suit install sets up the two timers, and status has a one-line form
suit install writes two timers and loads them: one starts suit-run every minute, the other starts suit tend every hour. On macOS they are launchd user agents in ~/Library/LaunchAgents; on Linux, systemd user units. Each file is written with exactly mode 0644 whatever the umask. Running install again with nothing changed writes nothing and loads nothing. suit install --print shows the files and changes nothing. suit uninstall unloads the timers and removes exactly its own files. Neither touches a file that is not one of suit's own names.
Two timers stand in place of the planned single timer with a detached hourly child: the minute run stays inside its speed budget, and each job can be seen and stopped by itself.
The loader (launchctl or systemctl) is started by absolute path, never through a shell. Its path and the platform can be overridden only in the test builds; a test checks the release binary holds neither name.
suit status --line prints one short line for a status bar: suit ok, how many things need you, or not installed. Plain suit status says whether the timers are installed.
Known gaps. If the loader fails, the files stay in place and a second install says already installed without trying to load again; that needs a retry and a line in the attention file. The Linux units are checked as text and as loader arguments on macOS; systemd itself has not run them.
Found by the local gate before this check-in: an analyzer report, tests that needed a user name, and timer files written with the wrong mode under a strict umask.
1269 unit checks, 428 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. 100% coverage in all 23 source files. No suite has fewer cases than at the last check-in. (user: dmestas, size: 4844) [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; }