ANNOUNCE new branch 'all-api' and reduced feature set on 'master'
by Angus Salkeld
Hi
I have just made a new branch 'all-api' which is at the same point as master is now.
I want to focus on getting the IPC ready for use by corosync and others. So
I will temporarly remove functionality from the master branch so
this can be distrubuted. Basically I don't want the entire API
used just yet.
I will re-add the other features once I am happy for them to be used.
Regards
-Angus
13 years, 6 months
[PATCH 1/2] RB: add qb_rb_chown()
by Angus Salkeld
change the ownership of the two mmap'ed files.
Signed-off-by: Angus Salkeld <asalkeld(a)redhat.com>
---
include/qb/qbrb.h | 10 ++++++++++
lib/ringbuffer.c | 16 +++++++++++++++-
2 files changed, 25 insertions(+), 1 deletions(-)
diff --git a/include/qb/qbrb.h b/include/qb/qbrb.h
index 953f376..e7c626b 100644
--- a/include/qb/qbrb.h
+++ b/include/qb/qbrb.h
@@ -248,6 +248,16 @@ ssize_t qb_rb_write_to_file(qb_ringbuffer_t * rb, int32_t fd);
*/
qb_ringbuffer_t *qb_rb_create_from_file(int32_t fd, uint32_t flags);
+/**
+ * Like 'chown' it changes the owner and group of the ringbuffers
+ * resources.
+ * @param owner uid of the owner to change to
+ * @param group gid of the group to change to
+ * @param rb ringbuffer instance
+ * @return status (0 = ok, -errno for error)
+ */
+int32_t qb_rb_chown(qb_ringbuffer_t * rb, uid_t owner, gid_t group);
+
/* *INDENT-OFF* */
#ifdef __cplusplus
diff --git a/lib/ringbuffer.c b/lib/ringbuffer.c
index 79702fe..3aa25a5 100644
--- a/lib/ringbuffer.c
+++ b/lib/ringbuffer.c
@@ -99,7 +99,7 @@ qb_ringbuffer_t *qb_rb_open(const char *name, size_t size, uint32_t flags)
fd_hdr = qb_util_mmap_file_open(path, name,
sizeof(struct qb_ringbuffer_shared_s),
file_flags);
- if (fd_hdr == -1) {
+ if (fd_hdr < 0) {
qb_util_log(LOG_ERR, "couldn't create file for mmap");
return NULL;
}
@@ -675,3 +675,17 @@ qb_ringbuffer_t *qb_rb_create_from_file(int32_t fd, uint32_t flags)
return rb;
}
+
+int32_t qb_rb_chown(qb_ringbuffer_t * rb, uid_t owner, gid_t group)
+{
+ int32_t res = chown(rb->shared_hdr->data_path, owner, group);
+ if (res < 0) {
+ return -errno;
+ }
+ res = chown(rb->shared_hdr->hdr_path, owner, group);
+ if (res < 0) {
+ return -errno;
+ }
+ return 0;
+}
+
--
1.7.2.3
13 years, 6 months
[PATCH 1/9] POLL: check for stop_requested before poll() as well.
by Angus Salkeld
Signed-off-by: Angus Salkeld <asalkeld(a)redhat.com>
---
lib/poll.c | 3 +++
1 files changed, 3 insertions(+), 0 deletions(-)
diff --git a/lib/poll.c b/lib/poll.c
index 36e061a..58dd237 100644
--- a/lib/poll.c
+++ b/lib/poll.c
@@ -441,6 +441,9 @@ int32_t qb_poll_run(qb_handle_t handle)
}
retry_poll:
+ if (poll_instance->stop_requested) {
+ return (0);
+ }
res = poll(poll_instance->ufds,
poll_instance->poll_entry_count,
expire_timeout_msec);
--
1.7.2.3
13 years, 6 months
[PATCH 1/6] Define new return status policy
by Angus Salkeld
Good >= 0 (0 = good, or positive value)
Bad < 0 (-errno)
Signed-off-by: Angus Salkeld <asalkeld(a)redhat.com>
---
include/tlist.h | 6 +--
lib/hash.c | 27 ++++--------
lib/hdb.c | 40 ++++++------------
lib/ipc_posix_mq.c | 65 +++++++++++++++++++----------
lib/ipc_shm.c | 37 ++++++++--------
lib/ipc_sysv_mq.c | 58 +++++++++++++++----------
lib/ipc_us.c | 95 +++++++++++++++++++++++------------------
lib/ipcc.c | 20 ++++----
lib/ipcs.c | 21 ++++-----
lib/poll.c | 23 +++-------
lib/ringbuffer.c | 106 +++++++++++++++++++++++++++-------------------
lib/ringbuffer_helper.c | 98 ++++++++++++++++++++++++++++---------------
lib/util.c | 30 +++++++-------
tests/bmc.c | 11 +++--
tests/bms.c | 2 +-
tests/check_rb.c | 12 +++---
16 files changed, 356 insertions(+), 295 deletions(-)
diff --git a/include/tlist.h b/include/tlist.h
index e324f80..a146235 100644
--- a/include/tlist.h
+++ b/include/tlist.h
@@ -154,8 +154,7 @@ static inline int32_t timerlist_add_absolute(struct timerlist *timerlist,
timer =
(struct timerlist_timer *)malloc(sizeof(struct timerlist_timer));
if (timer == 0) {
- errno = ENOMEM;
- return (-1);
+ return (-ENOMEM);
}
timer->expire_time = nano_from_epoch;
@@ -180,8 +179,7 @@ static inline int32_t timerlist_add_duration(struct timerlist *timerlist,
timer =
(struct timerlist_timer *)malloc(sizeof(struct timerlist_timer));
if (timer == 0) {
- errno = ENOMEM;
- return (-1);
+ return -ENOMEM;
}
timer->expire_time = timerlist_nano_current_get() + nano_duration;
diff --git a/lib/hash.c b/lib/hash.c
index e1dd623..472976c 100644
--- a/lib/hash.c
+++ b/lib/hash.c
@@ -80,14 +80,14 @@ int32_t qb_hash_initialize(qb_handle_t * handle,
res = qb_hdb_handle_create(&qb_hash_handle_db, size, handle);
if (res != 0) {
qb_util_log(LOG_ERR, "could not create handle (%s)",
- strerror(errno));
+ strerror(res));
return (res);
}
res =
qb_hdb_handle_get(&qb_hash_handle_db, *handle, (void *)&hash_table);
if (res != 0) {
qb_util_log(LOG_ERR, "could not get handle (%s)",
- strerror(errno));
+ strerror(res));
goto hash_destroy;
}
@@ -105,8 +105,8 @@ int32_t qb_hash_initialize(qb_handle_t * handle,
return (0);
hash_destroy:
- res = qb_hdb_handle_destroy(&qb_hash_handle_db, *handle);
- return (-1);
+ qb_hdb_handle_destroy(&qb_hash_handle_db, *handle);
+ return (res);
}
int32_t qb_hash_key_set(qb_handle_t handle,
@@ -141,9 +141,8 @@ int32_t qb_hash_key_set(qb_handle_t handle,
}
if (found == 0) {
hash_node = malloc(sizeof(struct hash_node) + strlen(key) + 1);
- if (hash_node == 0) {
- res = -1;
- errno = -ENOMEM;
+ if (hash_node == NULL) {
+ res = -ENOMEM;
goto error_exit;
}
@@ -178,7 +177,7 @@ int32_t qb_hash_key_get(qb_handle_t handle,
{
struct hash_table *hash_table;
uint32_t hash_entry;
- uint32_t res = -1;
+ int32_t res;
struct qb_list_head *list;
struct hash_node *hash_node;
@@ -187,7 +186,7 @@ int32_t qb_hash_key_get(qb_handle_t handle,
if (res != 0) {
return (res);
}
- res = -1;
+ res = -ENOENT;
hash_entry = hash_fnv(key, strlen(key), hash_table->order);
@@ -208,9 +207,6 @@ int32_t qb_hash_key_get(qb_handle_t handle,
unlock_exit:
pthread_mutex_unlock(&hash_table->hash_buckets[hash_entry].mutex);
qb_hdb_handle_put(&qb_hash_handle_db, handle);
- if (res == -1) {
- errno = ENOENT;
- }
return (res);
}
@@ -219,7 +215,7 @@ int32_t qb_hash_key_delete(qb_handle_t handle, const char *key)
struct hash_table *hash_table;
struct qb_list_head *list;
uint32_t hash_entry;
- uint32_t res = ENOENT;
+ int32_t res;
struct hash_node *hash_node;
res =
@@ -227,7 +223,7 @@ int32_t qb_hash_key_delete(qb_handle_t handle, const char *key)
if (res != 0) {
return (res);
}
- res = -1;
+ res = -ENOENT;
hash_entry = hash_fnv(key, strlen(key), hash_table->order);
pthread_mutex_lock(&hash_table->hash_buckets[hash_entry].mutex);
@@ -248,9 +244,6 @@ int32_t qb_hash_key_delete(qb_handle_t handle, const char *key)
unlock_exit:
pthread_mutex_unlock(&hash_table->hash_buckets[hash_entry].mutex);
qb_hdb_handle_put(&qb_hash_handle_db, handle);
- if (res == -1) {
- errno = ENOENT;
- }
return (res);
}
diff --git a/lib/hdb.c b/lib/hdb.c
index edec694..8fdcd92 100644
--- a/lib/hdb.c
+++ b/lib/hdb.c
@@ -73,16 +73,14 @@ int32_t qb_hdb_handle_create(struct qb_hdb *hdb, int32_t instance_size,
hdb->handle_count);
if (new_handles == NULL) {
qb_thread_unlock(hdb->lock);
- errno = ENOMEM;
- return (-1);
+ return (-errno);
}
hdb->handles = new_handles;
}
instance = malloc(instance_size);
if (instance == 0) {
- errno = ENOMEM;
- return (-1);
+ return (-ENOMEM);
}
/*
@@ -130,21 +128,18 @@ int32_t qb_hdb_handle_get(struct qb_hdb * hdb, qb_handle_t handle_in,
*instance = NULL;
if (handle >= hdb->handle_count) {
qb_thread_unlock(hdb->lock);
- errno = EBADF;
- return (-1);
+ return (-EBADF);
}
if (hdb->handles[handle].state != QB_HDB_HANDLE_STATE_ACTIVE) {
qb_thread_unlock(hdb->lock);
- errno = EBADF;
- return (-1);
+ return (-EBADF);
}
if (check != 0xffffffff && check != hdb->handles[handle].check) {
qb_thread_unlock(hdb->lock);
- errno = EBADF;
- return (-1);
+ return (-EBADF);
}
*instance = hdb->handles[handle].instance;
@@ -176,15 +171,13 @@ int32_t qb_hdb_handle_get_always(struct qb_hdb * hdb, qb_handle_t handle_in,
if (hdb->handles[handle].state == QB_HDB_HANDLE_STATE_EMPTY) {
qb_thread_unlock(hdb->lock);
- errno = EBADF;
- return (-1);
+ return (-EBADF);
}
if (check != 0xffffffff && check != hdb->handles[handle].check) {
qb_thread_unlock(hdb->lock);
- errno = EBADF;
- return (-1);
+ return (-EBADF);
}
*instance = hdb->handles[handle].instance;
@@ -209,15 +202,13 @@ int32_t qb_hdb_handle_put(struct qb_hdb * hdb, qb_handle_t handle_in)
if (handle >= hdb->handle_count) {
qb_thread_unlock(hdb->lock);
- errno = EBADF;
- return (-1);
+ return (-EBADF);
}
if (check != 0xffffffff && check != hdb->handles[handle].check) {
qb_thread_unlock(hdb->lock);
- errno = EBADF;
- return (-1);
+ return (-EBADF);
}
hdb->handles[handle].ref_count -= 1;
@@ -248,15 +239,12 @@ int32_t qb_hdb_handle_destroy(struct qb_hdb * hdb, qb_handle_t handle_in)
if (handle >= hdb->handle_count) {
qb_thread_unlock(hdb->lock);
-
- errno = EBADF;
- return (-1);
+ return (-EBADF);
}
if (check != 0xffffffff && check != hdb->handles[handle].check) {
qb_thread_unlock(hdb->lock);
- errno = EBADF;
- return (-1);
+ return (-EBADF);
}
hdb->handles[handle].state = QB_HDB_HANDLE_STATE_PENDINGREMOVAL;
@@ -280,14 +268,12 @@ int32_t qb_hdb_handle_refcount_get(struct qb_hdb * hdb, qb_handle_t handle_in)
if (handle >= hdb->handle_count) {
qb_thread_unlock(hdb->lock);
- errno = EBADF;
- return (-1);
+ return (-EBADF);
}
if (check != 0xffffffff && check != hdb->handles[handle].check) {
qb_thread_unlock(hdb->lock);
- errno = EBADF;
- return (-1);
+ return (-EBADF);
}
refcount = hdb->handles[handle].ref_count;
diff --git a/lib/ipc_posix_mq.c b/lib/ipc_posix_mq.c
index 95de6ef..32d93d4 100644
--- a/lib/ipc_posix_mq.c
+++ b/lib/ipc_posix_mq.c
@@ -50,8 +50,8 @@ static int32_t posix_mq_increase_limits(size_t max_msg_size,
if (proc_fd > 0) {
res = fscanf(proc_fd, "%d", &msgsize_max);
} else {
+ res = -errno;
qb_util_log(LOG_ERR, "fopen failed");
- res = -1;
}
if (res == 1) {
if (msgsize_max <= max_msg_size) {
@@ -61,21 +61,23 @@ static int32_t posix_mq_increase_limits(size_t max_msg_size,
}
} else {
qb_util_log(LOG_ERR, "fscanf failed");
- return -1;
+ return res;
}
fclose(proc_fd);
#ifdef QB_LINUX
if (getrlimit(RLIMIT_MSGQUEUE, &rlim) != 0) {
+ res = -errno;
qb_util_log(LOG_ERR, "getrlimit failed");
- return -1;
+ return res;
}
q_limit = (max_msg_size * q_len * 4) / 3;
rlim.rlim_cur += q_limit;
rlim.rlim_max += q_limit;
if (setrlimit(RLIMIT_MSGQUEUE, &rlim) != 0) {
+ res = -errno;
qb_util_log(LOG_ERR, "setrlimit failed");
- return -1;
+ return res;
}
#endif /* QB_LINUX */
@@ -115,15 +117,23 @@ static mqd_t posix_mq_create(const char* mq_name, size_t max_msg_size,
static int32_t qb_ipcc_pmq_send(struct qb_ipcc_connection *c,
const void *msg_ptr, size_t msg_len)
{
- return mq_send(c->u.pmq.request.q, msg_ptr, msg_len, 1);
+ int32_t res = mq_send(c->u.pmq.request.q, msg_ptr, msg_len, 1);
+ if (res < 0) {
+ return -errno;
+ }
+ return 0;
}
static ssize_t qb_ipcc_pmq_recv(struct qb_ipcc_connection *c,
const void *msg_ptr, size_t msg_len)
{
uint32_t msg_prio;
- return mq_receive(c->u.pmq.response.q, (char *)msg_ptr, c->max_msg_size,
+ ssize_t res = mq_receive(c->u.pmq.response.q, (char *)msg_ptr, c->max_msg_size,
&msg_prio);
+ if (res < 0) {
+ return -errno;
+ }
+ return 0;
}
static void qb_ipcc_pmq_disconnect(struct qb_ipcc_connection *c)
@@ -168,7 +178,7 @@ static int32_t _ipcc_pmq_connect_to_service_(struct qb_ipcc_connection *c)
mq_send(c->u.pmq.request.q, (const char *)&start, start.hdr.size,
30);
if (res == -1) {
- res = errno;
+ res = -errno;
perror("mq_send");
return res;
}
@@ -184,7 +194,7 @@ mq_recv_again:
goto mq_recv_again;
}
if (size == -1) {
- res = errno;
+ res = -errno;
perror("_ipcc_pmq_connect_to_service_:mq_receive");
goto cleanup;
}
@@ -196,7 +206,6 @@ mq_recv_again:
}
cleanup:
-
return res;
}
@@ -209,8 +218,7 @@ int32_t qb_ipcc_pmq_connect(struct qb_ipcc_connection * c)
c->funcs.disconnect = qb_ipcc_pmq_disconnect;
if (strlen(c->name) > (NAME_MAX - 20)) {
- errno = EINVAL;
- return -1;
+ return -EINVAL;
}
/* Connect to the service's request message queue.
@@ -220,8 +228,9 @@ int32_t qb_ipcc_pmq_connect(struct qb_ipcc_connection * c)
c->u.pmq.request.q = mq_open(c->u.pmq.request.name,
O_WRONLY | O_NONBLOCK);
if (c->u.pmq.request.q == (mqd_t)-1) {
+ res = -errno;
perror("mq_open:REQUEST");
- return -1;
+ return res;
}
/* Create the response message queue.
@@ -236,6 +245,7 @@ int32_t qb_ipcc_pmq_connect(struct qb_ipcc_connection * c)
O_RDONLY | O_CREAT | O_EXCL | O_NONBLOCK);
if (c->u.pmq.response.q == (mqd_t)-1) {
+ res = -errno;
perror("mq_open:RESPONSE");
goto cleanup_request;
}
@@ -250,6 +260,7 @@ int32_t qb_ipcc_pmq_connect(struct qb_ipcc_connection * c)
O_RDONLY | O_CREAT | O_EXCL | O_NONBLOCK);
if (c->u.pmq.dispatch.q == (mqd_t)-1) {
+ res = -errno;
perror("mq_open:DISPATCH");
goto cleanup_request_response;
}
@@ -271,7 +282,7 @@ cleanup_request_response:
cleanup_request:
mq_close(c->u.pmq.request.q);
- return -1;
+ return res;
}
@@ -335,7 +346,7 @@ static int32_t qb_ipcs_pmq_connect(struct qb_ipcs_service *s,
c->u.pmq.response.q = mq_open(c->u.pmq.response.name,
O_WRONLY | O_NONBLOCK);
if (c->u.pmq.response.q == (mqd_t)-1) {
- res = errno;
+ res = -errno;
perror("mq_open:RESPONSE");
return res;
}
@@ -351,7 +362,7 @@ static int32_t qb_ipcs_pmq_connect(struct qb_ipcs_service *s,
O_WRONLY | O_NONBLOCK);
if (c->u.pmq.dispatch.q == (mqd_t)-1) {
- res = errno;
+ res = -errno;
perror("mq_open:DISPATCH");
goto cleanup_response;
}
@@ -367,7 +378,7 @@ static int32_t qb_ipcs_pmq_connect(struct qb_ipcs_service *s,
mq_send(c->u.pmq.response.q, (const char *)&accept_msg,
sizeof(struct mar_res_setup), 30);
if (res == -1) {
- res = errno;
+ res = -errno;
perror("mq_send:RESPONSE");
goto cleanup_response;
}
@@ -387,8 +398,11 @@ static ssize_t qb_ipcs_pmq_request_recv(struct qb_ipcs_service *s, void *buf,
size_t buf_size)
{
uint32_t msg_prio;
-
- return mq_receive(s->u.q, buf, buf_size, &msg_prio);
+ ssize_t res = mq_receive(s->u.q, buf, buf_size, &msg_prio);
+ if (res == -1) {
+ return -errno;
+ }
+ return res;
}
#if 0
@@ -401,19 +415,25 @@ static int32_t qb_ipcs_pmq_fd_get(struct qb_ipcs_service *s)
static ssize_t qb_ipcs_pmq_response_send(struct qb_ipcs_connection *c,
void *data, size_t size)
{
- return mq_send(c->u.pmq.response.q, (const char *)data, size, 1);
+ if (mq_send(c->u.pmq.response.q, (const char *)data, size, 1) == -1) {
+ return -errno;
+ }
+ return size;
}
static ssize_t qb_ipcs_pmq_dispatch_send(struct qb_ipcs_connection *c,
void *data, size_t size)
{
- return mq_send(c->u.pmq.dispatch.q, (const char *)data, size, 1);
+ if (mq_send(c->u.pmq.dispatch.q, (const char *)data, size, 1) == -1) {
+ return -errno;
+ }
+ return size;
}
-
int32_t qb_ipcs_pmq_create(struct qb_ipcs_service *s)
{
char mq_name[NAME_MAX];
+ int32_t res;
snprintf(mq_name, NAME_MAX, "/%s", s->name);
@@ -427,8 +447,9 @@ int32_t qb_ipcs_pmq_create(struct qb_ipcs_service *s)
s->u.q = posix_mq_create(mq_name, s->max_msg_size,
(O_RDONLY | O_CREAT | O_EXCL | O_NONBLOCK));
if (s->u.q == (mqd_t)-1) {
+ res = -errno;
perror("posix_mq_create:REQUEST");
- return -1;
+ return res;
}
qb_util_log(LOG_DEBUG, "%s() %d", __func__, s->u.q);
diff --git a/lib/ipc_shm.c b/lib/ipc_shm.c
index 5be55c4..46042c6 100644
--- a/lib/ipc_shm.c
+++ b/lib/ipc_shm.c
@@ -53,9 +53,9 @@ static int32_t qb_ipcc_shm_send(struct qb_ipcc_connection *c,
static ssize_t qb_ipcc_shm_recv(struct qb_ipcc_connection *c,
const void *msg_ptr, size_t msg_len)
{
- int32_t res = qb_rb_chunk_read(c->u.shm.response.rb, (void*)msg_ptr, msg_len, 0);
- if (res == -1 && errno == ETIMEDOUT) {
- errno = EAGAIN;
+ ssize_t res = qb_rb_chunk_read(c->u.shm.response.rb, (void*)msg_ptr, msg_len, 0);
+ if (res == -ETIMEDOUT) {
+ return -EAGAIN;
}
return res;
}
@@ -75,9 +75,7 @@ static int32_t _ipcc_shm_connect_to_service_(struct qb_ipcc_connection *c)
strcpy(start.dispatch, qb_rb_name_get(c->u.shm.dispatch.rb));
res = qb_rb_chunk_write(c->u.shm.request.rb, (const char *)&start, start.hdr.size);
- if (res == -1) {
- res = errno;
- perror("mq_send");
+ if (res < 0) {
return res;
}
if (c->needs_sock_for_poll) {
@@ -88,8 +86,8 @@ static int32_t _ipcc_shm_connect_to_service_(struct qb_ipcc_connection *c)
size = qb_rb_chunk_read(c->u.shm.response.rb, c->receive_buf,
c->max_msg_size, 100000);
- if (size == -1) {
- res = errno;
+ if (size < 0) {
+ res = size;
perror("_ipcc_shm_connect_to_service_:qb_rb_chunk_read");
goto cleanup;
}
@@ -101,7 +99,6 @@ static int32_t _ipcc_shm_connect_to_service_(struct qb_ipcc_connection *c)
}
cleanup:
-
return res;
}
@@ -151,6 +148,7 @@ int32_t qb_ipcc_shm_connect(struct qb_ipcc_connection * c)
QB_RB_FLAG_CREATE | QB_RB_FLAG_SHARED_PROCESS);
if (c->u.shm.dispatch.rb == NULL) {
+ res = -errno;
perror("qb_rb_open:DISPATCH");
goto cleanup_request_response;
}
@@ -170,7 +168,7 @@ cleanup_request_response:
cleanup_request:
qb_rb_close(c->u.shm.request.rb);
- return -1;
+ return res;
}
@@ -237,7 +235,7 @@ static int32_t qb_ipcs_shm_connect(struct qb_ipcs_service *s,
s->max_msg_size,
QB_RB_FLAG_SHARED_PROCESS);
if (c->u.shm.response.rb == NULL) {
- res = errno;
+ res = -errno;
perror("qb_rb_open:RESPONSE");
return res;
}
@@ -251,7 +249,7 @@ static int32_t qb_ipcs_shm_connect(struct qb_ipcs_service *s,
QB_RB_FLAG_SHARED_PROCESS);
if (c->u.shm.dispatch.rb == NULL) {
- res = errno;
+ res = -errno;
perror("mq_open:DISPATCH");
goto cleanup_response;
}
@@ -266,8 +264,8 @@ static int32_t qb_ipcs_shm_connect(struct qb_ipcs_service *s,
qb_util_log(LOG_DEBUG, "%s:sending response", __func__);
res = qb_rb_chunk_write(c->u.shm.response.rb, (const char *)&accept_msg,
sizeof(struct mar_res_setup));
- if (res == -1) {
- res = errno;
+ if (res < 0) {
+ res = -errno;
perror("qb_rb_chunk_write:RESPONSE");
goto cleanup_response;
}
@@ -287,8 +285,8 @@ static ssize_t qb_ipcs_shm_request_recv(struct qb_ipcs_service *s, void *buf,
size_t buf_size)
{
int32_t res = qb_rb_chunk_read(s->u.rb, buf, buf_size, 0);
- if (res == -1 && errno == ETIMEDOUT) {
- errno = EAGAIN;
+ if (res == -ETIMEDOUT) {
+ return -EAGAIN;
}
return res;
}
@@ -308,6 +306,8 @@ static ssize_t qb_ipcs_shm_dispatch_send(struct qb_ipcs_connection *c,
int32_t qb_ipcs_shm_create(struct qb_ipcs_service *s)
{
+ int32_t res = 0;
+
s->funcs.destroy = qb_ipcs_shm_destroy;
s->funcs.request_recv = qb_ipcs_shm_request_recv;
s->funcs.response_send = qb_ipcs_shm_response_send;
@@ -317,10 +317,11 @@ int32_t qb_ipcs_shm_create(struct qb_ipcs_service *s)
s->u.rb = qb_rb_open(s->name, s->max_msg_size,
QB_RB_FLAG_CREATE | QB_RB_FLAG_SHARED_PROCESS);
if (s->u.rb == NULL) {
+ res = -errno;
perror("qb_rb_open:REQUEST");
- return -1;
+ return res;
}
qb_util_log(LOG_DEBUG, "%s() %d", __func__, s->u.q);
- return 0;
+ return res;
}
diff --git a/lib/ipc_sysv_mq.c b/lib/ipc_sysv_mq.c
index cd614fb..89eca16 100644
--- a/lib/ipc_sysv_mq.c
+++ b/lib/ipc_sysv_mq.c
@@ -46,24 +46,28 @@ static int32_t sysv_mq_create(struct qb_ipcs_service *s,
mq->q = msgget(mq->key, IPC_CREAT | IPC_NOWAIT);
if (mq->q == -1) {
+ res = -errno;
perror("msgget:REQUEST");
- return -1;
+ return res;
}
res = msgctl(s->u.smq.q, IPC_STAT, &info);
if (res != 0) {
+ res = -errno;
perror("msgctl");
qb_util_log(LOG_ERR, "error getting mq info");
+ return res;
}
info.msg_qbytes = 10 * MSGMAX;
res = msgctl(s->u.smq.q, IPC_SET, &info);
if (res != 0) {
+ res = -errno;
perror("msgctl");
qb_util_log(LOG_ERR, "error changing msg_qbytes to %d",
10 * MSGMAX);
}
- return 0;
+ return res;
}
static int32_t sysv_mq_unnamed_create(struct qb_ipcc_smq_one_way *queue)
@@ -74,7 +78,7 @@ retry_creating_the_q:
if (queue->q == -1 && errno == EEXIST) {
goto retry_creating_the_q;
} else if (queue->q == -1) {
- return -1;
+ return -errno;
}
return 0;
}
@@ -198,35 +202,38 @@ int32_t qb_ipcc_smq_connect(struct qb_ipcc_connection * c)
if (strlen(c->name) > (NAME_MAX - 20)) {
free(c);
- errno = EINVAL;
- return -1;
+ return EINVAL;
}
/* Connect to the service's request message queue.
*/
c->u.smq.request.key = sysv_key_from_name(c->name);
if (c->u.smq.request.key == -1) {
+ res = -errno;
perror("ftok:REQUEST");
free(c);
- return -1;
+ return res;
}
c->u.smq.request.q = msgget(c->u.smq.request.key, IPC_NOWAIT);
if (c->u.smq.request.q == -1) {
+ res = -errno;
perror("msgget:REQUEST");
free(c);
- return -1;
+ return res;
}
/* Create the response message queue.
*/
- if (sysv_mq_unnamed_create(&c->u.smq.response) == -1) {
+ res = sysv_mq_unnamed_create(&c->u.smq.response);
+ if (res < 0) {
perror("msgget:RESPONSE");
goto cleanup_request;
}
/* Create the dispatch message queue.
*/
- if (sysv_mq_unnamed_create(&c->u.smq.dispatch) == -1) {
+ res = sysv_mq_unnamed_create(&c->u.smq.dispatch);
+ if (res < 0) {
perror("msgget:DISPATCH");
goto cleanup_request_response;
}
@@ -246,18 +253,13 @@ cleanup_request_response:
cleanup_request:
free(c);
- return -1;
+ return res;
}
+
/*
* service functions
* --------------------------------------------------------
*/
-
-
-
-
-
-
static void qb_ipcs_smq_disconnect(struct qb_ipcs_connection *c)
{
struct qb_ipc_response_header msg;
@@ -301,7 +303,7 @@ static int32_t qb_ipcs_smq_connect(struct qb_ipcs_service *s,
struct qb_ipcs_connection *c, void *data,
size_t size)
{
- int32_t res;
+ int32_t res = 0;
struct mar_req_smq_setup *init = (struct mar_req_smq_setup *)data;
struct mar_res_setup accept_msg;
@@ -316,7 +318,7 @@ static int32_t qb_ipcs_smq_connect(struct qb_ipcs_service *s,
c->u.smq.response.key = init->response_key;
c->u.smq.response.q = msgget(c->u.smq.response.key, IPC_NOWAIT);
if (c->u.smq.response.q == -1) {
- res = errno;
+ res = -errno;
perror("msgget:RESPONSE");
goto cleanup;
}
@@ -326,7 +328,7 @@ static int32_t qb_ipcs_smq_connect(struct qb_ipcs_service *s,
c->u.smq.dispatch.key = init->dispatch_key;
c->u.smq.dispatch.q = msgget(c->u.smq.dispatch.key, IPC_NOWAIT);
if (c->u.smq.dispatch.q == -1) {
- res = errno;
+ res = -errno;
perror("msgget:DISPATCH");
goto cleanup_response;
}
@@ -339,7 +341,7 @@ static int32_t qb_ipcs_smq_connect(struct qb_ipcs_service *s,
res = msgsnd(c->u.smq.response.q, (const char *)&accept_msg,
sizeof(struct mar_res_setup), 0);
if (res == -1) {
- res = errno;
+ res = -errno;
perror("msgsnd:RESPONSE");
goto cleanup_response;
}
@@ -378,20 +380,30 @@ static ssize_t qb_ipcs_smq_request_recv(struct qb_ipcs_service *s, void *buf,
if (res == -1 && errno == ENOMSG) {
return 0;
}
+ if (res == -1) {
+ return -errno;
+ }
return res;
}
static ssize_t qb_ipcs_smq_response_send(struct qb_ipcs_connection *c,
void *data, size_t size)
{
- //qb_util_log(LOG_INFO, "%s()", __func__);
- return msgsnd(c->u.smq.response.q, (const char *)data, size, 0);
+ ssize_t res = msgsnd(c->u.smq.response.q, (const char *)data, size, 0);
+ if (res == -1) {
+ return -errno;
+ }
+ return res;
}
static ssize_t qb_ipcs_smq_dispatch_send(struct qb_ipcs_connection *c,
void *data, size_t size)
{
- return msgsnd(c->u.smq.dispatch.q, (const char *)data, size, 0);
+ ssize_t res = msgsnd(c->u.smq.dispatch.q, (const char *)data, size, 0);
+ if (res == -1) {
+ return -errno;
+ }
+ return res;
}
int32_t qb_ipcs_smq_create(struct qb_ipcs_service * s)
diff --git a/lib/ipc_us.c b/lib/ipc_us.c
index 2682dee..a4924e5 100644
--- a/lib/ipc_us.c
+++ b/lib/ipc_us.c
@@ -63,7 +63,7 @@ int32_t qb_ipc_us_send(int32_t s, const void *msg, size_t len)
struct msghdr msg_send;
struct iovec iov_send;
char *rbuf = (char *)msg;
- int processed = 0;
+ int32_t processed = 0;
msg_send.msg_iov = &iov_send;
msg_send.msg_iovlen = 1;
@@ -88,23 +88,22 @@ retry_send:
goto retry_send;
}
if (result == -1) {
- return errno;
+ return -errno;
}
processed += result;
if (processed != len) {
goto retry_send;
}
-
- return 0;
+ return processed;
}
-static int32_t qb_ipc_us_recv_msghdr(int32_t s,
+static ssize_t qb_ipc_us_recv_msghdr(int32_t s,
struct msghdr *hdr, const char *msg,
size_t len)
{
- int result;
- int processed = 0;
+ int32_t result;
+ int32_t processed = 0;
retry_recv:
hdr->msg_iov->iov_base = (void *)&msg[processed];
@@ -115,14 +114,14 @@ retry_recv:
goto retry_recv;
}
if (result == -1) {
- return errno;
+ return -errno;
}
#if defined(QB_SOLARIS) || defined(QB_BSD) || defined(QB_DARWIN)
/* On many OS poll never return POLLHUP or POLLERR.
* EOF is detected when recvmsg return 0.
*/
if (result == 0) {
- return errno; //ENOTCONN
+ return -errno; //ENOTCONN
}
#endif
@@ -132,7 +131,7 @@ retry_recv:
}
assert(processed == len);
- return 0;
+ return processed;
}
int32_t qb_ipc_us_recv(int32_t s, void *msg, size_t len)
@@ -159,10 +158,8 @@ int32_t qb_ipc_us_recv(int32_t s, void *msg, size_t len)
static int32_t qb_ipcs_uc_recv_and_auth(struct qb_ipcs_connection *c)
{
int32_t res = 0;
- int32_t recv_res = 0;
struct msghdr msg_recv;
struct iovec iov_recv;
- int32_t authenticated = QB_FALSE;
char setup_msg[sizeof(struct mar_req_initial_setup)];
#ifdef QB_LINUX
@@ -192,15 +189,17 @@ static int32_t qb_ipcs_uc_recv_and_auth(struct qb_ipcs_connection *c)
setsockopt(c->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
#endif
- recv_res = qb_ipc_us_recv_msghdr(c->sock, &msg_recv, setup_msg,
+ res = qb_ipc_us_recv_msghdr(c->sock, &msg_recv, setup_msg,
sizeof(struct mar_req_initial_setup));
- if (recv_res != 0) {
- authenticated = QB_FALSE;
- res = recv_res;
+ if (res < 0) {
goto cleanup_and_return;
}
- res = -1;
+ if (res != sizeof(struct mar_req_initial_setup)) {
+ res = -EIO;
+ goto cleanup_and_return;
+ }
+ res = -EBADMSG;
/*
* currently support getpeerucred, getpeereid, and SO_PASSCRED credential
@@ -219,6 +218,8 @@ static int32_t qb_ipcs_uc_recv_and_auth(struct qb_ipcs_connection *c)
c->egid = ucred_getegid(uc);
c->pid = ucred_getpid(uc);
ucred_free(uc);
+ } else {
+ res = -errno;
}
}
#elif HAVE_GETPEEREID
@@ -230,7 +231,11 @@ static int32_t qb_ipcs_uc_recv_and_auth(struct qb_ipcs_connection *c)
* TODO get the peer's pid.
* c->pid = ?;
*/
- res = getpeereid(c->sock, &c->euid, &c->egid);
+ if (getpeereid(c->sock, &c->euid, &c->egid) == 0) {
+ res = 0;
+ } else {
+ res = -errno;
+ }
}
#elif SO_PASSCRED
@@ -245,9 +250,11 @@ static int32_t qb_ipcs_uc_recv_and_auth(struct qb_ipcs_connection *c)
c->pid = cred->pid;
c->euid = cred->uid;
c->egid = cred->gid;
+ } else {
+ res = -EBADMSG;
}
#else /* no credentials */
- authenticated = QB_TRUE;
+ res = -ENOTSUP;
#endif /* no credentials */
cleanup_and_return:
@@ -257,20 +264,17 @@ cleanup_and_return:
#endif
if (res == 0) {
+ res = -EACCES;
if (c->service->serv_fns.connection_authenticate &&
- c->service->serv_fns.connection_authenticate(c->handle,
- c->euid,
- c->egid)) {
- authenticated = QB_TRUE;
+ c->service->serv_fns.connection_authenticate(c->handle,
+ c->euid,
+ c->egid)) {
+ res = 0;
} else if (c->service->serv_fns.connection_authenticate == NULL) {
- authenticated = QB_TRUE;
+ res = 0;
}
}
- if (!authenticated) {
- return -1;
- }
-
- return 1;
+ return res;
}
int32_t qb_ipcc_us_connect(const char *socket_name, int32_t * sock_pt)
@@ -284,7 +288,7 @@ int32_t qb_ipcc_us_connect(const char *socket_name, int32_t * sock_pt)
request_fd = socket(PF_LOCAL, SOCK_STREAM, 0);
#endif
if (request_fd == -1) {
- return errno;
+ return -errno;
}
#ifdef SO_NOSIGPIPE
socket_nosigpipe(request_fd);
@@ -313,7 +317,7 @@ error_connect:
close(request_fd);
*sock_pt = -1;
- return errno;
+ return -errno;
}
void qb_ipcc_us_disconnect(int32_t sock)
@@ -404,6 +408,7 @@ int32_t qb_ipcs_us_publish(struct qb_ipcs_service * s)
{
struct sockaddr_un un_addr;
int32_t res;
+ char error_str[100];
/*
* Create socket for IPC clients, name socket, listen for connections
@@ -414,16 +419,16 @@ int32_t qb_ipcs_us_publish(struct qb_ipcs_service * s)
s->server_sock = socket(PF_LOCAL, SOCK_STREAM, 0);
#endif
if (s->server_sock == -1) {
- char error_str[100];
+ res = -errno;
strerror_r(errno, error_str, 100);
qb_util_log(LOG_ERR,
"Cannot create server socket: %s\n", error_str);
- return -1;
+ return res;
}
res = fcntl(s->server_sock, F_SETFL, O_NONBLOCK);
if (res == -1) {
- char error_str[100];
+ res = -errno;
strerror_r(errno, error_str, 100);
qb_util_log(LOG_CRIT,
"Could not set non-blocking operation on server socket: %s\n",
@@ -444,6 +449,7 @@ int32_t qb_ipcs_us_publish(struct qb_ipcs_service * s)
struct stat stat_out;
res = stat(SOCKETDIR, &stat_out);
if (res == -1 || (res == 0 && !S_ISDIR(stat_out.st_mode))) {
+ res = -errno;
qb_util_log(LOG_CRIT,
"Required directory not present %s\n",
SOCKETDIR);
@@ -458,7 +464,7 @@ int32_t qb_ipcs_us_publish(struct qb_ipcs_service * s)
bind(s->server_sock, (struct sockaddr *)&un_addr,
QB_SUN_LEN(&un_addr));
if (res) {
- char error_str[100];
+ res = -errno;
strerror_r(errno, error_str, 100);
qb_util_log(LOG_CRIT,
"Could not bind AF_UNIX (%s): %s.\n",
@@ -482,7 +488,7 @@ int32_t qb_ipcs_us_publish(struct qb_ipcs_service * s)
error_close:
close(s->server_sock);
- return -1;
+ return res;
}
int32_t qb_ipcs_us_withdraw(struct qb_ipcs_service * s)
@@ -502,6 +508,7 @@ static int32_t qb_ipcs_us_connection_acceptor(qb_handle_t handle,
struct mar_res_initial_setup init_res;
int32_t res;
socklen_t addrlen = sizeof(struct sockaddr_un);
+ char error_str[100];
retry_accept:
new_fd = accept(fd, (struct sockaddr *)&un_addr, &addrlen);
@@ -510,7 +517,6 @@ retry_accept:
}
if (new_fd == -1) {
- char error_str[100];
strerror_r(errno, error_str, 100);
qb_util_log(LOG_ERR,
"Could not accept Library connection: %s\n",
@@ -520,7 +526,6 @@ retry_accept:
res = fcntl(new_fd, F_SETFL, O_NONBLOCK);
if (res == -1) {
- char error_str[100];
strerror_r(errno, error_str, 100);
qb_util_log(LOG_ERR,
"Could not set non-blocking operation on library connection: %s\n",
@@ -535,7 +540,8 @@ retry_accept:
c = qb_ipcs_connection_alloc(s);
c->sock = new_fd;
- if (qb_ipcs_uc_recv_and_auth(c) == 1) {
+ res = qb_ipcs_uc_recv_and_auth(c);
+ if (res == 0) {
qb_util_log(LOG_INFO, "IPC credentials authenticated");
qb_list_add(&c->list, &s->connections);
@@ -555,11 +561,16 @@ retry_accept:
}
} else {
- qb_util_log(LOG_ERR, "Invalid IPC credentials.");
-
+ if (res == -EACCES) {
+ qb_util_log(LOG_ERR, "Invalid IPC credentials.");
+ } else {
+ strerror_r(-res, error_str, 100);
+ qb_util_log(LOG_ERR, "Error in conection setup: %s.",
+ error_str);
+ }
init_res.hdr.id = QB_IPC_MSG_AUTHENTICATE;
init_res.hdr.size = sizeof(init_res);
- init_res.hdr.error = EACCES;
+ init_res.hdr.error = res;
init_res.session_id = 0;
qb_ipc_us_send(c->sock, &init_res, init_res.hdr.size);
diff --git a/lib/ipcc.c b/lib/ipcc.c
index a8035cd..b21f503 100644
--- a/lib/ipcc.c
+++ b/lib/ipcc.c
@@ -34,30 +34,31 @@ qb_ipcc_connection_t *qb_ipcc_connect(const char *name, enum qb_ipc_type type)
res = qb_ipcc_us_connect(name, &usock);
if (res != 0) {
- errno = res;
+ errno = -res;
return NULL;
}
init_req.hdr.id = QB_IPC_MSG_AUTHENTICATE;
init_req.hdr.size = sizeof(init_req);
res = qb_ipc_us_send(usock, &init_req, init_req.hdr.size);
- if (res != 0) {
+ if (res < 0) {
+ errno = -res;
perror("qb_ipc_us_send");
qb_ipcc_us_disconnect(usock);
- errno = res;
+ errno = -res;
return NULL;
}
res = qb_ipc_us_recv(usock, &init_res, sizeof(init_res));
- if (res != 0) {
+ if (res < 0) {
perror("qb_ipc_us_recv");
qb_ipcc_us_disconnect(usock);
- errno = res;
+ errno = -res;
return NULL;
}
if (init_res.hdr.error != 0) {
- errno = init_res.hdr.error;
+ errno = -init_res.hdr.error;
perror("recv:message");
return NULL;
}
@@ -97,7 +98,7 @@ qb_ipcc_connection_t *qb_ipcc_connect(const char *name, enum qb_ipc_type type)
if (res != 0) {
free(c);
c = NULL;
- errno = res;
+ errno = -res;
}
return c;
}
@@ -109,14 +110,13 @@ int32_t qb_ipcc_send(struct qb_ipcc_connection * c, const void *msg_ptr,
ssize_t res;
if (msg_len > c->max_msg_size) {
- errno = EINVAL;
- return -1;
+ return -EINVAL;
}
hdr = (struct qb_ipc_request_header *)msg_ptr;
hdr->session_id = c->session_id;
res = c->funcs.send(c, msg_ptr, msg_len);
- if (res != -1 && c->needs_sock_for_poll) {
+ if (res > 0 && c->needs_sock_for_poll) {
qb_ipc_us_send(c->sock, msg_ptr, 1);
}
return res;
diff --git a/lib/ipcs.c b/lib/ipcs.c
index 4d269c3..282531b 100644
--- a/lib/ipcs.c
+++ b/lib/ipcs.c
@@ -107,9 +107,9 @@ int32_t qb_ipcs_run(qb_ipcs_service_pt pt, qb_handle_t poll_handle)
res = qb_ipcs_us_publish(s);
qb_util_log(LOG_INFO, "%d", res);
- if (res == -1) {
+ if (res < 0) {
qb_hdb_handle_put(&qb_ipc_services, pt);
- return -1;
+ return res;
}
switch (s->type) {
@@ -126,7 +126,7 @@ int32_t qb_ipcs_run(qb_ipcs_service_pt pt, qb_handle_t poll_handle)
res = qb_ipcs_smq_create((struct qb_ipcs_service *)s);
break;
default:
- res = EINVAL;
+ res = -ENOPROTOOPT;
break;
}
qb_hdb_handle_put(&qb_ipc_services, pt);
@@ -151,9 +151,9 @@ ssize_t qb_ipcs_response_send(qb_ipcs_connection_pt c, void *data, size_t size)
ssize_t res;
struct qb_ipcs_connection *con;
- if (qb_hdb_handle_get(&qb_ipc_connections, c, (void **)&con) == -1) {
- perror("qb_hdb_handle_get");
- return -1;
+ res = qb_hdb_handle_get(&qb_ipc_connections, c, (void **)&con);
+ if (res < 0) {
+ return res;
}
res = con->service->funcs.response_send(con, data, size);
qb_hdb_handle_put(&qb_ipc_connections, c);
@@ -212,11 +212,10 @@ static int32_t _process_request_(struct qb_ipcs_service *s)
get_msg_with_live_connection:
res = s->funcs.request_recv(s, hdr, s->max_msg_size);
- if (res == -1 && errno == EAGAIN) {
+ if (res == -EAGAIN) {
goto get_msg_with_live_connection;
}
- if (res == -1) {
- perror("qb_ipcs_dispatch_request");
+ if (res < 0) {
goto cleanup;
}
if (qb_hdb_handle_get
@@ -240,7 +239,7 @@ get_msg_with_live_connection:
case QB_IPC_MSG_DISCONNECT:
qb_util_log(LOG_DEBUG, "%s() QB_IPC_MSG_DISCONNECT", __func__);
qb_ipcs_disconnect(c);
- res = -1;
+ res = -ESHUTDOWN;
break;
case QB_IPC_MSG_NEW_MESSAGE:
@@ -269,7 +268,7 @@ int32_t qb_ipcs_dispatch_connection_request(qb_handle_t handle,
if (revents & POLLHUP) {
qb_util_log(LOG_DEBUG, "%s HUP", __func__);
qb_ipcs_disconnect(c);
- return -1;
+ return -ESHUTDOWN;
}
if (c->service->needs_sock_for_poll) {
qb_ipc_us_recv(c->sock, &one_byte, 1);
diff --git a/lib/poll.c b/lib/poll.c
index 6b70461..36e061a 100644
--- a/lib/poll.c
+++ b/lib/poll.c
@@ -85,7 +85,7 @@ error_destroy:
qb_hdb_handle_destroy(&poll_instance_database, handle);
error_exit:
- return (-1);
+ return (res);
}
int32_t qb_poll_destroy(qb_handle_t handle)
@@ -96,7 +96,6 @@ int32_t qb_poll_destroy(qb_handle_t handle)
res = qb_hdb_handle_get(&poll_instance_database, handle,
(void *)&poll_instance);
if (res != 0) {
- res = -ENOENT;
goto error_exit;
}
@@ -128,7 +127,6 @@ int32_t qb_poll_dispatch_add(qb_handle_t handle,
res = qb_hdb_handle_get(&poll_instance_database, handle,
(void *)&poll_instance);
if (res != 0) {
- res = -ENOENT;
goto error_exit;
}
@@ -200,26 +198,24 @@ int32_t qb_poll_dispatch_modify(qb_handle_t handle,
res = qb_hdb_handle_get(&poll_instance_database, handle,
(void *)&poll_instance);
if (res != 0) {
- res = -ENOENT;
goto error_exit;
}
/*
* Find file descriptor to modify events and dispatch function
*/
+ res = -EBADF;
for (i = 0; i < poll_instance->poll_entry_count; i++) {
if (poll_instance->poll_entries[i].ufd.fd == fd) {
poll_instance->poll_entries[i].ufd.events = events;
poll_instance->poll_entries[i].dispatch_fn =
dispatch_fn;
- goto error_put;
+ res = 0;
+ break;
}
}
- res = -EBADF;
-
-error_put:
qb_hdb_handle_put(&poll_instance_database, handle);
error_exit:
@@ -235,7 +231,6 @@ int32_t qb_poll_dispatch_delete(qb_handle_t handle, int32_t fd)
res = qb_hdb_handle_get(&poll_instance_database, handle,
(void *)&poll_instance);
if (res != 0) {
- res = -ENOENT;
goto error_exit;
}
@@ -276,7 +271,6 @@ int32_t qb_poll_timer_add(qb_handle_t handle,
res = qb_hdb_handle_get(&poll_instance_database, handle,
(void *)&poll_instance);
if (res != 0) {
- res = -ENOENT;
goto error_exit;
}
@@ -301,7 +295,6 @@ int32_t qb_poll_timer_delete(qb_handle_t handle, qb_poll_timer_handle th)
res = qb_hdb_handle_get(&poll_instance_database, handle,
(void *)&poll_instance);
if (res != 0) {
- res = -ENOENT;
goto error_exit;
}
@@ -330,7 +323,6 @@ int32_t qb_poll_job_add(qb_handle_t poll_handle,
res = qb_hdb_handle_get(&poll_instance_database, poll_handle,
(void *)&poll_instance);
if (res != 0) {
- res = -ENOENT;
goto error_exit;
}
job = malloc(sizeof(struct qb_poll_job));
@@ -359,7 +351,6 @@ int32_t qb_poll_job_delete(qb_handle_t poll_handle, qb_poll_job_handle job_handl
res = qb_hdb_handle_get(&poll_instance_database, poll_handle,
(void *)&poll_instance);
if (res != 0) {
- res = -ENOENT;
goto error_exit;
}
qb_list_del(&job->list);
@@ -378,7 +369,6 @@ int32_t qb_poll_stop(qb_handle_t handle)
res = qb_hdb_handle_get(&poll_instance_database, handle,
(void *)&poll_instance);
if (res != 0) {
- res = -ENOENT;
goto error_exit;
}
@@ -460,6 +450,7 @@ retry_poll:
if (errno == EINTR && res == -1) {
goto retry_poll;
} else if (res == -1) {
+ res = -errno;
goto error_exit;
}
@@ -479,7 +470,7 @@ retry_poll:
/*
* Remove dispatch functions that return -1
*/
- if (res == -1) {
+ if (res < 0) {
poll_instance->poll_entries[i].ufd.fd = -1; /* empty entry */
}
}
@@ -489,7 +480,7 @@ retry_poll:
qb_hdb_handle_put(&poll_instance_database, handle);
error_exit:
- return (-1);
+ return res;
}
#ifdef COMPILE_OUT
diff --git a/lib/ringbuffer.c b/lib/ringbuffer.c
index 989eefe..d98c532 100644
--- a/lib/ringbuffer.c
+++ b/lib/ringbuffer.c
@@ -127,13 +127,13 @@ qb_ringbuffer_t *qb_rb_open(const char *name, size_t size, uint32_t flags)
rb->shared_hdr->count = 0;
strncpy(rb->shared_hdr->hdr_path, path, PATH_MAX);
}
- if (qb_rb_lock_create(rb, flags) == -1) {
+ if (qb_rb_lock_create(rb, flags) < 0) {
qb_util_log(LOG_ERR, "couldn't get a shared lock %s",
strerror(errno));
goto cleanup_hdr;
}
- if (qb_rb_sem_create(rb, flags) == -1) {
+ if (qb_rb_sem_create(rb, flags) < 0) {
qb_util_log(LOG_ERR, "couldn't get a semaphore %s",
strerror(errno));
goto cleanup_hdr;
@@ -152,7 +152,7 @@ qb_ringbuffer_t *qb_rb_open(const char *name, size_t size, uint32_t flags)
rb->shared_hdr->data_path,
real_size, file_flags);
}
- if (fd_data == -1) {
+ if (fd_data < 0) {
qb_util_log(LOG_ERR, "couldn't create file for mmap");
goto cleanup_hdr;
}
@@ -255,16 +255,19 @@ static size_t _qb_rb_space_free_locked_(qb_ringbuffer_t * rb)
ssize_t qb_rb_space_free(qb_ringbuffer_t * rb)
{
size_t space_free;
+ int32_t res = 0;
- if (rb->lock_fn(rb) == -1) {
- return -1;
+ res = rb->lock_fn(rb);
+ if (res < 0) {
+ return res;
}
space_free = _qb_rb_space_free_locked_(rb);
- if (rb->unlock_fn(rb) == -1) {
+ res = rb->unlock_fn(rb);
+ if (res < 0) {
/* aarg stuck locked! */
qb_util_log(LOG_ERR, "failed to unlock ringbuffer lock %s",
strerror(errno));
- return -1;
+ return res;
}
return space_free;
}
@@ -298,17 +301,20 @@ static size_t _qb_rb_space_used_locked_(qb_ringbuffer_t * rb)
ssize_t qb_rb_space_used(qb_ringbuffer_t * rb)
{
- size_t used;
+ ssize_t used = 0;
+ int32_t res = 0;
- if (rb->lock_fn(rb) == -1) {
- return -1;
+ res = rb->lock_fn(rb);
+ if (res < 0) {
+ return res;
}
used = _qb_rb_space_used_locked_(rb);
- if (rb->unlock_fn(rb) == -1) {
+ res = rb->unlock_fn(rb);
+ if (res < 0) {
/* aarg stuck locked! */
qb_util_log(LOG_ERR, "failed to unlock ringbuffer lock %s",
strerror(errno));
- return -1;
+ return res;
}
return used;
}
@@ -316,15 +322,19 @@ ssize_t qb_rb_space_used(qb_ringbuffer_t * rb)
ssize_t qb_rb_chunks_used(qb_ringbuffer_t * rb)
{
ssize_t count = -1;
- if (rb->lock_fn(rb) == -1) {
- return -1;
+ int32_t res = 0;
+
+ res = rb->lock_fn(rb);
+ if (res < 0) {
+ return res;
}
count = rb->shared_hdr->count;
- if (rb->unlock_fn(rb) == -1) {
+ res = rb->unlock_fn(rb);
+ if (res < 0) {
/* aarg stuck locked! */
qb_util_log(LOG_ERR, "failed to unlock ringbuffer lock %s",
strerror(errno));
- return -1;
+ return res;
}
return count;
}
@@ -333,7 +343,10 @@ void *qb_rb_chunk_alloc(qb_ringbuffer_t * rb, size_t len)
{
uint32_t write_pt;
- rb->lock_fn(rb);
+ if (rb->lock_fn(rb) < 0) {
+ return NULL;
+ }
+
/*
* Reclaim data if we are over writing and we need space
*/
@@ -346,6 +359,7 @@ void *qb_rb_chunk_alloc(qb_ringbuffer_t * rb, size_t len)
if (_qb_rb_space_free_locked_(rb) <
(len + QB_RB_CHUNK_HEADER_SIZE + 4)) {
rb->unlock_fn(rb);
+ errno = ENOMEM;
return NULL;
}
}
@@ -392,6 +406,8 @@ _qb_rb_chunk_step_locked_(qb_ringbuffer_t * rb, uint32_t pointer)
int32_t qb_rb_chunk_commit(qb_ringbuffer_t * rb, size_t len)
{
uint32_t old_write_pt = rb->shared_hdr->write_pt;
+ int32_t res = 0;
+
/*
* commit the magic & chunk_size
*/
@@ -408,10 +424,11 @@ int32_t qb_rb_chunk_commit(qb_ringbuffer_t * rb, size_t len)
rb->shared_hdr->read_pt, rb->shared_hdr->write_pt,
old_write_pt);
- if (rb->unlock_fn(rb) != 0) {
+ res = rb->unlock_fn(rb);
+ if (res < 0) {
qb_util_log(LOG_ERR, "failed to unlock ringbuffer lock %s",
- strerror(errno));
- return -1;
+ strerror(-res));
+ return res;
}
/*
@@ -423,14 +440,18 @@ int32_t qb_rb_chunk_commit(qb_ringbuffer_t * rb, size_t len)
ssize_t qb_rb_chunk_write(qb_ringbuffer_t * rb, const void *data, size_t len)
{
char *dest = qb_rb_chunk_alloc(rb, len);
+ int32_t res = 0;
if (dest == NULL) {
- return -1;
+ return -errno;
}
memcpy(dest, data, len);
- qb_rb_chunk_commit(rb, len);
+ res = qb_rb_chunk_commit(rb, len);
+ if (res < 0) {
+ return res;
+ }
return len;
}
@@ -474,16 +495,16 @@ ssize_t qb_rb_chunk_peek(qb_ringbuffer_t * rb, void **data_out, int32_t timeout)
int32_t res;
res = rb->sem_timedwait_fn(rb, timeout);
- if (res == -1 && errno == ETIMEDOUT && rb->shared_hdr->count > 0) {
+ if (res == -ETIMEDOUT && rb->shared_hdr->count > 0) {
qb_util_log(LOG_ERR,
"sem timedout but count is %zu",
rb->shared_hdr->count);
- } else if (res == -1 && errno != EIDRM) {
- if (errno != ETIMEDOUT) {
+ } else if (res < 0 && res != -EIDRM) {
+ if (res != ETIMEDOUT) {
qb_util_log(LOG_ERR,
- "sem_timedwait %s", strerror(errno));
+ "sem_timedwait %s", strerror(res));
}
- return -1;
+ return res;
}
read_pt = rb->shared_hdr->read_pt;
chunk_size = QB_RB_CHUNK_SIZE_GET(rb, read_pt);
@@ -492,7 +513,7 @@ ssize_t qb_rb_chunk_peek(qb_ringbuffer_t * rb, void **data_out, int32_t timeout)
if (chunk_magic != QB_RB_CHUNK_MAGIC) {
errno = ENOMSG;
- return -1;
+ return 0;
} else {
return chunk_size;
}
@@ -507,25 +528,23 @@ qb_rb_chunk_read(qb_ringbuffer_t * rb, void *data_out, size_t len,
int32_t res;
res = rb->sem_timedwait_fn(rb, timeout);
- if (res == -1 && errno != EIDRM) {
- if (errno != ETIMEDOUT) {
+ if (res < 0 && res != -EIDRM) {
+ if (res != -ETIMEDOUT) {
qb_util_log(LOG_ERR,
"sem_timedwait %s", strerror(errno));
}
- return -1;
+ return res;
}
- if (rb->lock_fn(rb) == -1) {
- res = errno;
+ res = rb->lock_fn(rb);
+ if (res < 0) {
qb_util_log(LOG_ERR, "could not lock ringbuffer %s",
- strerror(errno));
- errno = res;
- return -1;
+ strerror(-res));
+ return res;
}
if (_qb_rb_space_used_locked_(rb) == 0) {
rb->unlock_fn(rb);
- errno = ENOMSG;
- return -1;
+ return -ENOMSG;
}
read_pt = rb->shared_hdr->read_pt;
@@ -534,8 +553,7 @@ qb_rb_chunk_read(qb_ringbuffer_t * rb, void *data_out, size_t len,
if (len < chunk_size) {
rb->unlock_fn(rb);
- errno = ENOBUFS;
- return -1;
+ return -ENOBUFS;
}
memcpy(data_out,
@@ -587,7 +605,7 @@ ssize_t qb_rb_write_to_file(qb_ringbuffer_t * rb, int32_t fd)
result = write(fd, &rb->shared_hdr->size, sizeof(uint32_t));
if ((result < 0) || (result != sizeof(uint32_t))) {
- return -1;
+ return -errno;
}
written_size += result;
@@ -595,7 +613,7 @@ ssize_t qb_rb_write_to_file(qb_ringbuffer_t * rb, int32_t fd)
rb->shared_hdr->size * sizeof(uint32_t));
if ((result < 0)
|| (result != rb->shared_hdr->size * sizeof(uint32_t))) {
- return -1;
+ return -errno;
}
written_size += result;
@@ -605,12 +623,12 @@ ssize_t qb_rb_write_to_file(qb_ringbuffer_t * rb, int32_t fd)
result +=
write(fd, (void *)&rb->shared_hdr->write_pt, sizeof(uint32_t));
if ((result < 0) || (result != sizeof(uint32_t))) {
- return -1;
+ return -errno;
}
written_size += result;
result += write(fd, (void *)&rb->shared_hdr->read_pt, sizeof(uint32_t));
if ((result < 0) || (result != sizeof(uint32_t))) {
- return -1;
+ return -errno;
}
written_size += result;
diff --git a/lib/ringbuffer_helper.c b/lib/ringbuffer_helper.c
index 8d31638..88dd72c 100644
--- a/lib/ringbuffer_helper.c
+++ b/lib/ringbuffer_helper.c
@@ -56,11 +56,13 @@ sem_wait_again:
goto sem_wait_again;
break;
case EAGAIN:
- errno = ETIMEDOUT;
+ res = -ETIMEDOUT;
break;
case ETIMEDOUT:
+ res = -errno;
break;
default:
+ res = -errno;
qb_util_log(LOG_ERR,
"error waiting for semaphore : %s",
strerror(errno));
@@ -75,6 +77,7 @@ static int32_t my_sysv_sem_timedwait(qb_ringbuffer_t * rb, int32_t ms_timeout)
struct sembuf sops[1];
struct timespec ts_timeout;
struct timespec *ts_pt;
+ int32_t res = 0;
if (ms_timeout >= 0) {
/*
@@ -102,22 +105,25 @@ semop_again:
goto semop_again;
} else if (errno == EAGAIN) {
/* make consistent with sem_timedwait */
- errno = ETIMEDOUT;
+ res = -ETIMEDOUT;
} else {
+ res = -errno;
qb_util_log(LOG_ERR,
"error waiting for semaphore : %s",
strerror(errno));
}
-
- return -1;
+ return res;
}
-
return 0;
}
static int32_t my_posix_sem_post(qb_ringbuffer_t * rb)
{
- return sem_post(&rb->shared_hdr->posix_sem);
+ if (sem_post(&rb->shared_hdr->posix_sem) < 0) {
+ return -errno;
+ } else {
+ return 0;
+ }
}
static int32_t my_sysv_sem_post(qb_ringbuffer_t * rb)
@@ -142,19 +148,27 @@ semop_again:
strerror(errno));
}
- return -1;
+ return -errno;
}
return 0;
}
static int32_t my_posix_sem_destroy(qb_ringbuffer_t * rb)
{
- return sem_destroy(&rb->shared_hdr->posix_sem);
+ if (sem_destroy(&rb->shared_hdr->posix_sem) == -1) {
+ return -errno;
+ } else {
+ return 0;
+ }
}
static int32_t my_sysv_sem_destroy(qb_ringbuffer_t * rb)
{
- return semctl(rb->sem_id, 0, IPC_RMID, 0);
+ if (semctl(rb->sem_id, 0, IPC_RMID, 0) == -1) {
+ return -errno;
+ } else {
+ return 0;
+ }
}
static int32_t my_sysv_lock_it_create(qb_ringbuffer_t * rb, uint32_t flags)
@@ -166,26 +180,29 @@ static int32_t my_sysv_lock_it_create(qb_ringbuffer_t * rb, uint32_t flags)
sem_key = ftok(rb->shared_hdr->hdr_path, rb->shared_hdr->size);
if (sem_key == -1) {
+ res = -errno;
qb_util_log(LOG_ERR, "couldn't get a sem id %s",
strerror(errno));
- return -1;
+ return res;
}
if (flags & QB_RB_FLAG_CREATE) {
rb->lock_id = semget(sem_key, 1, IPC_CREAT | IPC_EXCL | 0600);
if (rb->lock_id == -1) {
+ res = -errno;
qb_util_log(LOG_ERR, "couldn't create a semaphore %s",
strerror(errno));
- return -1;
+ return res;
}
options.val = 0;
res = semctl(rb->lock_id, 0, SETVAL, options);
} else {
rb->lock_id = semget(sem_key, 0, 0600);
if (rb->lock_id == -1) {
+ res = -errno;
qb_util_log(LOG_ERR, "couldn't get a sem id %s",
strerror(errno));
- return -1;
+ return res;
}
res = 0;
}
@@ -204,7 +221,11 @@ static int32_t my_posix_sem_create(struct qb_ringbuffer_s *rb, uint32_t flags)
}
pshared = 1;
}
- return sem_init(&rb->shared_hdr->posix_sem, pshared, 0);
+ if (sem_init(&rb->shared_hdr->posix_sem, pshared, 0) == -1) {
+ return -errno;
+ } else {
+ return 0;
+ }
}
static int32_t my_sysv_sem_create(qb_ringbuffer_t * rb, uint32_t flags)
@@ -216,26 +237,29 @@ static int32_t my_sysv_sem_create(qb_ringbuffer_t * rb, uint32_t flags)
sem_key = ftok(rb->shared_hdr->hdr_path, (rb->shared_hdr->size + 1));
if (sem_key == -1) {
+ res = -errno;
qb_util_log(LOG_ERR, "couldn't get a sem id %s",
- strerror(errno));
- return -1;
+ strerror(errno));
+ return res;
}
if (flags & QB_RB_FLAG_CREATE) {
rb->sem_id = semget(sem_key, 1, IPC_CREAT | IPC_EXCL | 0600);
if (rb->sem_id == -1) {
+ res = -errno;
qb_util_log(LOG_ERR, "couldn't create a semaphore %s",
- strerror(errno));
- return -1;
+ strerror(errno));
+ return res;
}
options.val = 0;
res = semctl(rb->sem_id, 0, SETVAL, options);
} else {
rb->sem_id = semget(sem_key, 0, 0600);
if (rb->sem_id == -1) {
+ res = -errno;
qb_util_log(LOG_ERR, "couldn't get a sem id %s",
- strerror(errno));
- return -1;
+ strerror(errno));
+ return res;
}
res = 0;
}
@@ -275,12 +299,13 @@ int32_t qb_rb_sem_create(struct qb_ringbuffer_s * rb, uint32_t flags)
static int32_t my_posix_lock_it(qb_ringbuffer_t * rb)
{
- return pthread_spin_lock(&rb->shared_hdr->spinlock);
+ return -pthread_spin_lock(&rb->shared_hdr->spinlock);
}
static int32_t my_sysv_lock_it(qb_ringbuffer_t * rb)
{
struct sembuf sops[2];
+ int32_t res = 0;
/*
* atomically wait for sem to get to 0 and then incr.
@@ -297,23 +322,24 @@ semop_again:
if (semop(rb->lock_id, sops, 2) == -1) {
if (errno == EINTR) {
goto semop_again;
- } else {
- qb_util_log(LOG_ERR, "could not lock it : %s",
- strerror(errno));
}
- return -1;
+ res = -errno;
+ qb_util_log(LOG_ERR, "could not lock it : %s",
+ strerror(errno));
+ return res;
}
- return 0;
+ return res;
}
static int32_t my_posix_unlock_it(qb_ringbuffer_t * rb)
{
- return pthread_spin_unlock(&rb->shared_hdr->spinlock);
+ return -pthread_spin_unlock(&rb->shared_hdr->spinlock);
}
static int32_t my_sysv_unlock_it(qb_ringbuffer_t * rb)
{
struct sembuf lock_it;
+ int32_t res = 0;
lock_it.sem_num = 0;
lock_it.sem_op = -1;
@@ -323,23 +349,27 @@ semop_again:
if (semop(rb->lock_id, &lock_it, 1) == -1) {
if (errno == EINTR) {
goto semop_again;
- } else {
- qb_util_log(LOG_ERR, "could not unlock it : %s",
- strerror(errno));
}
- return -1;
+ res = -errno;
+ qb_util_log(LOG_ERR, "could not unlock it : %s",
+ strerror(errno));
+ return res;
}
- return 0;
+ return res;
}
static int32_t my_posix_lock_it_destroy(qb_ringbuffer_t * rb)
{
- return pthread_spin_destroy(&rb->shared_hdr->spinlock);
+ return -pthread_spin_destroy(&rb->shared_hdr->spinlock);
}
static int32_t my_sysv_lock_it_destroy(qb_ringbuffer_t * rb)
{
- return semctl(rb->lock_id, 0, IPC_RMID, 0);
+ if (semctl(rb->lock_id, 0, IPC_RMID, 0) == -1) {
+ return -errno;
+ } else {
+ return 0;
+ }
}
int32_t qb_rb_lock_create(struct qb_ringbuffer_s * rb, uint32_t flags)
@@ -368,7 +398,7 @@ int32_t qb_rb_lock_create(struct qb_ringbuffer_s * rb, uint32_t flags)
}
if (flags & QB_RB_FLAG_CREATE) {
- return pthread_spin_init(&rb->shared_hdr->spinlock,
+ return -pthread_spin_init(&rb->shared_hdr->spinlock,
pshared);
} else {
return 0;
diff --git a/lib/util.c b/lib/util.c
index 0a71b59..469ac68 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -68,11 +68,11 @@ int32_t qb_thread_lock(qb_thread_lock_t * tl)
int32_t res;
#if defined(HAVE_PTHREAD_SPIN_LOCK)
if (tl->type == QB_THREAD_LOCK_SHORT) {
- res = pthread_spin_lock(&tl->spinlock);
+ res = -pthread_spin_lock(&tl->spinlock);
} else
#endif
{
- res = pthread_mutex_lock(&tl->mutex);
+ res = -pthread_mutex_lock(&tl->mutex);
}
return res;
}
@@ -82,11 +82,11 @@ int32_t qb_thread_unlock(qb_thread_lock_t * tl)
int32_t res;
#if defined(HAVE_PTHREAD_SPIN_LOCK)
if (tl->type == QB_THREAD_LOCK_SHORT) {
- res = pthread_spin_unlock(&tl->spinlock);
+ res = -pthread_spin_unlock(&tl->spinlock);
} else
#endif
{
- res = pthread_mutex_unlock(&tl->mutex);
+ res = -pthread_mutex_unlock(&tl->mutex);
}
return res;
}
@@ -96,11 +96,11 @@ int32_t qb_thread_trylock(qb_thread_lock_t * tl)
int32_t res;
#if defined(HAVE_PTHREAD_SPIN_LOCK)
if (tl->type == QB_THREAD_LOCK_SHORT) {
- res = pthread_spin_trylock(&tl->spinlock);
+ res = -pthread_spin_trylock(&tl->spinlock);
} else
#endif
{
- res = pthread_mutex_trylock(&tl->mutex);
+ res = -pthread_mutex_trylock(&tl->mutex);
}
return res;
}
@@ -110,11 +110,11 @@ int32_t qb_thread_lock_destroy(qb_thread_lock_t * tl)
int32_t res;
#if defined(HAVE_PTHREAD_SPIN_LOCK)
if (tl->type == QB_THREAD_LOCK_SHORT) {
- res = pthread_spin_destroy(&tl->spinlock);
+ res = -pthread_spin_destroy(&tl->spinlock);
} else
#endif
{
- res = pthread_mutex_destroy(&tl->mutex);
+ res = -pthread_mutex_destroy(&tl->mutex);
}
free(tl);
return res;
@@ -171,17 +171,17 @@ int32_t qb_util_mmap_file_open(char *path, const char *file, size_t bytes,
snprintf(path, PATH_MAX, "/dev/shm/%s", file);
}
fd = open_mmap_file(path, file_flags);
- if (fd == -1 && !is_absolute) {
+ if (fd < 0 && !is_absolute) {
qb_util_log(LOG_ERR, "couldn't open file %s error: %s",
path, strerror(errno));
snprintf(path, PATH_MAX, LOCALSTATEDIR "/run/%s", file);
fd = open_mmap_file(path, file_flags);
- if (fd == -1) {
- return -1;
+ if (fd < 0) {
+ return -errno;
}
}
- if (fd != -1) {
+ if (fd >= 0) {
ftruncate(fd, bytes);
}
return fd;
@@ -197,14 +197,14 @@ int32_t qb_util_circular_mmap(int32_t fd, void **buf, size_t bytes)
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (addr_orig == MAP_FAILED) {
- return (-1);
+ return (-errno);
}
addr = mmap(addr_orig, bytes, PROT_READ | PROT_WRITE,
MAP_FIXED | MAP_SHARED, fd, 0);
if (addr != addr_orig) {
- return (-1);
+ return (-errno);
}
#ifdef QB_BSD
madvise(addr_orig, bytes, MADV_NOSYNC);
@@ -219,7 +219,7 @@ int32_t qb_util_circular_mmap(int32_t fd, void **buf, size_t bytes)
res = close(fd);
if (res) {
- return (-1);
+ return (-errno);
}
*buf = addr_orig;
return (0);
diff --git a/tests/bmc.c b/tests/bmc.c
index 2296559..eff8a86 100644
--- a/tests/bmc.c
+++ b/tests/bmc.c
@@ -92,13 +92,14 @@ static int32_t bmc_send_nozc(uint32_t size)
repeat_send:
res = qb_ipcc_send(conn, req_header, req_header->size);
- if (res == -1) {
- if (errno == EAGAIN || errno == ENOMEM) {
+ if (res < 0) {
+ if (res == -EAGAIN || res == -ENOMEM) {
goto repeat_send;
- } else if (errno == EINVAL || errno == EINTR) {
+ } else if (res == -EINVAL || res == -EINTR) {
perror("qb_ipcc_send");
return -1;
} else {
+ errno = -res;
perror("qb_ipcc_send");
goto repeat_send;
}
@@ -109,10 +110,10 @@ repeat_send:
res = qb_ipcc_recv(conn,
&res_header,
sizeof(struct qb_ipc_response_header));
- if (res == -1 && errno == EAGAIN) {
+ if (res == -EAGAIN) {
goto repeat_recv;
}
- if (res == -1 && errno == EINTR) {
+ if (res == -EINTR) {
return -1;
}
assert(res == sizeof(struct qb_ipc_response_header));
diff --git a/tests/bms.c b/tests/bms.c
index 5253ae1..2ccc84c 100644
--- a/tests/bms.c
+++ b/tests/bms.c
@@ -97,7 +97,7 @@ static void s1_msg_process_fn(qb_ipcs_connection_pt conn,
if (blocking == 1) {
res = qb_ipcs_response_send(conn, &response,
sizeof(response));
- if (res == -1) {
+ if (res < 0) {
perror("qb_ipcs_response_send");
}
}
diff --git a/tests/check_rb.c b/tests/check_rb.c
index f26ea7b..f242eee 100644
--- a/tests/check_rb.c
+++ b/tests/check_rb.c
@@ -57,7 +57,7 @@ START_TEST(test_ring_buffer1)
avail = qb_rb_space_free(rb);
actual = qb_rb_chunk_write(rb, hdr, hdr->size);
if (avail < (hdr->size + (2 * sizeof(uint32_t)))) {
- ck_assert_int_eq(actual, -1);
+ ck_assert_int_eq(actual, -ENOMEM);
} else {
ck_assert_int_eq(actual, hdr->size);
}
@@ -70,7 +70,7 @@ START_TEST(test_ring_buffer1)
for (i = 0; i < 15; i++) {
actual = qb_rb_chunk_read(rb, hdr, 512, 0);
- if (actual == -1) {
+ if (actual < 0) {
ck_assert_int_eq(0, qb_rb_chunks_used(rb));
break;
}
@@ -102,7 +102,7 @@ START_TEST(test_ring_buffer2)
}
for (i = 0; i < 100; i++) {
l = qb_rb_chunk_peek(t, (void **)&new_data, 0);
- if (l == -1) {
+ if (l < 0) {
/* no more to read */
break;
}
@@ -116,7 +116,7 @@ START_TEST(test_ring_buffer2)
}
for (i = 0; i < 100; i++) {
l = qb_rb_chunk_peek(t, (void **)&new_data, 0);
- if (l == -1) {
+ if (l == 0) {
/* no more to read */
break;
}
@@ -148,7 +148,7 @@ START_TEST(test_ring_buffer3)
}
for (i = 0; i < 2000; i++) {
l = qb_rb_chunk_read(t, (void *)out, 32, 0);
- if (l == -1) {
+ if (l < 0) {
/* no more to read */
break;
}
@@ -177,7 +177,7 @@ END_TEST START_TEST(test_ring_buffer4)
}
for (i = 0; i < 2000; i++) {
l = qb_rb_chunk_peek(t, (void **)&new_data, 0);
- if (l == -1 && errno == ENODATA) {
+ if (l == 0) {
break;
}
ck_assert_int_eq(l, strlen(data));
--
1.7.2.2
13 years, 6 months
[PATCH] BSD: port new changes to BSD
by Angus Salkeld
- check for doxygen
- no RLIMIT_MSGQUEUE on bsd
- change ENODATA to ENOMSG
Signed-off-by: Angus Salkeld <asalkeld(a)redhat.com>
---
configure.ac | 6 ++++--
docs/Makefile.am | 8 +++++++-
lib/ipc_posix_mq.c | 25 +++++++++++++++----------
lib/ipc_us.c | 6 ++++--
lib/ringbuffer.c | 4 ++--
tests/bmc.c | 3 +++
6 files changed, 35 insertions(+), 17 deletions(-)
diff --git a/configure.ac b/configure.ac
index b7e431b..90e01ac 100644
--- a/configure.ac
+++ b/configure.ac
@@ -54,6 +54,8 @@ AC_PROG_LN_S
AC_PROG_INSTALL
AC_PROG_MAKE_SET
AC_CHECK_PROGS([PKGCONFIG], [pkg-config])
+AC_CHECK_PROG(HAVE_DOXYGEN, doxygen, have_doxygen="yes", have_doxygen="no")
+AM_CONDITIONAL(HAVE_DOXYGEN, test "${have_doxygen}" = "yes")
## local helper functions
@@ -96,7 +98,7 @@ AC_HEADER_SYS_WAIT
AC_CHECK_HEADERS([arpa/inet.h fcntl.h inttypes.h limits.h locale.h netdb.h netinet/in.h nl_types.h stdint.h \
stdlib.h string.h sys/ioctl.h sys/param.h sys/socket.h \
- time.h sys/time.h syslog.h unistd.h sys/types.h getopt.h malloc.h \
+ time.h sys/time.h syslog.h unistd.h sys/types.h sys/stat.h getopt.h malloc.h \
sys/sockio.h sys/un.h utmpx.h errno.h dlfcn.h dirent.h fnmatch.h])
# Checks for typedefs, structures, and compiler characteristics.
@@ -211,7 +213,7 @@ case "$host_os" in
AC_DEFINE_UNQUOTED([MAP_ANONYMOUS], [MAP_ANON],
[Shared memory define for Darwin platform])
OS_CFLAGS=""
- OS_CPPFLAGS="-I/usr/local/include"
+ OS_CPPFLAGS="-I/usr/include -I/usr/local/include"
OS_LDFLAGS="-L/usr/local/lib"
OS_DYFLAGS="-export-dynamic"
DARWIN_OPTS=""
diff --git a/docs/Makefile.am b/docs/Makefile.am
index 06c976e..389160b 100644
--- a/docs/Makefile.am
+++ b/docs/Makefile.am
@@ -20,13 +20,14 @@
MAINTAINERCLEANFILES = Makefile.in
EXTRA_DIST = man.dox html.dox
+if HAVE_DOXYGEN
inc_dir = $(top_srcdir)/include/qb
dependant_headers = $(wildcard $(inc_dir)/qb*.h)
dist_man_MANS = man3/qbhash.h.3 man3/qbipcc.h.3 man3/qbipcs.h.3 man3/qblogsys.h.3 \
man3/qbplugin.h.3 man3/qbqueue.h.3 man3/qbtsafe.h.3 man3/qbwthread.h.3 \
man3/qbhdb.h.3 man3/qbipc_common.h.3 man3/qblist.h.3 man3/qbplugin_comp.h.3 \
man3/qbpoll.h.3 man3/qbtimer.h.3 man3/qbutil.h.3
-#$(addsuffix .3,$(subst $(inc_dir),man3,$(dependant_headers)))
+
$(dist_man_MANS): man.dox $(dependant_headers)
mkdir -p man3
@@ -36,6 +37,11 @@ doxygen: html.dox
mkdir -p html
doxygen html.dox
+else
+doxygen:
+ @echo WARNING: no doxygen to build man pages!
+endif
+
clean-generic:
rm -rf html man3
diff --git a/lib/ipc_posix_mq.c b/lib/ipc_posix_mq.c
index 9af7174..95de6ef 100644
--- a/lib/ipc_posix_mq.c
+++ b/lib/ipc_posix_mq.c
@@ -41,8 +41,10 @@ static int32_t posix_mq_increase_limits(size_t max_msg_size,
int32_t msgsize_max;
char size_str[10];
int32_t res = 0;
+#ifdef QB_LINUX
struct rlimit rlim;
int32_t q_limit;
+#endif /* QB_LINUX */
proc_fd = fopen("/proc/sys/fs/mqueue/msgsize_max", "r+");
if (proc_fd > 0) {
@@ -63,6 +65,7 @@ static int32_t posix_mq_increase_limits(size_t max_msg_size,
}
fclose(proc_fd);
+#ifdef QB_LINUX
if (getrlimit(RLIMIT_MSGQUEUE, &rlim) != 0) {
qb_util_log(LOG_ERR, "getrlimit failed");
return -1;
@@ -74,24 +77,26 @@ static int32_t posix_mq_increase_limits(size_t max_msg_size,
qb_util_log(LOG_ERR, "setrlimit failed");
return -1;
}
+#endif /* QB_LINUX */
return 0;
}
-static int32_t posix_mq_create(const char* mq_name, size_t max_msg_size,
+static mqd_t posix_mq_create(const char* mq_name, size_t max_msg_size,
int32_t flags)
{
struct mq_attr attr;
- int32_t res = 0;
+ mqd_t res = 0;
int32_t q_len = 10;
+ mode_t m = 0600;
attr.mq_flags = O_NONBLOCK;
attr.mq_maxmsg = q_len;
attr.mq_msgsize = max_msg_size;
mq_unlink(mq_name);
- res = mq_open(mq_name, flags, 0600, &attr);
- if (res == -1) {
+ res = mq_open(mq_name, flags, m, &attr);
+ if (res == (mqd_t)-1) {
perror(mq_name);
}
@@ -214,7 +219,7 @@ int32_t qb_ipcc_pmq_connect(struct qb_ipcc_connection * c)
snprintf(c->u.pmq.request.name, NAME_MAX, "/%s", c->name);
c->u.pmq.request.q = mq_open(c->u.pmq.request.name,
O_WRONLY | O_NONBLOCK);
- if (c->u.pmq.request.q == -1) {
+ if (c->u.pmq.request.q == (mqd_t)-1) {
perror("mq_open:REQUEST");
return -1;
}
@@ -230,7 +235,7 @@ int32_t qb_ipcc_pmq_connect(struct qb_ipcc_connection * c)
c->max_msg_size,
O_RDONLY | O_CREAT | O_EXCL | O_NONBLOCK);
- if (c->u.pmq.response.q == -1) {
+ if (c->u.pmq.response.q == (mqd_t)-1) {
perror("mq_open:RESPONSE");
goto cleanup_request;
}
@@ -244,7 +249,7 @@ int32_t qb_ipcc_pmq_connect(struct qb_ipcc_connection * c)
c->max_msg_size,
O_RDONLY | O_CREAT | O_EXCL | O_NONBLOCK);
- if (c->u.pmq.dispatch.q == -1) {
+ if (c->u.pmq.dispatch.q == (mqd_t)-1) {
perror("mq_open:DISPATCH");
goto cleanup_request_response;
}
@@ -329,7 +334,7 @@ static int32_t qb_ipcs_pmq_connect(struct qb_ipcs_service *s,
strcpy(c->u.pmq.response.name, init->response_mq);
c->u.pmq.response.q = mq_open(c->u.pmq.response.name,
O_WRONLY | O_NONBLOCK);
- if (c->u.pmq.response.q == -1) {
+ if (c->u.pmq.response.q == (mqd_t)-1) {
res = errno;
perror("mq_open:RESPONSE");
return res;
@@ -345,7 +350,7 @@ static int32_t qb_ipcs_pmq_connect(struct qb_ipcs_service *s,
c->u.pmq.dispatch.q = mq_open(c->u.pmq.dispatch.name,
O_WRONLY | O_NONBLOCK);
- if (c->u.pmq.dispatch.q == -1) {
+ if (c->u.pmq.dispatch.q == (mqd_t)-1) {
res = errno;
perror("mq_open:DISPATCH");
goto cleanup_response;
@@ -421,7 +426,7 @@ int32_t qb_ipcs_pmq_create(struct qb_ipcs_service *s)
posix_mq_increase_limits(s->max_msg_size, 10);
s->u.q = posix_mq_create(mq_name, s->max_msg_size,
(O_RDONLY | O_CREAT | O_EXCL | O_NONBLOCK));
- if (s->u.q == -1) {
+ if (s->u.q == (mqd_t)-1) {
perror("posix_mq_create:REQUEST");
return -1;
}
diff --git a/lib/ipc_us.c b/lib/ipc_us.c
index 45dee69..2682dee 100644
--- a/lib/ipc_us.c
+++ b/lib/ipc_us.c
@@ -26,7 +26,9 @@
#ifdef HAVE_SYS_UN_H
#include <sys/un.h>
#endif /* HAVE_SYS_UN_H */
-
+#ifdef HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
#include <qb/qbipcs.h>
#include <qb/qbpoll.h>
#include "util_int.h"
@@ -447,7 +449,7 @@ int32_t qb_ipcs_us_publish(struct qb_ipcs_service * s)
SOCKETDIR);
goto error_close;
}
- sprintf(un_addr.sun_path, "%s/%s", SOCKETDIR, name);
+ sprintf(un_addr.sun_path, "%s/%s", SOCKETDIR, s->name);
unlink(un_addr.sun_path);
}
#endif
diff --git a/lib/ringbuffer.c b/lib/ringbuffer.c
index 009d23e..989eefe 100644
--- a/lib/ringbuffer.c
+++ b/lib/ringbuffer.c
@@ -491,7 +491,7 @@ ssize_t qb_rb_chunk_peek(qb_ringbuffer_t * rb, void **data_out, int32_t timeout)
*data_out = &rb->shared_data[read_pt + QB_RB_CHUNK_HEADER_WORDS];
if (chunk_magic != QB_RB_CHUNK_MAGIC) {
- errno = ENODATA;
+ errno = ENOMSG;
return -1;
} else {
return chunk_size;
@@ -524,7 +524,7 @@ qb_rb_chunk_read(qb_ringbuffer_t * rb, void *data_out, size_t len,
}
if (_qb_rb_space_used_locked_(rb) == 0) {
rb->unlock_fn(rb);
- errno = ENODATA;
+ errno = ENOMSG;
return -1;
}
diff --git a/tests/bmc.c b/tests/bmc.c
index b618bf0..2296559 100644
--- a/tests/bmc.c
+++ b/tests/bmc.c
@@ -18,6 +18,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with libqb. If not, see <http://www.gnu.org/licenses/>.
*/
+#include "config.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
@@ -40,6 +41,7 @@ static qb_ipcc_connection_t *conn;
static struct timeval tv1, tv2, tv_elapsed;
+#ifndef QB_BSD
#define timersub(a, b, result) \
do { \
(result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
@@ -49,6 +51,7 @@ do { \
(result)->tv_usec += 1000000; \
} \
} while (0)
+#endif
static void bm_start(void)
{
--
1.7.2.2
13 years, 7 months