Skip to content
suit Artifact [ad7f42b132]
Login

Artifact [ad7f42b132]

Artifact ad7f42b132951db62315e3992fd9e8e75e7d014ddb46313f5f7e88cd6b7be874:


// suit-run: the program a timer starts every minute. It does only the
// no-change compare, which touches no database, so it starts as fast as a
// program with nothing in it. When something differs it replaces itself with
// `suit run`, which does the work. It is built from stamp.c, hook.c and
// platform.c alone, with no reference to the store. `suit-run hook EVENT` is
// the hook dispatcher, which a harness starts (docs/hooks.md).

#include "hook.h"
#include "platform.h"
#include "stamp.h"

#include <errno.h>
#include <string.h>
#include <unistd.h>

constexpr size_t PATH_CAP = 4096;

int main(int argc, char **argv)
{
    // `suit-run hook EVENT` is the hook dispatcher (docs/hooks.md).
    if (argc >= 2 && strcmp(argv[1], "hook") == 0) {
        if (argc == 3) return hook_dispatch(argv[2]);
        say(STDERR_FILENO, "usage: suit-run hook EVENT\n");
        return HOOK_EXIT_USAGE;
    }
    int rc = stamp_check(false);
    if (rc != EXIT_CHANGED) return rc;

    char suit[PATH_CAP + 8];
    if (!platform_self_path(suit, PATH_CAP)) {
        say(STDERR_FILENO, "suit-run: cannot find the path of this program\n");
        return 1;
    }
    // suit sits beside suit-run.
    char *slash = strrchr(suit, '/');
    ensure(slash != nullptr);
    memcpy(slash + 1, "suit", sizeof "suit");
    char *run_argv[] = {(char *)"suit", (char *)"run", nullptr};
    (void)sys_execv(suit, run_argv);
    say(STDERR_FILENO, "suit-run: cannot run %s: %s\n", suit, strerror(errno));
    return 1;
}