Skip to content

Commit 5d22174

Browse files
committed
fix: retire units against a verified live continuation
1 parent f2eeefa commit 5d22174

1 file changed

Lines changed: 20 additions & 15 deletions

File tree

‎src/process.cpp‎

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,7 +1959,7 @@ int kal_process_job_limit(kal_job j, kal_uintptr kind, kal_u64 value) {
19591959
#include "unit_reference.h"
19601960
namespace {
19611961
struct unit_identity {
1962-
kal_u64 group = 0, birth = 0, previous = 0, previous_birth = 0;
1962+
kal_u64 group = 0, birth = 0;
19631963
char boot[64]{}, space[64]{};
19641964
};
19651965
bool unit_birth(okl_long pid, kal_u64& value) { okl_u64 ticks = 0; if (!started_ticks(pid,ticks)) return false; value = ticks; return true; }
@@ -1981,11 +1981,9 @@ bool unit_origin(char* boot, char* space) {
19811981
bool unit_parse(const char* p, kal_uintptr n, unit_identity& u) {
19821982
if (!p || n == 0 || n > 256) return false;
19831983
unit_reference::reader r{p,n}; char kind[8]{};
1984-
return r.token(kind, sizeof kind) && unit_reference::same(kind,"L1")
1984+
return r.token(kind, sizeof kind) && unit_reference::same(kind,"L2")
19851985
&& r.number(u.group) && u.group > 0 && u.group <= 0x7fffffffu
19861986
&& 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
19891987
&& r.token(u.boot,sizeof u.boot) && r.token(u.space,sizeof u.space) && r.at == n;
19901988
}
19911989
bool unit_same_origin(const unit_identity& u) {
@@ -1999,12 +1997,9 @@ int kal_process_job_create(kal_job* out, char* reference, kal_uintptr capacity,
19991997
if (capacity < 256) return kal_err_no_space;
20001998
unit_identity u;
20011999
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);
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);
20082003
if (!w.valid) return kal_err_no_space;
20092004
kal_job job{}; const int rc = kal_process_job_enter(&job);
20102005
if (rc != kal_ok) return rc;
@@ -2027,18 +2022,28 @@ int kal_process_job_observe(kal_job job, const char* reference, kal_uintptr leng
20272022
if (started_ticks(static_cast<okl_long>(u.group),birth) && birth != u.birth) return kal_ok;
20282023
*state = KAL_JOB_OCCUPIED; return kal_ok;
20292024
}
2030-
int kal_process_job_retire(kal_job job, const char* reference, kal_uintptr length, kal_u32* state) {
2025+
int kal_process_job_retire(kal_job job, const char* reference, kal_uintptr length,
2026+
const kal_program_identity* continuation, kal_u32* state) {
20312027
if (!state) return kal_err_invalid; *state = KAL_JOB_UNKNOWN;
20322028
unit_identity u; if (!unit_parse(reference,length,u) || job.h != u.group) return kal_err_invalid;
20332029
okl_u64 birth = 0;
20342030
if (static_cast<kal_u64>(okl::sys(okl::nr_getpid)) != u.group || !unit_same_origin(u)
20352031
|| !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;
20362039
const auto current = okl::sys(okl::nr_getpgid,0);
20372040
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;
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;
20422047
return kal_process_job_observe(job,reference,length,state);
20432048
}
20442049
}

0 commit comments

Comments
 (0)