sem_open.c 4.1 KB
Newer Older
R
Rich Felker 已提交
1 2 3 4 5 6 7 8 9 10 11
#include <semaphore.h>
#include <sys/mman.h>
#include <limits.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <time.h>
#include <stdio.h>
#include <sys/stat.h>
12 13
#include <stdlib.h>
#include <pthread.h>
14
#include "lock.h"
R
Rich Felker 已提交
15

16 17 18 19 20
static struct {
	ino_t ino;
	sem_t *sem;
	int refcnt;
} *semtab;
21
static volatile int lock[1];
22

R
Rich Felker 已提交
23
#define FLAGS (O_RDWR|O_NOFOLLOW|O_CLOEXEC|O_NONBLOCK)
R
Rich Felker 已提交
24 25 26 27 28 29

sem_t *sem_open(const char *name, int flags, ...)
{
	va_list ap;
	mode_t mode;
	unsigned value;
30
	int fd, i, e, slot, first=1, cnt, cs;
R
Rich Felker 已提交
31 32 33 34
	sem_t newsem;
	void *map;
	char tmp[64];
	struct timespec ts;
35
	struct stat st;
R
Rich Felker 已提交
36
	char buf[NAME_MAX+10];
R
Rich Felker 已提交
37

R
Rich Felker 已提交
38
	if (!(name = __shm_mapname(name, buf)))
R
Rich Felker 已提交
39 40
		return SEM_FAILED;

R
Rich Felker 已提交
41 42 43 44
	LOCK(lock);
	/* Allocate table if we don't have one yet */
	if (!semtab && !(semtab = calloc(sizeof *semtab, SEM_NSEMS_MAX))) {
		UNLOCK(lock);
45 46 47
		return SEM_FAILED;
	}

R
Rich Felker 已提交
48 49 50 51 52 53 54
	/* Reserve a slot in case this semaphore is not mapped yet;
	 * this is necessary because there is no way to handle
	 * failures after creation of the file. */
	slot = -1;
	for (cnt=i=0; i<SEM_NSEMS_MAX; i++) {
		cnt += semtab[i].refcnt;
		if (!semtab[i].sem && slot < 0) slot = i;
R
Rich Felker 已提交
55
	}
R
Rich Felker 已提交
56 57 58 59 60 61 62 63 64
	/* Avoid possibility of overflow later */
	if (cnt == INT_MAX || slot < 0) {
		errno = EMFILE;
		UNLOCK(lock);
		return SEM_FAILED;
	}
	/* Dummy pointer to make a reservation */
	semtab[slot].sem = (sem_t *)-1;
	UNLOCK(lock);
R
Rich Felker 已提交
65

R
Rich Felker 已提交
66
	flags &= (O_CREAT|O_EXCL);
R
Rich Felker 已提交
67

68 69
	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);

R
Rich Felker 已提交
70 71 72 73
	/* Early failure check for exclusive open; otherwise the case
	 * where the semaphore already exists is expensive. */
	if (flags == (O_CREAT|O_EXCL) && access(name, F_OK) == 0) {
		errno = EEXIST;
74
		goto fail;
R
Rich Felker 已提交
75
	}
76

R
Rich Felker 已提交
77
	for (;;) {
R
Rich Felker 已提交
78 79 80 81 82
		/* If exclusive mode is not requested, try opening an
		 * existing file first and fall back to creation. */
		if (flags != (O_CREAT|O_EXCL)) {
			fd = open(name, FLAGS);
			if (fd >= 0) {
83 84
				if (fstat(fd, &st) < 0 ||
				    (map = mmap(0, sizeof(sem_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) {
85
					close(fd);
86
					goto fail;
87
				}
88
				close(fd);
R
Rich Felker 已提交
89 90
				break;
			}
R
Rich Felker 已提交
91
			if (errno != ENOENT)
92
				goto fail;
R
Rich Felker 已提交
93
		}
R
Rich Felker 已提交
94
		if (!(flags & O_CREAT))
95
			goto fail;
R
Rich Felker 已提交
96 97 98 99 100 101 102 103
		if (first) {
			first = 0;
			va_start(ap, flags);
			mode = va_arg(ap, mode_t) & 0666;
			value = va_arg(ap, unsigned);
			va_end(ap);
			if (value > SEM_VALUE_MAX) {
				errno = EINVAL;
104
				goto fail;
R
Rich Felker 已提交
105 106
			}
			sem_init(&newsem, 1, value);
107
		}
R
Rich Felker 已提交
108 109 110 111 112 113 114
		/* Create a temp file with the new semaphore contents
		 * and attempt to atomically link it as the new name */
		clock_gettime(CLOCK_REALTIME, &ts);
		snprintf(tmp, sizeof(tmp), "/dev/shm/tmp-%d", (int)ts.tv_nsec);
		fd = open(tmp, O_CREAT|O_EXCL|FLAGS, mode);
		if (fd < 0) {
			if (errno == EEXIST) continue;
115
			goto fail;
R
Rich Felker 已提交
116
		}
R
Rich Felker 已提交
117 118 119
		if (write(fd, &newsem, sizeof newsem) != sizeof newsem || fstat(fd, &st) < 0 ||
		    (map = mmap(0, sizeof(sem_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) {
			close(fd);
R
Rich Felker 已提交
120
			unlink(tmp);
121
			goto fail;
R
Rich Felker 已提交
122
		}
123
		close(fd);
124
		e = link(tmp, name) ? errno : 0;
R
Rich Felker 已提交
125
		unlink(tmp);
126
		if (!e) break;
127
		munmap(map, sizeof(sem_t));
R
Rich Felker 已提交
128 129 130 131
		/* Failure is only fatal when doing an exclusive open;
		 * otherwise, next iteration will try to open the
		 * existing file. */
		if (e != EEXIST || flags == (O_CREAT|O_EXCL))
132
			goto fail;
133
	}
R
Rich Felker 已提交
134 135 136 137 138 139 140 141

	/* See if the newly mapped semaphore is already mapped. If
	 * so, unmap the new mapping and use the existing one. Otherwise,
	 * add it to the table of mapped semaphores. */
	LOCK(lock);
	for (i=0; i<SEM_NSEMS_MAX && semtab[i].ino != st.st_ino; i++);
	if (i<SEM_NSEMS_MAX) {
		munmap(map, sizeof(sem_t));
142 143 144
		semtab[slot].sem = 0;
		slot = i;
		map = semtab[i].sem;
145
	}
146
	semtab[slot].refcnt++;
R
Rich Felker 已提交
147 148 149
	semtab[slot].sem = map;
	semtab[slot].ino = st.st_ino;
	UNLOCK(lock);
150
	pthread_setcancelstate(cs, 0);
R
Rich Felker 已提交
151
	return map;
152 153 154

fail:
	pthread_setcancelstate(cs, 0);
155 156 157
	LOCK(lock);
	semtab[slot].sem = 0;
	UNLOCK(lock);
158
	return SEM_FAILED;
R
Rich Felker 已提交
159
}
160 161 162 163

int sem_close(sem_t *sem)
{
	int i;
R
Rich Felker 已提交
164
	LOCK(lock);
165
	for (i=0; i<SEM_NSEMS_MAX && semtab[i].sem != sem; i++);
166 167 168
	if (--semtab[i].refcnt) {
		UNLOCK(lock);
		return 0;
169
	}
170 171
	semtab[i].sem = 0;
	semtab[i].ino = 0;
R
Rich Felker 已提交
172 173 174
	UNLOCK(lock);
	munmap(sem, sizeof *sem);
	return 0;
175
}