clean up, bugfixes, and general improvement for shm_open/shm_unlink
1. don't make non-cloexec file descriptors 2. cancellation safety (cleanup handlers were missing, now unneeded) 3. share name validation/mapping code between open/unlink functions 4. avoid wasteful/slow syscalls
This commit is contained in:
parent
2e3648b85d
commit
6e2372a86c
2 changed files with 31 additions and 33 deletions
|
|
@ -3,19 +3,38 @@
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.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)
|
int shm_open(const char *name, int flag, mode_t mode)
|
||||||
{
|
{
|
||||||
int fd, dir;
|
char buf[NAME_MAX+10];
|
||||||
|
if (!(name = mapname(name, buf))) return -1;
|
||||||
while (*name == '/') name++;
|
return open(name, flag|O_NOFOLLOW|O_CLOEXEC|O_NONBLOCK, mode);
|
||||||
if (strchr(name, '/')) {
|
}
|
||||||
errno = EINVAL;
|
|
||||||
return -1;
|
int shm_unlink(const char *name)
|
||||||
}
|
{
|
||||||
|
char buf[NAME_MAX+10];
|
||||||
if ((dir = open("/dev/shm", O_DIRECTORY|O_RDONLY)) < 0) return -1;
|
if (!(name = mapname(name, buf))) return -1;
|
||||||
fd = openat(dir, name, flag|O_NOFOLLOW|O_CLOEXEC|O_NONBLOCK, mode);
|
return unlink(name);
|
||||||
close(dir);
|
|
||||||
return fd;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,21 +0,0 @@
|
||||||
#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