Skip to content

Commit f2eeefa

Browse files
committed
feat: observe declared units through native identity
1 parent 59a3a59 commit f2eeefa

2 files changed

Lines changed: 119 additions & 1 deletion

File tree

‎src/process.cpp‎

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1607,7 +1607,7 @@ int kal_process_end_as_requested(void) {
16071607
}
16081608

16091609
kal_uintptr kal_process_props(void) {
1610-
return KAL_PROCESS_PROP_TERMINATE | KAL_PROCESS_PROP_STREAM_PASSING
1610+
return KAL_PROCESS_PROP_JOB_OBSERVE | KAL_PROCESS_PROP_TERMINATE | KAL_PROCESS_PROP_STREAM_PASSING
16111611
| KAL_PROCESS_PROP_EXIT_STATUS
16121612
| KAL_PROCESS_PROP_CHANNEL | KAL_PROCESS_PROP_GRANT_DIR
16131613
| KAL_PROCESS_PROP_BOUND_LIFETIME
@@ -1953,3 +1953,92 @@ int kal_process_job_limit(kal_job j, kal_uintptr kind, kal_u64 value) {
19531953
}
19541954

19551955
}
1956+
1957+
// Observable units (openkal 0.24). The unit stays the kernel's own group;
1958+
// identity validation does not create a registry or infer quiet from a leader.
1959+
#include "unit_reference.h"
1960+
namespace {
1961+
struct unit_identity {
1962+
kal_u64 group = 0, birth = 0, previous = 0, previous_birth = 0;
1963+
char boot[64]{}, space[64]{};
1964+
};
1965+
bool unit_birth(okl_long pid, kal_u64& value) { okl_u64 ticks = 0; if (!started_ticks(pid,ticks)) return false; value = ticks; return true; }
1966+
bool unit_origin(char* boot, char* space) {
1967+
if (self_number() != okl::sys(okl::nr_getpid)) return false;
1968+
const auto fd = okl::sys(okl::nr_openat, okl::at_fdcwd,
1969+
reinterpret_cast<okl_long>("/proc/sys/kernel/random/boot_id"), okl::o_rdonly | okl::o_cloexec, 0);
1970+
if (okl::failed(fd)) return false;
1971+
auto n = okl::sys(okl::nr_read, fd, reinterpret_cast<okl_long>(boot), 63);
1972+
okl::sys(okl::nr_close, fd);
1973+
if (n <= 0 || n >= 63) return false;
1974+
while (n > 0 && (boot[n - 1] == '\n' || boot[n - 1] == '\r')) --n;
1975+
boot[n] = 0;
1976+
n = okl::sys(okl::nr_readlinkat, okl::at_fdcwd,
1977+
reinterpret_cast<okl_long>("/proc/self/ns/pid"), reinterpret_cast<okl_long>(space), 63);
1978+
if (n <= 0 || n >= 63) return false;
1979+
space[n] = 0; return true;
1980+
}
1981+
bool unit_parse(const char* p, kal_uintptr n, unit_identity& u) {
1982+
if (!p || n == 0 || n > 256) return false;
1983+
unit_reference::reader r{p,n}; char kind[8]{};
1984+
return r.token(kind, sizeof kind) && unit_reference::same(kind,"L1")
1985+
&& r.number(u.group) && u.group > 0 && u.group <= 0x7fffffffu
1986+
&& r.number(u.birth) && u.birth != 0
1987+
&& r.number(u.previous) && u.previous > 0 && u.previous <= 0x7fffffffu
1988+
&& r.number(u.previous_birth) && u.previous_birth != 0
1989+
&& r.token(u.boot,sizeof u.boot) && r.token(u.space,sizeof u.space) && r.at == n;
1990+
}
1991+
bool unit_same_origin(const unit_identity& u) {
1992+
char boot[64]{}, space[64]{};
1993+
return unit_origin(boot,space) && unit_reference::same(u.boot,boot) && unit_reference::same(u.space,space);
1994+
}
1995+
}
1996+
extern "C" {
1997+
int kal_process_job_create(kal_job* out, char* reference, kal_uintptr capacity, kal_uintptr* used) {
1998+
if (!out || !reference || !used) return kal_err_invalid;
1999+
if (capacity < 256) return kal_err_no_space;
2000+
unit_identity u;
2001+
u.group = static_cast<kal_u64>(okl::sys(okl::nr_getpid));
2002+
u.previous = static_cast<kal_u64>(okl::sys(okl::nr_getpgid,0));
2003+
if (u.previous == u.group || !unit_origin(u.boot,u.space)
2004+
|| !unit_birth(0,u.birth) || !unit_birth(static_cast<okl_long>(u.previous),u.previous_birth)
2005+
|| !u.birth || !u.previous_birth) return kal_err_not_supported;
2006+
unit_reference::writer w; w.text("L1;"); w.number(u.group); w.next(); w.number(u.birth); w.next();
2007+
w.number(u.previous); w.next(); w.number(u.previous_birth); w.next(); w.text(u.boot); w.next(); w.text(u.space);
2008+
if (!w.valid) return kal_err_no_space;
2009+
kal_job job{}; const int rc = kal_process_job_enter(&job);
2010+
if (rc != kal_ok) return rc;
2011+
*out = job; return unit_reference::copy(w,reference,capacity,used);
2012+
}
2013+
int kal_process_job_open(const char* reference, kal_uintptr length, kal_job* out) {
2014+
if (!out) return kal_err_invalid;
2015+
unit_identity u; if (!unit_parse(reference,length,u)) return kal_err_invalid;
2016+
if (!unit_same_origin(u)) return kal_err_not_supported;
2017+
out->h = static_cast<kal_uintptr>(u.group); return kal_ok;
2018+
}
2019+
int kal_process_job_observe(kal_job job, const char* reference, kal_uintptr length, kal_u32* state) {
2020+
if (!state) return kal_err_invalid; *state = KAL_JOB_UNKNOWN;
2021+
unit_identity u; if (!unit_parse(reference,length,u) || job.h != u.group) return kal_err_invalid;
2022+
if (!unit_same_origin(u)) return kal_ok;
2023+
const auto status = okl::sys(okl::nr_kill,-static_cast<okl_long>(u.group),0);
2024+
if (status == -3 /* ESRCH */) { *state = KAL_JOB_EMPTY; return kal_ok; }
2025+
if (okl::failed(status)) return kal_ok;
2026+
okl_u64 birth = 0;
2027+
if (started_ticks(static_cast<okl_long>(u.group),birth) && birth != u.birth) return kal_ok;
2028+
*state = KAL_JOB_OCCUPIED; return kal_ok;
2029+
}
2030+
int kal_process_job_retire(kal_job job, const char* reference, kal_uintptr length, kal_u32* state) {
2031+
if (!state) return kal_err_invalid; *state = KAL_JOB_UNKNOWN;
2032+
unit_identity u; if (!unit_parse(reference,length,u) || job.h != u.group) return kal_err_invalid;
2033+
okl_u64 birth = 0;
2034+
if (static_cast<kal_u64>(okl::sys(okl::nr_getpid)) != u.group || !unit_same_origin(u)
2035+
|| !started_ticks(0,birth) || birth != u.birth) return kal_err_permission;
2036+
const auto current = okl::sys(okl::nr_getpgid,0);
2037+
if (current == static_cast<okl_long>(u.group)) {
2038+
if (!started_ticks(static_cast<okl_long>(u.previous),birth) || birth != u.previous_birth) return kal_ok;
2039+
const auto rc = okl::sys(okl::nr_setpgid,0,static_cast<okl_long>(u.previous));
2040+
if (okl::failed(rc)) return kal_ok;
2041+
} else if (current != static_cast<okl_long>(u.previous)) return kal_ok;
2042+
return kal_process_job_observe(job,reference,length,state);
2043+
}
2044+
}

