From a46114ec01e8d174a41744f4f1b49905613847dc Mon Sep 17 00:00:00 2001 From: dimitri staessens Date: Mon, 20 Jun 2016 23:49:35 +0200 Subject: lib, irmd, ipcp: robust mutexes Update to POSIX 200112L to allow use of robust mutexes in the shm_du_map. Removed the implementation of the rw_lock in favor of pthread_rwlock_t. Placeholder for the shm_du_map_sanitize function. --- compile_debug.sh | 2 +- include/ouroboros/config.h.in | 2 +- include/ouroboros/rw_lock.h | 41 ---- include/ouroboros/shm_du_map.h | 12 +- install_debug.sh | 2 +- src/ipcpd/ipcp.c | 2 +- src/ipcpd/ipcp.h | 5 +- src/ipcpd/local/main.c | 121 ++++++----- src/ipcpd/shim-eth-llc/main.c | 187 ++++++++--------- src/ipcpd/shim-udp/main.c | 219 ++++++++++--------- src/irmd/main.c | 464 +++++++++++++++++++++-------------------- src/lib/CMakeLists.txt | 1 - src/lib/dev.c | 141 +++++++------ src/lib/rw_lock.c | 127 ----------- src/lib/shm_du_map.c | 32 ++- 15 files changed, 614 insertions(+), 744 deletions(-) delete mode 100644 include/ouroboros/rw_lock.h delete mode 100644 src/lib/rw_lock.c diff --git a/compile_debug.sh b/compile_debug.sh index 9f7da176..78a298fc 100755 --- a/compile_debug.sh +++ b/compile_debug.sh @@ -6,7 +6,7 @@ if (($# == 1 )) then PREFIX=$1 else - PREFIX="/usr/local/ouroboros" + PREFIX="/usr/local/ouroboros/" fi BUILDDIR=build diff --git a/include/ouroboros/config.h.in b/include/ouroboros/config.h.in index 0657710e..a20715ac 100644 --- a/include/ouroboros/config.h.in +++ b/include/ouroboros/config.h.in @@ -26,7 +26,7 @@ #define PROJECT_NAME "@CMAKE_PROJECT_NAME@" #define PROJECT_VERSION "@PACKAGE_VERSION@" #define INSTALL_DIR "@CMAKE_INSTALL_PREFIX@" -#define _POSIX_C_SOURCE 199506L +#define _POSIX_C_SOURCE 200112L #define IPCP_SHIM_UDP_EXEC "@IPCP_SHIM_UDP_TARGET@" #define IPCP_SHIM_ETH_LLC_EXEC "@IPCP_SHIM_ETH_LLC_TARGET@" #define IPCP_NORMAL_EXEC "@IPCP_NORMAL_TARGET@" diff --git a/include/ouroboros/rw_lock.h b/include/ouroboros/rw_lock.h deleted file mode 100644 index 03eee605..00000000 --- a/include/ouroboros/rw_lock.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Ouroboros - Copyright (C) 2016 - * - * Read/Write locks - * - * Dimitri Staessens - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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., 675 Mass Ave, Cambridge, MA 02139, USA. - */ - -#ifndef OUROBOROS_RWLOCK_H -#define OUROBOROS_RWLOCK_H - -#include -#include - -typedef struct rw_lock { - pthread_mutex_t lock; - pthread_mutex_t i_lock; - int i; -} rw_lock_t; - -int rw_lock_init(rw_lock_t * lock); -void rw_lock_destroy(rw_lock_t * lock); -int rw_lock_rdlock(rw_lock_t * lock); -int rw_lock_wrlock(rw_lock_t * lock); -int rw_lock_unlock(rw_lock_t * lock); - -#endif /* OUROBOROS_RWLOCK_H */ diff --git a/include/ouroboros/shm_du_map.h b/include/ouroboros/shm_du_map.h index 081a6f62..2d215651 100644 --- a/include/ouroboros/shm_du_map.h +++ b/include/ouroboros/shm_du_map.h @@ -38,15 +38,17 @@ #include "common.h" #include +#include struct shm_du_buff; struct shm_du_map; -struct shm_du_map * shm_du_map_create(); -struct shm_du_map * shm_du_map_open(); -void shm_du_map_close(struct shm_du_map * dum); -void shm_du_map_destroy(struct shm_du_map * dum); -pid_t shm_du_map_owner(struct shm_du_map * dum); +struct shm_du_map * shm_du_map_create(); +struct shm_du_map * shm_du_map_open(); +void shm_du_map_close(struct shm_du_map * dum); +void shm_du_map_destroy(struct shm_du_map * dum); +pid_t shm_du_map_owner(struct shm_du_map * dum); +void * shm_du_map_sanitize(void * o); /* returns the index of the buffer in the DU map */ ssize_t shm_create_du_buff(struct shm_du_map * dum, diff --git a/install_debug.sh b/install_debug.sh index 020677a0..abb5c1c9 100755 --- a/install_debug.sh +++ b/install_debug.sh @@ -6,7 +6,7 @@ if (($# == 1 )) then PREFIX=$1 else - PREFIX="/usr/local/ouroboros" + PREFIX="/usr/local/ouroboros/" fi BUILDDIR=build diff --git a/src/ipcpd/ipcp.c b/src/ipcpd/ipcp.c index 5a621391..901abae6 100644 --- a/src/ipcpd/ipcp.c +++ b/src/ipcpd/ipcp.c @@ -40,7 +40,7 @@ struct ipcp * ipcp_instance_create() i->irmd_fd = -1; i->state = IPCP_INIT; - rw_lock_init(&i->state_lock); + pthread_rwlock_init(&i->state_lock, NULL); return i; } diff --git a/src/ipcpd/ipcp.h b/src/ipcpd/ipcp.h index 63f9677a..1e9e9763 100644 --- a/src/ipcpd/ipcp.h +++ b/src/ipcpd/ipcp.h @@ -23,7 +23,8 @@ #ifndef IPCPD_IPCP_H #define IPCPD_IPCP_H -#include +#include +#include #include "ipcp-ops.h" #include "ipcp-data.h" @@ -41,7 +42,7 @@ struct ipcp { int irmd_fd; enum ipcp_state state; - rw_lock_t state_lock; + pthread_rwlock_t state_lock; }; struct ipcp * ipcp_instance_create(); diff --git a/src/ipcpd/local/main.c b/src/ipcpd/local/main.c index 43ca14da..1e033a21 100644 --- a/src/ipcpd/local/main.c +++ b/src/ipcpd/local/main.c @@ -33,7 +33,6 @@ #include #include #include -#include #define OUROBOROS_PREFIX "ipcpd/local" @@ -76,7 +75,7 @@ struct shim_ap_data { int in_out[AP_MAX_FLOWS]; struct flow flows[AP_MAX_FLOWS]; - rw_lock_t flows_lock; + pthread_rwlock_t flows_lock; pthread_t mainloop; pthread_t sduloop; @@ -137,7 +136,7 @@ static int shim_ap_init(char * ap_name) _ap_instance->in_out[i] = -1; } - rw_lock_init(&_ap_instance->flows_lock); + pthread_rwlock_init(&_ap_instance->flows_lock, NULL); return 0; } @@ -149,7 +148,7 @@ void shim_ap_fini() if (_ap_instance == NULL) return; - rw_lock_wrlock(&_ipcp->state_lock); + pthread_rwlock_wrlock(&_ipcp->state_lock); if (_ipcp->state != IPCP_SHUTDOWN) LOG_WARN("Cleaning up AP while not in shutdown."); @@ -163,14 +162,14 @@ void shim_ap_fini() if (_ap_instance->rb != NULL) shm_ap_rbuff_destroy(_ap_instance->rb); - rw_lock_wrlock(&_ap_instance->flows_lock); + pthread_rwlock_wrlock(&_ap_instance->flows_lock); for (i = 0; i < AP_MAX_FLOWS; i ++) if (_ap_instance->flows[i].rb != NULL) shm_ap_rbuff_close(_ap_instance->flows[i].rb); - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); free(_ap_instance); } @@ -206,18 +205,18 @@ static void * ipcp_local_sdu_loop(void * o) continue; } - rw_lock_rdlock(&_ipcp->state_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); if (_ipcp->state != IPCP_ENROLLED) { - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); return (void *) 1; /* -ENOTENROLLED */ } - rw_lock_rdlock(&_ap_instance->flows_lock); + pthread_rwlock_rdlock(&_ap_instance->flows_lock); fd = _ap_instance->in_out[port_id_to_fd(e->port_id)]; if (fd == -1) { - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); free(e); continue; } @@ -226,8 +225,8 @@ static void * ipcp_local_sdu_loop(void * o) while (shm_ap_rbuff_write(_ap_instance->flows[fd].rb, e) < 0) ; - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); } return (void *) 1; @@ -249,14 +248,14 @@ void ipcp_sig_handler(int sig, siginfo_t * info, void * c) LOG_DBG("Terminating by order of %d. Bye.", info->si_pid); - rw_lock_wrlock(&_ipcp->state_lock); + pthread_rwlock_wrlock(&_ipcp->state_lock); if (_ipcp->state == IPCP_ENROLLED) clean_threads = true; _ipcp->state = IPCP_SHUTDOWN; - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); if (clean_threads) { pthread_cancel(_ap_instance->sduloop); @@ -278,12 +277,12 @@ static int ipcp_local_bootstrap(struct dif_config * conf) } if (_ipcp->state != IPCP_INIT) { - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_ERR("IPCP in wrong state."); return -1; } - rw_lock_wrlock(&_ipcp->state_lock); + pthread_rwlock_wrlock(&_ipcp->state_lock); _ipcp->state = IPCP_ENROLLED; @@ -292,7 +291,7 @@ static int ipcp_local_bootstrap(struct dif_config * conf) ipcp_local_sdu_loop, NULL); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBG("Bootstrapped local IPCP with pid %d.", getpid()); @@ -302,25 +301,25 @@ static int ipcp_local_bootstrap(struct dif_config * conf) static int ipcp_local_name_reg(char * name) { - rw_lock_rdlock(&_ipcp->state_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); if (_ipcp->state != IPCP_ENROLLED) { - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBGF("Won't register with non-enrolled IPCP."); return -1; /* -ENOTENROLLED */ } - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); - rw_lock_rdlock(&_ipcp->state_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); if (ipcp_data_add_reg_entry(_ipcp->data, name)) { - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBGF("Failed to add %s to local registry.", name); return -1; } - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBG("Registered %s.", name); @@ -329,11 +328,11 @@ static int ipcp_local_name_reg(char * name) static int ipcp_local_name_unreg(char * name) { - rw_lock_rdlock(&_ipcp->state_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); ipcp_data_del_reg_entry(_ipcp->data, name); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); return 0; } @@ -356,26 +355,26 @@ static int ipcp_local_flow_alloc(pid_t n_pid, /* This ipcpd has all QoS */ - rw_lock_rdlock(&_ipcp->state_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); if (_ipcp->state != IPCP_ENROLLED) { - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBGF("Won't allocate flow with non-enrolled IPCP."); return -1; /* -ENOTENROLLED */ } rb = shm_ap_rbuff_open(n_pid); if (rb == NULL) { - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); return -1; /* -ENORBUFF */ } - rw_lock_wrlock(&_ap_instance->flows_lock); + pthread_rwlock_wrlock(&_ap_instance->flows_lock); in_fd = bmp_allocate(_ap_instance->fds); if (!bmp_is_id_valid(_ap_instance->fds, in_fd)) { - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); return -EMFILE; } @@ -391,8 +390,8 @@ static int ipcp_local_flow_alloc(pid_t n_pid, src_ae_name); if (port_id < 0) { - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_ERR("Could not get port id from IRMd"); /* shm_ap_rbuff_close(n_pid); */ return -1; @@ -401,8 +400,8 @@ static int ipcp_local_flow_alloc(pid_t n_pid, out_fd = bmp_allocate(_ap_instance->fds); if (!bmp_is_id_valid(_ap_instance->fds, out_fd)) { /* shm_ap_rbuff_close(n_pid); */ - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); return -1; /* -ENOMOREFDS */ } @@ -413,8 +412,8 @@ static int ipcp_local_flow_alloc(pid_t n_pid, _ap_instance->in_out[in_fd] = out_fd; _ap_instance->in_out[out_fd] = in_fd; - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBGF("Pending local allocation request, port_id %d.", port_id); @@ -433,23 +432,23 @@ static int ipcp_local_flow_alloc_resp(pid_t n_pid, if (response) return 0; - rw_lock_rdlock(&_ipcp->state_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); /* awaken pending flow */ - rw_lock_wrlock(&_ap_instance->flows_lock); + pthread_rwlock_wrlock(&_ap_instance->flows_lock); in_fd = port_id_to_fd(port_id); if (in_fd < 0) { - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBGF("Could not find flow with port_id %d.", port_id); return -1; } if (_ap_instance->flows[in_fd].state != FLOW_PENDING) { - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBGF("Flow was not pending."); return -1; } @@ -460,8 +459,8 @@ static int ipcp_local_flow_alloc_resp(pid_t n_pid, _ap_instance->flows[in_fd].state = FLOW_NULL; _ap_instance->flows[in_fd].port_id = -1; _ap_instance->in_out[in_fd] = -1; - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); return -1; } @@ -472,24 +471,24 @@ static int ipcp_local_flow_alloc_resp(pid_t n_pid, out_fd = _ap_instance->in_out[in_fd]; if (out_fd < 0) { - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBGF("No pending local flow with port_id %d.", port_id); return -1; } if (_ap_instance->flows[out_fd].state != FLOW_PENDING) { /* FIXME: clean up other end */ - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBGF("Flow was not pending."); return -1; } _ap_instance->flows[out_fd].state = FLOW_ALLOCATED; - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); if ((ret = ipcp_flow_alloc_reply(getpid(), _ap_instance->flows[out_fd].port_id, @@ -509,13 +508,13 @@ static int ipcp_local_flow_dealloc(int port_id) int fd = -1; struct shm_ap_rbuff * rb; - rw_lock_rdlock(&_ipcp->state_lock); - rw_lock_wrlock(&_ap_instance->flows_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); + pthread_rwlock_wrlock(&_ap_instance->flows_lock); fd = port_id_to_fd(port_id); if (fd < 0) { - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBGF("Could not find flow with port_id %d.", port_id); return 0; } @@ -532,12 +531,12 @@ static int ipcp_local_flow_dealloc(int port_id) rb = _ap_instance->flows[fd].rb; _ap_instance->flows[fd].rb = NULL; - rw_lock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); if (rb != NULL) shm_ap_rbuff_close(rb); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBGF("Flow with port_id %d deallocated.", port_id); @@ -632,7 +631,7 @@ int main (int argc, char * argv[]) exit(1); } - rw_lock_wrlock(&_ipcp->state_lock); + pthread_rwlock_wrlock(&_ipcp->state_lock); pthread_sigmask(SIG_BLOCK, &sigset, NULL); @@ -640,7 +639,7 @@ int main (int argc, char * argv[]) pthread_sigmask(SIG_UNBLOCK, &sigset, NULL); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); pthread_join(_ap_instance->mainloop, NULL); diff --git a/src/ipcpd/shim-eth-llc/main.c b/src/ipcpd/shim-eth-llc/main.c index 7ff59cde..9bd6920b 100644 --- a/src/ipcpd/shim-eth-llc/main.c +++ b/src/ipcpd/shim-eth-llc/main.c @@ -36,7 +36,6 @@ #include #include #include -#include #define OUROBOROS_PREFIX "ipcpd/shim-eth-llc" @@ -104,7 +103,7 @@ struct eth_llc_ipcp_data { int tx_offset; struct eth_llc_flow flows[AP_MAX_FLOWS]; - rw_lock_t flows_lock; + pthread_rwlock_t flows_lock; pthread_t mainloop; pthread_t sdu_writer; @@ -159,7 +158,7 @@ struct eth_llc_ipcp_data * eth_llc_ipcp_data_create() return NULL; } - rw_lock_init(ð_llc_data->flows_lock); + pthread_rwlock_init(ð_llc_data->flows_lock, NULL); return eth_llc_data; } @@ -171,7 +170,7 @@ void eth_llc_ipcp_data_destroy() if (_ipcp == NULL) return; - rw_lock_wrlock(&_ipcp->state_lock); + pthread_rwlock_wrlock(&_ipcp->state_lock); if (_ipcp->state != IPCP_SHUTDOWN) LOG_WARN("Cleaning up while not in shutdown."); @@ -185,14 +184,14 @@ void eth_llc_ipcp_data_destroy() if (shim_data(_ipcp)->saps != NULL) bmp_destroy(shim_data(_ipcp)->saps); - rw_lock_wrlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_wrlock(&shim_data(_ipcp)->flows_lock); for (i = 0; i < AP_MAX_FLOWS; i ++) if (ipcp_flow(i)->rb != NULL) shm_ap_rbuff_close(ipcp_flow(i)->rb); - rw_lock_unlock(&shim_data(_ipcp)->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); free(_ipcp->data); } @@ -440,13 +439,13 @@ static int eth_llc_ipcp_port_req(uint8_t r_sap, ssize_t index = 0; int i; - rw_lock_wrlock(&_ipcp->state_lock); - rw_lock_wrlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_wrlock(&_ipcp->state_lock); + pthread_rwlock_wrlock(&shim_data(_ipcp)->flows_lock); index = bmp_allocate(shim_data(_ipcp)->indices); if (index < 0) { - rw_lock_unlock(&_ipcp->state_lock); - rw_lock_unlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&shim_data(_ipcp)->flows_lock); LOG_ERR("Out of free indices."); return -1; } @@ -458,8 +457,8 @@ static int eth_llc_ipcp_port_req(uint8_t r_sap, if (port_id < 0) { bmp_release(shim_data(_ipcp)->indices, index); - rw_lock_unlock(&_ipcp->state_lock); - rw_lock_unlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&shim_data(_ipcp)->flows_lock); LOG_ERR("Could not get port id from IRMd."); return -1; } @@ -472,8 +471,8 @@ static int eth_llc_ipcp_port_req(uint8_t r_sap, shim_data(_ipcp)->flows[index].r_addr[i] = r_addr[i]; } - rw_lock_unlock(&shim_data(_ipcp)->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBG("New flow request, port_id %d, remote SAP %d.", port_id, r_sap); @@ -490,20 +489,20 @@ static int eth_llc_ipcp_port_alloc_reply(uint8_t ssap, int port_id = -1; int i; - rw_lock_rdlock(&_ipcp->state_lock); - rw_lock_rdlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); + pthread_rwlock_rdlock(&shim_data(_ipcp)->flows_lock); index = sap_to_index(ssap); if (index < 0) { - rw_lock_unlock(&shim_data(_ipcp)->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_ERR("No flow found with that SAP."); return -1; /* -EFLOWNOTFOUND */ } if (ipcp_flow(index)->state != FLOW_PENDING) { - rw_lock_unlock(&shim_data(_ipcp)->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); return -1; /* -EFLOWNOTPENDING */ } @@ -519,8 +518,8 @@ static int eth_llc_ipcp_port_alloc_reply(uint8_t ssap, } } - rw_lock_unlock(&shim_data(_ipcp)->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBG("Flow reply, port_id %d, remote SAP %d.", port_id, dsap); @@ -540,13 +539,13 @@ static int eth_llc_ipcp_flow_dealloc_req(uint8_t ssap, int port_id = -1; int i = 0; - rw_lock_rdlock(&_ipcp->state_lock); - rw_lock_wrlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); + pthread_rwlock_wrlock(&shim_data(_ipcp)->flows_lock); i = sap_to_index(ssap); if (i < 0) { - rw_lock_unlock(&shim_data(_ipcp)->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_ERR("No flow found for remote deallocation request."); return 0; } @@ -554,8 +553,8 @@ static int eth_llc_ipcp_flow_dealloc_req(uint8_t ssap, port_id = ipcp_flow(i)->port_id; destroy_ipcp_flow(i); - rw_lock_unlock(&shim_data(_ipcp)->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); ipcp_flow_dealloc(0, port_id); @@ -633,14 +632,14 @@ static void * eth_llc_ipcp_sdu_reader(void * o) memset(br_addr, 0xff, MAC_SIZE * sizeof(uint8_t)); while (true) { - rw_lock_rdlock(&_ipcp->state_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); if (_ipcp->state != IPCP_ENROLLED) { - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); return (void *) 1; /* -ENOTENROLLED */ } - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); #if defined(PACKET_RX_RING) && defined(PACKET_TX_RING) header = (void *) shim_data(_ipcp)->rx_ring + @@ -709,13 +708,13 @@ static void * eth_llc_ipcp_sdu_reader(void * o) eth_llc_ipcp_mgmt_frame((uint8_t *) (buf + i), frame_len, src_mac); } else { - rw_lock_rdlock(&_ipcp->state_lock); - rw_lock_rdlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); + pthread_rwlock_rdlock(&shim_data(_ipcp)->flows_lock); j = addr_and_saps_to_index(src_mac, ssap, dsap); if (j < 0) { - rw_lock_unlock(&shim_data(_ipcp)->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBG("Received data for unknown flow."); #if defined(PACKET_RX_RING) && defined(PACKET_TX_RING) offset = (offset + 1) @@ -738,8 +737,8 @@ static void * eth_llc_ipcp_sdu_reader(void * o) while (shm_ap_rbuff_write(ipcp_flow(j)->rb, &e) < 0) ; - rw_lock_unlock(&shim_data(_ipcp)->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); } #if defined(PACKET_RX_RING) && defined(PACKET_TX_RING) offset = (offset + 1) & (SHM_BLOCKS_IN_MAP -1); @@ -765,10 +764,10 @@ static void * eth_llc_ipcp_sdu_writer(void * o) continue; } - rw_lock_rdlock(&_ipcp->state_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); if (_ipcp->state != IPCP_ENROLLED) { - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); return (void *) 1; /* -ENOTENROLLED */ } @@ -776,18 +775,18 @@ static void * eth_llc_ipcp_sdu_writer(void * o) shim_data(_ipcp)->dum, e->index); if (len <= 0) { - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); free(e); continue; } - rw_lock_rdlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_rdlock(&shim_data(_ipcp)->flows_lock); i = port_id_to_index(e->port_id); if (i < 0) { free(e); - rw_lock_unlock(&shim_data(_ipcp)->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); continue; } @@ -798,12 +797,12 @@ static void * eth_llc_ipcp_sdu_writer(void * o) dsap, ssap, buf, len)) LOG_ERR("Failed to send SDU."); - rw_lock_unlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_unlock(&shim_data(_ipcp)->flows_lock); if (shim_data(_ipcp)->dum != NULL) shm_release_du_buff(shim_data(_ipcp)->dum, e->index); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); } return (void *) 1; @@ -824,14 +823,14 @@ void ipcp_sig_handler(int sig, siginfo_t * info, void * c) LOG_DBG("Terminating by order of %d. Bye.", info->si_pid); - rw_lock_wrlock(&_ipcp->state_lock); + pthread_rwlock_wrlock(&_ipcp->state_lock); if (_ipcp->state == IPCP_ENROLLED) clean_threads = true; _ipcp->state = IPCP_SHUTDOWN; - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); if (clean_threads) { pthread_cancel(shim_data(_ipcp)->sdu_reader); @@ -964,10 +963,10 @@ static int eth_llc_ipcp_bootstrap(struct dif_config * conf) #endif - rw_lock_wrlock(&_ipcp->state_lock); + pthread_rwlock_wrlock(&_ipcp->state_lock); if (_ipcp->state != IPCP_INIT) { - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_ERR("IPCP in wrong state."); close(fd); return -1; @@ -989,7 +988,7 @@ static int eth_llc_ipcp_bootstrap(struct dif_config * conf) eth_llc_ipcp_sdu_writer, NULL); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBG("Bootstrapped shim IPCP over Ethernet with LLC with pid %d.", getpid()); @@ -999,21 +998,21 @@ static int eth_llc_ipcp_bootstrap(struct dif_config * conf) static int eth_llc_ipcp_name_reg(char * name) { - rw_lock_rdlock(&_ipcp->state_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); if (_ipcp->state != IPCP_ENROLLED) { - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBGF("Won't register with non-enrolled IPCP."); return -1; /* -ENOTENROLLED */ } if (ipcp_data_add_reg_entry(_ipcp->data, name)) { - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_ERR("Failed to add %s to local registry.", name); return -1; } - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBG("Registered %s.", name); @@ -1022,11 +1021,11 @@ static int eth_llc_ipcp_name_reg(char * name) static int eth_llc_ipcp_name_unreg(char * name) { - rw_lock_rdlock(&_ipcp->state_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); ipcp_data_del_reg_entry(_ipcp->data, name); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); return 0; } @@ -1054,11 +1053,11 @@ static int eth_llc_ipcp_flow_alloc(pid_t n_pid, if (rb == NULL) return -1; /* -ENORBUFF */ - rw_lock_wrlock(&_ipcp->state_lock); + pthread_rwlock_wrlock(&_ipcp->state_lock); if (_ipcp->state != IPCP_ENROLLED) { shm_ap_rbuff_close(rb); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBGF("Won't allocate flow with non-enrolled IPCP."); return -1; /* -ENOTENROLLED */ } @@ -1066,18 +1065,18 @@ static int eth_llc_ipcp_flow_alloc(pid_t n_pid, index = bmp_allocate(shim_data(_ipcp)->indices); if (index < 0) { shm_ap_rbuff_close(rb); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); return -1; } - rw_lock_wrlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_wrlock(&shim_data(_ipcp)->flows_lock); ssap = bmp_allocate(shim_data(_ipcp)->saps); if (ssap < 0) { shm_ap_rbuff_close(rb); bmp_release(shim_data(_ipcp)->indices, index); - rw_lock_unlock(&shim_data(_ipcp)->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); return -1; } @@ -1086,8 +1085,8 @@ static int eth_llc_ipcp_flow_alloc(pid_t n_pid, ipcp_flow(index)->rb = rb; shim_data(_ipcp)->flows[index].sap = ssap; - rw_lock_unlock(&shim_data(_ipcp)->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); memset(r_addr, 0xff, MAC_SIZE * sizeof(uint8_t)); @@ -1095,11 +1094,11 @@ static int eth_llc_ipcp_flow_alloc(pid_t n_pid, dst_name, src_ae_name) < 0) { LOG_DBGF("Port alloc returned -1."); - rw_lock_wrlock(&_ipcp->state_lock); - rw_lock_wrlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_wrlock(&_ipcp->state_lock); + pthread_rwlock_wrlock(&shim_data(_ipcp)->flows_lock); destroy_ipcp_flow(index); - rw_lock_unlock(&shim_data(_ipcp)->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); return -1; } @@ -1117,20 +1116,20 @@ static int eth_llc_ipcp_flow_alloc_resp(pid_t n_pid, int index = -1; uint8_t ssap = 0; - rw_lock_wrlock(&_ipcp->state_lock); - rw_lock_wrlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_wrlock(&_ipcp->state_lock); + pthread_rwlock_wrlock(&shim_data(_ipcp)->flows_lock); index = port_id_to_index(port_id); if (index < 0) { - rw_lock_unlock(&shim_data(_ipcp)->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBGF("Could not find flow with port_id %d.", port_id); return -1; } if (ipcp_flow(index)->state != FLOW_PENDING) { - rw_lock_unlock(&shim_data(_ipcp)->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBGF("Flow was not pending."); return -1; } @@ -1141,8 +1140,8 @@ static int eth_llc_ipcp_flow_alloc_resp(pid_t n_pid, ipcp_flow(index)->state = FLOW_NULL; ipcp_flow(index)->port_id = -1; bmp_release(shim_data(_ipcp)->indices, index); - rw_lock_unlock(&shim_data(_ipcp)->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); return -1; } @@ -1152,8 +1151,8 @@ static int eth_llc_ipcp_flow_alloc_resp(pid_t n_pid, ipcp_flow(index)->port_id = -1; shm_ap_rbuff_close(ipcp_flow(index)->rb); bmp_release(shim_data(_ipcp)->indices, index); - rw_lock_unlock(&shim_data(_ipcp)->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); return -1; } @@ -1161,18 +1160,18 @@ static int eth_llc_ipcp_flow_alloc_resp(pid_t n_pid, ipcp_flow(index)->rb = rb; shim_data(_ipcp)->flows[index].sap = ssap; - rw_lock_unlock(&shim_data(_ipcp)->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); if (eth_llc_ipcp_port_alloc_resp(shim_data(_ipcp)->flows[index].r_addr, shim_data(_ipcp)->flows[index].r_sap, ssap, response) < 0) { - rw_lock_rdlock(&_ipcp->state_lock); - rw_lock_wrlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); + pthread_rwlock_wrlock(&shim_data(_ipcp)->flows_lock); destroy_ipcp_flow(index); - rw_lock_unlock(&shim_data(_ipcp)->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBGF("Could not send response."); return -1; @@ -1191,13 +1190,13 @@ static int eth_llc_ipcp_flow_dealloc(int port_id) int i; int ret; - rw_lock_rdlock(&_ipcp->state_lock); - rw_lock_wrlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); + pthread_rwlock_wrlock(&shim_data(_ipcp)->flows_lock); index = port_id_to_index(port_id); if (index < 0) { - rw_lock_unlock(&shim_data(_ipcp)->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); return 0; } @@ -1208,10 +1207,12 @@ static int eth_llc_ipcp_flow_dealloc(int port_id) destroy_ipcp_flow(index); - rw_lock_unlock(&shim_data(_ipcp)->flows_lock); + pthread_rwlock_unlock(&shim_data(_ipcp)->flows_lock); ret = eth_llc_ipcp_port_dealloc(addr, sap); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); + if (eth_llc_ipcp_port_dealloc(addr, sap) < 0) + LOG_DBGF("Could not notify remote."); if (ret < 0) LOG_DBGF("Could not notify remote."); @@ -1289,7 +1290,7 @@ int main(int argc, char * argv[]) _ipcp->ops = ð_llc_ops; _ipcp->state = IPCP_INIT; - rw_lock_wrlock(&_ipcp->state_lock); + pthread_rwlock_wrlock(&_ipcp->state_lock); pthread_sigmask(SIG_BLOCK, &sigset, NULL); @@ -1298,7 +1299,7 @@ int main(int argc, char * argv[]) pthread_sigmask(SIG_UNBLOCK, &sigset, NULL); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); pthread_join(shim_data(_ipcp)->mainloop, NULL); diff --git a/src/ipcpd/shim-udp/main.c b/src/ipcpd/shim-udp/main.c index ccd8530b..5a51433e 100644 --- a/src/ipcpd/shim-udp/main.c +++ b/src/ipcpd/shim-udp/main.c @@ -34,7 +34,6 @@ #include #include #include -#include #define OUROBOROS_PREFIX "ipcpd/shim-udp" @@ -93,7 +92,7 @@ struct shim_ap_data { struct shm_ap_rbuff * rb; struct flow flows[AP_MAX_FLOWS]; - rw_lock_t flows_lock; + pthread_rwlock_t flows_lock; pthread_t mainloop; pthread_t sduloop; @@ -157,7 +156,7 @@ static int shim_ap_init(char * ap_name) _ap_instance->flows[i].state = FLOW_NULL; } - rw_lock_init(&_ap_instance->flows_lock); + pthread_rwlock_init(&_ap_instance->flows_lock, NULL); pthread_mutex_init(&_ap_instance->fd_set_lock, NULL); return 0; @@ -170,7 +169,7 @@ void shim_ap_fini() if (_ap_instance == NULL) return; - rw_lock_wrlock(&_ipcp->state_lock); + pthread_rwlock_wrlock(&_ipcp->state_lock); if (_ipcp->state != IPCP_SHUTDOWN) LOG_WARN("Cleaning up AP while not in shutdown."); @@ -184,14 +183,14 @@ void shim_ap_fini() if (_ap_instance->rb != NULL) shm_ap_rbuff_destroy(_ap_instance->rb); - rw_lock_wrlock(&_ap_instance->flows_lock); + pthread_rwlock_wrlock(&_ap_instance->flows_lock); for (i = 0; i < AP_MAX_FLOWS; i ++) if (_ap_instance->flows[i].rb != NULL) shm_ap_rbuff_close(_ap_instance->flows[i].rb); - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); free(_ap_instance); } @@ -215,14 +214,14 @@ static ssize_t ipcp_udp_flow_write(int fd, void * buf, size_t count) ssize_t index; struct rb_entry e; - rw_lock_rdlock(&_ipcp->state_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); if (_ipcp->state != IPCP_ENROLLED) { - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); return -1; /* -ENOTENROLLED */ } - rw_lock_rdlock(&_ap_instance->flows_lock); + pthread_rwlock_rdlock(&_ap_instance->flows_lock); while ((index = shm_create_du_buff(_ap_instance->dum, count + DU_BUFF_HEADSPACE + @@ -238,8 +237,8 @@ static ssize_t ipcp_udp_flow_write(int fd, void * buf, size_t count) while (shm_ap_rbuff_write(_ap_instance->flows[fd].rb, &e) < 0) ; - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); return 0; } @@ -477,8 +476,8 @@ static int ipcp_udp_port_req(struct sockaddr_in * c_saddr, } - rw_lock_rdlock(&_ipcp->state_lock); - rw_lock_wrlock(&_ap_instance->flows_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); + pthread_rwlock_wrlock(&_ap_instance->flows_lock); /* reply to IRM */ port_id = ipcp_flow_req_arr(getpid(), @@ -486,8 +485,8 @@ static int ipcp_udp_port_req(struct sockaddr_in * c_saddr, src_ae_name); if (port_id < 0) { - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_ERR("Could not get port id from IRMd"); close(fd); return -1; @@ -497,8 +496,8 @@ static int ipcp_udp_port_req(struct sockaddr_in * c_saddr, _ap_instance->flows[fd].rb = NULL; _ap_instance->flows[fd].state = FLOW_PENDING; - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBGF("Pending allocation request, port_id %d, UDP port (%d, %d).", port_id, ntohs(f_saddr.sin_port), ntohs(c_saddr->sin_port)); @@ -520,20 +519,20 @@ static int ipcp_udp_port_alloc_reply(int src_udp_port, LOG_DBGF("Received reply for flow on udp port %d.", ntohs(dst_udp_port)); - rw_lock_rdlock(&_ipcp->state_lock); - rw_lock_rdlock(&_ap_instance->flows_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); + pthread_rwlock_rdlock(&_ap_instance->flows_lock); fd = udp_port_to_fd(dst_udp_port); if (fd == -1) { - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBGF("Unknown flow on UDP port %d.", dst_udp_port); return -1; /* -EUNKNOWNFLOW */ } if (_ap_instance->flows[fd].state != FLOW_PENDING) { - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBGF("Flow on UDP port %d not pending.", dst_udp_port); return -1; /* -EFLOWNOTPENDING */ } @@ -550,8 +549,8 @@ static int ipcp_udp_port_alloc_reply(int src_udp_port, if (getpeername(fd, (struct sockaddr *) &t_saddr, &t_saddr_len) < 0) { - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBGF("Flow with port_id %d has no peer.", port_id); return -1; } @@ -562,8 +561,8 @@ static int ipcp_udp_port_alloc_reply(int src_udp_port, if (connect(fd, (struct sockaddr *) &t_saddr, sizeof(t_saddr)) < 0) { - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); close(fd); return -1; } @@ -571,8 +570,8 @@ static int ipcp_udp_port_alloc_reply(int src_udp_port, _ap_instance->flows[fd].state = FLOW_ALLOCATED; } - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); if ((ret = ipcp_flow_alloc_reply(getpid(), @@ -595,13 +594,13 @@ static int ipcp_udp_flow_dealloc_req(int udp_port) struct shm_ap_rbuff * rb; - rw_lock_rdlock(&_ipcp->state_lock); - rw_lock_wrlock(&_ap_instance->flows_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); + pthread_rwlock_wrlock(&_ap_instance->flows_lock); fd = udp_port_to_fd(udp_port); if (fd < 0) { - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBGF("Could not find flow on UDP port %d.", ntohs(udp_port)); return 0; @@ -615,12 +614,12 @@ static int ipcp_udp_flow_dealloc_req(int udp_port) rb = _ap_instance->flows[fd].rb; _ap_instance->flows[fd].rb = NULL; - rw_lock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); if (rb != NULL) shm_ap_rbuff_close(rb); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); ipcp_flow_dealloc(0, port_id); @@ -642,16 +641,16 @@ static void * ipcp_udp_listener() int sfd = 0; shim_udp_msg_t * msg = NULL; - rw_lock_rdlock(&_ipcp->state_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); if (_ipcp->state != IPCP_ENROLLED) { - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); return (void *) 1; /* -ENOTENROLLED */ } sfd = shim_data(_ipcp)->s_fd; - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); memset(&buf, 0, SHIM_UDP_MSG_SIZE); n = sizeof(c_saddr); @@ -715,14 +714,14 @@ static void * ipcp_udp_sdu_reader() while (true) { struct timeval tv = {0, FD_UPDATE_TIMEOUT}; - rw_lock_rdlock(&_ipcp->state_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); if (_ipcp->state != IPCP_ENROLLED) { - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); return (void *) 1; /* -ENOTENROLLED */ } - rw_lock_rdlock(&_ap_instance->flows_lock); + pthread_rwlock_rdlock(&_ap_instance->flows_lock); pthread_mutex_lock(&_ap_instance->fd_set_lock); @@ -731,8 +730,8 @@ static void * ipcp_udp_sdu_reader() pthread_mutex_unlock(&_ap_instance->fd_set_lock); - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); if (select(FD_SETSIZE, &read_fds, NULL, NULL, &tv) <= 0) { continue; @@ -776,10 +775,10 @@ static void * ipcp_udp_sdu_loop(void * o) continue; } - rw_lock_rdlock(&_ipcp->state_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); if (_ipcp->state != IPCP_ENROLLED) { - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); return (void *) 1; /* -ENOTENROLLED */ } @@ -787,17 +786,17 @@ static void * ipcp_udp_sdu_loop(void * o) _ap_instance->dum, e->index); if (len <= 0) { - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); free(e); continue; } - rw_lock_rdlock(&_ap_instance->flows_lock); + pthread_rwlock_rdlock(&_ap_instance->flows_lock); fd = port_id_to_fd(e->port_id); - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); if (fd == -1) { free(e); @@ -807,12 +806,12 @@ static void * ipcp_udp_sdu_loop(void * o) if (send(fd, buf, len, 0) < 0) LOG_ERR("Failed to send SDU."); - rw_lock_rdlock(&_ipcp->state_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); if (_ap_instance->dum != NULL) shm_release_du_buff(_ap_instance->dum, e->index); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); } return (void *) 1; @@ -833,14 +832,14 @@ void ipcp_sig_handler(int sig, siginfo_t * info, void * c) LOG_DBG("Terminating by order of %d. Bye.", info->si_pid); - rw_lock_wrlock(&_ipcp->state_lock); + pthread_rwlock_wrlock(&_ipcp->state_lock); if (_ipcp->state == IPCP_ENROLLED) clean_threads = true; _ipcp->state = IPCP_SHUTDOWN; - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); if (clean_threads) { pthread_cancel(_ap_instance->handler); @@ -925,10 +924,10 @@ static int ipcp_udp_bootstrap(struct dif_config * conf) return -1; } - rw_lock_wrlock(&_ipcp->state_lock); + pthread_rwlock_wrlock(&_ipcp->state_lock); if (_ipcp->state != IPCP_INIT) { - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_ERR("IPCP in wrong state."); close(fd); return -1; @@ -956,7 +955,7 @@ static int ipcp_udp_bootstrap(struct dif_config * conf) ipcp_udp_sdu_loop, NULL); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBG("Bootstrapped shim IPCP over UDP with pid %d.", getpid()); @@ -1107,16 +1106,16 @@ static int ipcp_udp_name_reg(char * name) return -1; } - rw_lock_rdlock(&_ipcp->state_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); if (_ipcp->state != IPCP_ENROLLED) { - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBGF("Won't register with non-enrolled IPCP."); return -1; /* -ENOTENROLLED */ } if (ipcp_data_add_reg_entry(_ipcp->data, name)) { - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_ERR("Failed to add %s to local registry.", name); return -1; } @@ -1126,7 +1125,7 @@ static int ipcp_udp_name_reg(char * name) dns_addr = shim_data(_ipcp)->dns_addr; - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); if (dns_addr != 0) { ip_addr = shim_data(_ipcp)->ip_addr; @@ -1145,14 +1144,14 @@ static int ipcp_udp_name_reg(char * name) dnsstr, name, DNS_TTL, ipstr); if (ddns_send(cmd)) { - rw_lock_rdlock(&_ipcp->state_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); ipcp_data_del_reg_entry(_ipcp->data, name); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); return -1; } } #else - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); #endif LOG_DBG("Registered %s.", name); @@ -1176,17 +1175,17 @@ static int ipcp_udp_name_unreg(char * name) #ifdef CONFIG_OUROBOROS_ENABLE_DNS /* unregister application with DNS server */ - rw_lock_rdlock(&_ipcp->state_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); if (_ipcp->state != IPCP_ENROLLED) { - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBGF("IPCP is not enrolled"); return -1; /* -ENOTENROLLED */ } dns_addr = shim_data(_ipcp)->dns_addr; - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); if (dns_addr != 0) { if (inet_ntop(AF_INET, &dns_addr, dnsstr, INET_ADDRSTRLEN) @@ -1200,11 +1199,11 @@ static int ipcp_udp_name_unreg(char * name) } #endif - rw_lock_rdlock(&_ipcp->state_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); ipcp_data_del_reg_entry(_ipcp->data, name); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); return 0; } @@ -1262,10 +1261,10 @@ static int ipcp_udp_flow_alloc(pid_t n_pid, return -1; } - rw_lock_rdlock(&_ipcp->state_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); if (_ipcp->state != IPCP_ENROLLED) { - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBGF("Won't allocate flow with non-enrolled IPCP."); close(fd); return -1; /* -ENOTENROLLED */ @@ -1274,7 +1273,7 @@ static int ipcp_udp_flow_alloc(pid_t n_pid, #ifdef CONFIG_OUROBOROS_ENABLE_DNS dns_addr = shim_data(_ipcp)->dns_addr; - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); if (dns_addr != 0) { ip_addr = ddns_resolve(dst_name, dns_addr); @@ -1308,13 +1307,13 @@ static int ipcp_udp_flow_alloc(pid_t n_pid, return -1; } - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBGF("Pending flow with port_id %d on UDP port %d.", port_id, ntohs(f_saddr.sin_port)); - rw_lock_rdlock(&_ipcp->state_lock); - rw_lock_wrlock(&_ap_instance->flows_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); + pthread_rwlock_wrlock(&_ap_instance->flows_lock); set_fd(fd); @@ -1322,16 +1321,16 @@ static int ipcp_udp_flow_alloc(pid_t n_pid, _ap_instance->flows[fd].state = FLOW_PENDING; _ap_instance->flows[fd].rb = rb; - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); if (ipcp_udp_port_alloc(ip_addr, f_saddr.sin_port, dst_name, src_ae_name) < 0) { LOG_DBGF("Port alloc returned -1."); - rw_lock_rdlock(&_ipcp->state_lock); - rw_lock_wrlock(&_ap_instance->flows_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); + pthread_rwlock_wrlock(&_ap_instance->flows_lock); clr_fd(fd); @@ -1340,8 +1339,8 @@ static int ipcp_udp_flow_alloc(pid_t n_pid, shm_ap_rbuff_close(_ap_instance->flows[fd].rb); _ap_instance->flows[fd].rb = NULL; - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); close(fd); return -1; } @@ -1362,23 +1361,23 @@ static int ipcp_udp_flow_alloc_resp(pid_t n_pid, if (response) return 0; - rw_lock_rdlock(&_ipcp->state_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); /* awaken pending flow */ - rw_lock_wrlock(&_ap_instance->flows_lock); + pthread_rwlock_wrlock(&_ap_instance->flows_lock); fd = port_id_to_fd(port_id); if (fd < 0) { - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBGF("Could not find flow with port_id %d.", port_id); return -1; } if (_ap_instance->flows[fd].state != FLOW_PENDING) { - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBGF("Flow was not pending."); return -1; } @@ -1388,13 +1387,13 @@ static int ipcp_udp_flow_alloc_resp(pid_t n_pid, LOG_ERR("Could not open N + 1 ringbuffer."); _ap_instance->flows[fd].state = FLOW_NULL; _ap_instance->flows[fd].port_id = -1; - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); return -1; } - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); if (getsockname(fd, (struct sockaddr *) &f_saddr, &len) < 0) { LOG_DBGF("Flow with port_id %d has no socket.", port_id); @@ -1406,23 +1405,23 @@ static int ipcp_udp_flow_alloc_resp(pid_t n_pid, return -1; } - rw_lock_rdlock(&_ipcp->state_lock); - rw_lock_wrlock(&_ap_instance->flows_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); + pthread_rwlock_wrlock(&_ap_instance->flows_lock); _ap_instance->flows[fd].state = FLOW_ALLOCATED; _ap_instance->flows[fd].rb = rb; set_fd(fd); - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); if (ipcp_udp_port_alloc_resp(r_saddr.sin_addr.s_addr, f_saddr.sin_port, r_saddr.sin_port, response) < 0) { - rw_lock_rdlock(&_ipcp->state_lock); - rw_lock_wrlock(&_ap_instance->flows_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); + pthread_rwlock_wrlock(&_ap_instance->flows_lock); _ap_instance->flows[fd].state = FLOW_NULL; shm_ap_rbuff_close(_ap_instance->flows[fd].rb); @@ -1430,8 +1429,8 @@ static int ipcp_udp_flow_alloc_resp(pid_t n_pid, clr_fd(fd); - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBGF("Could not send response."); return -1; @@ -1450,13 +1449,13 @@ static int ipcp_udp_flow_dealloc(int port_id) struct sockaddr_in r_saddr; socklen_t r_saddr_len = sizeof(r_saddr); - rw_lock_rdlock(&_ipcp->state_lock); - rw_lock_wrlock(&_ap_instance->flows_lock); + pthread_rwlock_rdlock(&_ipcp->state_lock); + pthread_rwlock_wrlock(&_ap_instance->flows_lock); fd = port_id_to_fd(port_id); if (fd < 0) { - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBGF("Could not find flow with port_id %d.", port_id); return 0; } @@ -1468,13 +1467,13 @@ static int ipcp_udp_flow_dealloc(int port_id) clr_fd(fd); - rw_lock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); if (rb != NULL) shm_ap_rbuff_close(rb); if (getpeername(fd, (struct sockaddr *) &r_saddr, &r_saddr_len) < 0) { - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); LOG_DBGF("Flow with port_id %d has no peer.", port_id); close(fd); return 0; @@ -1484,7 +1483,7 @@ static int ipcp_udp_flow_dealloc(int port_id) r_saddr.sin_port = LISTEN_PORT; if (connect(fd, (struct sockaddr *) &r_saddr, sizeof(r_saddr)) < 0) { - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); close(fd); return 0 ; } @@ -1492,12 +1491,12 @@ static int ipcp_udp_flow_dealloc(int port_id) if (ipcp_udp_port_dealloc(r_saddr.sin_addr.s_addr, remote_udp) < 0) { LOG_DBGF("Could not notify remote."); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); close(fd); return 0; } - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); close(fd); @@ -1590,7 +1589,7 @@ int main (int argc, char * argv[]) exit(1); } - rw_lock_wrlock(&_ipcp->state_lock); + pthread_rwlock_wrlock(&_ipcp->state_lock); pthread_sigmask(SIG_BLOCK, &sigset, NULL); @@ -1598,7 +1597,7 @@ int main (int argc, char * argv[]) pthread_sigmask(SIG_UNBLOCK, &sigset, NULL); - rw_lock_unlock(&_ipcp->state_lock); + pthread_rwlock_unlock(&_ipcp->state_lock); pthread_join(_ap_instance->mainloop, NULL); diff --git a/src/irmd/main.c b/src/irmd/main.c index 5f7c1ddc..50055c4d 100644 --- a/src/irmd/main.c +++ b/src/irmd/main.c @@ -36,7 +36,6 @@ #include #include #include -#include #include #include "utils.h" @@ -113,20 +112,21 @@ struct irm { /* FIXME: list of ipcps could be merged with registered names */ struct list_head ipcps; struct list_head reg_names; - rw_lock_t reg_lock; + pthread_rwlock_t reg_lock; /* keep track of all flows in this processing system */ struct bmp * port_ids; /* maps port_ids to pid pair */ struct list_head port_map; - rw_lock_t flows_lock; + pthread_rwlock_t flows_lock; struct shm_du_map * dum; pthread_t * threadpool; int sockfd; - rw_lock_t state_lock; + pthread_rwlock_t state_lock; pthread_t cleanup_flows; + pthread_t shm_sanitize; } * instance = NULL; static struct port_map_entry * port_map_entry_create() @@ -435,18 +435,18 @@ static pid_t create_ipcp(char * ap_name, pid_t pid; struct ipcp_entry * tmp = NULL; - rw_lock_rdlock(&instance->state_lock); + pthread_rwlock_rdlock(&instance->state_lock); pid = ipcp_create(ap_name, ipcp_type); if (pid == -1) { - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->state_lock); LOG_ERR("Failed to create IPCP."); return -1; } tmp = ipcp_entry_create(); if (tmp == NULL) { - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->state_lock); return -1; } @@ -455,25 +455,25 @@ static pid_t create_ipcp(char * ap_name, tmp->api = instance_name_create(); if (tmp->api == NULL) { ipcp_entry_destroy(tmp); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->state_lock); return -1; } if(instance_name_init_from(tmp->api, ap_name, pid) == NULL) { instance_name_destroy(tmp->api); ipcp_entry_destroy(tmp); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->state_lock); return -1; } tmp->dif_name = NULL; - rw_lock_wrlock(&instance->reg_lock); + pthread_rwlock_wrlock(&instance->reg_lock); list_add(&tmp->next, &instance->ipcps); - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); LOG_INFO("Created IPCP %s-%d.", ap_name, pid); @@ -489,15 +489,10 @@ static int destroy_ipcp(instance_name_t * api) if (api == NULL) return 0; - rw_lock_rdlock(&instance->state_lock); - rw_lock_wrlock(&instance->reg_lock); - if (api->id == 0) api = get_ipcp_by_name(api->name); if (api == NULL) { - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); LOG_ERR("No such IPCP in the system."); return 0; } @@ -516,8 +511,6 @@ static int destroy_ipcp(instance_name_t * api) } } - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); LOG_INFO("Destroyed IPCP %d.", pid); @@ -529,46 +522,46 @@ static int bootstrap_ipcp(instance_name_t * api, { struct ipcp_entry * entry = NULL; - rw_lock_rdlock(&instance->state_lock); - rw_lock_wrlock(&instance->reg_lock); + pthread_rwlock_rdlock(&instance->state_lock); + pthread_rwlock_wrlock(&instance->reg_lock); if (api->id == 0) api = get_ipcp_by_name(api->name); if (api == NULL) { - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); LOG_ERR("No such IPCP in the system."); return -1; } entry = get_ipcp_entry_by_name(api); if (entry == NULL) { - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); LOG_ERR("No such IPCP."); return -1; } entry->dif_name = strdup(conf->dif_name); if (entry->dif_name == NULL) { - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); LOG_ERR("Failed to strdup."); return -1; } if (ipcp_bootstrap(entry->api->id, conf)) { - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); LOG_ERR("Could not bootstrap IPCP."); free(entry->dif_name); entry->dif_name = NULL; return -1; } - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); LOG_INFO("Bootstrapped IPCP %s-%d in DIF %s.", api->name, api->id, conf->dif_name); @@ -584,21 +577,21 @@ static int enroll_ipcp(instance_name_t * api, ssize_t n_1_difs_size = 0; struct ipcp_entry * entry = NULL; - rw_lock_rdlock(&instance->state_lock); - rw_lock_rdlock(&instance->reg_lock); + pthread_rwlock_rdlock(&instance->state_lock); + pthread_rwlock_rdlock(&instance->reg_lock); entry = get_ipcp_entry_by_name(api); if (entry == NULL) { - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); LOG_ERR("No such IPCP."); return -1; } entry->dif_name = strdup(dif_name); if (entry->dif_name == NULL) { - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); LOG_ERR("Failed to strdup."); return -1; } @@ -607,8 +600,8 @@ static int enroll_ipcp(instance_name_t * api, if (member == NULL) { free(entry->dif_name); entry->dif_name = NULL; - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); return -1; } @@ -616,8 +609,8 @@ static int enroll_ipcp(instance_name_t * api, if (n_1_difs_size < 1) { free(entry->dif_name); entry->dif_name = NULL; - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); LOG_ERR("Could not find N-1 DIFs."); return -1; } @@ -625,14 +618,14 @@ static int enroll_ipcp(instance_name_t * api, if (ipcp_enroll(api->id, member, n_1_difs[0])) { free(entry->dif_name); entry->dif_name = NULL; - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); LOG_ERR("Could not enroll IPCP."); return -1; } - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); LOG_INFO("Enrolled IPCP %s-%d in DIF %s.", api->name, api->id, dif_name); @@ -657,25 +650,25 @@ static int ap_reg(char * name, instance_name_t * api = NULL; char ** argv_dup = NULL; - rw_lock_rdlock(&instance->state_lock); - rw_lock_wrlock(&instance->reg_lock); + pthread_rwlock_rdlock(&instance->state_lock); + pthread_rwlock_wrlock(&instance->reg_lock); if (instance->ipcps.next == NULL) { - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); return -1; } api = instance_name_create(); if (api == NULL) { - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); return -1; } if (instance_name_init_from(api, path_strip(ap_name), ap_id) == NULL) { - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); instance_name_destroy(api); return -1; } @@ -683,8 +676,8 @@ static int ap_reg(char * name, /* check if this name is already registered */ rne = get_reg_name_entry_by_name(name); if (rne != NULL) { - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); instance_name_destroy(api); return -1; /* can only register one instance for now */ } @@ -711,8 +704,8 @@ static int ap_reg(char * name, } } if (ret == 0) { - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); instance_name_destroy(api); return -1; } @@ -735,8 +728,8 @@ static int ap_reg(char * name, < 0) LOG_DBGF("Failed to add application %s.", api->name); - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); return ret; } @@ -756,8 +749,8 @@ static int ap_unreg(char * name, if (name == NULL || len == 0 || difs == NULL || difs[0] == NULL) return -1; - rw_lock_rdlock(&instance->state_lock); - rw_lock_wrlock(&instance->reg_lock); + pthread_rwlock_rdlock(&instance->state_lock); + pthread_rwlock_wrlock(&instance->reg_lock); if (!hard && strcmp(difs[0], "*") != 0) { LOG_INFO("Unregistration not complete yet."); @@ -787,8 +780,8 @@ static int ap_unreg(char * name, reg_name_entry_del_name(rne->name); - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); return ret; } @@ -800,13 +793,13 @@ static struct port_map_entry * flow_accept(pid_t pid, struct port_map_entry * pme; struct reg_name_entry * rne = NULL; - rw_lock_rdlock(&instance->state_lock); - rw_lock_rdlock(&instance->reg_lock); + pthread_rwlock_rdlock(&instance->state_lock); + pthread_rwlock_wrlock(&instance->reg_lock); rne = get_reg_name_entry_by_ap_name(srv_ap_name); if (rne == NULL) { - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); LOG_DBGF("AP %s is unknown.", srv_ap_name); return NULL; } @@ -814,16 +807,16 @@ static struct port_map_entry * flow_accept(pid_t pid, if (rne->api->id == 0) { rne->api->id = pid; } else if (rne->api->id != pid) { - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); LOG_DBGF("Can only register one instance."); LOG_MISSING; return NULL; } if (rne->accept) { - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); LOG_DBGF("This AP still has a pending accept()."); return NULL; } @@ -833,18 +826,17 @@ static struct port_map_entry * flow_accept(pid_t pid, pthread_cond_broadcast(&rne->acc_signal); - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); pthread_mutex_lock(&rne->acc_lock); pthread_cleanup_push((void(*)(void *))pthread_mutex_unlock, - (void*) &rne->acc_lock); + (void *) &rne->acc_lock); while (rne->flow_arrived == -1) pthread_cond_wait(&rne->acc_arr_signal, &rne->acc_lock); - pthread_mutex_unlock(&rne->acc_lock); - pthread_cleanup_pop(0); + pthread_cleanup_pop(true); pthread_mutex_lock(&rne->acc_lock); @@ -856,13 +848,13 @@ static struct port_map_entry * flow_accept(pid_t pid, pthread_mutex_unlock(&rne->acc_lock); - rw_lock_rdlock(&instance->state_lock); - rw_lock_rdlock(&instance->flows_lock); + pthread_rwlock_rdlock(&instance->state_lock); + pthread_rwlock_rdlock(&instance->flows_lock); pme = get_port_map_entry_n(pid); if (pme == NULL) { - rw_lock_unlock(&instance->flows_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->flows_lock); + pthread_rwlock_unlock(&instance->state_lock); LOG_ERR("Port_id was not created yet."); return NULL; } @@ -870,8 +862,8 @@ static struct port_map_entry * flow_accept(pid_t pid, if (dst_ae_name != NULL) *dst_ae_name = rne->req_ae_name; - rw_lock_unlock(&instance->flows_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->flows_lock); + pthread_rwlock_unlock(&instance->state_lock); return pme; } @@ -884,20 +876,20 @@ static int flow_alloc_resp(pid_t n_pid, struct reg_name_entry * rne = NULL; int ret = -1; - rw_lock_rdlock(&instance->state_lock); - rw_lock_rdlock(&instance->reg_lock); + pthread_rwlock_rdlock(&instance->state_lock); + pthread_rwlock_rdlock(&instance->reg_lock); rne = get_reg_name_entry_by_id(n_pid); if (rne == NULL) { - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); return -1; } /* FIXME: check all instances associated with the name */ if (!rne->accept) { - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); LOG_ERR("No process listening for this name."); return -1; } @@ -914,28 +906,30 @@ static int flow_alloc_resp(pid_t n_pid, pthread_mutex_unlock(&rne->acc_lock); - rw_lock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->reg_lock); if (!response) { - rw_lock_wrlock(&instance->flows_lock); + pthread_rwlock_wrlock(&instance->flows_lock); pme = get_port_map_entry(port_id); if (pme == NULL) { - rw_lock_unlock(&instance->flows_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->flows_lock); + pthread_rwlock_unlock(&instance->state_lock); return -1; } pme->state = FLOW_ALLOCATED; + + pthread_rwlock_unlock(&instance->flows_lock); + ret = ipcp_flow_alloc_resp(pme->n_1_pid, port_id, pme->n_pid, response); - rw_lock_unlock(&instance->flows_lock); } - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->state_lock); return ret; } @@ -962,30 +956,30 @@ static struct port_map_entry * flow_alloc(pid_t pid, if (clock_gettime(CLOCK_MONOTONIC, &pme->t0) < 0) LOG_WARN("Failed to set timestamp."); - rw_lock_rdlock(&instance->state_lock); - rw_lock_rdlock(&instance->reg_lock); + pthread_rwlock_rdlock(&instance->state_lock); + pthread_rwlock_rdlock(&instance->reg_lock); if (qos != NULL) dif_name = qos->dif_name; ipcp = get_ipcp_by_dst_name(dst_name, dif_name); if (ipcp == NULL) { - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); LOG_DBG("Unknown DIF name."); return NULL; } - rw_lock_unlock(&instance->reg_lock); - rw_lock_wrlock(&instance->flows_lock); + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_wrlock(&instance->flows_lock); pme->port_id = bmp_allocate(instance->port_ids); pme->n_1_pid = ipcp->id; list_add(&pme->next, &instance->port_map); - rw_lock_unlock(&instance->flows_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->flows_lock); + pthread_rwlock_unlock(&instance->state_lock); if (ipcp_flow_alloc(ipcp->id, pme->port_id, @@ -993,12 +987,12 @@ static struct port_map_entry * flow_alloc(pid_t pid, dst_name, src_ae_name, QOS_CUBE_BE) < 0) { - rw_lock_rdlock(&instance->state_lock); - rw_lock_wrlock(&instance->flows_lock); + pthread_rwlock_rdlock(&instance->state_lock); + pthread_rwlock_wrlock(&instance->flows_lock); list_del(&pme->next); bmp_release(instance->port_ids, pme->port_id); - rw_lock_unlock(&instance->flows_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->flows_lock); + pthread_rwlock_unlock(&instance->state_lock); free(pme); return NULL; } @@ -1010,30 +1004,30 @@ static int flow_alloc_res(int port_id) { struct port_map_entry * e; - rw_lock_rdlock(&instance->state_lock); - rw_lock_rdlock(&instance->flows_lock); + pthread_rwlock_rdlock(&instance->state_lock); + pthread_rwlock_rdlock(&instance->flows_lock); e = get_port_map_entry(port_id); if (e == NULL) { - rw_lock_unlock(&instance->flows_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->flows_lock); + pthread_rwlock_unlock(&instance->state_lock); return -1; } if (e->state == FLOW_NULL) { - rw_lock_unlock(&instance->flows_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->flows_lock); + pthread_rwlock_unlock(&instance->state_lock); return -1; } if (e->state == FLOW_ALLOCATED) { - rw_lock_unlock(&instance->flows_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->flows_lock); + pthread_rwlock_unlock(&instance->state_lock); return 0; } - rw_lock_unlock(&instance->flows_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->flows_lock); + pthread_rwlock_unlock(&instance->state_lock); while (true) { pthread_mutex_lock(&e->res_lock); @@ -1042,35 +1036,34 @@ static int flow_alloc_res(int port_id) pthread_cond_wait(&e->res_signal, &e->res_lock); - pthread_mutex_unlock(&e->res_lock); - pthread_cleanup_pop(0); + pthread_cleanup_pop(true); - rw_lock_rdlock(&instance->state_lock); - rw_lock_wrlock(&instance->flows_lock); + pthread_rwlock_rdlock(&instance->state_lock); + pthread_rwlock_wrlock(&instance->flows_lock); e = get_port_map_entry(port_id); if (e == NULL) { - rw_lock_unlock(&instance->flows_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->flows_lock); + pthread_rwlock_unlock(&instance->state_lock); return -1; } if (e->state == FLOW_ALLOCATED) { - rw_lock_unlock(&instance->flows_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->flows_lock); + pthread_rwlock_unlock(&instance->state_lock); return 0; } if (e->state == FLOW_NULL) { /* don't release the port_id, AP has to call dealloc */ list_del(&e->next); - rw_lock_unlock(&instance->flows_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->flows_lock); + pthread_rwlock_unlock(&instance->state_lock); free(e); return -1; } /* still pending, spurious wake */ - rw_lock_unlock(&instance->flows_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->flows_lock); + pthread_rwlock_unlock(&instance->state_lock); } return 0; @@ -1083,15 +1076,14 @@ static int flow_dealloc(int port_id) struct port_map_entry * e = NULL; - rw_lock_rdlock(&instance->state_lock); - rw_lock_wrlock(&instance->flows_lock); - + pthread_rwlock_rdlock(&instance->state_lock); + pthread_rwlock_wrlock(&instance->flows_lock); bmp_release(instance->port_ids, port_id); e = get_port_map_entry(port_id); if (e == NULL) { - rw_lock_unlock(&instance->flows_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->flows_lock); + pthread_rwlock_unlock(&instance->state_lock); return 0; } @@ -1099,10 +1091,11 @@ static int flow_dealloc(int port_id) list_del(&e->next); + pthread_rwlock_unlock(&instance->flows_lock); + ret = ipcp_flow_dealloc(n_1_pid, port_id); - rw_lock_unlock(&instance->flows_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->state_lock); free(e); @@ -1148,17 +1141,17 @@ static struct port_map_entry * flow_req_arr(pid_t pid, pme->state = FLOW_PENDING; pme->n_1_pid = pid; - rw_lock_rdlock(&instance->state_lock); - rw_lock_rdlock(&instance->reg_lock); - rw_lock_wrlock(&instance->flows_lock); + pthread_rwlock_rdlock(&instance->state_lock); + pthread_rwlock_rdlock(&instance->reg_lock); + pthread_rwlock_wrlock(&instance->flows_lock); pme->port_id = bmp_allocate(instance->port_ids); rne = get_reg_name_entry_by_name(dst_name); if (rne == NULL) { - rw_lock_unlock(&instance->flows_lock); - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->flows_lock); + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); LOG_DBGF("Destination name %s unknown.", dst_name); free(pme); return NULL; @@ -1168,13 +1161,20 @@ static struct port_map_entry * flow_req_arr(pid_t pid, list_add(&pme->next, &instance->port_map); + pthread_rwlock_unlock(&instance->flows_lock); + pthread_mutex_lock(&rne->acc_lock); + pthread_cleanup_push((void(*)(void *)) pthread_mutex_unlock, + (void *) &rne->acc_lock); + rne->req_ae_name = ae_name; if (rne->accept == false) { if (rne->autoexec) { + pthread_rwlock_wrlock(&instance->flows_lock); pme->n_pid = auto_execute(rne->api->name, rne->argv); + pthread_rwlock_unlock(&instance->flows_lock); while (rne->accept == false) pthread_cond_wait(&rne->acc_signal, &rne->acc_lock); @@ -1188,21 +1188,19 @@ static struct port_map_entry * flow_req_arr(pid_t pid, rne->flow_arrived = 0; - pthread_mutex_unlock(&rne->acc_lock); - if (pthread_cond_signal(&rne->acc_arr_signal)) LOG_ERR("Failed to send signal."); + pthread_cleanup_pop(true); + while (acc_wait) { - sched_yield(); pthread_mutex_lock(&rne->acc_lock); acc_wait = (rne->flow_arrived != -1); pthread_mutex_unlock(&rne->acc_lock); } - rw_lock_unlock(&instance->flows_lock); - rw_lock_unlock(&instance->reg_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); return pme; } @@ -1212,13 +1210,13 @@ static int flow_alloc_reply(int port_id, { struct port_map_entry * e; - rw_lock_rdlock(&instance->state_lock); - rw_lock_rdlock(&instance->flows_lock); + pthread_rwlock_rdlock(&instance->state_lock); + pthread_rwlock_rdlock(&instance->flows_lock); e = get_port_map_entry(port_id); if (e == NULL) { - rw_lock_unlock(&instance->flows_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->flows_lock); + pthread_rwlock_unlock(&instance->state_lock); return -1; } @@ -1235,8 +1233,8 @@ static int flow_alloc_reply(int port_id, pthread_mutex_unlock(&e->res_lock); - rw_lock_unlock(&instance->flows_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->flows_lock); + pthread_rwlock_unlock(&instance->state_lock); return 0; } @@ -1245,48 +1243,46 @@ static int flow_dealloc_ipcp(int port_id) { struct port_map_entry * e = NULL; - rw_lock_rdlock(&instance->state_lock); - rw_lock_wrlock(&instance->flows_lock); + pthread_rwlock_rdlock(&instance->state_lock); + pthread_rwlock_wrlock(&instance->flows_lock); e = get_port_map_entry(port_id); if (e == NULL) { - rw_lock_unlock(&instance->flows_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->flows_lock); + pthread_rwlock_unlock(&instance->state_lock); return 0; } list_del(&e->next); - rw_lock_unlock(&instance->flows_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->flows_lock); + pthread_rwlock_unlock(&instance->state_lock); free(e); return 0; } -static void irm_destroy(struct irm * irm) +static void irm_destroy() { struct list_head * h; struct list_head * t; - if (irm == NULL) - return; - - rw_lock_wrlock(&irm->state_lock); + pthread_rwlock_wrlock(&instance->state_lock); + pthread_rwlock_wrlock(&instance->reg_lock); - if (irm->threadpool != NULL) - free(irm->threadpool); + if (instance->threadpool != NULL) + free(instance->threadpool); - if (irm->port_ids != NULL) - bmp_destroy(irm->port_ids); + if (instance->port_ids != NULL) + bmp_destroy(instance->port_ids); /* clear the lists */ - list_for_each_safe(h, t, &irm->ipcps) { + list_for_each_safe(h, t, &instance->ipcps) { struct ipcp_entry * e = list_entry(h, struct ipcp_entry, next); destroy_ipcp(e->api); } - list_for_each_safe(h, t, &irm->reg_names) { + list_for_each_safe(h, t, &instance->reg_names) { struct reg_name_entry * e = list_entry(h, struct reg_name_entry, next); @@ -1294,7 +1290,11 @@ static void irm_destroy(struct irm * irm) reg_name_entry_destroy(e); } - list_for_each_safe(h, t, &irm->port_map) { + pthread_rwlock_unlock(&instance->reg_lock); + + pthread_rwlock_wrlock(&instance->flows_lock); + + list_for_each_safe(h, t, &instance->port_map) { struct port_map_entry * e = list_entry(h, struct port_map_entry, next); @@ -1303,15 +1303,16 @@ static void irm_destroy(struct irm * irm) free(e); } + pthread_rwlock_unlock(&instance->flows_lock); - if (irm->dum != NULL) - shm_du_map_destroy(irm->dum); + if (instance->dum != NULL) + shm_du_map_destroy(instance->dum); - close(irm->sockfd); + close(instance->sockfd); - rw_lock_unlock(&irm->state_lock); + pthread_rwlock_unlock(&instance->state_lock); - free(irm); + free(instance); } @@ -1323,7 +1324,11 @@ void irmd_sig_handler(int sig, siginfo_t * info, void * c) case SIGINT: case SIGTERM: case SIGHUP: - rw_lock_wrlock(&instance->state_lock); + pthread_rwlock_wrlock(&instance->state_lock); + + + + pthread_rwlock_unlock(&instance->state_lock); if (instance->threadpool != NULL) { for (i = 0; i < IRMD_THREADPOOL_SIZE; i++) @@ -1331,9 +1336,9 @@ void irmd_sig_handler(int sig, siginfo_t * info, void * c) } + pthread_cancel(instance->shm_sanitize); pthread_cancel(instance->cleanup_flows); - rw_lock_unlock(&instance->state_lock); break; case SIGPIPE: @@ -1355,13 +1360,13 @@ void * irm_flow_cleaner() if(clock_gettime(CLOCK_MONOTONIC, &now) < 0) LOG_WARN("Failed to get time."); /* cleanup stale PENDING flows */ - rw_lock_rdlock(&instance->state_lock); - rw_lock_wrlock(&instance->flows_lock); + + pthread_rwlock_rdlock(&instance->state_lock); + pthread_rwlock_wrlock(&instance->flows_lock); list_for_each_safe(pos, n, &(instance->port_map)) { struct port_map_entry * e = list_entry(pos, struct port_map_entry, next); - pthread_mutex_lock(&e->res_lock); if (e->state == FLOW_PENDING && @@ -1369,9 +1374,12 @@ void * irm_flow_cleaner() LOG_DBGF("Pending port_id %d timed out.", e->port_id); e->state = FLOW_NULL; - pthread_cond_broadcast(&e->res_signal); + pthread_cond_signal(&e->res_signal); + pthread_mutex_unlock(&e->res_lock); + continue; } + pthread_mutex_unlock(&e->res_lock); if (kill(e->n_pid, 0) < 0) { bmp_release(instance->port_ids, e->port_id); @@ -1388,17 +1396,20 @@ void * irm_flow_cleaner() e->n_1_pid, e->port_id); free(e); } - - pthread_mutex_unlock(&e->res_lock); } - rw_lock_unlock(&instance->flows_lock); - rw_lock_unlock(&instance->state_lock); + pthread_rwlock_unlock(&instance->flows_lock); + pthread_rwlock_unlock(&instance->state_lock); nanosleep(&timeout, NULL); } } +void clean_msg(void * msg) +{ + irm_msg__free_unpacked(msg, NULL); +} + void * mainloop() { uint8_t buf[IRM_MSG_BUF_SIZE]; @@ -1433,6 +1444,8 @@ void * mainloop() continue; } + pthread_cleanup_push(clean_msg, (void *) msg); + api.name = msg->ap_name; if (msg->has_api_id == true) api.id = msg->api_id; @@ -1542,7 +1555,7 @@ void * mainloop() break; } - irm_msg__free_unpacked(msg, NULL); + pthread_cleanup_pop(true); buffer.size = irm_msg__get_packed_size(&ret_msg); if (buffer.size == 0) { @@ -1574,8 +1587,8 @@ static struct irm * irm_create() { struct stat st = {0}; - struct irm * i = malloc(sizeof(*i)); - if (i == NULL) + instance = malloc(sizeof(*instance)); + if (instance == NULL) return NULL; if (access("/dev/shm/" SHM_DU_MAP_FILENAME, F_OK) != -1) { @@ -1583,6 +1596,7 @@ static struct irm * irm_create() if (dum == NULL) { LOG_ERR("Could not examine existing shm file."); + free(instance); exit(EXIT_FAILURE); } @@ -1592,68 +1606,71 @@ static struct irm * irm_create() LOG_INFO("Stale shm file removed."); } else { LOG_INFO("IRMd already running, exiting."); - free(i); + free(instance); exit(EXIT_SUCCESS); } } - if (rw_lock_init(&i->state_lock)) { - irm_destroy(i); + if (pthread_rwlock_init(&instance->state_lock, NULL)) { + LOG_ERR("Failed to initialize rwlock."); + free(instance); + return NULL; + } + + if (pthread_rwlock_init(&instance->reg_lock, NULL)) { + LOG_ERR("Failed to initialize rwlock."); + free(instance); + return NULL; + } + + if (pthread_rwlock_init(&instance->flows_lock, NULL)) { + LOG_ERR("Failed to initialize rwlock."); + free(instance); return NULL; } - i->threadpool = malloc(sizeof(pthread_t) * IRMD_THREADPOOL_SIZE); - if (i->threadpool == NULL) { - irm_destroy(i); + instance->threadpool = malloc(sizeof(pthread_t) * IRMD_THREADPOOL_SIZE); + if (instance->threadpool == NULL) { + irm_destroy(); return NULL; } - if ((i->dum = shm_du_map_create()) == NULL) { - irm_destroy(i); + if ((instance->dum = shm_du_map_create()) == NULL) { + irm_destroy(); return NULL; } - INIT_LIST_HEAD(&i->ipcps); - INIT_LIST_HEAD(&i->reg_names); - INIT_LIST_HEAD(&i->port_map); + INIT_LIST_HEAD(&instance->ipcps); + INIT_LIST_HEAD(&instance->reg_names); + INIT_LIST_HEAD(&instance->port_map); - i->port_ids = bmp_create(IRMD_MAX_FLOWS, 0); - if (i->port_ids == NULL) { - irm_destroy(i); + instance->port_ids = bmp_create(IRMD_MAX_FLOWS, 0); + if (instance->port_ids == NULL) { + irm_destroy(); return NULL; } if (stat(SOCK_PATH, &st) == -1) { if (mkdir(SOCK_PATH, 0777)) { LOG_ERR("Failed to create sockets directory."); - irm_destroy(i); + irm_destroy(); return NULL; } } - i->sockfd = server_socket_open(IRM_SOCK_PATH); - if (i->sockfd < 0) { - irm_destroy(i); + instance->sockfd = server_socket_open(IRM_SOCK_PATH); + if (instance->sockfd < 0) { + irm_destroy(); return NULL; } if (chmod(IRM_SOCK_PATH, 0666)) { LOG_ERR("Failed to chmod socket."); - irm_destroy(i); - return NULL; - } - - if (rw_lock_init(&i->reg_lock)) { - irm_destroy(i); + irm_destroy(); return NULL; } - if (rw_lock_init(&i->flows_lock)) { - irm_destroy(i); - return NULL; - } - - return i; + return instance; } int main() @@ -1687,18 +1704,21 @@ int main() if (instance == NULL) exit(EXIT_FAILURE); - pthread_create(&instance->cleanup_flows, NULL, irm_flow_cleaner, NULL); - for (t = 0; t < IRMD_THREADPOOL_SIZE; ++t) pthread_create(&instance->threadpool[t], NULL, mainloop, NULL); + pthread_create(&instance->cleanup_flows, NULL, irm_flow_cleaner, NULL); + pthread_create(&instance->shm_sanitize, NULL, + shm_du_map_sanitize, NULL); + /* wait for (all of them) to return */ for (t = 0; t < IRMD_THREADPOOL_SIZE; ++t) pthread_join(instance->threadpool[t], NULL); + pthread_join(instance->shm_sanitize, NULL); pthread_join(instance->cleanup_flows, NULL); - irm_destroy(instance); + irm_destroy(); exit(EXIT_SUCCESS); } diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 7db083b6..ed7f2fc6 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -32,7 +32,6 @@ set(SOURCE_FILES ipcp.c irm.c list.c - rw_lock.c shm_ap_rbuff.c shm_du_map.c sockets.c diff --git a/src/lib/dev.c b/src/lib/dev.c index ab4365b8..1725cca3 100644 --- a/src/lib/dev.c +++ b/src/lib/dev.c @@ -31,7 +31,6 @@ #include #include #include -#include #include #include @@ -49,10 +48,10 @@ struct ap_data { struct shm_du_map * dum; struct bmp * fds; struct shm_ap_rbuff * rb; - rw_lock_t data_lock; + pthread_rwlock_t data_lock; struct flow flows[AP_MAX_FLOWS]; - rw_lock_t flows_lock; + pthread_rwlock_t flows_lock; } * _ap_instance; int ap_init(char * ap_name) @@ -109,8 +108,8 @@ int ap_init(char * ap_name) _ap_instance->flows[i].port_id = -1; } - rw_lock_init(&_ap_instance->flows_lock); - rw_lock_init(&_ap_instance->data_lock); + pthread_rwlock_init(&_ap_instance->flows_lock, NULL); + pthread_rwlock_init(&_ap_instance->data_lock, NULL); return 0; } @@ -122,7 +121,7 @@ void ap_fini(void) if (_ap_instance == NULL) return; - rw_lock_wrlock(&_ap_instance->data_lock); + pthread_rwlock_wrlock(&_ap_instance->data_lock); if (_ap_instance->api != NULL) instance_name_destroy(_ap_instance->api); @@ -133,14 +132,14 @@ void ap_fini(void) if (_ap_instance->rb != NULL) shm_ap_rbuff_destroy(_ap_instance->rb); - rw_lock_rdlock(&_ap_instance->flows_lock); + pthread_rwlock_rdlock(&_ap_instance->flows_lock); for (i = 0; i < AP_MAX_FLOWS; ++i) if (_ap_instance->flows[i].rb != NULL) shm_ap_rbuff_close(_ap_instance->flows[i].rb); - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ap_instance->data_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ap_instance->data_lock); free(_ap_instance); } @@ -166,12 +165,12 @@ int flow_accept(char ** ae_name) msg.code = IRM_MSG_CODE__IRM_FLOW_ACCEPT; msg.has_pid = true; - rw_lock_rdlock(&_ap_instance->data_lock); + pthread_rwlock_rdlock(&_ap_instance->data_lock); msg.ap_name = _ap_instance->api->name; msg.pid = _ap_instance->api->id; - rw_lock_unlock(&_ap_instance->data_lock); + pthread_rwlock_unlock(&_ap_instance->data_lock); recv_msg = send_recv_irm_msg(&msg); if (recv_msg == NULL) { @@ -183,13 +182,13 @@ int flow_accept(char ** ae_name) return -1; } - rw_lock_rdlock(&_ap_instance->data_lock); - rw_lock_wrlock(&_ap_instance->flows_lock); + pthread_rwlock_rdlock(&_ap_instance->data_lock); + pthread_rwlock_wrlock(&_ap_instance->flows_lock); cfd = bmp_allocate(_ap_instance->fds); if (!bmp_is_id_valid(_ap_instance->fds, cfd)) { - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ap_instance->data_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ap_instance->data_lock); irm_msg__free_unpacked(recv_msg, NULL); return -1; } @@ -197,8 +196,8 @@ int flow_accept(char ** ae_name) _ap_instance->flows[cfd].rb = shm_ap_rbuff_open(recv_msg->pid); if (_ap_instance->flows[cfd].rb == NULL) { bmp_release(_ap_instance->fds, cfd); - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ap_instance->data_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ap_instance->data_lock); irm_msg__free_unpacked(recv_msg, NULL); return -1; } @@ -208,8 +207,8 @@ int flow_accept(char ** ae_name) if (*ae_name == NULL) { shm_ap_rbuff_close(_ap_instance->flows[cfd].rb); bmp_release(_ap_instance->fds, cfd); - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ap_instance->data_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ap_instance->data_lock); irm_msg__free_unpacked(recv_msg, NULL); return -1; } @@ -218,8 +217,8 @@ int flow_accept(char ** ae_name) _ap_instance->flows[cfd].port_id = recv_msg->port_id; _ap_instance->flows[cfd].oflags = FLOW_O_DEFAULT; - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ap_instance->data_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ap_instance->data_lock); irm_msg__free_unpacked(recv_msg, NULL); @@ -241,31 +240,31 @@ int flow_alloc_resp(int fd, msg.pid = _ap_instance->api->id; msg.has_port_id = true; - rw_lock_rdlock(&_ap_instance->data_lock); - rw_lock_rdlock(&_ap_instance->flows_lock); + pthread_rwlock_rdlock(&_ap_instance->data_lock); + pthread_rwlock_rdlock(&_ap_instance->flows_lock); msg.port_id = _ap_instance->flows[fd].port_id; - rw_lock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); msg.has_response = true; msg.response = response; recv_msg = send_recv_irm_msg(&msg); if (recv_msg == NULL) { - rw_lock_unlock(&_ap_instance->data_lock); + pthread_rwlock_unlock(&_ap_instance->data_lock); return -1; } if (!recv_msg->has_result) { - rw_lock_unlock(&_ap_instance->data_lock); + pthread_rwlock_unlock(&_ap_instance->data_lock); irm_msg__free_unpacked(recv_msg, NULL); return -1; } ret = recv_msg->result; - rw_lock_unlock(&_ap_instance->data_lock); + pthread_rwlock_unlock(&_ap_instance->data_lock); irm_msg__free_unpacked(recv_msg, NULL); @@ -291,11 +290,11 @@ int flow_alloc(char * dst_name, msg.ae_name = src_ae_name; msg.has_pid = true; - rw_lock_rdlock(&_ap_instance->data_lock); + pthread_rwlock_rdlock(&_ap_instance->data_lock); msg.pid = _ap_instance->api->id; - rw_lock_unlock(&_ap_instance->data_lock); + pthread_rwlock_unlock(&_ap_instance->data_lock); recv_msg = send_recv_irm_msg(&msg); if (recv_msg == NULL) { @@ -307,13 +306,13 @@ int flow_alloc(char * dst_name, return -1; } - rw_lock_rdlock(&_ap_instance->data_lock); - rw_lock_wrlock(&_ap_instance->flows_lock); + pthread_rwlock_rdlock(&_ap_instance->data_lock); + pthread_rwlock_wrlock(&_ap_instance->flows_lock); fd = bmp_allocate(_ap_instance->fds); if (!bmp_is_id_valid(_ap_instance->fds, fd)) { - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ap_instance->data_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ap_instance->data_lock); irm_msg__free_unpacked(recv_msg, NULL); return -1; } @@ -321,8 +320,8 @@ int flow_alloc(char * dst_name, _ap_instance->flows[fd].rb = shm_ap_rbuff_open(recv_msg->pid); if (_ap_instance->flows[fd].rb == NULL) { bmp_release(_ap_instance->fds, fd); - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ap_instance->data_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ap_instance->data_lock); irm_msg__free_unpacked(recv_msg, NULL); return -1; } @@ -330,8 +329,8 @@ int flow_alloc(char * dst_name, _ap_instance->flows[fd].port_id = recv_msg->port_id; _ap_instance->flows[fd].oflags = FLOW_O_DEFAULT; - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ap_instance->data_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ap_instance->data_lock); irm_msg__free_unpacked(recv_msg, NULL); @@ -350,13 +349,13 @@ int flow_alloc_res(int fd) msg.code = IRM_MSG_CODE__IRM_FLOW_ALLOC_RES; msg.has_port_id = true; - rw_lock_rdlock(&_ap_instance->data_lock); - rw_lock_rdlock(&_ap_instance->flows_lock); + pthread_rwlock_rdlock(&_ap_instance->data_lock); + pthread_rwlock_rdlock(&_ap_instance->flows_lock); msg.port_id = _ap_instance->flows[fd].port_id; - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ap_instance->data_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ap_instance->data_lock); recv_msg = send_recv_irm_msg(&msg); if (recv_msg == NULL) { @@ -384,8 +383,8 @@ int flow_dealloc(int fd) msg.code = IRM_MSG_CODE__IRM_FLOW_DEALLOC; msg.has_port_id = true; - rw_lock_rdlock(&_ap_instance->data_lock); - rw_lock_wrlock(&_ap_instance->flows_lock); + pthread_rwlock_rdlock(&_ap_instance->data_lock); + pthread_rwlock_wrlock(&_ap_instance->flows_lock); msg.port_id = _ap_instance->flows[fd].port_id; @@ -395,23 +394,23 @@ int flow_dealloc(int fd) bmp_release(_ap_instance->fds, fd); - rw_lock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); recv_msg = send_recv_irm_msg(&msg); if (recv_msg == NULL) { - rw_lock_unlock(&_ap_instance->data_lock); + pthread_rwlock_unlock(&_ap_instance->data_lock); return -1; } if (!recv_msg->has_result) { - rw_lock_unlock(&_ap_instance->data_lock); + pthread_rwlock_unlock(&_ap_instance->data_lock); irm_msg__free_unpacked(recv_msg, NULL); return -1; } ret = recv_msg->result; - rw_lock_unlock(&_ap_instance->data_lock); + pthread_rwlock_unlock(&_ap_instance->data_lock); irm_msg__free_unpacked(recv_msg, NULL); @@ -425,24 +424,24 @@ int flow_cntl(int fd, int cmd, int oflags) if (fd < 0) return -EBADF; - rw_lock_rdlock(&_ap_instance->data_lock); - rw_lock_wrlock(&_ap_instance->flows_lock); + pthread_rwlock_rdlock(&_ap_instance->data_lock); + pthread_rwlock_wrlock(&_ap_instance->flows_lock); old = _ap_instance->flows[fd].oflags; switch (cmd) { case FLOW_F_GETFL: /* GET FLOW FLAGS */ - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ap_instance->data_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ap_instance->data_lock); return old; case FLOW_F_SETFL: /* SET FLOW FLAGS */ _ap_instance->flows[fd].oflags = oflags; - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ap_instance->data_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ap_instance->data_lock); return old; default: - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ap_instance->data_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ap_instance->data_lock); return FLOW_O_INVALID; /* unknown command */ } } @@ -458,8 +457,8 @@ ssize_t flow_write(int fd, void * buf, size_t count) if (fd < 0) return -EBADF; - rw_lock_rdlock(&_ap_instance->data_lock); - rw_lock_rdlock(&_ap_instance->flows_lock); + pthread_rwlock_rdlock(&_ap_instance->data_lock); + pthread_rwlock_rdlock(&_ap_instance->flows_lock); if (_ap_instance->flows[fd].oflags & FLOW_O_NONBLOCK) { index = shm_create_du_buff(_ap_instance->dum, @@ -469,8 +468,8 @@ ssize_t flow_write(int fd, void * buf, size_t count) (uint8_t *) buf, count); if (index == -1) { - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ap_instance->data_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ap_instance->data_lock); return -1; } @@ -479,8 +478,8 @@ ssize_t flow_write(int fd, void * buf, size_t count) if (shm_ap_rbuff_write(_ap_instance->flows[fd].rb, &e) < 0) { shm_release_du_buff(_ap_instance->dum, index); - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ap_instance->data_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ap_instance->data_lock); return -EPIPE; } } else { /* blocking */ @@ -499,8 +498,8 @@ ssize_t flow_write(int fd, void * buf, size_t count) ; } - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ap_instance->data_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ap_instance->data_lock); return 0; } @@ -514,12 +513,12 @@ ssize_t flow_read(int fd, void * buf, size_t count) if (fd < 0) return -EBADF; - rw_lock_rdlock(&_ap_instance->data_lock); - rw_lock_rdlock(&_ap_instance->flows_lock); + pthread_rwlock_rdlock(&_ap_instance->data_lock); + pthread_rwlock_rdlock(&_ap_instance->flows_lock); if (_ap_instance->flows[fd].port_id < 0) { - rw_lock_unlock(&_ap_instance->flows_lock); - rw_lock_unlock(&_ap_instance->data_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ap_instance->data_lock); return -1; } @@ -533,10 +532,10 @@ ssize_t flow_read(int fd, void * buf, size_t count) ; } - rw_lock_unlock(&_ap_instance->flows_lock); + pthread_rwlock_unlock(&_ap_instance->flows_lock); if (idx < 0) { - rw_lock_unlock(&_ap_instance->data_lock); + pthread_rwlock_unlock(&_ap_instance->data_lock); return -1; } @@ -544,7 +543,7 @@ ssize_t flow_read(int fd, void * buf, size_t count) _ap_instance->dum, idx); if (n < 0) { - rw_lock_unlock(&_ap_instance->data_lock); + pthread_rwlock_unlock(&_ap_instance->data_lock); return -1; } @@ -552,7 +551,7 @@ ssize_t flow_read(int fd, void * buf, size_t count) shm_release_du_buff(_ap_instance->dum, idx); - rw_lock_unlock(&_ap_instance->data_lock); + pthread_rwlock_unlock(&_ap_instance->data_lock); return n; } diff --git a/src/lib/rw_lock.c b/src/lib/rw_lock.c deleted file mode 100644 index ba285694..00000000 --- a/src/lib/rw_lock.c +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Ouroboros - Copyright (C) 2016 - * - * Read/Write locks - * - * Dimitri Staessens - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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., 675 Mass Ave, Cambridge, MA 02139, USA. - */ - -#include - -int rw_lock_init(rw_lock_t * lock) -{ - if (lock == NULL) - return -1; - - pthread_mutex_init(&lock->lock, NULL); - pthread_mutex_init(&lock->i_lock, NULL); - lock->i = 0; - - return 0; -} - -void rw_lock_destroy(rw_lock_t * lock) -{ - if (lock == NULL) - return; - - pthread_mutex_destroy(&lock->lock); - pthread_mutex_destroy(&lock->i_lock); - lock->i = -1; -} - -int rw_lock_rdlock(rw_lock_t * lock) -{ - int ret = 0; - - if (lock == NULL) - return -1; - - pthread_mutex_lock(&lock->i_lock); - - if (lock->i < 0) { - pthread_mutex_unlock(&lock->i_lock); - return -1; - } - - if (lock->i == 0) - ret = pthread_mutex_lock(&lock->lock); - - ++(lock->i); - - pthread_mutex_unlock(&lock->i_lock); - - return ret; -} - -int rw_lock_wrlock(rw_lock_t * lock) -{ - int ret = 0; - - if (lock == NULL) - return -1; - - pthread_mutex_lock(&lock->i_lock); - - if (lock->i < 0) { - pthread_mutex_unlock(&lock->i_lock); - return -1; - } - - pthread_mutex_unlock(&lock->i_lock); - - while (1) { - pthread_mutex_lock(&lock->i_lock); - - if (lock->i == 0) { - ++(lock->i); - ret = pthread_mutex_lock(&lock->lock); - pthread_mutex_unlock(&lock->i_lock); - break; - } - - pthread_mutex_unlock(&lock->i_lock); - - sched_yield(); - } - - return ret; -} - -int rw_lock_unlock(rw_lock_t * lock) -{ - int ret = 0; - - if (lock == NULL) - return -1; - - pthread_mutex_lock(&lock->i_lock); - - if (lock->i < 0) { - pthread_mutex_unlock(&lock->i_lock); - return -1; - } - - --(lock->i); - - if (lock->i == 0) - ret = pthread_mutex_unlock(&lock->lock); - - pthread_mutex_unlock(&lock->i_lock); - - return ret; -} diff --git a/src/lib/shm_du_map.c b/src/lib/shm_du_map.c index 5f935d3f..f5909e11 100644 --- a/src/lib/shm_du_map.c +++ b/src/lib/shm_du_map.c @@ -36,7 +36,8 @@ #define SHM_BLOCKS_SIZE (SHM_BLOCKS_IN_MAP * SHM_DU_BUFF_BLOCK_SIZE) #define SHM_FILE_SIZE (SHM_BLOCKS_SIZE + 2 * sizeof (size_t) \ - + sizeof(pthread_mutex_t)) + sizeof(pid_t) + + sizeof(pthread_mutex_t) + sizeof(pthread_cond_t) \ + + sizeof(pid_t)) #define get_head_ptr(dum) \ ((struct shm_du_buff *)(dum->shm_base + (*dum->ptr_head * \ @@ -73,6 +74,7 @@ struct shm_du_map { size_t * ptr_head; /* start of ringbuffer head */ size_t * ptr_tail; /* start of ringbuffer tail */ pthread_mutex_t * shm_mutex; /* lock all free space in shm */ + pthread_cond_t * sanitize; /* run sanitizer when buffer full */ pid_t * pid; /* pid of the irmd owner */ int fd; }; @@ -82,7 +84,8 @@ struct shm_du_map * shm_du_map_create() struct shm_du_map * dum; int shm_fd; uint8_t * shm_base; - pthread_mutexattr_t attr; + pthread_mutexattr_t mattr; + pthread_condattr_t cattr; dum = malloc(sizeof *dum); if (dum == NULL) { @@ -137,11 +140,17 @@ struct shm_du_map * shm_du_map_create() ((uint8_t *) dum->shm_base + SHM_BLOCKS_SIZE); dum->ptr_tail = dum->ptr_head + 1; dum->shm_mutex = (pthread_mutex_t *) (dum->ptr_tail + 1); - dum->pid = (pid_t *) (dum->shm_mutex + 1); + dum->sanitize = (pthread_cond_t *) (dum->shm_mutex + 1); + dum->pid = (pid_t *) (dum->sanitize + 1); - pthread_mutexattr_init(&attr); - pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); - pthread_mutex_init(dum->shm_mutex, &attr); + pthread_mutexattr_init(&mattr); + pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED); + pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST); + pthread_mutex_init(dum->shm_mutex, &mattr); + + pthread_condattr_init(&cattr); + pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED); + pthread_cond_init(dum->sanitize, &cattr); *dum->ptr_head = 0; *dum->ptr_tail = 0; @@ -193,7 +202,8 @@ struct shm_du_map * shm_du_map_open() ((uint8_t *) dum->shm_base + SHM_BLOCKS_SIZE); dum->ptr_tail = dum->ptr_head + 1; dum->shm_mutex = (pthread_mutex_t *) (dum->ptr_tail + 1); - dum->pid = (pid_t *) (dum->shm_mutex + 1); + dum->sanitize = (pthread_cond_t *) (dum->shm_mutex + 1); + dum->pid = (pid_t *) (dum->sanitize + 1); dum->fd = shm_fd; @@ -205,6 +215,12 @@ pid_t shm_du_map_owner(struct shm_du_map * dum) return *dum->pid; } +void * shm_du_map_sanitize(void * o) +{ + LOG_MISSING; + return (void *) 0; +} + void shm_du_map_close(struct shm_du_map * dum) { if (dum == NULL) { @@ -294,11 +310,13 @@ ssize_t shm_create_du_buff(struct shm_du_map * dum, if (!shm_map_free(dum, blocks)) { pthread_mutex_unlock(dum->shm_mutex); + pthread_cond_signal(dum->sanitize); return -1; } #else if (!shm_map_free(dum, 1)) { pthread_mutex_unlock(dum->shm_mutex); + ptrhead_cond_signal(dum->sanitize); return -1; } #endif -- cgit v1.2.3