]>
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 **, | |
171 | struct psemname *, struct psemcache **); | |
172 | static int psem_delete(struct pseminfo * pinfo); | |
1c79356b | 173 | |
91447636 | 174 | static int psem_read (struct fileproc *fp, struct uio *uio, |
2d21ac55 | 175 | int flags, vfs_context_t ctx); |
91447636 | 176 | static int psem_write (struct fileproc *fp, struct uio *uio, |
2d21ac55 | 177 | int flags, vfs_context_t ctx); |
91447636 | 178 | static int psem_ioctl (struct fileproc *fp, u_long com, |
2d21ac55 A |
179 | caddr_t data, vfs_context_t ctx); |
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 | |
2d21ac55 | 184 | static int psem_kqfilter (struct fileproc *fp, struct knote *kn, vfs_context_t ctx); |
55e303ae | 185 | |
39236c6e | 186 | static const struct fileops psemops = { |
39037602 A |
187 | .fo_type = DTYPE_PSXSEM, |
188 | .fo_read = psem_read, | |
189 | .fo_write = psem_write, | |
190 | .fo_ioctl = psem_ioctl, | |
191 | .fo_select = psem_select, | |
192 | .fo_close = psem_closefile, | |
193 | .fo_kqfilter = psem_kqfilter, | |
194 | .fo_drain = NULL, | |
39236c6e | 195 | }; |
91447636 A |
196 | |
197 | static lck_grp_t *psx_sem_subsys_lck_grp; | |
198 | static lck_grp_attr_t *psx_sem_subsys_lck_grp_attr; | |
199 | static lck_attr_t *psx_sem_subsys_lck_attr; | |
200 | static lck_mtx_t psx_sem_subsys_mutex; | |
201 | ||
202 | #define PSEM_SUBSYS_LOCK() lck_mtx_lock(& psx_sem_subsys_mutex) | |
203 | #define PSEM_SUBSYS_UNLOCK() lck_mtx_unlock(& psx_sem_subsys_mutex) | |
39037602 | 204 | #define PSEM_SUBSYS_ASSERT_HELD() LCK_MTX_ASSERT(&psx_sem_subsys_mutex, LCK_MTX_ASSERT_OWNED) |
91447636 A |
205 | |
206 | ||
207 | static int psem_cache_add(struct pseminfo *psemp, struct psemname *pnp, struct psemcache *pcp); | |
490019cf A |
208 | static void psem_cache_delete(struct psemcache *pcp); |
209 | int psem_cache_purge_all(proc_t); | |
210 | ||
211 | ||
91447636 A |
212 | /* Initialize the mutex governing access to the posix sem subsystem */ |
213 | __private_extern__ void | |
214 | psem_lock_init( void ) | |
215 | { | |
216 | ||
217 | psx_sem_subsys_lck_grp_attr = lck_grp_attr_alloc_init(); | |
91447636 A |
218 | |
219 | psx_sem_subsys_lck_grp = lck_grp_alloc_init("posix shared memory", psx_sem_subsys_lck_grp_attr); | |
220 | ||
221 | psx_sem_subsys_lck_attr = lck_attr_alloc_init(); | |
91447636 A |
222 | lck_mtx_init(& psx_sem_subsys_mutex, psx_sem_subsys_lck_grp, psx_sem_subsys_lck_attr); |
223 | } | |
1c79356b A |
224 | |
225 | /* | |
226 | * Lookup an entry in the cache | |
227 | * | |
228 | * | |
229 | * status of -1 is returned if matches | |
230 | * If the lookup determines that the name does not exist | |
231 | * (negative cacheing), a status of ENOENT is returned. If the lookup | |
232 | * fails, a status of zero is returned. | |
233 | */ | |
234 | ||
9bccf70c | 235 | static int |
2d21ac55 A |
236 | psem_cache_search(struct pseminfo **psemp, struct psemname *pnp, |
237 | struct psemcache **pcache) | |
1c79356b | 238 | { |
91447636 A |
239 | struct psemcache *pcp, *nnp; |
240 | struct psemhashhead *pcpp; | |
1c79356b A |
241 | |
242 | if (pnp->psem_namelen > PSEMNAMLEN) { | |
243 | psemstats.longnames++; | |
490019cf | 244 | return PSEMCACHE_NOTFOUND; |
1c79356b A |
245 | } |
246 | ||
247 | pcpp = PSEMHASH(pnp); | |
248 | for (pcp = pcpp->lh_first; pcp != 0; pcp = nnp) { | |
249 | nnp = pcp->psem_hash.le_next; | |
250 | if (pcp->psem_nlen == pnp->psem_namelen && | |
251 | !bcmp(pcp->psem_name, pnp->psem_nameptr, (u_int)pcp-> psem_nlen)) | |
252 | break; | |
253 | } | |
254 | ||
255 | if (pcp == 0) { | |
256 | psemstats.miss++; | |
490019cf | 257 | return PSEMCACHE_NOTFOUND; |
1c79356b A |
258 | } |
259 | ||
260 | /* We found a "positive" match, return the vnode */ | |
261 | if (pcp->pseminfo) { | |
262 | psemstats.goodhits++; | |
263 | /* TOUCH(ncp); */ | |
264 | *psemp = pcp->pseminfo; | |
265 | *pcache = pcp; | |
490019cf | 266 | return PSEMCACHE_FOUND; |
1c79356b A |
267 | } |
268 | ||
269 | /* | |
270 | * We found a "negative" match, ENOENT notifies client of this match. | |
271 | * The nc_vpid field records whether this is a whiteout. | |
272 | */ | |
273 | psemstats.neghits++; | |
490019cf | 274 | return PSEMCACHE_NEGATIVE; |
1c79356b A |
275 | } |
276 | ||
277 | /* | |
278 | * Add an entry to the cache. | |
279 | */ | |
9bccf70c | 280 | static int |
91447636 | 281 | psem_cache_add(struct pseminfo *psemp, struct psemname *pnp, struct psemcache *pcp) |
1c79356b | 282 | { |
91447636 | 283 | struct psemhashhead *pcpp; |
1c79356b A |
284 | struct pseminfo *dpinfo; |
285 | struct psemcache *dpcp; | |
286 | ||
287 | #if DIAGNOSTIC | |
2d21ac55 | 288 | if (pnp->psem_namelen > PSEMNAMLEN) |
1c79356b A |
289 | panic("cache_enter: name too long"); |
290 | #endif | |
291 | ||
91447636 | 292 | |
1c79356b | 293 | /* if the entry has already been added by some one else return */ |
490019cf A |
294 | if (psem_cache_search(&dpinfo, pnp, &dpcp) == PSEMCACHE_FOUND) { |
295 | return EEXIST; | |
1c79356b | 296 | } |
91447636 | 297 | if (psemnument >= posix_sem_max) |
490019cf | 298 | return ENOSPC; |
1c79356b | 299 | psemnument++; |
1c79356b A |
300 | /* |
301 | * Fill in cache info, if vp is NULL this is a "negative" cache entry. | |
302 | * For negative entries, we have to record whether it is a whiteout. | |
303 | * the whiteout flag is stored in the nc_vpid field which is | |
304 | * otherwise unused. | |
305 | */ | |
306 | pcp->pseminfo = psemp; | |
307 | pcp->psem_nlen = pnp->psem_namelen; | |
308 | bcopy(pnp->psem_nameptr, pcp->psem_name, (unsigned)pcp->psem_nlen); | |
309 | pcpp = PSEMHASH(pnp); | |
310 | #if DIAGNOSTIC | |
311 | { | |
91447636 | 312 | struct psemcache *p; |
1c79356b A |
313 | |
314 | for (p = pcpp->lh_first; p != 0; p = p->psem_hash.le_next) | |
315 | if (p == pcp) | |
316 | panic("psem:cache_enter duplicate"); | |
317 | } | |
318 | #endif | |
319 | LIST_INSERT_HEAD(pcpp, pcp, psem_hash); | |
490019cf | 320 | return 0; |
1c79356b A |
321 | } |
322 | ||
323 | /* | |
324 | * Name cache initialization, from vfs_init() when we are booting | |
325 | */ | |
326 | void | |
91447636 | 327 | psem_cache_init(void) |
1c79356b | 328 | { |
2d21ac55 | 329 | psemhashtbl = hashinit(posix_sem_max / 2, M_SHM, &psemhash); |
1c79356b A |
330 | } |
331 | ||
9bccf70c | 332 | static void |
91447636 | 333 | psem_cache_delete(struct psemcache *pcp) |
9bccf70c A |
334 | { |
335 | #if DIAGNOSTIC | |
336 | if (pcp->psem_hash.le_prev == 0) | |
337 | panic("psem namecache purge le_prev"); | |
338 | if (pcp->psem_hash.le_next == pcp) | |
339 | panic("namecache purge le_next"); | |
340 | #endif /* DIAGNOSTIC */ | |
341 | LIST_REMOVE(pcp, psem_hash); | |
2d21ac55 | 342 | pcp->psem_hash.le_prev = NULL; |
9bccf70c A |
343 | psemnument--; |
344 | } | |
345 | ||
1c79356b | 346 | /* |
490019cf A |
347 | * Remove all cached psem entries. Open semaphores (with a positive refcount) |
348 | * will continue to exist, but their cache entries tying them to a particular | |
349 | * name/path will be removed making all future lookups on the name fail. | |
1c79356b | 350 | */ |
490019cf A |
351 | int |
352 | psem_cache_purge_all(__unused proc_t p) | |
1c79356b | 353 | { |
490019cf | 354 | struct psemcache *pcp, *tmppcp; |
1c79356b | 355 | struct psemhashhead *pcpp; |
490019cf | 356 | int error = 0; |
1c79356b | 357 | |
490019cf A |
358 | if (kauth_cred_issuser(kauth_cred_get()) == 0) |
359 | return EPERM; | |
360 | ||
361 | PSEM_SUBSYS_LOCK(); | |
1c79356b | 362 | for (pcpp = &psemhashtbl[psemhash]; pcpp >= psemhashtbl; pcpp--) { |
490019cf A |
363 | LIST_FOREACH_SAFE(pcp, pcpp, psem_hash, tmppcp) { |
364 | assert(pcp->psem_nlen); | |
365 | /* | |
366 | * unconditionally unlink the cache entry | |
367 | */ | |
368 | error = psem_unlink_internal(pcp->pseminfo, pcp); | |
369 | if (error) | |
370 | goto out; | |
371 | } | |
1c79356b | 372 | } |
490019cf A |
373 | assert(psemnument == 0); |
374 | ||
375 | out: | |
376 | PSEM_SUBSYS_UNLOCK(); | |
377 | ||
378 | if (error) | |
379 | printf("%s: Error %d removing all semaphores: %ld remain!\n", | |
380 | __func__, error, psemnument); | |
381 | return error; | |
1c79356b | 382 | } |
1c79356b A |
383 | |
384 | int | |
2d21ac55 | 385 | sem_open(proc_t p, struct sem_open_args *uap, user_addr_t *retval) |
1c79356b | 386 | { |
91447636 | 387 | size_t i; |
91447636 | 388 | int indx, error; |
1c79356b A |
389 | struct psemname nd; |
390 | struct pseminfo *pinfo; | |
b0d623f7 A |
391 | struct fileproc *fp = NULL; |
392 | char *pnbuf = NULL; | |
393 | struct pseminfo *new_pinfo = PSEMINFO_NULL; | |
394 | struct psemnode *new_pnode = PSEMNODE_NULL; | |
395 | struct psemcache *pcache = PSEMCACHE_NULL; | |
1c79356b A |
396 | char * nameptr; |
397 | char * cp; | |
398 | size_t pathlen, plen; | |
399 | int fmode ; | |
400 | int cmode = uap->mode; | |
401 | int value = uap->value; | |
402 | int incache = 0; | |
b0d623f7 A |
403 | struct psemcache *pcp = PSEMCACHE_NULL; |
404 | kern_return_t kret = KERN_INVALID_ADDRESS; /* default fail */ | |
405 | ||
e5568f75 A |
406 | AUDIT_ARG(fflags, uap->oflag); |
407 | AUDIT_ARG(mode, uap->mode); | |
b0d623f7 | 408 | AUDIT_ARG(value32, uap->value); |
91447636 | 409 | |
1c79356b A |
410 | pinfo = PSEMINFO_NULL; |
411 | ||
b0d623f7 A |
412 | /* |
413 | * Preallocate everything we might need up front to avoid taking | |
414 | * and dropping the lock, opening us up to race conditions. | |
415 | */ | |
4d15aeb1 | 416 | MALLOC_ZONE(pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK | M_ZERO); |
b0d623f7 A |
417 | if (pnbuf == NULL) { |
418 | error = ENOSPC; | |
419 | goto bad; | |
420 | } | |
91447636 | 421 | |
1c79356b | 422 | pathlen = MAXPATHLEN; |
91447636 | 423 | error = copyinstr(uap->name, pnbuf, MAXPATHLEN, &pathlen); |
1c79356b A |
424 | if (error) { |
425 | goto bad; | |
426 | } | |
e5568f75 | 427 | AUDIT_ARG(text, pnbuf); |
91447636 | 428 | if ( (pathlen > PSEMNAMLEN) ) { |
1c79356b A |
429 | error = ENAMETOOLONG; |
430 | goto bad; | |
431 | } | |
432 | ||
1c79356b A |
433 | #ifdef PSXSEM_NAME_RESTRICT |
434 | nameptr = pnbuf; | |
435 | if (*nameptr == '/') { | |
436 | while (*(nameptr++) == '/') { | |
437 | plen--; | |
438 | error = EINVAL; | |
439 | goto bad; | |
440 | } | |
441 | } else { | |
442 | error = EINVAL; | |
443 | goto bad; | |
444 | } | |
445 | #endif /* PSXSEM_NAME_RESTRICT */ | |
446 | ||
447 | plen = pathlen; | |
448 | nameptr = pnbuf; | |
449 | nd.psem_nameptr = nameptr; | |
450 | nd.psem_namelen = plen; | |
2d21ac55 | 451 | nd.psem_hash = 0; |
1c79356b A |
452 | |
453 | for (cp = nameptr, i=1; *cp != 0 && i <= plen; i++, cp++) { | |
454 | nd.psem_hash += (unsigned char)*cp * i; | |
455 | } | |
456 | ||
b0d623f7 A |
457 | /* |
458 | * attempt to allocate a new fp; if unsuccessful, the fp will be | |
459 | * left unmodified (NULL). | |
460 | */ | |
461 | error = falloc(p, &fp, &indx, vfs_context_current()); | |
462 | if (error) | |
463 | goto bad; | |
464 | ||
465 | /* | |
466 | * We allocate a new entry if we are less than the maximum | |
467 | * allowed and the one at the front of the LRU list is in use. | |
468 | * Otherwise we use the one at the front of the LRU list. | |
469 | */ | |
470 | MALLOC(pcp, struct psemcache *, sizeof(struct psemcache), M_SHM, M_WAITOK|M_ZERO); | |
471 | if (pcp == PSEMCACHE_NULL) { | |
472 | error = ENOMEM; | |
473 | goto bad; | |
474 | } | |
475 | ||
476 | MALLOC(new_pinfo, struct pseminfo *, sizeof(struct pseminfo), M_SHM, M_WAITOK|M_ZERO); | |
477 | if (new_pinfo == NULL) { | |
478 | error = ENOSPC; | |
479 | goto bad; | |
480 | } | |
481 | #if CONFIG_MACF | |
482 | mac_posixsem_label_init(new_pinfo); | |
483 | #endif | |
484 | ||
485 | /* | |
486 | * Provisionally create the semaphore in the new_pinfo; we have to do | |
487 | * this here to prevent locking later. We use the value of kret to | |
488 | * signal success or failure, which is why we set its default value | |
489 | * to KERN_INVALID_ADDRESS, above. | |
490 | */ | |
491 | ||
492 | fmode = FFLAGS(uap->oflag); | |
493 | ||
494 | if((fmode & O_CREAT)) { | |
495 | ||
496 | if((value < 0) || (value > SEM_VALUE_MAX)) { | |
497 | error = EINVAL; | |
498 | goto bad; | |
499 | } | |
500 | ||
501 | kret = semaphore_create(kernel_task, &new_pinfo->psem_semobject, SYNC_POLICY_FIFO, value); | |
502 | ||
503 | if (kret != KERN_SUCCESS) { | |
504 | switch (kret) { | |
505 | case KERN_RESOURCE_SHORTAGE: | |
506 | error = ENOMEM; | |
507 | break; | |
508 | case KERN_PROTECTION_FAILURE: | |
509 | error = EACCES; | |
510 | break; | |
511 | default: | |
512 | error = EINVAL; | |
513 | } | |
514 | goto bad; | |
515 | } | |
516 | } | |
517 | ||
518 | MALLOC(new_pnode, struct psemnode *, sizeof(struct psemnode), M_SHM, M_WAITOK|M_ZERO); | |
519 | if (new_pnode == NULL) { | |
520 | error = ENOSPC; | |
521 | goto bad; | |
522 | } | |
523 | ||
91447636 | 524 | PSEM_SUBSYS_LOCK(); |
1c79356b A |
525 | error = psem_cache_search(&pinfo, &nd, &pcache); |
526 | ||
490019cf | 527 | if (error == PSEMCACHE_NEGATIVE) { |
1c79356b | 528 | error = EINVAL; |
b0d623f7 | 529 | goto bad_locked; |
1c79356b | 530 | } |
490019cf A |
531 | |
532 | if (error == PSEMCACHE_FOUND) | |
1c79356b | 533 | incache = 1; |
490019cf A |
534 | else |
535 | incache = 0; | |
1c79356b | 536 | |
1c79356b A |
537 | cmode &= ALLPERMS; |
538 | ||
539 | if (((fmode & (O_CREAT | O_EXCL))==(O_CREAT | O_EXCL)) && incache) { | |
540 | /* sem exists and opened O_EXCL */ | |
541 | #if notyet | |
542 | if (pinfo->psem_flags & PSEM_INDELETE) { | |
543 | } | |
544 | #endif | |
e5568f75 | 545 | AUDIT_ARG(posix_ipc_perm, pinfo->psem_uid, |
91447636 | 546 | pinfo->psem_gid, pinfo->psem_mode); |
1c79356b | 547 | error = EEXIST; |
b0d623f7 | 548 | goto bad_locked; |
1c79356b A |
549 | } |
550 | if (((fmode & (O_CREAT | O_EXCL))== O_CREAT) && incache) { | |
551 | /* As per POSIX, O_CREAT has no effect */ | |
552 | fmode &= ~O_CREAT; | |
553 | } | |
554 | ||
91447636 | 555 | if ( (fmode & O_CREAT) ) { |
b0d623f7 A |
556 | /* create a new one (commit the allocation) */ |
557 | pinfo = new_pinfo; | |
1c79356b A |
558 | pinfo->psem_flags = PSEM_DEFINED | PSEM_INCREATE; |
559 | pinfo->psem_usecount = 1; | |
560 | pinfo->psem_mode = cmode; | |
6d2010ae A |
561 | pinfo->psem_uid = kauth_getuid(); |
562 | pinfo->psem_gid = kauth_getgid(); | |
0c530ab8 A |
563 | bcopy(pnbuf, &pinfo->psem_name[0], PSEMNAMLEN); |
564 | pinfo->psem_name[PSEMNAMLEN]= 0; | |
1c79356b A |
565 | pinfo->psem_flags &= ~PSEM_DEFINED; |
566 | pinfo->psem_flags |= PSEM_ALLOCATED; | |
39236c6e A |
567 | pinfo->psem_creator_pid = p->p_pid; |
568 | pinfo->psem_creator_uniqueid = p->p_uniqueid; | |
b0d623f7 | 569 | |
2d21ac55 A |
570 | #if CONFIG_MACF |
571 | error = mac_posixsem_check_create(kauth_cred_get(), nameptr); | |
572 | if (error) { | |
b0d623f7 | 573 | goto bad_locked; |
2d21ac55 A |
574 | } |
575 | mac_posixsem_label_associate(kauth_cred_get(), pinfo, nameptr); | |
576 | #endif | |
1c79356b A |
577 | } else { |
578 | /* semaphore should exist as it is without O_CREAT */ | |
579 | if (!incache) { | |
580 | error = ENOENT; | |
b0d623f7 | 581 | goto bad_locked; |
1c79356b A |
582 | } |
583 | if( pinfo->psem_flags & PSEM_INDELETE) { | |
584 | error = ENOENT; | |
b0d623f7 | 585 | goto bad_locked; |
1c79356b | 586 | } |
e5568f75 | 587 | AUDIT_ARG(posix_ipc_perm, pinfo->psem_uid, |
91447636 | 588 | pinfo->psem_gid, pinfo->psem_mode); |
2d21ac55 A |
589 | #if CONFIG_MACF |
590 | error = mac_posixsem_check_open(kauth_cred_get(), pinfo); | |
591 | if (error) { | |
b0d623f7 | 592 | goto bad_locked; |
2d21ac55 A |
593 | } |
594 | #endif | |
91447636 | 595 | if ( (error = psem_access(pinfo, fmode, kauth_cred_get())) ) { |
b0d623f7 | 596 | goto bad_locked; |
91447636 | 597 | } |
1c79356b | 598 | } |
1c79356b A |
599 | |
600 | if (!incache) { | |
b0d623f7 | 601 | /* if successful, this will consume the pcp */ |
91447636 | 602 | if ( (error = psem_cache_add(pinfo, &nd, pcp)) ) { |
b0d623f7 | 603 | goto bad_locked; |
1c79356b A |
604 | } |
605 | } | |
606 | pinfo->psem_flags &= ~PSEM_INCREATE; | |
607 | pinfo->psem_usecount++; | |
b0d623f7 | 608 | new_pnode->pinfo = pinfo; |
91447636 A |
609 | PSEM_SUBSYS_UNLOCK(); |
610 | ||
b0d623f7 A |
611 | /* |
612 | * if incache, we did not use the new pcp or the new pcp or the | |
613 | * new . and we must free them. | |
614 | */ | |
615 | if (incache) { | |
616 | FREE(pcp, M_SHM); | |
617 | pcp = PSEMCACHE_NULL; | |
618 | if (new_pinfo != PSEMINFO_NULL) { | |
619 | /* return value ignored - we can't _not_ do this */ | |
620 | (void)semaphore_destroy(kernel_task, new_pinfo->psem_semobject); | |
621 | #if CONFIG_MACF | |
622 | mac_posixsem_label_destroy(new_pinfo); | |
623 | #endif | |
624 | FREE(new_pinfo, M_SHM); | |
625 | new_pinfo = PSEMINFO_NULL; | |
626 | } | |
627 | } | |
628 | ||
91447636 | 629 | proc_fdlock(p); |
55e303ae | 630 | fp->f_flag = fmode & FMASK; |
1c79356b | 631 | fp->f_ops = &psemops; |
b0d623f7 | 632 | fp->f_data = (caddr_t)new_pnode; |
6601e61a | 633 | procfdtbl_releasefd(p, indx, NULL); |
91447636 A |
634 | fp_drop(p, indx, fp, 1); |
635 | proc_fdunlock(p); | |
636 | ||
637 | *retval = CAST_USER_ADDR_T(indx); | |
55e303ae | 638 | FREE_ZONE(pnbuf, MAXPATHLEN, M_NAMEI); |
1c79356b A |
639 | return (0); |
640 | ||
b0d623f7 A |
641 | bad_locked: |
642 | PSEM_SUBSYS_UNLOCK(); | |
643 | bad: | |
644 | if (pcp != PSEMCACHE_NULL) | |
645 | FREE(pcp, M_SHM); | |
646 | ||
647 | if (new_pnode != PSEMNODE_NULL) | |
648 | FREE(new_pnode, M_SHM); | |
649 | ||
650 | if (fp != NULL) | |
651 | fp_free(p, indx, fp); | |
652 | ||
653 | if (new_pinfo != PSEMINFO_NULL) { | |
654 | /* | |
655 | * kret signals whether or not we successfully created a | |
656 | * Mach semaphore for this semaphore; if so, we need to | |
657 | * destroy it here. | |
658 | */ | |
659 | if (kret == KERN_SUCCESS) { | |
660 | /* return value ignored - we can't _not_ do this */ | |
661 | (void)semaphore_destroy(kernel_task, new_pinfo->psem_semobject); | |
662 | } | |
2d21ac55 | 663 | #if CONFIG_MACF |
b0d623f7 | 664 | mac_posixsem_label_destroy(new_pinfo); |
2d21ac55 | 665 | #endif |
b0d623f7 | 666 | FREE(new_pinfo, M_SHM); |
2d21ac55 | 667 | } |
b0d623f7 A |
668 | |
669 | if (pnbuf != NULL) | |
670 | FREE_ZONE(pnbuf, MAXPATHLEN, M_NAMEI); | |
1c79356b A |
671 | return (error); |
672 | } | |
673 | ||
91447636 A |
674 | /* |
675 | * XXX This code is repeated in several places | |
676 | */ | |
677 | static int | |
678 | psem_access(struct pseminfo *pinfo, int mode, kauth_cred_t cred) | |
1c79356b | 679 | { |
6d2010ae A |
680 | int mode_req = ((mode & FREAD) ? S_IRUSR : 0) | |
681 | ((mode & FWRITE) ? S_IWUSR : 0); | |
1c79356b A |
682 | |
683 | /* Otherwise, user id 0 always gets access. */ | |
91447636 | 684 | if (!suser(cred, NULL)) |
1c79356b A |
685 | return (0); |
686 | ||
6d2010ae | 687 | return(posix_cred_access(cred, pinfo->psem_uid, pinfo->psem_gid, pinfo->psem_mode, mode_req)); |
1c79356b A |
688 | } |
689 | ||
490019cf A |
690 | static int |
691 | psem_unlink_internal(struct pseminfo *pinfo, struct psemcache *pcache) | |
692 | { | |
693 | PSEM_SUBSYS_ASSERT_HELD(); | |
694 | ||
695 | if (!pinfo || !pcache) | |
696 | return EINVAL; | |
697 | ||
698 | if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED)) == 0) | |
699 | return EINVAL; | |
700 | ||
701 | if (pinfo->psem_flags & PSEM_INDELETE) | |
702 | return 0; | |
703 | ||
704 | AUDIT_ARG(posix_ipc_perm, pinfo->psem_uid, pinfo->psem_gid, | |
705 | pinfo->psem_mode); | |
706 | ||
707 | pinfo->psem_flags |= PSEM_INDELETE; | |
708 | pinfo->psem_usecount--; | |
709 | ||
710 | if (!pinfo->psem_usecount) { | |
711 | psem_delete(pinfo); | |
712 | FREE(pinfo,M_SHM); | |
713 | } else { | |
714 | pinfo->psem_flags |= PSEM_REMOVED; | |
715 | } | |
716 | ||
717 | psem_cache_delete(pcache); | |
718 | FREE(pcache, M_SHM); | |
719 | return 0; | |
720 | } | |
721 | ||
722 | ||
1c79356b | 723 | int |
b0d623f7 | 724 | sem_unlink(__unused proc_t p, struct sem_unlink_args *uap, __unused int32_t *retval) |
1c79356b | 725 | { |
91447636 | 726 | size_t i; |
1c79356b A |
727 | int error=0; |
728 | struct psemname nd; | |
729 | struct pseminfo *pinfo; | |
1c79356b A |
730 | char * nameptr; |
731 | char * cp; | |
490019cf A |
732 | char * pnbuf; |
733 | size_t pathlen; | |
1c79356b | 734 | struct psemcache *pcache = PSEMCACHE_NULL; |
1c79356b A |
735 | |
736 | pinfo = PSEMINFO_NULL; | |
737 | ||
91447636 A |
738 | MALLOC_ZONE(pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK); |
739 | if (pnbuf == NULL) { | |
740 | return(ENOSPC); /* XXX non-standard */ | |
741 | } | |
1c79356b | 742 | pathlen = MAXPATHLEN; |
91447636 | 743 | error = copyinstr(uap->name, pnbuf, MAXPATHLEN, &pathlen); |
1c79356b A |
744 | if (error) { |
745 | goto bad; | |
746 | } | |
e5568f75 | 747 | AUDIT_ARG(text, pnbuf); |
1c79356b A |
748 | if (pathlen > PSEMNAMLEN) { |
749 | error = ENAMETOOLONG; | |
750 | goto bad; | |
751 | } | |
752 | ||
490019cf | 753 | nameptr = pnbuf; |
1c79356b A |
754 | |
755 | #ifdef PSXSEM_NAME_RESTRICT | |
1c79356b A |
756 | if (*nameptr == '/') { |
757 | while (*(nameptr++) == '/') { | |
490019cf | 758 | pathlen--; |
1c79356b A |
759 | error = EINVAL; |
760 | goto bad; | |
761 | } | |
762 | } else { | |
763 | error = EINVAL; | |
764 | goto bad; | |
765 | } | |
766 | #endif /* PSXSEM_NAME_RESTRICT */ | |
767 | ||
1c79356b | 768 | nd.psem_nameptr = nameptr; |
490019cf | 769 | nd.psem_namelen = pathlen; |
1c79356b A |
770 | nd. psem_hash =0; |
771 | ||
490019cf | 772 | for (cp = nameptr, i=1; *cp != 0 && i <= pathlen; i++, cp++) { |
1c79356b A |
773 | nd.psem_hash += (unsigned char)*cp * i; |
774 | } | |
775 | ||
91447636 | 776 | PSEM_SUBSYS_LOCK(); |
1c79356b A |
777 | error = psem_cache_search(&pinfo, &nd, &pcache); |
778 | ||
490019cf | 779 | if (error != PSEMCACHE_FOUND) { |
91447636 | 780 | PSEM_SUBSYS_UNLOCK(); |
1c79356b A |
781 | error = EINVAL; |
782 | goto bad; | |
783 | ||
784 | } | |
490019cf | 785 | |
2d21ac55 A |
786 | #if CONFIG_MACF |
787 | error = mac_posixsem_check_unlink(kauth_cred_get(), pinfo, nameptr); | |
788 | if (error) { | |
789 | PSEM_SUBSYS_UNLOCK(); | |
790 | goto bad; | |
791 | } | |
792 | #endif | |
91447636 A |
793 | if ( (error = psem_access(pinfo, pinfo->psem_mode, kauth_cred_get())) ) { |
794 | PSEM_SUBSYS_UNLOCK(); | |
1c79356b | 795 | goto bad; |
91447636 | 796 | } |
1c79356b | 797 | |
490019cf | 798 | error = psem_unlink_internal(pinfo, pcache); |
91447636 | 799 | PSEM_SUBSYS_UNLOCK(); |
490019cf | 800 | |
1c79356b | 801 | bad: |
55e303ae | 802 | FREE_ZONE(pnbuf, MAXPATHLEN, M_NAMEI); |
490019cf | 803 | return error; |
1c79356b A |
804 | } |
805 | ||
1c79356b | 806 | int |
b0d623f7 | 807 | sem_close(proc_t p, struct sem_close_args *uap, __unused int32_t *retval) |
1c79356b | 808 | { |
b0d623f7 | 809 | int fd = CAST_DOWN_EXPLICIT(int,uap->sem); |
91447636 | 810 | struct fileproc *fp; |
1c79356b A |
811 | int error = 0; |
812 | ||
e5568f75 | 813 | AUDIT_ARG(fd, fd); /* XXX This seems wrong; uap->sem is a pointer */ |
91447636 A |
814 | |
815 | proc_fdlock(p); | |
816 | error = fp_lookup(p,fd, &fp, 1); | |
817 | if (error) { | |
818 | proc_fdunlock(p); | |
1c79356b | 819 | return(error); |
91447636 | 820 | } |
6d2010ae | 821 | procfdtbl_markclosefd(p, fd); |
2d21ac55 | 822 | fileproc_drain(p, fp); |
91447636 A |
823 | fdrelse(p, fd); |
824 | error = closef_locked(fp, fp->f_fglob, p); | |
39236c6e | 825 | fileproc_free(fp); |
91447636 A |
826 | proc_fdunlock(p); |
827 | return(error); | |
1c79356b A |
828 | } |
829 | ||
1c79356b | 830 | int |
b0d623f7 | 831 | sem_wait(proc_t p, struct sem_wait_args *uap, int32_t *retval) |
2d21ac55 A |
832 | { |
833 | __pthread_testcancel(1); | |
834 | return(sem_wait_nocancel(p, (struct sem_wait_nocancel_args *)uap, retval)); | |
835 | } | |
836 | ||
837 | int | |
b0d623f7 | 838 | sem_wait_nocancel(proc_t p, struct sem_wait_nocancel_args *uap, __unused int32_t *retval) |
1c79356b | 839 | { |
b0d623f7 | 840 | int fd = CAST_DOWN_EXPLICIT(int,uap->sem); |
91447636 | 841 | struct fileproc *fp; |
1c79356b A |
842 | struct pseminfo * pinfo; |
843 | struct psemnode * pnode ; | |
844 | kern_return_t kret; | |
845 | int error; | |
846 | ||
91447636 A |
847 | error = fp_getfpsem(p, fd, &fp, &pnode); |
848 | if (error) | |
1c79356b | 849 | return (error); |
91447636 A |
850 | if (((pnode = (struct psemnode *)fp->f_data)) == PSEMNODE_NULL ) { |
851 | error = EINVAL; | |
852 | goto out; | |
853 | } | |
854 | PSEM_SUBSYS_LOCK(); | |
855 | if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) { | |
856 | PSEM_SUBSYS_UNLOCK(); | |
857 | error = EINVAL; | |
858 | goto out; | |
859 | } | |
1c79356b A |
860 | if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED)) |
861 | != PSEM_ALLOCATED) { | |
91447636 A |
862 | PSEM_SUBSYS_UNLOCK(); |
863 | error = EINVAL; | |
864 | goto out; | |
1c79356b | 865 | } |
2d21ac55 A |
866 | #if CONFIG_MACF |
867 | error = mac_posixsem_check_wait(kauth_cred_get(), pinfo); | |
868 | if (error) { | |
869 | PSEM_SUBSYS_UNLOCK(); | |
870 | goto out; | |
871 | } | |
872 | #endif | |
91447636 | 873 | PSEM_SUBSYS_UNLOCK(); |
1c79356b A |
874 | kret = semaphore_wait(pinfo->psem_semobject); |
875 | switch (kret) { | |
876 | case KERN_INVALID_ADDRESS: | |
877 | case KERN_PROTECTION_FAILURE: | |
91447636 A |
878 | error = EACCES; |
879 | break; | |
1c79356b A |
880 | case KERN_ABORTED: |
881 | case KERN_OPERATION_TIMED_OUT: | |
91447636 A |
882 | error = EINTR; |
883 | break; | |
1c79356b | 884 | case KERN_SUCCESS: |
91447636 A |
885 | error = 0; |
886 | break; | |
1c79356b | 887 | default: |
91447636 A |
888 | error = EINVAL; |
889 | break; | |
1c79356b | 890 | } |
91447636 A |
891 | out: |
892 | fp_drop(p, fd, fp, 0); | |
893 | return(error); | |
1c79356b | 894 | |
91447636 | 895 | } |
1c79356b A |
896 | |
897 | int | |
b0d623f7 | 898 | sem_trywait(proc_t p, struct sem_trywait_args *uap, __unused int32_t *retval) |
1c79356b | 899 | { |
b0d623f7 | 900 | int fd = CAST_DOWN_EXPLICIT(int,uap->sem); |
91447636 | 901 | struct fileproc *fp; |
1c79356b A |
902 | struct pseminfo * pinfo; |
903 | struct psemnode * pnode ; | |
904 | kern_return_t kret; | |
905 | mach_timespec_t wait_time; | |
906 | int error; | |
907 | ||
91447636 A |
908 | error = fp_getfpsem(p, fd, &fp, &pnode); |
909 | if (error) | |
1c79356b | 910 | return (error); |
91447636 A |
911 | if (((pnode = (struct psemnode *)fp->f_data)) == PSEMNODE_NULL ) { |
912 | error = EINVAL; | |
913 | goto out; | |
914 | } | |
915 | PSEM_SUBSYS_LOCK(); | |
916 | if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) { | |
917 | PSEM_SUBSYS_UNLOCK(); | |
918 | error = EINVAL; | |
919 | goto out; | |
920 | } | |
1c79356b A |
921 | if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED)) |
922 | != PSEM_ALLOCATED) { | |
91447636 A |
923 | PSEM_SUBSYS_UNLOCK(); |
924 | error = EINVAL; | |
925 | goto out; | |
1c79356b | 926 | } |
2d21ac55 A |
927 | #if CONFIG_MACF |
928 | error = mac_posixsem_check_wait(kauth_cred_get(), pinfo); | |
929 | if (error) { | |
930 | PSEM_SUBSYS_UNLOCK(); | |
931 | goto out; | |
932 | } | |
933 | #endif | |
91447636 | 934 | PSEM_SUBSYS_UNLOCK(); |
1c79356b A |
935 | wait_time.tv_sec = 0; |
936 | wait_time.tv_nsec = 0; | |
937 | ||
938 | kret = semaphore_timedwait(pinfo->psem_semobject, MACH_TIMESPEC_ZERO); | |
939 | switch (kret) { | |
940 | case KERN_INVALID_ADDRESS: | |
941 | case KERN_PROTECTION_FAILURE: | |
91447636 A |
942 | error = EINVAL; |
943 | break; | |
1c79356b | 944 | case KERN_ABORTED: |
91447636 A |
945 | error = EINTR; |
946 | break; | |
1c79356b | 947 | case KERN_OPERATION_TIMED_OUT: |
91447636 A |
948 | error = EAGAIN; |
949 | break; | |
1c79356b | 950 | case KERN_SUCCESS: |
91447636 A |
951 | error = 0; |
952 | break; | |
1c79356b | 953 | default: |
91447636 A |
954 | error = EINVAL; |
955 | break; | |
1c79356b | 956 | } |
91447636 A |
957 | out: |
958 | fp_drop(p, fd, fp, 0); | |
959 | return(error); | |
1c79356b A |
960 | } |
961 | ||
1c79356b | 962 | int |
b0d623f7 | 963 | sem_post(proc_t p, struct sem_post_args *uap, __unused int32_t *retval) |
1c79356b | 964 | { |
b0d623f7 | 965 | int fd = CAST_DOWN_EXPLICIT(int,uap->sem); |
91447636 | 966 | struct fileproc *fp; |
1c79356b A |
967 | struct pseminfo * pinfo; |
968 | struct psemnode * pnode ; | |
969 | kern_return_t kret; | |
970 | int error; | |
971 | ||
91447636 A |
972 | error = fp_getfpsem(p, fd, &fp, &pnode); |
973 | if (error) | |
1c79356b | 974 | return (error); |
91447636 A |
975 | if (((pnode = (struct psemnode *)fp->f_data)) == PSEMNODE_NULL ) { |
976 | error = EINVAL; | |
977 | goto out; | |
978 | } | |
979 | PSEM_SUBSYS_LOCK(); | |
980 | if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) { | |
981 | PSEM_SUBSYS_UNLOCK(); | |
982 | error = EINVAL; | |
983 | goto out; | |
984 | } | |
1c79356b A |
985 | if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED)) |
986 | != PSEM_ALLOCATED) { | |
91447636 A |
987 | PSEM_SUBSYS_UNLOCK(); |
988 | error = EINVAL; | |
989 | goto out; | |
1c79356b | 990 | } |
2d21ac55 A |
991 | #if CONFIG_MACF |
992 | error = mac_posixsem_check_post(kauth_cred_get(), pinfo); | |
993 | if (error) { | |
994 | PSEM_SUBSYS_UNLOCK(); | |
995 | goto out; | |
996 | } | |
997 | #endif | |
91447636 | 998 | PSEM_SUBSYS_UNLOCK(); |
1c79356b A |
999 | kret = semaphore_signal(pinfo->psem_semobject); |
1000 | switch (kret) { | |
1001 | case KERN_INVALID_ADDRESS: | |
1002 | case KERN_PROTECTION_FAILURE: | |
91447636 A |
1003 | error = EINVAL; |
1004 | break; | |
1c79356b A |
1005 | case KERN_ABORTED: |
1006 | case KERN_OPERATION_TIMED_OUT: | |
91447636 A |
1007 | error = EINTR; |
1008 | break; | |
1c79356b | 1009 | case KERN_SUCCESS: |
91447636 A |
1010 | error = 0; |
1011 | break; | |
1c79356b | 1012 | default: |
91447636 A |
1013 | error = EINVAL; |
1014 | break; | |
1c79356b | 1015 | } |
91447636 A |
1016 | out: |
1017 | fp_drop(p, fd, fp, 0); | |
1018 | return(error); | |
1c79356b A |
1019 | } |
1020 | ||
9bccf70c | 1021 | static int |
2d21ac55 | 1022 | psem_close(struct psemnode *pnode, __unused int flags) |
1c79356b A |
1023 | { |
1024 | int error=0; | |
2d21ac55 | 1025 | struct pseminfo *pinfo; |
1c79356b | 1026 | |
91447636 A |
1027 | PSEM_SUBSYS_LOCK(); |
1028 | if ((pinfo = pnode->pinfo) == PSEMINFO_NULL){ | |
1029 | PSEM_SUBSYS_UNLOCK(); | |
1c79356b | 1030 | return(EINVAL); |
91447636 | 1031 | } |
1c79356b A |
1032 | |
1033 | if ((pinfo->psem_flags & PSEM_ALLOCATED) != PSEM_ALLOCATED) { | |
91447636 | 1034 | PSEM_SUBSYS_UNLOCK(); |
1c79356b A |
1035 | return(EINVAL); |
1036 | } | |
1037 | #if DIAGNOSTIC | |
1038 | if(!pinfo->psem_usecount) { | |
1039 | kprintf("negative usecount in psem_close\n"); | |
1040 | } | |
1041 | #endif /* DIAGNOSTIC */ | |
1042 | pinfo->psem_usecount--; | |
1043 | ||
1044 | if ((pinfo->psem_flags & PSEM_REMOVED) && !pinfo->psem_usecount) { | |
91447636 A |
1045 | PSEM_SUBSYS_UNLOCK(); |
1046 | /* lock dropped as only semaphore is destroyed here */ | |
1c79356b | 1047 | error = psem_delete(pinfo); |
91447636 A |
1048 | FREE(pinfo,M_SHM); |
1049 | } else { | |
1050 | PSEM_SUBSYS_UNLOCK(); | |
1c79356b | 1051 | } |
91447636 A |
1052 | /* subsystem lock is dropped when we get here */ |
1053 | FREE(pnode, M_SHM); | |
1c79356b A |
1054 | return (error); |
1055 | } | |
1056 | ||
9bccf70c | 1057 | static int |
2d21ac55 | 1058 | psem_closefile(struct fileglob *fg, __unused vfs_context_t ctx) |
9bccf70c | 1059 | { |
91447636 | 1060 | int error; |
9bccf70c | 1061 | |
2d21ac55 A |
1062 | /* |
1063 | * Not locked as psem_close is called only from here and is locked | |
1064 | * properly | |
1065 | */ | |
1066 | error = psem_close(((struct psemnode *)fg->fg_data), fg->fg_flag); | |
91447636 A |
1067 | |
1068 | return(error); | |
9bccf70c A |
1069 | } |
1070 | ||
91447636 | 1071 | static int |
1c79356b A |
1072 | psem_delete(struct pseminfo * pinfo) |
1073 | { | |
1074 | kern_return_t kret; | |
1075 | ||
1076 | kret = semaphore_destroy(kernel_task, pinfo->psem_semobject); | |
2d21ac55 A |
1077 | #if CONFIG_MACF |
1078 | mac_posixsem_label_destroy(pinfo); | |
1079 | #endif | |
1c79356b A |
1080 | |
1081 | switch (kret) { | |
1082 | case KERN_INVALID_ADDRESS: | |
1083 | case KERN_PROTECTION_FAILURE: | |
1084 | return (EINVAL); | |
1085 | case KERN_ABORTED: | |
1086 | case KERN_OPERATION_TIMED_OUT: | |
1087 | return (EINTR); | |
1088 | case KERN_SUCCESS: | |
1089 | return(0); | |
1090 | default: | |
1091 | return (EINVAL); | |
1092 | } | |
1c79356b A |
1093 | } |
1094 | ||
9bccf70c | 1095 | static int |
91447636 | 1096 | psem_read(__unused struct fileproc *fp, __unused struct uio *uio, |
2d21ac55 | 1097 | __unused int flags, __unused vfs_context_t ctx) |
1c79356b | 1098 | { |
91447636 | 1099 | return(ENOTSUP); |
1c79356b | 1100 | } |
9bccf70c A |
1101 | |
1102 | static int | |
91447636 | 1103 | psem_write(__unused struct fileproc *fp, __unused struct uio *uio, |
2d21ac55 | 1104 | __unused int flags, __unused vfs_context_t ctx) |
1c79356b | 1105 | { |
91447636 | 1106 | return(ENOTSUP); |
1c79356b | 1107 | } |
9bccf70c A |
1108 | |
1109 | static int | |
91447636 | 1110 | psem_ioctl(__unused struct fileproc *fp, __unused u_long com, |
2d21ac55 | 1111 | __unused caddr_t data, __unused vfs_context_t ctx) |
1c79356b | 1112 | { |
91447636 | 1113 | return(ENOTSUP); |
1c79356b | 1114 | } |
9bccf70c A |
1115 | |
1116 | static int | |
91447636 | 1117 | psem_select(__unused struct fileproc *fp, __unused int which, |
2d21ac55 | 1118 | __unused void *wql, __unused vfs_context_t ctx) |
1c79356b | 1119 | { |
91447636 | 1120 | return(ENOTSUP); |
1c79356b | 1121 | } |
55e303ae A |
1122 | |
1123 | static int | |
39037602 | 1124 | psem_kqfilter(__unused struct fileproc *fp, struct knote *kn, |
2d21ac55 | 1125 | __unused vfs_context_t ctx) |
55e303ae | 1126 | { |
39037602 A |
1127 | kn->kn_flags = EV_ERROR; |
1128 | kn->kn_data = ENOTSUP; | |
1129 | return 0; | |
55e303ae A |
1130 | } |
1131 | ||
0c530ab8 A |
1132 | int |
1133 | fill_pseminfo(struct psemnode *pnode, struct psem_info * info) | |
1134 | { | |
2d21ac55 A |
1135 | struct pseminfo *pinfo; |
1136 | struct vinfo_stat *sb; | |
0c530ab8 A |
1137 | |
1138 | PSEM_SUBSYS_LOCK(); | |
1139 | if ((pinfo = pnode->pinfo) == PSEMINFO_NULL){ | |
1140 | PSEM_SUBSYS_UNLOCK(); | |
1141 | return(EINVAL); | |
1142 | } | |
1143 | ||
1144 | #if 0 | |
1145 | if ((pinfo->psem_flags & PSEM_ALLOCATED) != PSEM_ALLOCATED) { | |
1146 | PSEM_SUBSYS_UNLOCK(); | |
1147 | return(EINVAL); | |
1148 | } | |
1149 | #endif | |
1150 | ||
1151 | sb = &info->psem_stat; | |
2d21ac55 | 1152 | bzero(sb, sizeof(struct vinfo_stat)); |
0c530ab8 | 1153 | |
2d21ac55 A |
1154 | sb->vst_mode = pinfo->psem_mode; |
1155 | sb->vst_uid = pinfo->psem_uid; | |
1156 | sb->vst_gid = pinfo->psem_gid; | |
1157 | sb->vst_size = pinfo->psem_usecount; | |
0c530ab8 A |
1158 | bcopy(&pinfo->psem_name[0], &info->psem_name[0], PSEMNAMLEN+1); |
1159 | ||
1160 | PSEM_SUBSYS_UNLOCK(); | |
1161 | return(0); | |
1162 | } | |
1163 | ||
2d21ac55 A |
1164 | #if CONFIG_MACF |
1165 | void | |
1166 | psem_label_associate(struct fileproc *fp, struct vnode *vp, vfs_context_t ctx) | |
1167 | { | |
1168 | struct psemnode *pnode; | |
1169 | struct pseminfo *psem; | |
1170 | ||
1171 | PSEM_SUBSYS_LOCK(); | |
1172 | pnode = (struct psemnode *)fp->f_fglob->fg_data; | |
1173 | if (pnode != NULL) { | |
1174 | psem = pnode->pinfo; | |
1175 | if (psem != NULL) | |
1176 | mac_posixsem_vnode_label_associate( | |
1177 | vfs_context_ucred(ctx), psem, psem->psem_label, | |
1178 | vp, vp->v_label); | |
1179 | } | |
1180 | PSEM_SUBSYS_UNLOCK(); | |
1181 | } | |
1182 | #endif | |
1183 |