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/runtime/rx_operator.c # 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 / runtime / rx_operator.c` 565 lines ยท 24141 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 /* R16 G6 operator entry point (docs/r16-operator-control.md). */ 18 #include "rx_operator.h" 19 20 #include 21 #include 22 #include 23 #include 24 #include 25 #include 26 #include 27 #include 28 #include 29 #include 30 #include 31 #include 32 #include 33 34 struct RxOperator { 35 RxOperatorConfig cfg; 36 char sock_path[sizeof(((struct sockaddr_un *)0)->sun_path)]; 37 int dir_fd; /* the validated control directory, held open */ 38 int lock_fd; /* flock on RX_OPERATOR_LOCK in it, held until close */ 39 struct stat sock_st, cred_st;/* what this instance created (device, inode) */ 40 int sock_made, cred_made; 41 int listen_fd; 42 int wake[2]; /* a byte on wake[1] ends the listener */ 43 pthread_t thread; 44 int thread_live; 45 pthread_mutex_t gate; /* held by the program while it sets the world up */ 46 }; 47 48 /* Validate an open directory descriptor: a directory owned by this uid with no 49 * group or other bits. Checks the descriptor itself, not a path. */ 50 static int dir_fd_ok(int fd, const char *dir, char *why, size_t n) { 51 struct stat st; 52 if (fstat(fd, &st) != 0) { snprintf(why, n, "%s: %s", dir, strerror(errno)); return -1; } 53 if (!S_ISDIR(st.st_mode)) { snprintf(why, n, "%s is not a directory", dir); return -1; } 54 if (st.st_uid != geteuid()) { 55 snprintf(why, n, "%s is owned by uid %u, not by this program's uid %u", dir, 56 (unsigned)st.st_uid, (unsigned)geteuid()); 57 return -1; 58 } 59 if (st.st_mode & 077) { 60 snprintf(why, n, "%s has mode %03o: group or other may reach it (need no bits in 077)", 61 dir, (unsigned)(st.st_mode & 0777)); 62 return -1; 63 } 64 return 0; 65 } 66 67 int rx_operator_dir_open(const char *dir, int create, int *fd_out, char *why, size_t n) { 68 *fd_out = -1; 69 if (!dir || !dir[0]) { snprintf(why, n, "no directory"); return -1; } 70 int fd = open(dir, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); 71 if (fd < 0 && errno == ENOENT && create) { 72 if (mkdir(dir, 0700) != 0 && errno != EEXIST) { 73 snprintf(why, n, "%s: cannot create: %s", dir, strerror(errno)); 74 return -1; 75 } 76 fd = open(dir, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); 77 } 78 if (fd < 0) { 79 int e = errno; 80 struct stat st; /* only to name the refusal; the decision is the failed open */ 81 int l = lstat(dir, &st) == 0; 82 if (l && S_ISLNK(st.st_mode)) snprintf(why, n, "%s is a symlink", dir); 83 else if (l && st.st_uid != geteuid()) 84 snprintf(why, n, "%s is owned by uid %u, not by this program's uid %u (%s)", dir, 85 (unsigned)st.st_uid, (unsigned)geteuid(), strerror(e)); 86 else if (e == ENOTDIR) snprintf(why, n, "%s is not a directory", dir); 87 else snprintf(why, n, "%s: %s", dir, strerror(e)); 88 return -1; 89 } 90 if (dir_fd_ok(fd, dir, why, n) != 0) { close(fd); return -1; } 91 *fd_out = fd; 92 return 0; 93 } 94 95 int rx_operator_dir_check(const char *dir, int create, char *why, size_t n) { 96 int fd; 97 if (rx_operator_dir_open(dir, create, &fd, why, n) != 0) return -1; 98 close(fd); 99 return 0; 100 } 101 102 /* flock on RX_OPERATOR_LOCK inside the held directory `dfd`. */ 103 static int lock_at(int dfd, const char *dir, int *lock_fd, char *why, size_t n) { 104 *lock_fd = -1; 105 int fd = openat(dfd, RX_OPERATOR_LOCK, O_RDWR | O_CREAT | O_CLOEXEC | O_NOFOLLOW, 0600); 106 if (fd < 0) { snprintf(why, n, "%s/%s: %s", dir, RX_OPERATOR_LOCK, strerror(errno)); return -1; } 107 struct stat st; 108 if (fstat(fd, &st) != 0 || !S_ISREG(st.st_mode) || st.st_uid != geteuid()) { 109 snprintf(why, n, "%s/%s is not a regular file of this uid", dir, RX_OPERATOR_LOCK); 110 close(fd); 111 return -1; 112 } 113 if (flock(fd, LOCK_EX | LOCK_NB) != 0) { 114 int e = errno; 115 char held[32] = ""; 116 ssize_t r = pread(fd, held, sizeof held - 1, 0); 117 held[r > 0 ? r : 0] = 0; 118 held[strcspn(held, "\n")] = 0; 119 if (e == EWOULDBLOCK) 120 snprintf(why, n, "%s is in use by another program (pid %s)", dir, held[0] ? held : "?"); 121 else 122 snprintf(why, n, "%s/%s: lock: %s", dir, RX_OPERATOR_LOCK, strerror(e)); 123 close(fd); 124 return -1; 125 } 126 char pid[32]; 127 int len = snprintf(pid, sizeof pid, "%ld\n", (long)getpid()); 128 if (ftruncate(fd, 0) != 0 || pwrite(fd, pid, (size_t)len, 0) != len) { 129 snprintf(why, n, "%s/%s: %s", dir, RX_OPERATOR_LOCK, strerror(errno)); 130 close(fd); 131 return -1; 132 } 133 *lock_fd = fd; 134 return 0; 135 } 136 137 int rx_operator_lock_dir(const char *dir, int create, int *lock_fd, char *why, size_t n) { 138 int dfd; 139 *lock_fd = -1; 140 if (rx_operator_dir_open(dir, create, &dfd, why, n) != 0) return -1; 141 int rc = lock_at(dfd, dir, lock_fd, why, n); 142 close(dfd); 143 return rc; 144 } 145 146 static void hex32(const uint8_t *b, char out[65]) { 147 for (int i = 0; i < 32; i++) snprintf(out + 2 * i, 3, "%02x", b[i]); 148 } 149 150 static uint64_t realtime_ns(void) { 151 struct timespec ts; 152 clock_gettime(CLOCK_REALTIME, &ts); 153 return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; 154 } 155 156 static uint64_t monotonic_ns(void) { 157 struct timespec ts; 158 clock_gettime(CLOCK_MONOTONIC, &ts); 159 return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; 160 } 161 162 /* The operator's copy of its credential: 0600, written whole or not at all, 163 * every name relative to the held control directory. */ 164 static int write_cred(RxOperator *op, uint32_t subject, const RxCallerCred *cred, RxCapRef cap) { 165 static const char tmp[] = RX_OPERATOR_CRED ".partial"; 166 char secret[65], buf[1024]; 167 hex32(cred->secret, secret); 168 int len = snprintf(buf, sizeof buf, 169 "aien-operator-credential v1\nworld %s %ld %llu\nsocket %s\nsubject %u\n" 170 "cred %llu %s\ncap %u %llu\n", 171 op->cfg.world_key ? op->cfg.world_key : "-", (long)getpid(), 172 (unsigned long long)realtime_ns(), op->sock_path, subject, 173 (unsigned long long)cred->generation, secret, cap.cap_id, 174 (unsigned long long)cap.generation); 175 for (size_t i = 0; i < sizeof secret; i++) ((volatile char *)secret)[i] = 0; 176 if (len <= 0 || (size_t)len >= sizeof buf) return -1; 177 int fd = openat(op->dir_fd, tmp, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW, 0600); 178 int rc = fd < 0 ? -1 : 0; 179 if (rc == 0 && fchmod(fd, 0600) != 0) rc = -1; 180 for (int off = 0; rc == 0 && off < len;) { 181 ssize_t w = write(fd, buf + off, (size_t)(len - off)); 182 if (w < 0 && errno == EINTR) continue; 183 if (w <= 0) rc = -1; else off += (int)w; 184 } 185 if (rc == 0 && fsync(fd) != 0) rc = -1; 186 if (rc == 0 && fstat(fd, &op->cred_st) != 0) rc = -1; 187 if (fd >= 0) close(fd); 188 for (size_t i = 0; i < sizeof buf; i++) ((volatile char *)buf)[i] = 0; 189 if (rc == 0 && renameat(op->dir_fd, tmp, op->dir_fd, RX_OPERATOR_CRED) != 0) rc = -1; 190 if (rc == 0 && fsync(op->dir_fd) != 0) rc = -1; 191 if (rc != 0) unlinkat(op->dir_fd, tmp, 0); 192 else op->cred_made = 1; 193 return rc; 194 } 195 196 /* Remove `name` from the held directory only if it is still the file this 197 * instance made (same device and inode). */ 198 static void unlink_own(RxOperator *op, const char *name, const struct stat *made, int *flag) { 199 struct stat st; 200 if (!*flag || op->dir_fd < 0) return; 201 if (fstatat(op->dir_fd, name, &st, AT_SYMLINK_NOFOLLOW) == 0 && 202 st.st_dev == made->st_dev && st.st_ino == made->st_ino) 203 unlinkat(op->dir_fd, name, 0); 204 *flag = 0; 205 } 206 207 /* ---- one request ---------------------------------------------------------- */ 208 209 typedef struct { 210 char cmd[16]; 211 uint32_t subject; 212 RxCallerCred cred; 213 RxCapRef cap; 214 int32_t reason; 215 uint64_t deadline_ns; /* CLOCK_MONOTONIC: the client has given up after this */ 216 int have_subject, have_gen, have_secret, have_cap, have_reason, have_deadline; 217 } Req; 218 219 /* A request deadline further ahead than this is refused (the client waits 60 s). */ 220 #define RX_OPERATOR_DEADLINE_MAX_NS 120000000000ull 221 222 static int hexval(char c) { 223 if (c >= '0' && c <= '9') return c - '0'; 224 if (c >= 'a' && c <= 'f') return c - 'a' + 10; 225 if (c >= 'A' && c <= 'F') return c - 'A' + 10; 226 return -1; 227 } 228 229 /* Strict unsigned decimal into *out, at most `max`. */ 230 static int udec(const char *s, uint64_t max, uint64_t *out) { 231 if (!*s) return -1; 232 uint64_t v = 0; 233 for (; *s; s++) { 234 if (*s < '0' || *s > '9') return -1; 235 uint64_t d = (uint64_t)(*s - '0'); 236 if (v > (max - d) / 10) return -1; 237 v = v * 10 + d; 238 } 239 *out = v; 240 return 0; 241 } 242 243 static int parse(char *line, Req *q) { 244 memset(q, 0, sizeof *q); 245 char *save = NULL; 246 char *t = strtok_r(line, " ", &save); 247 if (!t || strcmp(t, "aien-operator") != 0) return -1; 248 if (!(t = strtok_r(NULL, " ", &save)) || strcmp(t, "v1") != 0) return -1; 249 if (!(t = strtok_r(NULL, " ", &save)) || strlen(t) >= sizeof q->cmd) return -1; 250 snprintf(q->cmd, sizeof q->cmd, "%s", t); 251 while ((t = strtok_r(NULL, " ", &save))) { 252 char *eq = strchr(t, '='); 253 if (!eq) return -1; 254 *eq = 0; 255 const char *k = t, *v = eq + 1; 256 uint64_t x, y; 257 if (strcmp(k, "subject") == 0 && !q->have_subject) { 258 if (udec(v, UINT32_MAX, &x) != 0) return -1; 259 q->subject = (uint32_t)x; q->have_subject = 1; 260 } else if (strcmp(k, "gen") == 0 && !q->have_gen) { 261 if (udec(v, UINT64_MAX, &x) != 0) return -1; 262 q->cred.generation = x; q->have_gen = 1; 263 } else if (strcmp(k, "secret") == 0 && !q->have_secret) { 264 if (strlen(v) != 2 * RX_CALLER_SECRET_LEN) return -1; 265 for (unsigned i = 0; i < RX_CALLER_SECRET_LEN; i++) { 266 int hi = hexval(v[2 * i]), lo = hexval(v[2 * i + 1]); 267 if (hi < 0 || lo < 0) return -1; 268 q->cred.secret[i] = (uint8_t)(hi << 4 | lo); 269 } 270 q->have_secret = 1; 271 } else if (strcmp(k, "cap") == 0 && !q->have_cap) { 272 char a[24]; 273 const char *c = strchr(v, ':'); 274 if (!c || (size_t)(c - v) >= sizeof a) return -1; 275 memcpy(a, v, (size_t)(c - v)); 276 a[c - v] = 0; 277 if (udec(a, UINT32_MAX, &x) != 0 || udec(c + 1, UINT64_MAX, &y) != 0) return -1; 278 q->cap = (RxCapRef){(uint32_t)x, y}; q->have_cap = 1; 279 } else if (strcmp(k, "reason") == 0 && !q->have_reason) { 280 if (udec(v, INT32_MAX, &x) != 0) return -1; 281 q->reason = (int32_t)x; q->have_reason = 1; 282 } else if (strcmp(k, "deadline") == 0 && !q->have_deadline) { 283 if (udec(v, UINT64_MAX, &x) != 0) return -1; 284 q->deadline_ns = x; q->have_deadline = 1; 285 } else { 286 return -1; 287 } 288 } 289 if (!q->have_subject || !q->have_gen || !q->have_secret || !q->have_cap || !q->have_deadline) 290 return -1; 291 if (q->deadline_ns > monotonic_ns() + RX_OPERATOR_DEADLINE_MAX_NS) return -1; 292 if (q->have_reason && strcmp(q->cmd, "stop") != 0) return -1; 293 return 0; 294 } 295 296 static const char *refusal(int rc) { 297 return rc == RX_ERR_IDENTITY ? "identity" : rc == RX_ERR_AUTHORITY ? "authority" : NULL; 298 } 299 300 static void handle(RxOperator *op, Req *q, char *out, size_t n) { 301 RxWorld *w = op->cfg.world; 302 RxHaltStatus h; 303 memset(&h, 0, sizeof h); 304 int stop = strcmp(q->cmd, "stop") == 0, resume = strcmp(q->cmd, "resume") == 0; 305 int rc; 306 if (stop) { 307 rc = rx_world_emergency_stop(w, q->subject, &q->cred, q->cap, q->reason, &h); 308 if (rc == RX_OK) { 309 if (op->cfg.on_halt) op->cfg.on_halt(op->cfg.ctx, 1); 310 if (h.durable == 1) 311 snprintf(out, n, "OK state=stopped seq=%llu crumb=%llu durable=1", 312 (unsigned long long)h.seq, (unsigned long long)h.crumb); 313 else /* in force in memory; a restart would NOT come up stopped */ 314 snprintf(out, n, "STOPPED_NOT_DURABLE state=stopped seq=%llu crumb=%llu durable=%d " 315 "errno=%d (%s)", (unsigned long long)h.seq, (unsigned long long)h.crumb, 316 h.durable, h.durable < 0 ? -h.durable : 0, 317 h.durable < 0 ? strerror(-h.durable) : "no durable halt directory"); 318 } else if (rc == RX_HALT_ALREADY) { 319 snprintf(out, n, "ALREADY state=stopped seq=%llu", (unsigned long long)h.seq); 320 } else if (refusal(rc)) { 321 snprintf(out, n, "REFUSED reason=%s", refusal(rc)); 322 } else { 323 snprintf(out, n, "ERROR rc=%d", rc); 324 } 325 return; 326 } 327 if (resume) { 328 rc = rx_world_emergency_resume(w, q->subject, &q->cred, q->cap, &h); 329 if (rc == RX_OK) { 330 if (op->cfg.on_halt) op->cfg.on_halt(op->cfg.ctx, 0); 331 snprintf(out, n, "OK state=running seq=%llu", (unsigned long long)h.seq); 332 } else if (rc == RX_HALT_NOT_STOPPED) { 333 snprintf(out, n, "NOT_STOPPED state=running"); 334 } else if (refusal(rc)) { 335 snprintf(out, n, "REFUSED reason=%s", refusal(rc)); 336 } else { 337 snprintf(out, n, "ERROR rc=%d", rc); 338 } 339 return; 340 } 341 int status = strcmp(q->cmd, "status") == 0, shutdown = strcmp(q->cmd, "shutdown") == 0; 342 int revoke = strcmp(q->cmd, "revoke") == 0, revoke_cap = strcmp(q->cmd, "revoke-cap") == 0; 343 if (!status && !shutdown && !revoke && !revoke_cap) { snprintf(out, n, "BAD_REQUEST"); return; } 344 rc = rx_world_operator_authorize(w, q->subject, &q->cred, q->cap); 345 if (rc != RX_OK) { 346 if (refusal(rc)) snprintf(out, n, "REFUSED reason=%s", refusal(rc)); 347 else snprintf(out, n, "ERROR rc=%d", rc); 348 return; 349 } 350 rx_world_halt_status(w, &h); 351 if (status) { 352 int used = snprintf(out, n, "OK state=%s restored=%d seq=%llu durable=%d refused=%llu " 353 "cancelled=%llu crumbs=%llu reactions=%u", 354 h.halted ? "stopped" : "running", h.restored ? 1 : 0, 355 (unsigned long long)h.seq, h.durable, (unsigned long long)h.refused, 356 (unsigned long long)h.cancelled, 357 (unsigned long long)__atomic_load_n(&w->n_crumbs, __ATOMIC_RELAXED), 358 (unsigned)__atomic_load_n(&w->n_reactions, __ATOMIC_RELAXED)); 359 if (op->cfg.describe && used > 0 && (size_t)used + 2 < n) { 360 out[used++] = ' '; 361 op->cfg.describe(op->cfg.ctx, out + used, n - (size_t)used); 362 } 363 return; 364 } 365 if (shutdown) { 366 if (!h.halted) { snprintf(out, n, "NOT_STOPPED state=running"); return; } 367 if (op->cfg.on_shutdown) op->cfg.on_shutdown(op->cfg.ctx); 368 snprintf(out, n, "OK state=stopped shutdown=1"); 369 return; 370 } 371 /* revoke-cap / revoke: the authority's office voids the control 372 * capability; revoke also retires the caller enrollment (it needs the 373 * current credential, so only its holder can). */ 374 AienosCapRef office; 375 if (aienos_cap_office(op->cfg.admin, &office) != 0 || 376 aienos_cap_revoke(op->cfg.admin, office, 377 (AienosCapRef){q->cap.cap_id, q->cap.generation}) != 0) { 378 snprintf(out, n, "ERROR rc=revoke-capability"); 379 return; 380 } 381 if (revoke) { 382 rc = rx_world_revoke_caller(w, q->subject, &q->cred); 383 if (rc != RX_CALLER_OK) { snprintf(out, n, "ERROR rc=revoke-credential %d", rc); return; } 384 unlink_own(op, RX_OPERATOR_CRED, &op->cred_st, &op->cred_made); 385 snprintf(out, n, "OK revoked=capability,credential"); 386 return; 387 } 388 snprintf(out, n, "OK revoked=capability"); 389 } 390 391 static void serve_one(RxOperator *op, int fd) { 392 char line[RX_OPERATOR_LINE], body[RX_OPERATOR_LINE + 512]; 393 struct ucred pc; 394 socklen_t pl = sizeof pc; 395 struct timeval tv = {2, 0}; 396 /* Without both timeouts one client could hold the listener: refused, no reply. */ 397 if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof tv) != 0 || 398 setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof tv) != 0) 399 return; 400 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &pc, &pl) != 0 || pl != sizeof pc || 401 pc.uid != geteuid()) { 402 snprintf(body, sizeof body, "REFUSED reason=peer"); 403 goto reply; 404 } 405 size_t got = 0; 406 int complete = 0; 407 while (got < sizeof line - 1) { 408 ssize_t r = read(fd, line + got, sizeof line - 1 - got); 409 if (r < 0 && errno == EINTR) continue; 410 if (r <= 0) break; 411 got += (size_t)r; 412 char *nl = memchr(line, '\n', got); 413 if (nl) { 414 size_t len = (size_t)(nl - line); 415 *nl = 0; 416 /* nothing after the newline, no NUL byte inside the line */ 417 complete = len == got - 1 && strlen(line) == len; 418 break; 419 } 420 } 421 Req q; 422 memset(&q, 0, sizeof q); 423 if (!complete || parse(line, &q) != 0) { 424 snprintf(body, sizeof body, "BAD_REQUEST"); 425 } else { 426 pthread_mutex_lock(&op->gate); 427 /* A request waits here while the program sets a world up. Once its 428 * client has given up (deadline passed) it is dropped, never run late: 429 * a late resume would restart a world nobody is watching. A late stop 430 * is fail-safe and still runs. */ 431 if (strcmp(q.cmd, "stop") != 0 && monotonic_ns() > q.deadline_ns) 432 snprintf(body, sizeof body, "EXPIRED (the client deadline passed; nothing done)"); 433 else 434 handle(op, &q, body, sizeof body); 435 pthread_mutex_unlock(&op->gate); 436 } 437 for (size_t i = 0; i < sizeof q; i++) ((volatile uint8_t *)&q)[i] = 0; 438 for (size_t i = 0; i < sizeof line; i++) ((volatile char *)line)[i] = 0; 439 reply:; 440 char out[sizeof body + 32]; 441 int len = snprintf(out, sizeof out, "aien-operator v1 %s\n", body); 442 if (len > 0) { 443 size_t sent = 0; 444 while (sent < (size_t)len) { 445 ssize_t s = send(fd, out + sent, (size_t)len - sent, MSG_NOSIGNAL); 446 if (s < 0 && errno == EINTR) continue; 447 if (s <= 0) break; 448 sent += (size_t)s; 449 } 450 } 451 /* Read what the client still sends (a refusal is answered before its request is 452 * read) until it closes its side, so closing here never resets a client that is 453 * still writing. Bounded: a few reads, each under the 2 s receive timeout. */ 454 shutdown(fd, SHUT_WR); 455 char sink[RX_OPERATOR_LINE]; 456 for (int i = 0; i < 4;) { 457 ssize_t r = read(fd, sink, sizeof sink); 458 if (r < 0 && errno == EINTR) continue; 459 if (r <= 0) break; 460 i++; 461 } 462 } 463 464 /* The listener: one connection at a time until the wake pipe fires. It 465 * sequences no faculty; it carries operator requests to the world. */ 466 static void *listener_main(void *arg) { 467 RxOperator *op = arg; 468 for (;;) { 469 struct pollfd p[2] = {{op->listen_fd, POLLIN, 0}, {op->wake[0], POLLIN, 0}}; 470 int r = poll(p, 2, -1); 471 if (r < 0 && errno == EINTR) continue; 472 if (r < 0 || (p[1].revents & (POLLIN | POLLHUP | POLLERR))) break; 473 if (!(p[0].revents & POLLIN)) continue; 474 int fd = accept4(op->listen_fd, NULL, NULL, SOCK_CLOEXEC); 475 if (fd < 0) continue; 476 serve_one(op, fd); 477 close(fd); 478 } 479 return NULL; 480 } 481 482 int rx_operator_open(RxOperator **out, const RxOperatorConfig *cfg, uint32_t subject, 483 RxCallerCred *cred, RxCapRef cap) { 484 if (!out || !cfg || !cfg->world || !cfg->admin || !cfg->control_dir || !cred) return -1; 485 *out = NULL; 486 char why[320]; 487 RxOperator *op = calloc(1, sizeof *op); 488 if (!op) return -1; 489 op->cfg = *cfg; 490 op->dir_fd = op->lock_fd = op->listen_fd = -1; 491 op->wake[0] = op->wake[1] = -1; 492 pthread_mutex_init(&op->gate, NULL); 493 pthread_mutex_lock(&op->gate); /* held until rx_operator_release */ 494 /* The control directory is held open and locked: from here every name in 495 * it is resolved against this descriptor, and no other program can be 496 * serving or replacing the socket and the credential. */ 497 if (rx_operator_dir_open(cfg->control_dir, 0, &op->dir_fd, why, sizeof why) != 0 || 498 lock_at(op->dir_fd, cfg->control_dir, &op->lock_fd, why, sizeof why) != 0) { 499 fprintf(stderr, "R13 operator: control directory refused: %s\n", why); 500 goto fail; 501 } 502 char bind_path[sizeof(((struct sockaddr_un *)0)->sun_path)]; 503 if ((size_t)snprintf(op->sock_path, sizeof op->sock_path, "%s/%s", cfg->control_dir, 504 RX_OPERATOR_SOCK) >= sizeof op->sock_path || 505 (size_t)snprintf(bind_path, sizeof bind_path, "/proc/self/fd/%d/%s", op->dir_fd, 506 RX_OPERATOR_SOCK) >= sizeof bind_path) { 507 fprintf(stderr, "R13 operator: control path too long for a socket address\n"); 508 goto fail; 509 } 510 /* A socket left by an earlier start of this program (the lock is ours). */ 511 if (unlinkat(op->dir_fd, RX_OPERATOR_SOCK, 0) != 0 && errno != ENOENT) { 512 fprintf(stderr, "R13 operator: stale socket: %s\n", strerror(errno)); 513 goto fail; 514 } 515 struct sockaddr_un a; 516 memset(&a, 0, sizeof a); 517 a.sun_family = AF_UNIX; 518 memcpy(a.sun_path, bind_path, strlen(bind_path) + 1); /* lands in the held directory */ 519 op->listen_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); 520 mode_t old = umask(077); 521 int ok = op->listen_fd >= 0 && bind(op->listen_fd, (struct sockaddr *)&a, sizeof a) == 0; 522 umask(old); 523 if (ok && fstatat(op->dir_fd, RX_OPERATOR_SOCK, &op->sock_st, AT_SYMLINK_NOFOLLOW) == 0 && 524 S_ISSOCK(op->sock_st.st_mode)) 525 op->sock_made = 1; 526 else 527 ok = 0; 528 ok = ok && fchmodat(op->dir_fd, RX_OPERATOR_SOCK, 0600, 0) == 0 && 529 listen(op->listen_fd, 8) == 0 && pipe2(op->wake, O_CLOEXEC) == 0 && 530 write_cred(op, subject, cred, cap) == 0; 531 for (size_t i = 0; i < sizeof *cred; i++) ((volatile uint8_t *)cred)[i] = 0; 532 if (ok && pthread_create(&op->thread, NULL, listener_main, op) == 0) { 533 op->thread_live = 1; 534 *out = op; 535 return 0; 536 } 537 fprintf(stderr, "R13 operator: entry point could not start: %s\n", strerror(errno)); 538 fail: 539 for (size_t i = 0; i < sizeof *cred; i++) ((volatile uint8_t *)cred)[i] = 0; 540 pthread_mutex_unlock(&op->gate); 541 rx_operator_close(op); 542 return -1; 543 } 544 545 void rx_operator_hold(RxOperator *op) { if (op) pthread_mutex_lock(&op->gate); } 546 void rx_operator_release(RxOperator *op) { if (op) pthread_mutex_unlock(&op->gate); } 547 548 void rx_operator_close(RxOperator *op) { 549 if (!op) return; 550 if (op->thread_live) { 551 char b = 1; 552 while (write(op->wake[1], &b, 1) < 0 && errno == EINTR) {} 553 pthread_join(op->thread, NULL); 554 } 555 if (op->listen_fd >= 0) close(op->listen_fd); 556 if (op->wake[0] >= 0) close(op->wake[0]); 557 if (op->wake[1] >= 0) close(op->wake[1]); 558 /* Only what this instance created, while its lock is still held. */ 559 unlink_own(op, RX_OPERATOR_SOCK, &op->sock_st, &op->sock_made); 560 unlink_own(op, RX_OPERATOR_CRED, &op->cred_st, &op->cred_made); 561 if (op->lock_fd >= 0) close(op->lock_fd); /* releases the flock */ 562 if (op->dir_fd >= 0) close(op->dir_fd); 563 pthread_mutex_destroy(&op->gate); 564 free(op); 565 }