Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/src/osh/host/osh_host.h # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / osh / host / osh_host.h` 225 lines ยท 12203 bytes Raw Blame History 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_host.h -- Linux host adapter for osh (aien-architecture#158): the execution service. 19 * 20 * Host code is replaceable scaffolding (ADR 0024). It never tokenizes, parses or expands: it consumes request 21 * records laid out as OSH_PLATFORM_ABI.md section 7 (v1 DRAFT, aien-protocols branch osh/platform-abi-v1-draft) 22 * and performs them. No shell is ever invoked (no sh -c, system(), popen()); every program is started with 23 * execve() on an argv this code built itself. 24 * 25 * Semantics decided here, where the ABI draft is silent, are listed at the top of osh_exec.c ("DECISIONS"). 26 */ 27 #ifndef OSH_HOST_H 28 #define OSH_HOST_H 29 #include 30 #include 31 #include 32 33 /* ---- ABI constants (section 3, 5.3, 6.1, 7.1, 8.6, 10) ---- */ 34 #define OSH_REQ_MAGIC 0x4F524551u /* "OREQ" */ 35 #define OSH_REQ_VERSION 1 36 #define OSH_REQ_HDR_CELLS 8 37 #define OSH_CMD_CELLS 164 38 #define OSH_MAX_CMDS 8 39 #define OSH_MAX_ARGV 32 40 #define OSH_MAX_ASSIGN 16 41 #define OSH_MAX_REDIR 8 42 #define OSH_OUT_CAP 8192 /* CAP_OUT */ 43 #define OSH_REQ_FLAG_DIRECT 1u 44 45 enum { /* refusal codes, section 5.3 (ABI group) */ 46 OSH_ABI_MAGIC = 214, OSH_ABI_VERSION = 215, OSH_ABI_LENGTH = 216, OSH_ABI_RESERVED = 217, OSH_REQ_FIELD = 218 47 }; 48 49 enum { /* platform error codes, section 8.6 (separate namespace from refusals) */ 50 OSH_E_OK = 0, OSH_E_DENIED = 1, OSH_E_REVOKED = 2, OSH_E_STALE = 3, OSH_E_NOT_FOUND = 4, OSH_E_UNAVAILABLE = 5, 51 OSH_E_INVALID_ARG = 6, OSH_E_LIMIT = 7, OSH_E_INTERRUPTED = 8, OSH_E_BROKEN_PIPE = 9, OSH_E_IO = 10, 52 OSH_E_OUTCOME_UNKNOWN = 11, OSH_E_CAP_DOMAIN_MISMATCH = 12, OSH_E_CAP_GEN_NARROW = 13, OSH_E_NOT_SUPPORTED = 14, 53 OSH_E_WOULD_BLOCK = 15, OSH_E_PARTIAL_LAUNCH = 16 54 }; 55 56 enum { /* section 10 outcomes, per command */ 57 OSH_OUT_NOT_STARTED = 0, OSH_OUT_COMPLETED, OSH_OUT_FAILED_NO_EFFECT, OSH_OUT_CANCELLED, OSH_OUT_UNKNOWN 58 }; 59 60 enum { OSH_B_NONE = 0, OSH_B_CD, OSH_B_PWD, OSH_B_PRINTF, OSH_B_EXPORT, OSH_B_UNSET, OSH_B_EXIT }; 61 enum { OSH_R_IN = 1, OSH_R_OUT = 2, OSH_R_APPEND = 3, OSH_R_DUP = 4 }; 62 enum { OSH_CONN_NONE = 0, OSH_CONN_SEMI = 1, OSH_CONN_AND = 2, OSH_CONN_OR = 3 }; 63 enum { OSH_CLASS_OMEGA_NATIVE_OP = 1, OSH_CLASS_AIENOS_ARTIFACT = 2, OSH_CLASS_AIENOS_SERVICE = 3, OSH_CLASS_LINUX = 4 }; 64 65 /* ---- decoded request (the one struct both the record path and the direct-argv path produce) ---- */ 66 typedef struct { 67 int kind; /* OSH_R_* */ 68 int fd; /* descriptor being redirected, 0..2 */ 69 int src_fd; /* OSH_R_DUP: source descriptor M, 0..2 */ 70 const char *path; /* kinds 1..3: NUL-terminated target (points into OshRequest.arena) */ 71 } OshRedir; 72 73 typedef struct { 74 const char *name, *value; /* NUL-terminated, point into the arena */ 75 } OshAssign; 76 77 typedef struct { 78 int builtin_id; 79 int nargv, nassign, nredir; 80 const char *argv[OSH_MAX_ARGV + 1]; /* NULL terminated */ 81 OshAssign assign[OSH_MAX_ASSIGN]; 82 OshRedir redir[OSH_MAX_REDIR]; 83 } OshCmd; 84 85 typedef struct { 86 int ncmds; 87 int connector_after; 88 unsigned flags; 89 OshCmd cmd[OSH_MAX_CMDS]; 90 /* Owned copy of the OUT bytes, each string NUL terminated by the decoder (so no pointer into the caller's 91 * workspace survives the call: borrow per call, ABI section 4.1). */ 92 char arena[OSH_OUT_CAP + OSH_MAX_CMDS * (OSH_MAX_ARGV + 2 * OSH_MAX_ASSIGN + OSH_MAX_REDIR) + 8]; 93 size_t arena_used; 94 } OshRequest; 95 96 /* Decode a request record. rec = cells of the record (header 8 + ncmds*164), out = the OUT area, one byte per 97 * cell. Check order: length, magic, version, reserved, then field validation (REQ_FIELD). 98 * Returns 0 and fills *r, or a refusal code (214..218); *r is unspecified after a refusal and nothing is partially 99 * applied. A pointer-free function of its inputs. */ 100 int osh_req_decode(const uint64_t *rec, size_t nrec, const uint64_t *out, size_t nout, OshRequest *r); 101 102 /* ---- record builder (hand-built records in tests, and the direct-argv path) ---- */ 103 typedef struct { 104 uint64_t rec[OSH_REQ_HDR_CELLS + OSH_MAX_CMDS * OSH_CMD_CELLS]; 105 uint64_t out[OSH_OUT_CAP]; 106 size_t out_used; 107 int ncmds, err; /* err: sticky OSH_REQ_FIELD / OSH_ABI_* */ 108 } OshBuilder; 109 110 void osh_rb_init(OshBuilder *b, unsigned flags, int connector_after); 111 void osh_rb_cmd(OshBuilder *b, int builtin_id); /* start the next command */ 112 void osh_rb_arg(OshBuilder *b, const char *s); /* append argv entry */ 113 void osh_rb_assign(OshBuilder *b, const char *name, const char *value); /* prefix assignment */ 114 void osh_rb_redir(OshBuilder *b, int kind, int fd, const char *path_or_null, int src_fd); 115 size_t osh_rb_cells(const OshBuilder *b); /* cells in rec (8 + ncmds*164) */ 116 void osh_rb_seal(OshBuilder *b); /* writes header cells (out_used, ncmds, version) */ 117 118 /* ---- session ---- */ 119 typedef struct { 120 uint8_t principal_id[32]; 121 uint8_t domain; /* 1 kernel IPC table, 2 hosted capability authority */ 122 uint32_t cap_index; 123 uint64_t cap_generation; 124 uint16_t resource_class, operation; 125 int valid; 126 } OshBinding; 127 128 typedef struct { 129 char *name, *value; /* value NULL: exported name that has no value yet */ 130 int exported; 131 } OshVar; 132 133 struct OshSession; 134 /* RESOLVE class hook: return OSH_CLASS_* for a command name. NULL means every name is OSH_CLASS_LINUX. */ 135 typedef int (*OshClassHook)(void *ctx, const char *name); 136 /* PERM_CHECK hook: return OSH_E_OK or a denial reason (OSH_E_*). NULL means any valid binding is allowed. */ 137 typedef int (*OshPermHook)(void *ctx, const OshBinding *b, int op); 138 enum { OSH_OP_SPAWN = 1, OSH_OP_CHDIR = 2, OSH_OP_OPEN_WRITE = 3, OSH_OP_OPEN_READ = 4 }; 139 /* EFFECT hook (ABI section 9.2): called with the canonical-able target path immediately before the syscall that would 140 * make the effect (execve/fork of a resolved program, open for a redirection, chdir). Return OSH_E_OK or a denial 141 * (OSH_E_DENIED / REVOKED / STALE / CAP_DOMAIN_MISMATCH ...). On denial the effect does not happen. NULL = not gated 142 * (embedder-supplied authorization only; the osh program installs one when started with --caps). */ 143 typedef int (*OshEffectHook)(void *ctx, const OshBinding *b, int op, const char *path); 144 145 typedef struct OshSession { 146 OshVar *vars; 147 size_t nvars, capvars; 148 char **raw_env; /* imported entries whose name is not an identifier ("A B=1", "1X=2", "BASH_FUNC_f%%=..."): 149 never shell variables, but handed verbatim to every child as bash does (#344 RISK-7) */ 150 size_t nraw; 151 int last_status; /* $? */ 152 int fd[3]; /* the shell's own stdin/stdout/stderr (default 0,1,2); never closed by osh */ 153 int interactive; /* nonzero: foreground pipelines take the terminal on tty_fd */ 154 int tty_fd; /* controlling terminal descriptor (interactive only) */ 155 uint64_t session_epoch; 156 OshBinding binding; 157 OshClassHook class_hook; 158 OshPermHook perm_hook; 159 void *hook_ctx; 160 OshEffectHook effect_hook; /* per-effect authority check, see OshEffectHook */ 161 void *effect_ctx; 162 int exit_requested, exit_status; /* set by the parent `exit` builtin */ 163 int abort_list; /* set by the parent `exit` with too many arguments: bash drops the rest of the list */ 164 struct OshJournal *journal; /* durable intent/outcome journal (osh_journal.h, ABI section 10); NULL = none */ 165 int journal_required; /* the effect journal is mandatory (ABI section 10): with journal NULL every effect is refused */ 166 const char *journal_error; /* why the mandatory journal is unavailable (named in the refusal) */ 167 /* test-only fault injection; all zero in production */ 168 int fail_fork_at; /* 1-based index of the command whose fork() is made to fail */ 169 void (*after_launch_hook)(void *); /* called after the last command is started, before waiting */ 170 void *after_launch_ctx; 171 } OshSession; 172 173 /* Fill the variable table from envp (NAME=value strings; entries without '=' or with an invalid name are ignored) 174 * and set PWD per POSIX (kept if it names the current directory, else getcwd). All imported variables are exported; entries whose name is not an identifier are kept in raw_env for children only. */ 175 int osh_session_init(OshSession *s, char *const *envp); 176 void osh_session_free(OshSession *s); 177 const char *osh_var_get(const OshSession *s, const char *name); /* NULL if unset */ 178 int osh_var_set(OshSession *s, const char *name, const char *value); /* keeps export flag; 0 ok, -1 invalid/oom */ 179 int osh_var_export(OshSession *s, const char *name, const char *value_or_null); 180 int osh_var_unexport(OshSession *s, const char *name); /* keeps the variable, drops the export mark */ 181 int osh_var_unset(OshSession *s, const char *name); 182 int osh_name_valid(const char *s); 183 char **osh_build_envp(const OshSession *s, const OshAssign *ov, int nov, const char *prog); /* prog: _=prog (NULL: none); malloc'd, free with osh_envp_free */ 184 void osh_envp_free(char **envp); 185 186 /* ---- results ---- */ 187 typedef struct { 188 pid_t pid; /* 0 when no process was started */ 189 int status; /* exit status of this command (127, 126, 128+sig, ...) */ 190 int outcome; /* OSH_OUT_* */ 191 int termsig; /* nonzero when killed by a signal */ 192 int err; /* OSH_E_* for this command */ 193 } OshCmdResult; 194 195 typedef struct { 196 int status; /* pipeline status = last command's status; also stored in session->last_status */ 197 int err; /* OSH_E_*: OK, DENIED, UNAVAILABLE, PARTIAL_LAUNCH, OUTCOME_UNKNOWN ... */ 198 int ncmds; 199 OshCmdResult cmd[OSH_MAX_CMDS]; 200 int sigint_seen; /* the shell itself received SIGINT while waiting (forwarded to the pipeline only when interactive; see osh_exec.c) */ 201 int killed_by_int; /* the last command died of SIGINT or SIGQUIT: list execution must stop */ 202 int exit_requested; /* mirrors session: parent `exit` ran */ 203 int refusal; /* decoder refusal code if the request was refused before any effect, else 0 */ 204 int journal_failed; /* an outcome record could not be made durable (the intent stays; recovery reports UNKNOWN) */ 205 } OshResult; 206 207 /* Execute one decoded request (one pipeline). Returns the pipeline status. */ 208 int osh_exec(OshSession *s, const OshRequest *r, OshResult *res); 209 /* Decode then execute. A refused record runs nothing: res->refusal is set, status is 2, return value is 2. */ 210 int osh_exec_record(OshSession *s, const uint64_t *rec, size_t nrec, const uint64_t *out, size_t nout, OshResult *res); 211 212 /* connector semantics: should the pipeline after `connector_after` run, given $? of what ran? */ 213 int osh_conn_should_run(int connector_after, int last_status); 214 /* Run a list of already decoded pipelines with && || ; and $? tracking. Returns final $?. nres may be NULL. */ 215 int osh_run_list(OshSession *s, const OshRequest *const *reqs, int n, OshResult *res_out); 216 217 /* AIEN direct-argv submission (ABI section 11): builds the SAME record, decodes it through the SAME decoder and 218 * runs the SAME osh_exec. No shell string exists. redirs are OshRedir with path or src_fd filled. */ 219 int osh_submit_argv(OshSession *s, const char *const *argv, int nargv, const OshAssign *ov, int nov, 220 const OshRedir *redirs, int nredir, OshResult *res); 221 222 /* path resolution exposed for tests: 0 found (out filled), 126, or 127. */ 223 int osh_resolve_linux(const OshSession *s, const char *name, char *out, size_t outsz); 224 225 #endif /* OSH_HOST_H */