]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
2d21ac55 | 2 | * Copyright (c) 2000-2007 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * Copyright (c) 1990, 1996-1998 Apple Computer, Inc. | |
30 | * All Rights Reserved. | |
31 | */ | |
32 | /* | |
6d2010ae | 33 | * posix_sem.c : Support for POSIX semaphore APIs |
1c79356b A |
34 | * |
35 | * File: posix_sem.c | |
36 | * Author: Ananthakrishna Ramesh | |
37 | * | |
38 | * HISTORY | |
39 | * 2-Sep-1999 A.Ramesh | |
40 | * Created for MacOSX | |
41 | * | |
42 | */ | |
2d21ac55 A |
43 | /* |
44 | * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce | |
45 | * support for mandatory and extensible security protections. This notice | |
46 | * is included in support of clause 2.2 (b) of the Apple Public License, | |
47 | * Version 2.0. | |
48 | */ | |
1c79356b A |
49 | |
50 | #include <sys/cdefs.h> | |
51 | #include <sys/param.h> | |
52 | #include <sys/systm.h> | |
53 | #include <sys/kernel.h> | |
91447636 | 54 | #include <sys/file_internal.h> |
1c79356b A |
55 | #include <sys/filedesc.h> |
56 | #include <sys/stat.h> | |
91447636 A |
57 | #include <sys/proc_internal.h> |
58 | #include <sys/kauth.h> | |
1c79356b A |
59 | #include <sys/mount.h> |
60 | #include <sys/namei.h> | |
61 | #include <sys/vnode.h> | |
62 | #include <sys/ioctl.h> | |
63 | #include <sys/tty.h> | |
64 | #include <sys/malloc.h> | |
65 | #include <sys/semaphore.h> | |
91447636 | 66 | #include <sys/sysproto.h> |
0c530ab8 | 67 | #include <sys/proc_info.h> |
e5568f75 | 68 | |
2d21ac55 A |
69 | #if CONFIG_MACF |
70 | #include <sys/vnode_internal.h> | |
71 | #include <security/mac_framework.h> | |
72 | #endif | |
73 | ||
b0d623f7 | 74 | #include <security/audit/audit.h> |
e5568f75 | 75 | |
1c79356b A |
76 | #include <mach/mach_types.h> |
77 | #include <mach/vm_prot.h> | |
78 | #include <mach/semaphore.h> | |
79 | #include <mach/sync_policy.h> | |
91447636 A |
80 | #include <mach/task.h> |
81 | #include <kern/kern_types.h> | |
1c79356b A |
82 | #include <kern/task.h> |
83 | #include <kern/clock.h> | |
84 | #include <mach/kern_return.h> | |
85 | ||
91447636 A |
86 | |
87 | #define f_flag f_fglob->fg_flag | |
39236c6e | 88 | #define f_type f_fglob->fg_ops->fo_type |
91447636 A |
89 | #define f_msgcount f_fglob->fg_msgcount |
90 | #define f_cred f_fglob->fg_cred | |
91 | #define f_ops f_fglob->fg_ops | |
92 | #define f_offset f_fglob->fg_offset | |
93 | #define f_data f_fglob->fg_data | |
1c79356b A |
94 | #define PSEMNAMLEN 31 /* maximum name segment length we bother with */ |
95 | ||
96 | struct pseminfo { | |
97 | unsigned int psem_flags; | |
98 | unsigned int psem_usecount; | |
99 | mode_t psem_mode; | |
100 | uid_t psem_uid; | |
101 | gid_t psem_gid; | |
102 | char psem_name[PSEMNAMLEN + 1]; /* segment name */ | |
91447636 | 103 | semaphore_t psem_semobject; |
2d21ac55 | 104 | struct label * psem_label; |
39236c6e A |
105 | pid_t psem_creator_pid; |
106 | uint64_t psem_creator_uniqueid; | |
1c79356b A |
107 | }; |
108 | #define PSEMINFO_NULL (struct pseminfo *)0 | |
109 | ||
110 | #define PSEM_NONE 1 | |
111 | #define PSEM_DEFINED 2 | |
112 | #define PSEM_ALLOCATED 4 | |
113 | #define PSEM_MAPPED 8 | |
114 | #define PSEM_INUSE 0x10 | |
115 | #define PSEM_REMOVED 0x20 | |
116 | #define PSEM_INCREATE 0x40 | |
117 | #define PSEM_INDELETE 0x80 | |
118 | ||
119 | struct psemcache { | |
120 | LIST_ENTRY(psemcache) psem_hash; /* hash chain */ | |
121 | struct pseminfo *pseminfo; /* vnode the name refers to */ | |
122 | int psem_nlen; /* length of name */ | |
123 | char psem_name[PSEMNAMLEN + 1]; /* segment name */ | |
124 | }; | |
125 | #define PSEMCACHE_NULL (struct psemcache *)0 | |
126 | ||
490019cf A |
127 | #define PSEMCACHE_NOTFOUND (0) |
128 | #define PSEMCACHE_FOUND (-1) | |
129 | #define PSEMCACHE_NEGATIVE (ENOENT) | |
130 | ||
1c79356b A |
131 | struct psemstats { |
132 | long goodhits; /* hits that we can really use */ | |
133 | long neghits; /* negative hits that we can use */ | |
134 | long badhits; /* hits we must drop */ | |
135 | long falsehits; /* hits with id mismatch */ | |
136 | long miss; /* misses */ | |
137 | long longnames; /* long names that ignore cache */ | |
138 | }; | |
139 | ||
140 | struct psemname { | |
141 | char *psem_nameptr; /* pointer to looked up name */ | |
142 | long psem_namelen; /* length of looked up component */ | |
b0d623f7 | 143 | u_int32_t psem_hash; /* hash value of looked up name */ |
1c79356b A |
144 | }; |
145 | ||
146 | struct psemnode { | |
147 | struct pseminfo *pinfo; | |
148 | #if DIAGNOSTIC | |
149 | unsigned int readcnt; | |
150 | unsigned int writecnt; | |
151 | #endif | |
152 | }; | |
153 | #define PSEMNODE_NULL (struct psemnode *)0 | |
154 | ||
155 | ||
156 | #define PSEMHASH(pnp) \ | |
157 | (&psemhashtbl[(pnp)->psem_hash & psemhash]) | |
158 | LIST_HEAD(psemhashhead, psemcache) *psemhashtbl; /* Hash Table */ | |
159 | u_long psemhash; /* size of hash table - 1 */ | |
160 | long psemnument; /* number of cache entries allocated */ | |
91447636 A |
161 | long posix_sem_max = 10000; /* tunable for max POSIX semaphores */ |
162 | /* 10000 limits to ~1M of memory */ | |
6d2010ae A |
163 | SYSCTL_NODE(_kern, KERN_POSIX, posix, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "Posix"); |
164 | SYSCTL_NODE(_kern_posix, OID_AUTO, sem, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "Semaphores"); | |
165 | SYSCTL_LONG (_kern_posix_sem, OID_AUTO, max, CTLFLAG_RW | CTLFLAG_LOCKED, &posix_sem_max, "max"); | |
91447636 | 166 | |
1c79356b A |
167 | struct psemstats psemstats; /* cache effectiveness statistics */ |
168 | ||
91447636 A |
169 | static int psem_access(struct pseminfo *pinfo, int mode, kauth_cred_t cred); |
170 | static int psem_cache_search(struct pseminfo **, | |
5ba3f43e | 171 | struct psemname *, struct psemcache **); |
91447636 | 172 | static int psem_delete(struct pseminfo * pinfo); |
1c79356b | 173 | |
91447636 | 174 | static int psem_read (struct fileproc *fp, struct uio *uio, |
5ba3f43e | 175 | int flags, vfs_context_t ctx); |
91447636 | 176 | static int psem_write (struct fileproc *fp, struct uio *uio, |
5ba3f43e | 177 | int flags, vfs_context_t ctx); |
91447636 | 178 | static int psem_ioctl (struct fileproc *fp, u_long com, |
5ba3f43e | 179 | caddr_t data, vfs_context_t ctx); |
2d21ac55 A |
180 | static int psem_select (struct fileproc *fp, int which, void *wql, vfs_context_t ctx); |
181 | static int psem_closefile (struct fileglob *fp, vfs_context_t ctx); | |
490019cf | 182 | static int psem_unlink_internal(struct pseminfo *pinfo, struct psemcache *pcache); |
1c79356b | 183 | |
5ba3f43e A |
184 | static int psem_kqfilter (struct fileproc *fp, struct knote *kn, |
185 | struct kevent_internal_s *kev, vfs_context_t ctx); | |
55e303ae | 186 | |
39236c6e | 187 | static const struct fileops psemops = { |
39037602 A |
188 | .fo_type = DTYPE_PSXSEM, |
189 | .fo_read = psem_read, | |
190 | .fo_write = psem_write, | |
191 | .fo_ioctl = psem_ioctl, | |
192 | .fo_select = psem_select, | |
193 | .fo_close = psem_closefile, | |
194 | .fo_kqfilter = psem_kqfilter, | |
195 | .fo_drain = NULL, | |
39236c6e | 196 | }; |
91447636 A |
197 | |
198 | static lck_grp_t *psx_sem_subsys_lck_grp; | |
199 | static lck_grp_attr_t *psx_sem_subsys_lck_grp_attr; | |
200 | static lck_attr_t *psx_sem_subsys_lck_attr; | |
201 | static lck_mtx_t psx_sem_subsys_mutex; | |
202 | ||
203 | #define PSEM_SUBSYS_LOCK() lck_mtx_lock(& psx_sem_subsys_mutex) | |
204 | #define PSEM_SUBSYS_UNLOCK() lck_mtx_unlock(& psx_sem_subsys_mutex) | |
39037602 | 205 | #define PSEM_SUBSYS_ASSERT_HELD() LCK_MTX_ASSERT(&psx_sem_subsys_mutex, LCK_MTX_ASSERT_OWNED) |
91447636 A |
206 | |
207 | ||
208 | static int psem_cache_add(struct pseminfo *psemp, struct psemname *pnp, struct psemcache *pcp); | |
490019cf A |
209 | static void psem_cache_delete(struct psemcache *pcp); |
210 | int psem_cache_purge_all(proc_t); | |
211 | ||
212 | ||
91447636 A |
213 | /* Initialize the mutex governing access to the posix sem subsystem */ |
214 | __private_extern__ void | |
215 | psem_lock_init( void ) | |
216 | { | |
217 | ||
218 | psx_sem_subsys_lck_grp_attr = lck_grp_attr_alloc_init(); | |
91447636 A |
219 | |
220 | psx_sem_subsys_lck_grp = lck_grp_alloc_init("posix shared memory", psx_sem_subsys_lck_grp_attr); | |
221 | ||
222 | psx_sem_subsys_lck_attr = lck_attr_alloc_init(); | |
91447636 A |
223 | lck_mtx_init(& psx_sem_subsys_mutex, psx_sem_subsys_lck_grp, psx_sem_subsys_lck_attr); |
224 | } | |
1c79356b A |
225 | |
226 | /* | |
227 | * Lookup an entry in the cache | |
228 | * | |
229 | * | |
230 | * status of -1 is returned if matches | |
231 | * If the lookup determines that the name does not exist | |
232 | * (negative cacheing), a status of ENOENT is returned. If the lookup | |
233 | * fails, a status of zero is returned. | |
234 | */ | |
235 | ||
9bccf70c | 236 | static int |
2d21ac55 A |
237 | psem_cache_search(struct pseminfo **psemp, struct psemname *pnp, |
238 | struct psemcache **pcache) | |
1c79356b | 239 | { |
91447636 A |
240 | struct psemcache *pcp, *nnp; |
241 | struct psemhashhead *pcpp; | |
1c79356b A |
242 | |
243 | if (pnp->psem_namelen > PSEMNAMLEN) { | |
244 | psemstats.longnames++; | |
490019cf | 245 | return PSEMCACHE_NOTFOUND; |
1c79356b A |
246 | } |
247 | ||
248 | pcpp = PSEMHASH(pnp); | |
249 | for (pcp = pcpp->lh_first; pcp != 0; pcp = nnp) { | |
250 | nnp = pcp->psem_hash.le_next; | |
251 | if (pcp->psem_nlen == pnp->psem_namelen && | |
252 | !bcmp(pcp->psem_name, pnp->psem_nameptr, (u_int)pcp-> psem_nlen)) | |
253 | break; | |
254 | } | |
255 | ||
256 | if (pcp == 0) { | |
257 | psemstats.miss++; | |
490019cf | 258 | return PSEMCACHE_NOTFOUND; |
1c79356b A |
259 | } |
260 | ||
261 | /* We found a "positive" match, return the vnode */ | |
262 | if (pcp->pseminfo) { | |
263 | psemstats.goodhits++; | |
264 | /* TOUCH(ncp); */ | |
265 | *psemp = pcp->pseminfo; | |
266 | *pcache = pcp; | |
490019cf | 267 | return PSEMCACHE_FOUND; |
1c79356b A |
268 | } |
269 | ||
270 | /* | |
271 | * We found a "negative" match, ENOENT notifies client of this match. | |
272 | * The nc_vpid field records whether this is a whiteout. | |
273 | */ | |
274 | psemstats.neghits++; | |
490019cf | 275 | return PSEMCACHE_NEGATIVE; |
1c79356b A |
276 | } |
277 | ||
278 | /* | |
279 | * Add an entry to the cache. | |
280 | */ | |
9bccf70c | 281 | static int |
91447636 | 282 | psem_cache_add(struct pseminfo *psemp, struct psemname *pnp, struct psemcache *pcp) |
1c79356b | 283 | { |
91447636 | 284 | struct psemhashhead *pcpp; |
1c79356b A |
285 | struct pseminfo *dpinfo; |
286 | struct psemcache *dpcp; | |
287 | ||
288 | #if DIAGNOSTIC | |
2d21ac55 | 289 | if (pnp->psem_namelen > PSEMNAMLEN) |
1c79356b A |
290 | panic("cache_enter: name too long"); |
291 | #endif | |
292 | ||
91447636 | 293 | |
1c79356b | 294 | /* if the entry has already been added by some one else return */ |
490019cf A |
295 | if (psem_cache_search(&dpinfo, pnp, &dpcp) == PSEMCACHE_FOUND) { |
296 | return EEXIST; | |
1c79356b | 297 | } |
91447636 | 298 | if (psemnument >= posix_sem_max) |
490019cf | 299 | return ENOSPC; |
1c79356b | 300 | psemnument++; |
1c79356b A |
301 | /* |
302 | * Fill in cache info, if vp is NULL this is a "negative" cache entry. | |
303 | * For negative entries, we have to record whether it is a whiteout. | |
304 | * the whiteout flag is stored in the nc_vpid field which is | |
305 | * otherwise unused. | |
306 | */ | |
307 | pcp->pseminfo = psemp; | |
308 | pcp->psem_nlen = pnp->psem_namelen; | |
309 | bcopy(pnp->psem_nameptr, pcp->psem_name, (unsigned)pcp->psem_nlen); | |
310 | pcpp = PSEMHASH(pnp); | |
311 | #if DIAGNOSTIC | |
312 | { | |
91447636 | 313 | struct psemcache *p; |
1c79356b A |
314 | |
315 | for (p = pcpp->lh_first; p != 0; p = p->psem_hash.le_next) | |
316 | if (p == pcp) | |
317 | panic("psem:cache_enter duplicate"); | |
318 | } | |
319 | #endif | |
320 | LIST_INSERT_HEAD(pcpp, pcp, psem_hash); | |
490019cf | 321 | return 0; |
1c79356b A |
322 | } |
323 | ||
324 | /* | |
325 | * Name cache initialization, from vfs_init() when we are booting | |
326 | */ | |
327 | void | |
91447636 | 328 | psem_cache_init(void) |
1c79356b | 329 | { |
2d21ac55 | 330 | psemhashtbl = hashinit(posix_sem_max / 2, M_SHM, &psemhash); |
1c79356b A |
331 | } |
332 | ||
9bccf70c | 333 | static void |
91447636 | 334 | psem_cache_delete(struct psemcache *pcp) |
9bccf70c A |
335 | { |
336 | #if DIAGNOSTIC | |
337 | if (pcp->psem_hash.le_prev == 0) | |
338 | panic("psem namecache purge le_prev"); | |
339 | if (pcp->psem_hash.le_next == pcp) | |
340 | panic("namecache purge le_next"); | |
341 | #endif /* DIAGNOSTIC */ | |
342 | LIST_REMOVE(pcp, psem_hash); | |
2d21ac55 | 343 | pcp->psem_hash.le_prev = NULL; |
9bccf70c A |
344 | psemnument--; |
345 | } | |
346 | ||
1c79356b | 347 | /* |
490019cf A |
348 | * Remove all cached psem entries. Open semaphores (with a positive refcount) |
349 | * will continue to exist, but their cache entries tying them to a particular | |
350 | * name/path will be removed making all future lookups on the name fail. | |
1c79356b | 351 | */ |
490019cf A |
352 | int |
353 | psem_cache_purge_all(__unused proc_t p) | |
1c79356b | 354 | { |
490019cf | 355 | struct psemcache *pcp, *tmppcp; |
1c79356b | 356 | struct psemhashhead *pcpp; |
490019cf | 357 | int error = 0; |
1c79356b | 358 | |
490019cf A |
359 | if (kauth_cred_issuser(kauth_cred_get()) == 0) |
360 | return EPERM; | |
361 | ||
362 | PSEM_SUBSYS_LOCK(); | |
1c79356b | 363 | for (pcpp = &psemhashtbl[psemhash]; pcpp >= psemhashtbl; pcpp--) { |
490019cf A |
364 | LIST_FOREACH_SAFE(pcp, pcpp, psem_hash, tmppcp) { |
365 | assert(pcp->psem_nlen); | |
366 | /* | |
367 | * unconditionally unlink the cache entry | |
368 | */ | |
369 | error = psem_unlink_internal(pcp->pseminfo, pcp); | |
370 | if (error) | |
371 | goto out; | |
372 | } | |
1c79356b | 373 | } |
490019cf A |
374 | assert(psemnument == 0); |
375 | ||
376 | out: | |
377 | PSEM_SUBSYS_UNLOCK(); | |
378 | ||
379 | if (error) | |
380 | printf("%s: Error %d removing all semaphores: %ld remain!\n", | |
381 | __func__, error, psemnument); | |
382 | return error; | |
1c79356b | 383 | } |
1c79356b A |
384 | |
385 | int | |
2d21ac55 | 386 | sem_open(proc_t p, struct sem_open_args *uap, user_addr_t *retval) |
1c79356b | 387 | { |
91447636 | 388 | size_t i; |
91447636 | 389 | int indx, error; |
1c79356b A |
390 | struct psemname nd; |
391 | struct pseminfo *pinfo; | |
b0d623f7 A |
392 | struct fileproc *fp = NULL; |
393 | char *pnbuf = NULL; | |
394 | struct pseminfo *new_pinfo = PSEMINFO_NULL; | |
395 | struct psemnode *new_pnode = PSEMNODE_NULL; | |
396 | struct psemcache *pcache = PSEMCACHE_NULL; | |
1c79356b A |
397 | char * nameptr; |
398 | char * cp; | |
399 | size_t pathlen, plen; | |
400 | int fmode ; | |
401 | int cmode = uap->mode; | |
402 | int value = uap->value; | |
403 | int incache = 0; | |
b0d623f7 A |
404 | struct psemcache *pcp = PSEMCACHE_NULL; |
405 | kern_return_t kret = KERN_INVALID_ADDRESS; /* default fail */ | |
406 | ||
e5568f75 A |
407 | AUDIT_ARG(fflags, uap->oflag); |
408 | AUDIT_ARG(mode, uap->mode); | |
b0d623f7 | 409 | AUDIT_ARG(value32, uap->value); |
91447636 | 410 | |
1c79356b A |
411 | pinfo = PSEMINFO_NULL; |
412 | ||
b0d623f7 A |
413 | /* |
414 | * Preallocate everything we might need up front to avoid taking | |
415 | * and dropping the lock, opening us up to race conditions. | |
416 | */ | |
4d15aeb1 | 417 | MALLOC_ZONE(pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK | M_ZERO); |
b0d623f7 A |
418 | if (pnbuf == NULL) { |
419 | error = ENOSPC; | |
420 | goto bad; | |
421 | } | |
91447636 | 422 | |
1c79356b | 423 | pathlen = MAXPATHLEN; |
91447636 | 424 | error = copyinstr(uap->name, pnbuf, MAXPATHLEN, &pathlen); |
1c79356b A |
425 | if (error) { |
426 | goto bad; | |
427 | } | |
e5568f75 | 428 | AUDIT_ARG(text, pnbuf); |
91447636 | 429 | if ( (pathlen > PSEMNAMLEN) ) { |
1c79356b A |
430 | error = ENAMETOOLONG; |
431 | goto bad; | |
432 | } | |
433 | ||
1c79356b A |
434 | #ifdef PSXSEM_NAME_RESTRICT |
435 | nameptr = pnbuf; | |
436 | if (*nameptr == '/') { | |
437 | while (*(nameptr++) == '/') { | |
438 | plen--; | |
439 | error = EINVAL; | |
440 | goto bad; | |
441 | } | |
442 | } else { | |
443 | error = EINVAL; | |
444 | goto bad; | |
445 | } | |
446 | #endif /* PSXSEM_NAME_RESTRICT */ | |
447 | ||
448 | plen = pathlen; | |
449 | nameptr = pnbuf; | |
450 | nd.psem_nameptr = nameptr; | |
451 | nd.psem_namelen = plen; | |
2d21ac55 | 452 | nd.psem_hash = 0; |
1c79356b A |
453 | |
454 | for (cp = nameptr, i=1; *cp != 0 && i <= plen; i++, cp++) { | |
455 | nd.psem_hash += (unsigned char)*cp * i; | |
456 | } | |
457 | ||
b0d623f7 A |
458 | /* |
459 | * attempt to allocate a new fp; if unsuccessful, the fp will be | |
460 | * left unmodified (NULL). | |
461 | */ | |
462 | error = falloc(p, &fp, &indx, vfs_context_current()); | |
463 | if (error) | |
464 | goto bad; | |
465 | ||
466 | /* | |
467 | * We allocate a new entry if we are less than the maximum | |
468 | * allowed and the one at the front of the LRU list is in use. | |
469 | * Otherwise we use the one at the front of the LRU list. | |
470 | */ | |
471 | MALLOC(pcp, struct psemcache *, sizeof(struct psemcache), M_SHM, M_WAITOK|M_ZERO); | |
472 | if (pcp == PSEMCACHE_NULL) { | |
473 | error = ENOMEM; | |
474 | goto bad; | |
475 | } | |
476 | ||
477 | MALLOC(new_pinfo, struct pseminfo *, sizeof(struct pseminfo), M_SHM, M_WAITOK|M_ZERO); | |
478 | if (new_pinfo == NULL) { | |
479 | error = ENOSPC; | |
480 | goto bad; | |
481 | } | |
482 | #if CONFIG_MACF | |
483 | mac_posixsem_label_init(new_pinfo); | |
484 | #endif | |
485 | ||
486 | /* | |
487 | * Provisionally create the semaphore in the new_pinfo; we have to do | |
488 | * this here to prevent locking later. We use the value of kret to | |
489 | * signal success or failure, which is why we set its default value | |
490 | * to KERN_INVALID_ADDRESS, above. | |
491 | */ | |
492 | ||
493 | fmode = FFLAGS(uap->oflag); | |
494 | ||
495 | if((fmode & O_CREAT)) { | |
496 | ||
497 | if((value < 0) || (value > SEM_VALUE_MAX)) { | |
498 | error = EINVAL; | |
499 | goto bad; | |
500 | } | |
501 | ||
502 | kret = semaphore_create(kernel_task, &new_pinfo->psem_semobject, SYNC_POLICY_FIFO, value); | |
503 | ||
504 | if (kret != KERN_SUCCESS) { | |
505 | switch (kret) { | |
506 | case KERN_RESOURCE_SHORTAGE: | |
507 | error = ENOMEM; | |
508 | break; | |
509 | case KERN_PROTECTION_FAILURE: | |
510 | error = EACCES; | |
511 | break; | |
512 | default: | |
513 | error = EINVAL; | |
514 | } | |
515 | goto bad; | |
516 | } | |
517 | } | |
518 | ||
519 | MALLOC(new_pnode, struct psemnode *, sizeof(struct psemnode), M_SHM, M_WAITOK|M_ZERO); | |
520 | if (new_pnode == NULL) { | |
521 | error = ENOSPC; | |
522 | goto bad; | |
523 | } | |
524 | ||
91447636 | 525 | PSEM_SUBSYS_LOCK(); |
1c79356b A |
526 | error = psem_cache_search(&pinfo, &nd, &pcache); |
527 | ||
490019cf | 528 | if (error == PSEMCACHE_NEGATIVE) { |
1c79356b | 529 | error = EINVAL; |
b0d623f7 | 530 | goto bad_locked; |
1c79356b | 531 | } |
490019cf A |
532 | |
533 | if (error == PSEMCACHE_FOUND) | |
1c79356b | 534 | incache = 1; |
490019cf A |
535 | else |
536 | incache = 0; | |
1c79356b | 537 | |
1c79356b A |
538 | cmode &= ALLPERMS; |
539 | ||
540 | if (((fmode & (O_CREAT | O_EXCL))==(O_CREAT | O_EXCL)) && incache) { | |
541 | /* sem exists and opened O_EXCL */ | |
542 | #if notyet | |
543 | if (pinfo->psem_flags & PSEM_INDELETE) { | |
544 | } | |
545 | #endif | |
e5568f75 | 546 | AUDIT_ARG(posix_ipc_perm, pinfo->psem_uid, |
91447636 | 547 | pinfo->psem_gid, pinfo->psem_mode); |
1c79356b | 548 | error = EEXIST; |
b0d623f7 | 549 | goto bad_locked; |
1c79356b A |
550 | } |
551 | if (((fmode & (O_CREAT | O_EXCL))== O_CREAT) && incache) { | |
552 | /* As per POSIX, O_CREAT has no effect */ | |
553 | fmode &= ~O_CREAT; | |
554 | } | |
555 | ||
91447636 | 556 | if ( (fmode & O_CREAT) ) { |
b0d623f7 A |
557 | /* create a new one (commit the allocation) */ |
558 | pinfo = new_pinfo; | |
1c79356b A |
559 | pinfo->psem_flags = PSEM_DEFINED | PSEM_INCREATE; |
560 | pinfo->psem_usecount = 1; | |
561 | pinfo->psem_mode = cmode; | |
6d2010ae A |
562 | pinfo->psem_uid = kauth_getuid(); |
563 | pinfo->psem_gid = kauth_getgid(); | |
0c530ab8 A |
564 | bcopy(pnbuf, &pinfo->psem_name[0], PSEMNAMLEN); |
565 | pinfo->psem_name[PSEMNAMLEN]= 0; | |
1c79356b A |
566 | pinfo->psem_flags &= ~PSEM_DEFINED; |
567 | pinfo->psem_flags |= PSEM_ALLOCATED; | |
39236c6e A |
568 | pinfo->psem_creator_pid = p->p_pid; |
569 | pinfo->psem_creator_uniqueid = p->p_uniqueid; | |
b0d623f7 | 570 | |
2d21ac55 A |
571 | #if CONFIG_MACF |
572 | error = mac_posixsem_check_create(kauth_cred_get(), nameptr); | |
573 | if (error) { | |
b0d623f7 | 574 | goto bad_locked; |
2d21ac55 A |
575 | } |
576 | mac_posixsem_label_associate(kauth_cred_get(), pinfo, nameptr); | |
577 | #endif | |
1c79356b A |
578 | } else { |
579 | /* semaphore should exist as it is without O_CREAT */ | |
580 | if (!incache) { | |
581 | error = ENOENT; | |
b0d623f7 | 582 | goto bad_locked; |
1c79356b A |
583 | } |
584 | if( pinfo->psem_flags & PSEM_INDELETE) { | |
585 | error = ENOENT; | |
b0d623f7 | 586 | goto bad_locked; |
1c79356b | 587 | } |
e5568f75 | 588 | AUDIT_ARG(posix_ipc_perm, pinfo->psem_uid, |
91447636 | 589 | pinfo->psem_gid, pinfo->psem_mode); |
2d21ac55 A |
590 | #if CONFIG_MACF |
591 | error = mac_posixsem_check_open(kauth_cred_get(), pinfo); | |
592 | if (error) { | |
b0d623f7 | 593 | goto bad_locked; |
2d21ac55 A |
594 | } |
595 | #endif | |
91447636 | 596 | if ( (error = psem_access(pinfo, fmode, kauth_cred_get())) ) { |
b0d623f7 | 597 | goto bad_locked; |
91447636 | 598 | } |
1c79356b | 599 | } |
1c79356b A |
600 | |
601 | if (!incache) { | |
b0d623f7 | 602 | /* if successful, this will consume the pcp */ |
91447636 | 603 | if ( (error = psem_cache_add(pinfo, &nd, pcp)) ) { |
b0d623f7 | 604 | goto bad_locked; |
1c79356b A |
605 | } |
606 | } | |
607 | pinfo->psem_flags &= ~PSEM_INCREATE; | |
608 | pinfo->psem_usecount++; | |
b0d623f7 | 609 | new_pnode->pinfo = pinfo; |
91447636 A |
610 | PSEM_SUBSYS_UNLOCK(); |
611 | ||
b0d623f7 A |
612 | /* |
613 | * if incache, we did not use the new pcp or the new pcp or the | |
614 | * new . and we must free them. | |
615 | */ | |
616 | if (incache) { | |
617 | FREE(pcp, M_SHM); | |
618 | pcp = PSEMCACHE_NULL; | |
619 | if (new_pinfo != PSEMINFO_NULL) { | |
620 | /* return value ignored - we can't _not_ do this */ | |
621 | (void)semaphore_destroy(kernel_task, new_pinfo->psem_semobject); | |
622 | #if CONFIG_MACF | |
623 | mac_posixsem_label_destroy(new_pinfo); | |
624 | #endif | |
625 | FREE(new_pinfo, M_SHM); | |
626 | new_pinfo = PSEMINFO_NULL; | |
627 | } | |
628 | } | |
629 | ||
91447636 | 630 | proc_fdlock(p); |
55e303ae | 631 | fp->f_flag = fmode & FMASK; |
1c79356b | 632 | fp->f_ops = &psemops; |
b0d623f7 | 633 | fp->f_data = (caddr_t)new_pnode; |
6601e61a | 634 | procfdtbl_releasefd(p, indx, NULL); |
91447636 A |
635 | fp_drop(p, indx, fp, 1); |
636 | proc_fdunlock(p); | |
637 | ||
638 | *retval = CAST_USER_ADDR_T(indx); | |
55e303ae | 639 | FREE_ZONE(pnbuf, MAXPATHLEN, M_NAMEI); |
1c79356b A |
640 | return (0); |
641 | ||
b0d623f7 A |
642 | bad_locked: |
643 | PSEM_SUBSYS_UNLOCK(); | |
644 | bad: | |
645 | if (pcp != PSEMCACHE_NULL) | |
646 | FREE(pcp, M_SHM); | |
647 | ||
648 | if (new_pnode != PSEMNODE_NULL) | |
649 | FREE(new_pnode, M_SHM); | |
650 | ||
651 | if (fp != NULL) | |
652 | fp_free(p, indx, fp); | |
653 | ||
654 | if (new_pinfo != PSEMINFO_NULL) { | |
655 | /* | |
656 | * kret signals whether or not we successfully created a | |
657 | * Mach semaphore for this semaphore; if so, we need to | |
658 | * destroy it here. | |
659 | */ | |
660 | if (kret == KERN_SUCCESS) { | |
661 | /* return value ignored - we can't _not_ do this */ | |
662 | (void)semaphore_destroy(kernel_task, new_pinfo->psem_semobject); | |
663 | } | |
2d21ac55 | 664 | #if CONFIG_MACF |
b0d623f7 | 665 | mac_posixsem_label_destroy(new_pinfo); |
2d21ac55 | 666 | #endif |
b0d623f7 | 667 | FREE(new_pinfo, M_SHM); |
2d21ac55 | 668 | } |
b0d623f7 A |
669 | |
670 | if (pnbuf != NULL) | |
671 | FREE_ZONE(pnbuf, MAXPATHLEN, M_NAMEI); | |
1c79356b A |
672 | return (error); |
673 | } | |
674 | ||
91447636 A |
675 | /* |
676 | * XXX This code is repeated in several places | |
677 | */ | |
678 | static int | |
679 | psem_access(struct pseminfo *pinfo, int mode, kauth_cred_t cred) | |
1c79356b | 680 | { |
6d2010ae A |
681 | int mode_req = ((mode & FREAD) ? S_IRUSR : 0) | |
682 | ((mode & FWRITE) ? S_IWUSR : 0); | |
1c79356b A |
683 | |
684 | /* Otherwise, user id 0 always gets access. */ | |
91447636 | 685 | if (!suser(cred, NULL)) |
1c79356b A |
686 | return (0); |
687 | ||
6d2010ae | 688 | return(posix_cred_access(cred, pinfo->psem_uid, pinfo->psem_gid, pinfo->psem_mode, mode_req)); |
1c79356b A |
689 | } |
690 | ||
490019cf A |
691 | static int |
692 | psem_unlink_internal(struct pseminfo *pinfo, struct psemcache *pcache) | |
693 | { | |
694 | PSEM_SUBSYS_ASSERT_HELD(); | |
695 | ||
696 | if (!pinfo || !pcache) | |
697 | return EINVAL; | |
698 | ||
699 | if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED)) == 0) | |
700 | return EINVAL; | |
701 | ||
702 | if (pinfo->psem_flags & PSEM_INDELETE) | |
703 | return 0; | |
704 | ||
705 | AUDIT_ARG(posix_ipc_perm, pinfo->psem_uid, pinfo->psem_gid, | |
706 | pinfo->psem_mode); | |
707 | ||
708 | pinfo->psem_flags |= PSEM_INDELETE; | |
709 | pinfo->psem_usecount--; | |
710 | ||
711 | if (!pinfo->psem_usecount) { | |
712 | psem_delete(pinfo); | |
713 | FREE(pinfo,M_SHM); | |
714 | } else { | |
715 | pinfo->psem_flags |= PSEM_REMOVED; | |
716 | } | |
717 | ||
718 | psem_cache_delete(pcache); | |
719 | FREE(pcache, M_SHM); | |
720 | return 0; | |
721 | } | |
722 | ||
723 | ||
1c79356b | 724 | int |
b0d623f7 | 725 | sem_unlink(__unused proc_t p, struct sem_unlink_args *uap, __unused int32_t *retval) |
1c79356b | 726 | { |
91447636 | 727 | size_t i; |
1c79356b A |
728 | int error=0; |
729 | struct psemname nd; | |
730 | struct pseminfo *pinfo; | |
1c79356b A |
731 | char * nameptr; |
732 | char * cp; | |
490019cf A |
733 | char * pnbuf; |
734 | size_t pathlen; | |
1c79356b | 735 | struct psemcache *pcache = PSEMCACHE_NULL; |
1c79356b A |
736 | |
737 | pinfo = PSEMINFO_NULL; | |
738 | ||
91447636 A |
739 | MALLOC_ZONE(pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK); |
740 | if (pnbuf == NULL) { | |
741 | return(ENOSPC); /* XXX non-standard */ | |
742 | } | |
1c79356b | 743 | pathlen = MAXPATHLEN; |
91447636 | 744 | error = copyinstr(uap->name, pnbuf, MAXPATHLEN, &pathlen); |
1c79356b A |
745 | if (error) { |
746 | goto bad; | |
747 | } | |
e5568f75 | 748 | AUDIT_ARG(text, pnbuf); |
1c79356b A |
749 | if (pathlen > PSEMNAMLEN) { |
750 | error = ENAMETOOLONG; | |
751 | goto bad; | |
752 | } | |
753 | ||
490019cf | 754 | nameptr = pnbuf; |
1c79356b A |
755 | |
756 | #ifdef PSXSEM_NAME_RESTRICT | |
1c79356b A |
757 | if (*nameptr == '/') { |
758 | while (*(nameptr++) == '/') { | |
490019cf | 759 | pathlen--; |
1c79356b A |
760 | error = EINVAL; |
761 | goto bad; | |
762 | } | |
763 | } else { | |
764 | error = EINVAL; | |
765 | goto bad; | |
766 | } | |
767 | #endif /* PSXSEM_NAME_RESTRICT */ | |
768 | ||
1c79356b | 769 | nd.psem_nameptr = nameptr; |
490019cf | 770 | nd.psem_namelen = pathlen; |
1c79356b A |
771 | nd. psem_hash =0; |
772 | ||
490019cf | 773 | for (cp = nameptr, i=1; *cp != 0 && i <= pathlen; i++, cp++) { |
1c79356b A |
774 | nd.psem_hash += (unsigned char)*cp * i; |
775 | } | |
776 | ||
91447636 | 777 | PSEM_SUBSYS_LOCK(); |
1c79356b A |
778 | error = psem_cache_search(&pinfo, &nd, &pcache); |
779 | ||
490019cf | 780 | if (error != PSEMCACHE_FOUND) { |
91447636 | 781 | PSEM_SUBSYS_UNLOCK(); |
1c79356b A |
782 | error = EINVAL; |
783 | goto bad; | |
784 | ||
785 | } | |
490019cf | 786 | |
2d21ac55 A |
787 | #if CONFIG_MACF |
788 | error = mac_posixsem_check_unlink(kauth_cred_get(), pinfo, nameptr); | |
789 | if (error) { | |
790 | PSEM_SUBSYS_UNLOCK(); | |
791 | goto bad; | |
792 | } | |
793 | #endif | |
91447636 A |
794 | if ( (error = psem_access(pinfo, pinfo->psem_mode, kauth_cred_get())) ) { |
795 | PSEM_SUBSYS_UNLOCK(); | |
1c79356b | 796 | goto bad; |
91447636 | 797 | } |
1c79356b | 798 | |
490019cf | 799 | error = psem_unlink_internal(pinfo, pcache); |
91447636 | 800 | PSEM_SUBSYS_UNLOCK(); |
490019cf | 801 | |
1c79356b | 802 | bad: |
55e303ae | 803 | FREE_ZONE(pnbuf, MAXPATHLEN, M_NAMEI); |
490019cf | 804 | return error; |
1c79356b A |
805 | } |
806 | ||
1c79356b | 807 | int |
b0d623f7 | 808 | sem_close(proc_t p, struct sem_close_args *uap, __unused int32_t *retval) |
1c79356b | 809 | { |
b0d623f7 | 810 | int fd = CAST_DOWN_EXPLICIT(int,uap->sem); |
91447636 | 811 | struct fileproc *fp; |
1c79356b A |
812 | int error = 0; |
813 | ||
e5568f75 | 814 | AUDIT_ARG(fd, fd); /* XXX This seems wrong; uap->sem is a pointer */ |
91447636 A |
815 | |
816 | proc_fdlock(p); | |
817 | error = fp_lookup(p,fd, &fp, 1); | |
818 | if (error) { | |
819 | proc_fdunlock(p); | |
1c79356b | 820 | return(error); |
91447636 | 821 | } |
6d2010ae | 822 | procfdtbl_markclosefd(p, fd); |
2d21ac55 | 823 | fileproc_drain(p, fp); |
91447636 A |
824 | fdrelse(p, fd); |
825 | error = closef_locked(fp, fp->f_fglob, p); | |
39236c6e | 826 | fileproc_free(fp); |
91447636 A |
827 | proc_fdunlock(p); |
828 | return(error); | |
1c79356b A |
829 | } |
830 | ||
1c79356b | 831 | int |
b0d623f7 | 832 | sem_wait(proc_t p, struct sem_wait_args *uap, int32_t *retval) |
2d21ac55 A |
833 | { |
834 | __pthread_testcancel(1); | |
835 | return(sem_wait_nocancel(p, (struct sem_wait_nocancel_args *)uap, retval)); | |
836 | } | |
837 | ||
838 | int | |
b0d623f7 | 839 | sem_wait_nocancel(proc_t p, struct sem_wait_nocancel_args *uap, __unused int32_t *retval) |
1c79356b | 840 | { |
b0d623f7 | 841 | int fd = CAST_DOWN_EXPLICIT(int,uap->sem); |
91447636 | 842 | struct fileproc *fp; |
1c79356b A |
843 | struct pseminfo * pinfo; |
844 | struct psemnode * pnode ; | |
845 | kern_return_t kret; | |
846 | int error; | |
847 | ||
91447636 A |
848 | error = fp_getfpsem(p, fd, &fp, &pnode); |
849 | if (error) | |
1c79356b | 850 | return (error); |
91447636 A |
851 | if (((pnode = (struct psemnode *)fp->f_data)) == PSEMNODE_NULL ) { |
852 | error = EINVAL; | |
853 | goto out; | |
854 | } | |
855 | PSEM_SUBSYS_LOCK(); | |
856 | if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) { | |
857 | PSEM_SUBSYS_UNLOCK(); | |
858 | error = EINVAL; | |
859 | goto out; | |
860 | } | |
1c79356b A |
861 | if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED)) |
862 | != PSEM_ALLOCATED) { | |
91447636 A |
863 | PSEM_SUBSYS_UNLOCK(); |
864 | error = EINVAL; | |
865 | goto out; | |
1c79356b | 866 | } |
2d21ac55 A |
867 | #if CONFIG_MACF |
868 | error = mac_posixsem_check_wait(kauth_cred_get(), pinfo); | |
869 | if (error) { | |
870 | PSEM_SUBSYS_UNLOCK(); | |
871 | goto out; | |
872 | } | |
873 | #endif | |
91447636 | 874 | PSEM_SUBSYS_UNLOCK(); |
1c79356b A |
875 | kret = semaphore_wait(pinfo->psem_semobject); |
876 | switch (kret) { | |
877 | case KERN_INVALID_ADDRESS: | |
878 | case KERN_PROTECTION_FAILURE: | |
91447636 A |
879 | error = EACCES; |
880 | break; | |
1c79356b A |
881 | case KERN_ABORTED: |
882 | case KERN_OPERATION_TIMED_OUT: | |
91447636 A |
883 | error = EINTR; |
884 | break; | |
1c79356b | 885 | case KERN_SUCCESS: |
91447636 A |
886 | error = 0; |
887 | break; | |
1c79356b | 888 | default: |
91447636 A |
889 | error = EINVAL; |
890 | break; | |
1c79356b | 891 | } |
91447636 A |
892 | out: |
893 | fp_drop(p, fd, fp, 0); | |
894 | return(error); | |
1c79356b | 895 | |
91447636 | 896 | } |
1c79356b A |
897 | |
898 | int | |
b0d623f7 | 899 | sem_trywait(proc_t p, struct sem_trywait_args *uap, __unused int32_t *retval) |
1c79356b | 900 | { |
b0d623f7 | 901 | int fd = CAST_DOWN_EXPLICIT(int,uap->sem); |
91447636 | 902 | struct fileproc *fp; |
1c79356b A |
903 | struct pseminfo * pinfo; |
904 | struct psemnode * pnode ; | |
905 | kern_return_t kret; | |
906 | mach_timespec_t wait_time; | |
907 | int error; | |
908 | ||
91447636 A |
909 | error = fp_getfpsem(p, fd, &fp, &pnode); |
910 | if (error) | |
1c79356b | 911 | return (error); |
91447636 A |
912 | if (((pnode = (struct psemnode *)fp->f_data)) == PSEMNODE_NULL ) { |
913 | error = EINVAL; | |
914 | goto out; | |
915 | } | |
916 | PSEM_SUBSYS_LOCK(); | |
917 | if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) { | |
918 | PSEM_SUBSYS_UNLOCK(); | |
919 | error = EINVAL; | |
920 | goto out; | |
921 | } | |
1c79356b A |
922 | if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED)) |
923 | != PSEM_ALLOCATED) { | |
91447636 A |
924 | PSEM_SUBSYS_UNLOCK(); |
925 | error = EINVAL; | |
926 | goto out; | |
1c79356b | 927 | } |
2d21ac55 A |
928 | #if CONFIG_MACF |
929 | error = mac_posixsem_check_wait(kauth_cred_get(), pinfo); | |
930 | if (error) { | |
931 | PSEM_SUBSYS_UNLOCK(); | |
932 | goto out; | |
933 | } | |
934 | #endif | |
91447636 | 935 | PSEM_SUBSYS_UNLOCK(); |
1c79356b A |
936 | wait_time.tv_sec = 0; |
937 | wait_time.tv_nsec = 0; | |
938 | ||
939 | kret = semaphore_timedwait(pinfo->psem_semobject, MACH_TIMESPEC_ZERO); | |
940 | switch (kret) { | |
941 | case KERN_INVALID_ADDRESS: | |
942 | case KERN_PROTECTION_FAILURE: | |
91447636 A |
943 | error = EINVAL; |
944 | break; | |
1c79356b | 945 | case KERN_ABORTED: |
91447636 A |
946 | error = EINTR; |
947 | break; | |
1c79356b | 948 | case KERN_OPERATION_TIMED_OUT: |
91447636 A |
949 | error = EAGAIN; |
950 | break; | |
1c79356b | 951 | case KERN_SUCCESS: |
91447636 A |
952 | error = 0; |
953 | break; | |
1c79356b | 954 | default: |
91447636 A |
955 | error = EINVAL; |
956 | break; | |
1c79356b | 957 | } |
91447636 A |
958 | out: |
959 | fp_drop(p, fd, fp, 0); | |
960 | return(error); | |
1c79356b A |
961 | } |
962 | ||
1c79356b | 963 | int |
b0d623f7 | 964 | sem_post(proc_t p, struct sem_post_args *uap, __unused int32_t *retval) |
1c79356b | 965 | { |
b0d623f7 | 966 | int fd = CAST_DOWN_EXPLICIT(int,uap->sem); |
91447636 | 967 | struct fileproc *fp; |
1c79356b A |
968 | struct pseminfo * pinfo; |
969 | struct psemnode * pnode ; | |
970 | kern_return_t kret; | |
971 | int error; | |
972 | ||
91447636 A |
973 | error = fp_getfpsem(p, fd, &fp, &pnode); |
974 | if (error) | |
1c79356b | 975 | return (error); |
91447636 A |
976 | if (((pnode = (struct psemnode *)fp->f_data)) == PSEMNODE_NULL ) { |
977 | error = EINVAL; | |
978 | goto out; | |
979 | } | |
980 | PSEM_SUBSYS_LOCK(); | |
981 | if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) { | |
982 | PSEM_SUBSYS_UNLOCK(); | |
983 | error = EINVAL; | |
984 | goto out; | |
985 | } | |
1c79356b A |
986 | if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED)) |
987 | != PSEM_ALLOCATED) { | |
91447636 A |
988 | PSEM_SUBSYS_UNLOCK(); |
989 | error = EINVAL; | |
990 | goto out; | |
1c79356b | 991 | } |
2d21ac55 A |
992 | #if CONFIG_MACF |
993 | error = mac_posixsem_check_post(kauth_cred_get(), pinfo); | |
994 | if (error) { | |
995 | PSEM_SUBSYS_UNLOCK(); | |
996 | goto out; | |
997 | } | |
998 | #endif | |
91447636 | 999 | PSEM_SUBSYS_UNLOCK(); |
1c79356b A |
1000 | kret = semaphore_signal(pinfo->psem_semobject); |
1001 | switch (kret) { | |
1002 | case KERN_INVALID_ADDRESS: | |
1003 | case KERN_PROTECTION_FAILURE: | |
91447636 A |
1004 | error = EINVAL; |
1005 | break; | |
1c79356b A |
1006 | case KERN_ABORTED: |
1007 | case KERN_OPERATION_TIMED_OUT: | |
91447636 A |
1008 | error = EINTR; |
1009 | break; | |
1c79356b | 1010 | case KERN_SUCCESS: |
91447636 A |
1011 | error = 0; |
1012 | break; | |
1c79356b | 1013 | default: |
91447636 A |
1014 | error = EINVAL; |
1015 | break; | |
1c79356b | 1016 | } |
91447636 A |
1017 | out: |
1018 | fp_drop(p, fd, fp, 0); | |
1019 | return(error); | |
1c79356b A |
1020 | } |
1021 | ||
9bccf70c | 1022 | static int |
2d21ac55 | 1023 | psem_close(struct psemnode *pnode, __unused int flags) |
1c79356b A |
1024 | { |
1025 | int error=0; | |
2d21ac55 | 1026 | struct pseminfo *pinfo; |
1c79356b | 1027 | |
91447636 A |
1028 | PSEM_SUBSYS_LOCK(); |
1029 | if ((pinfo = pnode->pinfo) == PSEMINFO_NULL){ | |
1030 | PSEM_SUBSYS_UNLOCK(); | |
1c79356b | 1031 | return(EINVAL); |
91447636 | 1032 | } |
1c79356b A |
1033 | |
1034 | if ((pinfo->psem_flags & PSEM_ALLOCATED) != PSEM_ALLOCATED) { | |
91447636 | 1035 | PSEM_SUBSYS_UNLOCK(); |
1c79356b A |
1036 | return(EINVAL); |
1037 | } | |
1038 | #if DIAGNOSTIC | |
1039 | if(!pinfo->psem_usecount) { | |
1040 | kprintf("negative usecount in psem_close\n"); | |
1041 | } | |
1042 | #endif /* DIAGNOSTIC */ | |
1043 | pinfo->psem_usecount--; | |
1044 | ||
1045 | if ((pinfo->psem_flags & PSEM_REMOVED) && !pinfo->psem_usecount) { | |
91447636 A |
1046 | PSEM_SUBSYS_UNLOCK(); |
1047 | /* lock dropped as only semaphore is destroyed here */ | |
1c79356b | 1048 | error = psem_delete(pinfo); |
91447636 A |
1049 | FREE(pinfo,M_SHM); |
1050 | } else { | |
1051 | PSEM_SUBSYS_UNLOCK(); | |
1c79356b | 1052 | } |
91447636 A |
1053 | /* subsystem lock is dropped when we get here */ |
1054 | FREE(pnode, M_SHM); | |
1c79356b A |
1055 | return (error); |
1056 | } | |
1057 | ||
9bccf70c | 1058 | static int |
2d21ac55 | 1059 | psem_closefile(struct fileglob *fg, __unused vfs_context_t ctx) |
9bccf70c | 1060 | { |
91447636 | 1061 | int error; |
9bccf70c | 1062 | |
2d21ac55 A |
1063 | /* |
1064 | * Not locked as psem_close is called only from here and is locked | |
1065 | * properly | |
1066 | */ | |
1067 | error = psem_close(((struct psemnode *)fg->fg_data), fg->fg_flag); | |
91447636 A |
1068 | |
1069 | return(error); | |
9bccf70c A |
1070 | } |
1071 | ||
91447636 | 1072 | static int |
1c79356b A |
1073 | psem_delete(struct pseminfo * pinfo) |
1074 | { | |
1075 | kern_return_t kret; | |
1076 | ||
1077 | kret = semaphore_destroy(kernel_task, pinfo->psem_semobject); | |
2d21ac55 A |
1078 | #if CONFIG_MACF |
1079 | mac_posixsem_label_destroy(pinfo); | |
1080 | #endif | |
1c79356b A |
1081 | |
1082 | switch (kret) { | |
1083 | case KERN_INVALID_ADDRESS: | |
1084 | case KERN_PROTECTION_FAILURE: | |
1085 | return (EINVAL); | |
1086 | case KERN_ABORTED: | |
1087 | case KERN_OPERATION_TIMED_OUT: | |
1088 | return (EINTR); | |
1089 | case KERN_SUCCESS: | |
1090 | return(0); | |
1091 | default: | |
1092 | return (EINVAL); | |
1093 | } | |
1c79356b A |
1094 | } |
1095 | ||
9bccf70c | 1096 | static int |
5ba3f43e A |
1097 | psem_read(__unused struct fileproc *fp, __unused struct uio *uio, |
1098 | __unused int flags, __unused vfs_context_t ctx) | |
1c79356b | 1099 | { |
91447636 | 1100 | return(ENOTSUP); |
1c79356b | 1101 | } |
9bccf70c A |
1102 | |
1103 | static int | |
5ba3f43e A |
1104 | psem_write(__unused struct fileproc *fp, __unused struct uio *uio, |
1105 | __unused int flags, __unused vfs_context_t ctx) | |
1c79356b | 1106 | { |
91447636 | 1107 | return(ENOTSUP); |
1c79356b | 1108 | } |
9bccf70c A |
1109 | |
1110 | static int | |
5ba3f43e A |
1111 | psem_ioctl(__unused struct fileproc *fp, __unused u_long com, |
1112 | __unused caddr_t data, __unused vfs_context_t ctx) | |
1c79356b | 1113 | { |
91447636 | 1114 | return(ENOTSUP); |
1c79356b | 1115 | } |
9bccf70c A |
1116 | |
1117 | static int | |
5ba3f43e A |
1118 | psem_select(__unused struct fileproc *fp, __unused int which, |
1119 | __unused void *wql, __unused vfs_context_t ctx) | |
1c79356b | 1120 | { |
91447636 | 1121 | return(ENOTSUP); |
1c79356b | 1122 | } |
55e303ae A |
1123 | |
1124 | static int | |
5ba3f43e A |
1125 | psem_kqfilter(__unused struct fileproc *fp, struct knote *kn, |
1126 | __unused struct kevent_internal_s *kev, __unused vfs_context_t ctx) | |
55e303ae | 1127 | { |
39037602 A |
1128 | kn->kn_flags = EV_ERROR; |
1129 | kn->kn_data = ENOTSUP; | |
1130 | return 0; | |
55e303ae A |
1131 | } |
1132 | ||
0c530ab8 A |
1133 | int |
1134 | fill_pseminfo(struct psemnode *pnode, struct psem_info * info) | |
1135 | { | |
2d21ac55 A |
1136 | struct pseminfo *pinfo; |
1137 | struct vinfo_stat *sb; | |
0c530ab8 A |
1138 | |
1139 | PSEM_SUBSYS_LOCK(); | |
1140 | if ((pinfo = pnode->pinfo) == PSEMINFO_NULL){ | |
1141 | PSEM_SUBSYS_UNLOCK(); | |
1142 | return(EINVAL); | |
1143 | } | |
1144 | ||
1145 | #if 0 | |
1146 | if ((pinfo->psem_flags & PSEM_ALLOCATED) != PSEM_ALLOCATED) { | |
1147 | PSEM_SUBSYS_UNLOCK(); | |
1148 | return(EINVAL); | |
1149 | } | |
1150 | #endif | |
1151 | ||
1152 | sb = &info->psem_stat; | |
2d21ac55 | 1153 | bzero(sb, sizeof(struct vinfo_stat)); |
0c530ab8 | 1154 | |
2d21ac55 A |
1155 | sb->vst_mode = pinfo->psem_mode; |
1156 | sb->vst_uid = pinfo->psem_uid; | |
1157 | sb->vst_gid = pinfo->psem_gid; | |
1158 | sb->vst_size = pinfo->psem_usecount; | |
0c530ab8 A |
1159 | bcopy(&pinfo->psem_name[0], &info->psem_name[0], PSEMNAMLEN+1); |
1160 | ||
1161 | PSEM_SUBSYS_UNLOCK(); | |
1162 | return(0); | |
1163 | } | |
1164 | ||
2d21ac55 A |
1165 | #if CONFIG_MACF |
1166 | void | |
1167 | psem_label_associate(struct fileproc *fp, struct vnode *vp, vfs_context_t ctx) | |
1168 | { | |
1169 | struct psemnode *pnode; | |
1170 | struct pseminfo *psem; | |
1171 | ||
1172 | PSEM_SUBSYS_LOCK(); | |
1173 | pnode = (struct psemnode *)fp->f_fglob->fg_data; | |
1174 | if (pnode != NULL) { | |
1175 | psem = pnode->pinfo; | |
1176 | if (psem != NULL) | |
1177 | mac_posixsem_vnode_label_associate( | |
1178 | vfs_context_ucred(ctx), psem, psem->psem_label, | |
1179 | vp, vp->v_label); | |
1180 | } | |
1181 | PSEM_SUBSYS_UNLOCK(); | |
1182 | } | |
1183 | #endif | |
1184 |