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 / osh / host / osh_journal.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * osh_journal.h -- durable effect journal of the osh Linux host adapter (OSH_PLATFORM_ABI.md section 10). |
| 19 | * |
| 20 | * An EFFECT is one external TASK_SPAWN or one write-class open (redirection kinds output/append). Before each effect the |
| 21 | * adapter appends an INTENT record and fsyncs the journal; after the pipeline is reaped it appends one OUTCOME record per |
| 22 | * effect and fsyncs again. After a crash, an intent with no outcome is OUTCOME_UNKNOWN and is NEVER replayed. |
| 23 | * |
| 24 | * This is the MANDATORY effect record (section 10). It is a separate file from any optional history or recording |
| 25 | * (section 13): it is written whatever the recording mode is, and it holds no argument values. |
| 26 | * |
| 27 | * File format (text, one record per line, '\n' terminated, append-only, mode 0600): |
| 28 | * OSHJ1 <rec> I <digest64> <idx>/<n> b=<builtin_id> argc=<k> wr=<w> a0=<argv0, %XX-escaped, last 64 bytes, "~" prefix when cut> lens=<l0,l1,..|-> |
| 29 | * OSHJ1 <rec> O <ref> <digest64> <idx>/<n> <OUTCOME> st=<status> err=<platform error> [recovered=1] |
| 30 | * <rec> is a journal-wide counter (1, 2, ...), never a pid or any transient id. <ref> is the <rec> of the intent this |
| 31 | * outcome closes, or 0 for an outcome with no intent (the effect was refused or never reached: nothing was launched). |
| 32 | * <digest64> is the lowercase hex request digest, HMAC-SHA256 under a random per-journal key kept in <journal>.key (osh_req_digest). OUTCOME is one of NOT_STARTED, COMPLETED, |
| 33 | * FAILED_NO_EFFECT, CANCELLED, OUTCOME_UNKNOWN. |
| 34 | * |
| 35 | * Redaction rule: argument values, assignment values, redirection targets and environment values are never written. |
| 36 | * Only argv[0], the argument count, argument lengths, the builtin id, the write-redirection count, the status and the |
| 37 | * request digest are. The digest is keyed (HMAC-SHA256, random per-journal key in <journal>.key, 0600), so a low-entropy secret |
| 38 | * given as a plain argument cannot be confirmed by hashing guesses against a leaked journal alone; it also replaces the VALUE |
| 39 | * of any assignment whose NAME is secret (osh_secret_name) by a fixed marker. Whoever holds journal AND key can test guesses. |
| 40 | */ |
| 41 | #ifndef OSH_JOURNAL_H |
| 42 | #define OSH_JOURNAL_H |
| 43 | #include <stddef.h> |
| 44 | #include <stdint.h> |
| 45 | |
| 46 | #include "osh_host.h" |
| 47 | |
| 48 | typedef struct OshJournal { |
| 49 | int fd; |
| 50 | uint64_t next_rec; |
| 51 | uint8_t key[32]; /* per-journal HMAC key (loaded from <journal>.key) */ |
| 52 | char why[4700]; /* why osh_journal_open or osh_journal_check_dir refused */ |
| 53 | int poisoned; /* sticky: the first failed write or fsync ends all journaling for this session; every later effect is refused */ |
| 54 | int tail_clean; /* after a failed write the fragment was cut back, so an outcome may still be appended */ |
| 55 | #ifdef OSH_JOURNAL_TEST_HOOKS |
| 56 | /* test-only fault injection; compiled out of the shipped binary */ |
| 57 | int die_after_intent; /* _exit(77) right after the Nth intent became durable (simulates a crash before launch) */ |
| 58 | int n_intent; |
| 59 | int fail_write_at; /* the Nth commit writes only half its bytes, then fails with ENOSPC */ |
| 60 | int fail_fsync_at; /* the Nth commit fails its fsync with EIO */ |
| 61 | int n_commit; |
| 62 | #endif |
| 63 | } OshJournal; |
| 64 | |
| 65 | typedef struct { |
| 66 | uint64_t intent_rec; |
| 67 | char digest[65]; |
| 68 | int idx, ncmds; |
| 69 | char argv0[200]; |
| 70 | } OshJournalUnknown; |
| 71 | |
| 72 | /* Open (create if needed, 0600) and fsync the directory when created; load or atomically create <path>.key (32 random bytes, 0600). |
| 73 | * FAIL CLOSED: a journal or key with another owner or a mode other than 0600, a key of the wrong size, an unreadable key, or a |
| 74 | * missing key beside a journal that has records is refused (-errno, j->why names the reason). 0 ok, -errno. A torn last line (crash during a |
| 75 | * write) is closed with '\n' so the next record starts on its own line. */ |
| 76 | int osh_journal_open(OshJournal *j, const char *path); |
| 77 | void osh_journal_close(OshJournal *j); |
| 78 | |
| 79 | /* Default location of the mandatory journal: $XDG_STATE_HOME/osh/effects.journal, else $HOME/.local/state/osh/effects.journal |
| 80 | * (XDG Base Directory spec; the ABI draft and the osh docs name no location). 0 ok, -1 when neither variable is usable. */ |
| 81 | int osh_journal_default_path(const char *xdg_state_home, const char *home, char *out, size_t cap); |
| 82 | /* mkdir -p the parent directories of path with mode 0700. 0 ok, -errno. */ |
| 83 | int osh_journal_mkparents(const char *path); |
| 84 | |
| 85 | /* Recovery: report every intent with no outcome as UNKNOWN (never replayed; nothing is launched here) and append an |
| 86 | * OUTCOME_UNKNOWN record (recovered=1) for each so it is reported once. Returns the number found (all counted, at most |
| 87 | * cap stored in out), or -errno. *bad (optional) gets the number of unparsable (torn) lines. */ |
| 88 | int osh_journal_recover(OshJournal *j, const char *path, OshJournalUnknown *out, int cap, int *bad); |
| 89 | |
| 90 | /* ---- used by osh_exec.c ---- */ |
| 91 | int osh_secret_name(const char *name); /* 1 when the variable name is in the secret class (see osh_journal.c) */ |
| 92 | /* Request digest: HMAC-SHA256 under the journal's random per-journal key over a canonical encoding of the request. */ |
| 93 | void osh_req_digest(const OshJournal *j, const OshRequest *r, uint8_t out[32]); |
| 94 | /* The same encoding under plain SHA-256. NOT used by the host; it exists so the tests can show that the recorded (keyed) |
| 95 | * digest cannot be matched by guessing arguments and hashing them. */ |
| 96 | void osh_req_digest_unkeyed(const OshRequest *r, uint8_t out[32]); |
| 97 | /* A state directory that already exists must be ours with mode 0700, else refuse (-EACCES, j->why says why). */ |
| 98 | int osh_journal_check_dir(OshJournal *j, const char *dir); |
| 99 | /* Append and fsync an intent for command idx. Returns its rec (> 0) or 0 when it is not durable (the effect must not run). */ |
| 100 | uint64_t osh_journal_intent(OshJournal *j, const uint8_t dig[32], const OshRequest *r, int idx); |
| 101 | |
| 102 | typedef struct { |
| 103 | int idx; |
| 104 | uint64_t ref; /* intent rec or 0 */ |
| 105 | int outcome, status, err; |
| 106 | } OshJournalOutcome; |
| 107 | /* Append all outcomes in one write and fsync. 0 ok, -1 not durable. */ |
| 108 | int osh_journal_outcomes(OshJournal *j, const uint8_t dig[32], int ncmds, const OshJournalOutcome *o, int n); |
| 109 | #endif |