summaryrefslogtreecommitdiff
path: root/src/mman/shm_open.c
blob: a9be899b2b5e9718b936e792dc37fdc662ca9d7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <sys/mman.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>

char *__strchrnul(const char *, int);

static const char *mapname(const char *name, char *buf)
{
	char *p;
	while (*name == '/') name++;
	if (*(p = __strchrnul(name, '/')) || p==name ||
	    (p-name <= 2 && name[0]=='.' && p[-1]=='.')) {
		errno = EINVAL;
		return 0;
	}
	if (p-name > NAME_MAX) {
		errno = ENAMETOOLONG;
		return 0;
	}
	memcpy(buf, "/dev/shm/", 9);
	memcpy(buf+9, name, p-name+1);
	return buf;
}

int shm_open(const char *name, int flag, mode_t mode)
{
	char buf[NAME_MAX+10];
	if (!(name = mapname(name, buf))) return -1;
	return open(name, flag|O_NOFOLLOW|O_CLOEXEC|O_NONBLOCK, mode);
}

int shm_unlink(const char *name)
{
	char buf[NAME_MAX+10];
	if (!(name = mapname(name, buf))) return -1;
	return unlink(name);
}