implement POSIX shared memory
This commit is contained in:
parent
71df8b2760
commit
ebd7af6940
2 changed files with 42 additions and 0 deletions
21
src/mman/shm_open.c
Normal file
21
src/mman/shm_open.c
Normal file
|
|
@ -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;
|
||||
}
|
||||
21
src/mman/shm_unlink.c
Normal file
21
src/mman/shm_unlink.c
Normal file
|
|
@ -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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue