summaryrefslogtreecommitdiff
path: root/src/irmd/reg/reg.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/irmd/reg/reg.c')
-rw-r--r--src/irmd/reg/reg.c542
1 files changed, 388 insertions, 154 deletions
diff --git a/src/irmd/reg/reg.c b/src/irmd/reg/reg.c
index a24a9d1d..0025f695 100644
--- a/src/irmd/reg/reg.c
+++ b/src/irmd/reg/reg.c
@@ -1,5 +1,5 @@
/*
- * Ouroboros - Copyright (C) 2016 - 2024
+ * Ouroboros - Copyright (C) 2016 - 2026
*
The IPC Resource Manager - Registry
*
@@ -35,6 +35,7 @@ The IPC Resource Manager - Registry
#include "flow.h"
#include "ipcp.h"
#include "name.h"
+#include "pool.h"
#include "proc.h"
#include "prog.h"
@@ -46,28 +47,18 @@ The IPC Resource Manager - Registry
#define ID_OFFT 1 /* reserve some flow_ids */
struct {
- struct bmp * flow_ids; /* flow_ids for flows */
-
- struct list_head flows; /* flow information */
- size_t n_flows; /* number of flows */
-
- struct list_head ipcps; /* list of ipcps in system */
- size_t n_ipcps; /* number of ipcps */
-
- struct list_head names; /* registered names known */
- size_t n_names; /* number of names */
-
- struct list_head procs; /* processes */
- size_t n_procs; /* number of processes */
-
- struct list_head progs; /* programs known */
- size_t n_progs; /* number of programs */
-
- struct list_head spawned; /* child processes */
- size_t n_spawned; /* number of child processes */
-
- pthread_mutex_t mtx; /* registry lock */
- pthread_cond_t cond; /* condvar for reg changes */
+ struct bmp * ids; /* flow bitmap */
+
+ struct llist flows; /* list of flows */
+ struct llist ipcps; /* list of ipcps in system */
+ struct llist names; /* registered names known */
+ struct llist pools; /* per-user pools */
+ struct llist procs; /* processes known */
+ struct llist progs; /* programs known */
+ struct llist spawned; /* child processes */
+
+ pthread_mutex_t mtx; /* registry lock */
+ pthread_cond_t cond; /* condvar for reg changes */
} reg;
struct pid_entry {
@@ -81,7 +72,7 @@ static struct reg_flow * __reg_get_flow(int flow_id)
assert(flow_id >= ID_OFFT);
- list_for_each(p, &reg.flows) {
+ llist_for_each(p, &reg.flows) {
struct reg_flow * entry;
entry = list_entry(p, struct reg_flow, next);
if (entry->info.id == flow_id)
@@ -95,7 +86,7 @@ static struct reg_flow * __reg_get_accept_flow(pid_t pid)
{
struct list_head * p;
- list_for_each(p, &reg.flows) {
+ llist_for_each(p, &reg.flows) {
struct reg_flow * entry;
entry = list_entry(p, struct reg_flow, next);
if (entry->info.state != FLOW_ACCEPT_PENDING)
@@ -113,7 +104,7 @@ static struct list_head * __reg_after_flow(int flow_id)
assert(flow_id >= ID_OFFT);
- list_for_each(p, &reg.flows) {
+ llist_for_each(p, &reg.flows) {
struct reg_flow * entry;
entry = list_entry(p, struct reg_flow, next);
if (entry->info.id > flow_id)
@@ -129,7 +120,7 @@ static struct reg_ipcp * __reg_get_ipcp(pid_t pid)
assert(pid > 0);
- list_for_each(p, &reg.ipcps) {
+ llist_for_each(p, &reg.ipcps) {
struct reg_ipcp * entry;
entry = list_entry(p, struct reg_ipcp, next);
if (entry->info.pid == pid)
@@ -143,7 +134,7 @@ static struct reg_ipcp * __reg_get_ipcp_by_layer(const char * layer)
{
struct list_head * p;
- list_for_each(p, &reg.ipcps) {
+ llist_for_each(p, &reg.ipcps) {
struct reg_ipcp * entry;
entry = list_entry(p, struct reg_ipcp, next);
if (strcmp(entry->layer.name, layer) == 0)
@@ -160,7 +151,7 @@ static struct list_head * __reg_after_ipcp(const struct ipcp_info * info)
assert(info != NULL);
- list_for_each(p, &reg.ipcps) {
+ llist_for_each(p, &reg.ipcps) {
struct reg_ipcp * entry;
entry = list_entry(p, struct reg_ipcp, next);
if (entry->info.type < info->type)
@@ -182,7 +173,7 @@ static struct reg_name * __reg_get_name(const char * name)
assert(name != NULL);
- list_for_each(p, &reg.names) {
+ llist_for_each(p, &reg.names) {
struct reg_name * entry;
entry = list_entry(p, struct reg_name, next);
if (strcmp(entry->info.name, name) == 0)
@@ -225,7 +216,7 @@ static struct list_head * __reg_after_name(const char * name)
assert(name != NULL);
- list_for_each(p, &reg.names) {
+ llist_for_each(p, &reg.names) {
struct reg_name * entry;
entry = list_entry(p, struct reg_name, next);
if (strcmp(entry->info.name, name) > 0)
@@ -235,11 +226,25 @@ static struct list_head * __reg_after_name(const char * name)
return p;
}
+static struct reg_pool * __reg_get_pool(uid_t uid)
+{
+ struct list_head * p;
+
+ llist_for_each(p, &reg.pools) {
+ struct reg_pool * entry;
+ entry = list_entry(p, struct reg_pool, next);
+ if (entry->uid == uid)
+ return entry;
+ }
+
+ return NULL;
+}
+
static struct reg_proc * __reg_get_proc(pid_t pid)
{
struct list_head * p;
- list_for_each(p, &reg.procs) {
+ llist_for_each(p, &reg.procs) {
struct reg_proc * entry;
entry = list_entry(p, struct reg_proc, next);
if (entry->info.pid == pid)
@@ -253,7 +258,7 @@ static struct list_head * __reg_after_proc(pid_t pid)
{
struct list_head * p;
- list_for_each(p, &reg.procs) {
+ llist_for_each(p, &reg.procs) {
struct reg_proc * entry;
entry = list_entry(p, struct reg_proc, next);
if (entry->info.pid > pid)
@@ -267,7 +272,7 @@ static void __reg_kill_all_proc(int signal)
{
struct list_head * p;
- list_for_each(p, &reg.procs) {
+ llist_for_each(p, &reg.procs) {
struct reg_proc * entry;
entry = list_entry(p, struct reg_proc, next);
kill(entry->info.pid, signal);
@@ -278,7 +283,7 @@ static pid_t __reg_get_dead_proc(void)
{
struct list_head * p;
- list_for_each(p, &reg.procs) {
+ llist_for_each(p, &reg.procs) {
struct reg_proc * entry;
entry = list_entry(p, struct reg_proc, next);
if (kill(entry->info.pid, 0) < 0)
@@ -293,7 +298,7 @@ static void __reg_cancel_flows_for_proc(pid_t pid)
struct list_head * p;
bool changed = false;
- list_for_each(p, &reg.flows) {
+ llist_for_each(p, &reg.flows) {
struct reg_flow * entry;
entry = list_entry(p, struct reg_flow, next);
if (entry->info.n_pid != pid)
@@ -319,7 +324,7 @@ static struct pid_entry * __reg_get_spawned(pid_t pid)
{
struct list_head * p;
- list_for_each(p, &reg.spawned) {
+ llist_for_each(p, &reg.spawned) {
struct pid_entry * entry;
entry = list_entry(p, struct pid_entry, next);
if (entry->pid == pid)
@@ -333,7 +338,7 @@ static struct list_head * __reg_after_spawned(pid_t pid)
{
struct list_head * p;
- list_for_each(p, &reg.spawned) {
+ llist_for_each(p, &reg.spawned) {
struct pid_entry * entry;
entry = list_entry(p, struct pid_entry, next);
if (entry->pid > pid)
@@ -347,7 +352,7 @@ static void __reg_kill_all_spawned(int signal)
{
struct list_head * p;
- list_for_each(p, &reg.spawned) {
+ llist_for_each(p, &reg.spawned) {
struct pid_entry * entry;
entry = list_entry(p, struct pid_entry, next);
kill(entry->pid, signal);
@@ -356,17 +361,17 @@ static void __reg_kill_all_spawned(int signal)
static pid_t __reg_first_spawned(void)
{
- if (list_is_empty(&reg.spawned))
+ if (llist_is_empty(&reg.spawned))
return -1;
- return list_first_entry(&reg.spawned, struct pid_entry, next)->pid;
+ return llist_first_entry(&reg.spawned, struct pid_entry, next)->pid;
}
static struct reg_prog * __reg_get_prog(const char * name)
{
struct list_head * p;
- list_for_each(p, &reg.progs) {
+ llist_for_each(p, &reg.progs) {
struct reg_prog * entry;
entry = list_entry(p, struct reg_prog, next);
if (strcmp(entry->info.name, name) == 0)
@@ -380,7 +385,7 @@ static char ** __reg_get_exec(const char * name)
{
struct list_head * p;
- list_for_each(p, &reg.names) {
+ llist_for_each(p, &reg.names) {
struct reg_name * entry;
entry = list_entry(p, struct reg_name, next);
if (strcmp(entry->info.name, name) == 0)
@@ -394,7 +399,7 @@ static struct list_head * __reg_after_prog(const char * name)
{
struct list_head * p;
- list_for_each(p, &reg.progs) {
+ llist_for_each(p, &reg.progs) {
struct reg_prog * entry;
entry = list_entry(p, struct reg_prog, next);
if (strcmp(entry->info.name, name) > 0)
@@ -408,7 +413,7 @@ static void __reg_del_name_from_procs(const char * name)
{
struct list_head * p;
- list_for_each(p, &reg.procs) {
+ llist_for_each(p, &reg.procs) {
struct reg_proc * proc;
proc = list_entry(p, struct reg_proc, next);
reg_proc_del_name(proc, name);
@@ -419,7 +424,7 @@ static void __reg_del_name_from_progs(const char * name)
{
struct list_head * p;
- list_for_each(p, &reg.progs) {
+ llist_for_each(p, &reg.progs) {
struct reg_prog * prog;
prog = list_entry(p, struct reg_prog, next);
reg_prog_del_name(prog, name);
@@ -431,13 +436,13 @@ static void __reg_proc_update_names(struct reg_proc * proc)
struct list_head * p;
struct reg_prog * prog;
- assert(list_is_empty(&proc->names));
+ assert(llist_is_empty(&proc->names));
prog = __reg_get_prog(proc->info.prog);
if (prog == NULL)
return;
- list_for_each(p, &reg.names) {
+ llist_for_each(p, &reg.names) {
struct reg_name * name;
name = list_entry(p, struct reg_name, next);
assert(!reg_name_has_proc(name, proc->info.pid));
@@ -452,7 +457,7 @@ static void __reg_del_proc_from_names(pid_t pid)
{
struct list_head * p;
- list_for_each(p, &reg.names) {
+ llist_for_each(p, &reg.names) {
struct reg_name * name;
name = list_entry(p, struct reg_name, next);
reg_name_del_proc(name, pid);
@@ -463,7 +468,7 @@ static void __reg_del_prog_from_names(const char * prog)
{
struct list_head * p;
- list_for_each(p, &reg.names) {
+ llist_for_each(p, &reg.names) {
struct reg_name * name;
name = list_entry(p, struct reg_name, next);
reg_name_del_prog(name, prog);
@@ -478,7 +483,7 @@ static int __reg_add_active_proc(pid_t pid)
assert(pid > 0);
- list_for_each(p, &reg.names) {
+ llist_for_each(p, &reg.names) {
struct reg_name * name;
name = list_entry(p, struct reg_name, next);
if (reg_name_has_proc(name, pid)) {
@@ -500,7 +505,7 @@ static void __reg_del_active_proc(pid_t pid)
assert(pid > 0);
- list_for_each(p, &reg.names) {
+ llist_for_each(p, &reg.names) {
struct reg_name * name;
name = list_entry(p, struct reg_name, next);
reg_name_del_active(name, pid);
@@ -529,20 +534,21 @@ int reg_init(void)
goto fail_cond;
}
- reg.flow_ids = bmp_create(SYS_MAX_FLOWS -ID_OFFT, ID_OFFT);
- if (reg.flow_ids == NULL) {
+ reg.ids = bmp_create(SYS_MAX_FLOWS - ID_OFFT, ID_OFFT);
+ if (reg.ids == NULL) {
log_err("Failed to create flow_ids bitmap.");
goto fail_flow_ids;
}
pthread_condattr_destroy(&cattr);
- list_head_init(&reg.flows);
- list_head_init(&reg.ipcps);
- list_head_init(&reg.names);
- list_head_init(&reg.procs);
- list_head_init(&reg.progs);
- list_head_init(&reg.spawned);
+ llist_init(&reg.flows);
+ llist_init(&reg.ipcps);
+ llist_init(&reg.names);
+ llist_init(&reg.pools);
+ llist_init(&reg.procs);
+ llist_init(&reg.progs);
+ llist_init(&reg.spawned);
return 0;
@@ -563,54 +569,56 @@ void reg_clear(void)
pthread_mutex_lock(&reg.mtx);
- list_for_each_safe(p, h, &reg.spawned) {
+ llist_for_each_safe(p, h, &reg.spawned) {
struct pid_entry * entry;
entry = list_entry(p, struct pid_entry, next);
- list_del(&entry->next);
+ llist_del(&entry->next, &reg.spawned);
free(entry);
- reg.n_spawned--;
}
- list_for_each_safe(p, h, &reg.progs) {
+ llist_for_each_safe(p, h, &reg.progs) {
struct reg_prog * entry;
entry = list_entry(p, struct reg_prog, next);
- list_del(&entry->next);
+ llist_del(&entry->next, &reg.progs);
__reg_del_prog_from_names(entry->info.path);
reg_prog_destroy(entry);
- reg.n_progs--;
}
- list_for_each_safe(p, h, &reg.procs) {
+ llist_for_each_safe(p, h, &reg.procs) {
struct reg_proc * entry;
entry = list_entry(p, struct reg_proc, next);
- list_del(&entry->next);
+ llist_del(&entry->next, &reg.procs);
__reg_del_proc_from_names(entry->info.pid);
reg_proc_destroy(entry);
- reg.n_procs--;
}
- list_for_each_safe(p, h, &reg.flows) {
+ llist_for_each_safe(p, h, &reg.pools) {
+ struct reg_pool * entry;
+ entry = list_entry(p, struct reg_pool, next);
+ llist_del(&entry->next, &reg.pools);
+ entry->refcount = 0; /* Force destroy during cleanup */
+ reg_pool_destroy(entry);
+ }
+
+ llist_for_each_safe(p, h, &reg.flows) {
struct reg_flow * entry;
entry = list_entry(p, struct reg_flow, next);
- list_del(&entry->next);
+ llist_del(&entry->next, &reg.flows);
reg_flow_destroy(entry);
- reg.n_flows--;
}
- list_for_each_safe(p, h, &reg.names) {
+ llist_for_each_safe(p, h, &reg.names) {
struct reg_name * entry;
entry = list_entry(p, struct reg_name, next);
- list_del(&entry->next);
+ llist_del(&entry->next, &reg.names);
reg_name_destroy(entry);
- reg.n_names--;
}
- list_for_each_safe(p, h, &reg.ipcps) {
+ llist_for_each_safe(p, h, &reg.ipcps) {
struct reg_ipcp * entry;
entry = list_entry(p, struct reg_ipcp, next);
- list_del(&entry->next);
+ llist_del(&entry->next, &reg.ipcps);
reg_ipcp_destroy(entry);
- reg.n_ipcps--;
}
pthread_mutex_unlock(&reg.mtx);
@@ -618,21 +626,15 @@ void reg_clear(void)
void reg_fini(void)
{
- assert(list_is_empty(&reg.spawned));
- assert(list_is_empty(&reg.progs));
- assert(list_is_empty(&reg.procs));
- assert(list_is_empty(&reg.names));
- assert(list_is_empty(&reg.ipcps));
- assert(list_is_empty(&reg.flows));
-
- assert(reg.n_spawned == 0);
- assert(reg.n_progs == 0);
- assert(reg.n_procs == 0);
- assert(reg.n_names == 0);
- assert(reg.n_ipcps == 0);
- assert(reg.n_flows == 0);
+ assert(llist_is_empty(&reg.spawned));
+ assert(llist_is_empty(&reg.progs));
+ assert(llist_is_empty(&reg.procs));
+ assert(llist_is_empty(&reg.pools));
+ assert(llist_is_empty(&reg.names));
+ assert(llist_is_empty(&reg.ipcps));
+ assert(llist_is_empty(&reg.flows));
- bmp_destroy(reg.flow_ids);
+ bmp_destroy(reg.ids);
if (pthread_cond_destroy(&reg.cond) != 0)
log_warn("Failed to destroy condvar.");
@@ -652,8 +654,8 @@ int reg_create_flow(struct flow_info * info)
pthread_mutex_lock(&reg.mtx);
- info->id = bmp_allocate(reg.flow_ids);
- if (!bmp_is_id_valid(reg.flow_ids, info->id)) {
+ info->id = bmp_allocate(reg.ids);
+ if (!bmp_is_id_valid(reg.ids, info->id)) {
log_err("Failed to allocate flow id.");
goto fail_id;
}
@@ -664,16 +666,14 @@ int reg_create_flow(struct flow_info * info)
goto fail_flow;
}
- list_add(&f->next, __reg_after_flow(info->id));
-
- reg.n_flows++;
+ llist_add_at(&f->next, __reg_after_flow(info->id), &reg.flows);
pthread_mutex_unlock(&reg.mtx);
return 0;
fail_flow:
- bmp_release(reg.flow_ids, info->id);
+ bmp_release(reg.ids, info->id);
info->id = 0;
fail_id:
pthread_mutex_unlock(&reg.mtx);
@@ -692,11 +692,9 @@ int reg_destroy_flow(int flow_id)
goto no_flow;
}
- list_del(&f->next);
-
- reg.n_flows--;
+ llist_del(&f->next, &reg.flows);
- bmp_release(reg.flow_ids, flow_id);
+ bmp_release(reg.ids, flow_id);
pthread_mutex_unlock(&reg.mtx);
@@ -755,11 +753,10 @@ int reg_create_ipcp(const struct ipcp_info * info)
entry->pid = info->pid;
- list_add_tail(&ipcp->next, __reg_after_ipcp(info));
- list_add(&entry->next, __reg_after_spawned(info->pid));
-
- reg.n_ipcps++;
- reg.n_spawned++;
+ llist_add_tail_at(&ipcp->next, __reg_after_ipcp(info), &reg.ipcps);
+ llist_add_at(&entry->next,
+ __reg_after_spawned(info->pid),
+ &reg.spawned);
pthread_mutex_unlock(&reg.mtx);
@@ -849,16 +846,16 @@ int reg_list_ipcps(ipcp_list_msg_t *** ipcps)
pthread_mutex_lock(&reg.mtx);
- if (reg.n_ipcps == 0)
+ if (llist_is_empty(&reg.ipcps))
goto finish;
- *ipcps = malloc(reg.n_ipcps * sizeof(**ipcps));
+ *ipcps = malloc(reg.ipcps.len * sizeof(**ipcps));
if (*ipcps == NULL) {
log_err("Failed to malloc ipcps.");
goto fail_malloc;
}
- list_for_each(p, &reg.ipcps) {
+ llist_for_each(p, &reg.ipcps) {
struct reg_ipcp * entry;
entry = list_entry(p, struct reg_ipcp, next);
if (__get_ipcp_info(&(*ipcps)[i], entry) < 0)
@@ -900,9 +897,7 @@ int reg_create_name(const struct name_info * info)
goto fail_name;
}
- list_add(&n->next, __reg_after_name(info->name));
-
- reg.n_names++;
+ llist_add_at(&n->next, __reg_after_name(info->name), &reg.names);
pthread_mutex_unlock(&reg.mtx);
return 0;
@@ -931,9 +926,7 @@ int reg_destroy_name(const char * name)
__reg_del_name_from_procs(name);
__reg_del_name_from_progs(name);
- list_del(&n->next);
-
- reg.n_names--;
+ llist_del(&n->next, &reg.names);
pthread_mutex_unlock(&reg.mtx);
@@ -1004,7 +997,7 @@ int reg_get_name_for_hash(char * buf,
pthread_mutex_lock(&reg.mtx);
- list_for_each(p, &reg.names) {
+ llist_for_each(p, &reg.names) {
struct reg_name * n = list_entry(p, struct reg_name, next);
str_hash(algo, thash, n->info.name);
if (memcmp(thash, hash, len) == 0) {
@@ -1046,16 +1039,16 @@ int reg_list_names(name_info_msg_t *** names)
pthread_mutex_lock(&reg.mtx);
- if (reg.n_names == 0)
+ if (llist_is_empty(&reg.names))
goto finish;
- *names = malloc(reg.n_names * sizeof(**names));
+ *names = malloc(reg.names.len * sizeof(**names));
if (*names == NULL) {
log_err("Failed to malloc names.");
goto fail_malloc;
}
- list_for_each(p, &reg.names) {
+ llist_for_each(p, &reg.names) {
struct reg_name * entry;
entry = list_entry(p, struct reg_name, next);
(*names)[i] = name_info_s_to_msg(&entry->info);
@@ -1090,6 +1083,34 @@ int reg_list_names(name_info_msg_t *** names)
return -ENOMEM;
}
+int reg_prepare_pool(uid_t uid,
+ gid_t gid)
+{
+ struct reg_pool * pool;
+
+ if (is_ouroboros_member_uid(uid))
+ return 0;
+
+ pthread_mutex_lock(&reg.mtx);
+
+ pool = __reg_get_pool(uid);
+ if (pool == NULL) {
+ pool = reg_pool_create(uid, gid);
+ if (pool == NULL) {
+ log_err("Failed to create pool for uid %d.", uid);
+ pthread_mutex_unlock(&reg.mtx);
+ return -1;
+ }
+ llist_add(&pool->next, &reg.pools);
+ }
+
+ reg_pool_ref(pool);
+
+ pthread_mutex_unlock(&reg.mtx);
+
+ return 0;
+}
+
int reg_create_proc(const struct proc_info * info)
{
struct reg_proc * proc;
@@ -1100,20 +1121,18 @@ int reg_create_proc(const struct proc_info * info)
if (__reg_get_proc(info->pid) != NULL) {
log_err("Process %d already exists.", info->pid);
- goto fail_proc;
+ goto fail;
}
proc = reg_proc_create(info);
if (proc == NULL) {
log_err("Failed to create process %d.", info->pid);
- goto fail_proc;
+ goto fail;
}
__reg_proc_update_names(proc);
- list_add(&proc->next, __reg_after_proc(info->pid));
-
- reg.n_procs++;
+ llist_add_at(&proc->next, __reg_after_proc(info->pid), &reg.procs);
pthread_cond_broadcast(&reg.cond);
@@ -1121,7 +1140,7 @@ int reg_create_proc(const struct proc_info * info)
return 0;
- fail_proc:
+ fail:
pthread_mutex_unlock(&reg.mtx);
return -1;
}
@@ -1129,6 +1148,7 @@ int reg_create_proc(const struct proc_info * info)
int reg_destroy_proc(pid_t pid)
{
struct reg_proc * proc;
+ struct reg_pool * pool = NULL;
struct pid_entry * spawn;
struct reg_ipcp * ipcp;
@@ -1136,24 +1156,27 @@ int reg_destroy_proc(pid_t pid)
proc = __reg_get_proc(pid);
if (proc != NULL) {
- list_del(&proc->next);
- reg.n_procs--;
+ if (!is_ouroboros_member_uid(proc->info.uid))
+ pool = __reg_get_pool(proc->info.uid);
+ llist_del(&proc->next, &reg.procs);
reg_proc_destroy(proc);
__reg_del_proc_from_names(pid);
__reg_cancel_flows_for_proc(pid);
+ if (pool != NULL && reg_pool_unref(pool) == 0) {
+ llist_del(&pool->next, &reg.pools);
+ reg_pool_destroy(pool);
+ }
}
spawn = __reg_get_spawned(pid);
if (spawn != NULL) {
- list_del(&spawn->next);
- reg.n_spawned--;
+ llist_del(&spawn->next, &reg.spawned);
free(spawn);
}
ipcp = __reg_get_ipcp(pid);
if (ipcp != NULL) {
- list_del(&ipcp->next);
- reg.n_ipcps--;
+ llist_del(&ipcp->next, &reg.ipcps);
reg_ipcp_destroy(ipcp);
}
@@ -1175,6 +1198,38 @@ bool reg_has_proc(pid_t pid)
return ret;
}
+bool reg_is_proc_privileged(pid_t pid)
+{
+ struct reg_proc * proc;
+ bool ret = false;
+
+ pthread_mutex_lock(&reg.mtx);
+
+ proc = __reg_get_proc(pid);
+ if (proc != NULL)
+ ret = reg_proc_is_privileged(proc);
+
+ pthread_mutex_unlock(&reg.mtx);
+
+ return ret;
+}
+
+uid_t reg_get_proc_uid(pid_t pid)
+{
+ struct reg_proc * proc;
+ uid_t ret = 0;
+
+ pthread_mutex_lock(&reg.mtx);
+
+ proc = __reg_get_proc(pid);
+ if (proc != NULL && !is_ouroboros_member_uid(proc->info.uid))
+ ret = proc->info.uid;
+
+ pthread_mutex_unlock(&reg.mtx);
+
+ return ret;
+}
+
void reg_kill_all_proc(int signal)
{
pthread_mutex_lock(&reg.mtx);
@@ -1216,9 +1271,7 @@ int reg_create_spawned(pid_t pid)
entry->pid = pid;
- list_add(&entry->next, __reg_after_spawned(pid));
-
- reg.n_spawned++;
+ llist_add_at(&entry->next, __reg_after_spawned(pid), &reg.spawned);
pthread_mutex_unlock(&reg.mtx);
@@ -1388,9 +1441,7 @@ int reg_create_prog(const struct prog_info * info)
goto fail_prog;
}
- list_add(&prog->next, __reg_after_prog(info->name));
-
- reg.n_progs++;
+ llist_add_at(&prog->next, __reg_after_prog(info->name), &reg.progs);
exists:
pthread_mutex_unlock(&reg.mtx);
@@ -1418,9 +1469,7 @@ int reg_destroy_prog(const char * name)
__reg_del_prog_from_names(prog->info.path);
- list_del(&prog->next);
-
- reg.n_progs--;
+ llist_del(&prog->next, &reg.progs);
pthread_mutex_unlock(&reg.mtx);
@@ -1736,7 +1785,8 @@ int reg_wait_flow_allocated(struct flow_info * info,
}
if (flow != NULL) {
- reg_flow_get_data(flow, pbuf);
+ *pbuf = flow->rsp_data;
+ clrbuf(flow->rsp_data);
*info = flow->info;
}
@@ -1771,8 +1821,8 @@ int reg_respond_alloc(struct flow_info * info,
}
assert(flow->info.state == FLOW_ALLOC_PENDING);
- assert(flow->data.len == 0);
- assert(flow->data.data == NULL);
+ assert(flow->rsp_data.len == 0);
+ assert(flow->rsp_data.data == NULL);
info->n_pid = flow->info.n_pid;
info->n_1_pid = flow->info.n_pid;
@@ -1784,8 +1834,10 @@ int reg_respond_alloc(struct flow_info * info,
flow->response = response;
- if (info->state == FLOW_ALLOCATED)
- reg_flow_set_data(flow, pbuf);
+ if (info->state == FLOW_ALLOCATED) {
+ flow->rsp_data = *pbuf;
+ clrbuf(*pbuf);
+ }
pthread_cond_broadcast(&reg.cond);
@@ -1816,6 +1868,8 @@ int reg_prepare_flow_accept(struct flow_info * info)
ret = reg_flow_update(flow, info);
+ pthread_cond_broadcast(&reg.cond);
+
pthread_mutex_unlock(&reg.mtx);
return ret;
@@ -1893,7 +1947,8 @@ int reg_wait_flow_accepted(struct flow_info * info,
pthread_cleanup_pop(true); /* __cleanup_wait_accept */
if (flow != NULL) {
- reg_flow_get_data(flow, pbuf);
+ *pbuf = flow->req_data;
+ clrbuf(flow->req_data);
*info = flow->info;
}
@@ -1953,8 +2008,63 @@ int reg_respond_accept(struct flow_info * info,
info->n_pid = flow->info.n_pid;
- reg_flow_set_data(flow, pbuf);
- clrbuf(pbuf);
+ flow->req_data = *pbuf;
+ clrbuf(*pbuf);
+
+ if (reg_flow_update(flow, info) < 0) {
+ log_err("Failed to create flow structs.");
+ goto fail_flow;
+ }
+
+ pthread_cond_broadcast(&reg.cond);
+
+ pthread_mutex_unlock(&reg.mtx);
+
+ return 0;
+
+ fail_flow:
+ pthread_mutex_unlock(&reg.mtx);
+ return -1;
+}
+
+int reg_prepare_flow_direct(struct flow_info * info,
+ buffer_t * pbuf,
+ uid_t alloc_uid)
+{
+ struct reg_flow * flow;
+ struct reg_proc * proc;
+ uid_t accept_uid = 0;
+
+ assert(info != NULL);
+ assert(info->state == FLOW_ALLOCATED);
+ assert(info->n_1_pid != 0);
+ assert(pbuf != NULL);
+
+ pthread_mutex_lock(&reg.mtx);
+
+ flow = __reg_get_flow(info->id);
+ if (flow == NULL) {
+ log_err("Flow not found: %d.", info->id);
+ goto fail_flow;
+ }
+
+ assert(flow->info.state == FLOW_ACCEPT_PENDING);
+
+ info->n_pid = flow->info.n_pid;
+
+ proc = __reg_get_proc(info->n_pid);
+ if (proc != NULL && !is_ouroboros_member_uid(proc->info.uid))
+ accept_uid = proc->info.uid;
+
+ if (alloc_uid != accept_uid) {
+ pthread_mutex_unlock(&reg.mtx);
+ return -EPERM;
+ }
+
+ flow->direct = true;
+
+ flow->req_data = *pbuf;
+ clrbuf(*pbuf);
if (reg_flow_update(flow, info) < 0) {
log_err("Failed to create flow structs.");
@@ -1972,6 +2082,109 @@ int reg_respond_accept(struct flow_info * info,
return -1;
}
+bool reg_flow_is_direct(int flow_id)
+{
+ struct reg_flow * flow;
+ bool ret;
+
+ pthread_mutex_lock(&reg.mtx);
+
+ flow = __reg_get_flow(flow_id);
+
+ ret = flow != NULL && flow->direct;
+
+ pthread_mutex_unlock(&reg.mtx);
+
+ return ret;
+}
+
+int reg_respond_flow_direct(int flow_id,
+ buffer_t * pbuf)
+{
+ struct reg_flow * flow;
+
+ assert(pbuf != NULL);
+
+ pthread_mutex_lock(&reg.mtx);
+
+ flow = __reg_get_flow(flow_id);
+ if (flow == NULL) {
+ log_err("Flow %d not found.", flow_id);
+ goto fail;
+ }
+
+ assert(flow->direct);
+ assert(flow->rsp_data.data == NULL);
+
+ flow->rsp_data = *pbuf;
+ clrbuf(*pbuf);
+
+ pthread_cond_broadcast(&reg.cond);
+
+ pthread_mutex_unlock(&reg.mtx);
+
+ return 0;
+ fail:
+ pthread_mutex_unlock(&reg.mtx);
+ return -1;
+}
+
+int reg_wait_flow_direct(int flow_id,
+ buffer_t * pbuf,
+ const struct timespec * abstime)
+{
+ struct reg_flow * flow;
+ int ret = -1;
+
+ assert(pbuf != NULL);
+
+ pthread_mutex_lock(&reg.mtx);
+
+ flow = __reg_get_flow(flow_id);
+ if (flow == NULL)
+ goto fail;
+
+ assert(flow->direct);
+
+ pthread_cleanup_push(__cleanup_mutex_unlock, &reg.mtx);
+
+ while (flow != NULL && flow->rsp_data.data == NULL) {
+ ret = -__timedwait(&reg.cond, &reg.mtx, abstime);
+ if (ret == -ETIMEDOUT)
+ break;
+ flow = __reg_get_flow(flow_id);
+ }
+
+ if (flow != NULL && flow->rsp_data.data != NULL) {
+ *pbuf = flow->rsp_data;
+ clrbuf(flow->rsp_data);
+ ret = 0;
+ }
+
+ pthread_cleanup_pop(true);
+
+ return ret;
+ fail:
+ pthread_mutex_unlock(&reg.mtx);
+ return -1;
+}
+
+static int direct_flow_dealloc(struct reg_flow * flow,
+ pid_t pid)
+{
+ if (!flow->direct)
+ return -1;
+
+ if (pid == flow->info.n_pid && flow->info.n_pid != -1)
+ flow->info.n_pid = -1;
+ else if (pid == flow->info.n_1_pid && flow->info.n_1_pid != -1)
+ flow->info.n_1_pid = -1;
+ else
+ return -1;
+
+ return 0;
+}
+
void reg_dealloc_flow(struct flow_info * info)
{
struct reg_flow * flow;
@@ -1985,13 +2198,32 @@ void reg_dealloc_flow(struct flow_info * info)
flow = __reg_get_flow(info->id);
assert(flow != NULL);
- assert(flow->data.data == NULL);
- assert(flow->data.len == 0);
+ assert(flow->req_data.data == NULL);
+ assert(flow->req_data.len == 0);
+ assert(flow->rsp_data.data == NULL);
+ assert(flow->rsp_data.len == 0);
+
+ info->n_1_pid = flow->info.n_1_pid;
+
+ if (flow->info.state == FLOW_DEALLOC_PENDING) {
+ if (direct_flow_dealloc(flow, info->n_pid) < 0) {
+ info->state = FLOW_DEALLOC_PENDING;
+ pthread_mutex_unlock(&reg.mtx);
+ return;
+ }
+ flow->info.state = FLOW_DEALLOCATED;
+ info->state = FLOW_DEALLOCATED;
+ reg_flow_update(flow, info);
+ pthread_mutex_unlock(&reg.mtx);
+ return;
+ }
+
assert(flow->info.state == FLOW_ALLOCATED);
flow->info.state = FLOW_DEALLOC_PENDING;
info->state = FLOW_DEALLOC_PENDING;
- info->n_1_pid = flow->info.n_1_pid;
+
+ direct_flow_dealloc(flow, info->n_pid);
memset(flow->name, 0, sizeof(flow->name));
@@ -2013,8 +2245,10 @@ void reg_dealloc_flow_resp(struct flow_info * info)
flow = __reg_get_flow(info->id);
assert(flow != NULL);
- assert(flow->data.data == NULL);
- assert(flow->data.len == 0);
+ assert(flow->req_data.data == NULL);
+ assert(flow->req_data.len == 0);
+ assert(flow->rsp_data.data == NULL);
+ assert(flow->rsp_data.len == 0);
assert(flow->info.state == FLOW_DEALLOC_PENDING);
flow->info.state = FLOW_DEALLOCATED;