@@ -1607,7 +1607,7 @@ int kal_process_end_as_requested(void) {
16071607}
16081608
16091609kal_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+ }
0 commit comments