diff options
Diffstat (limited to 'src/irmd/reg/reg.c')
-rw-r--r-- | src/irmd/reg/reg.c | 289 |
1 files changed, 155 insertions, 134 deletions
diff --git a/src/irmd/reg/reg.c b/src/irmd/reg/reg.c index d95a4722..a24a9d1d 100644 --- a/src/irmd/reg/reg.c +++ b/src/irmd/reg/reg.c @@ -28,6 +28,7 @@ The IPC Resource Manager - Registry #include <ouroboros/errno.h> #include <ouroboros/list.h> #include <ouroboros/logs.h> +#include <ouroboros/protobuf.h> #include <ouroboros/pthread.h> #include "reg.h" @@ -46,6 +47,7 @@ The IPC Resource Manager - Registry struct { struct bmp * flow_ids; /* flow_ids for flows */ + struct list_head flows; /* flow information */ size_t n_flows; /* number of flows */ @@ -151,16 +153,23 @@ static struct reg_ipcp * __reg_get_ipcp_by_layer(const char * layer) return NULL; } -static struct list_head * __reg_after_ipcp(pid_t pid) + +static struct list_head * __reg_after_ipcp(const struct ipcp_info * info) { struct list_head * p; - assert(pid > 0); + assert(info != NULL); list_for_each(p, ®.ipcps) { struct reg_ipcp * entry; entry = list_entry(p, struct reg_ipcp, next); - if (entry->info.pid > pid) + if (entry->info.type < info->type) + continue; + + if (entry->info.type > info->type) + break; + + if (entry->info.pid > info->pid) break; } @@ -183,41 +192,17 @@ static struct reg_name * __reg_get_name(const char * name) return NULL; } -static struct reg_name * __reg_get_name_by_hash(enum hash_algo algo, - const uint8_t * hash) -{ - struct list_head * p; - uint8_t * thash; - size_t len; - - len = hash_len(algo); - - thash = malloc(len); - if (thash == NULL) - return NULL; - - list_for_each(p, ®.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) { - free(thash); - return n; - } - } - - free(thash); - - return NULL; -} - -static int __reg_get_pending_flow_id_for_hash(enum hash_algo algo, - const uint8_t * hash) +static int __reg_get_pending_flow_id(const char * name) { struct reg_name * entry; struct reg_flow * flow; pid_t pid; - entry =__reg_get_name_by_hash(algo, hash); + assert(name != NULL); + assert(strlen(name) > 0); + assert(strlen(name) < NAME_SIZE + 1); + + entry =__reg_get_name(name); if (entry == NULL) return -ENAME; @@ -226,7 +211,10 @@ static int __reg_get_pending_flow_id_for_hash(enum hash_algo algo, return -EAGAIN; flow = __reg_get_accept_flow(pid); - assert(flow != NULL); + if (flow == NULL) /* compiler barks, this can't be NULL */ + return -EAGAIN; + + strcpy(flow->name, name); return flow->info.id; } @@ -388,30 +376,17 @@ static struct reg_prog * __reg_get_prog(const char * name) return NULL; } -static char ** __reg_get_exec(enum hash_algo algo, - const uint8_t * hash) +static char ** __reg_get_exec(const char * name) { struct list_head * p; - uint8_t * buf; - - buf = malloc(hash_len(algo)); - if (buf == NULL) { - log_err("Failed to malloc hash buffer."); - return NULL; - } list_for_each(p, ®.names) { struct reg_name * entry; entry = list_entry(p, struct reg_name, next); - str_hash(algo, buf, entry->info.name); - if (memcmp(buf, hash, hash_len(algo)) == 0) { - free(buf); + if (strcmp(entry->info.name, name) == 0) return reg_name_get_exec(entry); - } } - free(buf); - return NULL; } @@ -614,6 +589,14 @@ void reg_clear(void) reg.n_procs--; } + list_for_each_safe(p, h, ®.flows) { + struct reg_flow * entry; + entry = list_entry(p, struct reg_flow, next); + list_del(&entry->next); + reg_flow_destroy(entry); + reg.n_flows--; + } + list_for_each_safe(p, h, ®.names) { struct reg_name * entry; entry = list_entry(p, struct reg_name, next); @@ -630,14 +613,6 @@ void reg_clear(void) reg.n_ipcps--; } - list_for_each_safe(p, h, ®.flows) { - struct reg_flow * entry; - entry = list_entry(p, struct reg_flow, next); - list_del(&entry->next); - reg_flow_destroy(entry); - reg.n_flows--; - } - pthread_mutex_unlock(®.mtx); } @@ -757,7 +732,7 @@ int reg_create_ipcp(const struct ipcp_info * info) assert(info != NULL); assert(info->pid != 0); - assert(info->state == IPCP_BOOT); + assert(info->state == IPCP_INIT); pthread_mutex_lock(®.mtx); @@ -780,7 +755,7 @@ int reg_create_ipcp(const struct ipcp_info * info) entry->pid = info->pid; - list_add(&ipcp->next, __reg_after_ipcp(info->pid)); + list_add_tail(&ipcp->next, __reg_after_ipcp(info)); list_add(&entry->next, __reg_after_spawned(info->pid)); reg.n_ipcps++; @@ -848,11 +823,11 @@ static int __get_ipcp_info(ipcp_list_msg_t ** msg, (*msg)->name = strdup(ipcp->info.name); if ((*msg)->name == NULL) - goto fail_name; + goto fail_msg; (*msg)->layer = strdup(ipcp->layer.name); if ((*msg)->layer == NULL) - goto fail_layer; + goto fail_msg; (*msg)->pid = ipcp->info.pid; (*msg)->type = ipcp->info.type; @@ -860,10 +835,8 @@ static int __get_ipcp_info(ipcp_list_msg_t ** msg, return 0; - fail_layer: - free((*msg)->name); - fail_name: - free(*msg); + fail_msg: + ipcp_list_msg__free_unpacked(*msg, NULL); *msg = NULL; fail: return -1; @@ -876,10 +849,8 @@ int reg_list_ipcps(ipcp_list_msg_t *** ipcps) pthread_mutex_lock(®.mtx); - if (reg.n_ipcps == 0) { - *ipcps = NULL; + if (reg.n_ipcps == 0) goto finish; - } *ipcps = malloc(reg.n_ipcps * sizeof(**ipcps)); if (*ipcps == NULL) { @@ -890,24 +861,19 @@ int reg_list_ipcps(ipcp_list_msg_t *** ipcps) list_for_each(p, ®.ipcps) { struct reg_ipcp * entry; entry = list_entry(p, struct reg_ipcp, next); - if (__get_ipcp_info(&((*ipcps)[i]), entry) < 0) { - log_err("Failed to create ipcp list info."); + if (__get_ipcp_info(&(*ipcps)[i], entry) < 0) goto fail; - } - ++i; + i++; } - - assert(i == (int) reg.n_ipcps); finish: pthread_mutex_unlock(®.mtx); return i; fail: - while (i > 0) - ipcp_list_msg__free_unpacked((*ipcps)[--i], NULL); - + while (i-- > 0) + ipcp_list_msg__free_unpacked((*ipcps)[i], NULL); free(*ipcps); fail_malloc: pthread_mutex_unlock(®.mtx); @@ -993,28 +959,84 @@ bool reg_has_name(const char * name) return ret; } -static int __get_name_info(name_info_msg_t ** msg, - struct reg_name * n) +int reg_get_name_info(const char * name, + struct name_info * info) { - *msg = malloc(sizeof(**msg)); - if (*msg == NULL) - goto fail; + struct reg_name * n; - name_info_msg__init(*msg); + assert(name != NULL); + assert(info != NULL); - (*msg)->name = strdup(n->info.name); - if ((*msg)->name == NULL) - goto fail_name; + pthread_mutex_lock(®.mtx); + + n = __reg_get_name(name); + if (n == NULL) { + log_err("Name %s does not exist.", name); + goto no_name; + } - (*msg)->pol_lb = n->info.pol_lb; + *info = n->info; + + pthread_mutex_unlock(®.mtx); return 0; - fail_name: - free(*msg); - *msg = NULL; - fail: - return -1; + no_name: + pthread_mutex_unlock(®.mtx); + return -ENOENT; + +} + +int reg_get_name_for_hash(char * buf, + enum hash_algo algo, + const uint8_t * hash) +{ + struct list_head * p; + uint8_t * thash; + size_t len; + char * name = NULL; + + len = hash_len(algo); + + thash = malloc(len); + if (thash == NULL) + return -ENOMEM; + + pthread_mutex_lock(®.mtx); + + list_for_each(p, ®.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) { + name = n->info.name; + break; + } + } + + if (name != NULL) + strcpy(buf, name); + + pthread_mutex_unlock(®.mtx); + + free(thash); + + return name == NULL ? -ENOENT : 0; +} + +int reg_get_name_for_flow_id(char * buf, + int flow_id) +{ + struct reg_flow * f; + + pthread_mutex_lock(®.mtx); + + f = __reg_get_flow(flow_id); + if (f != NULL) + strcpy(buf, f->name); + + pthread_mutex_unlock(®.mtx); + + return f == NULL ? -ENOENT : 0; } int reg_list_names(name_info_msg_t *** names) @@ -1036,24 +1058,31 @@ int reg_list_names(name_info_msg_t *** names) list_for_each(p, ®.names) { struct reg_name * entry; entry = list_entry(p, struct reg_name, next); - if (__get_name_info(&((*names)[i]), entry) < 0) { + (*names)[i] = name_info_s_to_msg(&entry->info); + if ((*names)[i] == NULL) { log_err("Failed to create name list info."); goto fail; } - - ++i; + /* wipe security info to avoid huge messages */ + free((*names)[i]->scrt); + (*names)[i]->scrt = NULL; + free((*names)[i]->skey); + (*names)[i]->skey = NULL; + free((*names)[i]->ccrt); + (*names)[i]->ccrt = NULL; + free((*names)[i]->ckey); + (*names)[i]->ckey = NULL; + + i++; } - - assert(i == (int) reg.n_names); finish: pthread_mutex_unlock(®.mtx); return i; fail: - while (i > 0) - name_info_msg__free_unpacked((*names)[--i], NULL); - + while (i-- > 0) + name_info_msg__free_unpacked((*names)[i], NULL); free(*names); fail_malloc: pthread_mutex_unlock(®.mtx); @@ -1419,19 +1448,18 @@ bool reg_has_prog(const char * name) return ret; } -int reg_get_exec(enum hash_algo algo, - const uint8_t * hash, - char *** prog) +int reg_get_exec(const char * name, + char *** prog) { char ** exec; int ret = 0; - assert(hash != NULL); + assert(name != NULL); assert(prog != NULL); pthread_mutex_lock(®.mtx); - exec = __reg_get_exec(algo, hash); + exec = __reg_get_exec(name); if (exec == NULL) { ret = -EPERM; goto finish; @@ -1444,12 +1472,9 @@ int reg_get_exec(enum hash_algo algo, goto finish; } - pthread_mutex_unlock(®.mtx); - - return 0; - finish: pthread_mutex_unlock(®.mtx); + return ret; } @@ -1557,8 +1582,7 @@ int reg_set_layer_for_ipcp(struct ipcp_info * info, struct reg_ipcp * ipcp; assert(info != NULL); - assert(info->state > IPCP_BOOT); - assert(info->state < IPCP_SHUTDOWN); + assert(info->state == IPCP_BOOT); pthread_mutex_lock(®.mtx); @@ -1690,7 +1714,7 @@ int reg_wait_flow_allocated(struct flow_info * info, stop = true; break; case FLOW_DEALLOCATED: - ret = -1; + ret = flow->response; stop = true; break; default: @@ -1722,7 +1746,8 @@ int reg_wait_flow_allocated(struct flow_info * info, } int reg_respond_alloc(struct flow_info * info, - buffer_t * pbuf) + buffer_t * pbuf, + int response) { struct reg_flow * flow; @@ -1755,7 +1780,9 @@ int reg_respond_alloc(struct flow_info * info, if (reg_flow_update(flow, info) < 0) { log_err("Failed to create flow structs."); goto fail_flow; - }; + } + + flow->response = response; if (info->state == FLOW_ALLOCATED) reg_flow_set_data(flow, pbuf); @@ -1771,8 +1798,7 @@ int reg_respond_alloc(struct flow_info * info, return -1; } -int reg_prepare_flow_accept(struct flow_info * info, - buffer_t * pbuf) +int reg_prepare_flow_accept(struct flow_info * info) { struct reg_flow * flow; int ret; @@ -1790,8 +1816,6 @@ int reg_prepare_flow_accept(struct flow_info * info, ret = reg_flow_update(flow, info); - reg_flow_set_data(flow, pbuf); - pthread_mutex_unlock(®.mtx); return ret; @@ -1824,8 +1848,6 @@ int reg_wait_flow_accepted(struct flow_info * info, assert(flow != NULL); assert(info->id == flow->info.id); assert(info->n_pid == flow->info.n_pid); - assert(info->state == flow->info.state); - assert(flow->info.state == FLOW_ACCEPT_PENDING); if (__reg_add_active_proc(info->n_pid) < 0) { log_err("Failed to mark pid %d active.", info->n_pid); @@ -1883,13 +1905,12 @@ int reg_wait_flow_accepted(struct flow_info * info, return -1; } -int reg_wait_flow_accepting(enum hash_algo algo, - const uint8_t * hash, +int reg_wait_flow_accepting(const char * name, const struct timespec * abstime) { int ret; - assert(hash != NULL); + assert(name != NULL); assert(abstime != NULL); pthread_mutex_lock(®.mtx); @@ -1897,7 +1918,7 @@ int reg_wait_flow_accepting(enum hash_algo algo, pthread_cleanup_push(__cleanup_mutex_unlock, ®.mtx); while (true) { - ret = __reg_get_pending_flow_id_for_hash(algo, hash); + ret = __reg_get_pending_flow_id(name); if (ret != -EAGAIN) break; @@ -1915,7 +1936,6 @@ int reg_respond_accept(struct flow_info * info, buffer_t * pbuf) { struct reg_flow * flow; - buffer_t temp; assert(info != NULL); assert(info->state == FLOW_ALLOCATED); @@ -1933,11 +1953,8 @@ int reg_respond_accept(struct flow_info * info, info->n_pid = flow->info.n_pid; - if (info->qs.cypher_s > 0) { - reg_flow_get_data(flow, &temp); - reg_flow_set_data(flow, pbuf); - *pbuf = temp; - } + reg_flow_set_data(flow, pbuf); + clrbuf(pbuf); if (reg_flow_update(flow, info) < 0) { log_err("Failed to create flow structs."); @@ -1970,12 +1987,14 @@ void reg_dealloc_flow(struct flow_info * info) assert(flow != NULL); assert(flow->data.data == NULL); assert(flow->data.len == 0); - 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; + memset(flow->name, 0, sizeof(flow->name)); + reg_flow_update(flow, info); pthread_mutex_unlock(®.mtx); @@ -2043,7 +2062,7 @@ int reg_wait_ipcp_boot(struct ipcp_info * info, int ret; bool stop = false; - assert(info->state == IPCP_BOOT); + assert(info->state == IPCP_INIT); pthread_mutex_lock(®.mtx); @@ -2063,16 +2082,18 @@ int reg_wait_ipcp_boot(struct ipcp_info * info, ret = -1; stop = true; break; + case IPCP_BOOT: + /* FALLTHRU*/ case IPCP_OPERATIONAL: ret = 0; stop = true; break; - case IPCP_BOOT: + case IPCP_INIT: ret = -__timedwait(®.cond, ®.mtx, abstime); break; default: assert(false); - continue; /* Shut up static analyzer. */ + break; /* Shut up static analyzer. */ } ipcp = __reg_get_ipcp(info->pid); |