‎src/unit_reference.h‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Counted, opaque unit references. This codec stores no process registry.
2+
#pragma once
3+
#include <openkal/types.h>
4+
namespace unit_reference {
5+
struct writer {
6+
char bytes[256]{}; kal_uintptr size = 0; bool valid = true;
7+
void text(const char* p) { for (; *p; ++p) { if (size == sizeof bytes) { valid = false; return; } bytes[size++] = *p; } }
8+
void number(kal_u64 n) { char b[24]; unsigned used = 0; do { b[used++] = char('0' + n % 10); n /= 10; } while (n); while (used) { char c[2]{b[--used],0}; text(c); } }
9+
void next() { text(";"); }
10+
};
11+
struct reader {
12+
const char* bytes; kal_uintptr size, at = 0;
13+
bool token(char* p, kal_uintptr capacity) {
14+
kal_uintptr n = 0;
15+
while (at < size && bytes[at] != ';') { if (n + 1 >= capacity || bytes[at] < 33 || bytes[at] > 126) return false; p[n++] = bytes[at++]; }
16+
p[n] = 0; if (at < size) ++at; return n != 0;
17+
}
18+
bool number(kal_u64& n) {
19+
n = 0; const auto begin = at;
20+
while (at < size && bytes[at] != ';') { const auto c = bytes[at++]; if (c < '0' || c > '9' || n > (~kal_u64{0} - kal_u64(c - '0')) / 10) return false; n = n * 10 + kal_u64(c - '0'); }
21+
if (at == begin) return false; if (at < size) ++at; return true;
22+
}
23+
};
24+
inline bool same(const char* a, const char* b) { for (; *a && *a == *b; ++a, ++b) {} return *a == *b; }
25+
inline int copy(const writer& w, char* out, kal_uintptr capacity, kal_uintptr* used) {
26+
if (!w.valid || capacity < w.size) return kal_err_no_space;
27+
for (kal_uintptr i = 0; i < w.size; ++i) out[i] = w.bytes[i]; *used = w.size; return kal_ok;
28+
}
29+
}

0 commit comments

Comments
 (0)