Skip to content

Commit 225c545

Browse files
authored
Merge pull request #51 from mcpplibs/feat/observable-process-units
feat: release openkal-linux 0.25.0 observable process units
2 parents 59a3a59 + b429bac commit 225c545

6 files changed

Lines changed: 149 additions & 10 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ env:
3636
# fails with exit 2. This is the CI pin -- what this repository is
3737
# tested against -- and not a floor: nothing here records a minimum
3838
# engine for consumers.
39-
MCPP_VERSION: 2026.9.11.3
40-
XLINGS_VERSION: v2026.8.17.2
39+
MCPP_VERSION: 2026.11.5.1
40+
XLINGS_VERSION: v2026.10.26.1
4141
XLINGS_NON_INTERACTIVE: '1'
4242

4343
jobs:

‎README.md‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@ for Linux, written on the kernel's own system-call interface.
55

66
```toml
77
[dependencies]
8-
openkal = "0.23.0"
8+
openkal = "0.24.0"
99

1010
[target.'cfg(os = "linux")'.dependencies]
11-
openkal-linux = "0.24.2"
11+
openkal-linux = "0.25.0"
1212
```
1313

14+
## Observable process units
15+
16+
The process interface provides the OpenKal 0.24 declared-unit operations.
17+
References bind the original process-group identity to its birth, boot and
18+
PID namespace. Retirement checks the live continuation before and after
19+
membership migration; ambiguity reports `unknown`. No process registry is
20+
installed.
21+
1422
## Why it does not use a C library
1523

1624
Version 0.4 of this implementation was written upon the C library of the host,

‎mcpp.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
namespace = "mcpplibs"
33
name = "openkal-linux"
4-
version = "0.24.2"
4+
version = "0.25.0"
55
description = "The reference implementation of openkal for Linux, written on the kernel's own system-call interface so that it can be placed beneath a C library as well as above one."
66
license = "Apache-2.0"
77

@@ -67,7 +67,7 @@ provides-interfaces = [
6767
]
6868

6969
[dependencies]
70-
openkal = "0.23.0"
70+
openkal = "0.24.0"
7171

7272
# The package contributes definitions and no modules. The interface it
7373
# implements is declared by the specification package, which this package

‎src/process.cpp‎

