#include #include #include #include #include #include #include char *__shm_mapname(const char *name, char *buf) { const char *s=name, *e; while (*s == '/') s++; if (*(e = __strchrnul(s, '/')) || e==s || (e-s <= 2 && s[0]=='.' && e[-1]=='.')) { errno = EINVAL; return 0; } if (e-name > NAME_MAX) { errno = ENAMETOOLONG; return 0; } memcpy(buf, "/dev/shm/", 9); memcpy(buf+9, s, e-s+1); return buf; } int shm_open(const char *name, int flag, mode_t mode) { int cs; char buf[NAME_MAX+10]; if (!(name = __shm_mapname(name, buf))) return -1; pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); int fd = open(name, flag|O_NOFOLLOW|O_CLOEXEC|O_NONBLOCK, mode); pthread_setcancelstate(cs, 0); return fd; } int shm_unlink(const char *name) { char buf[NAME_MAX+10]; if (!(name = __shm_mapname(name, buf))) return -1; return unlink(name); }