aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / core / lifecycle_fsm.c
| 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 | /* lifecycle_fsm.c -- see lifecycle_fsm.h. No kernel calls, no libc. */ |
| 18 | #include "lifecycle_fsm.h" |
| 19 | |
| 20 | static int printable(char c) { return c >= 0x20 && c <= 0x7e; } |
| 21 | |
| 22 | static int word_is(const char *w, size_t n, const char *lit) |
| 23 | { |
| 24 | size_t i; |
| 25 | for (i = 0; lit[i]; i++) |
| 26 | if (i >= n || w[i] != lit[i]) return 0; |
| 27 | return i == n; |
| 28 | } |
| 29 | |
| 30 | enum ck_lc_verb ck_lc_decode(const char *line, size_t len, const char **arg, size_t *arg_len) |
| 31 | { |
| 32 | size_t i = 0, ws, we; |
| 33 | *arg = line; |
| 34 | *arg_len = 0; |
| 35 | if (len > CK_LC_LINE_MAX) return CK_LC_V_INVALID; |
| 36 | for (i = 0; i < len; i++) |
| 37 | if (!printable(line[i])) return CK_LC_V_INVALID; |
| 38 | i = 0; |
| 39 | while (i < len && line[i] == ' ') i++; |
| 40 | if (i == len) return CK_LC_V_EMPTY; |
| 41 | ws = i; |
| 42 | while (i < len && line[i] != ' ') i++; |
| 43 | we = i; |
| 44 | while (i < len && line[i] == ' ') i++; |
| 45 | /* trailing blanks are not part of the argument */ |
| 46 | { |
| 47 | size_t end = len; |
| 48 | while (end > i && line[end - 1] == ' ') end--; |
| 49 | *arg = line + i; |
| 50 | *arg_len = end - i; |
| 51 | } |
| 52 | const char *w = line + ws; |
| 53 | size_t n = we - ws; |
| 54 | if (word_is(w, n, "task")) { |
| 55 | if (*arg_len == 0 || *arg_len > CK_LC_TASK_MAX) return CK_LC_V_INVALID; |
| 56 | return CK_LC_V_TASK; |
| 57 | } |
| 58 | if (word_is(w, n, "prompt")) { |
| 59 | if (*arg_len == 0 || *arg_len > CK_LC_TASK_MAX) return CK_LC_V_INVALID; |
| 60 | return CK_LC_V_PROMPT; |
| 61 | } |
| 62 | if (word_is(w, n, "remember")) { |
| 63 | if (*arg_len == 0 || *arg_len > CK_LC_TASK_MAX) return CK_LC_V_INVALID; |
| 64 | return CK_LC_V_REMEMBER; |
| 65 | } |
| 66 | if (word_is(w, n, "prefer")) { |
| 67 | if (*arg_len == 0 || *arg_len > CK_LC_TASK_MAX) return CK_LC_V_INVALID; |
| 68 | return CK_LC_V_PREFER; |
| 69 | } |
| 70 | /* unlock carries the optional recovery secret (the binding checks its |
| 71 | * shape and never echoes it); every other verb takes no argument. */ |
| 72 | if (word_is(w, n, "unlock")) return CK_LC_V_UNLOCK; |
| 73 | /* rotate carries "<current 64-hex> <new 64-hex>" (the binding checks it) */ |
| 74 | if (word_is(w, n, "rotate")) return CK_LC_V_ROTATE; |
| 75 | /* update carries an optional argument the binding checks (slot_service.c) */ |
| 76 | if (word_is(w, n, "update")) return CK_LC_V_UPDATE; |
| 77 | if (*arg_len != 0) return CK_LC_V_INVALID; |
| 78 | if (word_is(w, n, "help")) return CK_LC_V_HELP; |
| 79 | if (word_is(w, n, "status")) return CK_LC_V_STATUS; |
| 80 | if (word_is(w, n, "approve")) return CK_LC_V_APPROVE; |
| 81 | if (word_is(w, n, "deny")) return CK_LC_V_DENY; |
| 82 | if (word_is(w, n, "cancel")) return CK_LC_V_CANCEL; |
| 83 | if (word_is(w, n, "recall")) return CK_LC_V_RECALL; |
| 84 | if (word_is(w, n, "facts")) return CK_LC_V_FACTS; |
| 85 | if (word_is(w, n, "evidence")) return CK_LC_V_EVIDENCE; |
| 86 | if (word_is(w, n, "shutdown")) return CK_LC_V_SHUTDOWN; |
| 87 | return CK_LC_V_UNKNOWN; |
| 88 | } |
| 89 | |
| 90 | const char *ck_lc_verb_name(enum ck_lc_verb v) |
| 91 | { |
| 92 | switch (v) { |
| 93 | case CK_LC_V_INVALID: return "invalid"; |
| 94 | case CK_LC_V_UNKNOWN: return "unknown"; |
| 95 | case CK_LC_V_EMPTY: return "empty"; |
| 96 | case CK_LC_V_HELP: return "help"; |
| 97 | case CK_LC_V_STATUS: return "status"; |
| 98 | case CK_LC_V_UNLOCK: return "unlock"; |
| 99 | case CK_LC_V_TASK: return "task"; |
| 100 | case CK_LC_V_APPROVE: return "approve"; |
| 101 | case CK_LC_V_DENY: return "deny"; |
| 102 | case CK_LC_V_CANCEL: return "cancel"; |
| 103 | case CK_LC_V_EVIDENCE: return "evidence"; |
| 104 | case CK_LC_V_SHUTDOWN: return "shutdown"; |
| 105 | case CK_LC_V_PROMPT: return "prompt"; |
| 106 | case CK_LC_V_RECALL: return "recall"; |
| 107 | case CK_LC_V_REMEMBER: return "remember"; |
| 108 | case CK_LC_V_FACTS: return "facts"; |
| 109 | case CK_LC_V_ROTATE: return "rotate"; |
| 110 | case CK_LC_V_UPDATE: return "update"; |
| 111 | case CK_LC_V_PREFER: return "prefer"; |
| 112 | } |
| 113 | return "?"; |
| 114 | } |
| 115 | |
| 116 | const char *ck_lc_state_name(enum ck_lc_state s) |
| 117 | { |
| 118 | switch (s) { |
| 119 | case CK_LC_INIT: return "init"; |
| 120 | case CK_LC_LOCKED: return "locked"; |
| 121 | case CK_LC_READY: return "ready"; |
| 122 | case CK_LC_SERVE: return "serve"; |
| 123 | case CK_LC_QUIESCE: return "quiesce"; |
| 124 | case CK_LC_STOP: return "stop"; |
| 125 | } |
| 126 | return "?"; |
| 127 | } |
| 128 | |
| 129 | void ck_lc_init(ck_lc *lc) |
| 130 | { |
| 131 | size_t i; |
| 132 | unsigned char *p = (unsigned char *)lc; |
| 133 | for (i = 0; i < sizeof *lc; i++) p[i] = 0; |
| 134 | lc->state = CK_LC_INIT; |
| 135 | lc->reason = ""; |
| 136 | } |
| 137 | |
| 138 | void ck_lc_start(ck_lc *lc, int store_open) |
| 139 | { |
| 140 | lc->state = store_open ? CK_LC_READY : CK_LC_LOCKED; |
| 141 | } |
| 142 | |
| 143 | static enum ck_lc_result refuse(ck_lc *lc, const char *why) |
| 144 | { |
| 145 | lc->refused++; |
| 146 | lc->reason = why; |
| 147 | return CK_LC_R_REFUSED; |
| 148 | } |
| 149 | |
| 150 | static void drop_task(ck_lc *lc) |
| 151 | { |
| 152 | size_t i; |
| 153 | for (i = 0; i < sizeof lc->task; i++) lc->task[i] = 0; |
| 154 | for (i = 0; i < sizeof lc->reply; i++) lc->reply[i] = 0; |
| 155 | lc->task_len = 0; |
| 156 | lc->reply_len = 0; |
| 157 | lc->pending = CK_LC_PENDING_NONE; |
| 158 | } |
| 159 | |
| 160 | static void set_task(ck_lc *lc, const char *arg, size_t arg_len) |
| 161 | { |
| 162 | size_t i; |
| 163 | for (i = 0; i < arg_len; i++) lc->task[i] = arg[i]; |
| 164 | lc->task[arg_len] = 0; |
| 165 | lc->task_len = arg_len; |
| 166 | } |
| 167 | |
| 168 | enum ck_lc_result ck_lc_apply(ck_lc *lc, enum ck_lc_verb v, const char *arg, size_t arg_len) |
| 169 | { |
| 170 | lc->requests++; |
| 171 | lc->reason = ""; |
| 172 | if (lc->state == CK_LC_QUIESCE || lc->state == CK_LC_STOP) return refuse(lc, "stopping"); |
| 173 | if (lc->state == CK_LC_INIT) return refuse(lc, "not started"); |
| 174 | switch (v) { |
| 175 | case CK_LC_V_INVALID: |
| 176 | lc->invalid++; |
| 177 | lc->reason = "line refused (bounds or syntax)"; |
| 178 | return CK_LC_R_INVALID; |
| 179 | case CK_LC_V_UNKNOWN: return refuse(lc, "unknown verb"); |
| 180 | case CK_LC_V_EMPTY: |
| 181 | case CK_LC_V_HELP: |
| 182 | case CK_LC_V_STATUS: |
| 183 | case CK_LC_V_EVIDENCE: return CK_LC_R_OK; |
| 184 | case CK_LC_V_UNLOCK: |
| 185 | if (lc->state != CK_LC_LOCKED) return refuse(lc, "already unlocked"); |
| 186 | return CK_LC_R_UNLOCK; |
| 187 | case CK_LC_V_ROTATE: |
| 188 | if (lc->state == CK_LC_LOCKED) return refuse(lc, "locked: unlock before rotating the recovery secret"); |
| 189 | if (lc->state == CK_LC_SERVE) return refuse(lc, "a task is pending: approve, deny or cancel it first"); |
| 190 | if (arg_len == 0) return refuse(lc, "rotate needs <current 64-hex> <new 64-hex>"); |
| 191 | return CK_LC_R_ROTATE; |
| 192 | case CK_LC_V_UPDATE: |
| 193 | if (lc->state == CK_LC_LOCKED) return refuse(lc, "locked: unlock before updating a slot"); |
| 194 | if (lc->state == CK_LC_SERVE) return refuse(lc, "a task is pending: approve, deny or cancel it first"); |
| 195 | lc->updates++; |
| 196 | return CK_LC_R_UPDATE; |
| 197 | case CK_LC_V_TASK: |
| 198 | if (lc->state == CK_LC_LOCKED) return refuse(lc, "locked: no production Store"); |
| 199 | if (lc->state == CK_LC_SERVE) return refuse(lc, "a task is pending: approve, deny or cancel it first"); |
| 200 | if (arg_len == 0 || arg_len > CK_LC_TASK_MAX) return refuse(lc, "task text out of bounds"); |
| 201 | set_task(lc, arg, arg_len); |
| 202 | lc->pending = CK_LC_PENDING_NOTE; |
| 203 | lc->tasks++; |
| 204 | lc->state = CK_LC_SERVE; |
| 205 | return CK_LC_R_OK; |
| 206 | case CK_LC_V_PROMPT: |
| 207 | if (lc->state == CK_LC_LOCKED) return refuse(lc, "locked: no production Store"); |
| 208 | if (lc->state == CK_LC_SERVE) return refuse(lc, "a task is pending: approve, deny or cancel it first"); |
| 209 | if (arg_len == 0 || arg_len > CK_LC_TASK_MAX) return refuse(lc, "prompt text out of bounds"); |
| 210 | set_task(lc, arg, arg_len); /* the prompt; the reply arrives through ck_lc_generate_done */ |
| 211 | lc->prompts++; |
| 212 | return CK_LC_R_GENERATE; |
| 213 | case CK_LC_V_RECALL: |
| 214 | if (lc->state == CK_LC_LOCKED) return refuse(lc, "locked: no production Store"); |
| 215 | lc->recalls++; |
| 216 | return CK_LC_R_RECALL; |
| 217 | case CK_LC_V_REMEMBER: { |
| 218 | size_t i; |
| 219 | if (lc->state == CK_LC_LOCKED) return refuse(lc, "locked: no production Store"); |
| 220 | if (arg_len == 0 || arg_len > CK_LC_TASK_MAX) return refuse(lc, "fact text out of bounds"); |
| 221 | for (i = 0; i < arg_len; i++) lc->fact[i] = arg[i]; |
| 222 | lc->fact[arg_len] = 0; |
| 223 | lc->fact_len = arg_len; /* the pending task (if any) is untouched: remember never changes the state */ |
| 224 | lc->remembers++; |
| 225 | return CK_LC_R_REMEMBER; |
| 226 | } |
| 227 | case CK_LC_V_PREFER: { |
| 228 | size_t i; |
| 229 | if (lc->state == CK_LC_LOCKED) return refuse(lc, "locked: no production Store"); |
| 230 | if (arg_len == 0 || arg_len > CK_LC_TASK_MAX) return refuse(lc, "preference text out of bounds"); |
| 231 | for (i = 0; i < arg_len; i++) lc->fact[i] = arg[i]; |
| 232 | lc->fact[arg_len] = 0; |
| 233 | lc->fact_len = arg_len; /* like remember: never changes the state or the pending task */ |
| 234 | lc->prefers++; |
| 235 | return CK_LC_R_PREFER; |
| 236 | } |
| 237 | case CK_LC_V_FACTS: |
| 238 | if (lc->state == CK_LC_LOCKED) return refuse(lc, "locked: no production Store"); |
| 239 | lc->facts++; |
| 240 | return CK_LC_R_FACTS; |
| 241 | case CK_LC_V_APPROVE: |
| 242 | if (lc->state != CK_LC_SERVE) return refuse(lc, "nothing pending"); |
| 243 | return CK_LC_R_COMMIT; |
| 244 | case CK_LC_V_DENY: |
| 245 | if (lc->state != CK_LC_SERVE) return refuse(lc, "nothing pending"); |
| 246 | lc->denied++; |
| 247 | drop_task(lc); |
| 248 | lc->state = CK_LC_READY; |
| 249 | return CK_LC_R_OK; |
| 250 | case CK_LC_V_CANCEL: |
| 251 | if (lc->state != CK_LC_SERVE) return refuse(lc, "nothing to cancel"); |
| 252 | lc->cancelled++; |
| 253 | drop_task(lc); |
| 254 | lc->state = CK_LC_READY; |
| 255 | return CK_LC_R_OK; |
| 256 | case CK_LC_V_SHUTDOWN: |
| 257 | if (lc->state == CK_LC_SERVE) drop_task(lc); /* a pending, unapproved task is dropped, never committed */ |
| 258 | lc->state = CK_LC_QUIESCE; |
| 259 | return CK_LC_R_STOP; |
| 260 | } |
| 261 | return refuse(lc, "unknown verb"); |
| 262 | } |
| 263 | |
| 264 | void ck_lc_commit_done(ck_lc *lc, int rc) |
| 265 | { |
| 266 | if (lc->state != CK_LC_SERVE) return; |
| 267 | if (rc == 0) { |
| 268 | lc->approved++; |
| 269 | drop_task(lc); |
| 270 | lc->state = CK_LC_READY; |
| 271 | } else { |
| 272 | lc->refused++; |
| 273 | lc->reason = "commit failed: task still pending (approve again or cancel)"; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | void ck_lc_commit_replay(ck_lc *lc) |
| 278 | { |
| 279 | if (lc->state != CK_LC_SERVE) return; |
| 280 | lc->replays++; |
| 281 | lc->refused++; |
| 282 | lc->reason = "replay: this request is already committed (nothing written)"; |
| 283 | drop_task(lc); |
| 284 | lc->state = CK_LC_READY; |
| 285 | } |
| 286 | |
| 287 | void ck_lc_commit_refused(ck_lc *lc, const char *why) |
| 288 | { |
| 289 | if (lc->state != CK_LC_SERVE) return; |
| 290 | lc->bind_refused++; |
| 291 | lc->refused++; |
| 292 | lc->reason = why; |
| 293 | } |
| 294 | |
| 295 | void ck_lc_rotate_done(ck_lc *lc, int ok) |
| 296 | { |
| 297 | if (lc->state != CK_LC_READY) return; |
| 298 | if (!ok) { |
| 299 | lc->refused++; |
| 300 | lc->reason = "recovery secret rotation refused: Store unchanged"; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | void ck_lc_unlock_done(ck_lc *lc, int opened) |
| 305 | { |
| 306 | if (lc->state != CK_LC_LOCKED) return; |
| 307 | if (opened) lc->state = CK_LC_READY; |
| 308 | else { |
| 309 | lc->refused++; |
| 310 | lc->reason = "production key source refused: still locked"; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | void ck_lc_stopped(ck_lc *lc) |
| 315 | { |
| 316 | lc->state = CK_LC_STOP; |
| 317 | } |
| 318 | |
| 319 | void ck_lc_generate_done(ck_lc *lc, enum ck_lc_gen outcome, const char *reply, size_t n) |
| 320 | { |
| 321 | if (lc->state != CK_LC_READY || lc->task_len == 0) return; /* only a READY prompt generates */ |
| 322 | if (outcome == CK_LC_GEN_REPLY && n > 0 && n <= CK_LC_REPLY_MAX) { |
| 323 | size_t i; |
| 324 | for (i = 0; i < n; i++) lc->reply[i] = reply[i]; |
| 325 | lc->reply[n] = 0; |
| 326 | lc->reply_len = n; |
| 327 | lc->pending = CK_LC_PENDING_REPLY; |
| 328 | lc->replies++; |
| 329 | lc->state = CK_LC_SERVE; |
| 330 | return; |
| 331 | } |
| 332 | if (outcome == CK_LC_GEN_CANCELLED) lc->gen_cancelled++; |
| 333 | else lc->gen_failed++; |
| 334 | drop_task(lc); |
| 335 | } |