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 / runtime / rx_operator.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 | #ifndef RX_OPERATOR_H |
| 18 | #define RX_OPERATOR_H |
| 19 | /* R16 G6: the operator's outside entry point to a running production world |
| 20 | * (docs/r16-operator-control.md). An owner-only Unix-domain socket in a |
| 21 | * validated 0700 directory, a SO_PEERCRED check, and one line per request |
| 22 | * carrying the operator's runtime-issued caller credential and its |
| 23 | * RX_WORLD_RES_CONTROL capability. Every decision is the world's |
| 24 | * (rx_world_emergency_stop / _resume / rx_world_operator_authorize); this |
| 25 | * module only carries requests and replies. */ |
| 26 | #include "rx_world.h" |
| 27 | #include "aienos_cap.h" |
| 28 | |
| 29 | #include <stddef.h> |
| 30 | #include <stdint.h> |
| 31 | |
| 32 | #define RX_OPERATOR_SUBJ 70u /* runs no reaction; holds only the control capability */ |
| 33 | #define RX_OPERATOR_CRED "operator.cred" |
| 34 | #define RX_OPERATOR_SOCK "operator.sock" |
| 35 | #define RX_OPERATOR_LINE 512u |
| 36 | |
| 37 | typedef struct RxOperator RxOperator; |
| 38 | |
| 39 | typedef struct { |
| 40 | RxWorld *world; |
| 41 | AienosCapAdmin *admin; /* the authority: its office revokes the capability */ |
| 42 | const char *control_dir; /* validated with rx_operator_dir_check */ |
| 43 | const char *world_key; /* the mode this world runs, for the credential file */ |
| 44 | /* Program counters appended to a status reply (key=value ...). */ |
| 45 | void (*describe)(void *ctx, char *buf, size_t n); |
| 46 | /* Called after the world accepted a stop (1) or a resume (0). */ |
| 47 | void (*on_halt)(void *ctx, int halted); |
| 48 | /* Called after an authorized shutdown of a stopped world. */ |
| 49 | void (*on_shutdown)(void *ctx); |
| 50 | void *ctx; |
| 51 | } RxOperatorConfig; |
| 52 | |
| 53 | /* `dir` must be a real directory (opened O_DIRECTORY|O_NOFOLLOW, then |
| 54 | * fstat on that descriptor: no symlink, no path re-resolution), owned by the |
| 55 | * effective uid, with no group or other permission bits. With `create`, a |
| 56 | * missing directory is made 0700 first. 0 ok; -1 refused, with the reason in |
| 57 | * `why`. rx_operator_dir_open also hands back the held descriptor. */ |
| 58 | int rx_operator_dir_check(const char *dir, int create, char *why, size_t n); |
| 59 | int rx_operator_dir_open(const char *dir, int create, int *fd_out, char *why, size_t n); |
| 60 | |
| 61 | /* Exclusive lock on `dir` (validated as above): flock(LOCK_EX|LOCK_NB) on |
| 62 | * RX_OPERATOR_LOCK, opened with openat(O_CLOEXEC|O_NOFOLLOW) in the held |
| 63 | * directory. Held until the returned descriptor is closed (or the process |
| 64 | * ends). -1 refused (held by another program: its pid is in `why`). */ |
| 65 | #define RX_OPERATOR_LOCK "operator.lock" |
| 66 | int rx_operator_lock_dir(const char *dir, int create, int *lock_fd, char *why, size_t n); |
| 67 | |
| 68 | /* Lock the control directory (refused if another program holds it), write |
| 69 | * the credential file (0600, temporary file, fsync, rename, all relative to |
| 70 | * the held directory descriptor), wipe `cred`, bind the socket inside the |
| 71 | * held directory and start the listener. The request gate starts |
| 72 | * HELD by the caller: no request is served until rx_operator_release. */ |
| 73 | int rx_operator_open(RxOperator **out, const RxOperatorConfig *cfg, uint32_t subject, |
| 74 | RxCallerCred *cred, RxCapRef cap); |
| 75 | /* The request gate: while the program holds it (world setup), requests wait. */ |
| 76 | void rx_operator_hold(RxOperator *op); |
| 77 | void rx_operator_release(RxOperator *op); |
| 78 | /* Stop the listener and remove the socket and the credential file, each only |
| 79 | * if it is still the file this instance created (same device and inode), |
| 80 | * then release the control directory lock. The gate must not be held by the |
| 81 | * caller. */ |
| 82 | void rx_operator_close(RxOperator *op); |
| 83 | |
| 84 | #endif |