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