summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/dev.c205
-rw-r--r--src/lib/shm_flow_set.c18
2 files changed, 120 insertions, 103 deletions
diff --git a/src/lib/dev.c b/src/lib/dev.c
index ec1cd696..fc8739a2 100644
--- a/src/lib/dev.c
+++ b/src/lib/dev.c
@@ -388,6 +388,14 @@ int flow_accept(char ** ae_name, struct qos_spec * qos)
return -1;
}
+ ai.flows[fd].tx_rb = shm_rbuff_open(recv_msg->api, recv_msg->port_id);
+ if (ai.flows[fd].tx_rb == NULL) {
+ reset_flow(fd);
+ pthread_rwlock_unlock(&ai.flows_lock);
+ pthread_rwlock_unlock(&ai.data_lock);
+ return -1;
+ }
+
ai.flows[fd].set = shm_flow_set_open(recv_msg->api);
if (ai.flows[fd].set == NULL) {
reset_flow(fd);
@@ -398,7 +406,6 @@ int flow_accept(char ** ae_name, struct qos_spec * qos)
return -1;
}
-
if (ae_name != NULL) {
*ae_name = strdup(recv_msg->ae_name);
if (*ae_name == NULL) {
@@ -452,40 +459,34 @@ int flow_alloc_resp(int fd, int response)
msg.port_id = ai.flows[fd].port_id;
pthread_rwlock_unlock(&ai.flows_lock);
+ pthread_rwlock_unlock(&ai.data_lock);
msg.has_response = true;
msg.response = response;
recv_msg = send_recv_irm_msg(&msg);
- if (recv_msg == NULL) {
- pthread_rwlock_unlock(&ai.data_lock);
+ if (recv_msg == NULL)
return -1;
- }
if (!recv_msg->has_result) {
- pthread_rwlock_unlock(&ai.data_lock);
irm_msg__free_unpacked(recv_msg, NULL);
return -1;
}
ret = recv_msg->result;
- pthread_rwlock_wrlock(&ai.flows_lock);
+ irm_msg__free_unpacked(recv_msg, NULL);
+
+ if (response) {
+ pthread_rwlock_rdlock(&ai.data_lock);
+ pthread_rwlock_wrlock(&ai.flows_lock);
- ai.flows[fd].tx_rb = shm_rbuff_open(ai.flows[fd].api,
- ai.flows[fd].port_id);
- if (ai.flows[fd].tx_rb == NULL) {
reset_flow(fd);
+
pthread_rwlock_unlock(&ai.flows_lock);
pthread_rwlock_unlock(&ai.data_lock);
- return -1;
}
- pthread_rwlock_unlock(&ai.flows_lock);
- pthread_rwlock_unlock(&ai.data_lock);
-
- irm_msg__free_unpacked(recv_msg, NULL);
-
return ret;
}
@@ -535,9 +536,6 @@ int flow_alloc(char * dst_name, char * src_ae_name, struct qos_spec * qos)
return -1;
}
- ai.flows[fd].port_id = recv_msg->port_id;
- ai.flows[fd].oflags = FLOW_O_DEFAULT;
- ai.flows[fd].api = recv_msg->api;
ai.flows[fd].rx_rb = shm_rbuff_open(ai.api, recv_msg->port_id);
if (ai.flows[fd].rx_rb == NULL) {
reset_flow(fd);
@@ -548,6 +546,25 @@ int flow_alloc(char * dst_name, char * src_ae_name, struct qos_spec * qos)
return -1;
}
+ ai.flows[fd].tx_rb = shm_rbuff_open(recv_msg->api, recv_msg->port_id);
+ if (ai.flows[fd].tx_rb == NULL) {
+ pthread_rwlock_unlock(&ai.flows_lock);
+ pthread_rwlock_unlock(&ai.data_lock);
+ return -1;
+ }
+
+ ai.flows[fd].set = shm_flow_set_open(recv_msg->api);
+ if (ai.flows[fd].set == NULL) {
+ reset_flow(fd);
+ pthread_rwlock_unlock(&ai.flows_lock);
+ pthread_rwlock_unlock(&ai.data_lock);
+ return -1;
+ }
+
+ ai.flows[fd].port_id = recv_msg->port_id;
+ ai.flows[fd].oflags = FLOW_O_DEFAULT;
+ ai.flows[fd].api = recv_msg->api;
+
ai.ports[recv_msg->port_id].fd = fd;
ai.ports[recv_msg->port_id].state = PORT_ID_ASSIGNED;
@@ -582,22 +599,6 @@ int flow_alloc_res(int fd)
msg.port_id = ai.flows[fd].port_id;
- ai.flows[fd].tx_rb = shm_rbuff_open(ai.flows[fd].api,
- ai.flows[fd].port_id);
- if (ai.flows[fd].tx_rb == NULL) {
- pthread_rwlock_unlock(&ai.flows_lock);
- pthread_rwlock_unlock(&ai.data_lock);
- return -1;
- }
-
- ai.flows[fd].set = shm_flow_set_open(ai.flows[fd].api);
- if (ai.flows[fd].set == NULL) {
- reset_flow(fd);
- pthread_rwlock_unlock(&ai.flows_lock);
- pthread_rwlock_unlock(&ai.data_lock);
- return -1;
- }
-
pthread_rwlock_unlock(&ai.flows_lock);
pthread_rwlock_unlock(&ai.data_lock);
@@ -764,10 +765,14 @@ ssize_t flow_write(int fd, void * buf, size_t count)
}
} else { /* blocking */
struct shm_rdrbuff * rdrb = ai.rdrb;
- pid_t api = ai.flows[fd].api;
+ struct shm_rbuff * tx_rb = ai.flows[fd].tx_rb;
+ pid_t api = ai.flows[fd].api;
+
pthread_rwlock_unlock(&ai.flows_lock);
pthread_rwlock_unlock(&ai.data_lock);
+ assert(tx_rb);
+
idx = shm_rdrbuff_write_b(rdrb,
api,
DU_BUFF_HEADSPACE,
@@ -775,15 +780,13 @@ ssize_t flow_write(int fd, void * buf, size_t count)
buf,
count);
- pthread_rwlock_rdlock(&ai.data_lock);
- pthread_rwlock_rdlock(&ai.flows_lock);
-
- if (shm_rbuff_write(ai.flows[fd].tx_rb, idx) < 0) {
- shm_rdrbuff_remove(ai.rdrb, idx);
- pthread_rwlock_unlock(&ai.flows_lock);
- pthread_rwlock_unlock(&ai.data_lock);
+ if (shm_rbuff_write(tx_rb, idx) < 0) {
+ shm_rdrbuff_remove(rdrb, idx);
return -ENOTALLOC;
}
+
+ pthread_rwlock_rdlock(&ai.data_lock);
+ pthread_rwlock_rdlock(&ai.flows_lock);
}
shm_flow_set_notify(ai.flows[fd].set, ai.flows[fd].port_id);
@@ -993,7 +996,7 @@ int flow_event_wait(struct flow_set * set,
{
ssize_t ret;
- if (set == NULL)
+ if (set == NULL || fq == NULL)
return -EINVAL;
if (fq->fqsize > 0)
@@ -1002,12 +1005,16 @@ int flow_event_wait(struct flow_set * set,
assert(!fq->next);
ret = shm_flow_set_wait(ai.fqset, set->idx, fq->fqueue, timeout);
- if (ret == -ETIMEDOUT)
+ if (ret == -ETIMEDOUT) {
+ fq->fqsize = 0;
return -ETIMEDOUT;
+ }
fq->fqsize = ret;
- return 0;
+ assert(ret);
+
+ return ret;
}
/* ipcp-dev functions */
@@ -1034,6 +1041,22 @@ int np1_flow_alloc(pid_t n_api, int port_id)
return -1;
}
+ ai.flows[fd].tx_rb = shm_rbuff_open(n_api, port_id);
+ if (ai.flows[fd].tx_rb == NULL) {
+ reset_flow(fd);
+ pthread_rwlock_unlock(&ai.flows_lock);
+ pthread_rwlock_unlock(&ai.data_lock);
+ return -1;
+ }
+
+ ai.flows[fd].set = shm_flow_set_open(n_api);
+ if (ai.flows[fd].set == NULL) {
+ reset_flow(fd);
+ pthread_rwlock_unlock(&ai.flows_lock);
+ pthread_rwlock_unlock(&ai.data_lock);
+ return -1;
+ }
+
ai.flows[fd].port_id = port_id;
ai.flows[fd].oflags = FLOW_O_DEFAULT;
ai.flows[fd].api = n_api;
@@ -1062,8 +1085,7 @@ int np1_flow_dealloc(int port_id)
return fd;
}
-
-int np1_flow_resp(pid_t n_api, int port_id)
+int np1_flow_resp(int port_id)
{
int fd;
@@ -1073,28 +1095,6 @@ int np1_flow_resp(pid_t n_api, int port_id)
pthread_rwlock_wrlock(&ai.flows_lock);
fd = ai.ports[port_id].fd;
- if (fd < 0) {
- pthread_rwlock_unlock(&ai.flows_lock);
- pthread_rwlock_unlock(&ai.data_lock);
- return fd;
- }
-
- ai.flows[fd].tx_rb = shm_rbuff_open(n_api, port_id);
- if (ai.flows[fd].tx_rb == NULL) {
- reset_flow(fd);
- port_destroy(&ai.ports[port_id]);
- pthread_rwlock_unlock(&ai.flows_lock);
- pthread_rwlock_unlock(&ai.data_lock);
- return -1;
- }
-
- ai.flows[fd].set = shm_flow_set_open(n_api);
- if (ai.flows[fd].set == NULL) {
- reset_flow(fd);
- pthread_rwlock_unlock(&ai.flows_lock);
- pthread_rwlock_unlock(&ai.data_lock);
- return -1;
- }
pthread_rwlock_unlock(&ai.flows_lock);
pthread_rwlock_unlock(&ai.data_lock);
@@ -1162,21 +1162,47 @@ int ipcp_flow_req_arr(pid_t api, char * dst_name, char * src_ae_name)
if (recv_msg == NULL)
return -1;
- if (!recv_msg->has_port_id) {
+ if (!recv_msg->has_port_id || !recv_msg->has_api) {
irm_msg__free_unpacked(recv_msg, NULL);
return -1;
}
+ if (recv_msg->has_result && recv_msg->result) {
+ irm_msg__free_unpacked(recv_msg, NULL);
+ return -1;
+ }
+
port_id = recv_msg->port_id;
- irm_msg__free_unpacked(recv_msg, NULL);
- if (port_id < 0)
+ if (port_id < 0) {
+ irm_msg__free_unpacked(recv_msg, NULL);
return -1;
+ }
pthread_rwlock_rdlock(&ai.data_lock);
pthread_rwlock_wrlock(&ai.flows_lock);
ai.flows[fd].rx_rb = shm_rbuff_open(ai.api, port_id);
if (ai.flows[fd].rx_rb == NULL) {
+ irm_msg__free_unpacked(recv_msg, NULL);
+ reset_flow(fd);
+ pthread_rwlock_unlock(&ai.flows_lock);
+ pthread_rwlock_unlock(&ai.data_lock);
+ return -1;
+ }
+
+ ai.flows[fd].tx_rb = shm_rbuff_open(recv_msg->api, port_id);
+ if (ai.flows[fd].tx_rb == NULL) {
+ irm_msg__free_unpacked(recv_msg, NULL);
+ reset_flow(fd);
+ port_destroy(&ai.ports[port_id]);
+ pthread_rwlock_unlock(&ai.flows_lock);
+ pthread_rwlock_unlock(&ai.data_lock);
+ return -1;
+ }
+
+ ai.flows[fd].set = shm_flow_set_open(recv_msg->api);
+ if (ai.flows[fd].set == NULL) {
+ irm_msg__free_unpacked(recv_msg, NULL);
reset_flow(fd);
pthread_rwlock_unlock(&ai.flows_lock);
pthread_rwlock_unlock(&ai.data_lock);
@@ -1192,6 +1218,8 @@ int ipcp_flow_req_arr(pid_t api, char * dst_name, char * src_ae_name)
pthread_rwlock_unlock(&ai.flows_lock);
pthread_rwlock_unlock(&ai.data_lock);
+ irm_msg__free_unpacked(recv_msg, NULL);
+
return fd;
}
@@ -1224,27 +1252,6 @@ int ipcp_flow_alloc_reply(int fd, int response)
ret = recv_msg->result;
- pthread_rwlock_wrlock(&ai.flows_lock);
-
- ai.flows[fd].tx_rb = shm_rbuff_open(ai.flows[fd].api,
- ai.flows[fd].port_id);
- if (ai.flows[fd].tx_rb == NULL) {
- reset_flow(fd);
- pthread_rwlock_unlock(&ai.flows_lock);
- pthread_rwlock_unlock(&ai.data_lock);
- return -1;
- }
-
- ai.flows[fd].set = shm_flow_set_open(ai.flows[fd].api);
- if (ai.flows[fd].set == NULL) {
- reset_flow(fd);
- pthread_rwlock_unlock(&ai.flows_lock);
- pthread_rwlock_unlock(&ai.data_lock);
- return -1;
- }
-
- pthread_rwlock_unlock(&ai.flows_lock);
-
irm_msg__free_unpacked(recv_msg, NULL);
return ret;
@@ -1295,6 +1302,12 @@ int ipcp_flow_write(int fd, struct shm_du_buff * sdb)
pthread_rwlock_rdlock(&ai.data_lock);
pthread_rwlock_rdlock(&ai.flows_lock);
+ if (ai.flows[fd].port_id < 0) {
+ pthread_rwlock_unlock(&ai.flows_lock);
+ pthread_rwlock_unlock(&ai.data_lock);
+ return -ENOTALLOC;
+ }
+
if ((ai.flows[fd].oflags & FLOW_O_ACCMODE) == FLOW_O_RDONLY) {
pthread_rwlock_unlock(&ai.flows_lock);
pthread_rwlock_unlock(&ai.data_lock);
@@ -1356,7 +1369,11 @@ int local_flow_write(int fd, size_t idx)
pthread_rwlock_rdlock(&ai.data_lock);
pthread_rwlock_rdlock(&ai.flows_lock);
- assert(ai.flows[fd].tx_rb);
+ if (ai.flows[fd].port_id < 0) {
+ pthread_rwlock_unlock(&ai.flows_lock);
+ pthread_rwlock_unlock(&ai.data_lock);
+ return -ENOTALLOC;
+ }
shm_rbuff_write(ai.flows[fd].tx_rb, idx);
diff --git a/src/lib/shm_flow_set.c b/src/lib/shm_flow_set.c
index 3b1af83f..6cc94573 100644
--- a/src/lib/shm_flow_set.c
+++ b/src/lib/shm_flow_set.c
@@ -376,22 +376,20 @@ ssize_t shm_flow_set_wait(const struct shm_flow_set * shm_set,
while (shm_set->heads[idx] == 0 && ret != -ETIMEDOUT) {
if (timeout != NULL)
- ret = pthread_cond_timedwait(shm_set->conds + idx,
- shm_set->lock,
- &abstime);
+ ret = -pthread_cond_timedwait(shm_set->conds + idx,
+ shm_set->lock,
+ &abstime);
else
- ret = pthread_cond_wait(shm_set->conds + idx,
- shm_set->lock);
+ ret = -pthread_cond_wait(shm_set->conds + idx,
+ shm_set->lock);
#ifndef __APPLE__
- if (ret == EOWNERDEAD) {
+ if (ret == -EOWNERDEAD) {
LOG_DBG("Recovering dead mutex.");
pthread_mutex_consistent(shm_set->lock);
}
#endif
- if (ret == ETIMEDOUT) {
- ret = -ETIMEDOUT;
+ if (ret == -ETIMEDOUT)
break;
- }
}
if (ret != -ETIMEDOUT) {
@@ -404,5 +402,7 @@ ssize_t shm_flow_set_wait(const struct shm_flow_set * shm_set,
pthread_cleanup_pop(true);
+ assert(ret);
+
return ret;
}