1 #include <sys/mman.h>
2 #include <errno.h>
3 #include <fcntl.h>
4 #include <unistd.h>
5 #include <string.h>
6 #include <limits.h>
7 #include <pthread.h>
8 #include <unsupported_api.h>
9
__shm_mapname(const char * name,char * buf)10 char *__shm_mapname(const char *name, char *buf)
11 {
12 char *p;
13 while (*name == '/') name++;
14 if (*(p = __strchrnul(name, '/')) || p==name ||
15 (p-name <= 2 && name[0]=='.' && p[-1]=='.')) {
16 errno = EINVAL;
17 return 0;
18 }
19 if (p-name > NAME_MAX) {
20 errno = ENAMETOOLONG;
21 return 0;
22 }
23 memcpy(buf, "/dev/shm/", 9);
24 memcpy(buf+9, name, p-name+1);
25 return buf;
26 }
27
shm_open(const char * name,int flag,mode_t mode)28 int shm_open(const char *name, int flag, mode_t mode)
29 {
30 int cs;
31 char buf[NAME_MAX+10];
32 unsupported_api(__FUNCTION__);
33 if (!(name = __shm_mapname(name, buf))) return -1;
34 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
35 int fd = open(name, flag|O_NOFOLLOW|O_CLOEXEC|O_NONBLOCK, mode);
36 pthread_setcancelstate(cs, 0);
37 return fd;
38 }
39
shm_unlink(const char * name)40 int shm_unlink(const char *name)
41 {
42 char buf[NAME_MAX+10];
43 unsupported_api(__FUNCTION__);
44 if (!(name = __shm_mapname(name, buf))) return -1;
45 return unlink(name);
46 }
47