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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#pragma weak sem_open = _sem_open
#pragma weak sem_close = _sem_close
#pragma weak sem_unlink = _sem_unlink
#pragma weak sem_init = _sem_init
#pragma weak sem_destroy = _sem_destroy
#pragma weak sem_wait = _sem_wait
#pragma weak sem_timedwait = _sem_timedwait
#pragma weak sem_reltimedwait_np = _sem_reltimedwait_np
#pragma weak sem_trywait = _sem_trywait
#pragma weak sem_post = _sem_post
#pragma weak sem_getvalue = _sem_getvalue
#include "c_synonyms.h"
#include <sys/types.h>
#include <semaphore.h>
#include <synch.h>
#include <errno.h>
#include <stdarg.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <thread.h>
#include "pos4obj.h"
#include "pos4.h"
typedef struct semaddr {
struct semaddr *sad_next; /* next in the link */
char sad_name[PATH_MAX + 1]; /* name of sem object */
sem_t *sad_addr; /* mmapped address of semaphore */
ino64_t sad_inode; /* inode # of the mmapped file */
} semaddr_t;
static semaddr_t *semheadp = NULL;
static mutex_t semlock = DEFAULTMUTEX;
sem_t *
_sem_open(const char *path, int oflag, /* mode_t mode, int value */ ...)
{
va_list ap;
mode_t crmode = 0;
sem_t *sem = NULL;
struct stat64 statbuf;
semaddr_t *next = NULL;
int fd = 0;
int error = 0;
int cr_flag = 0;
uint_t value = 0;
if (__pos4obj_check(path) == -1)
return (SEM_FAILED);
/* acquire semaphore lock to have atomic operation */
if (__pos4obj_lock(path, SEM_LOCK_TYPE) < 0)
return (SEM_FAILED);
/* modify oflag to have RDWR and filter CREATE mode only */
oflag = (oflag & (O_CREAT|O_EXCL)) | (O_RDWR);
if ((oflag & O_CREAT) != 0) {
va_start(ap, oflag);
crmode = va_arg(ap, mode_t);
value = va_arg(ap, uint_t);
va_end(ap);
/* check value < the max for a named semaphore */
if ((_lsemvaluemax == -1L) ||
((unsigned long)value > (unsigned long)_lsemvaluemax)) {
errno = EINVAL;
goto out;
}
}
errno = 0;
if ((fd = __pos4obj_open(path, SEM_DATA_TYPE,
oflag, crmode, &cr_flag)) < 0)
goto out;
if (cr_flag)
cr_flag = DFILE_CREATE | DFILE_OPEN;
else
cr_flag = DFILE_OPEN;
/* find out inode # for the opened file */
if (fstat64(fd, &statbuf) < 0)
goto out;
/* if created, acquire total_size in the file */
if ((cr_flag & DFILE_CREATE) != 0) {
if (ftruncate64(fd, (off64_t)sizeof (sem_t)) < 0)
goto out;
} else {
(void) mutex_lock(&semlock);
/*
* if this semaphore has already been opened, inode
* will indicate then return the same semaphore address
*/
for (next = semheadp; next != NULL; next = next->sad_next) {
if (statbuf.st_ino == next->sad_inode &&
strcmp(path, next->sad_name) == 0) {
(void) __close_nc(fd);
(void) mutex_unlock(&semlock);
(void) __pos4obj_unlock(path, SEM_LOCK_TYPE);
return ((sem_t *)next->sad_addr);
}
}
(void) mutex_unlock(&semlock);
}
/* new sem descriptor to be allocated and new address to be mapped */
if ((next = (semaddr_t *)malloc(sizeof (semaddr_t))) == NULL) {
errno = ENOMEM;
goto out;
}
cr_flag |= ALLOC_MEM;
/* LINTED */
sem = (sem_t *)mmap64(NULL, sizeof (sem_t), PROT_READ|PROT_WRITE,
MAP_SHARED, fd, (off64_t)0);
(void) __close_nc(fd);
cr_flag &= ~DFILE_OPEN;
if (sem == MAP_FAILED)
goto out;
cr_flag |= DFILE_MMAP;
/* add to the list pointed by semheadp */
next->sad_next = semheadp;
semheadp = next;
next->sad_addr = sem;
next->sad_inode = statbuf.st_ino;
(void) strcpy(next->sad_name, path);
/* initialize it by jumping through the jump table */
if ((cr_flag & DFILE_CREATE) != 0) {
if ((error = sema_init((sema_t *)sem, value, USYNC_PROCESS, 0))
!= 0) {
errno = error;
goto out;
}
}
if (__pos4obj_unlock(path, SEM_LOCK_TYPE) < 0)
return (SEM_FAILED);
return (sem);
out:
error = errno;
if ((cr_flag & DFILE_OPEN) != 0)
(void) __close_nc(fd);
if ((cr_flag & DFILE_CREATE) != 0)
(void) __pos4obj_unlink(path, SEM_DATA_TYPE);
if ((cr_flag & ALLOC_MEM) != 0)
free((caddr_t)next);
if ((cr_flag & DFILE_MMAP) != 0)
(void) munmap((caddr_t)sem, sizeof (sem_t));
errno = error;
(void) __pos4obj_unlock(path, SEM_LOCK_TYPE);
return (SEM_FAILED);
}
int
_sem_close(sem_t *sem)
{
semaddr_t **next;
semaddr_t *freeit;
(void) mutex_lock(&semlock);
for (next = &semheadp; (freeit = *next) != NULL;
next = &(freeit->sad_next)) {
if (freeit->sad_addr == sem) {
*next = freeit->sad_next;
free((caddr_t)freeit);
(void) mutex_unlock(&semlock);
return (munmap((caddr_t)sem, sizeof (sem_t)));
}
}
(void) mutex_unlock(&semlock);
errno = EINVAL;
return (-1);
}
int
_sem_unlink(const char *path)
{
int error;
int oerrno;
if (__pos4obj_check(path) < 0)
return (-1);
if (__pos4obj_lock(path, SEM_LOCK_TYPE) < 0)
return (-1);
error = __pos4obj_unlink(path, SEM_DATA_TYPE);
oerrno = errno;
(void) __pos4obj_unlock(path, SEM_LOCK_TYPE);
errno = oerrno;
return (error);
}
/*
* SUSV3 requires ("shall fail") an EINVAL failure for operations
* on invalid semaphores, including uninitialized unnamed semaphores.
* The best we can do is check that the magic number is correct.
* This is not perfect, but it allows the test suite to pass.
* (Standards bodies are filled with fools and idiots.)
*/
static int
sem_invalid(sem_t *sem)
{
if (sem->sem_magic != SEMA_MAGIC) {
errno = EINVAL;
return (-1);
}
return (0);
}
int
_sem_init(sem_t *sem, int pshared, uint_t value)
{
int error;
if ((error = sema_init((sema_t *)sem, value,
pshared ? USYNC_PROCESS : USYNC_THREAD, NULL)) != 0) {
errno = error;
return (-1);
}
return (0);
}
int
_sem_destroy(sem_t *sem)
{
int error;
if (sem_invalid(sem))
return (-1);
if ((error = sema_destroy((sema_t *)sem)) != 0) {
errno = error;
return (-1);
}
return (0);
}
int
_sem_post(sem_t *sem)
{
int error;
if (sem_invalid(sem))
return (-1);
if ((error = sema_post((sema_t *)sem)) != 0) {
errno = error;
return (-1);
}
return (0);
}
int
_sem_wait(sem_t *sem)
{
int error;
if (sem_invalid(sem))
return (-1);
if ((error = sema_wait((sema_t *)sem)) != 0) {
errno = error;
return (-1);
}
return (0);
}
int
_sem_timedwait(sem_t *sem, const timespec_t *abstime)
{
int error;
if (sem_invalid(sem))
return (-1);
if ((error = sema_timedwait((sema_t *)sem, abstime)) != 0) {
if (error == ETIME)
error = ETIMEDOUT;
errno = error;
return (-1);
}
return (0);
}
int
_sem_reltimedwait_np(sem_t *sem, const timespec_t *reltime)
{
int error;
if (sem_invalid(sem))
return (-1);
if ((error = sema_reltimedwait((sema_t *)sem, reltime)) != 0) {
if (error == ETIME)
error = ETIMEDOUT;
errno = error;
return (-1);
}
return (0);
}
int
_sem_trywait(sem_t *sem)
{
int error;
if (sem_invalid(sem))
return (-1);
if ((error = sema_trywait((sema_t *)sem)) != 0) {
if (error == EBUSY)
error = EAGAIN;
errno = error;
return (-1);
}
return (0);
}
int
_sem_getvalue(sem_t *sem, int *sval)
{
if (sem_invalid(sem))
return (-1);
*sval = (int)sem->sem_count;
return (0);
}
|