]>
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@ |
0a7de745 | 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. | |
0a7de745 | 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. | |
0a7de745 | 17 | * |
2d21ac55 A |
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. | |
0a7de745 | 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 | |
0a7de745 | 94 | #define PSEMNAMLEN 31 /* maximum name segment length we bother with */ |
1c79356b A |
95 | |
96 | struct pseminfo { | |
0a7de745 A |
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 */ | |
103 | semaphore_t psem_semobject; | |
104 | struct label * psem_label; | |
105 | pid_t psem_creator_pid; | |
106 | uint64_t psem_creator_uniqueid; | |
1c79356b A |
107 | }; |
108 | #define PSEMINFO_NULL (struct pseminfo *)0 | |
109 | ||
0a7de745 A |
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 */ | |
1c79356b A |
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 | ||
0a7de745 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 */ | |
1c79356b A |
138 | }; |
139 | ||
140 | struct psemname { | |
0a7de745 A |
141 | char *psem_nameptr; /* pointer to looked up name */ |
142 | long psem_namelen; /* length of looked up component */ | |
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]) | |
0a7de745 A |
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 */ | |
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"); | |
0a7de745 | 165 | SYSCTL_LONG(_kern_posix_sem, OID_AUTO, max, CTLFLAG_RW | CTLFLAG_LOCKED, &posix_sem_max, "max"); |
91447636 | 166 | |
0a7de745 | 167 | struct psemstats psemstats; /* cache effectiveness statistics */ |
1c79356b | 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 **, | |
0a7de745 | 171 | struct psemname *, struct psemcache **); |
91447636 | 172 | static int psem_delete(struct pseminfo * pinfo); |
1c79356b | 173 | |
0a7de745 | 174 | static int psem_closefile(struct fileglob *fp, vfs_context_t ctx); |
490019cf | 175 | static int psem_unlink_internal(struct pseminfo *pinfo, struct psemcache *pcache); |
1c79356b | 176 | |
39236c6e | 177 | static const struct fileops psemops = { |
cb323159 A |
178 | .fo_type = DTYPE_PSXSEM, |
179 | .fo_read = fo_no_read, | |
180 | .fo_write = fo_no_write, | |
181 | .fo_ioctl = fo_no_ioctl, | |
182 | .fo_select = fo_no_select, | |
183 | .fo_close = psem_closefile, | |
184 | .fo_drain = fo_no_drain, | |
185 | .fo_kqfilter = fo_no_kqfilter, | |
39236c6e | 186 | }; |
91447636 A |
187 | |
188 | static lck_grp_t *psx_sem_subsys_lck_grp; | |
189 | static lck_grp_attr_t *psx_sem_subsys_lck_grp_attr; | |
190 | static lck_attr_t *psx_sem_subsys_lck_attr; | |
191 | static lck_mtx_t psx_sem_subsys_mutex; | |
192 | ||
193 | #define PSEM_SUBSYS_LOCK() lck_mtx_lock(& psx_sem_subsys_mutex) | |
194 | #define PSEM_SUBSYS_UNLOCK() lck_mtx_unlock(& psx_sem_subsys_mutex) | |
39037602 | 195 | #define PSEM_SUBSYS_ASSERT_HELD() LCK_MTX_ASSERT(&psx_sem_subsys_mutex, LCK_MTX_ASSERT_OWNED) |
91447636 A |
196 | |
197 | ||
198 | static int psem_cache_add(struct pseminfo *psemp, struct psemname *pnp, struct psemcache *pcp); | |
490019cf A |
199 | static void psem_cache_delete(struct psemcache *pcp); |
200 | int psem_cache_purge_all(proc_t); | |
201 | ||
202 | ||
91447636 A |
203 | /* Initialize the mutex governing access to the posix sem subsystem */ |
204 | __private_extern__ void | |
205 | psem_lock_init( void ) | |
206 | { | |
0a7de745 | 207 | psx_sem_subsys_lck_grp_attr = lck_grp_attr_alloc_init(); |
91447636 | 208 | |
0a7de745 | 209 | psx_sem_subsys_lck_grp = lck_grp_alloc_init("posix shared memory", psx_sem_subsys_lck_grp_attr); |
91447636 | 210 | |
0a7de745 A |
211 | psx_sem_subsys_lck_attr = lck_attr_alloc_init(); |
212 | lck_mtx_init(&psx_sem_subsys_mutex, psx_sem_subsys_lck_grp, psx_sem_subsys_lck_attr); | |
91447636 | 213 | } |
1c79356b A |
214 | |
215 | /* | |
0a7de745 A |
216 | * Lookup an entry in the cache |
217 | * | |
218 | * | |
1c79356b A |
219 | * status of -1 is returned if matches |
220 | * If the lookup determines that the name does not exist | |
221 | * (negative cacheing), a status of ENOENT is returned. If the lookup | |
222 | * fails, a status of zero is returned. | |
223 | */ | |
224 | ||
9bccf70c | 225 | static int |
2d21ac55 | 226 | psem_cache_search(struct pseminfo **psemp, struct psemname *pnp, |
0a7de745 | 227 | struct psemcache **pcache) |
1c79356b | 228 | { |
91447636 A |
229 | struct psemcache *pcp, *nnp; |
230 | struct psemhashhead *pcpp; | |
1c79356b A |
231 | |
232 | if (pnp->psem_namelen > PSEMNAMLEN) { | |
233 | psemstats.longnames++; | |
490019cf | 234 | return PSEMCACHE_NOTFOUND; |
1c79356b A |
235 | } |
236 | ||
237 | pcpp = PSEMHASH(pnp); | |
238 | for (pcp = pcpp->lh_first; pcp != 0; pcp = nnp) { | |
239 | nnp = pcp->psem_hash.le_next; | |
240 | if (pcp->psem_nlen == pnp->psem_namelen && | |
0a7de745 | 241 | !bcmp(pcp->psem_name, pnp->psem_nameptr, (u_int)pcp->psem_nlen)) { |
1c79356b | 242 | break; |
0a7de745 | 243 | } |
1c79356b A |
244 | } |
245 | ||
246 | if (pcp == 0) { | |
247 | psemstats.miss++; | |
490019cf | 248 | return PSEMCACHE_NOTFOUND; |
1c79356b A |
249 | } |
250 | ||
251 | /* We found a "positive" match, return the vnode */ | |
0a7de745 | 252 | if (pcp->pseminfo) { |
1c79356b A |
253 | psemstats.goodhits++; |
254 | /* TOUCH(ncp); */ | |
255 | *psemp = pcp->pseminfo; | |
256 | *pcache = pcp; | |
490019cf | 257 | return PSEMCACHE_FOUND; |
1c79356b A |
258 | } |
259 | ||
260 | /* | |
261 | * We found a "negative" match, ENOENT notifies client of this match. | |
262 | * The nc_vpid field records whether this is a whiteout. | |
263 | */ | |
264 | psemstats.neghits++; | |
490019cf | 265 | return PSEMCACHE_NEGATIVE; |
1c79356b A |
266 | } |
267 | ||
268 | /* | |
269 | * Add an entry to the cache. | |
270 | */ | |
9bccf70c | 271 | static int |
91447636 | 272 | psem_cache_add(struct pseminfo *psemp, struct psemname *pnp, struct psemcache *pcp) |
1c79356b | 273 | { |
91447636 | 274 | struct psemhashhead *pcpp; |
1c79356b A |
275 | struct pseminfo *dpinfo; |
276 | struct psemcache *dpcp; | |
277 | ||
278 | #if DIAGNOSTIC | |
0a7de745 | 279 | if (pnp->psem_namelen > PSEMNAMLEN) { |
1c79356b | 280 | panic("cache_enter: name too long"); |
0a7de745 | 281 | } |
1c79356b A |
282 | #endif |
283 | ||
91447636 | 284 | |
1c79356b | 285 | /* if the entry has already been added by some one else return */ |
490019cf A |
286 | if (psem_cache_search(&dpinfo, pnp, &dpcp) == PSEMCACHE_FOUND) { |
287 | return EEXIST; | |
1c79356b | 288 | } |
0a7de745 | 289 | if (psemnument >= posix_sem_max) { |
490019cf | 290 | return ENOSPC; |
0a7de745 | 291 | } |
1c79356b | 292 | psemnument++; |
1c79356b A |
293 | /* |
294 | * Fill in cache info, if vp is NULL this is a "negative" cache entry. | |
295 | * For negative entries, we have to record whether it is a whiteout. | |
296 | * the whiteout flag is stored in the nc_vpid field which is | |
297 | * otherwise unused. | |
298 | */ | |
299 | pcp->pseminfo = psemp; | |
300 | pcp->psem_nlen = pnp->psem_namelen; | |
301 | bcopy(pnp->psem_nameptr, pcp->psem_name, (unsigned)pcp->psem_nlen); | |
302 | pcpp = PSEMHASH(pnp); | |
303 | #if DIAGNOSTIC | |
304 | { | |
91447636 | 305 | struct psemcache *p; |
1c79356b | 306 | |
0a7de745 A |
307 | for (p = pcpp->lh_first; p != 0; p = p->psem_hash.le_next) { |
308 | if (p == pcp) { | |
1c79356b | 309 | panic("psem:cache_enter duplicate"); |
0a7de745 A |
310 | } |
311 | } | |
1c79356b A |
312 | } |
313 | #endif | |
314 | LIST_INSERT_HEAD(pcpp, pcp, psem_hash); | |
490019cf | 315 | return 0; |
1c79356b A |
316 | } |
317 | ||
318 | /* | |
319 | * Name cache initialization, from vfs_init() when we are booting | |
320 | */ | |
321 | void | |
91447636 | 322 | psem_cache_init(void) |
1c79356b | 323 | { |
2d21ac55 | 324 | psemhashtbl = hashinit(posix_sem_max / 2, M_SHM, &psemhash); |
1c79356b A |
325 | } |
326 | ||
9bccf70c | 327 | static void |
91447636 | 328 | psem_cache_delete(struct psemcache *pcp) |
9bccf70c A |
329 | { |
330 | #if DIAGNOSTIC | |
0a7de745 | 331 | if (pcp->psem_hash.le_prev == 0) { |
9bccf70c | 332 | panic("psem namecache purge le_prev"); |
0a7de745 A |
333 | } |
334 | if (pcp->psem_hash.le_next == pcp) { | |
9bccf70c | 335 | panic("namecache purge le_next"); |
0a7de745 | 336 | } |
9bccf70c A |
337 | #endif /* DIAGNOSTIC */ |
338 | LIST_REMOVE(pcp, psem_hash); | |
0a7de745 | 339 | pcp->psem_hash.le_prev = NULL; |
9bccf70c A |
340 | psemnument--; |
341 | } | |
342 | ||
1c79356b | 343 | /* |
490019cf A |
344 | * Remove all cached psem entries. Open semaphores (with a positive refcount) |
345 | * will continue to exist, but their cache entries tying them to a particular | |
346 | * name/path will be removed making all future lookups on the name fail. | |
1c79356b | 347 | */ |
490019cf A |
348 | int |
349 | psem_cache_purge_all(__unused proc_t p) | |
1c79356b | 350 | { |
490019cf | 351 | struct psemcache *pcp, *tmppcp; |
1c79356b | 352 | struct psemhashhead *pcpp; |
490019cf | 353 | int error = 0; |
1c79356b | 354 | |
0a7de745 | 355 | if (kauth_cred_issuser(kauth_cred_get()) == 0) { |
490019cf | 356 | return EPERM; |
0a7de745 | 357 | } |
490019cf A |
358 | |
359 | PSEM_SUBSYS_LOCK(); | |
1c79356b | 360 | for (pcpp = &psemhashtbl[psemhash]; pcpp >= psemhashtbl; pcpp--) { |
490019cf A |
361 | LIST_FOREACH_SAFE(pcp, pcpp, psem_hash, tmppcp) { |
362 | assert(pcp->psem_nlen); | |
363 | /* | |
364 | * unconditionally unlink the cache entry | |
365 | */ | |
366 | error = psem_unlink_internal(pcp->pseminfo, pcp); | |
0a7de745 | 367 | if (error) { |
490019cf | 368 | goto out; |
0a7de745 | 369 | } |
490019cf | 370 | } |
1c79356b | 371 | } |
490019cf A |
372 | assert(psemnument == 0); |
373 | ||
374 | out: | |
375 | PSEM_SUBSYS_UNLOCK(); | |
376 | ||
0a7de745 | 377 | if (error) { |
490019cf | 378 | printf("%s: Error %d removing all semaphores: %ld remain!\n", |
0a7de745 A |
379 | __func__, error, psemnument); |
380 | } | |
490019cf | 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; | |
0a7de745 | 399 | int fmode; |
1c79356b A |
400 | int cmode = uap->mode; |
401 | int value = uap->value; | |
402 | int incache = 0; | |
b0d623f7 | 403 | struct psemcache *pcp = PSEMCACHE_NULL; |
0a7de745 A |
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); |
0a7de745 | 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 | } | |
0a7de745 | 441 | } else { |
1c79356b A |
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 | 452 | |
0a7de745 A |
453 | for (cp = nameptr, i = 1; *cp != 0 && i <= plen; i++, cp++) { |
454 | nd.psem_hash += (unsigned char)*cp * i; | |
1c79356b A |
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()); | |
0a7de745 | 462 | if (error) { |
b0d623f7 | 463 | goto bad; |
0a7de745 | 464 | } |
b0d623f7 A |
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 | */ | |
0a7de745 | 471 | MALLOC(pcp, struct psemcache *, sizeof(struct psemcache), M_SHM, M_WAITOK | M_ZERO); |
b0d623f7 A |
472 | if (pcp == PSEMCACHE_NULL) { |
473 | error = ENOMEM; | |
474 | goto bad; | |
475 | } | |
476 | ||
0a7de745 | 477 | MALLOC(new_pinfo, struct pseminfo *, sizeof(struct pseminfo), M_SHM, M_WAITOK | M_ZERO); |
b0d623f7 A |
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 | */ | |
0a7de745 | 492 | |
b0d623f7 | 493 | fmode = FFLAGS(uap->oflag); |
0a7de745 A |
494 | |
495 | if ((fmode & O_CREAT)) { | |
496 | if ((value < 0) || (value > SEM_VALUE_MAX)) { | |
b0d623f7 A |
497 | error = EINVAL; |
498 | goto bad; | |
499 | } | |
0a7de745 | 500 | |
b0d623f7 | 501 | kret = semaphore_create(kernel_task, &new_pinfo->psem_semobject, SYNC_POLICY_FIFO, value); |
0a7de745 | 502 | |
b0d623f7 A |
503 | if (kret != KERN_SUCCESS) { |
504 | switch (kret) { | |
0a7de745 A |
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; | |
b0d623f7 A |
513 | } |
514 | goto bad; | |
515 | } | |
516 | } | |
0a7de745 A |
517 | |
518 | MALLOC(new_pnode, struct psemnode *, sizeof(struct psemnode), M_SHM, M_WAITOK | M_ZERO); | |
b0d623f7 A |
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 | 531 | |
0a7de745 | 532 | if (error == PSEMCACHE_FOUND) { |
1c79356b | 533 | incache = 1; |
0a7de745 | 534 | } else { |
490019cf | 535 | incache = 0; |
0a7de745 | 536 | } |
1c79356b | 537 | |
1c79356b A |
538 | cmode &= ALLPERMS; |
539 | ||
0a7de745 | 540 | if (((fmode & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) && incache) { |
1c79356b A |
541 | /* sem exists and opened O_EXCL */ |
542 | #if notyet | |
543 | if (pinfo->psem_flags & PSEM_INDELETE) { | |
544 | } | |
0a7de745 | 545 | #endif |
e5568f75 | 546 | AUDIT_ARG(posix_ipc_perm, pinfo->psem_uid, |
0a7de745 | 547 | pinfo->psem_gid, pinfo->psem_mode); |
1c79356b | 548 | error = EEXIST; |
b0d623f7 | 549 | goto bad_locked; |
1c79356b | 550 | } |
0a7de745 | 551 | if (((fmode & (O_CREAT | O_EXCL)) == O_CREAT) && incache) { |
1c79356b A |
552 | /* As per POSIX, O_CREAT has no effect */ |
553 | fmode &= ~O_CREAT; | |
554 | } | |
555 | ||
0a7de745 | 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 | 564 | bcopy(pnbuf, &pinfo->psem_name[0], PSEMNAMLEN); |
0a7de745 | 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; | |
0a7de745 | 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 | 583 | } |
0a7de745 | 584 | if (pinfo->psem_flags & PSEM_INDELETE) { |
1c79356b | 585 | error = ENOENT; |
b0d623f7 | 586 | goto bad_locked; |
0a7de745 | 587 | } |
e5568f75 | 588 | AUDIT_ARG(posix_ipc_perm, pinfo->psem_uid, |
0a7de745 | 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 | |
0a7de745 | 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 */ |
0a7de745 | 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); |
0a7de745 | 640 | return 0; |
1c79356b | 641 | |
b0d623f7 A |
642 | bad_locked: |
643 | PSEM_SUBSYS_UNLOCK(); | |
644 | bad: | |
0a7de745 | 645 | if (pcp != PSEMCACHE_NULL) { |
b0d623f7 | 646 | FREE(pcp, M_SHM); |
0a7de745 | 647 | } |
b0d623f7 | 648 | |
0a7de745 | 649 | if (new_pnode != PSEMNODE_NULL) { |
b0d623f7 | 650 | FREE(new_pnode, M_SHM); |
0a7de745 | 651 | } |
b0d623f7 | 652 | |
0a7de745 | 653 | if (fp != NULL) { |
b0d623f7 | 654 | fp_free(p, indx, fp); |
0a7de745 | 655 | } |
b0d623f7 A |
656 | |
657 | if (new_pinfo != PSEMINFO_NULL) { | |
658 | /* | |
659 | * kret signals whether or not we successfully created a | |
660 | * Mach semaphore for this semaphore; if so, we need to | |
661 | * destroy it here. | |
662 | */ | |
663 | if (kret == KERN_SUCCESS) { | |
664 | /* return value ignored - we can't _not_ do this */ | |
665 | (void)semaphore_destroy(kernel_task, new_pinfo->psem_semobject); | |
666 | } | |
2d21ac55 | 667 | #if CONFIG_MACF |
b0d623f7 | 668 | mac_posixsem_label_destroy(new_pinfo); |
2d21ac55 | 669 | #endif |
b0d623f7 | 670 | FREE(new_pinfo, M_SHM); |
2d21ac55 | 671 | } |
b0d623f7 | 672 | |
0a7de745 | 673 | if (pnbuf != NULL) { |
b0d623f7 | 674 | FREE_ZONE(pnbuf, MAXPATHLEN, M_NAMEI); |
0a7de745 A |
675 | } |
676 | return error; | |
1c79356b A |
677 | } |
678 | ||
91447636 A |
679 | /* |
680 | * XXX This code is repeated in several places | |
681 | */ | |
682 | static int | |
683 | psem_access(struct pseminfo *pinfo, int mode, kauth_cred_t cred) | |
1c79356b | 684 | { |
6d2010ae | 685 | int mode_req = ((mode & FREAD) ? S_IRUSR : 0) | |
0a7de745 | 686 | ((mode & FWRITE) ? S_IWUSR : 0); |
1c79356b A |
687 | |
688 | /* Otherwise, user id 0 always gets access. */ | |
0a7de745 A |
689 | if (!suser(cred, NULL)) { |
690 | return 0; | |
691 | } | |
1c79356b | 692 | |
0a7de745 | 693 | return posix_cred_access(cred, pinfo->psem_uid, pinfo->psem_gid, pinfo->psem_mode, mode_req); |
1c79356b A |
694 | } |
695 | ||
490019cf A |
696 | static int |
697 | psem_unlink_internal(struct pseminfo *pinfo, struct psemcache *pcache) | |
698 | { | |
699 | PSEM_SUBSYS_ASSERT_HELD(); | |
700 | ||
0a7de745 | 701 | if (!pinfo || !pcache) { |
490019cf | 702 | return EINVAL; |
0a7de745 | 703 | } |
490019cf | 704 | |
0a7de745 | 705 | if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED)) == 0) { |
490019cf | 706 | return EINVAL; |
0a7de745 | 707 | } |
490019cf | 708 | |
0a7de745 | 709 | if (pinfo->psem_flags & PSEM_INDELETE) { |
490019cf | 710 | return 0; |
0a7de745 | 711 | } |
490019cf A |
712 | |
713 | AUDIT_ARG(posix_ipc_perm, pinfo->psem_uid, pinfo->psem_gid, | |
0a7de745 | 714 | pinfo->psem_mode); |
490019cf A |
715 | |
716 | pinfo->psem_flags |= PSEM_INDELETE; | |
717 | pinfo->psem_usecount--; | |
718 | ||
719 | if (!pinfo->psem_usecount) { | |
720 | psem_delete(pinfo); | |
0a7de745 | 721 | FREE(pinfo, M_SHM); |
490019cf A |
722 | } else { |
723 | pinfo->psem_flags |= PSEM_REMOVED; | |
724 | } | |
725 | ||
726 | psem_cache_delete(pcache); | |
727 | FREE(pcache, M_SHM); | |
728 | return 0; | |
729 | } | |
730 | ||
731 | ||
1c79356b | 732 | int |
b0d623f7 | 733 | sem_unlink(__unused proc_t p, struct sem_unlink_args *uap, __unused int32_t *retval) |
1c79356b | 734 | { |
91447636 | 735 | size_t i; |
0a7de745 | 736 | int error = 0; |
1c79356b A |
737 | struct psemname nd; |
738 | struct pseminfo *pinfo; | |
1c79356b A |
739 | char * nameptr; |
740 | char * cp; | |
490019cf A |
741 | char * pnbuf; |
742 | size_t pathlen; | |
1c79356b | 743 | struct psemcache *pcache = PSEMCACHE_NULL; |
1c79356b A |
744 | |
745 | pinfo = PSEMINFO_NULL; | |
746 | ||
91447636 A |
747 | MALLOC_ZONE(pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK); |
748 | if (pnbuf == NULL) { | |
0a7de745 | 749 | return ENOSPC; /* XXX non-standard */ |
91447636 | 750 | } |
1c79356b | 751 | pathlen = MAXPATHLEN; |
91447636 | 752 | error = copyinstr(uap->name, pnbuf, MAXPATHLEN, &pathlen); |
1c79356b A |
753 | if (error) { |
754 | goto bad; | |
755 | } | |
e5568f75 | 756 | AUDIT_ARG(text, pnbuf); |
1c79356b A |
757 | if (pathlen > PSEMNAMLEN) { |
758 | error = ENAMETOOLONG; | |
759 | goto bad; | |
760 | } | |
761 | ||
490019cf | 762 | nameptr = pnbuf; |
1c79356b A |
763 | |
764 | #ifdef PSXSEM_NAME_RESTRICT | |
1c79356b A |
765 | if (*nameptr == '/') { |
766 | while (*(nameptr++) == '/') { | |
490019cf | 767 | pathlen--; |
1c79356b A |
768 | error = EINVAL; |
769 | goto bad; | |
770 | } | |
0a7de745 | 771 | } else { |
1c79356b A |
772 | error = EINVAL; |
773 | goto bad; | |
774 | } | |
775 | #endif /* PSXSEM_NAME_RESTRICT */ | |
776 | ||
1c79356b | 777 | nd.psem_nameptr = nameptr; |
490019cf | 778 | nd.psem_namelen = pathlen; |
0a7de745 | 779 | nd.psem_hash = 0; |
1c79356b | 780 | |
0a7de745 A |
781 | for (cp = nameptr, i = 1; *cp != 0 && i <= pathlen; i++, cp++) { |
782 | nd.psem_hash += (unsigned char)*cp * i; | |
1c79356b A |
783 | } |
784 | ||
91447636 | 785 | PSEM_SUBSYS_LOCK(); |
1c79356b A |
786 | error = psem_cache_search(&pinfo, &nd, &pcache); |
787 | ||
490019cf | 788 | if (error != PSEMCACHE_FOUND) { |
91447636 | 789 | PSEM_SUBSYS_UNLOCK(); |
cb323159 | 790 | error = ENOENT; |
1c79356b | 791 | goto bad; |
1c79356b | 792 | } |
490019cf | 793 | |
2d21ac55 A |
794 | #if CONFIG_MACF |
795 | error = mac_posixsem_check_unlink(kauth_cred_get(), pinfo, nameptr); | |
796 | if (error) { | |
797 | PSEM_SUBSYS_UNLOCK(); | |
798 | goto bad; | |
799 | } | |
800 | #endif | |
0a7de745 | 801 | if ((error = psem_access(pinfo, pinfo->psem_mode, kauth_cred_get()))) { |
91447636 | 802 | PSEM_SUBSYS_UNLOCK(); |
1c79356b | 803 | goto bad; |
91447636 | 804 | } |
1c79356b | 805 | |
490019cf | 806 | error = psem_unlink_internal(pinfo, pcache); |
91447636 | 807 | PSEM_SUBSYS_UNLOCK(); |
490019cf | 808 | |
1c79356b | 809 | bad: |
55e303ae | 810 | FREE_ZONE(pnbuf, MAXPATHLEN, M_NAMEI); |
490019cf | 811 | return error; |
1c79356b A |
812 | } |
813 | ||
1c79356b | 814 | int |
b0d623f7 | 815 | sem_close(proc_t p, struct sem_close_args *uap, __unused int32_t *retval) |
1c79356b | 816 | { |
0a7de745 | 817 | int fd = CAST_DOWN_EXPLICIT(int, uap->sem); |
91447636 | 818 | struct fileproc *fp; |
1c79356b A |
819 | int error = 0; |
820 | ||
e5568f75 | 821 | AUDIT_ARG(fd, fd); /* XXX This seems wrong; uap->sem is a pointer */ |
91447636 A |
822 | |
823 | proc_fdlock(p); | |
0a7de745 | 824 | error = fp_lookup(p, fd, &fp, 1); |
91447636 A |
825 | if (error) { |
826 | proc_fdunlock(p); | |
0a7de745 | 827 | return error; |
91447636 | 828 | } |
e8c3f781 | 829 | if (fp->f_type != DTYPE_PSXSEM) { |
0a7de745 | 830 | fp_drop(p, fd, fp, 1); |
e8c3f781 | 831 | proc_fdunlock(p); |
0a7de745 | 832 | return EBADF; |
e8c3f781 | 833 | } |
6d2010ae | 834 | procfdtbl_markclosefd(p, fd); |
cb323159 A |
835 | /* release the ref returned from fp_lookup before calling drain */ |
836 | (void) os_ref_release_locked(&fp->f_iocount); | |
2d21ac55 | 837 | fileproc_drain(p, fp); |
91447636 A |
838 | fdrelse(p, fd); |
839 | error = closef_locked(fp, fp->f_fglob, p); | |
39236c6e | 840 | fileproc_free(fp); |
91447636 | 841 | proc_fdunlock(p); |
0a7de745 | 842 | return error; |
1c79356b A |
843 | } |
844 | ||
1c79356b | 845 | int |
b0d623f7 | 846 | sem_wait(proc_t p, struct sem_wait_args *uap, int32_t *retval) |
2d21ac55 A |
847 | { |
848 | __pthread_testcancel(1); | |
0a7de745 | 849 | return sem_wait_nocancel(p, (struct sem_wait_nocancel_args *)uap, retval); |
2d21ac55 A |
850 | } |
851 | ||
852 | int | |
b0d623f7 | 853 | sem_wait_nocancel(proc_t p, struct sem_wait_nocancel_args *uap, __unused int32_t *retval) |
1c79356b | 854 | { |
0a7de745 | 855 | int fd = CAST_DOWN_EXPLICIT(int, uap->sem); |
91447636 | 856 | struct fileproc *fp; |
1c79356b | 857 | struct pseminfo * pinfo; |
0a7de745 | 858 | struct psemnode * pnode; |
1c79356b A |
859 | kern_return_t kret; |
860 | int error; | |
861 | ||
91447636 | 862 | error = fp_getfpsem(p, fd, &fp, &pnode); |
0a7de745 A |
863 | if (error) { |
864 | return error; | |
865 | } | |
866 | if (((pnode = (struct psemnode *)fp->f_data)) == PSEMNODE_NULL) { | |
91447636 A |
867 | error = EINVAL; |
868 | goto out; | |
869 | } | |
870 | PSEM_SUBSYS_LOCK(); | |
871 | if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) { | |
872 | PSEM_SUBSYS_UNLOCK(); | |
873 | error = EINVAL; | |
874 | goto out; | |
875 | } | |
0a7de745 A |
876 | if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED)) |
877 | != PSEM_ALLOCATED) { | |
91447636 A |
878 | PSEM_SUBSYS_UNLOCK(); |
879 | error = EINVAL; | |
880 | goto out; | |
1c79356b | 881 | } |
2d21ac55 A |
882 | #if CONFIG_MACF |
883 | error = mac_posixsem_check_wait(kauth_cred_get(), pinfo); | |
884 | if (error) { | |
885 | PSEM_SUBSYS_UNLOCK(); | |
886 | goto out; | |
887 | } | |
888 | #endif | |
91447636 | 889 | PSEM_SUBSYS_UNLOCK(); |
1c79356b A |
890 | kret = semaphore_wait(pinfo->psem_semobject); |
891 | switch (kret) { | |
892 | case KERN_INVALID_ADDRESS: | |
893 | case KERN_PROTECTION_FAILURE: | |
91447636 A |
894 | error = EACCES; |
895 | break; | |
1c79356b A |
896 | case KERN_ABORTED: |
897 | case KERN_OPERATION_TIMED_OUT: | |
91447636 A |
898 | error = EINTR; |
899 | break; | |
1c79356b | 900 | case KERN_SUCCESS: |
91447636 A |
901 | error = 0; |
902 | break; | |
1c79356b | 903 | default: |
91447636 A |
904 | error = EINVAL; |
905 | break; | |
1c79356b | 906 | } |
91447636 A |
907 | out: |
908 | fp_drop(p, fd, fp, 0); | |
0a7de745 | 909 | return error; |
91447636 | 910 | } |
1c79356b A |
911 | |
912 | int | |
b0d623f7 | 913 | sem_trywait(proc_t p, struct sem_trywait_args *uap, __unused int32_t *retval) |
1c79356b | 914 | { |
0a7de745 | 915 | int fd = CAST_DOWN_EXPLICIT(int, uap->sem); |
91447636 | 916 | struct fileproc *fp; |
1c79356b | 917 | struct pseminfo * pinfo; |
0a7de745 | 918 | struct psemnode * pnode; |
1c79356b A |
919 | kern_return_t kret; |
920 | mach_timespec_t wait_time; | |
921 | int error; | |
0a7de745 | 922 | |
91447636 | 923 | error = fp_getfpsem(p, fd, &fp, &pnode); |
0a7de745 A |
924 | if (error) { |
925 | return error; | |
926 | } | |
927 | if (((pnode = (struct psemnode *)fp->f_data)) == PSEMNODE_NULL) { | |
91447636 A |
928 | error = EINVAL; |
929 | goto out; | |
930 | } | |
931 | PSEM_SUBSYS_LOCK(); | |
932 | if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) { | |
933 | PSEM_SUBSYS_UNLOCK(); | |
934 | error = EINVAL; | |
935 | goto out; | |
936 | } | |
0a7de745 A |
937 | if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED)) |
938 | != PSEM_ALLOCATED) { | |
91447636 A |
939 | PSEM_SUBSYS_UNLOCK(); |
940 | error = EINVAL; | |
941 | goto out; | |
1c79356b | 942 | } |
2d21ac55 A |
943 | #if CONFIG_MACF |
944 | error = mac_posixsem_check_wait(kauth_cred_get(), pinfo); | |
945 | if (error) { | |
946 | PSEM_SUBSYS_UNLOCK(); | |
947 | goto out; | |
948 | } | |
949 | #endif | |
91447636 | 950 | PSEM_SUBSYS_UNLOCK(); |
1c79356b A |
951 | wait_time.tv_sec = 0; |
952 | wait_time.tv_nsec = 0; | |
953 | ||
954 | kret = semaphore_timedwait(pinfo->psem_semobject, MACH_TIMESPEC_ZERO); | |
955 | switch (kret) { | |
956 | case KERN_INVALID_ADDRESS: | |
957 | case KERN_PROTECTION_FAILURE: | |
91447636 A |
958 | error = EINVAL; |
959 | break; | |
1c79356b | 960 | case KERN_ABORTED: |
91447636 A |
961 | error = EINTR; |
962 | break; | |
1c79356b | 963 | case KERN_OPERATION_TIMED_OUT: |
91447636 A |
964 | error = EAGAIN; |
965 | break; | |
1c79356b | 966 | case KERN_SUCCESS: |
91447636 A |
967 | error = 0; |
968 | break; | |
1c79356b | 969 | default: |
91447636 A |
970 | error = EINVAL; |
971 | break; | |
1c79356b | 972 | } |
91447636 A |
973 | out: |
974 | fp_drop(p, fd, fp, 0); | |
0a7de745 | 975 | return error; |
1c79356b A |
976 | } |
977 | ||
1c79356b | 978 | int |
b0d623f7 | 979 | sem_post(proc_t p, struct sem_post_args *uap, __unused int32_t *retval) |
1c79356b | 980 | { |
0a7de745 | 981 | int fd = CAST_DOWN_EXPLICIT(int, uap->sem); |
91447636 | 982 | struct fileproc *fp; |
1c79356b | 983 | struct pseminfo * pinfo; |
0a7de745 | 984 | struct psemnode * pnode; |
1c79356b A |
985 | kern_return_t kret; |
986 | int error; | |
987 | ||
91447636 | 988 | error = fp_getfpsem(p, fd, &fp, &pnode); |
0a7de745 A |
989 | if (error) { |
990 | return error; | |
991 | } | |
992 | if (((pnode = (struct psemnode *)fp->f_data)) == PSEMNODE_NULL) { | |
91447636 A |
993 | error = EINVAL; |
994 | goto out; | |
995 | } | |
996 | PSEM_SUBSYS_LOCK(); | |
997 | if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) { | |
998 | PSEM_SUBSYS_UNLOCK(); | |
999 | error = EINVAL; | |
1000 | goto out; | |
1001 | } | |
0a7de745 A |
1002 | if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED)) |
1003 | != PSEM_ALLOCATED) { | |
91447636 A |
1004 | PSEM_SUBSYS_UNLOCK(); |
1005 | error = EINVAL; | |
1006 | goto out; | |
1c79356b | 1007 | } |
2d21ac55 A |
1008 | #if CONFIG_MACF |
1009 | error = mac_posixsem_check_post(kauth_cred_get(), pinfo); | |
1010 | if (error) { | |
1011 | PSEM_SUBSYS_UNLOCK(); | |
1012 | goto out; | |
1013 | } | |
1014 | #endif | |
91447636 | 1015 | PSEM_SUBSYS_UNLOCK(); |
1c79356b A |
1016 | kret = semaphore_signal(pinfo->psem_semobject); |
1017 | switch (kret) { | |
1018 | case KERN_INVALID_ADDRESS: | |
1019 | case KERN_PROTECTION_FAILURE: | |
91447636 A |
1020 | error = EINVAL; |
1021 | break; | |
1c79356b A |
1022 | case KERN_ABORTED: |
1023 | case KERN_OPERATION_TIMED_OUT: | |
91447636 A |
1024 | error = EINTR; |
1025 | break; | |
1c79356b | 1026 | case KERN_SUCCESS: |
91447636 A |
1027 | error = 0; |
1028 | break; | |
1c79356b | 1029 | default: |
91447636 A |
1030 | error = EINVAL; |
1031 | break; | |
1c79356b | 1032 | } |
91447636 A |
1033 | out: |
1034 | fp_drop(p, fd, fp, 0); | |
0a7de745 | 1035 | return error; |
1c79356b A |
1036 | } |
1037 | ||
9bccf70c | 1038 | static int |
2d21ac55 | 1039 | psem_close(struct psemnode *pnode, __unused int flags) |
1c79356b | 1040 | { |
0a7de745 | 1041 | int error = 0; |
2d21ac55 | 1042 | struct pseminfo *pinfo; |
1c79356b | 1043 | |
91447636 | 1044 | PSEM_SUBSYS_LOCK(); |
0a7de745 | 1045 | if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) { |
91447636 | 1046 | PSEM_SUBSYS_UNLOCK(); |
0a7de745 | 1047 | return EINVAL; |
91447636 | 1048 | } |
1c79356b A |
1049 | |
1050 | if ((pinfo->psem_flags & PSEM_ALLOCATED) != PSEM_ALLOCATED) { | |
91447636 | 1051 | PSEM_SUBSYS_UNLOCK(); |
0a7de745 | 1052 | return EINVAL; |
1c79356b A |
1053 | } |
1054 | #if DIAGNOSTIC | |
0a7de745 | 1055 | if (!pinfo->psem_usecount) { |
1c79356b A |
1056 | kprintf("negative usecount in psem_close\n"); |
1057 | } | |
1058 | #endif /* DIAGNOSTIC */ | |
1059 | pinfo->psem_usecount--; | |
1060 | ||
0a7de745 | 1061 | if ((pinfo->psem_flags & PSEM_REMOVED) && !pinfo->psem_usecount) { |
91447636 A |
1062 | PSEM_SUBSYS_UNLOCK(); |
1063 | /* lock dropped as only semaphore is destroyed here */ | |
1c79356b | 1064 | error = psem_delete(pinfo); |
0a7de745 | 1065 | FREE(pinfo, M_SHM); |
91447636 A |
1066 | } else { |
1067 | PSEM_SUBSYS_UNLOCK(); | |
1c79356b | 1068 | } |
91447636 A |
1069 | /* subsystem lock is dropped when we get here */ |
1070 | FREE(pnode, M_SHM); | |
0a7de745 | 1071 | return error; |
1c79356b A |
1072 | } |
1073 | ||
9bccf70c | 1074 | static int |
2d21ac55 | 1075 | psem_closefile(struct fileglob *fg, __unused vfs_context_t ctx) |
9bccf70c | 1076 | { |
91447636 | 1077 | int error; |
9bccf70c | 1078 | |
2d21ac55 A |
1079 | /* |
1080 | * Not locked as psem_close is called only from here and is locked | |
1081 | * properly | |
1082 | */ | |
1083 | error = psem_close(((struct psemnode *)fg->fg_data), fg->fg_flag); | |
91447636 | 1084 | |
0a7de745 | 1085 | return error; |
9bccf70c A |
1086 | } |
1087 | ||
0a7de745 | 1088 | static int |
1c79356b A |
1089 | psem_delete(struct pseminfo * pinfo) |
1090 | { | |
1091 | kern_return_t kret; | |
1092 | ||
1093 | kret = semaphore_destroy(kernel_task, pinfo->psem_semobject); | |
2d21ac55 A |
1094 | #if CONFIG_MACF |
1095 | mac_posixsem_label_destroy(pinfo); | |
1096 | #endif | |
1c79356b A |
1097 | |
1098 | switch (kret) { | |
1099 | case KERN_INVALID_ADDRESS: | |
1100 | case KERN_PROTECTION_FAILURE: | |
0a7de745 | 1101 | return EINVAL; |
1c79356b A |
1102 | case KERN_ABORTED: |
1103 | case KERN_OPERATION_TIMED_OUT: | |
0a7de745 | 1104 | return EINTR; |
1c79356b | 1105 | case KERN_SUCCESS: |
0a7de745 | 1106 | return 0; |
1c79356b | 1107 | default: |
0a7de745 | 1108 | return EINVAL; |
1c79356b | 1109 | } |
1c79356b A |
1110 | } |
1111 | ||
0c530ab8 A |
1112 | int |
1113 | fill_pseminfo(struct psemnode *pnode, struct psem_info * info) | |
1114 | { | |
2d21ac55 A |
1115 | struct pseminfo *pinfo; |
1116 | struct vinfo_stat *sb; | |
0c530ab8 A |
1117 | |
1118 | PSEM_SUBSYS_LOCK(); | |
0a7de745 | 1119 | if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) { |
0c530ab8 | 1120 | PSEM_SUBSYS_UNLOCK(); |
0a7de745 | 1121 | return EINVAL; |
0c530ab8 A |
1122 | } |
1123 | ||
1124 | #if 0 | |
1125 | if ((pinfo->psem_flags & PSEM_ALLOCATED) != PSEM_ALLOCATED) { | |
1126 | PSEM_SUBSYS_UNLOCK(); | |
0a7de745 | 1127 | return EINVAL; |
0c530ab8 A |
1128 | } |
1129 | #endif | |
1130 | ||
1131 | sb = &info->psem_stat; | |
2d21ac55 | 1132 | bzero(sb, sizeof(struct vinfo_stat)); |
0c530ab8 | 1133 | |
0a7de745 A |
1134 | sb->vst_mode = pinfo->psem_mode; |
1135 | sb->vst_uid = pinfo->psem_uid; | |
1136 | sb->vst_gid = pinfo->psem_gid; | |
1137 | sb->vst_size = pinfo->psem_usecount; | |
1138 | bcopy(&pinfo->psem_name[0], &info->psem_name[0], PSEMNAMLEN + 1); | |
0c530ab8 A |
1139 | |
1140 | PSEM_SUBSYS_UNLOCK(); | |
0a7de745 | 1141 | return 0; |
0c530ab8 A |
1142 | } |
1143 | ||
2d21ac55 A |
1144 | #if CONFIG_MACF |
1145 | void | |
1146 | psem_label_associate(struct fileproc *fp, struct vnode *vp, vfs_context_t ctx) | |
1147 | { | |
1148 | struct psemnode *pnode; | |
1149 | struct pseminfo *psem; | |
1150 | ||
1151 | PSEM_SUBSYS_LOCK(); | |
1152 | pnode = (struct psemnode *)fp->f_fglob->fg_data; | |
1153 | if (pnode != NULL) { | |
1154 | psem = pnode->pinfo; | |
0a7de745 | 1155 | if (psem != NULL) { |
2d21ac55 A |
1156 | mac_posixsem_vnode_label_associate( |
1157 | vfs_context_ucred(ctx), psem, psem->psem_label, | |
1158 | vp, vp->v_label); | |
0a7de745 | 1159 | } |
2d21ac55 A |
1160 | } |
1161 | PSEM_SUBSYS_UNLOCK(); | |
1162 | } | |
1163 | #endif |