FreeBSD/Linux Kernel Cross Reference
sys/kern/sysv_sem.c
1 /*-
2 * Implementation of SVID semaphores
3 *
4 * Author: Daniel Boulet
5 *
6 * This software is provided ``AS IS'' without any warranties of any kind.
7 */
8 /*-
9 * Copyright (c) 2003-2005 McAfee, Inc.
10 * All rights reserved.
11 *
12 * This software was developed for the FreeBSD Project in part by McAfee
13 * Research, the Security Research Division of McAfee, Inc under DARPA/SPAWAR
14 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS research
15 * program.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41
42 #include "opt_compat.h"
43 #include "opt_sysvipc.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/sysproto.h>
48 #include <sys/abi_compat.h>
49 #include <sys/eventhandler.h>
50 #include <sys/kernel.h>
51 #include <sys/proc.h>
52 #include <sys/lock.h>
53 #include <sys/module.h>
54 #include <sys/mutex.h>
55 #include <sys/racct.h>
56 #include <sys/sem.h>
57 #include <sys/sx.h>
58 #include <sys/syscall.h>
59 #include <sys/syscallsubr.h>
60 #include <sys/sysent.h>
61 #include <sys/sysctl.h>
62 #include <sys/uio.h>
63 #include <sys/malloc.h>
64 #include <sys/jail.h>
65
66 #include <security/mac/mac_framework.h>
67
68 FEATURE(sysv_sem, "System V semaphores support");
69
70 static MALLOC_DEFINE(M_SEM, "sem", "SVID compatible semaphores");
71
72 #ifdef SEM_DEBUG
73 #define DPRINTF(a) printf a
74 #else
75 #define DPRINTF(a)
76 #endif
77
78 static int seminit(void);
79 static int sysvsem_modload(struct module *, int, void *);
80 static int semunload(void);
81 static void semexit_myhook(void *arg, struct proc *p);
82 static int sysctl_sema(SYSCTL_HANDLER_ARGS);
83 static int semvalid(int semid, struct prison *rpr,
84 struct semid_kernel *semakptr);
85 static void sem_remove(int semidx, struct ucred *cred);
86 static struct prison *sem_find_prison(struct ucred *);
87 static int sem_prison_cansee(struct prison *, struct semid_kernel *);
88 static int sem_prison_check(void *, void *);
89 static int sem_prison_set(void *, void *);
90 static int sem_prison_get(void *, void *);
91 static int sem_prison_remove(void *, void *);
92 static void sem_prison_cleanup(struct prison *);
93
94 #ifndef _SYS_SYSPROTO_H_
95 struct __semctl_args;
96 int __semctl(struct thread *td, struct __semctl_args *uap);
97 struct semget_args;
98 int semget(struct thread *td, struct semget_args *uap);
99 struct semop_args;
100 int semop(struct thread *td, struct semop_args *uap);
101 #endif
102
103 static struct sem_undo *semu_alloc(struct thread *td);
104 static int semundo_adjust(struct thread *td, struct sem_undo **supptr,
105 int semid, int semseq, int semnum, int adjval);
106 static void semundo_clear(int semid, int semnum);
107
108 static struct mtx sem_mtx; /* semaphore global lock */
109 static struct mtx sem_undo_mtx;
110 static int semtot = 0;
111 static struct semid_kernel *sema; /* semaphore id pool */
112 static struct mtx *sema_mtx; /* semaphore id pool mutexes*/
113 static struct sem *sem; /* semaphore pool */
114 LIST_HEAD(, sem_undo) semu_list; /* list of active undo structures */
115 LIST_HEAD(, sem_undo) semu_free_list; /* list of free undo structures */
116 static int *semu; /* undo structure pool */
117 static eventhandler_tag semexit_tag;
118 static unsigned sem_prison_slot; /* prison OSD slot */
119
120 #define SEMUNDO_MTX sem_undo_mtx
121 #define SEMUNDO_LOCK() mtx_lock(&SEMUNDO_MTX);
122 #define SEMUNDO_UNLOCK() mtx_unlock(&SEMUNDO_MTX);
123 #define SEMUNDO_LOCKASSERT(how) mtx_assert(&SEMUNDO_MTX, (how));
124
125 struct sem {
126 u_short semval; /* semaphore value */
127 pid_t sempid; /* pid of last operation */
128 u_short semncnt; /* # awaiting semval > cval */
129 u_short semzcnt; /* # awaiting semval = 0 */
130 };
131
132 /*
133 * Undo structure (one per process)
134 */
135 struct sem_undo {
136 LIST_ENTRY(sem_undo) un_next; /* ptr to next active undo structure */
137 struct proc *un_proc; /* owner of this structure */
138 short un_cnt; /* # of active entries */
139 struct undo {
140 short un_adjval; /* adjust on exit values */
141 short un_num; /* semaphore # */
142 int un_id; /* semid */
143 unsigned short un_seq;
144 } un_ent[1]; /* undo entries */
145 };
146
147 /*
148 * Configuration parameters
149 */
150 #ifndef SEMMNI
151 #define SEMMNI 50 /* # of semaphore identifiers */
152 #endif
153 #ifndef SEMMNS
154 #define SEMMNS 340 /* # of semaphores in system */
155 #endif
156 #ifndef SEMUME
157 #define SEMUME 50 /* max # of undo entries per process */
158 #endif
159 #ifndef SEMMNU
160 #define SEMMNU 150 /* # of undo structures in system */
161 #endif
162
163 /* shouldn't need tuning */
164 #ifndef SEMMSL
165 #define SEMMSL SEMMNS /* max # of semaphores per id */
166 #endif
167 #ifndef SEMOPM
168 #define SEMOPM 100 /* max # of operations per semop call */
169 #endif
170
171 #define SEMVMX 32767 /* semaphore maximum value */
172 #define SEMAEM 16384 /* adjust on exit max value */
173
174 /*
175 * Due to the way semaphore memory is allocated, we have to ensure that
176 * SEMUSZ is properly aligned.
177 */
178
179 #define SEM_ALIGN(bytes) roundup2(bytes, sizeof(long))
180
181 /* actual size of an undo structure */
182 #define SEMUSZ(x) SEM_ALIGN(offsetof(struct sem_undo, un_ent[(x)]))
183
184 /*
185 * Macro to find a particular sem_undo vector
186 */
187 #define SEMU(ix) \
188 ((struct sem_undo *)(((intptr_t)semu) + (ix) * seminfo.semusz))
189
190 /*
191 * semaphore info struct
192 */
193 struct seminfo seminfo = {
194 .semmni = SEMMNI, /* # of semaphore identifiers */
195 .semmns = SEMMNS, /* # of semaphores in system */
196 .semmnu = SEMMNU, /* # of undo structures in system */
197 .semmsl = SEMMSL, /* max # of semaphores per id */
198 .semopm = SEMOPM, /* max # of operations per semop call */
199 .semume = SEMUME, /* max # of undo entries per process */
200 .semusz = SEMUSZ(SEMUME), /* size in bytes of undo structure */
201 .semvmx = SEMVMX, /* semaphore maximum value */
202 .semaem = SEMAEM, /* adjust on exit max value */
203 };
204
205 SYSCTL_INT(_kern_ipc, OID_AUTO, semmni, CTLFLAG_RDTUN, &seminfo.semmni, 0,
206 "Number of semaphore identifiers");
207 SYSCTL_INT(_kern_ipc, OID_AUTO, semmns, CTLFLAG_RDTUN, &seminfo.semmns, 0,
208 "Maximum number of semaphores in the system");
209 SYSCTL_INT(_kern_ipc, OID_AUTO, semmnu, CTLFLAG_RDTUN, &seminfo.semmnu, 0,
210 "Maximum number of undo structures in the system");
211 SYSCTL_INT(_kern_ipc, OID_AUTO, semmsl, CTLFLAG_RWTUN, &seminfo.semmsl, 0,
212 "Max semaphores per id");
213 SYSCTL_INT(_kern_ipc, OID_AUTO, semopm, CTLFLAG_RDTUN, &seminfo.semopm, 0,
214 "Max operations per semop call");
215 SYSCTL_INT(_kern_ipc, OID_AUTO, semume, CTLFLAG_RDTUN, &seminfo.semume, 0,
216 "Max undo entries per process");
217 SYSCTL_INT(_kern_ipc, OID_AUTO, semusz, CTLFLAG_RD, &seminfo.semusz, 0,
218 "Size in bytes of undo structure");
219 SYSCTL_INT(_kern_ipc, OID_AUTO, semvmx, CTLFLAG_RWTUN, &seminfo.semvmx, 0,
220 "Semaphore maximum value");
221 SYSCTL_INT(_kern_ipc, OID_AUTO, semaem, CTLFLAG_RWTUN, &seminfo.semaem, 0,
222 "Adjust on exit max value");
223 SYSCTL_PROC(_kern_ipc, OID_AUTO, sema,
224 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE,
225 NULL, 0, sysctl_sema, "",
226 "Array of struct semid_kernel for each potential semaphore");
227
228 static struct syscall_helper_data sem_syscalls[] = {
229 SYSCALL_INIT_HELPER(__semctl),
230 SYSCALL_INIT_HELPER(semget),
231 SYSCALL_INIT_HELPER(semop),
232 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
233 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
234 SYSCALL_INIT_HELPER(semsys),
235 SYSCALL_INIT_HELPER_COMPAT(freebsd7___semctl),
236 #endif
237 SYSCALL_INIT_LAST
238 };
239
240 #ifdef COMPAT_FREEBSD32
241 #include <compat/freebsd32/freebsd32.h>
242 #include <compat/freebsd32/freebsd32_ipc.h>
243 #include <compat/freebsd32/freebsd32_proto.h>
244 #include <compat/freebsd32/freebsd32_signal.h>
245 #include <compat/freebsd32/freebsd32_syscall.h>
246 #include <compat/freebsd32/freebsd32_util.h>
247
248 static struct syscall_helper_data sem32_syscalls[] = {
249 SYSCALL32_INIT_HELPER(freebsd32_semctl),
250 SYSCALL32_INIT_HELPER_COMPAT(semget),
251 SYSCALL32_INIT_HELPER_COMPAT(semop),
252 SYSCALL32_INIT_HELPER(freebsd32_semsys),
253 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
254 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
255 SYSCALL32_INIT_HELPER(freebsd7_freebsd32_semctl),
256 #endif
257 SYSCALL_INIT_LAST
258 };
259 #endif
260
261 static int
262 seminit(void)
263 {
264 struct prison *pr;
265 void **rsv;
266 int i, error;
267 osd_method_t methods[PR_MAXMETHOD] = {
268 [PR_METHOD_CHECK] = sem_prison_check,
269 [PR_METHOD_SET] = sem_prison_set,
270 [PR_METHOD_GET] = sem_prison_get,
271 [PR_METHOD_REMOVE] = sem_prison_remove,
272 };
273
274 sem = malloc(sizeof(struct sem) * seminfo.semmns, M_SEM, M_WAITOK);
275 sema = malloc(sizeof(struct semid_kernel) * seminfo.semmni, M_SEM,
276 M_WAITOK | M_ZERO);
277 sema_mtx = malloc(sizeof(struct mtx) * seminfo.semmni, M_SEM,
278 M_WAITOK | M_ZERO);
279 seminfo.semusz = SEMUSZ(seminfo.semume);
280 semu = malloc(seminfo.semmnu * seminfo.semusz, M_SEM, M_WAITOK);
281
282 for (i = 0; i < seminfo.semmni; i++) {
283 sema[i].u.sem_base = 0;
284 sema[i].u.sem_perm.mode = 0;
285 sema[i].u.sem_perm.seq = 0;
286 #ifdef MAC
287 mac_sysvsem_init(&sema[i]);
288 #endif
289 }
290 for (i = 0; i < seminfo.semmni; i++)
291 mtx_init(&sema_mtx[i], "semid", NULL, MTX_DEF);
292 LIST_INIT(&semu_free_list);
293 for (i = 0; i < seminfo.semmnu; i++) {
294 struct sem_undo *suptr = SEMU(i);
295 suptr->un_proc = NULL;
296 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
297 }
298 LIST_INIT(&semu_list);
299 mtx_init(&sem_mtx, "sem", NULL, MTX_DEF);
300 mtx_init(&sem_undo_mtx, "semu", NULL, MTX_DEF);
301 semexit_tag = EVENTHANDLER_REGISTER(process_exit, semexit_myhook, NULL,
302 EVENTHANDLER_PRI_ANY);
303
304 /* Set current prisons according to their allow.sysvipc. */
305 sem_prison_slot = osd_jail_register(NULL, methods);
306 rsv = osd_reserve(sem_prison_slot);
307 prison_lock(&prison0);
308 (void)osd_jail_set_reserved(&prison0, sem_prison_slot, rsv, &prison0);
309 prison_unlock(&prison0);
310 rsv = NULL;
311 sx_slock(&allprison_lock);
312 TAILQ_FOREACH(pr, &allprison, pr_list) {
313 if (rsv == NULL)
314 rsv = osd_reserve(sem_prison_slot);
315 prison_lock(pr);
316 if ((pr->pr_allow & PR_ALLOW_SYSVIPC) && pr->pr_ref > 0) {
317 (void)osd_jail_set_reserved(pr, sem_prison_slot, rsv,
318 &prison0);
319 rsv = NULL;
320 }
321 prison_unlock(pr);
322 }
323 if (rsv != NULL)
324 osd_free_reserved(rsv);
325 sx_sunlock(&allprison_lock);
326
327 error = syscall_helper_register(sem_syscalls, SY_THR_STATIC_KLD);
328 if (error != 0)
329 return (error);
330 #ifdef COMPAT_FREEBSD32
331 error = syscall32_helper_register(sem32_syscalls, SY_THR_STATIC_KLD);
332 if (error != 0)
333 return (error);
334 #endif
335 return (0);
336 }
337
338 static int
339 semunload(void)
340 {
341 int i;
342
343 /* XXXKIB */
344 if (semtot != 0)
345 return (EBUSY);
346
347 #ifdef COMPAT_FREEBSD32
348 syscall32_helper_unregister(sem32_syscalls);
349 #endif
350 syscall_helper_unregister(sem_syscalls);
351 EVENTHANDLER_DEREGISTER(process_exit, semexit_tag);
352 if (sem_prison_slot != 0)
353 osd_jail_deregister(sem_prison_slot);
354 #ifdef MAC
355 for (i = 0; i < seminfo.semmni; i++)
356 mac_sysvsem_destroy(&sema[i]);
357 #endif
358 free(sem, M_SEM);
359 free(sema, M_SEM);
360 free(semu, M_SEM);
361 for (i = 0; i < seminfo.semmni; i++)
362 mtx_destroy(&sema_mtx[i]);
363 free(sema_mtx, M_SEM);
364 mtx_destroy(&sem_mtx);
365 mtx_destroy(&sem_undo_mtx);
366 return (0);
367 }
368
369 static int
370 sysvsem_modload(struct module *module, int cmd, void *arg)
371 {
372 int error = 0;
373
374 switch (cmd) {
375 case MOD_LOAD:
376 error = seminit();
377 if (error != 0)
378 semunload();
379 break;
380 case MOD_UNLOAD:
381 error = semunload();
382 break;
383 case MOD_SHUTDOWN:
384 break;
385 default:
386 error = EINVAL;
387 break;
388 }
389 return (error);
390 }
391
392 static moduledata_t sysvsem_mod = {
393 "sysvsem",
394 &sysvsem_modload,
395 NULL
396 };
397
398 DECLARE_MODULE(sysvsem, sysvsem_mod, SI_SUB_SYSV_SEM, SI_ORDER_FIRST);
399 MODULE_VERSION(sysvsem, 1);
400
401 /*
402 * Allocate a new sem_undo structure for a process
403 * (returns ptr to structure or NULL if no more room)
404 */
405
406 static struct sem_undo *
407 semu_alloc(struct thread *td)
408 {
409 struct sem_undo *suptr;
410
411 SEMUNDO_LOCKASSERT(MA_OWNED);
412 if ((suptr = LIST_FIRST(&semu_free_list)) == NULL)
413 return (NULL);
414 LIST_REMOVE(suptr, un_next);
415 LIST_INSERT_HEAD(&semu_list, suptr, un_next);
416 suptr->un_cnt = 0;
417 suptr->un_proc = td->td_proc;
418 return (suptr);
419 }
420
421 static int
422 semu_try_free(struct sem_undo *suptr)
423 {
424
425 SEMUNDO_LOCKASSERT(MA_OWNED);
426
427 if (suptr->un_cnt != 0)
428 return (0);
429 LIST_REMOVE(suptr, un_next);
430 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
431 return (1);
432 }
433
434 /*
435 * Adjust a particular entry for a particular proc
436 */
437
438 static int
439 semundo_adjust(struct thread *td, struct sem_undo **supptr, int semid,
440 int semseq, int semnum, int adjval)
441 {
442 struct proc *p = td->td_proc;
443 struct sem_undo *suptr;
444 struct undo *sunptr;
445 int i;
446
447 SEMUNDO_LOCKASSERT(MA_OWNED);
448 /* Look for and remember the sem_undo if the caller doesn't provide
449 it */
450
451 suptr = *supptr;
452 if (suptr == NULL) {
453 LIST_FOREACH(suptr, &semu_list, un_next) {
454 if (suptr->un_proc == p) {
455 *supptr = suptr;
456 break;
457 }
458 }
459 if (suptr == NULL) {
460 if (adjval == 0)
461 return(0);
462 suptr = semu_alloc(td);
463 if (suptr == NULL)
464 return (ENOSPC);
465 *supptr = suptr;
466 }
467 }
468
469 /*
470 * Look for the requested entry and adjust it (delete if adjval becomes
471 * 0).
472 */
473 sunptr = &suptr->un_ent[0];
474 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
475 if (sunptr->un_id != semid || sunptr->un_num != semnum)
476 continue;
477 if (adjval != 0) {
478 adjval += sunptr->un_adjval;
479 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
480 return (ERANGE);
481 }
482 sunptr->un_adjval = adjval;
483 if (sunptr->un_adjval == 0) {
484 suptr->un_cnt--;
485 if (i < suptr->un_cnt)
486 suptr->un_ent[i] =
487 suptr->un_ent[suptr->un_cnt];
488 if (suptr->un_cnt == 0)
489 semu_try_free(suptr);
490 }
491 return (0);
492 }
493
494 /* Didn't find the right entry - create it */
495 if (adjval == 0)
496 return (0);
497 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
498 return (ERANGE);
499 if (suptr->un_cnt != seminfo.semume) {
500 sunptr = &suptr->un_ent[suptr->un_cnt];
501 suptr->un_cnt++;
502 sunptr->un_adjval = adjval;
503 sunptr->un_id = semid;
504 sunptr->un_num = semnum;
505 sunptr->un_seq = semseq;
506 } else
507 return (EINVAL);
508 return (0);
509 }
510
511 static void
512 semundo_clear(int semid, int semnum)
513 {
514 struct sem_undo *suptr, *suptr1;
515 struct undo *sunptr;
516 int i;
517
518 SEMUNDO_LOCKASSERT(MA_OWNED);
519 LIST_FOREACH_SAFE(suptr, &semu_list, un_next, suptr1) {
520 sunptr = &suptr->un_ent[0];
521 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
522 if (sunptr->un_id != semid)
523 continue;
524 if (semnum == -1 || sunptr->un_num == semnum) {
525 suptr->un_cnt--;
526 if (i < suptr->un_cnt) {
527 suptr->un_ent[i] =
528 suptr->un_ent[suptr->un_cnt];
529 continue;
530 }
531 semu_try_free(suptr);
532 }
533 if (semnum != -1)
534 break;
535 }
536 }
537 }
538
539 static int
540 semvalid(int semid, struct prison *rpr, struct semid_kernel *semakptr)
541 {
542
543 return ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
544 semakptr->u.sem_perm.seq != IPCID_TO_SEQ(semid) ||
545 sem_prison_cansee(rpr, semakptr) ? EINVAL : 0);
546 }
547
548 static void
549 sem_remove(int semidx, struct ucred *cred)
550 {
551 struct semid_kernel *semakptr;
552 int i;
553
554 KASSERT(semidx >= 0 && semidx < seminfo.semmni,
555 ("semidx out of bounds"));
556 mtx_assert(&sem_mtx, MA_OWNED);
557 semakptr = &sema[semidx];
558 KASSERT(semakptr->u.sem_base - sem + semakptr->u.sem_nsems <= semtot,
559 ("sem_remove: sema %d corrupted sem pointer %p %p %d %d",
560 semidx, semakptr->u.sem_base, sem, semakptr->u.sem_nsems,
561 semtot));
562
563 semakptr->u.sem_perm.cuid = cred ? cred->cr_uid : 0;
564 semakptr->u.sem_perm.uid = cred ? cred->cr_uid : 0;
565 semakptr->u.sem_perm.mode = 0;
566 racct_sub_cred(semakptr->cred, RACCT_NSEM, semakptr->u.sem_nsems);
567 crfree(semakptr->cred);
568 semakptr->cred = NULL;
569 SEMUNDO_LOCK();
570 semundo_clear(semidx, -1);
571 SEMUNDO_UNLOCK();
572 #ifdef MAC
573 mac_sysvsem_cleanup(semakptr);
574 #endif
575 wakeup(semakptr);
576 for (i = 0; i < seminfo.semmni; i++) {
577 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
578 sema[i].u.sem_base > semakptr->u.sem_base)
579 mtx_lock_flags(&sema_mtx[i], LOP_DUPOK);
580 }
581 for (i = semakptr->u.sem_base - sem + semakptr->u.sem_nsems;
582 i < semtot; i++)
583 sem[i - semakptr->u.sem_nsems] = sem[i];
584 for (i = 0; i < seminfo.semmni; i++) {
585 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
586 sema[i].u.sem_base > semakptr->u.sem_base) {
587 sema[i].u.sem_base -= semakptr->u.sem_nsems;
588 mtx_unlock(&sema_mtx[i]);
589 }
590 }
591 semtot -= semakptr->u.sem_nsems;
592 }
593
594 static struct prison *
595 sem_find_prison(struct ucred *cred)
596 {
597 struct prison *pr, *rpr;
598
599 pr = cred->cr_prison;
600 prison_lock(pr);
601 rpr = osd_jail_get(pr, sem_prison_slot);
602 prison_unlock(pr);
603 return rpr;
604 }
605
606 static int
607 sem_prison_cansee(struct prison *rpr, struct semid_kernel *semakptr)
608 {
609
610 if (semakptr->cred == NULL ||
611 !(rpr == semakptr->cred->cr_prison ||
612 prison_ischild(rpr, semakptr->cred->cr_prison)))
613 return (EINVAL);
614 return (0);
615 }
616
617 /*
618 * Note that the user-mode half of this passes a union, not a pointer.
619 */
620 #ifndef _SYS_SYSPROTO_H_
621 struct __semctl_args {
622 int semid;
623 int semnum;
624 int cmd;
625 union semun *arg;
626 };
627 #endif
628 int
629 sys___semctl(struct thread *td, struct __semctl_args *uap)
630 {
631 struct semid_ds dsbuf;
632 union semun arg, semun;
633 register_t rval;
634 int error;
635
636 switch (uap->cmd) {
637 case SEM_STAT:
638 case IPC_SET:
639 case IPC_STAT:
640 case GETALL:
641 case SETVAL:
642 case SETALL:
643 error = copyin(uap->arg, &arg, sizeof(arg));
644 if (error)
645 return (error);
646 break;
647 }
648
649 switch (uap->cmd) {
650 case SEM_STAT:
651 case IPC_STAT:
652 semun.buf = &dsbuf;
653 break;
654 case IPC_SET:
655 error = copyin(arg.buf, &dsbuf, sizeof(dsbuf));
656 if (error)
657 return (error);
658 semun.buf = &dsbuf;
659 break;
660 case GETALL:
661 case SETALL:
662 semun.array = arg.array;
663 break;
664 case SETVAL:
665 semun.val = arg.val;
666 break;
667 }
668
669 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
670 &rval);
671 if (error)
672 return (error);
673
674 switch (uap->cmd) {
675 case SEM_STAT:
676 case IPC_STAT:
677 error = copyout(&dsbuf, arg.buf, sizeof(dsbuf));
678 break;
679 }
680
681 if (error == 0)
682 td->td_retval[0] = rval;
683 return (error);
684 }
685
686 int
687 kern_semctl(struct thread *td, int semid, int semnum, int cmd,
688 union semun *arg, register_t *rval)
689 {
690 u_short *array;
691 struct ucred *cred = td->td_ucred;
692 int i, error;
693 struct prison *rpr;
694 struct semid_ds *sbuf;
695 struct semid_kernel *semakptr;
696 struct mtx *sema_mtxp;
697 u_short usval, count;
698 int semidx;
699
700 DPRINTF(("call to semctl(%d, %d, %d, 0x%p)\n",
701 semid, semnum, cmd, arg));
702
703 rpr = sem_find_prison(td->td_ucred);
704 if (sem == NULL)
705 return (ENOSYS);
706
707 array = NULL;
708
709 switch(cmd) {
710 case SEM_STAT:
711 /*
712 * For this command we assume semid is an array index
713 * rather than an IPC id.
714 */
715 if (semid < 0 || semid >= seminfo.semmni)
716 return (EINVAL);
717 semakptr = &sema[semid];
718 sema_mtxp = &sema_mtx[semid];
719 mtx_lock(sema_mtxp);
720 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
721 error = EINVAL;
722 goto done2;
723 }
724 if ((error = sem_prison_cansee(rpr, semakptr)))
725 goto done2;
726 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
727 goto done2;
728 #ifdef MAC
729 error = mac_sysvsem_check_semctl(cred, semakptr, cmd);
730 if (error != 0)
731 goto done2;
732 #endif
733 bcopy(&semakptr->u, arg->buf, sizeof(struct semid_ds));
734 if (cred->cr_prison != semakptr->cred->cr_prison)
735 arg->buf->sem_perm.key = IPC_PRIVATE;
736 *rval = IXSEQ_TO_IPCID(semid, semakptr->u.sem_perm);
737 mtx_unlock(sema_mtxp);
738 return (0);
739 }
740
741 semidx = IPCID_TO_IX(semid);
742 if (semidx < 0 || semidx >= seminfo.semmni)
743 return (EINVAL);
744
745 semakptr = &sema[semidx];
746 sema_mtxp = &sema_mtx[semidx];
747 if (cmd == IPC_RMID)
748 mtx_lock(&sem_mtx);
749 mtx_lock(sema_mtxp);
750
751 #ifdef MAC
752 error = mac_sysvsem_check_semctl(cred, semakptr, cmd);
753 if (error != 0)
754 goto done2;
755 #endif
756
757 error = 0;
758 *rval = 0;
759
760 switch (cmd) {
761 case IPC_RMID:
762 if ((error = semvalid(semid, rpr, semakptr)) != 0)
763 goto done2;
764 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
765 goto done2;
766 sem_remove(semidx, cred);
767 break;
768
769 case IPC_SET:
770 if ((error = semvalid(semid, rpr, semakptr)) != 0)
771 goto done2;
772 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
773 goto done2;
774 sbuf = arg->buf;
775 semakptr->u.sem_perm.uid = sbuf->sem_perm.uid;
776 semakptr->u.sem_perm.gid = sbuf->sem_perm.gid;
777 semakptr->u.sem_perm.mode = (semakptr->u.sem_perm.mode &
778 ~0777) | (sbuf->sem_perm.mode & 0777);
779 semakptr->u.sem_ctime = time_second;
780 break;
781
782 case IPC_STAT:
783 if ((error = semvalid(semid, rpr, semakptr)) != 0)
784 goto done2;
785 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
786 goto done2;
787 bcopy(&semakptr->u, arg->buf, sizeof(struct semid_ds));
788 if (cred->cr_prison != semakptr->cred->cr_prison)
789 arg->buf->sem_perm.key = IPC_PRIVATE;
790 break;
791
792 case GETNCNT:
793 if ((error = semvalid(semid, rpr, semakptr)) != 0)
794 goto done2;
795 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
796 goto done2;
797 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
798 error = EINVAL;
799 goto done2;
800 }
801 *rval = semakptr->u.sem_base[semnum].semncnt;
802 break;
803
804 case GETPID:
805 if ((error = semvalid(semid, rpr, semakptr)) != 0)
806 goto done2;
807 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
808 goto done2;
809 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
810 error = EINVAL;
811 goto done2;
812 }
813 *rval = semakptr->u.sem_base[semnum].sempid;
814 break;
815
816 case GETVAL:
817 if ((error = semvalid(semid, rpr, semakptr)) != 0)
818 goto done2;
819 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
820 goto done2;
821 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
822 error = EINVAL;
823 goto done2;
824 }
825 *rval = semakptr->u.sem_base[semnum].semval;
826 break;
827
828 case GETALL:
829 /*
830 * Unfortunately, callers of this function don't know
831 * in advance how many semaphores are in this set.
832 * While we could just allocate the maximum size array
833 * and pass the actual size back to the caller, that
834 * won't work for SETALL since we can't copyin() more
835 * data than the user specified as we may return a
836 * spurious EFAULT.
837 *
838 * Note that the number of semaphores in a set is
839 * fixed for the life of that set. The only way that
840 * the 'count' could change while are blocked in
841 * malloc() is if this semaphore set were destroyed
842 * and a new one created with the same index.
843 * However, semvalid() will catch that due to the
844 * sequence number unless exactly 0x8000 (or a
845 * multiple thereof) semaphore sets for the same index
846 * are created and destroyed while we are in malloc!
847 *
848 */
849 count = semakptr->u.sem_nsems;
850 mtx_unlock(sema_mtxp);
851 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK);
852 mtx_lock(sema_mtxp);
853 if ((error = semvalid(semid, rpr, semakptr)) != 0)
854 goto done2;
855 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed"));
856 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
857 goto done2;
858 for (i = 0; i < semakptr->u.sem_nsems; i++)
859 array[i] = semakptr->u.sem_base[i].semval;
860 mtx_unlock(sema_mtxp);
861 error = copyout(array, arg->array, count * sizeof(*array));
862 mtx_lock(sema_mtxp);
863 break;
864
865 case GETZCNT:
866 if ((error = semvalid(semid, rpr, semakptr)) != 0)
867 goto done2;
868 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
869 goto done2;
870 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
871 error = EINVAL;
872 goto done2;
873 }
874 *rval = semakptr->u.sem_base[semnum].semzcnt;
875 break;
876
877 case SETVAL:
878 if ((error = semvalid(semid, rpr, semakptr)) != 0)
879 goto done2;
880 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
881 goto done2;
882 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
883 error = EINVAL;
884 goto done2;
885 }
886 if (arg->val < 0 || arg->val > seminfo.semvmx) {
887 error = ERANGE;
888 goto done2;
889 }
890 semakptr->u.sem_base[semnum].semval = arg->val;
891 SEMUNDO_LOCK();
892 semundo_clear(semidx, semnum);
893 SEMUNDO_UNLOCK();
894 wakeup(semakptr);
895 break;
896
897 case SETALL:
898 /*
899 * See comment on GETALL for why 'count' shouldn't change
900 * and why we require a userland buffer.
901 */
902 count = semakptr->u.sem_nsems;
903 mtx_unlock(sema_mtxp);
904 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK);
905 error = copyin(arg->array, array, count * sizeof(*array));
906 mtx_lock(sema_mtxp);
907 if (error)
908 break;
909 if ((error = semvalid(semid, rpr, semakptr)) != 0)
910 goto done2;
911 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed"));
912 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
913 goto done2;
914 for (i = 0; i < semakptr->u.sem_nsems; i++) {
915 usval = array[i];
916 if (usval > seminfo.semvmx) {
917 error = ERANGE;
918 break;
919 }
920 semakptr->u.sem_base[i].semval = usval;
921 }
922 SEMUNDO_LOCK();
923 semundo_clear(semidx, -1);
924 SEMUNDO_UNLOCK();
925 wakeup(semakptr);
926 break;
927
928 default:
929 error = EINVAL;
930 break;
931 }
932
933 done2:
934 mtx_unlock(sema_mtxp);
935 if (cmd == IPC_RMID)
936 mtx_unlock(&sem_mtx);
937 if (array != NULL)
938 free(array, M_TEMP);
939 return(error);
940 }
941
942 #ifndef _SYS_SYSPROTO_H_
943 struct semget_args {
944 key_t key;
945 int nsems;
946 int semflg;
947 };
948 #endif
949 int
950 sys_semget(struct thread *td, struct semget_args *uap)
951 {
952 int semid, error = 0;
953 int key = uap->key;
954 int nsems = uap->nsems;
955 int semflg = uap->semflg;
956 struct ucred *cred = td->td_ucred;
957
958 DPRINTF(("semget(0x%x, %d, 0%o)\n", key, nsems, semflg));
959
960 if (sem_find_prison(cred) == NULL)
961 return (ENOSYS);
962
963 mtx_lock(&sem_mtx);
964 if (key != IPC_PRIVATE) {
965 for (semid = 0; semid < seminfo.semmni; semid++) {
966 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) &&
967 sema[semid].cred != NULL &&
968 sema[semid].cred->cr_prison == cred->cr_prison &&
969 sema[semid].u.sem_perm.key == key)
970 break;
971 }
972 if (semid < seminfo.semmni) {
973 DPRINTF(("found public key\n"));
974 if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
975 DPRINTF(("not exclusive\n"));
976 error = EEXIST;
977 goto done2;
978 }
979 if ((error = ipcperm(td, &sema[semid].u.sem_perm,
980 semflg & 0700))) {
981 goto done2;
982 }
983 if (nsems > 0 && sema[semid].u.sem_nsems < nsems) {
984 DPRINTF(("too small\n"));
985 error = EINVAL;
986 goto done2;
987 }
988 #ifdef MAC
989 error = mac_sysvsem_check_semget(cred, &sema[semid]);
990 if (error != 0)
991 goto done2;
992 #endif
993 goto found;
994 }
995 }
996
997 DPRINTF(("need to allocate the semid_kernel\n"));
998 if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
999 if (nsems <= 0 || nsems > seminfo.semmsl) {
1000 DPRINTF(("nsems out of range (0<%d<=%d)\n", nsems,
1001 seminfo.semmsl));
1002 error = EINVAL;
1003 goto done2;
1004 }
1005 if (nsems > seminfo.semmns - semtot) {
1006 DPRINTF((
1007 "not enough semaphores left (need %d, got %d)\n",
1008 nsems, seminfo.semmns - semtot));
1009 error = ENOSPC;
1010 goto done2;
1011 }
1012 for (semid = 0; semid < seminfo.semmni; semid++) {
1013 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0)
1014 break;
1015 }
1016 if (semid == seminfo.semmni) {
1017 DPRINTF(("no more semid_kernel's available\n"));
1018 error = ENOSPC;
1019 goto done2;
1020 }
1021 #ifdef RACCT
1022 if (racct_enable) {
1023 PROC_LOCK(td->td_proc);
1024 error = racct_add(td->td_proc, RACCT_NSEM, nsems);
1025 PROC_UNLOCK(td->td_proc);
1026 if (error != 0) {
1027 error = ENOSPC;
1028 goto done2;
1029 }
1030 }
1031 #endif
1032 DPRINTF(("semid %d is available\n", semid));
1033 mtx_lock(&sema_mtx[semid]);
1034 KASSERT((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0,
1035 ("Lost semaphore %d", semid));
1036 sema[semid].u.sem_perm.key = key;
1037 sema[semid].u.sem_perm.cuid = cred->cr_uid;
1038 sema[semid].u.sem_perm.uid = cred->cr_uid;
1039 sema[semid].u.sem_perm.cgid = cred->cr_gid;
1040 sema[semid].u.sem_perm.gid = cred->cr_gid;
1041 sema[semid].u.sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
1042 sema[semid].cred = crhold(cred);
1043 sema[semid].u.sem_perm.seq =
1044 (sema[semid].u.sem_perm.seq + 1) & 0x7fff;
1045 sema[semid].u.sem_nsems = nsems;
1046 sema[semid].u.sem_otime = 0;
1047 sema[semid].u.sem_ctime = time_second;
1048 sema[semid].u.sem_base = &sem[semtot];
1049 semtot += nsems;
1050 bzero(sema[semid].u.sem_base,
1051 sizeof(sema[semid].u.sem_base[0])*nsems);
1052 #ifdef MAC
1053 mac_sysvsem_create(cred, &sema[semid]);
1054 #endif
1055 mtx_unlock(&sema_mtx[semid]);
1056 DPRINTF(("sembase = %p, next = %p\n",
1057 sema[semid].u.sem_base, &sem[semtot]));
1058 } else {
1059 DPRINTF(("didn't find it and wasn't asked to create it\n"));
1060 error = ENOENT;
1061 goto done2;
1062 }
1063
1064 found:
1065 td->td_retval[0] = IXSEQ_TO_IPCID(semid, sema[semid].u.sem_perm);
1066 done2:
1067 mtx_unlock(&sem_mtx);
1068 return (error);
1069 }
1070
1071 #ifndef _SYS_SYSPROTO_H_
1072 struct semop_args {
1073 int semid;
1074 struct sembuf *sops;
1075 size_t nsops;
1076 };
1077 #endif
1078 int
1079 sys_semop(struct thread *td, struct semop_args *uap)
1080 {
1081 #define SMALL_SOPS 8
1082 struct sembuf small_sops[SMALL_SOPS];
1083 int semid = uap->semid;
1084 size_t nsops = uap->nsops;
1085 struct prison *rpr;
1086 struct sembuf *sops;
1087 struct semid_kernel *semakptr;
1088 struct sembuf *sopptr = NULL;
1089 struct sem *semptr = NULL;
1090 struct sem_undo *suptr;
1091 struct mtx *sema_mtxp;
1092 size_t i, j, k;
1093 int error;
1094 int do_wakeup, do_undos;
1095 unsigned short seq;
1096
1097 #ifdef SEM_DEBUG
1098 sops = NULL;
1099 #endif
1100 DPRINTF(("call to semop(%d, %p, %u)\n", semid, sops, nsops));
1101
1102 rpr = sem_find_prison(td->td_ucred);
1103 if (sem == NULL)
1104 return (ENOSYS);
1105
1106 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
1107
1108 if (semid < 0 || semid >= seminfo.semmni)
1109 return (EINVAL);
1110
1111 /* Allocate memory for sem_ops */
1112 if (nsops <= SMALL_SOPS)
1113 sops = small_sops;
1114 else if (nsops > seminfo.semopm) {
1115 DPRINTF(("too many sops (max=%d, nsops=%d)\n", seminfo.semopm,
1116 nsops));
1117 return (E2BIG);
1118 } else {
1119 #ifdef RACCT
1120 if (racct_enable) {
1121 PROC_LOCK(td->td_proc);
1122 if (nsops >
1123 racct_get_available(td->td_proc, RACCT_NSEMOP)) {
1124 PROC_UNLOCK(td->td_proc);
1125 return (E2BIG);
1126 }
1127 PROC_UNLOCK(td->td_proc);
1128 }
1129 #endif
1130
1131 sops = malloc(nsops * sizeof(*sops), M_TEMP, M_WAITOK);
1132 }
1133 if ((error = copyin(uap->sops, sops, nsops * sizeof(sops[0]))) != 0) {
1134 DPRINTF(("error = %d from copyin(%p, %p, %d)\n", error,
1135 uap->sops, sops, nsops * sizeof(sops[0])));
1136 if (sops != small_sops)
1137 free(sops, M_SEM);
1138 return (error);
1139 }
1140
1141 semakptr = &sema[semid];
1142 sema_mtxp = &sema_mtx[semid];
1143 mtx_lock(sema_mtxp);
1144 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
1145 error = EINVAL;
1146 goto done2;
1147 }
1148 seq = semakptr->u.sem_perm.seq;
1149 if (seq != IPCID_TO_SEQ(uap->semid)) {
1150 error = EINVAL;
1151 goto done2;
1152 }
1153 if ((error = sem_prison_cansee(rpr, semakptr)) != 0)
1154 goto done2;
1155 /*
1156 * Initial pass through sops to see what permissions are needed.
1157 * Also perform any checks that don't need repeating on each
1158 * attempt to satisfy the request vector.
1159 */
1160 j = 0; /* permission needed */
1161 do_undos = 0;
1162 for (i = 0; i < nsops; i++) {
1163 sopptr = &sops[i];
1164 if (sopptr->sem_num >= semakptr->u.sem_nsems) {
1165 error = EFBIG;
1166 goto done2;
1167 }
1168 if (sopptr->sem_flg & SEM_UNDO && sopptr->sem_op != 0)
1169 do_undos = 1;
1170 j |= (sopptr->sem_op == 0) ? SEM_R : SEM_A;
1171 }
1172
1173 if ((error = ipcperm(td, &semakptr->u.sem_perm, j))) {
1174 DPRINTF(("error = %d from ipaccess\n", error));
1175 goto done2;
1176 }
1177 #ifdef MAC
1178 error = mac_sysvsem_check_semop(td->td_ucred, semakptr, j);
1179 if (error != 0)
1180 goto done2;
1181 #endif
1182
1183 /*
1184 * Loop trying to satisfy the vector of requests.
1185 * If we reach a point where we must wait, any requests already
1186 * performed are rolled back and we go to sleep until some other
1187 * process wakes us up. At this point, we start all over again.
1188 *
1189 * This ensures that from the perspective of other tasks, a set
1190 * of requests is atomic (never partially satisfied).
1191 */
1192 for (;;) {
1193 do_wakeup = 0;
1194 error = 0; /* error return if necessary */
1195
1196 for (i = 0; i < nsops; i++) {
1197 sopptr = &sops[i];
1198 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1199
1200 DPRINTF((
1201 "semop: semakptr=%p, sem_base=%p, "
1202 "semptr=%p, sem[%d]=%d : op=%d, flag=%s\n",
1203 semakptr, semakptr->u.sem_base, semptr,
1204 sopptr->sem_num, semptr->semval, sopptr->sem_op,
1205 (sopptr->sem_flg & IPC_NOWAIT) ?
1206 "nowait" : "wait"));
1207
1208 if (sopptr->sem_op < 0) {
1209 if (semptr->semval + sopptr->sem_op < 0) {
1210 DPRINTF(("semop: can't do it now\n"));
1211 break;
1212 } else {
1213 semptr->semval += sopptr->sem_op;
1214 if (semptr->semval == 0 &&
1215 semptr->semzcnt > 0)
1216 do_wakeup = 1;
1217 }
1218 } else if (sopptr->sem_op == 0) {
1219 if (semptr->semval != 0) {
1220 DPRINTF(("semop: not zero now\n"));
1221 break;
1222 }
1223 } else if (semptr->semval + sopptr->sem_op >
1224 seminfo.semvmx) {
1225 error = ERANGE;
1226 break;
1227 } else {
1228 if (semptr->semncnt > 0)
1229 do_wakeup = 1;
1230 semptr->semval += sopptr->sem_op;
1231 }
1232 }
1233
1234 /*
1235 * Did we get through the entire vector?
1236 */
1237 if (i >= nsops)
1238 goto done;
1239
1240 /*
1241 * No ... rollback anything that we've already done
1242 */
1243 DPRINTF(("semop: rollback 0 through %d\n", i-1));
1244 for (j = 0; j < i; j++)
1245 semakptr->u.sem_base[sops[j].sem_num].semval -=
1246 sops[j].sem_op;
1247
1248 /* If we detected an error, return it */
1249 if (error != 0)
1250 goto done2;
1251
1252 /*
1253 * If the request that we couldn't satisfy has the
1254 * NOWAIT flag set then return with EAGAIN.
1255 */
1256 if (sopptr->sem_flg & IPC_NOWAIT) {
1257 error = EAGAIN;
1258 goto done2;
1259 }
1260
1261 if (sopptr->sem_op == 0)
1262 semptr->semzcnt++;
1263 else
1264 semptr->semncnt++;
1265
1266 DPRINTF(("semop: good night!\n"));
1267 error = msleep(semakptr, sema_mtxp, (PZERO - 4) | PCATCH,
1268 "semwait", 0);
1269 DPRINTF(("semop: good morning (error=%d)!\n", error));
1270 /* return code is checked below, after sem[nz]cnt-- */
1271
1272 /*
1273 * Make sure that the semaphore still exists
1274 */
1275 seq = semakptr->u.sem_perm.seq;
1276 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
1277 seq != IPCID_TO_SEQ(uap->semid)) {
1278 error = EIDRM;
1279 goto done2;
1280 }
1281
1282 /*
1283 * Renew the semaphore's pointer after wakeup since
1284 * during msleep sem_base may have been modified and semptr
1285 * is not valid any more
1286 */
1287 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1288
1289 /*
1290 * The semaphore is still alive. Readjust the count of
1291 * waiting processes.
1292 */
1293 if (sopptr->sem_op == 0)
1294 semptr->semzcnt--;
1295 else
1296 semptr->semncnt--;
1297
1298 /*
1299 * Is it really morning, or was our sleep interrupted?
1300 * (Delayed check of msleep() return code because we
1301 * need to decrement sem[nz]cnt either way.)
1302 */
1303 if (error != 0) {
1304 error = EINTR;
1305 goto done2;
1306 }
1307 DPRINTF(("semop: good morning!\n"));
1308 }
1309
1310 done:
1311 /*
1312 * Process any SEM_UNDO requests.
1313 */
1314 if (do_undos) {
1315 SEMUNDO_LOCK();
1316 suptr = NULL;
1317 for (i = 0; i < nsops; i++) {
1318 /*
1319 * We only need to deal with SEM_UNDO's for non-zero
1320 * op's.
1321 */
1322 int adjval;
1323
1324 if ((sops[i].sem_flg & SEM_UNDO) == 0)
1325 continue;
1326 adjval = sops[i].sem_op;
1327 if (adjval == 0)
1328 continue;
1329 error = semundo_adjust(td, &suptr, semid, seq,
1330 sops[i].sem_num, -adjval);
1331 if (error == 0)
1332 continue;
1333
1334 /*
1335 * Oh-Oh! We ran out of either sem_undo's or undo's.
1336 * Rollback the adjustments to this point and then
1337 * rollback the semaphore ups and down so we can return
1338 * with an error with all structures restored. We
1339 * rollback the undo's in the exact reverse order that
1340 * we applied them. This guarantees that we won't run
1341 * out of space as we roll things back out.
1342 */
1343 for (j = 0; j < i; j++) {
1344 k = i - j - 1;
1345 if ((sops[k].sem_flg & SEM_UNDO) == 0)
1346 continue;
1347 adjval = sops[k].sem_op;
1348 if (adjval == 0)
1349 continue;
1350 if (semundo_adjust(td, &suptr, semid, seq,
1351 sops[k].sem_num, adjval) != 0)
1352 panic("semop - can't undo undos");
1353 }
1354
1355 for (j = 0; j < nsops; j++)
1356 semakptr->u.sem_base[sops[j].sem_num].semval -=
1357 sops[j].sem_op;
1358
1359 DPRINTF(("error = %d from semundo_adjust\n", error));
1360 SEMUNDO_UNLOCK();
1361 goto done2;
1362 } /* loop through the sops */
1363 SEMUNDO_UNLOCK();
1364 } /* if (do_undos) */
1365
1366 /* We're definitely done - set the sempid's and time */
1367 for (i = 0; i < nsops; i++) {
1368 sopptr = &sops[i];
1369 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1370 semptr->sempid = td->td_proc->p_pid;
1371 }
1372 semakptr->u.sem_otime = time_second;
1373
1374 /*
1375 * Do a wakeup if any semaphore was up'd whilst something was
1376 * sleeping on it.
1377 */
1378 if (do_wakeup) {
1379 DPRINTF(("semop: doing wakeup\n"));
1380 wakeup(semakptr);
1381 DPRINTF(("semop: back from wakeup\n"));
1382 }
1383 DPRINTF(("semop: done\n"));
1384 td->td_retval[0] = 0;
1385 done2:
1386 mtx_unlock(sema_mtxp);
1387 if (sops != small_sops)
1388 free(sops, M_SEM);
1389 return (error);
1390 }
1391
1392 /*
1393 * Go through the undo structures for this process and apply the adjustments to
1394 * semaphores.
1395 */
1396 static void
1397 semexit_myhook(void *arg, struct proc *p)
1398 {
1399 struct sem_undo *suptr;
1400 struct semid_kernel *semakptr;
1401 struct mtx *sema_mtxp;
1402 int semid, semnum, adjval, ix;
1403 unsigned short seq;
1404
1405 /*
1406 * Go through the chain of undo vectors looking for one
1407 * associated with this process.
1408 */
1409 SEMUNDO_LOCK();
1410 LIST_FOREACH(suptr, &semu_list, un_next) {
1411 if (suptr->un_proc == p)
1412 break;
1413 }
1414 if (suptr == NULL) {
1415 SEMUNDO_UNLOCK();
1416 return;
1417 }
1418 LIST_REMOVE(suptr, un_next);
1419
1420 DPRINTF(("proc @%p has undo structure with %d entries\n", p,
1421 suptr->un_cnt));
1422
1423 /*
1424 * If there are any active undo elements then process them.
1425 */
1426 if (suptr->un_cnt > 0) {
1427 SEMUNDO_UNLOCK();
1428 for (ix = 0; ix < suptr->un_cnt; ix++) {
1429 semid = suptr->un_ent[ix].un_id;
1430 semnum = suptr->un_ent[ix].un_num;
1431 adjval = suptr->un_ent[ix].un_adjval;
1432 seq = suptr->un_ent[ix].un_seq;
1433 semakptr = &sema[semid];
1434 sema_mtxp = &sema_mtx[semid];
1435
1436 mtx_lock(sema_mtxp);
1437 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
1438 (semakptr->u.sem_perm.seq != seq)) {
1439 mtx_unlock(sema_mtxp);
1440 continue;
1441 }
1442 if (semnum >= semakptr->u.sem_nsems)
1443 panic("semexit - semnum out of range");
1444
1445 DPRINTF((
1446 "semexit: %p id=%d num=%d(adj=%d) ; sem=%d\n",
1447 suptr->un_proc, suptr->un_ent[ix].un_id,
1448 suptr->un_ent[ix].un_num,
1449 suptr->un_ent[ix].un_adjval,
1450 semakptr->u.sem_base[semnum].semval));
1451
1452 if (adjval < 0 && semakptr->u.sem_base[semnum].semval <
1453 -adjval)
1454 semakptr->u.sem_base[semnum].semval = 0;
1455 else
1456 semakptr->u.sem_base[semnum].semval += adjval;
1457
1458 wakeup(semakptr);
1459 DPRINTF(("semexit: back from wakeup\n"));
1460 mtx_unlock(sema_mtxp);
1461 }
1462 SEMUNDO_LOCK();
1463 }
1464
1465 /*
1466 * Deallocate the undo vector.
1467 */
1468 DPRINTF(("removing vector\n"));
1469 suptr->un_proc = NULL;
1470 suptr->un_cnt = 0;
1471 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
1472 SEMUNDO_UNLOCK();
1473 }
1474
1475 static int
1476 sysctl_sema(SYSCTL_HANDLER_ARGS)
1477 {
1478 struct prison *pr, *rpr;
1479 struct semid_kernel tsemak;
1480 #ifdef COMPAT_FREEBSD32
1481 struct semid_kernel32 tsemak32;
1482 #endif
1483 void *outaddr;
1484 size_t outsize;
1485 int error, i;
1486
1487 pr = req->td->td_ucred->cr_prison;
1488 rpr = sem_find_prison(req->td->td_ucred);
1489 error = 0;
1490 for (i = 0; i < seminfo.semmni; i++) {
1491 mtx_lock(&sema_mtx[i]);
1492 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) == 0 ||
1493 rpr == NULL || sem_prison_cansee(rpr, &sema[i]) != 0)
1494 bzero(&tsemak, sizeof(tsemak));
1495 else {
1496 tsemak = sema[i];
1497 if (tsemak.cred->cr_prison != pr)
1498 tsemak.u.sem_perm.key = IPC_PRIVATE;
1499 }
1500 mtx_unlock(&sema_mtx[i]);
1501 #ifdef COMPAT_FREEBSD32
1502 if (SV_CURPROC_FLAG(SV_ILP32)) {
1503 bzero(&tsemak32, sizeof(tsemak32));
1504 freebsd32_ipcperm_out(&tsemak.u.sem_perm,
1505 &tsemak32.u.sem_perm);
1506 /* Don't copy u.sem_base */
1507 CP(tsemak, tsemak32, u.sem_nsems);
1508 CP(tsemak, tsemak32, u.sem_otime);
1509 CP(tsemak, tsemak32, u.sem_ctime);
1510 /* Don't copy label or cred */
1511 outaddr = &tsemak32;
1512 outsize = sizeof(tsemak32);
1513 } else
1514 #endif
1515 {
1516 tsemak.u.sem_base = NULL;
1517 tsemak.label = NULL;
1518 tsemak.cred = NULL;
1519 outaddr = &tsemak;
1520 outsize = sizeof(tsemak);
1521 }
1522 error = SYSCTL_OUT(req, outaddr, outsize);
1523 if (error != 0)
1524 break;
1525 }
1526 return (error);
1527 }
1528
1529 static int
1530 sem_prison_check(void *obj, void *data)
1531 {
1532 struct prison *pr = obj;
1533 struct prison *prpr;
1534 struct vfsoptlist *opts = data;
1535 int error, jsys;
1536
1537 /*
1538 * sysvsem is a jailsys integer.
1539 * It must be "disable" if the parent jail is disabled.
1540 */
1541 error = vfs_copyopt(opts, "sysvsem", &jsys, sizeof(jsys));
1542 if (error != ENOENT) {
1543 if (error != 0)
1544 return (error);
1545 switch (jsys) {
1546 case JAIL_SYS_DISABLE:
1547 break;
1548 case JAIL_SYS_NEW:
1549 case JAIL_SYS_INHERIT:
1550 prison_lock(pr->pr_parent);
1551 prpr = osd_jail_get(pr->pr_parent, sem_prison_slot);
1552 prison_unlock(pr->pr_parent);
1553 if (prpr == NULL)
1554 return (EPERM);
1555 break;
1556 default:
1557 return (EINVAL);
1558 }
1559 }
1560
1561 return (0);
1562 }
1563
1564 static int
1565 sem_prison_set(void *obj, void *data)
1566 {
1567 struct prison *pr = obj;
1568 struct prison *tpr, *orpr, *nrpr, *trpr;
1569 struct vfsoptlist *opts = data;
1570 void *rsv;
1571 int jsys, descend;
1572
1573 /*
1574 * sysvsem controls which jail is the root of the associated sems (this
1575 * jail or same as the parent), or if the feature is available at all.
1576 */
1577 if (vfs_copyopt(opts, "sysvsem", &jsys, sizeof(jsys)) == ENOENT)
1578 jsys = vfs_flagopt(opts, "allow.sysvipc", NULL, 0)
1579 ? JAIL_SYS_INHERIT
1580 : vfs_flagopt(opts, "allow.nosysvipc", NULL, 0)
1581 ? JAIL_SYS_DISABLE
1582 : -1;
1583 if (jsys == JAIL_SYS_DISABLE) {
1584 prison_lock(pr);
1585 orpr = osd_jail_get(pr, sem_prison_slot);
1586 if (orpr != NULL)
1587 osd_jail_del(pr, sem_prison_slot);
1588 prison_unlock(pr);
1589 if (orpr != NULL) {
1590 if (orpr == pr)
1591 sem_prison_cleanup(pr);
1592 /* Disable all child jails as well. */
1593 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1594 prison_lock(tpr);
1595 trpr = osd_jail_get(tpr, sem_prison_slot);
1596 if (trpr != NULL) {
1597 osd_jail_del(tpr, sem_prison_slot);
1598 prison_unlock(tpr);
1599 if (trpr == tpr)
1600 sem_prison_cleanup(tpr);
1601 } else {
1602 prison_unlock(tpr);
1603 descend = 0;
1604 }
1605 }
1606 }
1607 } else if (jsys != -1) {
1608 if (jsys == JAIL_SYS_NEW)
1609 nrpr = pr;
1610 else {
1611 prison_lock(pr->pr_parent);
1612 nrpr = osd_jail_get(pr->pr_parent, sem_prison_slot);
1613 prison_unlock(pr->pr_parent);
1614 }
1615 rsv = osd_reserve(sem_prison_slot);
1616 prison_lock(pr);
1617 orpr = osd_jail_get(pr, sem_prison_slot);
1618 if (orpr != nrpr)
1619 (void)osd_jail_set_reserved(pr, sem_prison_slot, rsv,
1620 nrpr);
1621 else
1622 osd_free_reserved(rsv);
1623 prison_unlock(pr);
1624 if (orpr != nrpr) {
1625 if (orpr == pr)
1626 sem_prison_cleanup(pr);
1627 if (orpr != NULL) {
1628 /* Change child jails matching the old root, */
1629 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1630 prison_lock(tpr);
1631 trpr = osd_jail_get(tpr,
1632 sem_prison_slot);
1633 if (trpr == orpr) {
1634 (void)osd_jail_set(tpr,
1635 sem_prison_slot, nrpr);
1636 prison_unlock(tpr);
1637 if (trpr == tpr)
1638 sem_prison_cleanup(tpr);
1639 } else {
1640 prison_unlock(tpr);
1641 descend = 0;
1642 }
1643 }
1644 }
1645 }
1646 }
1647
1648 return (0);
1649 }
1650
1651 static int
1652 sem_prison_get(void *obj, void *data)
1653 {
1654 struct prison *pr = obj;
1655 struct prison *rpr;
1656 struct vfsoptlist *opts = data;
1657 int error, jsys;
1658
1659 /* Set sysvsem based on the jail's root prison. */
1660 prison_lock(pr);
1661 rpr = osd_jail_get(pr, sem_prison_slot);
1662 prison_unlock(pr);
1663 jsys = rpr == NULL ? JAIL_SYS_DISABLE
1664 : rpr == pr ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
1665 error = vfs_setopt(opts, "sysvsem", &jsys, sizeof(jsys));
1666 if (error == ENOENT)
1667 error = 0;
1668 return (error);
1669 }
1670
1671 static int
1672 sem_prison_remove(void *obj, void *data __unused)
1673 {
1674 struct prison *pr = obj;
1675 struct prison *rpr;
1676
1677 prison_lock(pr);
1678 rpr = osd_jail_get(pr, sem_prison_slot);
1679 prison_unlock(pr);
1680 if (rpr == pr)
1681 sem_prison_cleanup(pr);
1682 return (0);
1683 }
1684
1685 static void
1686 sem_prison_cleanup(struct prison *pr)
1687 {
1688 int i;
1689
1690 /* Remove any sems that belong to this jail. */
1691 mtx_lock(&sem_mtx);
1692 for (i = 0; i < seminfo.semmni; i++) {
1693 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
1694 sema[i].cred != NULL && sema[i].cred->cr_prison == pr) {
1695 mtx_lock(&sema_mtx[i]);
1696 sem_remove(i, NULL);
1697 mtx_unlock(&sema_mtx[i]);
1698 }
1699 }
1700 mtx_unlock(&sem_mtx);
1701 }
1702
1703 SYSCTL_JAIL_PARAM_SYS_NODE(sysvsem, CTLFLAG_RW, "SYSV semaphores");
1704
1705 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1706 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1707
1708 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1709 static sy_call_t *semcalls[] = {
1710 (sy_call_t *)freebsd7___semctl, (sy_call_t *)sys_semget,
1711 (sy_call_t *)sys_semop
1712 };
1713
1714 /*
1715 * Entry point for all SEM calls.
1716 */
1717 int
1718 sys_semsys(td, uap)
1719 struct thread *td;
1720 /* XXX actually varargs. */
1721 struct semsys_args /* {
1722 int which;
1723 int a2;
1724 int a3;
1725 int a4;
1726 int a5;
1727 } */ *uap;
1728 {
1729 int error;
1730
1731 if (uap->which < 0 || uap->which >= nitems(semcalls))
1732 return (EINVAL);
1733 error = (*semcalls[uap->which])(td, &uap->a2);
1734 return (error);
1735 }
1736
1737 #ifndef _SYS_SYSPROTO_H_
1738 struct freebsd7___semctl_args {
1739 int semid;
1740 int semnum;
1741 int cmd;
1742 union semun_old *arg;
1743 };
1744 #endif
1745 int
1746 freebsd7___semctl(struct thread *td, struct freebsd7___semctl_args *uap)
1747 {
1748 struct semid_ds_old dsold;
1749 struct semid_ds dsbuf;
1750 union semun_old arg;
1751 union semun semun;
1752 register_t rval;
1753 int error;
1754
1755 switch (uap->cmd) {
1756 case SEM_STAT:
1757 case IPC_SET:
1758 case IPC_STAT:
1759 case GETALL:
1760 case SETVAL:
1761 case SETALL:
1762 error = copyin(uap->arg, &arg, sizeof(arg));
1763 if (error)
1764 return (error);
1765 break;
1766 }
1767
1768 switch (uap->cmd) {
1769 case SEM_STAT:
1770 case IPC_STAT:
1771 semun.buf = &dsbuf;
1772 break;
1773 case IPC_SET:
1774 error = copyin(arg.buf, &dsold, sizeof(dsold));
1775 if (error)
1776 return (error);
1777 ipcperm_old2new(&dsold.sem_perm, &dsbuf.sem_perm);
1778 CP(dsold, dsbuf, sem_base);
1779 CP(dsold, dsbuf, sem_nsems);
1780 CP(dsold, dsbuf, sem_otime);
1781 CP(dsold, dsbuf, sem_ctime);
1782 semun.buf = &dsbuf;
1783 break;
1784 case GETALL:
1785 case SETALL:
1786 semun.array = arg.array;
1787 break;
1788 case SETVAL:
1789 semun.val = arg.val;
1790 break;
1791 }
1792
1793 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1794 &rval);
1795 if (error)
1796 return (error);
1797
1798 switch (uap->cmd) {
1799 case SEM_STAT:
1800 case IPC_STAT:
1801 bzero(&dsold, sizeof(dsold));
1802 ipcperm_new2old(&dsbuf.sem_perm, &dsold.sem_perm);
1803 CP(dsbuf, dsold, sem_base);
1804 CP(dsbuf, dsold, sem_nsems);
1805 CP(dsbuf, dsold, sem_otime);
1806 CP(dsbuf, dsold, sem_ctime);
1807 error = copyout(&dsold, arg.buf, sizeof(dsold));
1808 break;
1809 }
1810
1811 if (error == 0)
1812 td->td_retval[0] = rval;
1813 return (error);
1814 }
1815
1816 #endif /* COMPAT_FREEBSD{4,5,6,7} */
1817
1818 #ifdef COMPAT_FREEBSD32
1819
1820 int
1821 freebsd32_semsys(struct thread *td, struct freebsd32_semsys_args *uap)
1822 {
1823
1824 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1825 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1826 switch (uap->which) {
1827 case 0:
1828 return (freebsd7_freebsd32_semctl(td,
1829 (struct freebsd7_freebsd32_semctl_args *)&uap->a2));
1830 default:
1831 return (sys_semsys(td, (struct semsys_args *)uap));
1832 }
1833 #else
1834 return (nosys(td, NULL));
1835 #endif
1836 }
1837
1838 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1839 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1840 int
1841 freebsd7_freebsd32_semctl(struct thread *td,
1842 struct freebsd7_freebsd32_semctl_args *uap)
1843 {
1844 struct semid_ds32_old dsbuf32;
1845 struct semid_ds dsbuf;
1846 union semun semun;
1847 union semun32 arg;
1848 register_t rval;
1849 int error;
1850
1851 switch (uap->cmd) {
1852 case SEM_STAT:
1853 case IPC_SET:
1854 case IPC_STAT:
1855 case GETALL:
1856 case SETVAL:
1857 case SETALL:
1858 error = copyin(uap->arg, &arg, sizeof(arg));
1859 if (error)
1860 return (error);
1861 break;
1862 }
1863
1864 switch (uap->cmd) {
1865 case SEM_STAT:
1866 case IPC_STAT:
1867 semun.buf = &dsbuf;
1868 break;
1869 case IPC_SET:
1870 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32));
1871 if (error)
1872 return (error);
1873 freebsd32_ipcperm_old_in(&dsbuf32.sem_perm, &dsbuf.sem_perm);
1874 PTRIN_CP(dsbuf32, dsbuf, sem_base);
1875 CP(dsbuf32, dsbuf, sem_nsems);
1876 CP(dsbuf32, dsbuf, sem_otime);
1877 CP(dsbuf32, dsbuf, sem_ctime);
1878 semun.buf = &dsbuf;
1879 break;
1880 case GETALL:
1881 case SETALL:
1882 semun.array = PTRIN(arg.array);
1883 break;
1884 case SETVAL:
1885 semun.val = arg.val;
1886 break;
1887 }
1888
1889 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1890 &rval);
1891 if (error)
1892 return (error);
1893
1894 switch (uap->cmd) {
1895 case SEM_STAT:
1896 case IPC_STAT:
1897 bzero(&dsbuf32, sizeof(dsbuf32));
1898 freebsd32_ipcperm_old_out(&dsbuf.sem_perm, &dsbuf32.sem_perm);
1899 PTROUT_CP(dsbuf, dsbuf32, sem_base);
1900 CP(dsbuf, dsbuf32, sem_nsems);
1901 CP(dsbuf, dsbuf32, sem_otime);
1902 CP(dsbuf, dsbuf32, sem_ctime);
1903 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32));
1904 break;
1905 }
1906
1907 if (error == 0)
1908 td->td_retval[0] = rval;
1909 return (error);
1910 }
1911 #endif
1912
1913 int
1914 freebsd32_semctl(struct thread *td, struct freebsd32_semctl_args *uap)
1915 {
1916 struct semid_ds32 dsbuf32;
1917 struct semid_ds dsbuf;
1918 union semun semun;
1919 union semun32 arg;
1920 register_t rval;
1921 int error;
1922
1923 switch (uap->cmd) {
1924 case SEM_STAT:
1925 case IPC_SET:
1926 case IPC_STAT:
1927 case GETALL:
1928 case SETVAL:
1929 case SETALL:
1930 error = copyin(uap->arg, &arg, sizeof(arg));
1931 if (error)
1932 return (error);
1933 break;
1934 }
1935
1936 switch (uap->cmd) {
1937 case SEM_STAT:
1938 case IPC_STAT:
1939 semun.buf = &dsbuf;
1940 break;
1941 case IPC_SET:
1942 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32));
1943 if (error)
1944 return (error);
1945 freebsd32_ipcperm_in(&dsbuf32.sem_perm, &dsbuf.sem_perm);
1946 PTRIN_CP(dsbuf32, dsbuf, sem_base);
1947 CP(dsbuf32, dsbuf, sem_nsems);
1948 CP(dsbuf32, dsbuf, sem_otime);
1949 CP(dsbuf32, dsbuf, sem_ctime);
1950 semun.buf = &dsbuf;
1951 break;
1952 case GETALL:
1953 case SETALL:
1954 semun.array = PTRIN(arg.array);
1955 break;
1956 case SETVAL:
1957 semun.val = arg.val;
1958 break;
1959 }
1960
1961 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1962 &rval);
1963 if (error)
1964 return (error);
1965
1966 switch (uap->cmd) {
1967 case SEM_STAT:
1968 case IPC_STAT:
1969 bzero(&dsbuf32, sizeof(dsbuf32));
1970 freebsd32_ipcperm_out(&dsbuf.sem_perm, &dsbuf32.sem_perm);
1971 PTROUT_CP(dsbuf, dsbuf32, sem_base);
1972 CP(dsbuf, dsbuf32, sem_nsems);
1973 CP(dsbuf, dsbuf32, sem_otime);
1974 CP(dsbuf, dsbuf32, sem_ctime);
1975 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32));
1976 break;
1977 }
1978
1979 if (error == 0)
1980 td->td_retval[0] = rval;
1981 return (error);
1982 }
1983
1984 #endif /* COMPAT_FREEBSD32 */
Cache object: 174b34a7d493f9b282799d8639b49759
|