summaryrefslogtreecommitdiff
path: root/src/thread/sem_open.c
blob: 809925d25a7a38834dc7bde773611507d809b930 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#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>
#include <stdlib.h>
#include <pthread.h>

static struct {
	ino_t ino;
	sem_t *sem;
	int refcnt;
} *semtab;

static int semcnt;
static pthread_spinlock_t lock;
static pthread_once_t once;

static void init()
{
	semtab = calloc(sizeof *semtab, SEM_NSEMS_MAX);
}

static sem_t *find_map(ino_t ino)
{
	int i;
	for (i=0; i<SEM_NSEMS_MAX && semtab[i].ino != ino; i++);
	if (i==SEM_NSEMS_MAX) return 0;
	if (semtab[i].refcnt == INT_MAX) return (sem_t *)-1;
	semtab[i].refcnt++;
	return semtab[i].sem;
}

sem_t *sem_open(const char *name, int flags, ...)
{
	va_list ap;
	mode_t mode;
	unsigned value;
	int fd, tfd, dir;
	sem_t newsem;
	void *map;
	char tmp[64];
	struct timespec ts;
	struct stat st;
	int i;

	while (*name=='/') name++;
	if (strchr(name, '/')) {
		errno = EINVAL;
		return SEM_FAILED;
	}

	pthread_once(&once, init);
	if (!semtab) {
		errno = ENOMEM;
		return SEM_FAILED;
	}

	if (flags & O_CREAT) {
		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;
			return SEM_FAILED;
		}
		sem_init(&newsem, 0, value);
		clock_gettime(CLOCK_REALTIME, &ts);
		snprintf(tmp, sizeof(tmp), "/dev/shm/%p-%p-%d-%d",
			&name, name, (int)getpid(), (int)ts.tv_nsec);
		tfd = open(tmp, O_CREAT|O_EXCL|O_RDWR, mode);
		if (tfd<0) return SEM_FAILED;
		dir = open("/dev/shm", O_DIRECTORY|O_RDONLY);
		if (dir<0 || write(tfd,&newsem,sizeof newsem)!=sizeof newsem) {
			if (dir >= 0) close(dir);
			close(tfd);
			unlink(tmp);
			return SEM_FAILED;
		}
	}

	flags &= ~O_ACCMODE;
	flags |= O_RDWR;

	pthread_spin_lock(&lock);

	for (;;) {
		if (!(flags & O_EXCL)) {
			fd = shm_open(name, flags&~O_CREAT, mode);
			if (fd >= 0 || errno != ENOENT) {
				if (flags & O_CREAT) {
					close(dir);
					close(tfd);
					unlink(tmp);
				}
				if (fd >= 0 && fstat(fd, &st) < 0) {
					close(fd);
					fd = -1;
				}
				if (fd < 0) {
					pthread_spin_unlock(&lock);
					return SEM_FAILED;
				}
				if ((map = find_map(st.st_ino))) {
					pthread_spin_unlock(&lock);
					close(fd);
					if (map == (sem_t *)-1)
						return SEM_FAILED;
					return map;
				}
				break;
			}
		}
		if (!(flags & O_CREAT)) {
			pthread_spin_unlock(&lock);
			return SEM_FAILED;
		}
		if (!linkat(AT_FDCWD, tmp, dir, name, 0)) {
			fd = tfd;
			close(dir);
			unlink(tmp);
			break;
		}
		if ((flags & O_EXCL) || errno != EEXIST) {
			close(dir);
			close(tfd);
			unlink(tmp);
			return SEM_FAILED;
		}
	}
	if (fstat(fd, &st) < 0) {
		pthread_spin_unlock(&lock);
		close(fd);
		return SEM_FAILED;
	}
	if (semcnt == SEM_NSEMS_MAX) {
		pthread_spin_unlock(&lock);
		close(fd);
		errno = EMFILE;
		return SEM_FAILED;
	}
	for (i=0; i<SEM_NSEMS_MAX && semtab[i].sem; i++);
	map = mmap(0, sizeof(sem_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
	close(fd);
	if (map == MAP_FAILED) {
		pthread_spin_unlock(&lock);
		return SEM_FAILED;
	}
	semtab[i].ino = st.st_ino;
	semtab[i].sem = map;
	semtab[i].refcnt = 1;
	pthread_spin_unlock(&lock);
	return map;
}

int sem_close(sem_t *sem)
{
	int i;
	pthread_spin_lock(&lock);
	for (i=0; i<SEM_NSEMS_MAX && semtab[i].sem != sem; i++);
	if (!--semtab[i].refcnt) {
		semtab[i].sem = 0;
		semtab[i].ino = 0;
	}
	pthread_spin_unlock(&lock);
	return munmap(sem, sizeof *sem);
}