summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorSander Vrijders <[email protected]>2016-06-22 16:05:56 +0200
committerSander Vrijders <[email protected]>2016-06-22 16:07:56 +0200
commitfa0a0dbc88adcf9cff8bf35a253f020a49ff4084 (patch)
tree4af7b471aeeff9d5f6224c48fcaf067104ac0a07 /src/lib
parentaa6255a605cac034089c78562c0d000aacd0af1e (diff)
downloadouroboros-fa0a0dbc88adcf9cff8bf35a253f020a49ff4084.tar.gz
ouroboros-fa0a0dbc88adcf9cff8bf35a253f020a49ff4084.zip
lib: Add errnos specific for Ouroboros
This adds error numbers specificly for Ouroboros. It also refactors some of the header files that are installed. Fixes #2
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/dev.c84
-rw-r--r--src/lib/instance_name.c4
-rw-r--r--src/lib/irm.c17
3 files changed, 69 insertions, 36 deletions
diff --git a/src/lib/dev.c b/src/lib/dev.c
index 1725cca3..d0f29ade 100644
--- a/src/lib/dev.c
+++ b/src/lib/dev.c
@@ -23,6 +23,7 @@
#define OUROBOROS_PREFIX "libouroboros-dev"
#include <ouroboros/config.h>
+#include <ouroboros/errno.h>
#include <ouroboros/logs.h>
#include <ouroboros/dev.h>
#include <ouroboros/sockets.h>
@@ -62,13 +63,13 @@ int ap_init(char * ap_name)
_ap_instance = malloc(sizeof(struct ap_data));
if (_ap_instance == NULL) {
- return -1;
+ return -ENOMEM;
}
_ap_instance->api = instance_name_create();
if (_ap_instance->api == NULL) {
free(_ap_instance);
- return -1;
+ return -ENOMEM;
}
if (instance_name_init_from(_ap_instance->api,
@@ -76,14 +77,14 @@ int ap_init(char * ap_name)
getpid()) == NULL) {
instance_name_destroy(_ap_instance->api);
free(_ap_instance);
- return -1;
+ return -ENOMEM;
}
_ap_instance->fds = bmp_create(AP_MAX_FLOWS, 0);
if (_ap_instance->fds == NULL) {
instance_name_destroy(_ap_instance->api);
free(_ap_instance);
- return -1;
+ return -ENOMEM;
}
_ap_instance->dum = shm_du_map_open();
@@ -91,7 +92,7 @@ int ap_init(char * ap_name)
instance_name_destroy(_ap_instance->api);
bmp_destroy(_ap_instance->fds);
free(_ap_instance);
- return -1;
+ return -ENOSHM;
}
_ap_instance->rb = shm_ap_rbuff_create();
@@ -100,7 +101,7 @@ int ap_init(char * ap_name)
shm_du_map_close(_ap_instance->dum);
bmp_destroy(_ap_instance->fds);
free(_ap_instance);
- return -1;
+ return -ENOSHM;
}
for (i = 0; i < AP_MAX_FLOWS; ++i) {
@@ -174,7 +175,7 @@ int flow_accept(char ** ae_name)
recv_msg = send_recv_irm_msg(&msg);
if (recv_msg == NULL) {
- return -1;
+ return -EIRM;
}
if (!recv_msg->has_pid || !recv_msg->has_port_id) {
@@ -199,7 +200,7 @@ int flow_accept(char ** ae_name)
pthread_rwlock_unlock(&_ap_instance->flows_lock);
pthread_rwlock_unlock(&_ap_instance->data_lock);
irm_msg__free_unpacked(recv_msg, NULL);
- return -1;
+ return -ENOSHM;
}
if (ae_name != NULL) {
@@ -210,7 +211,7 @@ int flow_accept(char ** ae_name)
pthread_rwlock_unlock(&_ap_instance->flows_lock);
pthread_rwlock_unlock(&_ap_instance->data_lock);
irm_msg__free_unpacked(recv_msg, NULL);
- return -1;
+ return -ENOMEM;
}
}
@@ -232,7 +233,7 @@ int flow_alloc_resp(int fd,
irm_msg_t * recv_msg = NULL;
int ret = -1;
- if (fd < 0)
+ if (fd < 0 || fd >= AP_MAX_FLOWS)
return -EBADF;
msg.code = IRM_MSG_CODE__IRM_FLOW_ALLOC_RESP;
@@ -243,6 +244,12 @@ int flow_alloc_resp(int fd,
pthread_rwlock_rdlock(&_ap_instance->data_lock);
pthread_rwlock_rdlock(&_ap_instance->flows_lock);
+ if (_ap_instance->flows[fd].port_id < 0) {
+ pthread_rwlock_unlock(&_ap_instance->flows_lock);
+ pthread_rwlock_unlock(&_ap_instance->data_lock);
+ return -ENOTALLOC;
+ }
+
msg.port_id = _ap_instance->flows[fd].port_id;
pthread_rwlock_unlock(&_ap_instance->flows_lock);
@@ -253,7 +260,7 @@ int flow_alloc_resp(int fd,
recv_msg = send_recv_irm_msg(&msg);
if (recv_msg == NULL) {
pthread_rwlock_unlock(&_ap_instance->data_lock);
- return -1;
+ return -EIRM;
}
if (!recv_msg->has_result) {
@@ -298,7 +305,7 @@ int flow_alloc(char * dst_name,
recv_msg = send_recv_irm_msg(&msg);
if (recv_msg == NULL) {
- return -1;
+ return -EIRM;
}
if (!recv_msg->has_pid || !recv_msg->has_port_id) {
@@ -323,7 +330,7 @@ int flow_alloc(char * dst_name,
pthread_rwlock_unlock(&_ap_instance->flows_lock);
pthread_rwlock_unlock(&_ap_instance->data_lock);
irm_msg__free_unpacked(recv_msg, NULL);
- return -1;
+ return -ENOSHM;
}
_ap_instance->flows[fd].port_id = recv_msg->port_id;
@@ -343,7 +350,7 @@ int flow_alloc_res(int fd)
irm_msg_t * recv_msg = NULL;
int result = 0;
- if (fd < 0)
+ if (fd < 0 || fd >= AP_MAX_FLOWS)
return -EBADF;
msg.code = IRM_MSG_CODE__IRM_FLOW_ALLOC_RES;
@@ -352,6 +359,12 @@ int flow_alloc_res(int fd)
pthread_rwlock_rdlock(&_ap_instance->data_lock);
pthread_rwlock_rdlock(&_ap_instance->flows_lock);
+ if (_ap_instance->flows[fd].port_id < 0) {
+ pthread_rwlock_unlock(&_ap_instance->flows_lock);
+ pthread_rwlock_unlock(&_ap_instance->data_lock);
+ return -ENOTALLOC;
+ }
+
msg.port_id = _ap_instance->flows[fd].port_id;
pthread_rwlock_unlock(&_ap_instance->flows_lock);
@@ -359,7 +372,7 @@ int flow_alloc_res(int fd)
recv_msg = send_recv_irm_msg(&msg);
if (recv_msg == NULL) {
- return -1;
+ return -EIRM;
}
if (!recv_msg->has_result) {
@@ -386,6 +399,12 @@ int flow_dealloc(int fd)
pthread_rwlock_rdlock(&_ap_instance->data_lock);
pthread_rwlock_wrlock(&_ap_instance->flows_lock);
+ if (_ap_instance->flows[fd].port_id < 0) {
+ pthread_rwlock_unlock(&_ap_instance->flows_lock);
+ pthread_rwlock_unlock(&_ap_instance->data_lock);
+ return -ENOTALLOC;
+ }
+
msg.port_id = _ap_instance->flows[fd].port_id;
_ap_instance->flows[fd].port_id = -1;
@@ -399,7 +418,7 @@ int flow_dealloc(int fd)
recv_msg = send_recv_irm_msg(&msg);
if (recv_msg == NULL) {
pthread_rwlock_unlock(&_ap_instance->data_lock);
- return -1;
+ return -EIRM;
}
if (!recv_msg->has_result) {
@@ -421,12 +440,18 @@ int flow_cntl(int fd, int cmd, int oflags)
{
int old;
- if (fd < 0)
+ if (fd < 0 || fd >= AP_MAX_FLOWS)
return -EBADF;
pthread_rwlock_rdlock(&_ap_instance->data_lock);
pthread_rwlock_wrlock(&_ap_instance->flows_lock);
+ if (_ap_instance->flows[fd].port_id < 0) {
+ pthread_rwlock_unlock(&_ap_instance->flows_lock);
+ pthread_rwlock_unlock(&_ap_instance->data_lock);
+ return -ENOTALLOC;
+ }
+
old = _ap_instance->flows[fd].oflags;
switch (cmd) {
@@ -454,12 +479,18 @@ ssize_t flow_write(int fd, void * buf, size_t count)
if (buf == NULL)
return 0;
- if (fd < 0)
+ if (fd < 0 || fd >= AP_MAX_FLOWS)
return -EBADF;
pthread_rwlock_rdlock(&_ap_instance->data_lock);
pthread_rwlock_rdlock(&_ap_instance->flows_lock);
+ if (_ap_instance->flows[fd].port_id < 0) {
+ pthread_rwlock_unlock(&_ap_instance->flows_lock);
+ pthread_rwlock_unlock(&_ap_instance->data_lock);
+ return -ENOTALLOC;
+ }
+
if (_ap_instance->flows[fd].oflags & FLOW_O_NONBLOCK) {
index = shm_create_du_buff(_ap_instance->dum,
count + DU_BUFF_HEADSPACE +
@@ -470,7 +501,7 @@ ssize_t flow_write(int fd, void * buf, size_t count)
if (index == -1) {
pthread_rwlock_unlock(&_ap_instance->flows_lock);
pthread_rwlock_unlock(&_ap_instance->data_lock);
- return -1;
+ return -EAGAIN;
}
e.index = index;
@@ -480,7 +511,7 @@ ssize_t flow_write(int fd, void * buf, size_t count)
shm_release_du_buff(_ap_instance->dum, index);
pthread_rwlock_unlock(&_ap_instance->flows_lock);
pthread_rwlock_unlock(&_ap_instance->data_lock);
- return -EPIPE;
+ return -ENOSHM;
}
} else { /* blocking */
while ((index = shm_create_du_buff(_ap_instance->dum,
@@ -510,7 +541,7 @@ ssize_t flow_read(int fd, void * buf, size_t count)
int n;
uint8_t * sdu;
- if (fd < 0)
+ if (fd < 0 || fd >= AP_MAX_FLOWS)
return -EBADF;
pthread_rwlock_rdlock(&_ap_instance->data_lock);
@@ -519,16 +550,17 @@ ssize_t flow_read(int fd, void * buf, size_t count)
if (_ap_instance->flows[fd].port_id < 0) {
pthread_rwlock_unlock(&_ap_instance->flows_lock);
pthread_rwlock_unlock(&_ap_instance->data_lock);
- return -1;
+ return -ENOTALLOC;
}
if (_ap_instance->flows[fd].oflags & FLOW_O_NONBLOCK) {
idx = shm_ap_rbuff_read_port(_ap_instance->rb,
_ap_instance->flows[fd].port_id);
} else { /* block */
- while ((idx = shm_ap_rbuff_read_port(
- _ap_instance->rb,
- _ap_instance->flows[fd].port_id)) < 0)
+ while ((idx =
+ shm_ap_rbuff_read_port(_ap_instance->rb,
+ _ap_instance->
+ flows[fd].port_id)) < 0)
;
}
@@ -536,7 +568,7 @@ ssize_t flow_read(int fd, void * buf, size_t count)
if (idx < 0) {
pthread_rwlock_unlock(&_ap_instance->data_lock);
- return -1;
+ return -EAGAIN;
}
n = shm_du_map_read_sdu(&sdu,
diff --git a/src/lib/instance_name.c b/src/lib/instance_name.c
index 10a432d5..844bb924 100644
--- a/src/lib/instance_name.c
+++ b/src/lib/instance_name.c
@@ -106,11 +106,11 @@ int instance_name_cpy(instance_name_t * dst,
instance_name_t * res;
if (src == NULL || dst == NULL)
- return -1;
+ return -EINVAL;
res = instance_name_init_from(dst, src->name, src->id);
if (res == NULL)
- return -1;
+ return -ENOMEM;
return 0;
}
diff --git a/src/lib/irm.c b/src/lib/irm.c
index ff5c3237..01ee498c 100644
--- a/src/lib/irm.c
+++ b/src/lib/irm.c
@@ -22,6 +22,7 @@
#define OUROBOROS_PREFIX "libouroboros-irm"
+#include <ouroboros/errno.h>
#include <ouroboros/irm.h>
#include <ouroboros/common.h>
#include <ouroboros/logs.h>
@@ -47,7 +48,7 @@ pid_t irm_create_ipcp(char * ipcp_name,
recv_msg = send_recv_irm_msg(&msg);
if (recv_msg == NULL)
- return -1;
+ return -EIRM;
if (recv_msg->has_result == false) {
irm_msg__free_unpacked(recv_msg, NULL);
@@ -76,7 +77,7 @@ int irm_destroy_ipcp(instance_name_t * api)
recv_msg = send_recv_irm_msg(&msg);
if (recv_msg == NULL)
- return -1;
+ return -EIRM;
if (recv_msg->has_result == false) {
irm_msg__free_unpacked(recv_msg, NULL);
@@ -143,12 +144,12 @@ int irm_bootstrap_ipcp(instance_name_t * api,
config.if_name = conf->if_name;
break;
default:
- return -1;
+ return -EIPCPTYPE;
}
recv_msg = send_recv_irm_msg(&msg);
if (recv_msg == NULL) {
- return -1;
+ return -EIRM;
}
if (recv_msg->has_result == false) {
@@ -180,14 +181,14 @@ int irm_enroll_ipcp(instance_name_t * api,
msg.dif_name = malloc(sizeof(*(msg.dif_name)));
if (msg.dif_name == NULL) {
LOG_ERR("Failed to malloc");
- return -1;
+ return -ENOMEM;
}
msg.dif_name[0] = dif_name;
recv_msg = send_recv_irm_msg(&msg);
if (recv_msg == NULL) {
free(msg.dif_name);
- return -1;
+ return -EIRM;
}
if (recv_msg->has_result == false) {
@@ -238,7 +239,7 @@ int irm_reg(char * name,
recv_msg = send_recv_irm_msg(&msg);
if (recv_msg == NULL)
- return -1;
+ return -EIRM;
if (recv_msg->has_result == false) {
irm_msg__free_unpacked(recv_msg, NULL);
@@ -284,7 +285,7 @@ int irm_unreg(char * name,
recv_msg = send_recv_irm_msg(&msg);
if (recv_msg == NULL)
- return -1;
+ return -EIRM;
if (recv_msg->has_result == false) {
irm_msg__free_unpacked(recv_msg, NULL);