interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / src / osh / host / osh_main.c

205 lines · 9601 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_main.c -- the osh program (aien-architecture#158).
19 * osh read commands from standard input (prompts only when it is a terminal)
20 * osh -c 'string' [arg0 [args...]]
21 * osh script [args...]
22 * A leading `--caps POLICYFILE` enforces the launch policy (osh_caps.h): the host starts omega's capability root, mints
23 * the grants, seals the admin handle, and every spawn / redirection open / cd is checked at the moment of the effect.
24 * Without it the policy is a principal with no grants: every such effect is denied (fail closed). Builtins that make
25 * no effect (printf, pwd, export, unset, exit) still run.
26 * The effect journal (osh_journal.h, ABI section 10) is MANDATORY and always on: before each external spawn or write-class
27 * redirection an intent is fsynced, after it an outcome. Default file: $XDG_STATE_HOME/osh/effects.journal, else
28 * $HOME/.local/state/osh/effects.journal (directories 0700, file 0600); `--journal FILE` (before or after --caps) names another.
29 * At startup every intent left without an outcome by an earlier run is reported on stderr as UNKNOWN (never re-run) and
30 * closed. There is no opt-out. If the journal cannot be opened or recovered the shell says so on stderr and refuses every
31 * effect (status 1, nothing launched); builtins without a write redirection still run.
32 * OSH_INTERP=1 in the environment runs the shell core in the reference interpreter instead of native code.
33 * Never runs a command through another shell: programs are started by the host adapter with execve().
34 */
35#include <errno.h>
36#include <fcntl.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <sys/stat.h>
41#include <unistd.h>
42
43#include "osh_caps.h"
44#include "osh_journal.h"
45#include "osh_shell.h"
46
47extern char **environ;
48
49static const char *g_journal_file;
50static OshJournal g_journal = {.fd = -1};
51
52static char g_journal_err[4700];
53
54/* The effect journal is MANDATORY (ABI section 10 has no off switch). Open it (default location when no --journal was
55 * given), report what a previous run left unfinished, attach it. If it cannot be opened, recovered or written, the shell
56 * still starts but says so loudly and refuses every effect (spawn, write-class open) until it is run with a working one. */
57static void journal_unavailable(OshShell *sh, const char *what)
58{
59 snprintf(g_journal_err, sizeof g_journal_err, "%s", what);
60 sh->s.journal_error = g_journal_err;
61 osh_journal_close(&g_journal);
62 sh->s.journal = NULL;
63 fprintf(stderr, "osh: effect journal unavailable (%s): every command that starts a program or writes a file will be refused\n", what);
64}
65
66static int journal_attach(OshShell *sh)
67{
68 char def[4096], why[4300];
69 const char *path = g_journal_file;
70 sh->s.journal_required = 1;
71 if (!path) {
72 if (osh_journal_default_path(getenv("XDG_STATE_HOME"), getenv("HOME"), def, sizeof def) != 0) {
73 journal_unavailable(sh, "neither XDG_STATE_HOME nor HOME names an absolute directory; use --journal FILE");
74 return 0;
75 }
76 int mk = osh_journal_mkparents(def);
77 if (mk) { snprintf(why, sizeof why, "%s: %s", def, strerror(-mk)); journal_unavailable(sh, why); return 0; }
78 path = def;
79 }
80 /* default or explicit: the directory holding the journal must be ours and private */
81 {
82 char dir[4200];
83 snprintf(dir, sizeof dir, "%s", path);
84 char *sl = strrchr(dir, '/');
85 if (!sl) strcpy(dir, ".");
86 else if (sl == dir) dir[1] = 0;
87 else *sl = 0;
88 if (osh_journal_check_dir(&g_journal, dir)) { journal_unavailable(sh, g_journal.why); return 0; }
89 }
90 int rc = osh_journal_open(&g_journal, path);
91 if (rc) { journal_unavailable(sh, g_journal.why); return 0; }
92 OshJournalUnknown u[16];
93 int bad = 0, n = osh_journal_recover(&g_journal, path, u, 16, &bad);
94 if (n < 0) { snprintf(why, sizeof why, "%s: recovery failed: %s", path, strerror(-n)); journal_unavailable(sh, why); return 0; }
95 for (int i = 0; i < n && i < 16; i++)
96 fprintf(stderr, "osh: journal: effect %d/%d of request %.16s... (record %llu, argv0 %s) has outcome UNKNOWN; it is not re-run\n", u[i].idx + 1, u[i].ncmds, u[i].digest, (unsigned long long)u[i].intent_rec, u[i].argv0);
97 if (n > 16) fprintf(stderr, "osh: journal: and %d more UNKNOWN\n", n - 16);
98 if (bad) fprintf(stderr, "osh: journal: %d unreadable line(s) ignored\n", bad);
99 sh->s.journal = &g_journal;
100 return 0;
101}
102
103static char *slurp_fd(int fd, size_t *n)
104{
105 size_t cap = 4096, len = 0;
106 char *b = malloc(cap);
107 for (;;) {
108 if (len == cap) { cap *= 2; b = realloc(b, cap); }
109 if (!b) return NULL;
110 ssize_t r = read(fd, b + len, cap - len);
111 if (r < 0) { free(b); return NULL; }
112 if (r == 0) break;
113 len += (size_t)r;
114 }
115 *n = len;
116 return b;
117}
118
119/* With no --caps the policy is DEFAULT_POLICY: a principal and no grants, so every spawn, redirection open and cd is
120 * denied (fail closed). Authority comes only from a policy file named on the command line by whoever starts osh. */
121static const char DEFAULT_POLICY[] = "principal 1\n";
122
123/* Read the policy (or take the default), start the root, mint, bind the session, then seal: nothing in this process
124 * can mint or revoke after. */
125static int arm(OshShell *sh, OshCaps *caps, const char *file)
126{
127 char *z = NULL;
128 if (journal_attach(sh) != 0) return -1;
129 if (file) {
130 int fd = open(file, O_RDONLY | O_CLOEXEC);
131 if (fd < 0) { fprintf(stderr, "osh: --caps %s: cannot open\n", file); return -1; }
132 size_t n = 0;
133 char *text = slurp_fd(fd, &n);
134 close(fd);
135 if (!text || n > (1u << 16)) { fprintf(stderr, "osh: --caps %s: unreadable or too large\n", file); free(text); return -1; }
136 z = realloc(text, n + 1);
137 if (!z) { free(text); return -1; }
138 z[n] = 0;
139 if (strlen(z) != n) { fprintf(stderr, "osh: --caps %s: contains a NUL byte\n", file); free(z); return -1; }
140 } else {
141 z = strdup(DEFAULT_POLICY);
142 if (!z) return -1;
143 }
144 char err[256];
145 int rc = osh_caps_start(caps);
146 if (rc != 0) fprintf(stderr, "osh: capability root did not start (%s)\n", rx_cap_strerror(rc));
147 else if (osh_caps_load(caps, z, err, sizeof err) != 0) { fprintf(stderr, "osh: --caps %s: %s\n", file ? file : "(default)", err); rc = -1; }
148 free(z);
149 if (rc != 0) return -1;
150 osh_caps_attach(caps, &sh->s);
151 osh_caps_seal(caps);
152 return 0;
153}
154
155int main(int argc, char **argv)
156{
157 int native = 1;
158 const char *ei = getenv("OSH_INTERP");
159 if (ei && strcmp(ei, "1") == 0) native = 0;
160 const char *caps_file = NULL;
161 while (argc >= 3 && (strcmp(argv[1], "--caps") == 0 || strcmp(argv[1], "--journal") == 0)) {
162 if (argv[1][2] == 'c') caps_file = argv[2];
163 else g_journal_file = argv[2];
164 argv[2] = argv[0];
165 argv += 2;
166 argc -= 2;
167 }
168 OshShell sh;
169 OshCaps caps = {0};
170 int rc;
171 if (argc >= 2 && strcmp(argv[1], "-c") == 0) {
172 if (argc < 3) { fprintf(stderr, "osh: -c needs a command string\n"); return 2; }
173 const char *arg0 = argc > 3 ? argv[3] : "osh";
174 if (osh_shell_init(&sh, environ, native, 0, arg0, argc > 4 ? argv + 4 : NULL, argc > 4 ? argc - 4 : 0) != 0) return 70;
175 if (arm(&sh, &caps, caps_file) != 0) { osh_shell_free(&sh); osh_caps_stop(&caps); return 70; }
176 rc = osh_shell_run_string(&sh, argv[2], strlen(argv[2]));
177 } else if (argc >= 2) {
178 int fd = open(argv[1], O_RDONLY | O_CLOEXEC);
179 if (fd < 0) { /* bash: 127 only when the path does not exist, 126 for anything else, with the system's message */
180 int e = errno;
181 fprintf(stderr, "osh: %s: %s\n", argv[1], strerror(e));
182 return (e == ENOENT || e == ENOTDIR) ? 127 : 126;
183 }
184 struct stat st;
185 if (fstat(fd, &st) == 0 && S_ISDIR(st.st_mode)) { fprintf(stderr, "osh: %s: Is a directory\n", argv[1]); close(fd); return 126; }
186 size_t n = 0;
187 char *text = slurp_fd(fd, &n);
188 int e = errno;
189 close(fd);
190 if (!text) { fprintf(stderr, "osh: %s: %s\n", argv[1], strerror(e)); return 126; }
191 if (osh_shell_init(&sh, environ, native, 0, argv[1], argc > 2 ? argv + 2 : NULL, argc > 2 ? argc - 2 : 0) != 0) return 70;
192 if (arm(&sh, &caps, caps_file) != 0) { osh_shell_free(&sh); osh_caps_stop(&caps); return 70; }
193 rc = osh_shell_run_script(&sh, text, n);
194 free(text);
195 } else {
196 int tty = isatty(0) && isatty(2);
197 if (osh_shell_init(&sh, environ, native, tty, "osh", NULL, 0) != 0) return 70;
198 if (arm(&sh, &caps, caps_file) != 0) { osh_shell_free(&sh); osh_caps_stop(&caps); return 70; }
199 rc = osh_shell_run_fd(&sh, 0);
200 }
201 osh_shell_free(&sh);
202 osh_caps_stop(&caps);
203 osh_journal_close(&g_journal);
204 return rc & 255;
205}