From ebd7af6940bebb3b083a4d9239da27a66d1c32ec Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Thu, 3 Mar 2011 00:30:31 -0500 Subject: [PATCH] implement POSIX shared memory --- src/mman/shm_open.c | 21 +++++++++++++++++++++ src/mman/shm_unlink.c | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 src/mman/shm_open.c create mode 100644 src/mman/shm_unlink.c diff --git a/src/mman/shm_open.c b/src/mman/shm_open.c new file mode 100644 index 00000000..d368622d --- /dev/null +++ b/src/mman/shm_open.c @@ -0,0 +1,21 @@ +#include +#include +#include +#include +#include + +int shm_open(const char *name, int flag, mode_t mode) +{ + int fd, dir; + + while (*name == '/') name++; + if (strchr(name, '/')) { + errno = EINVAL; + return -1; + } + + if ((dir = open("/dev/shm", O_DIRECTORY|O_RDONLY)) < 0) return -1; + fd = openat(dir, name, flag|O_NOFOLLOW|O_CLOEXEC|O_NONBLOCK, mode); + close(dir); + return fd; +} diff --git a/src/mman/shm_unlink.c b/src/mman/shm_unlink.c new file mode 100644 index 00000000..46e60b3b --- /dev/null +++ b/src/mman/shm_unlink.c @@ -0,0 +1,21 @@ +#include +#include +#include +#include +#include + +int shm_unlink(const char *name) +{ + int dir, ret; + + while (*name == '/') name++; + if (strchr(name, '/')) { + errno = EINVAL; + return -1; + } + + if ((dir = open("/dev/shm", O_DIRECTORY|O_RDONLY)) < 0) return -1; + ret = unlinkat(dir, name, 0); + close(dir); + return ret; +} -- 2.25.1