diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/lockfile.c | 41 | ||||
| -rw-r--r-- | src/lib/shm_flow_set.c | 148 | ||||
| -rw-r--r-- | src/lib/shm_rbuff.c | 69 | ||||
| -rw-r--r-- | src/lib/shm_rdrbuff.c | 113 | ||||
| -rw-r--r-- | src/lib/sockets.c | 25 | 
5 files changed, 123 insertions, 273 deletions
| diff --git a/src/lib/lockfile.c b/src/lib/lockfile.c index a0222f18..5c96d22a 100644 --- a/src/lib/lockfile.c +++ b/src/lib/lockfile.c @@ -23,15 +23,12 @@  #include <ouroboros/config.h>  #include <ouroboros/lockfile.h> -#define OUROBOROS_PREFIX "lockfile" - -#include <ouroboros/logs.h> -  #include <stdlib.h>  #include <unistd.h>  #include <fcntl.h>  #include <string.h>  #include <signal.h> +#include <assert.h>  #include <sys/mman.h>  #include <sys/stat.h> @@ -52,7 +49,6 @@ struct lockfile * lockfile_create() {          fd = shm_open(LOCKFILE_NAME, O_CREAT | O_EXCL | O_RDWR, 0666);          if (fd == -1) { -                LOG_DBGF("Could not create lock file.");                  free(lf);                  return NULL;          } @@ -60,7 +56,6 @@ struct lockfile * lockfile_create() {          umask(mask);          if (ftruncate(fd, LF_SIZE - 1) < 0) { -                LOG_DBGF("Failed to extend lockfile.");                  free(lf);                  return NULL;          } @@ -74,9 +69,7 @@ struct lockfile * lockfile_create() {          close (fd);          if (lf->api == MAP_FAILED) { -                LOG_DBGF("Failed to map lockfile."); -                if (shm_unlink(LOCKFILE_NAME) == -1) -                        LOG_DBGF("Failed to remove invalid lockfile."); +                shm_unlink(LOCKFILE_NAME);                  free(lf);                  return NULL;          } @@ -94,7 +87,6 @@ struct lockfile * lockfile_open() {          fd = shm_open(LOCKFILE_NAME, O_RDWR, 0666);          if (fd < 0) { -                LOG_DBGF("Could not open lock file.");                  free(lf);                  return NULL;          } @@ -108,9 +100,7 @@ struct lockfile * lockfile_open() {          close(fd);          if (lf->api == MAP_FAILED) { -                LOG_DBGF("Failed to map lockfile."); -                if (shm_unlink(LOCKFILE_NAME) == -1) -                        LOG_DBGF("Failed to remove invalid lockfile."); +                shm_unlink(LOCKFILE_NAME);                  free(lf);                  return NULL;          } @@ -120,39 +110,30 @@ struct lockfile * lockfile_open() {  void lockfile_close(struct lockfile * lf)  { -        if (lf == NULL) { -                LOG_DBGF("Bogus input. Bugging out."); -                return; -        } +        assert(lf); -        if (munmap(lf->api, LF_SIZE) == -1) -                LOG_DBGF("Couldn't unmap lockfile."); +        munmap(lf->api, LF_SIZE);          free(lf);  }  void lockfile_destroy(struct lockfile * lf)  { -        if (lf == NULL) { -                LOG_DBGF("Bogus input. Bugging out."); -                return; -        } +        assert(lf); -        if (getpid() != *lf->api && kill(*lf->api, 0) == 0) { -                LOG_DBGF("Only IRMd can destroy %s.", LOCKFILE_NAME); +        if (getpid() != *lf->api && kill(*lf->api, 0) == 0)                  return; -        } -        if (munmap(lf->api, LF_SIZE) == -1) -                LOG_DBGF("Couldn't unmap lockfile."); +        munmap(lf->api, LF_SIZE); -        if (shm_unlink(LOCKFILE_NAME) == -1) -                LOG_DBGF("Failed to remove lockfile."); +        shm_unlink(LOCKFILE_NAME);          free(lf);  }  pid_t lockfile_owner(struct lockfile * lf)  { +        assert(lf); +          return *lf->api;  } diff --git a/src/lib/shm_flow_set.c b/src/lib/shm_flow_set.c index 6cc94573..4a1bff83 100644 --- a/src/lib/shm_flow_set.c +++ b/src/lib/shm_flow_set.c @@ -27,14 +27,11 @@  #include <ouroboros/fqueue.h>  #include <ouroboros/errno.h> -#define OUROBOROS_PREFIX "shm_flow_set" - -#include <ouroboros/logs.h> -  #include <pthread.h>  #include <sys/mman.h>  #include <fcntl.h>  #include <stdlib.h> +#include <stdio.h>  #include <unistd.h>  #include <signal.h>  #include <sys/stat.h> @@ -77,16 +74,13 @@ struct shm_flow_set * shm_flow_set_create()          sprintf(fn, SHM_FLOW_SET_PREFIX "%d", getpid());          set = malloc(sizeof(*set)); -        if (set == NULL) { -                LOG_DBG("Could not allocate struct."); +        if (set == NULL)                  return NULL; -        }          mask = umask(0);          shm_fd = shm_open(fn, O_CREAT | O_EXCL | O_RDWR, 0666);          if (shm_fd == -1) { -                LOG_DBG("Failed creating flag file.");                  free(set);                  return NULL;          } @@ -94,7 +88,6 @@ struct shm_flow_set * shm_flow_set_create()          umask(mask);          if (ftruncate(shm_fd, SHM_FLOW_SET_FILE_SIZE - 1) < 0) { -                LOG_DBG("Failed to extend flag file.");                  free(set);                  close(shm_fd);                  return NULL; @@ -110,10 +103,7 @@ struct shm_flow_set * shm_flow_set_create()          close(shm_fd);          if (shm_base == MAP_FAILED) { -                LOG_DBG("Failed to map shared memory."); -                if (shm_unlink(fn) == -1) -                        LOG_DBG("Failed to remove invalid shm."); - +                shm_unlink(fn);                  free(set);                  return NULL;          } @@ -160,14 +150,11 @@ struct shm_flow_set * shm_flow_set_open(pid_t api)          sprintf(fn, SHM_FLOW_SET_PREFIX "%d", api);          set = malloc(sizeof(*set)); -        if (set == NULL) { -                LOG_DBG("Could not allocate struct."); +        if (set == NULL)                  return NULL; -        }          shm_fd = shm_open(fn, O_RDWR, 0666);          if (shm_fd == -1) { -                LOG_DBG("%d failed opening shared memory %s.", getpid(), fn);                  free(set);                  return NULL;          } @@ -182,9 +169,7 @@ struct shm_flow_set * shm_flow_set_open(pid_t api)          close(shm_fd);          if (shm_base == MAP_FAILED) { -                LOG_DBG("Failed to map shared memory."); -                if (shm_unlink(fn) == -1) -                        LOG_DBG("Failed to remove invalid shm."); +                shm_unlink(fn);                  free(set);                  return NULL;          } @@ -210,18 +195,12 @@ void shm_flow_set_destroy(struct shm_flow_set * set)          if (set->api != getpid()) {                  lf = lockfile_open(); -                if (lf == NULL) { -                        LOG_ERR("Failed to open lockfile."); +                if (lf == NULL)                          return; -                }                  if (lockfile_owner(lf) == getpid()) { -                        LOG_DBG("Flow set %d destroyed by IRMd %d.", -                                set->api, getpid());                          lockfile_close(lf);                  } else { -                        LOG_ERR("AP-I %d tried to destroy flowset owned by %d.", -                                getpid(), set->api);                          lockfile_close(lf);                          return;                  } @@ -229,11 +208,8 @@ void shm_flow_set_destroy(struct shm_flow_set * set)          sprintf(fn, SHM_FLOW_SET_PREFIX "%d", set->api); -        if (munmap(set->mtable, SHM_FLOW_SET_FILE_SIZE) == -1) -                LOG_DBG("Couldn't unmap shared memory."); - -        if (shm_unlink(fn) == -1) -                LOG_DBG("Failed to unlink shm."); +        munmap(set->mtable, SHM_FLOW_SET_FILE_SIZE); +        shm_unlink(fn);          free(set);  } @@ -242,111 +218,111 @@ void shm_flow_set_close(struct shm_flow_set * set)  {          assert(set); -        if (munmap(set->mtable, SHM_FLOW_SET_FILE_SIZE) == -1) -                LOG_DBG("Couldn't unmap shared memory."); +        munmap(set->mtable, SHM_FLOW_SET_FILE_SIZE);          free(set);  } -void shm_flow_set_zero(struct shm_flow_set * shm_set, +void shm_flow_set_zero(struct shm_flow_set * set,                         size_t                idx)  {          ssize_t i = 0; +        assert(set);          assert(idx < AP_MAX_FQUEUES); -        pthread_mutex_lock(shm_set->lock); +        pthread_mutex_lock(set->lock);          for (i = 0; i < IRMD_MAX_FLOWS; ++i) -                if (shm_set->mtable[i] == (ssize_t) idx) -                        shm_set->mtable[i] = -1; +                if (set->mtable[i] == (ssize_t) idx) +                        set->mtable[i] = -1; -        shm_set->heads[idx] = 0; +        set->heads[idx] = 0; -        pthread_mutex_unlock(shm_set->lock); +        pthread_mutex_unlock(set->lock);  } -int shm_flow_set_add(struct shm_flow_set * shm_set, +int shm_flow_set_add(struct shm_flow_set * set,                       size_t                idx,                       int                   port_id)  { -        assert(shm_set); +        assert(set);          assert(!(port_id < 0) && port_id < IRMD_MAX_FLOWS);          assert(idx < AP_MAX_FQUEUES); -        pthread_mutex_lock(shm_set->lock); +        pthread_mutex_lock(set->lock); -        if (shm_set->mtable[port_id] != -1) { -                pthread_mutex_unlock(shm_set->lock); +        if (set->mtable[port_id] != -1) { +                pthread_mutex_unlock(set->lock);                  return -EPERM;          } -        shm_set->mtable[port_id] = idx; +        set->mtable[port_id] = idx; -        pthread_mutex_unlock(shm_set->lock); +        pthread_mutex_unlock(set->lock);          return 0;  } -void shm_flow_set_del(struct shm_flow_set * shm_set, +void shm_flow_set_del(struct shm_flow_set * set,                        size_t                idx,                        int                   port_id)  { -        assert(shm_set); +        assert(set);          assert(!(port_id < 0) && port_id < IRMD_MAX_FLOWS);          assert(idx < AP_MAX_FQUEUES); -        pthread_mutex_lock(shm_set->lock); +        pthread_mutex_lock(set->lock); -        if (shm_set->mtable[port_id] == (ssize_t) idx) -                shm_set->mtable[port_id] = -1; +        if (set->mtable[port_id] == (ssize_t) idx) +                set->mtable[port_id] = -1; -        pthread_mutex_unlock(shm_set->lock); +        pthread_mutex_unlock(set->lock);  } -int shm_flow_set_has(struct shm_flow_set * shm_set, +int shm_flow_set_has(struct shm_flow_set * set,                       size_t                idx,                       int                   port_id)  {          int ret = 0; -        assert(shm_set); +        assert(set);          assert(!(port_id < 0) && port_id < IRMD_MAX_FLOWS);          assert(idx < AP_MAX_FQUEUES); -        pthread_mutex_lock(shm_set->lock); +        pthread_mutex_lock(set->lock); -        if (shm_set->mtable[port_id] == (ssize_t) idx) +        if (set->mtable[port_id] == (ssize_t) idx)                  ret = 1; -        pthread_mutex_unlock(shm_set->lock); +        pthread_mutex_unlock(set->lock);          return ret;  } -void shm_flow_set_notify(struct shm_flow_set * shm_set, int port_id) +void shm_flow_set_notify(struct shm_flow_set * set, int port_id)  { -        assert(shm_set); +        assert(set);          assert(!(port_id < 0) && port_id < IRMD_MAX_FLOWS); -        pthread_mutex_lock(shm_set->lock); +        pthread_mutex_lock(set->lock); -        if (shm_set->mtable[port_id] == -1) { -                pthread_mutex_unlock(shm_set->lock); +        if (set->mtable[port_id] == -1) { +                pthread_mutex_unlock(set->lock);                  return;          } -        *(fqueue_ptr(shm_set, shm_set->mtable[port_id]) + -                     (shm_set->heads[shm_set->mtable[port_id]])++) = port_id; +        *(fqueue_ptr(set, set->mtable[port_id]) + +                     (set->heads[set->mtable[port_id]])++) = port_id; -        pthread_cond_signal(&shm_set->conds[shm_set->mtable[port_id]]); +        pthread_cond_signal(&set->conds[set->mtable[port_id]]); -        pthread_mutex_unlock(shm_set->lock); +        pthread_mutex_unlock(set->lock);  } -ssize_t shm_flow_set_wait(const struct shm_flow_set * shm_set, +ssize_t shm_flow_set_wait(const struct shm_flow_set * set,                            size_t                      idx,                            int *                       fqueue,                            const struct timespec *     timeout) @@ -354,17 +330,15 @@ ssize_t shm_flow_set_wait(const struct shm_flow_set * shm_set,          ssize_t ret = 0;          struct timespec abstime; -        assert(shm_set); +        assert(set);          assert(idx < AP_MAX_FQUEUES);          assert(fqueue);  #ifdef __APPLE__ -        pthread_mutex_lock(shm_set->lock); +        pthread_mutex_lock(set->lock);  #else -        if (pthread_mutex_lock(shm_set->lock) == EOWNERDEAD) { -                LOG_DBG("Recovering dead mutex."); -                pthread_mutex_consistent(shm_set->lock); -        } +        if (pthread_mutex_lock(set->lock) == EOWNERDEAD) +                pthread_mutex_consistent(set->lock);  #endif          if (timeout != NULL) {                  clock_gettime(PTHREAD_COND_CLOCK, &abstime); @@ -372,21 +346,19 @@ ssize_t shm_flow_set_wait(const struct shm_flow_set * shm_set,          }          pthread_cleanup_push((void(*)(void *))pthread_mutex_unlock, -                             (void *) shm_set->lock); +                             (void *) set->lock); -        while (shm_set->heads[idx] == 0 && ret != -ETIMEDOUT) { +        while (set->heads[idx] == 0 && ret != -ETIMEDOUT) {                  if (timeout != NULL) -                        ret = -pthread_cond_timedwait(shm_set->conds + idx, -                                                      shm_set->lock, +                        ret = -pthread_cond_timedwait(set->conds + idx, +                                                      set->lock,                                                        &abstime);                  else -                        ret = -pthread_cond_wait(shm_set->conds + idx, -                                                 shm_set->lock); +                        ret = -pthread_cond_wait(set->conds + idx, +                                                 set->lock);  #ifndef __APPLE__ -                if (ret == -EOWNERDEAD) { -                        LOG_DBG("Recovering dead mutex."); -                        pthread_mutex_consistent(shm_set->lock); -                } +                if (ret == -EOWNERDEAD) +                        pthread_mutex_consistent(set->lock);  #endif                  if (ret == -ETIMEDOUT)                          break; @@ -394,10 +366,10 @@ ssize_t shm_flow_set_wait(const struct shm_flow_set * shm_set,          if (ret != -ETIMEDOUT) {                  memcpy(fqueue, -                       fqueue_ptr(shm_set, idx), -                       shm_set->heads[idx] * sizeof(int)); -                ret = shm_set->heads[idx]; -                shm_set->heads[idx] = 0; +                       fqueue_ptr(set, idx), +                       set->heads[idx] * sizeof(int)); +                ret = set->heads[idx]; +                set->heads[idx] = 0;          }          pthread_cleanup_pop(true); diff --git a/src/lib/shm_rbuff.c b/src/lib/shm_rbuff.c index 5d6d30c7..29a62f62 100644 --- a/src/lib/shm_rbuff.c +++ b/src/lib/shm_rbuff.c @@ -26,15 +26,12 @@  #include <ouroboros/time_utils.h>  #include <ouroboros/errno.h> -#define OUROBOROS_PREFIX "shm_rbuff" - -#include <ouroboros/logs.h> -  #include <pthread.h>  #include <sys/mman.h>  #include <fcntl.h>  #include <stdlib.h>  #include <string.h> +#include <stdio.h>  #include <stdint.h>  #include <unistd.h>  #include <signal.h> @@ -83,16 +80,13 @@ struct shm_rbuff * shm_rbuff_create(pid_t api, int port_id)          sprintf(fn, SHM_RBUFF_PREFIX "%d.%d", api, port_id);          rb = malloc(sizeof(*rb)); -        if (rb == NULL) { -                LOG_DBG("Could not allocate struct."); +        if (rb == NULL)                  return NULL; -        }          mask = umask(0);          shm_fd = shm_open(fn, O_CREAT | O_EXCL | O_RDWR, 0666);          if (shm_fd == -1) { -                LOG_DBG("Failed creating ring buffer.");                  free(rb);                  return NULL;          } @@ -100,7 +94,6 @@ struct shm_rbuff * shm_rbuff_create(pid_t api, int port_id)          umask(mask);          if (ftruncate(shm_fd, SHM_RBUFF_FILE_SIZE - 1) < 0) { -                LOG_DBG("Failed to extend ringbuffer.");                  free(rb);                  close(shm_fd);                  return NULL; @@ -116,9 +109,7 @@ struct shm_rbuff * shm_rbuff_create(pid_t api, int port_id)          close(shm_fd);          if (shm_base == MAP_FAILED) { -                LOG_DBG("Failed to map shared memory."); -                if (shm_unlink(fn) == -1) -                        LOG_DBG("Failed to remove invalid shm."); +                shm_unlink(fn);                  free(rb);                  return NULL;          } @@ -166,14 +157,11 @@ struct shm_rbuff * shm_rbuff_open(pid_t api, int port_id)          sprintf(fn, SHM_RBUFF_PREFIX "%d.%d", api, port_id);          rb = malloc(sizeof(*rb)); -        if (rb == NULL) { -                LOG_DBG("Could not allocate struct."); +        if (rb == NULL)                  return NULL; -        }          shm_fd = shm_open(fn, O_RDWR, 0666);          if (shm_fd == -1) { -                LOG_DBG("%d failed opening shared memory %s.", getpid(), fn);                  free(rb);                  return NULL;          } @@ -188,10 +176,7 @@ struct shm_rbuff * shm_rbuff_open(pid_t api, int port_id)          close(shm_fd);          if (shm_base == MAP_FAILED) { -                LOG_DBG("Failed to map shared memory."); -                if (shm_unlink(fn) == -1) -                        LOG_DBG("Failed to remove invalid shm."); - +                shm_unlink(fn);                  free(rb);                  return NULL;          } @@ -214,8 +199,7 @@ void shm_rbuff_close(struct shm_rbuff * rb)  {          assert(rb); -        if (munmap(rb->shm_base, SHM_RBUFF_FILE_SIZE) == -1) -                LOG_DBG("Couldn't unmap shared memory."); +        munmap(rb->shm_base, SHM_RBUFF_FILE_SIZE);          free(rb);  } @@ -235,11 +219,8 @@ void shm_rbuff_destroy(struct shm_rbuff * rb)  #endif          sprintf(fn, SHM_RBUFF_PREFIX "%d.%d", rb->api, rb->port_id); -        if (munmap(rb->shm_base, SHM_RBUFF_FILE_SIZE) == -1) -                LOG_DBG("Couldn't unmap shared memory."); - -        if (shm_unlink(fn) == -1) -                LOG_DBG("Failed to unlink shm %s.", fn); +        munmap(rb->shm_base, SHM_RBUFF_FILE_SIZE); +        shm_unlink(fn);          free(rb);  } @@ -252,10 +233,8 @@ int shm_rbuff_write(struct shm_rbuff * rb, size_t idx)  #ifdef __APPLE__          pthread_mutex_lock(rb->lock);  #else -        if (pthread_mutex_lock(rb->lock) == EOWNERDEAD) { -                LOG_DBG("Recovering dead mutex."); +        if (pthread_mutex_lock(rb->lock) == EOWNERDEAD)                  pthread_mutex_consistent(rb->lock); -        }  #endif          if (*rb->acl == RB_CLOSED) {                  pthread_mutex_unlock(rb->lock); @@ -264,7 +243,7 @@ int shm_rbuff_write(struct shm_rbuff * rb, size_t idx)          if (!shm_rbuff_free(rb)) {                  pthread_mutex_unlock(rb->lock); -                return -1; +                return -EAGAIN;          }          if (shm_rbuff_empty(rb)) @@ -287,10 +266,8 @@ ssize_t shm_rbuff_read(struct shm_rbuff * rb)  #ifdef __APPLE__          pthread_mutex_lock(rb->lock);  #else -        if (pthread_mutex_lock(rb->lock) == EOWNERDEAD) { -                LOG_DBG("Recovering dead mutex."); +        if (pthread_mutex_lock(rb->lock) == EOWNERDEAD)                  pthread_mutex_consistent(rb->lock); -        }  #endif          if (shm_rbuff_empty(rb)) {                  pthread_mutex_unlock(rb->lock); @@ -318,10 +295,8 @@ ssize_t shm_rbuff_read_b(struct shm_rbuff *      rb,  #ifdef __APPLE__          pthread_mutex_lock(rb->lock);  #else -        if (pthread_mutex_lock(rb->lock) == EOWNERDEAD) { -                LOG_DBG("Recovering dead mutex."); +        if (pthread_mutex_lock(rb->lock) == EOWNERDEAD)                  pthread_mutex_consistent(rb->lock); -        }  #endif          if (timeout != NULL) {                  idx = -ETIMEDOUT; @@ -340,10 +315,8 @@ ssize_t shm_rbuff_read_b(struct shm_rbuff *      rb,                  else                          ret = pthread_cond_wait(rb->add, rb->lock);  #ifndef __APPLE__ -                if (ret == EOWNERDEAD) { -                        LOG_DBG("Recovering dead mutex."); +                if (ret == EOWNERDEAD)                          pthread_mutex_consistent(rb->lock); -                }  #endif                  if (ret == ETIMEDOUT) {                          idx = -ETIMEDOUT; @@ -369,10 +342,8 @@ void shm_rbuff_block(struct shm_rbuff * rb)  #ifdef __APPLE__          pthread_mutex_lock(rb->lock);  #else -        if (pthread_mutex_lock(rb->lock) == EOWNERDEAD) { -                LOG_DBG("Recovering dead mutex."); +        if (pthread_mutex_lock(rb->lock) == EOWNERDEAD)                  pthread_mutex_consistent(rb->lock); -        }  #endif          *rb->acl = RB_CLOSED; @@ -386,10 +357,8 @@ void shm_rbuff_unblock(struct shm_rbuff * rb)  #ifdef __APPLE__          pthread_mutex_lock(rb->lock);  #else -        if (pthread_mutex_lock(rb->lock) == EOWNERDEAD) { -                LOG_DBG("Recovering dead mutex."); +        if (pthread_mutex_lock(rb->lock) == EOWNERDEAD)                  pthread_mutex_consistent(rb->lock); -        }  #endif          *rb->acl = RB_OPEN; @@ -403,10 +372,8 @@ void shm_rbuff_fini(struct shm_rbuff * rb)  #ifdef __APPLE__          pthread_mutex_lock(rb->lock);  #else -        if (pthread_mutex_lock(rb->lock) == EOWNERDEAD) { -                LOG_DBG("Recovering dead mutex."); +        if (pthread_mutex_lock(rb->lock) == EOWNERDEAD)                  pthread_mutex_consistent(rb->lock); -        }  #endif          assert(*rb->acl == RB_CLOSED); @@ -417,10 +384,8 @@ void shm_rbuff_fini(struct shm_rbuff * rb)  #ifdef __APPLE__                  pthread_cond_wait(rb->del, rb->lock);  #else -                if (pthread_cond_wait(rb->del, rb->lock) == EOWNERDEAD) { -                        LOG_DBG("Recovering dead mutex."); +                if (pthread_cond_wait(rb->del, rb->lock) == EOWNERDEAD)                          pthread_mutex_consistent(rb->lock); -                }  #endif          pthread_cleanup_pop(true);  } diff --git a/src/lib/shm_rdrbuff.c b/src/lib/shm_rdrbuff.c index a8245447..3ad8a470 100644 --- a/src/lib/shm_rdrbuff.c +++ b/src/lib/shm_rdrbuff.c @@ -32,15 +32,12 @@  #include <unistd.h>  #include <stdlib.h>  #include <string.h> +#include <stdio.h>  #include <signal.h>  #include <sys/stat.h>  #include <stdbool.h>  #include <assert.h> -#define OUROBOROS_PREFIX "shm_rdrbuff" - -#include <ouroboros/logs.h> -  #define SHM_BLOCKS_SIZE ((SHM_BUFFER_SIZE) * SHM_RDRB_BLOCK_SIZE)  #define SHM_FILE_SIZE (SHM_BLOCKS_SIZE + 2 * sizeof(size_t)                    \                         + sizeof(pthread_mutex_t) + 2 * sizeof(pthread_cond_t)  \ @@ -126,10 +123,8 @@ static char * rdrb_filename(void)          char * str;          str = malloc(strlen(SHM_RDRB_PREFIX) + 1); -        if (str == NULL) { -                LOG_ERR("Failed to create shm_rdrbuff: Out of Memory."); +        if (str == NULL)                  return NULL; -        }          sprintf(str, "%s", SHM_RDRB_PREFIX); @@ -145,22 +140,17 @@ struct shm_rdrbuff * shm_rdrbuff_create()          pthread_mutexattr_t  mattr;          pthread_condattr_t   cattr;          char *               shm_rdrb_fn = rdrb_filename(); -        if (shm_rdrb_fn == NULL) { -                LOG_ERR("Could not create rdrbuff. Out of Memory"); +        if (shm_rdrb_fn == NULL)                  return NULL; -        }          rdrb = malloc(sizeof *rdrb); -        if (rdrb == NULL) { -                LOG_DBGF("Could not allocate struct."); +        if (rdrb == NULL)                  return NULL; -        }          mask = umask(0);          shm_fd = shm_open(shm_rdrb_fn, O_CREAT | O_EXCL | O_RDWR, 0666);          if (shm_fd == -1) { -                LOG_DBGF("Failed creating shared memory map.");                  free(shm_rdrb_fn);                  free(rdrb);                  return NULL; @@ -169,7 +159,6 @@ struct shm_rdrbuff * shm_rdrbuff_create()          umask(mask);          if (ftruncate(shm_fd, SHM_FILE_SIZE - 1) < 0) { -                LOG_DBGF("Failed to extend shared memory map.");                  free(shm_rdrb_fn);                  close(shm_fd);                  free(rdrb); @@ -186,9 +175,7 @@ struct shm_rdrbuff * shm_rdrbuff_create()          close(shm_fd);          if (shm_base == MAP_FAILED) { -                LOG_DBGF("Failed to map shared memory."); -                if (shm_unlink(shm_rdrb_fn) == -1) -                        LOG_DBGF("Failed to remove invalid shm."); +                shm_unlink(shm_rdrb_fn);                  free(shm_rdrb_fn);                  free(rdrb);                  return NULL; @@ -233,20 +220,15 @@ struct shm_rdrbuff * shm_rdrbuff_open()          int                  shm_fd;          uint8_t *            shm_base;          char *               shm_rdrb_fn = rdrb_filename(); -        if (shm_rdrb_fn == NULL) { -                LOG_ERR("Could not create rdrbuff. Out of Memory"); +        if (shm_rdrb_fn == NULL)                  return NULL; -        }          rdrb = malloc(sizeof *rdrb); -        if (rdrb == NULL) { -                LOG_DBGF("Could not allocate struct."); +        if (rdrb == NULL)                  return NULL; -        }          shm_fd = shm_open(shm_rdrb_fn, O_RDWR, 0666);          if (shm_fd < 0) { -                LOG_DBGF("Failed opening shared memory.");                  free(shm_rdrb_fn);                  free(rdrb);                  return NULL; @@ -262,9 +244,7 @@ struct shm_rdrbuff * shm_rdrbuff_open()          close(shm_fd);          if (shm_base == MAP_FAILED) { -                LOG_DBGF("Failed to map shared memory."); -                if (shm_unlink(shm_rdrb_fn) == -1) -                        LOG_DBG("Failed to unlink invalid shm."); +                shm_unlink(shm_rdrb_fn);                  free(shm_rdrb_fn);                  free(rdrb);                  return NULL; @@ -289,10 +269,8 @@ void shm_rdrbuff_wait_full(struct shm_rdrbuff * rdrb)  #ifdef __APPLE__          pthread_mutex_lock(rdrb->lock);  #else -        if (pthread_mutex_lock(rdrb->lock) == EOWNERDEAD) { -                LOG_WARN("Recovering dead mutex."); +        if (pthread_mutex_lock(rdrb->lock) == EOWNERDEAD)                  pthread_mutex_consistent(rdrb->lock); -        }  #endif          pthread_cleanup_push((void (*)(void *)) pthread_mutex_unlock,                               (void *) rdrb->lock); @@ -301,10 +279,8 @@ void shm_rdrbuff_wait_full(struct shm_rdrbuff * rdrb)  #ifdef __APPLE__                  pthread_cond_wait(rdrb->full, rdrb->lock);  #else -                if (pthread_cond_wait(rdrb->full, rdrb->lock) == EOWNERDEAD) { -                        LOG_WARN("Recovering dead mutex."); +                if (pthread_cond_wait(rdrb->full, rdrb->lock) == EOWNERDEAD)                          pthread_mutex_consistent(rdrb->lock); -                }  #endif          } @@ -317,9 +293,7 @@ void shm_rdrbuff_close(struct shm_rdrbuff * rdrb)  {          assert(rdrb); -        if (munmap(rdrb->shm_base, SHM_FILE_SIZE) == -1) -                LOG_DBGF("Couldn't unmap shared memory."); - +        munmap(rdrb->shm_base, SHM_FILE_SIZE);          free(rdrb);  } @@ -329,22 +303,16 @@ void shm_rdrbuff_destroy(struct shm_rdrbuff * rdrb)          assert(rdrb); -        if (getpid() != *rdrb->api && kill(*rdrb->api, 0) == 0) { -                LOG_DBG("Process %d tried to destroy active rdrb.", getpid()); +        if (getpid() != *rdrb->api && kill(*rdrb->api, 0) == 0)                  return; -        } -        if (munmap(rdrb->shm_base, SHM_FILE_SIZE) == -1) -                LOG_DBG("Couldn't unmap shared memory."); +        munmap(rdrb->shm_base, SHM_FILE_SIZE);          shm_rdrb_fn = rdrb_filename(); -        if (shm_rdrb_fn == NULL) { -                LOG_ERR("Could not create rdrbuff. Out of Memory"); +        if (shm_rdrb_fn == NULL)                  return; -        } -        if (shm_unlink(shm_rdrb_fn) == -1) -                LOG_DBG("Failed to unlink shm."); +        shm_unlink(shm_rdrb_fn);          free(rdrb);          free(shm_rdrb_fn); @@ -368,18 +336,14 @@ ssize_t shm_rdrbuff_write(struct shm_rdrbuff * rdrb,          assert(data);  #ifndef SHM_RDRB_MULTI_BLOCK -        if (sz > SHM_RDRB_BLOCK_SIZE) { -                LOG_DBGF("Multi-block SDUs disabled. Dropping."); -                return -1; -        } +        if (sz > SHM_RDRB_BLOCK_SIZE) +                return -EMSGSIZE;  #endif  #ifdef __APPLE__          pthread_mutex_lock(rdrb->lock);  #else -        if (pthread_mutex_lock(rdrb->lock) == EOWNERDEAD) { -                LOG_DBGF("Recovering dead mutex."); +        if (pthread_mutex_lock(rdrb->lock) == EOWNERDEAD)                  pthread_mutex_consistent(rdrb->lock); -        }  #endif  #ifdef SHM_RDRB_MULTI_BLOCK          while (sz > 0) { @@ -394,10 +358,9 @@ ssize_t shm_rdrbuff_write(struct shm_rdrbuff * rdrb,  #else          if (!shm_rdrb_free(rdrb, 1)) {  #endif -                LOG_DBG("buffer full, idx = %ld.", *rdrb->tail);                  pthread_cond_broadcast(rdrb->full);                  pthread_mutex_unlock(rdrb->lock); -                return -1; +                return -EAGAIN;          }  #ifdef SHM_RDRB_MULTI_BLOCK @@ -452,18 +415,14 @@ ssize_t shm_rdrbuff_write_b(struct shm_rdrbuff * rdrb,          assert(data);  #ifndef SHM_RDRB_MULTI_BLOCK -        if (sz > SHM_RDRB_BLOCK_SIZE) { -                LOG_DBGF("Multi-block SDUs disabled. Dropping."); -                return -1; -        } +        if (sz > SHM_RDRB_BLOCK_SIZE) +                return -EMSGSIZE;  #endif  #ifdef __APPLE__          pthread_mutex_lock(rdrb->lock);  #else -        if (pthread_mutex_lock(rdrb->lock) == EOWNERDEAD) { -                LOG_DBGF("Recovering dead mutex."); +        if (pthread_mutex_lock(rdrb->lock) == EOWNERDEAD)                  pthread_mutex_consistent(rdrb->lock); -        }  #endif          pthread_cleanup_push((void (*) (void *)) pthread_mutex_unlock,                               (void *) rdrb->lock); @@ -533,10 +492,8 @@ ssize_t shm_rdrbuff_read(uint8_t **           dst,  #ifdef __APPLE__          pthread_mutex_lock(rdrb->lock);  #else -        if (pthread_mutex_lock(rdrb->lock) == EOWNERDEAD) { -                LOG_DBGF("Recovering dead mutex."); +        if (pthread_mutex_lock(rdrb->lock) == EOWNERDEAD)                  pthread_mutex_consistent(rdrb->lock); -        }  #endif          if (shm_rdrb_empty(rdrb)) {                  pthread_mutex_unlock(rdrb->lock); @@ -562,10 +519,8 @@ struct shm_du_buff * shm_rdrbuff_get(struct shm_rdrbuff * rdrb, size_t idx)  #ifdef __APPLE__          pthread_mutex_lock(rdrb->lock);  #else -        if (pthread_mutex_lock(rdrb->lock) == EOWNERDEAD) { -                LOG_DBGF("Recovering dead mutex."); +        if (pthread_mutex_lock(rdrb->lock) == EOWNERDEAD)                  pthread_mutex_consistent(rdrb->lock); -        }  #endif          if (shm_rdrb_empty(rdrb)) {                  pthread_mutex_unlock(rdrb->lock); @@ -587,10 +542,8 @@ int shm_rdrbuff_remove(struct shm_rdrbuff * rdrb, size_t idx)  #ifdef __APPLE__          pthread_mutex_lock(rdrb->lock);  #else -        if (pthread_mutex_lock(rdrb->lock) == EOWNERDEAD) { -                LOG_DBGF("Recovering dead mutex."); +        if (pthread_mutex_lock(rdrb->lock) == EOWNERDEAD)                  pthread_mutex_consistent(rdrb->lock); -        }  #endif          if (shm_rdrb_empty(rdrb)) {                  pthread_mutex_unlock(rdrb->lock); @@ -639,10 +592,8 @@ uint8_t * shm_du_buff_head_alloc(struct shm_du_buff * sdb,          assert(sdb); -        if ((long) (sdb->du_head - size) < 0) { -                LOG_ERR("Failed to allocate PCI headspace."); +        if (sdb->du_head < size)                  return NULL; -        }          sdb->du_head -= size; @@ -658,10 +609,8 @@ uint8_t * shm_du_buff_tail_alloc(struct shm_du_buff * sdb,          assert(sdb); -        if (sdb->du_tail + size >= sdb->size) { -                LOG_ERR("Failed to allocate PCI tailspace."); +        if (sdb->du_tail + size >= sdb->size)                  return NULL; -        }          buf = (uint8_t *) (sdb + 1) + sdb->du_tail; @@ -675,10 +624,8 @@ int shm_du_buff_head_release(struct shm_du_buff * sdb,  {          assert(sdb); -        if (size > sdb->du_tail - sdb->du_head) { -                LOG_DBGF("Tried to release beyond SDU boundary."); +        if (size > sdb->du_tail - sdb->du_head)                  return -EOVERFLOW; -        }          sdb->du_head += size; @@ -690,10 +637,8 @@ int shm_du_buff_tail_release(struct shm_du_buff * sdb,  {          assert(sdb); -        if (size > sdb->du_tail - sdb->du_head) { -                LOG_ERR("Tried to release beyond SDU boundary."); +        if (size > sdb->du_tail - sdb->du_head)                  return -EOVERFLOW; -        }          sdb->du_tail -= size; diff --git a/src/lib/sockets.c b/src/lib/sockets.c index a1517b7b..5c09e65e 100644 --- a/src/lib/sockets.c +++ b/src/lib/sockets.c @@ -20,17 +20,15 @@   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.   */ -#define OUROBOROS_PREFIX "libouroboros-sockets" -  #include <ouroboros/config.h>  #include <ouroboros/errno.h> -#include <ouroboros/logs.h>  #include <ouroboros/sockets.h>  #include <ouroboros/utils.h>  #include <sys/socket.h>  #include <sys/un.h>  #include <string.h> +#include <stdio.h>  #include <stdlib.h>  #include <pthread.h>  #include <stdbool.h> @@ -42,10 +40,8 @@ int client_socket_open(char * file_name)          struct sockaddr_un serv_addr;          sockfd = socket(AF_UNIX, SOCK_STREAM, 0); -        if (sockfd < 0) { -                LOG_ERR("Failed to open socket"); +        if (sockfd < 0)                  return -1; -        }          serv_addr.sun_family = AF_UNIX;          sprintf(serv_addr.sun_path, "%s", file_name); @@ -53,7 +49,6 @@ int client_socket_open(char * file_name)          if (connect(sockfd,                      (struct sockaddr *) &serv_addr,                      sizeof(serv_addr))) { -                LOG_ERR("Failed to connect to daemon");                  close(sockfd);                  return -1;          } @@ -68,18 +63,13 @@ int server_socket_open(char * file_name)          if (access(file_name, F_OK) != -1) {                  /* File exists */ -                if (unlink(file_name)) { -                        LOG_ERR("Failed to unlink filename: %s", -                                strerror(errno)); +                if (unlink(file_name))                          return -1; -                }          }          sockfd = socket(AF_UNIX, SOCK_STREAM, 0); -        if (sockfd < 0) { -                LOG_ERR("Failed to open socket"); +        if (sockfd < 0)                  return -1; -        }          serv_addr.sun_family = AF_UNIX;          sprintf(serv_addr.sun_path, "%s", file_name); @@ -87,13 +77,11 @@ int server_socket_open(char * file_name)          if (bind(sockfd,                   (struct sockaddr *) &serv_addr,                   sizeof(serv_addr))) { -                LOG_ERR("Failed to bind socket");                  close(sockfd);                  return -1;          }          if (listen(sockfd, 0)) { -                LOG_ERR("Failed to listen to socket");                  close(sockfd);                  return -1;          } @@ -120,9 +108,8 @@ static irm_msg_t * send_recv_irm_msg_timed(irm_msg_t * msg, bool timed)                  return NULL;          if (timed) -                if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, -                               (void *) &tv, sizeof(tv))) -                        LOG_WARN("Failed to set timeout on socket."); +                setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, +                           (void *) &tv, sizeof(tv));          buf.len = irm_msg__get_packed_size(msg);          if (buf.len == 0) { | 
