08f98c25a3fa270be7235df68a0b1444905e7eb2
[musl] / src / thread / sem_open.c
1 #include <semaphore.h>
2 #include <sys/mman.h>
3 #include <limits.h>
4 #include <fcntl.h>
5 #include <unistd.h>
6 #include <string.h>
7 #include <stdarg.h>
8 #include <errno.h>
9 #include <time.h>
10 #include <stdio.h>
11 #include <sys/stat.h>
12 #include <stdlib.h>
13 #include <pthread.h>
14 #include "libc.h"
15
16 char *__shm_mapname(const char *, char *);
17
18 static struct {
19         ino_t ino;
20         sem_t *sem;
21         int refcnt;
22 } *semtab;
23 static int lock[2];
24
25 #define FLAGS (O_RDWR|O_NOFOLLOW|O_CLOEXEC|O_NONBLOCK)
26
27 sem_t *sem_open(const char *name, int flags, ...)
28 {
29         va_list ap;
30         mode_t mode;
31         unsigned value;
32         int fd, i, e, slot, first=1, cnt;
33         sem_t newsem;
34         void *map;
35         char tmp[64];
36         struct timespec ts;
37         struct stat st;
38         char buf[NAME_MAX+10];
39
40         if (!(name = __shm_mapname(name, buf)))
41                 return SEM_FAILED;
42
43         LOCK(lock);
44         /* Allocate table if we don't have one yet */
45         if (!semtab && !(semtab = calloc(sizeof *semtab, SEM_NSEMS_MAX))) {
46                 UNLOCK(lock);
47                 return SEM_FAILED;
48         }
49
50         /* Reserve a slot in case this semaphore is not mapped yet;
51          * this is necessary because there is no way to handle
52          * failures after creation of the file. */
53         slot = -1;
54         for (cnt=i=0; i<SEM_NSEMS_MAX; i++) {
55                 cnt += semtab[i].refcnt;
56                 if (!semtab[i].sem && slot < 0) slot = i;
57         }
58         /* Avoid possibility of overflow later */
59         if (cnt == INT_MAX || slot < 0) {
60                 errno = EMFILE;
61                 UNLOCK(lock);
62                 return SEM_FAILED;
63         }
64         /* Dummy pointer to make a reservation */
65         semtab[slot].sem = (sem_t *)-1;
66         UNLOCK(lock);
67
68         flags &= (O_CREAT|O_EXCL);
69
70         /* Early failure check for exclusive open; otherwise the case
71          * where the semaphore already exists is expensive. */
72         if (flags == (O_CREAT|O_EXCL) && access(name, F_OK) == 0) {
73                 errno = EEXIST;
74                 return SEM_FAILED;
75         }
76
77         for (;;) {
78                 /* If exclusive mode is not requested, try opening an
79                  * existing file first and fall back to creation. */
80                 if (flags != (O_CREAT|O_EXCL)) {
81                         fd = open(name, FLAGS);
82                         if (fd >= 0) {
83                                 if ((map = mmap(0, sizeof(sem_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED ||
84                                     fstat(fd, &st) < 0) {
85                                         close(fd);
86                                         return SEM_FAILED;
87                                 }
88                                 close(fd);
89                                 break;
90                         }
91                         if (errno != ENOENT)
92                                 return SEM_FAILED;
93                 }
94                 if (!(flags & O_CREAT))
95                         return SEM_FAILED;
96                 if (first) {
97                         first = 0;
98                         va_start(ap, flags);
99                         mode = va_arg(ap, mode_t) & 0666;
100                         value = va_arg(ap, unsigned);
101                         va_end(ap);
102                         if (value > SEM_VALUE_MAX) {
103                                 errno = EINVAL;
104                                 return SEM_FAILED;
105                         }
106                         sem_init(&newsem, 1, value);
107                 }
108                 /* Create a temp file with the new semaphore contents
109                  * and attempt to atomically link it as the new name */
110                 clock_gettime(CLOCK_REALTIME, &ts);
111                 snprintf(tmp, sizeof(tmp), "/dev/shm/tmp-%d", (int)ts.tv_nsec);
112                 fd = open(tmp, O_CREAT|O_EXCL|FLAGS, mode);
113                 if (fd < 0) {
114                         if (errno == EEXIST) continue;
115                         return SEM_FAILED;
116                 }
117                 if (write(fd, &newsem, sizeof newsem) != sizeof newsem || fstat(fd, &st) < 0 ||
118                     (map = mmap(0, sizeof(sem_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) {
119                         close(fd);
120                         unlink(tmp);
121                         return SEM_FAILED;
122                 }
123                 close(fd);
124                 if (link(tmp, name) == 0) break;
125                 e = errno;
126                 unlink(tmp);
127                 /* Failure is only fatal when doing an exclusive open;
128                  * otherwise, next iteration will try to open the
129                  * existing file. */
130                 if (e != EEXIST || flags == (O_CREAT|O_EXCL))
131                         return SEM_FAILED;
132         }
133
134         /* See if the newly mapped semaphore is already mapped. If
135          * so, unmap the new mapping and use the existing one. Otherwise,
136          * add it to the table of mapped semaphores. */
137         LOCK(lock);
138         for (i=0; i<SEM_NSEMS_MAX && semtab[i].ino != st.st_ino; i++);
139         if (i<SEM_NSEMS_MAX) {
140                 munmap(map, sizeof(sem_t));
141                 semtab[i].refcnt++;
142                 UNLOCK(lock);
143                 return semtab[i].sem;
144         }
145         semtab[slot].refcnt = 1;
146         semtab[slot].sem = map;
147         semtab[slot].ino = st.st_ino;
148         UNLOCK(lock);
149
150         return map;
151 }
152
153 int sem_close(sem_t *sem)
154 {
155         int i;
156         LOCK(lock);
157         for (i=0; i<SEM_NSEMS_MAX && semtab[i].sem != sem; i++);
158         if (!--semtab[i].refcnt) {
159                 semtab[i].sem = 0;
160                 semtab[i].ino = 0;
161         }
162         UNLOCK(lock);
163         munmap(sem, sizeof *sem);
164         return 0;
165 }