From 06ee3370998f965b469d1c2859e3e34159c71e20 Mon Sep 17 00:00:00 2001 From: Dimitri Staessens Date: Sat, 17 Feb 2024 10:19:46 +0100 Subject: irmd: Revise IRMd internals This is a full revision of the IRMd internal implementation. The registry is now a proper subcomponent managing its own internal lock (a single mutex). Some tests are added for the registry and its data structures. Some macros for tests are added in . Flow allocation is now more symmetric between the client side (alloc) and server size (accept). Each will create a flow in pending state (ALLOC_PENDING/ACCEPT_PENDING) that is potentially fulfilled by an IPCP using respond_alloc and respond_accept primitives. Deallocation is split in flow_dealloc (application side) and ipcp_flow_dealloc (IPCP side) to get the flow in DEALLOC_PENDING and DEALLOCATED state. Cleanup of failed flow allocation is now properly handled instead of relying on the sanitizer thread. The new sanitizer only needs to monitor crashed processes. On shutdown, the IRMd will now detect hanging processes and SIGKILL them and clean up their fuse mountpoints if needed. A lot of other things have been cleaned up and shuffled around a bit. Signed-off-by: Dimitri Staessens Signed-off-by: Sander Vrijders --- src/lib/CMakeLists.txt | 2 +- src/lib/crypt.c | 13 ++-- src/lib/dev.c | 99 ++++++++++++++++++++---- src/lib/irm.c | 67 ++++++++++------ src/lib/list.c | 2 +- src/lib/pb/irm.proto | 47 +++++++----- src/lib/protobuf.c | 8 +- src/lib/rib.c | 10 +++ src/lib/shm_flow_set.c | 16 ++-- src/lib/shm_rbuff.c | 16 ++-- src/lib/shm_rbuff_pthr.c | 20 ++--- src/lib/shm_rdrbuff.c | 16 ++-- src/lib/sockets.c | 20 +++-- src/lib/tests/CMakeLists.txt | 2 +- src/lib/tests/time_test.c | 164 ++++++++++++++++++++++++++++++++++++++++ src/lib/tests/time_utils_test.c | 164 ---------------------------------------- src/lib/tpm.c | 11 ++- src/lib/utils.c | 59 +++++++++++++++ 18 files changed, 450 insertions(+), 286 deletions(-) create mode 100644 src/lib/tests/time_test.c delete mode 100644 src/lib/tests/time_utils_test.c (limited to 'src/lib') diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 514d60d4..4a4684b0 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -172,7 +172,7 @@ else () set(PTHREAD_COND_CLOCK "CLOCK_REALTIME" CACHE INTERNAL "Clock to use for condition variable timing") endif () -set(SOCKET_TIMEOUT 1000 CACHE STRING +set(SOCKET_TIMEOUT 500 CACHE STRING "Default timeout for responses from IPCPs (ms)") set(SHM_PREFIX "ouroboros" CACHE STRING "String to prepend to POSIX shared memory filenames") diff --git a/src/lib/crypt.c b/src/lib/crypt.c index f3b7076d..ad679501 100644 --- a/src/lib/crypt.c +++ b/src/lib/crypt.c @@ -182,15 +182,14 @@ static void openssl_ecdh_pkp_destroy(void * pkp) } static int openssl_ecdh_derive(void * pkp, - uint8_t * pk, - size_t len, + buffer_t pk, uint8_t * s) { uint8_t * pos; EVP_PKEY * pub; - pos = pk; /* d2i_PUBKEY increments the pointer, don't use key ptr! */ - pub = d2i_PUBKEY(NULL, (const uint8_t **) &pos, (long) len); + pos = pk.data; /* d2i_PUBKEY increments the pointer, don't use key ptr! */ + pub = d2i_PUBKEY(NULL, (const uint8_t **) &pos, (long) pk.len); if (pub == NULL) return -ECRYPT; @@ -390,16 +389,14 @@ void crypt_dh_pkp_destroy(void * pkp) } int crypt_dh_derive(void * pkp, - uint8_t * pk, - size_t len, + buffer_t pk, uint8_t * s) { #ifdef HAVE_OPENSSL - return openssl_ecdh_derive(pkp, pk, len, s); + return openssl_ecdh_derive(pkp, pk, s); #else (void) pkp; (void) pk; - (void) len; memset(s, 0, SYMMKEYSZ); diff --git a/src/lib/dev.c b/src/lib/dev.c index 216bf670..9e37978c 100644 --- a/src/lib/dev.c +++ b/src/lib/dev.c @@ -233,9 +233,8 @@ static int proc_announce(char * prog) msg.prog = prog; recv_msg = send_recv_irm_msg(&msg); - if (recv_msg == NULL) { + if (recv_msg == NULL) return -EIRMD; - } if (!recv_msg->has_result || (ret = recv_msg->result)) { irm_msg__free_unpacked(recv_msg, NULL); @@ -247,11 +246,35 @@ static int proc_announce(char * prog) return ret; } +static void proc_exit(void) +{ + irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg; + int ret = -1; + + msg.code = IRM_MSG_CODE__IRM_PROC_EXIT; + msg.has_pid = true; + msg.pid = getpid(); + + recv_msg = send_recv_irm_msg(&msg); + if (recv_msg == NULL) + return; + + if (!recv_msg->has_result || (ret = recv_msg->result)) { + irm_msg__free_unpacked(recv_msg, NULL); + return; + } + + irm_msg__free_unpacked(recv_msg, NULL); + + return; +} + #include "frct.c" void * flow_tx(void * o) { - struct timespec tic = {0, TICTIME}; + struct timespec tic = TIMESPEC_INIT_NS(TICTIME); (void) o; @@ -347,7 +370,7 @@ static void __cleanup_fqueue_destroy(void * fq) void * flow_rx(void * o) { - struct timespec tic = {0, TICTIME}; + struct timespec tic = TIMESPEC_INIT_NS(TICTIME); int ret; struct fqueue * fq; @@ -549,7 +572,7 @@ static void init(int argc, prog = path_strip(prog); if (prog == NULL) { - fprintf(stderr, "FATAL: Could not find program name.\n"); + fprintf(stderr, "FATAL: Could not determine program name.\n"); goto fail_prog; } @@ -588,7 +611,7 @@ static void init(int argc, ai.flows = malloc(sizeof(*ai.flows) * PROG_MAX_FLOWS); if (ai.flows == NULL) { - fprintf(stderr, "FATAL: Could not allocate flows.\n"); + fprintf(stderr, "FATAL: Could not malloc flows.\n"); goto fail_flows; } @@ -597,7 +620,7 @@ static void init(int argc, ai.id_to_fd = malloc(sizeof(*ai.id_to_fd) * SYS_MAX_FLOWS); if (ai.id_to_fd == NULL) { - fprintf(stderr, "FATAL: Could not allocate id_to_fd.\n"); + fprintf(stderr, "FATAL: Could not malloc id_to_fd.\n"); goto fail_id_to_fd; } @@ -716,7 +739,6 @@ static void fini(void) #ifdef PROC_FLOW_STATS rib_fini(); #endif - timerwheel_fini(); fset_destroy(ai.frct_set); @@ -733,6 +755,8 @@ static void fini(void) bmp_destroy(ai.fds); bmp_destroy(ai.fqueues); + proc_exit(); + memset(&ai, 0, sizeof(ai)); } @@ -946,7 +970,7 @@ int flow_dealloc(int fd) irm_msg_t msg = IRM_MSG__INIT; irm_msg_t * recv_msg; uint8_t buf[128]; - struct timespec tic = {0, TICTIME}; + struct timespec tic = TIMESPEC_INIT_NS(TICTIME); struct flow * f; time_t timeo; @@ -1027,6 +1051,53 @@ int flow_dealloc(int fd) return 0; } +int ipcp_flow_dealloc(int fd) +{ + irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg; + struct flow * f; + + if (fd < 0 || fd >= SYS_MAX_FLOWS ) + return -EINVAL; + + msg.code = IRM_MSG_CODE__IPCP_FLOW_DEALLOC; + msg.has_pid = true; + msg.pid = getpid(); + msg.has_flow_id = true; + + f = &ai.flows[fd]; + + pthread_rwlock_rdlock(&ai.lock); + + if (f->flow_id < 0) { + pthread_rwlock_unlock(&ai.lock); + return -ENOTALLOC; + } + + msg.flow_id = f->flow_id; + + pthread_rwlock_unlock(&ai.lock); + + recv_msg = send_recv_irm_msg(&msg); + if (recv_msg == NULL) + return -EIRMD; + + if (!recv_msg->has_result) { + irm_msg__free_unpacked(recv_msg, NULL); + return -EIRMD; + } + + irm_msg__free_unpacked(recv_msg, NULL); + + pthread_rwlock_wrlock(&ai.lock); + + flow_fini(fd); + + pthread_rwlock_unlock(&ai.lock); + + return 0; +} + int fccntl(int fd, int cmd, ...) @@ -1801,19 +1872,19 @@ int np1_flow_resp(int flow_id) return fd; } -int ipcp_create_r(int result) +int ipcp_create_r(const struct ipcp_info * info) { irm_msg_t msg = IRM_MSG__INIT; irm_msg_t * recv_msg; int ret; msg.code = IRM_MSG_CODE__IPCP_CREATE_R; - msg.has_pid = true; - msg.pid = getpid(); - msg.has_result = true; - msg.result = result; + msg.ipcp_info = ipcp_info_s_to_msg(info); recv_msg = send_recv_irm_msg(&msg); + + ipcp_info_msg__free_unpacked(msg.ipcp_info, NULL); + if (recv_msg == NULL) return -EIRMD; diff --git a/src/lib/irm.c b/src/lib/irm.c index 0947c0c7..d25101f3 100644 --- a/src/lib/irm.c +++ b/src/lib/irm.c @@ -38,8 +38,8 @@ #include #include -pid_t irm_create_ipcp(const char * name, - enum ipcp_type type) +int irm_create_ipcp(const char * name, + enum ipcp_type type) { irm_msg_t msg = IRM_MSG__INIT; irm_msg_t * recv_msg; @@ -49,6 +49,8 @@ pid_t irm_create_ipcp(const char * name, if (name == NULL || strlen(name) > IPCP_NAME_SIZE) return -EINVAL; + memset(&info, 0, sizeof(info)); + info.type = type; strcpy(info.name, name); @@ -366,50 +368,69 @@ int irm_bind_program(const char * prog, int argc, char ** argv) { - irm_msg_t msg = IRM_MSG__INIT; - irm_msg_t * recv_msg = NULL; - int ret = -1; - char * full_name; + irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg; + char ** exec; + int ret; + int i; if (prog == NULL || name == NULL) return -EINVAL; - full_name = strdup(prog); - if (full_name == NULL) - return -ENOMEM; + exec = malloc((argc + 2) * sizeof(*exec)); + if (exec== NULL) { + ret = -ENOMEM; + goto fail_exec; + } - if ((ret = check_prog_path(&full_name)) < 0) { - free(full_name); - return ret; + exec[0] = strdup(prog); + if (exec[0] == NULL) { + ret = -ENOMEM; + goto fail_exec0; } + ret = check_prog_path(&exec[0]); + if (ret < 0) + goto fail; + + for (i = 0; i < argc; i++) + exec[i + 1] = argv[i]; + + exec[argc + 1] = ""; + msg.code = IRM_MSG_CODE__IRM_BIND_PROGRAM; msg.name = (char *) name; - msg.prog = full_name; - if (argv != NULL) { - msg.n_args = argc; - msg.args = (char **) argv; - } + msg.n_exec = argc + 2; + msg.exec = exec; msg.has_opts = true; msg.opts = opts; recv_msg = send_recv_irm_msg(&msg); - - free(full_name); - - if (recv_msg == NULL) - return -EIRMD; + if (recv_msg == NULL) { + ret = -EIRMD; + goto fail; + } if (!recv_msg->has_result) { irm_msg__free_unpacked(recv_msg, NULL); - return -1; + ret = -EPERM; + goto fail; } ret = recv_msg->result; irm_msg__free_unpacked(recv_msg, NULL); + free(exec[0]); + free(exec); + + return ret; + fail: + free(exec[0]); + fail_exec0: + free(exec); + fail_exec: return ret; } diff --git a/src/lib/list.c b/src/lib/list.c index 4fc9fa4a..62b2eb27 100644 --- a/src/lib/list.c +++ b/src/lib/list.c @@ -65,7 +65,7 @@ void list_del(struct list_head * e) e->nxt = e->prv = e; } -bool list_is_empty(struct list_head * h) +bool list_is_empty(const struct list_head * h) { return h->nxt == h; } diff --git a/src/lib/pb/irm.proto b/src/lib/pb/irm.proto index 77db8057..c962e5e5 100644 --- a/src/lib/pb/irm.proto +++ b/src/lib/pb/irm.proto @@ -37,32 +37,37 @@ enum irm_msg_code { IRM_BIND_PROGRAM = 9; IRM_UNBIND_PROGRAM = 10; IRM_PROC_ANNOUNCE = 11; - IRM_BIND_PROCESS = 12; - IRM_UNBIND_PROCESS = 13; - IRM_CREATE_NAME = 14; - IRM_DESTROY_NAME = 15; - IRM_LIST_NAMES = 16; - IRM_REG_NAME = 17; - IRM_UNREG_NAME = 18; - IRM_FLOW_ALLOC = 19; - IRM_FLOW_ACCEPT = 20; - IRM_FLOW_JOIN = 21; - IRM_FLOW_DEALLOC = 22; - IPCP_FLOW_REQ_ARR = 23; - IPCP_FLOW_ALLOC_REPLY = 24; - IRM_REPLY = 25; + IRM_PROC_EXIT = 12; + IRM_BIND_PROCESS = 13; + IRM_UNBIND_PROCESS = 14; + IRM_CREATE_NAME = 15; + IRM_DESTROY_NAME = 16; + IRM_LIST_NAMES = 17; + IRM_REG_NAME = 18; + IRM_UNREG_NAME = 19; + IRM_FLOW_ALLOC = 20; + IRM_FLOW_ACCEPT = 21; + IRM_FLOW_JOIN = 22; + IRM_FLOW_DEALLOC = 23; + IPCP_FLOW_DEALLOC = 24; + IPCP_FLOW_REQ_ARR = 25; + IPCP_FLOW_ALLOC_REPLY = 26; + IRM_REPLY = 27; } message ipcp_info_msg { - required uint32 type = 1; - required string name = 2; + required uint32 type = 1; + required string name = 2; + required uint32 pid = 3; + required uint32 state = 4; } message ipcp_list_msg { - required uint32 pid = 1; - required uint32 type = 2; - required string name = 3; - required string layer = 4; + required uint32 pid = 1; + required uint32 type = 2; + required string name = 3; + required string layer = 4; + required uint32 hash_algo = 5; } message name_info_msg { @@ -77,7 +82,7 @@ message irm_msg { optional string name = 4; optional ipcp_info_msg ipcp_info = 5; optional string layer = 6; - repeated string args = 7; + repeated string exec = 7; optional sint32 response = 8; optional string dst = 9; optional bytes hash = 10; diff --git a/src/lib/protobuf.c b/src/lib/protobuf.c index b6bec783..2135d57e 100644 --- a/src/lib/protobuf.c +++ b/src/lib/protobuf.c @@ -83,7 +83,9 @@ ipcp_info_msg_t * ipcp_info_s_to_msg(const struct ipcp_info * s) if (msg->name == NULL) goto fail_msg; - msg->type = s->type; + msg->type = s->type; + msg->pid = s->pid; + msg->state = s->state; return msg; fail_msg: @@ -100,8 +102,10 @@ struct ipcp_info ipcp_info_msg_to_s(const ipcp_info_msg_t * msg) assert(msg->name != NULL); assert(strlen(msg->name) <= NAME_SIZE); - s.type = msg->type; strcpy(s.name, msg->name); + s.type = msg->type; + s.pid = msg->pid; + s.state = msg->state; return s; } diff --git a/src/lib/rib.c b/src/lib/rib.c index abbeba21..97a20f47 100644 --- a/src/lib/rib.c +++ b/src/lib/rib.c @@ -391,6 +391,16 @@ void rib_fini(void) #endif } +void rib_cleanup(const char * mnt) +{ +#ifdef HAVE_FUSE + fuse_unmount(mnt, NULL); + rmdir(mnt); +#else + (void) mnt; +#endif +} + int rib_reg(const char * path, struct rib_ops * ops) { diff --git a/src/lib/shm_flow_set.c b/src/lib/shm_flow_set.c index 5f31ed3e..39913fd1 100644 --- a/src/lib/shm_flow_set.c +++ b/src/lib/shm_flow_set.c @@ -24,21 +24,21 @@ #include "config.h" -#include -#include -#include #include +#include #include +#include +#include -#include +#include #include +#include #include #include +#include #include -#include +#include #include -#include -#include /* * pthread_cond_timedwait has a WONTFIX bug as of glibc 2.25 where it @@ -196,7 +196,7 @@ struct shm_flow_set * shm_flow_set_open(pid_t pid) void shm_flow_set_destroy(struct shm_flow_set * set) { - char fn[25]; + char fn[FN_MAX_CHARS]; assert(set); diff --git a/src/lib/shm_rbuff.c b/src/lib/shm_rbuff.c index 19a775ae..22cff41c 100644 --- a/src/lib/shm_rbuff.c +++ b/src/lib/shm_rbuff.c @@ -26,22 +26,22 @@ #include #include -#include #include #include #include +#include -#include +#include #include +#include +#include +#include +#include #include #include -#include -#include #include -#include +#include #include -#include -#include #define FN_MAX_CHARS 255 @@ -172,7 +172,7 @@ struct shm_rbuff * shm_rbuff_create(pid_t pid, *rb->head = 0; *rb->tail = 0; - rb->pid = pid; + rb->pid = pid; rb->flow_id = flow_id; pthread_mutexattr_destroy(&mattr); diff --git a/src/lib/shm_rbuff_pthr.c b/src/lib/shm_rbuff_pthr.c index 1ee346e8..b543fb07 100644 --- a/src/lib/shm_rbuff_pthr.c +++ b/src/lib/shm_rbuff_pthr.c @@ -24,7 +24,7 @@ void shm_rbuff_destroy(struct shm_rbuff * rb) { char fn[FN_MAX_CHARS]; - assert(rb); + assert(rb != NULL); #ifdef CONFIG_OUROBOROS_DEBUG pthread_mutex_lock(rb->lock); @@ -48,7 +48,7 @@ int shm_rbuff_write(struct shm_rbuff * rb, { int ret = 0; - assert(rb); + assert(rb != NULL); assert(idx < SHM_BUFFER_SIZE); #ifndef HAVE_ROBUST_MUTEX @@ -91,7 +91,7 @@ int shm_rbuff_write_b(struct shm_rbuff * rb, { int ret = 0; - assert(rb); + assert(rb != NULL); assert(idx < SHM_BUFFER_SIZE); #ifndef HAVE_ROBUST_MUTEX @@ -138,6 +138,8 @@ int shm_rbuff_write_b(struct shm_rbuff * rb, static int check_rb_acl(struct shm_rbuff * rb) { + assert(rb != NULL); + if (*rb->acl & ACL_FLOWDOWN) return -EFLOWDOWN; @@ -151,7 +153,7 @@ ssize_t shm_rbuff_read(struct shm_rbuff * rb) { ssize_t ret = 0; - assert(rb); + assert(rb != NULL); #ifndef HAVE_ROBUST_MUTEX pthread_mutex_lock(rb->lock); @@ -180,7 +182,7 @@ ssize_t shm_rbuff_read_b(struct shm_rbuff * rb, { ssize_t idx = -1; - assert(rb); + assert(rb != NULL); #ifndef HAVE_ROBUST_MUTEX pthread_mutex_lock(rb->lock); @@ -224,7 +226,7 @@ ssize_t shm_rbuff_read_b(struct shm_rbuff * rb, void shm_rbuff_set_acl(struct shm_rbuff * rb, uint32_t flags) { - assert(rb); + assert(rb != NULL); #ifndef HAVE_ROBUST_MUTEX pthread_mutex_lock(rb->lock); @@ -244,7 +246,7 @@ uint32_t shm_rbuff_get_acl(struct shm_rbuff * rb) { uint32_t flags; - assert(rb); + assert(rb != NULL); #ifndef HAVE_ROBUST_MUTEX pthread_mutex_lock(rb->lock); @@ -261,7 +263,7 @@ uint32_t shm_rbuff_get_acl(struct shm_rbuff * rb) void shm_rbuff_fini(struct shm_rbuff * rb) { - assert(rb); + assert(rb != NULL); #ifndef HAVE_ROBUST_MUTEX pthread_mutex_lock(rb->lock); @@ -285,7 +287,7 @@ size_t shm_rbuff_queued(struct shm_rbuff * rb) { size_t ret; - assert(rb); + assert(rb != NULL); #ifndef HAVE_ROBUST_MUTEX pthread_mutex_lock(rb->lock); diff --git a/src/lib/shm_rdrbuff.c b/src/lib/shm_rdrbuff.c index d6849d03..7ad1bd2e 100644 --- a/src/lib/shm_rdrbuff.c +++ b/src/lib/shm_rdrbuff.c @@ -25,21 +25,19 @@ #include "config.h" #include -#include -#include -#include #include +#include -#include +#include #include -#include +#include +#include +#include #include #include -#include -#include +#include +#include #include -#include -#include #define SHM_BLOCKS_SIZE ((SHM_BUFFER_SIZE) * SHM_RDRB_BLOCK_SIZE) #define SHM_FILE_SIZE (SHM_BLOCKS_SIZE + 2 * sizeof(size_t) \ diff --git a/src/lib/sockets.c b/src/lib/sockets.c index 5ed43647..9c5b7a51 100644 --- a/src/lib/sockets.c +++ b/src/lib/sockets.c @@ -31,7 +31,6 @@ #include #include #include -#include /* Apple doesn't support SEQPACKET. */ #ifdef __APPLE__ @@ -57,8 +56,7 @@ int client_socket_open(char * file_name) serv_addr.sun_family = AF_UNIX; sprintf(serv_addr.sun_path, "%s", file_name); - if (connect(sockfd, - (struct sockaddr *) &serv_addr, + if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))) { close(sockfd); return -1; @@ -100,13 +98,11 @@ int server_socket_open(char * file_name) return sockfd; } -__attribute__((no_sanitize_address)) irm_msg_t * send_recv_irm_msg(irm_msg_t * msg) { - int sockfd; - uint8_t buf[SOCK_BUF_SIZE]; - ssize_t len; - irm_msg_t * recv_msg = NULL; + int sockfd; + uint8_t buf[SOCK_BUF_SIZE]; + ssize_t len; sockfd = client_socket_open(IRM_SOCK_PATH); if (sockfd < 0) @@ -127,10 +123,12 @@ irm_msg_t * send_recv_irm_msg(irm_msg_t * msg) pthread_cleanup_pop(true); - if (len > 0) - recv_msg = irm_msg__unpack(NULL, len, buf); + if (len < 0) + goto fail; - return recv_msg; + return irm_msg__unpack(NULL, len, buf); + fail: + return NULL; } char * ipcp_sock_path(pid_t pid) diff --git a/src/lib/tests/CMakeLists.txt b/src/lib/tests/CMakeLists.txt index b3b79760..0e114548 100644 --- a/src/lib/tests/CMakeLists.txt +++ b/src/lib/tests/CMakeLists.txt @@ -10,7 +10,7 @@ create_test_sourcelist(${PARENT_DIR}_tests test_suite.c md5_test.c sha3_test.c shm_rbuff_test.c - time_utils_test.c + time_test.c ) add_executable(${PARENT_DIR}_test EXCLUDE_FROM_ALL ${${PARENT_DIR}_tests}) diff --git a/src/lib/tests/time_test.c b/src/lib/tests/time_test.c new file mode 100644 index 00000000..65f896bb --- /dev/null +++ b/src/lib/tests/time_test.c @@ -0,0 +1,164 @@ +/* + * Ouroboros - Copyright (C) 2016 - 2024 + * + * Test of the time utilities + * + * Dimitri Staessens + * Sander Vrijders + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., http://www.fsf.org/about/contact/. + */ + +#define _POSIX_C_SOURCE 200809L + +#include + +#include + +static void ts_print(struct timespec * s) +{ + printf("timespec is %zd:%ld.\n", (ssize_t) s->tv_sec, s->tv_nsec); +} + +static void tv_print(struct timeval * v) +{ + printf("timeval is %zd:%zu.\n", (ssize_t) v->tv_sec, (size_t) v->tv_usec); +} + +static void ts_init(struct timespec * s, + time_t sec, + time_t nsec) +{ + s->tv_sec = sec; + s->tv_nsec = nsec; +} + +static void tv_init(struct timeval * v, + time_t sec, + time_t usec) +{ + v->tv_sec = sec; + v->tv_usec = usec; +} + +static int ts_check(struct timespec * s, + time_t sec, + time_t nsec) +{ + return s->tv_sec == sec && s->tv_nsec == nsec; +} + +static int tv_check(struct timeval * v, + time_t sec, + time_t usec) +{ + return v->tv_sec == sec && v->tv_usec == usec; +} + +int time_test(int argc, + char ** argv) +{ + struct timespec s0; + struct timespec s1; + struct timespec s2; + + struct timeval v0; + struct timeval v1; + struct timeval v2; + + (void) argc; + (void) argv; + + ts_init(&s0, 0, 0); + ts_init(&s1, 5, 0); + + ts_add(&s0, &s1, &s2); + if (!ts_check(&s2, 5, 0)) { + printf("ts_add failed.\n"); + ts_print(&s2); + return -1; + } + + tv_init(&v0, 0, 0); + tv_init(&v1, 5, 0); + + tv_add(&v0, &v1, &v2); + if (!tv_check(&v2, 5, 0)) { + printf("tv_add failed.\n"); + tv_print(&v2); + return -1; + } + + ts_init(&s0, 0, 500 * MILLION); + ts_init(&s1, 0, 600 * MILLION); + + ts_add(&s0, &s1, &s2); + if (!ts_check(&s2, 1, 100 * MILLION)) { + printf("ts_add with nano overflow failed.\n"); + ts_print(&s2); + return -1; + } + + tv_init(&v0, 0, 500 * 1000); + tv_init(&v1, 0, 600 * 1000); + + tv_add(&v0, &v1, &v2); + if (!tv_check(&v2, 1, 100 * 1000)) { + printf("tv_add with nano overflow failed.\n"); + tv_print(&v2); + return -1; + } + + ts_init(&s0, 0, 0); + ts_init(&s1, 5, 0); + + ts_diff(&s0, &s1, &s2); + if (!ts_check(&s2, -5, 0)) { + printf("ts_diff failed.\n"); + ts_print(&s2); + return -1; + } + + tv_init(&v0, 0, 0); + tv_init(&v1, 5, 0); + + tv_diff(&v0, &v1, &v2); + if (!tv_check(&v2, -5, 0)) { + printf("tv_diff failed.\n"); + tv_print(&v2); + return -1; + } + + ts_init(&s0, 0, 500 * MILLION); + ts_init(&s1, 0, 600 * MILLION); + + ts_diff(&s0, &s1, &s2); + if (!ts_check(&s2, -1, 900 * MILLION)) { + printf("ts_diff with nano underflow failed.\n"); + ts_print(&s2); + return -1; + } + + tv_init(&v0, 0, 500 * 1000); + tv_init(&v1, 0, 600 * 1000); + + tv_diff(&v0, &v1, &v2); + if (!tv_check(&v2, -1, 900 * 1000)) { + printf("tv_diff with nano underflow failed.\n"); + tv_print(&v2); + return -1; + } + + return 0; +} diff --git a/src/lib/tests/time_utils_test.c b/src/lib/tests/time_utils_test.c deleted file mode 100644 index 1b9e7a80..00000000 --- a/src/lib/tests/time_utils_test.c +++ /dev/null @@ -1,164 +0,0 @@ -/* - * Ouroboros - Copyright (C) 2016 - 2024 - * - * Test of the time utilities - * - * Dimitri Staessens - * Sander Vrijders - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., http://www.fsf.org/about/contact/. - */ - -#define _POSIX_C_SOURCE 200809L - -#include - -#include - -static void ts_print(struct timespec * s) -{ - printf("timespec is %zd:%ld.\n", (ssize_t) s->tv_sec, s->tv_nsec); -} - -static void tv_print(struct timeval * v) -{ - printf("timeval is %zd:%zu.\n", (ssize_t) v->tv_sec, (size_t) v->tv_usec); -} - -static void ts_init(struct timespec * s, - time_t sec, - time_t nsec) -{ - s->tv_sec = sec; - s->tv_nsec = nsec; -} - -static void tv_init(struct timeval * v, - time_t sec, - time_t usec) -{ - v->tv_sec = sec; - v->tv_usec = usec; -} - -static int ts_check(struct timespec * s, - time_t sec, - time_t nsec) -{ - return s->tv_sec == sec && s->tv_nsec == nsec; -} - -static int tv_check(struct timeval * v, - time_t sec, - time_t usec) -{ - return v->tv_sec == sec && v->tv_usec == usec; -} - -int time_utils_test(int argc, - char ** argv) -{ - struct timespec s0; - struct timespec s1; - struct timespec s2; - - struct timeval v0; - struct timeval v1; - struct timeval v2; - - (void) argc; - (void) argv; - - ts_init(&s0, 0, 0); - ts_init(&s1, 5, 0); - - ts_add(&s0, &s1, &s2); - if (!ts_check(&s2, 5, 0)) { - printf("ts_add failed.\n"); - ts_print(&s2); - return -1; - } - - tv_init(&v0, 0, 0); - tv_init(&v1, 5, 0); - - tv_add(&v0, &v1, &v2); - if (!tv_check(&v2, 5, 0)) { - printf("tv_add failed.\n"); - tv_print(&v2); - return -1; - } - - ts_init(&s0, 0, 500 * MILLION); - ts_init(&s1, 0, 600 * MILLION); - - ts_add(&s0, &s1, &s2); - if (!ts_check(&s2, 1, 100 * MILLION)) { - printf("ts_add with nano overflow failed.\n"); - ts_print(&s2); - return -1; - } - - tv_init(&v0, 0, 500 * 1000); - tv_init(&v1, 0, 600 * 1000); - - tv_add(&v0, &v1, &v2); - if (!tv_check(&v2, 1, 100 * 1000)) { - printf("tv_add with nano overflow failed.\n"); - tv_print(&v2); - return -1; - } - - ts_init(&s0, 0, 0); - ts_init(&s1, 5, 0); - - ts_diff(&s0, &s1, &s2); - if (!ts_check(&s2, -5, 0)) { - printf("ts_diff failed.\n"); - ts_print(&s2); - return -1; - } - - tv_init(&v0, 0, 0); - tv_init(&v1, 5, 0); - - tv_diff(&v0, &v1, &v2); - if (!tv_check(&v2, -5, 0)) { - printf("tv_diff failed.\n"); - tv_print(&v2); - return -1; - } - - ts_init(&s0, 0, 500 * MILLION); - ts_init(&s1, 0, 600 * MILLION); - - ts_diff(&s0, &s1, &s2); - if (!ts_check(&s2, -1, 900 * MILLION)) { - printf("ts_diff with nano underflow failed.\n"); - ts_print(&s2); - return -1; - } - - tv_init(&v0, 0, 500 * 1000); - tv_init(&v1, 0, 600 * 1000); - - tv_diff(&v0, &v1, &v2); - if (!tv_check(&v2, -1, 900 * 1000)) { - printf("tv_diff with nano underflow failed.\n"); - tv_print(&v2); - return -1; - } - - return 0; -} diff --git a/src/lib/tpm.c b/src/lib/tpm.c index ae495954..0ef1fda8 100644 --- a/src/lib/tpm.c +++ b/src/lib/tpm.c @@ -26,12 +26,12 @@ #include #include -#include +#include #include +#include #include #include -#include #define TPM_TIMEOUT 1000 @@ -117,8 +117,7 @@ static void tpm_kill(struct tpm * tpm) static void * tpmgr(void * o) { struct timespec dl; - struct timespec to = {(TPM_TIMEOUT / 1000), - (TPM_TIMEOUT % 1000) * MILLION}; + struct timespec to = TIMESPEC_INIT_MS(TPM_TIMEOUT); struct tpm * tpm = (struct tpm *) o; while (true) { @@ -239,12 +238,12 @@ void tpm_stop(struct tpm * tpm) tpm->state = TPM_NULL; pthread_mutex_unlock(&tpm->lock); + + pthread_join(tpm->mgr, NULL); } void tpm_destroy(struct tpm * tpm) { - pthread_join(tpm->mgr, NULL); - pthread_mutex_destroy(&tpm->lock); pthread_cond_destroy(&tpm->cond); diff --git a/src/lib/utils.c b/src/lib/utils.c index 6f6a65ae..fdbcd9d9 100644 --- a/src/lib/utils.c +++ b/src/lib/utils.c @@ -20,6 +20,10 @@ * Foundation, Inc., http://www.fsf.org/about/contact/. */ +#define _POSIX_C_SOURCE 200809L + +#include + #include #include @@ -52,3 +56,58 @@ char * path_strip(const char * src) return dst; } + +size_t argvlen(const char ** argv) +{ + size_t argc = 0; + + if (argv == NULL) + return 0; + + while (*argv++ != NULL) + argc++; + + return argc; +} + +void argvfree(char ** argv) +{ + char ** argv_dup; + + if (argv == NULL) + return; + + argv_dup = argv; + while (*argv_dup != NULL) + free(*(argv_dup++)); + + free(argv); +} + +char ** argvdup(char ** argv) +{ + int argc = 0; + char ** argv_dup = argv; + int i; + + if (argv == NULL) + return NULL; + + while (*(argv_dup++) != NULL) + argc++; + + argv_dup = malloc((argc + 1) * sizeof(*argv_dup)); + if (argv_dup == NULL) + return NULL; + + for (i = 0; i < argc; ++i) { + argv_dup[i] = strdup(argv[i]); + if (argv_dup[i] == NULL) { + argvfree(argv_dup); + return NULL; + } + } + + argv_dup[argc] = NULL; + return argv_dup; +} -- cgit v1.2.3