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