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