summaryrefslogtreecommitdiff
path: root/src/irmd/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/irmd/main.c')
-rw-r--r--src/irmd/main.c136
1 files changed, 60 insertions, 76 deletions
diff --git a/src/irmd/main.c b/src/irmd/main.c
index 6e3f952f..13bfa052 100644
--- a/src/irmd/main.c
+++ b/src/irmd/main.c
@@ -55,6 +55,7 @@
#include <sys/wait.h>
#define IRMD_CLEANUP_TIMER ((IRMD_FLOW_TIMEOUT / 20) * MILLION) /* ns */
+#define SHM_SAN_HOLDOFF 1000 /* ms */
struct ipcp_entry {
struct list_head next;
@@ -1141,49 +1142,21 @@ static struct irm_flow * flow_alloc(pid_t api,
}
pthread_rwlock_unlock(&irmd->reg_lock);
-
- f = irm_flow_create();
- if (f == NULL) {
- pthread_rwlock_unlock(&irmd->state_lock);
- LOG_ERR("Failed to create irm_flow.");
- return NULL;
- }
-
- f->n_api = api;
- f->state = FLOW_ALLOC_PENDING;
-
- if (clock_gettime(CLOCK_MONOTONIC, &f->t0) < 0)
- LOG_WARN("Failed to set timestamp.");
-
pthread_rwlock_wrlock(&irmd->flows_lock);
-
- port_id = f->port_id = bmp_allocate(irmd->port_ids);
+ port_id = bmp_allocate(irmd->port_ids);
if (!bmp_is_id_valid(irmd->port_ids, port_id)) {
pthread_rwlock_unlock(&irmd->flows_lock);
pthread_rwlock_unlock(&irmd->state_lock);
LOG_ERR("Could not allocate port_id.");
- irm_flow_destroy(f);
return NULL;
}
- f->n_1_api = ipcp;
- f->n_rb = shm_rbuff_create(api, port_id);
- if (f->n_rb == NULL) {
- bmp_release(irmd->port_ids, port_id);
- pthread_rwlock_unlock(&irmd->flows_lock);
- pthread_rwlock_unlock(&irmd->state_lock);
- LOG_ERR("Could not create ringbuffer for AP-I %d.", api);
- irm_flow_destroy(f);
- return NULL;
- }
-
- f->n_1_rb = shm_rbuff_create(ipcp, port_id);
- if (f->n_1_rb == NULL) {
+ f = irm_flow_create(api, ipcp, port_id);
+ if (f == NULL) {
bmp_release(irmd->port_ids, port_id);
pthread_rwlock_unlock(&irmd->flows_lock);
pthread_rwlock_unlock(&irmd->state_lock);
- LOG_ERR("Could not create ringbuffer for AP-I %d.", ipcp);
- irm_flow_destroy(f);
+ LOG_ERR("Could not allocate port_id.");
return NULL;
}
@@ -1268,7 +1241,7 @@ static int flow_dealloc(pid_t api, int port_id)
if (f == NULL) {
pthread_rwlock_unlock(&irmd->flows_lock);
pthread_rwlock_unlock(&irmd->state_lock);
- LOG_DBG("Deallocate called for unknown port.");
+ LOG_DBG("Deallocate unknown port %d by %d.", port_id, api);
return 0;
}
@@ -1297,11 +1270,12 @@ static int flow_dealloc(pid_t api, int port_id)
}
pthread_rwlock_unlock(&irmd->flows_lock);
- pthread_rwlock_unlock(&irmd->state_lock);
if (n_1_api != -1)
ret = ipcp_flow_dealloc(n_1_api, port_id);
+ pthread_rwlock_unlock(&irmd->state_lock);
+
return ret;
}
@@ -1351,21 +1325,11 @@ static struct irm_flow * flow_req_arr(pid_t api,
struct pid_el * c_api;
pid_t h_api = -1;
+ int port_id = -1;
LOG_DBGF("Flow req arrived from IPCP %d for %s on AE %s.",
api, dst_name, ae_name);
- f = irm_flow_create();
- if (f == NULL) {
- LOG_ERR("Failed to create irm_flow.");
- return NULL;
- }
-
- f->state = FLOW_ALLOC_PENDING;
- f->n_1_api = api;
- if (clock_gettime(CLOCK_MONOTONIC, &f->t0) < 0)
- LOG_WARN("Failed to set timestamp.");
-
pthread_rwlock_rdlock(&irmd->state_lock);
pthread_rwlock_wrlock(&irmd->reg_lock);
@@ -1374,7 +1338,6 @@ static struct irm_flow * flow_req_arr(pid_t api,
pthread_rwlock_unlock(&irmd->reg_lock);
pthread_rwlock_unlock(&irmd->state_lock);
LOG_ERR("Unknown name: %s.", dst_name);
- irm_flow_destroy(f);
return NULL;
}
@@ -1387,14 +1350,12 @@ static struct irm_flow * flow_req_arr(pid_t api,
pthread_rwlock_unlock(&irmd->reg_lock);
pthread_rwlock_unlock(&irmd->state_lock);
LOG_ERR("No AP's for %s.", dst_name);
- irm_flow_destroy(f);
return NULL;
case REG_NAME_AUTO_ACCEPT:
c_api = malloc(sizeof(*c_api));
if (c_api == NULL) {
pthread_rwlock_unlock(&irmd->reg_lock);
pthread_rwlock_unlock(&irmd->state_lock);
- irm_flow_destroy(f);
return NULL;
}
@@ -1412,7 +1373,6 @@ static struct irm_flow * flow_req_arr(pid_t api,
pthread_rwlock_unlock(&irmd->state_lock);
LOG_ERR("Could not get start apn for reg_entry %s.",
re->name);
- irm_flow_destroy(f);
free(c_api);
return NULL;
}
@@ -1439,7 +1399,6 @@ static struct irm_flow * flow_req_arr(pid_t api,
pthread_mutex_unlock(&re->state_lock);
pthread_rwlock_unlock(&irmd->reg_lock);
pthread_rwlock_unlock(&irmd->state_lock);
- irm_flow_destroy(f);
return NULL;
}
@@ -1447,13 +1406,12 @@ static struct irm_flow * flow_req_arr(pid_t api,
case REG_NAME_FLOW_ACCEPT:
pthread_mutex_lock(&re->state_lock);
- h_api = f->n_api = reg_entry_get_api(re);
+ h_api = reg_entry_get_api(re);
pthread_mutex_unlock(&re->state_lock);
- if (f->n_api == -1) {
+ if (h_api == -1) {
pthread_rwlock_unlock(&irmd->reg_lock);
pthread_rwlock_unlock(&irmd->state_lock);
LOG_ERR("Invalid api returned.");
- irm_flow_destroy(f);
return NULL;
}
@@ -1462,39 +1420,25 @@ static struct irm_flow * flow_req_arr(pid_t api,
pthread_rwlock_unlock(&irmd->reg_lock);
pthread_rwlock_unlock(&irmd->state_lock);
LOG_ERR("IRMd in wrong state.");
- irm_flow_destroy(f);
return NULL;
}
- pthread_rwlock_unlock(&irmd->reg_lock);
+ pthread_rwlock_unlock(&irmd->reg_lock);
pthread_rwlock_wrlock(&irmd->flows_lock);
- f->port_id = bmp_allocate(irmd->port_ids);
- if (!bmp_is_id_valid(irmd->port_ids, f->port_id)) {
- pthread_rwlock_unlock(&irmd->flows_lock);
- pthread_rwlock_unlock(&irmd->state_lock);
- LOG_ERR("Could not create ringbuffer for AP-I %d.", f->n_api);
- irm_flow_destroy(f);
- return NULL;
- }
-
- f->n_rb = shm_rbuff_create(f->n_api, f->port_id);
- if (f->n_rb == NULL) {
- bmp_release(irmd->port_ids, f->port_id);
+ port_id = bmp_allocate(irmd->port_ids);
+ if (!bmp_is_id_valid(irmd->port_ids, port_id)) {
pthread_rwlock_unlock(&irmd->flows_lock);
pthread_rwlock_unlock(&irmd->state_lock);
- LOG_ERR("Could not create ringbuffer for AP-I %d.", f->n_api);
- irm_flow_destroy(f);
return NULL;
}
- f->n_1_rb = shm_rbuff_create(f->n_1_api, f->port_id);
- if (f->n_1_rb == NULL) {
- bmp_release(irmd->port_ids, f->port_id);
+ f = irm_flow_create(h_api, api, port_id);
+ if (f == NULL) {
+ bmp_release(irmd->port_ids, port_id);
pthread_rwlock_unlock(&irmd->flows_lock);
pthread_rwlock_unlock(&irmd->state_lock);
- LOG_ERR("Could not create ringbuffer for AP-I %d.", f->n_1_api);
- irm_flow_destroy(f);
+ LOG_ERR("Could not allocate port_id.");
return NULL;
}
@@ -1515,6 +1459,7 @@ static struct irm_flow * flow_req_arr(pid_t api,
pthread_rwlock_unlock(&irmd->reg_lock);
pthread_rwlock_wrlock(&irmd->flows_lock);
bmp_release(irmd->port_ids, f->port_id);
+ list_del(&f->next);
pthread_rwlock_unlock(&irmd->flows_lock);
pthread_rwlock_unlock(&irmd->state_lock);
LOG_ERR("Could not get api table entry for %d.", h_api);
@@ -1580,7 +1525,6 @@ static void irm_destroy(void)
list_for_each_safe(p, h, &irmd->irm_flows) {
struct irm_flow * f = list_entry(p, struct irm_flow, next);
list_del(&f->next);
- ipcp_flow_dealloc(f->n_1_api, f->port_id);
irm_flow_destroy(f);
}
@@ -1669,6 +1613,46 @@ void irmd_sig_handler(int sig, siginfo_t * info, void * c)
}
}
+void * shm_sanitize(void * o)
+{
+ struct list_head * p = NULL;
+ struct timespec ts = {SHM_SAN_HOLDOFF / 1000,
+ (SHM_SAN_HOLDOFF % 1000) * MILLION};
+ ssize_t idx;
+
+ (void) o;
+
+ while (true) {
+ shm_rdrbuff_wait_full(irmd->rdrb);
+
+ pthread_rwlock_rdlock(&irmd->state_lock);
+ pthread_rwlock_wrlock(&irmd->flows_lock);
+
+ list_for_each(p, &irmd->irm_flows) {
+ struct irm_flow * f =
+ list_entry(p, struct irm_flow, next);
+ if (kill(f->n_api, 0) < 0) {
+ while ((idx = shm_rbuff_read(f->n_rb)) >= 0)
+ shm_rdrbuff_remove(irmd->rdrb, idx);
+ continue;
+ }
+
+ if (kill(f->n_1_api, 0) < 0) {
+ while ((idx = shm_rbuff_read(f->n_1_rb)) >= 0)
+ shm_rdrbuff_remove(irmd->rdrb, idx);
+ continue;
+ }
+ }
+
+ pthread_rwlock_unlock(&irmd->flows_lock);
+ pthread_rwlock_unlock(&irmd->state_lock);
+
+ nanosleep(&ts, NULL);
+ }
+
+ return (void *) 0;
+}
+
void * irm_sanitize(void * o)
{
struct timespec now;
@@ -2210,7 +2194,7 @@ int main(int argc, char ** argv)
pthread_create(&irmd->irm_sanitize, NULL, irm_sanitize, NULL);
pthread_create(&irmd->shm_sanitize, NULL,
- shm_rdrbuff_sanitize, irmd->rdrb);
+ shm_sanitize, irmd->rdrb);
/* wait for (all of them) to return */
for (t = 0; t < IRMD_THREADPOOL_SIZE; ++t)