uipc_sem.c revision 1.3 1 /* $NetBSD: uipc_sem.c,v 1.3 2003/01/24 01:46:27 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of Wasabi Systems, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 2002 Alfred Perlstein <alfred (at) FreeBSD.org>
41 * All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 */
64
65 #include "opt_posix.h"
66
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/kernel.h>
70 #include <sys/proc.h>
71 #include <sys/lock.h>
72 #include <sys/ksem.h>
73 #include <sys/syscall.h>
74 #include <sys/stat.h>
75 #include <sys/malloc.h>
76 #include <sys/fcntl.h>
77
78 #include <sys/mount.h>
79
80 #include <sys/syscallargs.h>
81
82 #ifndef SEM_MAX
83 #define SEM_MAX 30
84 #endif
85
86 #define SEM_MAX_NAMELEN 14
87 #define SEM_VALUE_MAX (~0U)
88
89 #define SEM_TO_ID(x) ((intptr_t)(x))
90
91 /*
92 * Note: to read the ks_name member, you need either the ks_interlock
93 * or the ksem_slock. To write the ks_name member, you need both. Make
94 * sure the order is ksem_slock -> ks_interlock.
95 */
96 struct ksem {
97 LIST_ENTRY(ksem) ks_entry; /* global list entry */
98 struct simplelock ks_interlock; /* lock on this ksem */
99 char *ks_name; /* if named, this is the name */
100 unsigned int ks_ref; /* number of references */
101 mode_t ks_mode; /* protection bits */
102 uid_t ks_uid; /* creator uid */
103 gid_t ks_gid; /* creator gid */
104 unsigned int ks_value; /* current value */
105 unsigned int ks_waiters; /* number of waiters */
106 };
107
108 struct ksem_ref {
109 LIST_ENTRY(ksem_ref) ksr_list;
110 struct ksem *ksr_ksem;
111 };
112
113 struct ksem_proc {
114 struct lock kp_lock;
115 LIST_HEAD(, ksem_ref) kp_ksems;
116 };
117
118 /*
119 * ksem_slock protects ksem_head and nsems. Only named semaphores go
120 * onto ksem_head.
121 */
122 static struct simplelock ksem_slock;
123 static LIST_HEAD(, ksem) ksem_head = LIST_HEAD_INITIALIZER(&ksem_head);
124 static int nsems = 0;
125
126 static void
127 ksem_free(struct ksem *ks)
128 {
129
130 LOCK_ASSERT(simple_lock_held(&ks->ks_interlock));
131 /*
132 * If the ksem is anonymous (or has been unlinked), then
133 * this is the end if its life.
134 */
135 if (ks->ks_name == NULL) {
136 simple_unlock(&ks->ks_interlock);
137 free(ks, M_SEM);
138
139 simple_lock(&ksem_slock);
140 nsems--;
141 simple_unlock(&ksem_slock);
142 return;
143 }
144 simple_unlock(&ks->ks_interlock);
145 }
146
147 static __inline void
148 ksem_addref(struct ksem *ks)
149 {
150
151 LOCK_ASSERT(simple_lock_held(&ks->ks_interlock));
152 ks->ks_ref++;
153 KASSERT(ks->ks_ref != 0); /* XXX KDASSERT */
154 }
155
156 static __inline void
157 ksem_delref(struct ksem *ks)
158 {
159
160 LOCK_ASSERT(simple_lock_held(&ks->ks_interlock));
161 KASSERT(ks->ks_ref != 0); /* XXX KDASSERT */
162 if (--ks->ks_ref == 0) {
163 ksem_free(ks);
164 return;
165 }
166 simple_unlock(&ks->ks_interlock);
167 }
168
169 static struct ksem_proc *
170 ksem_proc_alloc(void)
171 {
172 struct ksem_proc *kp;
173
174 kp = malloc(sizeof(*kp), M_SEM, M_WAITOK);
175 lockinit(&kp->kp_lock, PWAIT, "ksproc", 0, 0);
176 LIST_INIT(&kp->kp_ksems);
177
178 return (kp);
179 }
180
181 static void
182 ksem_add_proc(struct proc *p, struct ksem *ks)
183 {
184 struct ksem_proc *kp;
185 struct ksem_ref *ksr;
186
187 if (p->p_ksems == NULL) {
188 kp = ksem_proc_alloc();
189 p->p_ksems = kp;
190 } else
191 kp = p->p_ksems;
192
193 ksr = malloc(sizeof(*ksr), M_SEM, M_WAITOK);
194 ksr->ksr_ksem = ks;
195
196 lockmgr(&kp->kp_lock, LK_EXCLUSIVE, NULL);
197 LIST_INSERT_HEAD(&kp->kp_ksems, ksr, ksr_list);
198 lockmgr(&kp->kp_lock, LK_RELEASE, NULL);
199 }
200
201 /* We MUST have a write lock on the ksem_proc list! */
202 static struct ksem_ref *
203 ksem_drop_proc(struct ksem_proc *kp, struct ksem *ks)
204 {
205 struct ksem_ref *ksr;
206
207 LOCK_ASSERT(simple_lock_held(&ks->ks_interlock));
208 LIST_FOREACH(ksr, &kp->kp_ksems, ksr_list) {
209 if (ksr->ksr_ksem == ks) {
210 ksem_delref(ks);
211 LIST_REMOVE(ksr, ksr_list);
212 return (ksr);
213 }
214 }
215 #ifdef DIAGNOSTIC
216 panic("ksem_drop_proc: ksem_proc %p ksem %p", kp, ks);
217 #endif
218 return (NULL);
219 }
220
221 static int
222 ksem_perm(struct proc *p, struct ksem *ks)
223 {
224 struct ucred *uc;
225
226 LOCK_ASSERT(simple_lock_held(&ks->ks_interlock));
227 uc = p->p_ucred;
228 if ((uc->cr_uid == ks->ks_uid && (ks->ks_mode & S_IWUSR) != 0) ||
229 (uc->cr_gid == ks->ks_gid && (ks->ks_mode & S_IWGRP) != 0) ||
230 (ks->ks_mode & S_IWOTH) != 0 || suser(uc, &p->p_acflag) == 0)
231 return (0);
232 return (EPERM);
233 }
234
235 static struct ksem *
236 ksem_lookup_byname(const char *name)
237 {
238 struct ksem *ks;
239
240 LOCK_ASSERT(simple_lock_held(&ksem_slock));
241 LIST_FOREACH(ks, &ksem_head, ks_entry) {
242 if (strcmp(ks->ks_name, name) == 0) {
243 simple_lock(&ks->ks_interlock);
244 return (ks);
245 }
246 }
247 return (NULL);
248 }
249
250 static int
251 ksem_create(struct proc *p, const char *name, struct ksem **ksret,
252 mode_t mode, unsigned int value)
253 {
254 struct ksem *ret;
255 struct ucred *uc;
256 size_t len;
257
258 uc = p->p_ucred;
259 if (value > SEM_VALUE_MAX)
260 return (EINVAL);
261 ret = malloc(sizeof(*ret), M_SEM, M_WAITOK | M_ZERO);
262 if (name != NULL) {
263 len = strlen(name);
264 if (len > SEM_MAX_NAMELEN) {
265 free(ret, M_SEM);
266 return (ENAMETOOLONG);
267 }
268 /* name must start with a '/' but not contain one. */
269 if (*name != '/' || len < 2 || strchr(name + 1, '/') != NULL) {
270 free(ret, M_SEM);
271 return (EINVAL);
272 }
273 ret->ks_name = malloc(len + 1, M_SEM, M_WAITOK);
274 strcpy(ret->ks_name, name);
275 } else
276 ret->ks_name = NULL;
277 ret->ks_mode = mode;
278 ret->ks_value = value;
279 ret->ks_ref = 1;
280 ret->ks_waiters = 0;
281 ret->ks_uid = uc->cr_uid;
282 ret->ks_gid = uc->cr_gid;
283 simple_lock_init(&ret->ks_interlock);
284
285 simple_lock(&ksem_slock);
286 if (nsems >= SEM_MAX) {
287 simple_unlock(&ksem_slock);
288 if (ret->ks_name != NULL)
289 free(ret->ks_name, M_SEM);
290 free(ret, M_SEM);
291 return (ENFILE);
292 }
293 nsems++;
294 simple_unlock(&ksem_slock);
295
296 *ksret = ret;
297 return (0);
298 }
299
300 int
301 sys__ksem_init(struct lwp *l, void *v, register_t *retval)
302 {
303 struct sys__ksem_init_args /* {
304 unsigned int value;
305 semid_t *idp;
306 } */ *uap = v;
307 struct ksem *ks;
308 semid_t id;
309 int error;
310
311 /* Note the mode does not matter for anonymous semaphores. */
312 error = ksem_create(l->l_proc, NULL, &ks, 0, SCARG(uap, value));
313 if (error)
314 return (error);
315 id = SEM_TO_ID(ks);
316 error = copyout(&id, SCARG(uap, idp), sizeof(id));
317 if (error) {
318 simple_lock(&ks->ks_interlock);
319 ksem_delref(ks);
320 return (error);
321 }
322
323 ksem_add_proc(l->l_proc, ks);
324
325 return (0);
326 }
327
328 int
329 sys__ksem_open(struct lwp *l, void *v, register_t *retval)
330 {
331 struct sys__ksem_open_args /* {
332 const char *name;
333 int oflag;
334 mode_t mode;
335 unsigned int value;
336 semid_t *idp;
337 } */ *uap = v;
338 char name[SEM_MAX_NAMELEN + 1];
339 size_t done;
340 int error;
341 struct ksem *ksnew, *ks;
342 semid_t id;
343
344 error = copyinstr(SCARG(uap, name), name, sizeof(name), &done);
345 if (error)
346 return (error);
347
348 ksnew = NULL;
349 simple_lock(&ksem_slock);
350 ks = ksem_lookup_byname(name);
351
352 /* Found one? */
353 if (ks != NULL) {
354 /* Check for exclusive create. */
355 if (SCARG(uap, oflag) & O_EXCL) {
356 simple_unlock(&ks->ks_interlock);
357 simple_unlock(&ksem_slock);
358 return (EEXIST);
359 }
360 found_one:
361 /*
362 * Verify permissions. If we can access it, add
363 * this process's reference.
364 */
365 LOCK_ASSERT(simple_lock_held(&ks->ks_interlock));
366 error = ksem_perm(l->l_proc, ks);
367 if (error == 0)
368 ksem_addref(ks);
369 simple_unlock(&ks->ks_interlock);
370 simple_unlock(&ksem_slock);
371 if (error)
372 return (error);
373
374 id = SEM_TO_ID(ks);
375 error = copyout(&id, SCARG(uap, idp), sizeof(id));
376 if (error) {
377 simple_lock(&ks->ks_interlock);
378 ksem_delref(ks);
379 return (error);
380 }
381
382 ksem_add_proc(l->l_proc, ks);
383
384 return (0);
385 }
386
387 /*
388 * didn't ask for creation? error.
389 */
390 if ((SCARG(uap, oflag) & O_CREAT) == 0) {
391 simple_unlock(&ksem_slock);
392 return (ENOENT);
393 }
394
395 /*
396 * We may block during creation, so drop the lock.
397 */
398 simple_unlock(&ksem_slock);
399 error = ksem_create(l->l_proc, name, &ksnew, SCARG(uap, mode),
400 SCARG(uap, value));
401 if (error != 0)
402 return (error);
403
404 id = SEM_TO_ID(ksnew);
405 error = copyout(&id, SCARG(uap, idp), sizeof(id));
406 if (error) {
407 free(ksnew->ks_name, M_SEM);
408 ksnew->ks_name = NULL;
409
410 simple_lock(&ksnew->ks_interlock);
411 ksem_delref(ksnew);
412 return (error);
413 }
414
415 /*
416 * We need to make sure we haven't lost a race while
417 * allocating during creation.
418 */
419 simple_lock(&ksem_slock);
420 if ((ks = ksem_lookup_byname(name)) != NULL) {
421 if (SCARG(uap, oflag) & O_EXCL) {
422 simple_unlock(&ks->ks_interlock);
423 simple_unlock(&ksem_slock);
424
425 free(ksnew->ks_name, M_SEM);
426 ksnew->ks_name = NULL;
427
428 simple_lock(&ksnew->ks_interlock);
429 ksem_delref(ksnew);
430 return (EEXIST);
431 }
432 goto found_one;
433 } else {
434 /* ksnew already has its initial reference. */
435 LIST_INSERT_HEAD(&ksem_head, ksnew, ks_entry);
436 simple_unlock(&ksem_slock);
437
438 ksem_add_proc(l->l_proc, ksnew);
439 }
440 return (error);
441 }
442
443 /* We must have a read lock on the ksem_proc list! */
444 static struct ksem *
445 ksem_lookup_proc(struct ksem_proc *kp, semid_t id)
446 {
447 struct ksem_ref *ksr;
448
449 LIST_FOREACH(ksr, &kp->kp_ksems, ksr_list) {
450 if (id == (semid_t) ksr->ksr_ksem) {
451 simple_lock(&ksr->ksr_ksem->ks_interlock);
452 return (ksr->ksr_ksem);
453 }
454 }
455
456 return (NULL);
457 }
458
459 int
460 sys__ksem_unlink(struct lwp *l, void *v, register_t *retval)
461 {
462 struct sys__ksem_unlink_args /* {
463 const char *name;
464 } */ *uap = v;
465 char name[SEM_MAX_NAMELEN + 1], *cp;
466 size_t done;
467 struct ksem *ks;
468 int error;
469
470 error = copyinstr(SCARG(uap, name), name, sizeof(name), &done);
471 if (error)
472 return error;
473
474 simple_lock(&ksem_slock);
475 ks = ksem_lookup_byname(name);
476 if (ks == NULL) {
477 simple_unlock(&ksem_slock);
478 return (ENOENT);
479 }
480
481 LOCK_ASSERT(simple_lock_held(&ks->ks_interlock));
482
483 LIST_REMOVE(ks, ks_entry);
484 cp = ks->ks_name;
485 ks->ks_name = NULL;
486
487 simple_unlock(&ksem_slock);
488
489 if (ks->ks_ref == 0)
490 ksem_free(ks);
491 else
492 simple_unlock(&ks->ks_interlock);
493
494 free(cp, M_SEM);
495
496 return (0);
497 }
498
499 int
500 sys__ksem_close(struct lwp *l, void *v, register_t *retval)
501 {
502 struct sys__ksem_close_args /* {
503 semid_t id;
504 } */ *uap = v;
505 struct ksem_proc *kp;
506 struct ksem_ref *ksr;
507 struct ksem *ks;
508
509 if ((kp = l->l_proc->p_ksems) == NULL)
510 return (EINVAL);
511
512 lockmgr(&kp->kp_lock, LK_EXCLUSIVE, NULL);
513
514 ks = ksem_lookup_proc(kp, SCARG(uap, id));
515 if (ks == NULL) {
516 lockmgr(&kp->kp_lock, LK_RELEASE, NULL);
517 return (EINVAL);
518 }
519
520 LOCK_ASSERT(simple_lock_held(&ks->ks_interlock));
521 if (ks->ks_name == NULL) {
522 simple_unlock(&ks->ks_interlock);
523 lockmgr(&kp->kp_lock, LK_RELEASE, NULL);
524 return (EINVAL);
525 }
526
527 ksr = ksem_drop_proc(kp, ks);
528 lockmgr(&kp->kp_lock, LK_RELEASE, NULL);
529 free(ksr, M_SEM);
530
531 return (0);
532 }
533
534 int
535 sys__ksem_post(struct lwp *l, void *v, register_t *retval)
536 {
537 struct sys__ksem_post_args /* {
538 semid_t id;
539 } */ *uap = v;
540 struct ksem_proc *kp;
541 struct ksem *ks;
542 int error;
543
544 if ((kp = l->l_proc->p_ksems) == NULL)
545 return (EINVAL);
546
547 lockmgr(&kp->kp_lock, LK_SHARED, NULL);
548 ks = ksem_lookup_proc(kp, SCARG(uap, id));
549 lockmgr(&kp->kp_lock, LK_RELEASE, NULL);
550 if (ks == NULL)
551 return (EINVAL);
552
553 LOCK_ASSERT(simple_lock_held(&ks->ks_interlock));
554 if (ks->ks_value == SEM_VALUE_MAX) {
555 error = EOVERFLOW;
556 goto out;
557 }
558 ++ks->ks_value;
559 if (ks->ks_waiters)
560 wakeup(ks);
561 error = 0;
562 out:
563 simple_unlock(&ks->ks_interlock);
564 return (error);
565 }
566
567 static int
568 ksem_wait(struct lwp *l, semid_t id, int tryflag)
569 {
570 struct ksem_proc *kp;
571 struct ksem *ks;
572 int error;
573
574 if ((kp = l->l_proc->p_ksems) == NULL)
575 return (EINVAL);
576
577 lockmgr(&kp->kp_lock, LK_SHARED, NULL);
578 ks = ksem_lookup_proc(kp, id);
579 lockmgr(&kp->kp_lock, LK_RELEASE, NULL);
580 if (ks == NULL)
581 return (EINVAL);
582
583 LOCK_ASSERT(simple_lock_held(&ks->ks_interlock));
584 ksem_addref(ks);
585 while (ks->ks_value == 0) {
586 ks->ks_waiters++;
587 error = tryflag ? EAGAIN : ltsleep(ks, PCATCH, "psem", 0,
588 &ks->ks_interlock);
589 ks->ks_waiters--;
590 if (error)
591 goto out;
592 }
593 ks->ks_value--;
594 error = 0;
595 out:
596 ksem_delref(ks);
597 return (error);
598 }
599
600 int
601 sys__ksem_wait(struct lwp *l, void *v, register_t *retval)
602 {
603 struct sys__ksem_wait_args /* {
604 semid_t id;
605 } */ *uap = v;
606
607 return ksem_wait(l, SCARG(uap, id), 0);
608 }
609
610 int
611 sys__ksem_trywait(struct lwp *l, void *v, register_t *retval)
612 {
613 struct sys__ksem_trywait_args /* {
614 semid_t id;
615 } */ *uap = v;
616
617 return ksem_wait(l, SCARG(uap, id), 1);
618 }
619
620 int
621 sys__ksem_getvalue(struct lwp *l, void *v, register_t *retval)
622 {
623 struct sys__ksem_getvalue_args /* {
624 semid_t id;
625 unsigned int *value;
626 } */ *uap = v;
627 struct ksem_proc *kp;
628 struct ksem *ks;
629 unsigned int val;
630
631 if ((kp = l->l_proc->p_ksems) == NULL)
632 return (EINVAL);
633
634 lockmgr(&kp->kp_lock, LK_SHARED, NULL);
635 ks = ksem_lookup_proc(kp, SCARG(uap, id));
636 lockmgr(&kp->kp_lock, LK_RELEASE, NULL);
637 if (ks == NULL)
638 return (EINVAL);
639
640 LOCK_ASSERT(simple_lock_held(&ks->ks_interlock));
641 val = ks->ks_value;
642 simple_unlock(&ks->ks_interlock);
643
644 return (copyout(&val, SCARG(uap, value), sizeof(val)));
645 }
646
647 int
648 sys__ksem_destroy(struct lwp *l, void *v, register_t *retval)
649 {
650 struct sys__ksem_destroy_args /*{
651 semid_t id;
652 } */ *uap = v;
653 struct ksem_proc *kp;
654 struct ksem_ref *ksr;
655 struct ksem *ks;
656
657 if ((kp = l->l_proc->p_ksems) == NULL)
658 return (EINVAL);
659
660 lockmgr(&kp->kp_lock, LK_EXCLUSIVE, NULL);
661
662 ks = ksem_lookup_proc(kp, SCARG(uap, id));
663 if (ks == NULL) {
664 lockmgr(&kp->kp_lock, LK_RELEASE, NULL);
665 return (EINVAL);
666 }
667
668 LOCK_ASSERT(simple_lock_held(&ks->ks_interlock));
669
670 /*
671 * XXX This misses named semaphores which have been unlink'd,
672 * XXX but since behavior of destroying a named semaphore is
673 * XXX undefined, this is technically allowed.
674 */
675 if (ks->ks_name != NULL) {
676 simple_unlock(&ks->ks_interlock);
677 lockmgr(&kp->kp_lock, LK_RELEASE, NULL);
678 return (EINVAL);
679 }
680
681 if (ks->ks_waiters) {
682 simple_unlock(&ks->ks_interlock);
683 lockmgr(&kp->kp_lock, LK_RELEASE, NULL);
684 return (EBUSY);
685 }
686
687 ksr = ksem_drop_proc(kp, ks);
688 lockmgr(&kp->kp_lock, LK_RELEASE, NULL);
689 free(ksr, M_SEM);
690
691 return (0);
692 }
693
694 static void
695 ksem_forkhook(struct proc *p2, struct proc *p1)
696 {
697 struct ksem_proc *kp1, *kp2;
698 struct ksem_ref *ksr, *ksr1;
699
700 if ((kp1 = p1->p_ksems) == NULL) {
701 p2->p_ksems = NULL;
702 return;
703 }
704
705 p2->p_ksems = kp2 = ksem_proc_alloc();
706
707 lockmgr(&kp1->kp_lock, LK_SHARED, NULL);
708
709 if (!LIST_EMPTY(&kp1->kp_ksems)) {
710 LIST_FOREACH(ksr, &kp1->kp_ksems, ksr_list) {
711 ksr1 = malloc(sizeof(*ksr), M_SEM, M_WAITOK);
712 ksr1->ksr_ksem = ksr->ksr_ksem;
713 simple_lock(&ksr->ksr_ksem->ks_interlock);
714 ksem_addref(ksr->ksr_ksem);
715 simple_unlock(&ksr->ksr_ksem->ks_interlock);
716 LIST_INSERT_HEAD(&kp2->kp_ksems, ksr1, ksr_list);
717 }
718 }
719
720 lockmgr(&kp1->kp_lock, LK_RELEASE, NULL);
721 }
722
723 static void
724 ksem_exithook(struct proc *p, void *arg)
725 {
726 struct ksem_proc *kp;
727 struct ksem_ref *ksr;
728
729 if ((kp = p->p_ksems) == NULL)
730 return;
731
732 /* Don't bother locking; process is dying. */
733
734 while ((ksr = LIST_FIRST(&kp->kp_ksems)) != NULL) {
735 LIST_REMOVE(ksr, ksr_list);
736 simple_lock(&ksr->ksr_ksem->ks_interlock);
737 ksem_delref(ksr->ksr_ksem);
738 free(ksr, M_SEM);
739 }
740 }
741
742 void
743 ksem_init(void)
744 {
745
746 simple_lock_init(&ksem_slock);
747 exithook_establish(ksem_exithook, NULL);
748 exechook_establish(ksem_exithook, NULL);
749 forkhook_establish(ksem_forkhook);
750 }
751