Lines changed: 95 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,97 @@ 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;
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,"L2")
1985+
&& r.number(u.group) && u.group > 0 && u.group <= 0x7fffffffu
1986+
&& r.number(u.birth) && u.birth != 0
1987+
&& r.token(u.boot,sizeof u.boot) && r.token(u.space,sizeof u.space) && r.at == n;
1988+
}
1989+
bool unit_same_origin(const unit_identity& u) {
1990+
char boot[64]{}, space[64]{};
1991+
return unit_origin(boot,space) && unit_reference::same(u.boot,boot) && unit_reference::same(u.space,space);
1992+
}
1993+
}
1994+
extern "C" {
1995+
int kal_process_job_create(kal_job* out, char* reference, kal_uintptr capacity, kal_uintptr* used) {
1996+
if (!out || !reference || !used) return kal_err_invalid;
1997+
if (capacity < 256) return kal_err_no_space;
1998+
unit_identity u;
1999+
u.group = static_cast<kal_u64>(okl::sys(okl::nr_getpid));
2000+
if (!unit_origin(u.boot,u.space)||!unit_birth(0,u.birth)||!u.birth) return kal_err_not_supported;
2001+
unit_reference::writer w; w.text("L2;"); w.number(u.group); w.next(); w.number(u.birth); w.next();
2002+
w.text(u.boot); w.next(); w.text(u.space);
2003+
if (!w.valid) return kal_err_no_space;
2004+
kal_job job{}; const int rc = kal_process_job_enter(&job);
2005+
if (rc != kal_ok) return rc;
2006+
*out = job; return unit_reference::copy(w,reference,capacity,used);
2007+
}
2008+
int kal_process_job_open(const char* reference, kal_uintptr length, kal_job* out) {
2009+
if (!out) return kal_err_invalid;
2010+
unit_identity u; if (!unit_parse(reference,length,u)) return kal_err_invalid;
2011+
if (!unit_same_origin(u)) return kal_err_not_supported;
2012+
out->h = static_cast<kal_uintptr>(u.group); return kal_ok;
2013+
}
2014+
int kal_process_job_observe(kal_job job, const char* reference, kal_uintptr length, kal_u32* state) {
2015+
if (!state) return kal_err_invalid; *state = KAL_JOB_UNKNOWN;
2016+
unit_identity u; if (!unit_parse(reference,length,u) || job.h != u.group) return kal_err_invalid;
2017+
if (!unit_same_origin(u)) return kal_ok;
2018+
const auto status = okl::sys(okl::nr_kill,-static_cast<okl_long>(u.group),0);
2019+
if (status == -3 /* ESRCH */) { *state = KAL_JOB_EMPTY; return kal_ok; }
2020+
if (okl::failed(status)) return kal_ok;
2021+
okl_u64 birth = 0;
2022+
if (started_ticks(static_cast<okl_long>(u.group),birth) && birth != u.birth) return kal_ok;
2023+
*state = KAL_JOB_OCCUPIED; return kal_ok;
2024+
}
2025+
int kal_process_job_retire(kal_job job, const char* reference, kal_uintptr length,
2026+
const kal_program_identity* continuation, kal_u32* state) {
2027+
if (!state) return kal_err_invalid; *state = KAL_JOB_UNKNOWN;
2028+
unit_identity u; if (!unit_parse(reference,length,u) || job.h != u.group) return kal_err_invalid;
2029+
okl_u64 birth = 0;
2030+
if (static_cast<kal_u64>(okl::sys(okl::nr_getpid)) != u.group || !unit_same_origin(u)
2031+
|| !started_ticks(0,birth) || birth != u.birth) return kal_err_permission;
2032+
if (!continuation || !continuation->id || continuation->id > 0x7fffffffu || !continuation->started_ns)
2033+
return kal_ok;
2034+
const auto anchor = static_cast<okl_long>(continuation->id);
2035+
if (!started_ticks(anchor,birth) || ticks_to_ns(birth) != continuation->started_ns
2036+
|| stat_state(anchor) == 'Z' || stat_state(anchor) == 'X') return kal_ok;
2037+
const auto destination = okl::sys(okl::nr_getpgid,anchor);
2038+
if (destination <= 0 || destination == static_cast<okl_long>(u.group)) return kal_ok;
2039+
const auto current = okl::sys(okl::nr_getpgid,0);
2040+
if (current == static_cast<okl_long>(u.group)) {
2041+
if (okl::failed(okl::sys(okl::nr_setpgid,0,destination))) return kal_ok;
2042+
} else if (current != destination) return kal_ok;
2043+
if (!started_ticks(anchor,birth) || ticks_to_ns(birth) != continuation->started_ns
2044+
|| stat_state(anchor) == 'Z' || stat_state(anchor) == 'X'
2045+
|| okl::sys(okl::nr_getpgid,anchor) != destination
2046+
|| okl::sys(okl::nr_getpgid,0) != destination) return kal_ok;
2047+
return kal_process_job_observe(job,reference,length,state);
2048+
}
2049+
}

‎src/task.cpp‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,15 @@ struct self_bounds {
213213
int state; // 0 not yet asked, 1 answered
214214
};
215215

216-
thread_local self_bounds g_self = { nullptr, 0, 0 };
216+
// A standalone implementation establishes the program's one native TLS block.
217+
// PIC must not add a call into a C library's dynamic TLS resolver beneath it.
218+
// The hosted arrangement retains the compiler's dynamic-loading convention.
219+
#ifdef OKL_STANDALONE
220+
#define OKL_TLS_MODEL __attribute__((tls_model("initial-exec")))
221+
#else
222+
#define OKL_TLS_MODEL
223+
#endif
224+
thread_local OKL_TLS_MODEL self_bounds g_self = { nullptr, 0, 0 };
217225

218226
#ifdef OKL_STANDALONE
219227

@@ -243,7 +251,7 @@ okl_uptr pages_per_chunk() {
243251
// apart from "not mapped" because the two lead to different reports: a range
244252
// derived from a refusal is a range derived from nothing, and the caller is
245253
// owed the refusal instead.
246-
thread_local okl_long g_refusal = 0;
254+
thread_local OKL_TLS_MODEL okl_long g_refusal = 0;
247255

248256
// Whether every page in the range is mapped. False with `mapped' unset means
249257
// at least one is not; false with `g_refusal' set means the environment would
@@ -620,7 +628,7 @@ kal_uintptr kal_task_current(void) {
620628
// The identity is the kernel's, read once per context. It is unique among
621629
// contexts running at the same moment and may be reused after one ends,
622630
// which is what the specification says of it.
623-
static thread_local int cached = 0;
631+
static thread_local OKL_TLS_MODEL int cached = 0;
624632
if (cached == 0) cached = static_cast<int>(okl::sys(okl::nr_gettid));
625633
return static_cast<kal_uintptr>(cached);
626634
}

‎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)