implement POSIX shared memory
authorRich Felker <dalias@aerifal.cx>
Thu, 3 Mar 2011 05:30:31 +0000 (00:30 -0500)
committerRich Felker <dalias@aerifal.cx>
Thu, 3 Mar 2011 05:30:31 +0000 (00:30 -0500)
src/mman/shm_open.c [new file with mode: 0644]
src/mman/shm_unlink.c [new file with mode: 0644]

diff --git a/src/mman/shm_open.c b/src/mman/shm_open.c
new file mode 100644 (file)
index 0000000..d368622
--- /dev/null
@@ -0,0 +1,21 @@
+#include <sys/mman.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+
+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 (file)
index 0000000..46e60b3
--- /dev/null
@@ -0,0 +1,21 @@
+#include <sys/mman.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+
+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;
+}