summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Staessens <[email protected]>2017-11-29 23:20:51 +0100
committerSander Vrijders <[email protected]>2017-11-30 09:03:21 +0100
commita3fa3d4fec3994597b9c745709b4b85887913308 (patch)
tree7ff8974507dbf227fd92c3de392a1d55af645578
parent0dad9034236652fb68f27651ddbe0c65376a255f (diff)
downloadouroboros-a3fa3d4fec3994597b9c745709b4b85887913308.tar.gz
ouroboros-a3fa3d4fec3994597b9c745709b4b85887913308.zip
lib: Refactor shm_rdrbuff and shm_rbuff
This refactors the creation of shm_rdrbuff and shm_rbuff elements. All cleanup is now handled and the common code between the _open() and _create() calls is moved to a static function. Common code between the pthread and lockless rbuff implementations was moved to shm_rbuff.c Signed-off-by: Dimitri Staessens <[email protected]> Signed-off-by: Sander Vrijders <[email protected]>
-rw-r--r--src/lib/shm_rbuff.c175
-rw-r--r--src/lib/shm_rbuff_ll.c189
-rw-r--r--src/lib/shm_rbuff_pthr.c189
-rw-r--r--src/lib/shm_rdrbuff.c230
4 files changed, 286 insertions, 497 deletions
diff --git a/src/lib/shm_rbuff.c b/src/lib/shm_rbuff.c
index 93108332..00f0b92b 100644
--- a/src/lib/shm_rbuff.c
+++ b/src/lib/shm_rbuff.c
@@ -24,6 +24,181 @@
#include "config.h"
+#include <ouroboros/shm_rbuff.h>
+#include <ouroboros/lockfile.h>
+#include <ouroboros/time_utils.h>
+#include <ouroboros/errno.h>
+#include <ouroboros/fccntl.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>
+#include <sys/stat.h>
+#include <assert.h>
+#include <stdbool.h>
+
+#define FN_MAX_CHARS 255
+
+#define SHM_RB_FILE_SIZE ((SHM_BUFFER_SIZE) * sizeof(ssize_t) \
+ + 3 * sizeof(size_t) \
+ + sizeof(pthread_mutex_t) \
+ + 2 * sizeof (pthread_cond_t))
+
+#define shm_rbuff_used(rb) ((*rb->head + (SHM_BUFFER_SIZE) - *rb->tail) \
+ & ((SHM_BUFFER_SIZE) - 1))
+#define shm_rbuff_free(rb) (shm_rbuff_used(rb) + 1 < (SHM_BUFFER_SIZE))
+#define shm_rbuff_empty(rb) (*rb->head == *rb->tail)
+#define head_el_ptr(rb) (rb->shm_base + *rb->head)
+#define tail_el_ptr(rb) (rb->shm_base + *rb->tail)
+
+struct shm_rbuff {
+ ssize_t * shm_base; /* start of entry */
+ size_t * head; /* start of ringbuffer head */
+ size_t * tail; /* start of ringbuffer tail */
+ size_t * acl; /* access control */
+ pthread_mutex_t * lock; /* lock all free space in shm */
+ pthread_cond_t * add; /* SDU arrived */
+ pthread_cond_t * del; /* SDU removed */
+ pid_t api; /* api of the owner */
+ int port_id; /* port_id of the flow */
+};
+
+void shm_rbuff_close(struct shm_rbuff * rb)
+{
+ assert(rb);
+
+ munmap(rb->shm_base, SHM_RB_FILE_SIZE);
+
+ free(rb);
+}
+
+#define MM_FLAGS (PROT_READ | PROT_WRITE)
+
+struct shm_rbuff * rbuff_create(pid_t api, int port_id, int flags)
+{
+ struct shm_rbuff * rb;
+ int fd;
+ ssize_t * shm_base;
+ char fn[FN_MAX_CHARS];
+
+ sprintf(fn, SHM_RBUFF_PREFIX "%d.%d", api, port_id);
+
+ rb = malloc(sizeof(*rb));
+ if (rb == NULL)
+ goto fail_malloc;
+
+ fd = shm_open(fn, flags, 0666);
+ if (fd == -1)
+ goto fail_open;
+
+ if ((flags & O_CREAT) && ftruncate(fd, SHM_RB_FILE_SIZE - 1) < 0)
+ goto fail_truncate;
+
+ shm_base = mmap(NULL, SHM_RB_FILE_SIZE, MM_FLAGS, MAP_SHARED, fd, 0);
+ if (shm_base == MAP_FAILED)
+ goto fail_truncate;
+
+ close(fd);
+
+ rb->shm_base = shm_base;
+ rb->head = (size_t *) (rb->shm_base + (SHM_BUFFER_SIZE));
+ rb->tail = rb->head + 1;
+ rb->acl = rb->tail + 1;
+ rb->lock = (pthread_mutex_t *) (rb->acl + 1);
+ rb->add = (pthread_cond_t *) (rb->lock + 1);
+ rb->del = rb->add + 1;
+
+ rb->api = api;
+ rb->port_id = port_id;
+
+ return rb;
+
+ fail_truncate:
+ close(fd);
+ if (flags & O_CREAT)
+ shm_unlink(fn);
+ fail_open:
+ free(rb);
+ fail_malloc:
+ return NULL;
+}
+
+struct shm_rbuff * shm_rbuff_create(pid_t api, int port_id)
+{
+ struct shm_rbuff * rb;
+ pthread_mutexattr_t mattr;
+ pthread_condattr_t cattr;
+ mode_t mask;
+
+ mask = umask(0);
+
+ rb = rbuff_create(api, port_id, O_CREAT | O_EXCL | O_RDWR);
+
+ umask(mask);
+
+ if (rb == NULL)
+ goto fail_rb;
+
+ if (pthread_mutexattr_init(&mattr))
+ goto fail_mattr;
+
+ pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
+#ifdef HAVE_ROBUST_MUTEX
+ pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST);
+#endif
+ if (pthread_mutex_init(rb->lock, &mattr))
+ goto fail_mutex;
+
+ if (pthread_condattr_init(&cattr))
+ goto fail_cattr;
+
+ pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED);
+#ifndef __APPLE__
+ pthread_condattr_setclock(&cattr, PTHREAD_COND_CLOCK);
+#endif
+ if (pthread_cond_init(rb->add, &cattr))
+ goto fail_add;
+
+ if (pthread_cond_init(rb->del, &cattr))
+ goto fail_del;
+
+ *rb->acl = ACL_RDWR;
+ *rb->head = 0;
+ *rb->tail = 0;
+
+ rb->api = api;
+ rb->port_id = port_id;
+
+ pthread_mutexattr_destroy(&mattr);
+ pthread_condattr_destroy(&cattr);
+
+ return rb;
+
+ fail_del:
+ pthread_cond_destroy(rb->add);
+ fail_add:
+ pthread_condattr_destroy(&cattr);
+ fail_cattr:
+ pthread_mutex_destroy(rb->lock);
+ fail_mutex:
+ pthread_mutexattr_destroy(&mattr);
+ fail_mattr:
+ shm_rbuff_destroy(rb);
+ fail_rb:
+ return NULL;
+}
+
+struct shm_rbuff * shm_rbuff_open(pid_t api, int port_id)
+{
+ return rbuff_create(api, port_id, O_RDWR);
+}
+
#if (defined(SHM_RBUFF_LOCKLESS) && \
(defined(__GNUC__) || defined (__clang__)))
#include "shm_rbuff_ll.c"
diff --git a/src/lib/shm_rbuff_ll.c b/src/lib/shm_rbuff_ll.c
index b77c4374..6ac9af47 100644
--- a/src/lib/shm_rbuff_ll.c
+++ b/src/lib/shm_rbuff_ll.c
@@ -20,193 +20,9 @@
* Foundation, Inc., http://www.fsf.org/about/contact/.
*/
-#include "config.h"
-
-#include <ouroboros/shm_rbuff.h>
-#include <ouroboros/lockfile.h>
-#include <ouroboros/time_utils.h>
-#include <ouroboros/errno.h>
-#include <ouroboros/fccntl.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>
-#include <sys/stat.h>
-#include <assert.h>
-#include <stdbool.h>
-
-#define FN_MAX_CHARS 255
-
-#define SHM_RBUFF_FILE_SIZE ((SHM_BUFFER_SIZE) * sizeof(ssize_t) \
- + 3 * sizeof(size_t) \
- + sizeof(pthread_mutex_t) \
- + 2 * sizeof (pthread_cond_t))
-
#define RB_HEAD __sync_fetch_and_add(rb->head, 0)
#define RB_TAIL __sync_fetch_and_add(rb->tail, 0)
-#define shm_rbuff_used(rb) ((RB_HEAD + (SHM_BUFFER_SIZE) - RB_TAIL) \
- & ((SHM_BUFFER_SIZE) - 1))
-#define shm_rbuff_free(rb) (shm_rbuff_used(rb) + 1 < (SHM_BUFFER_SIZE))
-#define shm_rbuff_empty(rb) (RB_HEAD == RB_TAIL)
-#define head_el_ptr(rb) (rb->shm_base + RB_HEAD)
-#define tail_el_ptr(rb) (rb->shm_base + RB_TAIL)
-
-struct shm_rbuff {
- ssize_t * shm_base; /* start of entry */
- size_t * head; /* start of ringbuffer head */
- size_t * tail; /* start of ringbuffer tail */
- size_t * acl; /* access control */
- pthread_mutex_t * lock; /* lock all free space in shm */
- pthread_cond_t * add; /* SDU arrived */
- pthread_cond_t * del; /* SDU removed */
- pid_t api; /* api of the owner */
- int port_id; /* port_id of the flow */
-};
-
-struct shm_rbuff * shm_rbuff_create(pid_t api, int port_id)
-{
- struct shm_rbuff * rb;
- int shm_fd;
- ssize_t * shm_base;
- pthread_mutexattr_t mattr;
- pthread_condattr_t cattr;
- char fn[FN_MAX_CHARS];
- mode_t mask;
-
- sprintf(fn, SHM_RBUFF_PREFIX "%d.%d", api, port_id);
-
- rb = malloc(sizeof(*rb));
- if (rb == NULL)
- return NULL;
-
- mask = umask(0);
-
- shm_fd = shm_open(fn, O_CREAT | O_EXCL | O_RDWR, 0666);
- if (shm_fd == -1) {
- free(rb);
- return NULL;
- }
-
- umask(mask);
-
- if (ftruncate(shm_fd, SHM_RBUFF_FILE_SIZE - 1) < 0) {
- free(rb);
- close(shm_fd);
- return NULL;
- }
-
- shm_base = mmap(NULL,
- SHM_RBUFF_FILE_SIZE,
- PROT_READ | PROT_WRITE,
- MAP_SHARED,
- shm_fd,
- 0);
-
- close(shm_fd);
-
- if (shm_base == MAP_FAILED) {
- shm_unlink(fn);
- free(rb);
- return NULL;
- }
-
- rb->shm_base = shm_base;
- rb->head = (size_t *) (rb->shm_base + (SHM_BUFFER_SIZE));
- rb->tail = rb->head + 1;
- rb->acl = rb->tail + 1;
- rb->lock = (pthread_mutex_t *) (rb->acl + 1);
- rb->add = (pthread_cond_t *) (rb->lock + 1);
- rb->del = rb->add + 1;
-
- pthread_mutexattr_init(&mattr);
-#ifdef HAVE_ROBUST_MUTEX
- pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST);
-#endif
- pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
- pthread_mutex_init(rb->lock, &mattr);
-
- pthread_condattr_init(&cattr);
- pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED);
-#ifndef __APPLE__
- pthread_condattr_setclock(&cattr, PTHREAD_COND_CLOCK);
-#endif
- pthread_cond_init(rb->add, &cattr);
- pthread_cond_init(rb->del, &cattr);
-
- *rb->acl = ACL_RDWR;
- *rb->head = 0;
- *rb->tail = 0;
-
- rb->api = api;
- rb->port_id = port_id;
-
- return rb;
-}
-
-struct shm_rbuff * shm_rbuff_open(pid_t api, int port_id)
-{
- struct shm_rbuff * rb;
- int shm_fd;
- ssize_t * shm_base;
- char fn[FN_MAX_CHARS];
-
- sprintf(fn, SHM_RBUFF_PREFIX "%d.%d", api, port_id);
-
- rb = malloc(sizeof(*rb));
- if (rb == NULL)
- return NULL;
-
- shm_fd = shm_open(fn, O_RDWR, 0666);
- if (shm_fd == -1) {
- free(rb);
- return NULL;
- }
-
- shm_base = mmap(NULL,
- SHM_RBUFF_FILE_SIZE,
- PROT_READ | PROT_WRITE,
- MAP_SHARED,
- shm_fd,
- 0);
-
- close(shm_fd);
-
- if (shm_base == MAP_FAILED) {
- shm_unlink(fn);
- free(rb);
- return NULL;
- }
-
- rb->shm_base = shm_base;
- rb->head = (size_t *) (rb->shm_base + (SHM_BUFFER_SIZE));
- rb->tail = rb->head + 1;
- rb->acl = rb->tail + 1;
- rb->lock = (pthread_mutex_t *) (rb->acl + 1);
- rb->add = (pthread_cond_t *) (rb->lock + 1);
- rb->del = rb->add + 1;
-
- rb->api = api;
- rb->port_id = port_id;
-
- return rb;
-}
-
-void shm_rbuff_close(struct shm_rbuff * rb)
-{
- assert(rb);
-
- munmap(rb->shm_base, SHM_RBUFF_FILE_SIZE);
-
- free(rb);
-}
-
void shm_rbuff_destroy(struct shm_rbuff * rb)
{
char fn[FN_MAX_CHARS];
@@ -215,10 +31,9 @@ void shm_rbuff_destroy(struct shm_rbuff * rb)
sprintf(fn, SHM_RBUFF_PREFIX "%d.%d", rb->api, rb->port_id);
- munmap(rb->shm_base, SHM_RBUFF_FILE_SIZE);
- shm_unlink(fn);
+ shm_rbuff_close(rb);
- free(rb);
+ shm_unlink(fn);
}
int shm_rbuff_write(struct shm_rbuff * rb,
diff --git a/src/lib/shm_rbuff_pthr.c b/src/lib/shm_rbuff_pthr.c
index fb183d8f..565bb1fa 100644
--- a/src/lib/shm_rbuff_pthr.c
+++ b/src/lib/shm_rbuff_pthr.c
@@ -20,190 +20,6 @@
* Foundation, Inc., http://www.fsf.org/about/contact/.
*/
-#define _POSIX_C_SOURCE 200809L
-
-#include <ouroboros/shm_rbuff.h>
-#include <ouroboros/lockfile.h>
-#include <ouroboros/time_utils.h>
-#include <ouroboros/errno.h>
-#include <ouroboros/fccntl.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>
-#include <sys/stat.h>
-#include <assert.h>
-#include <stdbool.h>
-
-#define FN_MAX_CHARS 255
-
-#define SHM_RBUFF_FILE_SIZE ((SHM_BUFFER_SIZE) * sizeof(ssize_t) \
- + 3 * sizeof(size_t) \
- + sizeof(pthread_mutex_t) \
- + 2 * sizeof (pthread_cond_t))
-
-#define shm_rbuff_used(rb) ((*rb->head + (SHM_BUFFER_SIZE) - *rb->tail) \
- & ((SHM_BUFFER_SIZE) - 1))
-#define shm_rbuff_free(rb) (shm_rbuff_used(rb) + 1 < (SHM_BUFFER_SIZE))
-#define shm_rbuff_empty(rb) (*rb->head == *rb->tail)
-#define head_el_ptr(rb) (rb->shm_base + *rb->head)
-#define tail_el_ptr(rb) (rb->shm_base + *rb->tail)
-
-struct shm_rbuff {
- ssize_t * shm_base; /* start of entry */
- size_t * head; /* start of ringbuffer head */
- size_t * tail; /* start of ringbuffer tail */
- size_t * acl; /* access control */
- pthread_mutex_t * lock; /* lock all free space in shm */
- pthread_cond_t * add; /* SDU arrived */
- pthread_cond_t * del; /* SDU removed */
- pid_t api; /* api of the owner */
- int port_id; /* port_id of the flow */
-};
-
-struct shm_rbuff * shm_rbuff_create(pid_t api, int port_id)
-{
- struct shm_rbuff * rb;
- int shm_fd;
- ssize_t * shm_base;
- pthread_mutexattr_t mattr;
- pthread_condattr_t cattr;
- char fn[FN_MAX_CHARS];
- mode_t mask;
-
- sprintf(fn, SHM_RBUFF_PREFIX "%d.%d", api, port_id);
-
- rb = malloc(sizeof(*rb));
- if (rb == NULL)
- return NULL;
-
- mask = umask(0);
-
- shm_fd = shm_open(fn, O_CREAT | O_EXCL | O_RDWR, 0666);
- if (shm_fd == -1) {
- free(rb);
- return NULL;
- }
-
- umask(mask);
-
- if (ftruncate(shm_fd, SHM_RBUFF_FILE_SIZE - 1) < 0) {
- free(rb);
- close(shm_fd);
- return NULL;
- }
-
- shm_base = mmap(NULL,
- SHM_RBUFF_FILE_SIZE,
- PROT_READ | PROT_WRITE,
- MAP_SHARED,
- shm_fd,
- 0);
-
- close(shm_fd);
-
- if (shm_base == MAP_FAILED) {
- shm_unlink(fn);
- free(rb);
- return NULL;
- }
-
- rb->shm_base = shm_base;
- rb->head = (size_t *) (rb->shm_base + (SHM_BUFFER_SIZE));
- rb->tail = rb->head + 1;
- rb->acl = rb->tail + 1;
- rb->lock = (pthread_mutex_t *) (rb->acl + 1);
- rb->add = (pthread_cond_t *) (rb->lock + 1);
- rb->del = rb->add + 1;
-
- pthread_mutexattr_init(&mattr);
-#ifdef HAVE_ROBUST_MUTEX
- pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST);
-#endif
- pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
- pthread_mutex_init(rb->lock, &mattr);
-
- pthread_condattr_init(&cattr);
- pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED);
-#ifndef __APPLE__
- pthread_condattr_setclock(&cattr, PTHREAD_COND_CLOCK);
-#endif
- pthread_cond_init(rb->add, &cattr);
- pthread_cond_init(rb->del, &cattr);
-
- *rb->acl = ACL_RDWR;
- *rb->head = 0;
- *rb->tail = 0;
-
- rb->api = api;
- rb->port_id = port_id;
-
- return rb;
-}
-
-struct shm_rbuff * shm_rbuff_open(pid_t api, int port_id)
-{
- struct shm_rbuff * rb;
- int shm_fd;
- ssize_t * shm_base;
- char fn[FN_MAX_CHARS];
-
- sprintf(fn, SHM_RBUFF_PREFIX "%d.%d", api, port_id);
-
- rb = malloc(sizeof(*rb));
- if (rb == NULL)
- return NULL;
-
- shm_fd = shm_open(fn, O_RDWR, 0666);
- if (shm_fd == -1) {
- free(rb);
- return NULL;
- }
-
- shm_base = mmap(NULL,
- SHM_RBUFF_FILE_SIZE,
- PROT_READ | PROT_WRITE,
- MAP_SHARED,
- shm_fd,
- 0);
-
- close(shm_fd);
-
- if (shm_base == MAP_FAILED) {
- shm_unlink(fn);
- free(rb);
- return NULL;
- }
-
- rb->shm_base = shm_base;
- rb->head = (size_t *) (rb->shm_base + (SHM_BUFFER_SIZE));
- rb->tail = rb->head + 1;
- rb->acl = rb->tail + 1;
- rb->lock = (pthread_mutex_t *) (rb->acl + 1);
- rb->add = (pthread_cond_t *) (rb->lock + 1);
- rb->del = rb->add + 1;
-
- rb->api = api;
- rb->port_id = port_id;
-
- return rb;
-}
-
-void shm_rbuff_close(struct shm_rbuff * rb)
-{
- assert(rb);
-
- munmap(rb->shm_base, SHM_RBUFF_FILE_SIZE);
-
- free(rb);
-}
-
void shm_rbuff_destroy(struct shm_rbuff * rb)
{
char fn[FN_MAX_CHARS];
@@ -219,10 +35,9 @@ void shm_rbuff_destroy(struct shm_rbuff * rb)
#endif
sprintf(fn, SHM_RBUFF_PREFIX "%d.%d", rb->api, rb->port_id);
- munmap(rb->shm_base, SHM_RBUFF_FILE_SIZE);
- shm_unlink(fn);
+ shm_rbuff_close(rb);
- free(rb);
+ shm_unlink(fn);
}
int shm_rbuff_write(struct shm_rbuff * rb,
diff --git a/src/lib/shm_rdrbuff.c b/src/lib/shm_rdrbuff.c
index 2853d5bb..4c00c74d 100644
--- a/src/lib/shm_rdrbuff.c
+++ b/src/lib/shm_rdrbuff.c
@@ -134,57 +134,62 @@ static char * rdrb_filename(void)
return str;
}
-struct shm_rdrbuff * shm_rdrbuff_create()
+void shm_rdrbuff_close(struct shm_rdrbuff * rdrb)
{
- struct shm_rdrbuff * rdrb;
- mode_t mask;
- int shm_fd;
- uint8_t * shm_base;
- pthread_mutexattr_t mattr;
- pthread_condattr_t cattr;
- char * shm_rdrb_fn = rdrb_filename();
+ assert(rdrb);
+
+ munmap(rdrb->shm_base, SHM_FILE_SIZE);
+ free(rdrb);
+}
+
+void shm_rdrbuff_destroy(struct shm_rdrbuff * rdrb)
+{
+ char * shm_rdrb_fn;
+
+ assert(rdrb);
+
+ if (getpid() != *rdrb->api && kill(*rdrb->api, 0) == 0)
+ return;
+
+ shm_rdrbuff_close(rdrb);
+
+ shm_rdrb_fn = rdrb_filename();
if (shm_rdrb_fn == NULL)
- return NULL;
+ return;
- rdrb = malloc(sizeof *rdrb);
- if (rdrb == NULL) {
- free(shm_rdrb_fn);
- return NULL;
- }
+ shm_unlink(shm_rdrb_fn);
+ free(shm_rdrb_fn);
+}
- mask = umask(0);
+#define MM_FLAGS (PROT_READ | PROT_WRITE)
- shm_fd = shm_open(shm_rdrb_fn, O_CREAT | O_EXCL | O_RDWR, 0666);
- if (shm_fd == -1) {
- free(shm_rdrb_fn);
- free(rdrb);
- return NULL;
- }
+static struct shm_rdrbuff * rdrb_create(int flags)
+{
+ struct shm_rdrbuff * rdrb;
+ int fd;
+ uint8_t * shm_base;
+ char * shm_rdrb_fn;
- umask(mask);
+ shm_rdrb_fn = rdrb_filename();
+ if (shm_rdrb_fn == NULL)
+ goto fail_fn;
- if (ftruncate(shm_fd, SHM_FILE_SIZE - 1) < 0) {
- free(shm_rdrb_fn);
- close(shm_fd);
- free(rdrb);
- return NULL;
- }
+ rdrb = malloc(sizeof *rdrb);
+ if (rdrb == NULL)
+ goto fail_rdrb;
- shm_base = mmap(NULL,
- SHM_FILE_SIZE,
- PROT_READ | PROT_WRITE,
- MAP_SHARED,
- shm_fd,
- 0);
+ fd = shm_open(shm_rdrb_fn, flags, 0666);
+ if (fd == -1)
+ goto fail_open;
- close(shm_fd);
+ if ((flags & O_CREAT) && ftruncate(fd, SHM_FILE_SIZE - 1) < 0)
+ goto fail_truncate;
- if (shm_base == MAP_FAILED) {
- shm_unlink(shm_rdrb_fn);
- free(shm_rdrb_fn);
- free(rdrb);
- return NULL;
- }
+ shm_base = mmap(NULL, SHM_FILE_SIZE, MM_FLAGS, MAP_SHARED, fd, 0);
+ if (shm_base == MAP_FAILED)
+ goto fail_truncate;
+
+ close(fd);
rdrb->shm_base = shm_base;
rdrb->head = (size_t *) ((uint8_t *) rdrb->shm_base + SHM_BLOCKS_SIZE);
@@ -194,80 +199,88 @@ struct shm_rdrbuff * shm_rdrbuff_create()
rdrb->healthy = rdrb->full + 1;
rdrb->api = (pid_t *) (rdrb->healthy + 1);
- pthread_mutexattr_init(&mattr);
+ free(shm_rdrb_fn);
+
+ return rdrb;
+
+ fail_truncate:
+ close(fd);
+ if (flags & O_CREAT)
+ shm_unlink(shm_rdrb_fn);
+ fail_open:
+ free(rdrb);
+ fail_rdrb:
+ free(shm_rdrb_fn);
+ fail_fn:
+ return NULL;
+}
+
+struct shm_rdrbuff * shm_rdrbuff_create()
+{
+ struct shm_rdrbuff * rdrb;
+ mode_t mask;
+ pthread_mutexattr_t mattr;
+ pthread_condattr_t cattr;
+
+ mask = umask(0);
+
+ rdrb = rdrb_create(O_CREAT | O_EXCL | O_RDWR);
+
+ umask(mask);
+
+ if (rdrb == NULL)
+ goto fail_rdrb;
+
+ if (pthread_mutexattr_init(&mattr))
+ goto fail_mattr;
+
pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
#ifdef HAVE_ROBUST_MUTEX
pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST);
#endif
- pthread_mutex_init(rdrb->lock, &mattr);
+ if (pthread_mutex_init(rdrb->lock, &mattr))
+ goto fail_mutex;
+
+ if (pthread_condattr_init(&cattr))
+ goto fail_cattr;
- pthread_condattr_init(&cattr);
pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED);
#ifndef __APPLE__
pthread_condattr_setclock(&cattr, PTHREAD_COND_CLOCK);
#endif
- pthread_cond_init(rdrb->full, &cattr);
- pthread_cond_init(rdrb->healthy, &cattr);
+ if (pthread_cond_init(rdrb->full, &cattr))
+ goto fail_full;
+
+ if (pthread_cond_init(rdrb->healthy, &cattr))
+ goto fail_healthy;
*rdrb->head = 0;
*rdrb->tail = 0;
*rdrb->api = getpid();
- free(shm_rdrb_fn);
+ pthread_mutexattr_destroy(&mattr);
+ pthread_condattr_destroy(&cattr);
return rdrb;
+
+ fail_healthy:
+ pthread_cond_destroy(rdrb->full);
+ fail_full:
+ pthread_condattr_destroy(&cattr);
+ fail_cattr:
+ pthread_mutex_destroy(rdrb->lock);
+ fail_mutex:
+ pthread_mutexattr_destroy(&mattr);
+ fail_mattr:
+ shm_rdrbuff_destroy(rdrb);
+ fail_rdrb:
+ return NULL;
}
struct shm_rdrbuff * shm_rdrbuff_open()
{
- struct shm_rdrbuff * rdrb;
- int shm_fd;
- uint8_t * shm_base;
- char * shm_rdrb_fn = rdrb_filename();
- if (shm_rdrb_fn == NULL)
- return NULL;
-
- rdrb = malloc(sizeof *rdrb);
- if (rdrb == NULL) {
- free(shm_rdrb_fn);
- return NULL;
- }
-
- shm_fd = shm_open(shm_rdrb_fn, O_RDWR, 0666);
- if (shm_fd < 0) {
- free(shm_rdrb_fn);
- free(rdrb);
- return NULL;
- }
-
- shm_base = mmap(NULL,
- SHM_FILE_SIZE,
- PROT_READ | PROT_WRITE,
- MAP_SHARED,
- shm_fd,
- 0);
-
- close(shm_fd);
-
- if (shm_base == MAP_FAILED) {
- shm_unlink(shm_rdrb_fn);
- free(shm_rdrb_fn);
- free(rdrb);
- return NULL;
- }
-
- rdrb->shm_base = shm_base;
- rdrb->head = (size_t *) ((uint8_t *) rdrb->shm_base + SHM_BLOCKS_SIZE);
- rdrb->tail = rdrb->head + 1;
- rdrb->lock = (pthread_mutex_t *) (rdrb->tail + 1);
- rdrb->full = (pthread_cond_t *) (rdrb->lock + 1);
- rdrb->healthy = rdrb->full + 1;
- rdrb->api = (pid_t *) (rdrb->healthy + 1);
-
- free(shm_rdrb_fn);
-
- return rdrb;
+ return rdrb_create(O_RDWR);
}
int shm_rdrbuff_wait_full(struct shm_rdrbuff * rdrb,
@@ -315,14 +328,6 @@ int shm_rdrbuff_wait_full(struct shm_rdrbuff * rdrb,
return 0;
}
-void shm_rdrbuff_close(struct shm_rdrbuff * rdrb)
-{
- assert(rdrb);
-
- munmap(rdrb->shm_base, SHM_FILE_SIZE);
- free(rdrb);
-}
-
void shm_rdrbuff_purge(void)
{
char * shm_rdrb_fn;
@@ -335,27 +340,6 @@ void shm_rdrbuff_purge(void)
free(shm_rdrb_fn);
}
-void shm_rdrbuff_destroy(struct shm_rdrbuff * rdrb)
-{
- char * shm_rdrb_fn;
-
- assert(rdrb);
-
- if (getpid() != *rdrb->api && kill(*rdrb->api, 0) == 0)
- return;
-
- munmap(rdrb->shm_base, SHM_FILE_SIZE);
-
- shm_rdrb_fn = rdrb_filename();
- if (shm_rdrb_fn == NULL)
- return;
-
- shm_unlink(shm_rdrb_fn);
-
- free(rdrb);
- free(shm_rdrb_fn);
-}
-
ssize_t shm_rdrbuff_write(struct shm_rdrbuff * rdrb,
size_t headspace,
size_t tailspace,