summaryrefslogtreecommitdiff
path: root/src/lib/shm_flow_set.c
diff options
context:
space:
mode:
authorDimitri Staessens <[email protected]>2018-12-21 16:16:46 +0100
committerSander Vrijders <[email protected]>2018-12-22 10:20:31 +0100
commit8eed1b3fc3d4e3261a68855ccc54c35102738a79 (patch)
tree28cbf1e57f267330aae761fa27d47821f7954fb4 /src/lib/shm_flow_set.c
parent933aa5ab479732d84b3331ef4638dd9be07695b2 (diff)
downloadouroboros-8eed1b3fc3d4e3261a68855ccc54c35102738a79.tar.gz
ouroboros-8eed1b3fc3d4e3261a68855ccc54c35102738a79.zip
irmd: Manage shm_flow_set from IRMd
This moves the creation and destruction of shm_flow_set shared memory structures from the init to the IRMd. Now the management of all shared data objects is performed by the IRMd. Signed-off-by: Dimitri Staessens <[email protected]> Signed-off-by: Sander Vrijders <[email protected]>
Diffstat (limited to 'src/lib/shm_flow_set.c')
-rw-r--r--src/lib/shm_flow_set.c147
1 files changed, 63 insertions, 84 deletions
diff --git a/src/lib/shm_flow_set.c b/src/lib/shm_flow_set.c
index 1c94c599..bab5d86e 100644
--- a/src/lib/shm_flow_set.c
+++ b/src/lib/shm_flow_set.c
@@ -78,31 +78,24 @@ struct shm_flow_set {
pid_t pid;
};
-struct shm_flow_set * shm_flow_set_create()
+static struct shm_flow_set * flow_set_create(pid_t pid,
+ int flags)
{
struct shm_flow_set * set;
ssize_t * shm_base;
- pthread_mutexattr_t mattr;
- pthread_condattr_t cattr;
char fn[FN_MAX_CHARS];
- mode_t mask;
int shm_fd;
- int i;
- sprintf(fn, SHM_FLOW_SET_PREFIX "%d", getpid());
+ sprintf(fn, SHM_FLOW_SET_PREFIX "%d", pid);
set = malloc(sizeof(*set));
if (set == NULL)
- return NULL;
+ goto fail_malloc;
- mask = umask(0);
-
- shm_fd = shm_open(fn, O_CREAT | O_RDWR, 0666);
+ shm_fd = shm_open(fn, flags, 0666);
if (shm_fd == -1)
goto fail_shm_open;
- umask(mask);
-
if (ftruncate(shm_fd, SHM_FLOW_SET_FILE_SIZE - 1) < 0) {
close(shm_fd);
goto fail_shm_open;
@@ -127,27 +120,61 @@ struct shm_flow_set * shm_flow_set_create()
set->lock = (pthread_mutex_t *)
(set->fqueues + PROG_MAX_FQUEUES * (SHM_BUFFER_SIZE));
+ return set;
+
+ fail_mmap:
+ if (flags & O_CREAT)
+ shm_unlink(fn);
+ fail_shm_open:
+ free(set);
+ fail_malloc:
+ return NULL;
+}
+
+struct shm_flow_set * shm_flow_set_create(pid_t pid)
+{
+ struct shm_flow_set * set;
+ pthread_mutexattr_t mattr;
+ pthread_condattr_t cattr;
+ mode_t mask;
+ int i;
+
+ mask = umask(0);
+
+ set = flow_set_create(pid, O_CREAT | O_RDWR);
+
+ umask(mask);
+
+ if (set == NULL)
+ goto fail_set;
+
if (pthread_mutexattr_init(&mattr))
- goto fail_mmap;
+ goto fail_mutexattr_init;
#ifdef HAVE_ROBUST_MUTEX
if (pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST))
- goto fail_mmap;
+ goto fail_mattr_set;
#endif
- if (pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED) ||
- pthread_mutex_init(set->lock, &mattr) ||
- pthread_condattr_init(&cattr) ||
- pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED))
- goto fail_mmap;
+ if (pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED))
+ goto fail_mattr_set;
+
+ if (pthread_mutex_init(set->lock, &mattr))
+ goto fail_mattr_set;
+
+ if (pthread_condattr_init(&cattr))
+ goto fail_condattr_init;
+
+ if (pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED))
+ goto fail_condattr_set;
#ifndef __APPLE__
if (pthread_condattr_setclock(&cattr, PTHREAD_COND_CLOCK))
- goto fail_mmap;
+ goto fail_condattr_set;
#endif
for (i = 0; i < PROG_MAX_FQUEUES; ++i) {
set->heads[i] = 0;
if (pthread_cond_init(&set->conds[i], &cattr))
- goto fail_mmap;
+ goto fail_init;
}
for (i = 0; i < SYS_MAX_FLOWS; ++i)
@@ -157,84 +184,37 @@ struct shm_flow_set * shm_flow_set_create()
return set;
- fail_mmap:
- shm_unlink(fn);
- fail_shm_open:
- free(set);
+ fail_init:
+ while (i-- > 0)
+ pthread_cond_destroy(&set->conds[i]);
+ fail_condattr_set:
+ pthread_condattr_destroy(&cattr);
+ fail_condattr_init:
+ pthread_mutex_destroy(set->lock);
+ fail_mattr_set:
+ pthread_mutexattr_destroy(&mattr);
+ fail_mutexattr_init:
+ shm_flow_set_destroy(set);
+ fail_set:
return NULL;
}
struct shm_flow_set * shm_flow_set_open(pid_t pid)
{
- struct shm_flow_set * set;
- ssize_t * shm_base;
- char fn[FN_MAX_CHARS];
- int shm_fd;
-
- sprintf(fn, SHM_FLOW_SET_PREFIX "%d", pid);
-
- set = malloc(sizeof(*set));
- if (set == NULL)
- return NULL;
-
- shm_fd = shm_open(fn, O_RDWR, 0666);
- if (shm_fd == -1) {
- free(set);
- return NULL;
- }
-
- shm_base = mmap(NULL,
- SHM_FLOW_SET_FILE_SIZE,
- PROT_READ | PROT_WRITE,
- MAP_SHARED,
- shm_fd,
- 0);
-
- close(shm_fd);
-
- if (shm_base == MAP_FAILED) {
- shm_unlink(fn);
- free(set);
- return NULL;
- }
-
- set->mtable = shm_base;
- set->heads = (size_t *) (set->mtable + SYS_MAX_FLOWS);
- set->conds = (pthread_cond_t *)(set->heads + PROG_MAX_FQUEUES);
- set->fqueues = (struct portevent *) (set->conds + PROG_MAX_FQUEUES);
- set->lock = (pthread_mutex_t *)
- (set->fqueues + PROG_MAX_FQUEUES * (SHM_BUFFER_SIZE));
- set->pid = pid;
-
- return set;
+ return flow_set_create(pid, O_RDWR);
}
void shm_flow_set_destroy(struct shm_flow_set * set)
{
char fn[25];
- struct lockfile * lf = NULL;
assert(set);
- if (set->pid != getpid()) {
- lf = lockfile_open();
- if (lf == NULL)
- return;
-
- if (lockfile_owner(lf) == getpid()) {
- lockfile_close(lf);
- } else {
- lockfile_close(lf);
- return;
- }
- }
-
sprintf(fn, SHM_FLOW_SET_PREFIX "%d", set->pid);
- munmap(set->mtable, SHM_FLOW_SET_FILE_SIZE);
- shm_unlink(fn);
+ shm_flow_set_close(set);
- free(set);
+ shm_unlink(fn);
}
void shm_flow_set_close(struct shm_flow_set * set)
@@ -242,7 +222,6 @@ void shm_flow_set_close(struct shm_flow_set * set)
assert(set);
munmap(set->mtable, SHM_FLOW_SET_FILE_SIZE);
-
free(set);
}