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 / runtime / rx_argus.c

991 lines · 40537 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 * rx_argus.c -- ARGUS producer (per-thread rings + use tables) and optional
19 * consumer (ordered merge -> argus_core) for the reaction runtime, ABI v1.1.
20 * See rx_argus.h for the rules. Compiled only when RX_ARGUS >= 1.
21 */
22#include "rx_argus.h"
23
24#if RX_ARGUS
25
26#include "sha256.h"
27#include "aien_machine_id.h"
28
29#include <pthread.h>
30#include <sched.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <time.h>
35#include <unistd.h>
36
37#if RX_ARGUS >= 2
38#define RX_ARGUS_HAS_CONSUMER 1
39#else
40#define RX_ARGUS_HAS_CONSUMER 0
41#endif
42
43#define BATCH 64u
44#define RING_MEM_MAX ((size_t)RX_ARGUS_RING_CAPACITY * ARGUS_EVENT_SIZE + 4096u)
45#define AUTH_MAX 8u
46
47/* Relaxed single-writer counter bump (the owner thread is the only writer). */
48#define BUMP(x, n) atomic_store_explicit(&(x), atomic_load_explicit(&(x), memory_order_relaxed) + (n), memory_order_relaxed)
49#define LOAD(x) atomic_load_explicit(&(x), memory_order_relaxed)
50
51typedef struct {
52 RxArgusProducer p; /* first: rx_argus_tls points here */
53 /* owner side */
54 uint64_t seq; /* next sequence of this stream */
55 uint64_t leftover_key; /* stamp of refused, still-counted uses (0 = none) */
56 uint16_t stream; /* 1..RX_ARGUS_PRODUCERS */
57 _Atomic uint64_t emitted, pushed, refused, malformed;
58 _Atomic uint64_t emitted_by_kind[ARGUS_EV_KIND_MAX + 1];
59 _Atomic uint64_t uses_flushed, summaries, summary_refused, order_slow, mixed_keys;
60 uint64_t keys[RX_ARGUS_KEYS]; /* order key by sequence & (KEYS-1) */
61 /* shared */
62 _Atomic int owned, inited;
63 ArgusRing *ring;
64 _Alignas(64) uint8_t ring_mem[RING_MEM_MAX];
65 /* consumer side */
66 ArgusEvent buf[BATCH];
67 uint32_t n, pos;
68 uint64_t stall_pub; /* pub value the consumer stopped waiting for */
69} Slot;
70
71_Thread_local RxArgusProducer *rx_argus_tls;
72_Atomic uint64_t rx_argus_stamp = 1;
73
74static struct {
75 Slot slots[RX_ARGUS_PRODUCERS];
76 ArgusCore *core;
77 ArgusFinding findings[RX_ARGUS_FINDINGS_MAX];
78 _Alignas(64) uint8_t core_mem[1u << 20];
79 int active, mode;
80 pid_t pid;
81 uint8_t machine_id[ARGUS_MACHINE_ID_LEN];
82 pthread_key_t exit_key;
83 _Atomic uint64_t unregistered_uses, lost_transitions;
84 _Atomic uint32_t claimed, live, live_max;
85 /* authority observer bookkeeping */
86 pthread_mutex_t obs_mu;
87 const AienosCapView *auth_view[AUTH_MAX];
88 uint64_t auth_live[AUTH_MAX][RX_ARGUS_USE_SLOTS]; /* live generation, 0 = none */
89 uint32_t auth_subject[AUTH_MAX][RX_ARGUS_USE_SLOTS];
90 uint32_t n_auth;
91 _Atomic uint64_t obs_calls[9];
92 /* consumer */
93 pthread_t thread;
94 int thread_started;
95 char consumer_cpu[64]; /* consumer placement as applied (see consumer_place) */
96 int consumer_pin_rc; /* pthread_setaffinity_np result (0 = ok) */
97 _Atomic int stop;
98 uint64_t consumer_seq, last_key;
99 uint64_t received, synthesized, batches, stream_records, ingest_errors, late, stall_breaks;
100 uint64_t hold_ns_max, hold_start;
101 uint64_t received_by_kind[ARGUS_EV_KIND_MAX + 1];
102 uint64_t findings_total, findings_by_code[ARGUS_F_MAX + 1];
103 uint32_t lag_max;
104 FILE *stream;
105 size_t ring_bytes, core_bytes;
106} g;
107
108static inline Slot *slot_of(RxArgusProducer *t) { return (Slot *)(void *)t; }
109
110static inline void cpu_relax(void) {
111#if defined(__aarch64__)
112 __asm__ __volatile__("yield" ::: "memory");
113#elif defined(__x86_64__)
114 __asm__ __volatile__("pause" ::: "memory");
115#endif
116}
117
118static __attribute__((unused)) uint64_t now_ns(void) {
119 struct timespec ts;
120 clock_gettime(CLOCK_MONOTONIC, &ts);
121 return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;
122}
123
124/* ---- producer slots --------------------------------------------------------- */
125
126static void reset_use(RxArgusUse *u) {
127 u->count = 0;
128 u->min_gen = UINT64_MAX;
129 u->max_gen = 0;
130}
131
132static void thread_exit(void *arg);
133
134static RxArgusProducer *claim(void) {
135 for (uint32_t i = 0; i < RX_ARGUS_PRODUCERS; i++) {
136 Slot *s = &g.slots[i];
137 int expect = 0;
138 if (atomic_load_explicit(&s->owned, memory_order_relaxed) ||
139 !atomic_compare_exchange_strong(&s->owned, &expect, 1))
140 continue;
141 if (!atomic_load_explicit(&s->inited, memory_order_acquire)) {
142 if (argus_ring_init(&s->ring, s->ring_mem, sizeof s->ring_mem, RX_ARGUS_RING_CAPACITY) != ARGUS_OK) {
143 atomic_store(&s->owned, 0);
144 return NULL;
145 }
146 s->stream = (uint16_t)(i + 1);
147 s->seq = 1;
148 for (uint32_t k = 0; k < RX_ARGUS_USE_SLOTS; k++) reset_use(&s->p.use[k]);
149 s->p.flush_at = ARGUS_USE_FLUSH_OPS;
150 atomic_store_explicit(&s->p.pub, RX_ARGUS_IDLE, memory_order_relaxed);
151 atomic_fetch_add(&g.claimed, 1);
152 atomic_store_explicit(&s->inited, 1, memory_order_release);
153 }
154 s->p.epoch = 0;
155 rx_argus_tls = &s->p;
156 pthread_setspecific(g.exit_key, s);
157 uint32_t live = atomic_fetch_add(&g.live, 1) + 1;
158 uint32_t mx = atomic_load(&g.live_max);
159 while (live > mx && !atomic_compare_exchange_weak(&g.live_max, &mx, live)) {}
160 return &s->p;
161 }
162 return NULL;
163}
164
165static inline void fill(ArgusEvent *ev, uint16_t kind, int code, uint8_t outcome) {
166 memset(ev, 0, sizeof *ev);
167 ev->version = ARGUS_ABI_VERSION;
168 ev->kind = kind;
169 ev->class_ = argus_event_min_class(kind);
170 if (!ev->class_) ev->class_ = ARGUS_CLASS_CRITICAL;
171 ev->effect_class = ARGUS_EFFECT_NONE;
172 ev->outcome = outcome;
173 ev->code = code;
174 ev->cap_id = ARGUS_CAP_NONE; /* callers set a real cap (0 = office) */
175 memcpy(ev->machine_id, g.machine_id, ARGUS_MACHINE_ID_LEN);
176}
177
178/* Owner-only: push one event on this slot's ring with its order key. The
179 * sequence is consumed only on success, so keys[seq & (KEYS-1)] never
180 * overwrites an entry still in the ring (the ring holds <= KEYS/2). */
181static int push(Slot *s, ArgusEvent *ev, uint64_t key) {
182 ev->sequence = s->seq;
183 ev->flags = (uint16_t)((ev->flags & ~ARGUS_FLAG_STREAM_MASK) |
184 ((uint32_t)s->stream << ARGUS_FLAG_STREAM_SHIFT));
185 s->keys[s->seq & (RX_ARGUS_KEYS - 1)] = key;
186 int rc = argus_ring_push(s->ring, ev);
187 BUMP(s->emitted, 1);
188 if (ev->kind <= ARGUS_EV_KIND_MAX) BUMP(s->emitted_by_kind[ev->kind], 1);
189 if (rc == ARGUS_OK) {
190 s->seq++;
191 BUMP(s->pushed, 1);
192 } else if (rc == ARGUS_ERR_FULL) {
193 BUMP(s->refused, 1);
194 } else {
195 BUMP(s->malformed, 1);
196 }
197 return rc;
198}
199
200/* Owner-only: one summary per touched slot. Refused summaries keep their counts. */
201static void flush_all(Slot *s) {
202 RxArgusProducer *t = &s->p;
203 uint64_t stamp = s->leftover_key ? s->leftover_key : t->epoch;
204 int left = 0;
205 for (uint32_t c = 0; c < RX_ARGUS_USE_SLOTS; c++) {
206 RxArgusUse *u = &t->use[c];
207 if (!u->count) continue;
208 ArgusEvent ev;
209 fill(&ev, ARGUS_EV_CAPABILITY_USE_SUMMARY, 0, ARGUS_OUTCOME_OK);
210 ev.principal = u->principal;
211 ev.cap_id = c;
212 ev.resource = u->count;
213 ev.cap_generation = u->max_gen; /* MAX generation seen */
214 ev.world_generation = u->min_gen; /* MIN generation seen (v1.1, 398cfb9); object_id 0 */
215 BUMP(s->summaries, 1);
216 if (push(s, &ev, 2 * stamp) == ARGUS_OK) {
217 BUMP(s->uses_flushed, u->count);
218 reset_use(u);
219 } else {
220 BUMP(s->summary_refused, 1);
221 left = 1;
222 }
223 }
224 s->leftover_key = left ? stamp : 0;
225 t->flush_at = t->ops + ARGUS_USE_FLUSH_OPS;
226 atomic_store_explicit(&t->flush_req, 0, memory_order_relaxed);
227}
228
229void rx_argus_flush_self(void) {
230 RxArgusProducer *t = rx_argus_tls;
231 if (t) flush_all(slot_of(t));
232}
233
234/* Publish "idle": nothing pending, nothing in flight. */
235static void go_idle(Slot *s) {
236 s->p.epoch = 0;
237 atomic_store_explicit(&s->p.pub, RX_ARGUS_IDLE, memory_order_release);
238}
239
240uint64_t rx_argus_use_begin_slow(uint64_t e) {
241 if (!g.active) return e;
242 RxArgusProducer *t = rx_argus_tls;
243 if (!t && !(t = claim())) return e;
244 Slot *s = slot_of(t);
245 flush_all(s); /* pending uses keep their old key */
246 if (s->leftover_key) BUMP(s->mixed_keys, 1);
247 /* Dekker with the consumer: publish, fence, re-read. The key returned is
248 * read before the caller's validate, and pub <= key. */
249 atomic_store_explicit(&t->pub, e, memory_order_seq_cst);
250 atomic_thread_fence(memory_order_seq_cst);
251 uint64_t e2 = atomic_load_explicit(&rx_argus_stamp, memory_order_seq_cst);
252 t->epoch = e2;
253 return e2;
254}
255
256/* A transition: flush, take a stamp (unless reserved), push with key 2s+1, go idle. */
257static void transition(ArgusEvent *ev, const AienosCapView *view) {
258 if (!g.active) return;
259 RxArgusProducer *t = rx_argus_tls;
260 if (!t && !(t = claim())) {
261 atomic_fetch_add(&g.lost_transitions, 1);
262 return;
263 }
264 Slot *s = slot_of(t);
265 flush_all(s);
266 uint64_t st = atomic_fetch_add_explicit(&rx_argus_stamp, 1, memory_order_seq_cst);
267 ev->tick = view ? aienos_cap_clock(view) : 0;
268 atomic_store_explicit(&t->pub, st, memory_order_seq_cst);
269 push(s, ev, 2 * st + 1);
270 if (!s->leftover_key) go_idle(s);
271}
272
273void rx_argus_use_slow(uint64_t key, const AienosCapView *view, uint32_t subject, uint32_t cap_id,
274 uint64_t gen, uint64_t resource, int code) {
275 if (!g.active) return;
276 RxArgusProducer *t = rx_argus_tls;
277 if (!t) {
278 atomic_fetch_add(&g.unregistered_uses, 1);
279 return;
280 }
281 Slot *s = slot_of(t);
282 if (code == 0 && cap_id < RX_ARGUS_USE_SLOTS && key == t->epoch) {
283 /* principal changed on this cap: close the old summary first */
284 RxArgusUse *u = &t->use[cap_id];
285 flush_all(s);
286 if (u->count == 0) {
287 u->principal = subject;
288 u->count = 1;
289 u->min_gen = u->max_gen = gen;
290 t->ops++;
291 return;
292 }
293 } else if (code == 0 && key != t->epoch) {
294 BUMP(s->order_slow, 1);
295 }
296 /* failed validate, cap outside the table, or no table room: one full USED
297 * event. It is keyed like the use it is (2*key, tick 0, no stamp), so a
298 * denial never forces other threads to flush; the price is that a USED/DENIED
299 * caused by a revoke may be ordered just before that REVOKED (no detector
300 * reads outcome-DENIED uses). */
301 ArgusEvent ev;
302 fill(&ev, ARGUS_EV_CAPABILITY_USED, code, code == 0 ? ARGUS_OUTCOME_OK : ARGUS_OUTCOME_DENIED);
303 ev.principal = subject;
304 ev.cap_id = cap_id;
305 ev.cap_generation = gen;
306 ev.resource = resource;
307 if (key == t->epoch && !s->leftover_key) {
308 (void)view;
309 push(s, &ev, 2 * key);
310 return;
311 }
312 transition(&ev, view);
313}
314
315int rx_argus_idle_pending(void) {
316 RxArgusProducer *t = rx_argus_tls;
317 return t && g.active && (t->epoch != 0 || slot_of(t)->leftover_key != 0);
318}
319
320void rx_argus_idle(void) {
321 RxArgusProducer *t = rx_argus_tls;
322 if (!t || !g.active) return;
323 Slot *s = slot_of(t);
324 if (t->epoch == 0 && !s->leftover_key) return;
325 flush_all(s);
326 if (!s->leftover_key) go_idle(s);
327}
328
329static void thread_exit(void *arg) {
330 Slot *s = arg;
331 if (!s) return;
332 if (g.active && g.pid == getpid()) {
333 flush_all(s);
334 go_idle(s);
335 }
336 rx_argus_tls = NULL;
337 atomic_fetch_sub(&g.live, 1);
338 atomic_store_explicit(&s->owned, 0, memory_order_release);
339}
340
341/* ---- transitions ---------------------------------------------------------------- */
342
343void rx_argus_emit_cap_granted(const AienosCapView *view, uint32_t subject, uint32_t cap_id,
344 uint64_t cap_generation, uint64_t resource, uint32_t rights,
345 int code) {
346 ArgusEvent ev;
347 fill(&ev, ARGUS_EV_CAPABILITY_GRANTED, code, code == 0 ? ARGUS_OUTCOME_OK : ARGUS_OUTCOME_ERROR);
348 ev.principal = subject;
349 ev.cap_id = cap_id;
350 ev.cap_generation = cap_generation;
351 ev.object_id = rights;
352 ev.resource = resource;
353 transition(&ev, view);
354}
355
356void rx_argus_emit_cap_denied(const AienosCapView *view, uint32_t subject, uint32_t cap_id,
357 uint64_t cap_generation, uint64_t resource, uint32_t rights,
358 int code) {
359 ArgusEvent ev;
360 fill(&ev, ARGUS_EV_CAPABILITY_DENIED, code, ARGUS_OUTCOME_DENIED);
361 ev.principal = subject;
362 ev.cap_id = cap_id;
363 ev.cap_generation = cap_generation;
364 ev.object_id = rights;
365 ev.resource = resource;
366 transition(&ev, view);
367}
368
369void rx_argus_emit_cap_revoked(const AienosCapView *view, uint32_t subject, uint32_t cap_id,
370 uint64_t cap_generation, int code) {
371 ArgusEvent ev;
372 fill(&ev, ARGUS_EV_CAPABILITY_REVOKED, code, code == 0 ? ARGUS_OUTCOME_OK : ARGUS_OUTCOME_ERROR);
373 ev.principal = subject;
374 ev.cap_id = cap_id;
375 ev.cap_generation = cap_generation;
376 transition(&ev, view);
377}
378
379void rx_argus_emit_world_committed(uint32_t store_id, uint32_t subject, uint32_t cap_id,
380 uint64_t cap_generation, uint64_t world_generation,
381 const uint8_t digest[32], int code) {
382 ArgusEvent ev;
383 fill(&ev, ARGUS_EV_WORLD_COMMITTED, code, code == 0 ? ARGUS_OUTCOME_OK : ARGUS_OUTCOME_ERROR);
384 ev.principal = subject;
385 ev.cap_id = cap_id;
386 ev.cap_generation = cap_generation;
387 ev.world_generation = world_generation;
388 ev.object_id = store_id;
389 if (digest) memcpy(ev.evidence_digest, digest, ARGUS_DIGEST_LEN);
390 transition(&ev, NULL);
391}
392
393uint32_t rx_argus_store_id(const char *dir) {
394 uint8_t d[32];
395 sha256_ctx h;
396 sha256_init(&h);
397 sha256_update(&h, (const uint8_t *)"ARGUS-STORE-v1", 14);
398 if (dir) sha256_update(&h, (const uint8_t *)dir, strlen(dir));
399 sha256_final(&h, d);
400 uint32_t id = (uint32_t)d[0] | (uint32_t)d[1] << 8 | (uint32_t)d[2] << 16 | (uint32_t)d[3] << 24;
401 return id ? id : 1u;
402}
403
404/* ---- consumer ------------------------------------------------------------------------ */
405
406#if RX_ARGUS_HAS_CONSUMER
407static void consume_one(const ArgusEvent *ev, uint64_t key) {
408 g.received++;
409 if (key < g.last_key) g.late++;
410 else g.last_key = key;
411 if (ev->kind <= ARGUS_EV_KIND_MAX) g.received_by_kind[ev->kind]++;
412 if (g.stream) {
413 uint8_t rec[ARGUS_EVENT_SIZE];
414 argus_event_encode(ev, rec);
415 if (fwrite(rec, sizeof rec, 1, g.stream) == 1) g.stream_records++;
416 }
417 if (g.mode != RX_ARGUS_CONSUMER_INGEST) return;
418 ArgusFinding f[32];
419 size_t n = 0;
420 int rc = argus_core_ingest(g.core, ev, f, 32, &n);
421 if (rc != ARGUS_OK) g.ingest_errors++;
422 for (size_t i = 0; i < n; i++) {
423 if (g.findings_total < RX_ARGUS_FINDINGS_MAX) g.findings[g.findings_total] = f[i];
424 g.findings_total++;
425 if (f[i].code <= ARGUS_F_MAX) g.findings_by_code[f[i].code]++;
426 }
427}
428
429static inline int inited(uint32_t i) {
430 return atomic_load_explicit(&g.slots[i].inited, memory_order_acquire);
431}
432
433static void refill(Slot *s) {
434 if (s->pos < s->n) return;
435 ArgusRingStats st;
436 argus_ring_stats(s->ring, &st);
437 if (st.depth > g.lag_max) g.lag_max = st.depth;
438 s->n = (uint32_t)argus_ring_pop_batch(s->ring, s->buf, BATCH);
439 s->pos = 0;
440 if (s->n) g.batches++;
441}
442
443static inline uint64_t head_key(const Slot *s) {
444 return s->keys[s->buf[s->pos].sequence & (RX_ARGUS_KEYS - 1)];
445}
446
447/* One merge pass. final = every producer is quiescent (shutdown): no bounds. */
448static size_t merge(int final) {
449 size_t done = 0;
450 for (uint32_t i = 0; i < RX_ARGUS_PRODUCERS; i++)
451 if (inited(i)) refill(&g.slots[i]);
452 for (;;) {
453 uint32_t best = UINT32_MAX;
454 uint64_t best_key = UINT64_MAX;
455 for (uint32_t i = 0; i < RX_ARGUS_PRODUCERS; i++) {
456 if (!inited(i)) continue;
457 Slot *s = &g.slots[i];
458 if (s->pos < s->n && head_key(s) < best_key) { best_key = head_key(s); best = i; }
459 }
460 if (best == UINT32_MAX) break;
461 /* lower bound of every producer without a buffered head: read pub, then re-poll */
462 uint64_t bound = UINT64_MAX;
463 uint32_t blocker = UINT32_MAX;
464 int refilled = 0;
465 for (uint32_t i = 0; i < RX_ARGUS_PRODUCERS && !final; i++) {
466 if (!inited(i)) continue;
467 Slot *s = &g.slots[i];
468 if (s->pos < s->n) continue;
469 uint64_t pub = atomic_load_explicit(&s->p.pub, memory_order_seq_cst);
470 refill(s);
471 if (s->pos < s->n) { refilled = 1; break; }
472 if (pub == RX_ARGUS_IDLE || pub == s->stall_pub) continue;
473 if (2 * pub < bound) { bound = 2 * pub; blocker = i; }
474 }
475 if (refilled) continue;
476 if (best_key <= bound) {
477 Slot *s = &g.slots[best];
478 consume_one(&s->buf[s->pos], best_key);
479 s->pos++;
480 done++;
481 if (g.hold_start) {
482 uint64_t h = now_ns() - g.hold_start;
483 if (h > g.hold_ns_max) g.hold_ns_max = h;
484 g.hold_start = 0;
485 }
486 if (s->pos == s->n) refill(s);
487 continue;
488 }
489 /* blocked by a producer that may still push a smaller key */
490 uint64_t now = now_ns();
491 if (!g.hold_start) g.hold_start = now;
492 if (now - g.hold_start >= RX_ARGUS_STALL_NS) {
493 g.slots[blocker].stall_pub = atomic_load(&g.slots[blocker].p.pub);
494 g.stall_breaks++;
495 continue;
496 }
497 break;
498 }
499 /* ring refusals -> TELEMETRY_DROPPED on the consumer stream */
500 for (uint32_t i = 0; i < RX_ARGUS_PRODUCERS; i++) {
501 if (!inited(i)) continue;
502 ArgusEvent drops[ARGUS_CLASS_MAX + 1];
503 size_t d = argus_ring_drain_drops(g.slots[i].ring, drops, ARGUS_CLASS_MAX + 1, &g.consumer_seq);
504 g.synthesized += d;
505 for (size_t k = 0; k < d; k++) consume_one(&drops[k], g.last_key);
506 done += d;
507 }
508 return done;
509}
510
511static void *consumer_main(void *arg) {
512 (void)arg;
513 unsigned idle = 0;
514 uint64_t next_flush = now_ns() + RX_ARGUS_FLUSH_NS;
515 while (!atomic_load_explicit(&g.stop, memory_order_acquire)) {
516 size_t n = merge(0);
517 uint64_t now = (idle & 15) == 0 ? now_ns() : 0;
518 if (now >= next_flush) {
519 for (uint32_t i = 0; i < RX_ARGUS_PRODUCERS; i++)
520 if (inited(i) && atomic_load_explicit(&g.slots[i].p.pub, memory_order_relaxed) != RX_ARGUS_IDLE)
521 atomic_store_explicit(&g.slots[i].p.flush_req, 1, memory_order_relaxed);
522 next_flush = now + RX_ARGUS_FLUSH_NS;
523 }
524 if (n) { idle = 0; continue; }
525 if (++idle < 64) { cpu_relax(); continue; }
526 /* idle back-off: 20 us doubling to 160 us (summaries are already
527 * flush-bounded at RX_ARGUS_FLUSH_NS; a busy poll only burns a core) */
528 unsigned shift = idle - 64 < 3 ? idle - 64 : 3;
529 long ns = 20000L << shift;
530 struct timespec ts = { 0, ns };
531 nanosleep(&ts, NULL);
532 }
533 while (merge(1)) {}
534 return NULL;
535}
536#endif
537
538/* ---- lifecycle ------------------------------------------------------------------------ */
539
540#if RX_ARGUS_HAS_CONSUMER
541/* Consumer placement (speed2 fix a). R8 measured the consumer's cost as core sharing:
542 * unpinned it rides the workload's cores (+5% wall), pinned off them +3%.
543 * RX_ARGUS_CONSUMER_CPU unset/"auto" (default): if the process affinity mask at start
544 * leaves some online CPUs out, pin the consumer to those CPUs (a core the workload
545 * does not use); if the process may use every CPU, leave the consumer unpinned.
546 * RX_ARGUS_CONSUMER_CPU="none": never pin.
547 * RX_ARGUS_CONSUMER_CPU="N" or "N-M,K": pin to exactly those CPUs (may lie outside the
548 * process mask; that is the point).
549 * Deployment requirement: give the consumer a core the workload does not use (a taskset
550 * workload gets that automatically). A bad list or a failed pin leaves the consumer
551 * unpinned and is reported (stderr + summary consumer_pin_rc), never fatal. */
552static int consumer_auto_set(cpu_set_t *out) {
553 cpu_set_t mask;
554 CPU_ZERO(out);
555 if (sched_getaffinity(0, sizeof mask, &mask) != 0) return 0;
556 long n = sysconf(_SC_NPROCESSORS_ONLN);
557 int any = 0;
558 for (long c = 0; c < n && c < CPU_SETSIZE; c++)
559 if (!CPU_ISSET((int)c, &mask)) { CPU_SET((int)c, out); any = 1; }
560 return any;
561}
562
563static int consumer_pin(pthread_t th, const char *spec) {
564 cpu_set_t set;
565 CPU_ZERO(&set);
566 const char *p = spec;
567 int any = 0;
568 while (*p) {
569 char *e;
570 long a = strtol(p, &e, 10), b;
571 if (e == p || a < 0 || a >= CPU_SETSIZE) return -1;
572 b = a;
573 if (*e == '-') {
574 p = e + 1;
575 b = strtol(p, &e, 10);
576 if (e == p || b < a || b >= CPU_SETSIZE) return -1;
577 }
578 for (long c = a; c <= b; c++) CPU_SET((int)c, &set);
579 any = 1;
580 if (*e == ',') e++;
581 else if (*e) return -1;
582 p = e;
583 }
584 if (!any) return -1;
585 return pthread_setaffinity_np(th, sizeof set, &set);
586}
587
588static void consumer_place(pthread_t th) {
589 const char *cpu = getenv("RX_ARGUS_CONSUMER_CPU");
590 if (cpu && strcmp(cpu, "none") == 0) {
591 snprintf(g.consumer_cpu, sizeof g.consumer_cpu, "none");
592 return;
593 }
594 if (!cpu || !*cpu || strcmp(cpu, "auto") == 0) {
595 cpu_set_t set;
596 if (!consumer_auto_set(&set)) {
597 snprintf(g.consumer_cpu, sizeof g.consumer_cpu, "auto:unpinned");
598 return;
599 }
600 size_t o = (size_t)snprintf(g.consumer_cpu, sizeof g.consumer_cpu, "auto:");
601 for (int c = 0; c < CPU_SETSIZE && o < sizeof g.consumer_cpu - 8; c++) {
602 if (!CPU_ISSET(c, &set)) continue;
603 int e = c;
604 while (e + 1 < CPU_SETSIZE && CPU_ISSET(e + 1, &set)) e++;
605 o += (size_t)snprintf(g.consumer_cpu + o, sizeof g.consumer_cpu - o,
606 e > c ? "%s%d-%d" : "%s%d", o > 5 ? "," : "", c, e);
607 c = e;
608 }
609 g.consumer_pin_rc = pthread_setaffinity_np(th, sizeof set, &set);
610 } else {
611 snprintf(g.consumer_cpu, sizeof g.consumer_cpu, "%s", cpu);
612 g.consumer_pin_rc = consumer_pin(th, cpu);
613 }
614 if (g.consumer_pin_rc != 0)
615 fprintf(stderr, "rx_argus: consumer placement %s not applied (%d); consumer unpinned\n",
616 g.consumer_cpu, g.consumer_pin_rc);
617}
618#endif
619
620_Static_assert(ARGUS_MACHINE_ID_LEN == AIEN_MID_ID_BYTES, "ARGUS machine slot is the canonical id");
621
622/* machine_id: the canonical AienMachineId slot bytes, or NULL for the provisional
623 * per-run id (removal path: drop the fallback once AIENOS provisioning writes the
624 * local identity record for every runtime host). */
625static int rx_argus_start(const char *run_id, const uint8_t *machine_id, int consumer_mode,
626 const char *stream_path) {
627 if (g.active) return ARGUS_ERR_STATE;
628 if (argus_ring_footprint(RX_ARGUS_RING_CAPACITY) > RING_MEM_MAX) return ARGUS_ERR_ARG;
629 g.ring_bytes = argus_ring_footprint(RX_ARGUS_RING_CAPACITY);
630 pthread_mutex_init(&g.obs_mu, NULL);
631 if (pthread_key_create(&g.exit_key, thread_exit) != 0) return ARGUS_ERR_STATE;
632#if RX_ARGUS_HAS_CONSUMER
633 g.mode = consumer_mode;
634 if (g.mode == RX_ARGUS_CONSUMER_INGEST) {
635 g.core_bytes = argus_core_footprint();
636 if (g.core_bytes > sizeof g.core_mem ||
637 argus_core_init(&g.core, g.core_mem, sizeof g.core_mem) != ARGUS_OK)
638 return ARGUS_ERR_ARG;
639 }
640 if (stream_path && g.mode != RX_ARGUS_CONSUMER_OFF) {
641 g.stream = fopen(stream_path, "wb");
642 if (!g.stream) return ARGUS_ERR_ARG;
643 }
644#else
645 (void)consumer_mode;
646 (void)stream_path;
647 g.mode = RX_ARGUS_CONSUMER_OFF;
648#endif
649 if (machine_id) {
650 memcpy(g.machine_id, machine_id, ARGUS_MACHINE_ID_LEN);
651 } else { /* Provisional machine id: SHA-256("ARGUS-PROVISIONAL-MACHINE-v1" || run_id). */
652 static const char tag[] = "ARGUS-PROVISIONAL-MACHINE-v1";
653 sha256_ctx h;
654 sha256_init(&h);
655 sha256_update(&h, (const uint8_t *)tag, sizeof tag - 1);
656 if (run_id) sha256_update(&h, (const uint8_t *)run_id, strlen(run_id));
657 sha256_final(&h, g.machine_id);
658 }
659 g.consumer_seq = 1;
660 g.pid = getpid();
661 g.active = 1;
662
663 ArgusEvent ev;
664 fill(&ev, ARGUS_EV_MACHINE_JOINED, 0, ARGUS_OUTCOME_OK);
665 ev.object_id = ARGUS_TRUST_OBSERVED;
666 transition(&ev, NULL);
667
668#if RX_ARGUS_HAS_CONSUMER
669 if (g.mode != RX_ARGUS_CONSUMER_OFF) {
670 atomic_store(&g.stop, 0);
671 if (pthread_create(&g.thread, NULL, consumer_main, NULL) != 0) {
672 g.active = 0;
673 return ARGUS_ERR_STATE;
674 }
675 g.thread_started = 1;
676 consumer_place(g.thread);
677 }
678#endif
679 return ARGUS_OK;
680}
681
682int rx_argus_active(void) { return g.active; }
683
684/* Assumes producers are quiescent (workers joined): flushes every slot's
685 * table from this thread, then stops the consumer after a final drain. */
686void rx_argus_shutdown(void) {
687 if (!g.active || g.pid != getpid()) return;
688 for (uint32_t i = 0; i < RX_ARGUS_PRODUCERS; i++) {
689 Slot *s = &g.slots[i];
690 if (!atomic_load(&s->inited)) continue;
691 flush_all(s);
692 go_idle(s);
693 }
694#if RX_ARGUS_HAS_CONSUMER
695 if (g.thread_started) {
696 atomic_store_explicit(&g.stop, 1, memory_order_release);
697 pthread_join(g.thread, NULL);
698 g.thread_started = 0;
699 }
700 if (g.stream) {
701 fclose(g.stream);
702 g.stream = NULL;
703 }
704#endif
705 g.active = 0;
706}
707
708void rx_argus_stats(RxArgusStats *o) {
709 memset(o, 0, sizeof *o);
710 for (uint32_t i = 0; i < RX_ARGUS_PRODUCERS; i++) {
711 Slot *s = &g.slots[i];
712 if (!atomic_load(&s->inited)) continue;
713 o->emitted += LOAD(s->emitted);
714 o->pushed += LOAD(s->pushed);
715 o->ring_refused += LOAD(s->refused);
716 o->ring_malformed += LOAD(s->malformed);
717 o->uses_counted += LOAD(s->uses_flushed);
718 o->summaries_emitted += LOAD(s->summaries);
719 o->summary_refused += LOAD(s->summary_refused);
720 o->order_slow += LOAD(s->order_slow);
721 o->mixed_keys += LOAD(s->mixed_keys);
722 for (unsigned k = 0; k <= ARGUS_EV_KIND_MAX; k++) o->emitted_by_kind[k] += LOAD(s->emitted_by_kind[k]);
723 }
724 o->unregistered_uses = atomic_load(&g.unregistered_uses);
725 o->producers_claimed = atomic_load(&g.claimed);
726 o->producers_max_live = atomic_load(&g.live_max);
727 for (unsigned k = 0; k <= ARGUS_EV_KIND_MAX; k++) o->received_by_kind[k] = g.received_by_kind[k];
728 o->received = g.received;
729 o->synthesized = g.synthesized;
730 o->batches = g.batches;
731 o->stream_records = g.stream_records;
732 o->ingest_errors = g.ingest_errors;
733 o->findings_total = g.findings_total;
734 for (unsigned c = 0; c <= ARGUS_F_MAX; c++) o->findings_by_code[c] = g.findings_by_code[c];
735 o->findings_kept = g.findings_total < RX_ARGUS_FINDINGS_MAX ? g.findings_total : RX_ARGUS_FINDINGS_MAX;
736 o->late_events = g.late;
737 o->stall_breaks = g.stall_breaks;
738 o->hold_ns_max = g.hold_ns_max;
739 o->lag_max = g.lag_max;
740 for (unsigned k = 0; k < 9; k++) o->authority_obs[k] = atomic_load(&g.obs_calls[k]);
741 o->consumer_mode = g.mode;
742 o->ring_bytes = g.ring_bytes;
743 o->core_bytes = g.core_bytes;
744 o->findings_bytes = sizeof g.findings;
745 o->producer_bytes = sizeof(Slot);
746#if RX_ARGUS_HAS_CONSUMER
747 if (g.core) argus_core_health(g.core, &o->core);
748#endif
749}
750
751size_t rx_argus_findings(ArgusFinding *out, size_t max) {
752 size_t n = g.findings_total < RX_ARGUS_FINDINGS_MAX ? g.findings_total : RX_ARGUS_FINDINGS_MAX;
753 if (n > max) n = max;
754 if (n) memcpy(out, g.findings, n * sizeof *out);
755 return n;
756}
757
758static const char *kind_name(unsigned k) {
759 switch (k) {
760 case ARGUS_EV_CAPABILITY_GRANTED: return "CAPABILITY_GRANTED";
761 case ARGUS_EV_CAPABILITY_USED: return "CAPABILITY_USED";
762 case ARGUS_EV_CAPABILITY_DENIED: return "CAPABILITY_DENIED";
763 case ARGUS_EV_CAPABILITY_REVOKED: return "CAPABILITY_REVOKED";
764 case ARGUS_EV_MACHINE_JOINED: return "MACHINE_JOINED";
765 case ARGUS_EV_WORLD_COMMITTED: return "WORLD_COMMITTED";
766 case ARGUS_EV_TELEMETRY_DROPPED: return "TELEMETRY_DROPPED";
767 case ARGUS_EV_CAPABILITY_USE_SUMMARY: return "CAPABILITY_USE_SUMMARY";
768 default: return NULL;
769 }
770}
771
772#define U(x) ((unsigned long long)(x))
773
774int rx_argus_write_summary(const char *path, const char *suite) {
775 FILE *f = fopen(path, "w");
776 if (!f) return -1;
777 RxArgusStats s;
778 rx_argus_stats(&s);
779 const char *auth =
780#if defined(RX_ARGUS_AUTHORITY_OBSERVER)
781 "observer";
782#elif defined(RX_ARGUS_AUTHORITY_WRAP)
783 "wrap";
784#else
785 "rx_aegis";
786#endif
787 fprintf(f, "{\n \"suite\": \"%s\",\n \"rx_argus\": %d,\n \"consumer_mode\": %d,\n"
788 " \"grant_source\": \"%s\",\n", suite ? suite : "", RX_ARGUS, s.consumer_mode, auth);
789 fprintf(f, " \"consumer_cpu\": \"%s\",\n \"consumer_pin_rc\": %d,\n", g.consumer_cpu, g.consumer_pin_rc);
790 fprintf(f, " \"machine_id\": \"");
791 for (unsigned i = 0; i < ARGUS_MACHINE_ID_LEN; i++) fprintf(f, "%02x", g.machine_id[i]);
792 fprintf(f, "\",\n \"producers_claimed\": %u,\n \"producers_max_live\": %u,\n",
793 s.producers_claimed, s.producers_max_live);
794 fprintf(f, " \"emitted\": %llu,\n \"pushed\": %llu,\n \"ring_refused\": %llu,\n"
795 " \"ring_malformed\": %llu,\n \"uses_counted\": %llu,\n \"summaries\": %llu,\n"
796 " \"summary_refused\": %llu,\n \"unregistered_uses\": %llu,\n \"lost_transitions\": %llu,\n"
797 " \"order_slow\": %llu,\n \"mixed_keys\": %llu,\n",
798 U(s.emitted), U(s.pushed), U(s.ring_refused), U(s.ring_malformed), U(s.uses_counted),
799 U(s.summaries_emitted), U(s.summary_refused), U(s.unregistered_uses),
800 U(atomic_load(&g.lost_transitions)), U(s.order_slow), U(s.mixed_keys));
801 fprintf(f, " \"received\": %llu,\n \"synthesized\": %llu,\n \"stream_records\": %llu,\n"
802 " \"lag_max_depth\": %u,\n \"hold_ns_max\": %llu,\n \"late_events\": %llu,\n"
803 " \"stall_breaks\": %llu,\n \"batches\": %llu,\n \"ingest_errors\": %llu,\n",
804 U(s.received), U(s.synthesized), U(s.stream_records), s.lag_max, U(s.hold_ns_max),
805 U(s.late_events), U(s.stall_breaks), U(s.batches), U(s.ingest_errors));
806 fprintf(f, " \"authority_observer_calls\": { \"mint\": %llu, \"revoke\": %llu, \"reclaim\": %llu, "
807 "\"epoch\": %llu, \"clock\": %llu, \"kill\": %llu, \"restart\": %llu },\n",
808 U(s.authority_obs[1]), U(s.authority_obs[2]), U(s.authority_obs[3]), U(s.authority_obs[4]),
809 U(s.authority_obs[5]), U(s.authority_obs[6]), U(s.authority_obs[7]));
810 fprintf(f, " \"by_kind\": {");
811 int first = 1;
812 for (unsigned k = 0; k <= ARGUS_EV_KIND_MAX; k++) {
813 if (!s.received_by_kind[k] && !s.emitted_by_kind[k]) continue;
814 const char *nm = kind_name(k);
815 fprintf(f, "%s\n \"%s%s%u\": { \"emitted\": %llu, \"received\": %llu }", first ? "" : ",",
816 nm ? nm : "", nm ? "_" : "kind_", k, U(s.emitted_by_kind[k]), U(s.received_by_kind[k]));
817 first = 0;
818 }
819 fprintf(f, "\n },\n \"core\": { \"events_received\": %llu, \"events_rejected\": %llu, "
820 "\"findings_emitted\": %llu, \"incidents_open\": %llu, \"events_not_applied\": %llu, "
821 "\"tables_full\": %llu, \"producers_untracked\": %llu },\n",
822 U(s.core.events_received), U(s.core.events_rejected), U(s.core.findings_emitted),
823 U(s.core.incidents_open), U(s.core.events_not_applied), U(s.core.tables_full),
824 U(s.core.producers_untracked));
825 fprintf(f, " \"memory_bytes\": { \"per_producer_slot\": %zu, \"ring_per_producer\": %zu, "
826 "\"producers_touched\": %zu, \"core\": %zu, \"findings\": %zu, \"static_reserved\": %zu },\n",
827 s.producer_bytes, s.ring_bytes, (size_t)s.producers_claimed * s.producer_bytes, s.core_bytes,
828 s.findings_bytes, sizeof g);
829 fprintf(f, " \"findings_total\": %llu,\n \"findings_by_code\": {", U(s.findings_total));
830 first = 1;
831 for (unsigned c = 0; c <= ARGUS_F_MAX; c++) {
832 if (!s.findings_by_code[c]) continue;
833 fprintf(f, "%s \"%u\": %llu", first ? "" : ",", c, U(s.findings_by_code[c]));
834 first = 0;
835 }
836 fprintf(f, " },\n \"findings\": [");
837 for (uint64_t i = 0; i < s.findings_kept; i++) {
838 const ArgusFinding *x = &g.findings[i];
839 fprintf(f, "%s\n { \"code\": %u, \"severity\": %u, \"sequence\": %llu, "
840 "\"prior_sequence\": %llu, \"principal\": %u, \"cap_id\": %u, \"cap_generation\": %llu }",
841 i ? "," : "", x->code, x->severity, U(x->sequence), U(x->prior_sequence), x->principal,
842 x->cap_id, U(x->cap_generation));
843 }
844 fprintf(f, "\n ]\n}\n");
845 fclose(f);
846 return 0;
847}
848
849/* ---- automatic process-wide instance -----------------------------------------------
850 * Existing suites are linked unchanged: a constructor starts ARGUS unless
851 * RX_ARGUS_AUTO=0. Environment:
852 * RX_ARGUS_CONSUMER ingest (default) | discard | off
853 * RX_ARGUS_STREAM path for the raw 128-byte record stream (ingest order)
854 * RX_ARGUS_SUMMARY path for the JSON summary written at exit
855 * RX_ARGUS_SUITE label in the summary
856 * RX_ARGUS_MACHINE_ID canonical machine identity, text form (aien_machine_id.h);
857 * malformed = ARGUS does not start. Unset: provisional id
858 * RX_ARGUS_RUN_ID run id for the provisional machine id (default: pid-based)
859 * RX_ARGUS_CONSUMER_CPU consumer placement: auto (default: the CPUs outside the
860 * process mask, if any) | none | a CPU list such as "3" or "0-2,4" */
861__attribute__((constructor)) static void rx_argus_auto_start(void) {
862 const char *a = getenv("RX_ARGUS_AUTO");
863 if (a && strcmp(a, "0") == 0) return;
864 const char *c = getenv("RX_ARGUS_CONSUMER");
865 int mode = RX_ARGUS_CONSUMER_INGEST;
866 if (c && strcmp(c, "discard") == 0) mode = RX_ARGUS_CONSUMER_DISCARD;
867 else if (c && strcmp(c, "off") == 0) mode = RX_ARGUS_CONSUMER_OFF;
868 const char *run = getenv("RX_ARGUS_RUN_ID");
869 char fallback[64];
870 if (!run) {
871 snprintf(fallback, sizeof fallback, "pid-%ld", (long)getpid());
872 run = fallback;
873 }
874 const char *mtext = getenv("RX_ARGUS_MACHINE_ID");
875 AienMachineId mid;
876 if (mtext && aien_mid_from_text(mtext, &mid) != AIEN_MID_OK) {
877 fprintf(stderr, "rx_argus: RX_ARGUS_MACHINE_ID is not a canonical machine identity\n");
878 return;
879 }
880 if (rx_argus_start(run, mtext ? mid.id : NULL, mode, getenv("RX_ARGUS_STREAM")) != ARGUS_OK)
881 fprintf(stderr, "rx_argus: start failed\n");
882}
883
884__attribute__((destructor)) static void rx_argus_auto_stop(void) {
885 if (!g.active || g.pid != getpid()) return;
886 rx_argus_shutdown();
887 const char *p = getenv("RX_ARGUS_SUMMARY");
888 if (p) rx_argus_write_summary(p, getenv("RX_ARGUS_SUITE"));
889}
890
891/* ---- authority-sourced GRANTED/REVOKED (RX_ARGUS_AUTHORITY_OBSERVER) -----------------
892 * The authority announces its own transitions (aienos_cap_set_observer,
893 * aienos 12add16, native in the authority pinned by aienos.lock, d39dd5b).
894 *
895 * The observer is installed on every authority the process starts: a link
896 * wrap of aienos_cap_start (the admin handle is only known there). The wrap
897 * also emits GRANTED for the office (cap 0), which is created at start and
898 * never minted. Mapping:
899 * MINT ok -> GRANTED MINT refused -> DENIED (cap ARGUS_CAP_NONE)
900 * REVOKE ok -> REVOKED (each descendant too)
901 * EPOCH ok -> REVOKED for every cap this authority announced live
902 * (an epoch bump kills every earlier reference, office included)
903 * RESTART ok -> REVOKED for every live cap, then GRANTED for the new office
904 * RECLAIM/CLOCK/KILL, refused admin ops -> counted only (no v1.1 kind). */
905#if defined(RX_ARGUS_AUTHORITY_OBSERVER)
906
907int __real_aienos_cap_start(AienosCapAdmin **admin, AienosCapView **view);
908int __wrap_aienos_cap_start(AienosCapAdmin **admin, AienosCapView **view);
909
910static void kill_all_live(uint32_t a, const AienosCapView *view) {
911 for (uint32_t c = 0; c < RX_ARGUS_USE_SLOTS; c++) {
912 if (!g.auth_live[a][c]) continue;
913 rx_argus_emit_cap_revoked(view, g.auth_subject[a][c], c, g.auth_live[a][c], 0);
914 g.auth_live[a][c] = 0;
915 }
916}
917
918static void authority_observer(void *ctx, uint32_t op, const AienosCapEntry *e, int rc) {
919 uint32_t a = (uint32_t)(uintptr_t)ctx;
920 if (op < 9) atomic_fetch_add(&g.obs_calls[op], 1);
921 if (!g.active || a >= AUTH_MAX) return;
922 const AienosCapView *view = g.auth_view[a];
923 pthread_mutex_lock(&g.obs_mu);
924 switch (op) {
925 case AIENOS_CAP_OBS_MINT:
926 if (e && rc == 0) {
927 rx_argus_emit_cap_granted(view, e->subject, e->cap_id, (uint64_t)e->generation,
928 e->resource, e->rights, 0);
929 if (e->cap_id < RX_ARGUS_USE_SLOTS) {
930 g.auth_live[a][e->cap_id] = (uint64_t)e->generation;
931 g.auth_subject[a][e->cap_id] = e->subject;
932 }
933 } else {
934 rx_argus_emit_cap_denied(view, 0, ARGUS_CAP_NONE, 0, 0, 0, rc);
935 }
936 break;
937 case AIENOS_CAP_OBS_REVOKE:
938 if (e && rc == 0) {
939 rx_argus_emit_cap_revoked(view, e->subject, e->cap_id, (uint64_t)e->generation, 0);
940 if (e->cap_id < RX_ARGUS_USE_SLOTS) g.auth_live[a][e->cap_id] = 0;
941 }
942 break;
943 case AIENOS_CAP_OBS_EPOCH:
944 if (e && rc == 0) kill_all_live(a, view);
945 break;
946 case AIENOS_CAP_OBS_RESTART:
947 if (e && rc == 0) {
948 kill_all_live(a, view);
949 rx_argus_emit_cap_granted(view, e->subject, e->cap_id, (uint64_t)e->generation,
950 e->resource, e->rights, 0);
951 if (e->cap_id < RX_ARGUS_USE_SLOTS) {
952 g.auth_live[a][e->cap_id] = (uint64_t)e->generation;
953 g.auth_subject[a][e->cap_id] = e->subject;
954 }
955 }
956 break;
957 default:
958 break;
959 }
960 pthread_mutex_unlock(&g.obs_mu);
961}
962
963int __wrap_aienos_cap_start(AienosCapAdmin **admin, AienosCapView **view) {
964 int rc = __real_aienos_cap_start(admin, view);
965 if (rc != 0 || !g.active || !admin || !*admin || !view || !*view) return rc;
966 pthread_mutex_lock(&g.obs_mu);
967 uint32_t a = g.n_auth < AUTH_MAX ? g.n_auth++ : AUTH_MAX;
968 if (a < AUTH_MAX) {
969 g.auth_view[a] = *view;
970 memset(g.auth_live[a], 0, sizeof g.auth_live[a]);
971 }
972 pthread_mutex_unlock(&g.obs_mu);
973 if (a >= AUTH_MAX) return rc;
974 aienos_cap_set_observer(*admin, authority_observer, (void *)(uintptr_t)a);
975 AienosCapRef office;
976 AienosCapEntry oe;
977 if (aienos_cap_office(*admin, &office) == 0 && aienos_cap_inspect(*view, office, &oe) == 0) {
978 pthread_mutex_lock(&g.obs_mu);
979 rx_argus_emit_cap_granted(*view, oe.subject, office.cap_id, (uint64_t)office.generation,
980 oe.resource, oe.rights, 0);
981 if (office.cap_id < RX_ARGUS_USE_SLOTS) {
982 g.auth_live[a][office.cap_id] = (uint64_t)office.generation;
983 g.auth_subject[a][office.cap_id] = oe.subject;
984 }
985 pthread_mutex_unlock(&g.obs_mu);
986 }
987 return rc;
988}
989#endif /* RX_ARGUS_AUTHORITY_OBSERVER */
990
991#endif /* RX_ARGUS */