]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/sysv_sem.c
199cdd1618bb59f0ec7e03e97752448afa00d596
[apple/xnu.git] / bsd / kern / sysv_sem.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * Implementation of SVID semaphores
24 *
25 * Author: Daniel Boulet
26 *
27 * This software is provided ``AS IS'' without any warranties of any kind.
28 */
29 /*
30 * John Bellardo modified the implementation for Darwin. 12/2000
31 */
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/proc_internal.h>
37 #include <sys/kauth.h>
38 #include <sys/sem_internal.h>
39 #include <sys/malloc.h>
40 #include <mach/mach_types.h>
41
42 #include <sys/filedesc.h>
43 #include <sys/file_internal.h>
44 #include <sys/sysctl.h>
45 #include <sys/ipcs.h>
46 #include <sys/sysent.h>
47 #include <sys/sysproto.h>
48
49 #include <bsm/audit_kernel.h>
50
51
52 /* Uncomment this line to see the debugging output */
53 /* #define SEM_DEBUG */
54
55 #define M_SYSVSEM M_TEMP
56
57
58 /* Hard system limits to avoid resource starvation / DOS attacks.
59 * These are not needed if we can make the semaphore pages swappable.
60 */
61 static struct seminfo limitseminfo = {
62 SEMMAP, /* # of entries in semaphore map */
63 SEMMNI, /* # of semaphore identifiers */
64 SEMMNS, /* # of semaphores in system */
65 SEMMNU, /* # of undo structures in system */
66 SEMMSL, /* max # of semaphores per id */
67 SEMOPM, /* max # of operations per semop call */
68 SEMUME, /* max # of undo entries per process */
69 SEMUSZ, /* size in bytes of undo structure */
70 SEMVMX, /* semaphore maximum value */
71 SEMAEM /* adjust on exit max value */
72 };
73
74 /* Current system allocations. We use this structure to track how many
75 * resources we have allocated so far. This way we can set large hard limits
76 * and not allocate the memory for them up front.
77 */
78 struct seminfo seminfo = {
79 SEMMAP, /* Unused, # of entries in semaphore map */
80 0, /* # of semaphore identifiers */
81 0, /* # of semaphores in system */
82 0, /* # of undo entries in system */
83 SEMMSL, /* max # of semaphores per id */
84 SEMOPM, /* max # of operations per semop call */
85 SEMUME, /* max # of undo entries per process */
86 SEMUSZ, /* size in bytes of undo structure */
87 SEMVMX, /* semaphore maximum value */
88 SEMAEM /* adjust on exit max value */
89 };
90
91
92 static struct sem_undo *semu_alloc(struct proc *p);
93 static int semundo_adjust(struct proc *p, struct sem_undo **supptr,
94 int semid, int semnum, int adjval);
95 static void semundo_clear(int semid, int semnum);
96
97 /* XXX casting to (sy_call_t *) is bogus, as usual. */
98 static sy_call_t *semcalls[] = {
99 (sy_call_t *)semctl, (sy_call_t *)semget,
100 (sy_call_t *)semop
101 };
102
103 static int semtot = 0; /* # of used semaphores */
104 struct user_semid_ds *sema = NULL; /* semaphore id pool */
105 struct sem *sem_pool = NULL; /* semaphore pool */
106 static struct sem_undo *semu_list = NULL; /* active undo structures */
107 struct sem_undo *semu = NULL; /* semaphore undo pool */
108
109
110 void sysv_sem_lock_init(void);
111 static lck_grp_t *sysv_sem_subsys_lck_grp;
112 static lck_grp_attr_t *sysv_sem_subsys_lck_grp_attr;
113 static lck_attr_t *sysv_sem_subsys_lck_attr;
114 static lck_mtx_t sysv_sem_subsys_mutex;
115
116 #define SYSV_SEM_SUBSYS_LOCK() lck_mtx_lock(&sysv_sem_subsys_mutex)
117 #define SYSV_SEM_SUBSYS_UNLOCK() lck_mtx_unlock(&sysv_sem_subsys_mutex)
118
119
120 __private_extern__ void
121 sysv_sem_lock_init( void )
122 {
123
124 sysv_sem_subsys_lck_grp_attr = lck_grp_attr_alloc_init();
125
126 sysv_sem_subsys_lck_grp = lck_grp_alloc_init("sysv_sem_subsys_lock", sysv_sem_subsys_lck_grp_attr);
127
128 sysv_sem_subsys_lck_attr = lck_attr_alloc_init();
129 lck_mtx_init(&sysv_sem_subsys_mutex, sysv_sem_subsys_lck_grp, sysv_sem_subsys_lck_attr);
130 }
131
132 static __inline__ user_time_t
133 sysv_semtime(void)
134 {
135 struct timeval tv;
136 microtime(&tv);
137 return (tv.tv_sec);
138 }
139
140 /*
141 * XXX conversion of internal user_time_t to external tume_t loses
142 * XXX precision; not an issue for us now, since we are only ever
143 * XXX setting 32 bits worth of time into it.
144 *
145 * pad field contents are not moved correspondingly; contents will be lost
146 *
147 * NOTE: Source and target may *NOT* overlap! (target is smaller)
148 */
149 static void
150 semid_ds_64to32(struct user_semid_ds *in, struct semid_ds *out)
151 {
152 out->sem_perm = in->sem_perm;
153 out->sem_base = (__int32_t)in->sem_base;
154 out->sem_nsems = in->sem_nsems;
155 out->sem_otime = in->sem_otime; /* XXX loses precision */
156 out->sem_ctime = in->sem_ctime; /* XXX loses precision */
157 }
158
159 /*
160 * pad field contents are not moved correspondingly; contents will be lost
161 *
162 * NOTE: Source and target may are permitted to overlap! (source is smaller);
163 * this works because we copy fields in order from the end of the struct to
164 * the beginning.
165 *
166 * XXX use CAST_USER_ADDR_T() for lack of a CAST_USER_TIME_T(); net effect
167 * XXX is the same.
168 */
169 static void
170 semid_ds_32to64(struct semid_ds *in, struct user_semid_ds *out)
171 {
172 out->sem_ctime = in->sem_ctime;
173 out->sem_otime = in->sem_otime;
174 out->sem_nsems = in->sem_nsems;
175 out->sem_base = (void *)in->sem_base;
176 out->sem_perm = in->sem_perm;
177 }
178
179
180 /*
181 * Entry point for all SEM calls
182 *
183 * In Darwin this is no longer the entry point. It will be removed after
184 * the code has been tested better.
185 */
186 /* XXX actually varargs. */
187 int
188 semsys(struct proc *p, struct semsys_args *uap, register_t *retval)
189 {
190
191 /* The individual calls handling the locking now */
192
193 if (uap->which >= sizeof(semcalls)/sizeof(semcalls[0]))
194 return (EINVAL);
195 return ((*semcalls[uap->which])(p, &uap->a2, retval));
196 }
197
198 /*
199 * Expand the semu array to the given capacity. If the expansion fails
200 * return 0, otherwise return 1.
201 *
202 * Assumes we already have the subsystem lock.
203 */
204 static int
205 grow_semu_array(int newSize)
206 {
207 register int i;
208 register struct sem_undo *newSemu;
209
210 if (newSize <= seminfo.semmnu)
211 return 1;
212 if (newSize > limitseminfo.semmnu) /* enforce hard limit */
213 {
214 #ifdef SEM_DEBUG
215 printf("undo structure hard limit of %d reached, requested %d\n",
216 limitseminfo.semmnu, newSize);
217 #endif
218 return 0;
219 }
220 newSize = (newSize/SEMMNU_INC + 1) * SEMMNU_INC;
221 newSize = newSize > limitseminfo.semmnu ? limitseminfo.semmnu : newSize;
222
223 #ifdef SEM_DEBUG
224 printf("growing semu[] from %d to %d\n", seminfo.semmnu, newSize);
225 #endif
226 MALLOC(newSemu, struct sem_undo *, sizeof (struct sem_undo) * newSize,
227 M_SYSVSEM, M_WAITOK | M_ZERO);
228 if (NULL == newSemu)
229 {
230 #ifdef SEM_DEBUG
231 printf("allocation failed. no changes made.\n");
232 #endif
233 return 0;
234 }
235
236 /* copy the old data to the new array */
237 for (i = 0; i < seminfo.semmnu; i++)
238 {
239 newSemu[i] = semu[i];
240 }
241 /*
242 * The new elements (from newSemu[i] to newSemu[newSize-1]) have their
243 * "un_proc" set to 0 (i.e. NULL) by the M_ZERO flag to MALLOC() above,
244 * so they're already marked as "not in use".
245 */
246
247 /* Clean up the old array */
248 if (semu)
249 FREE(semu, M_SYSVSEM);
250
251 semu = newSemu;
252 seminfo.semmnu = newSize;
253 #ifdef SEM_DEBUG
254 printf("expansion successful\n");
255 #endif
256 return 1;
257 }
258
259 /*
260 * Expand the sema array to the given capacity. If the expansion fails
261 * we return 0, otherwise we return 1.
262 *
263 * Assumes we already have the subsystem lock.
264 */
265 static int
266 grow_sema_array(int newSize)
267 {
268 register struct user_semid_ds *newSema;
269 register int i;
270
271 if (newSize <= seminfo.semmni)
272 return 0;
273 if (newSize > limitseminfo.semmni) /* enforce hard limit */
274 {
275 #ifdef SEM_DEBUG
276 printf("identifier hard limit of %d reached, requested %d\n",
277 limitseminfo.semmni, newSize);
278 #endif
279 return 0;
280 }
281 newSize = (newSize/SEMMNI_INC + 1) * SEMMNI_INC;
282 newSize = newSize > limitseminfo.semmni ? limitseminfo.semmni : newSize;
283
284 #ifdef SEM_DEBUG
285 printf("growing sema[] from %d to %d\n", seminfo.semmni, newSize);
286 #endif
287 MALLOC(newSema, struct user_semid_ds *,
288 sizeof (struct user_semid_ds) * newSize,
289 M_SYSVSEM, M_WAITOK | M_ZERO);
290 if (NULL == newSema)
291 {
292 #ifdef SEM_DEBUG
293 printf("allocation failed. no changes made.\n");
294 #endif
295 return 0;
296 }
297
298 /* copy over the old ids */
299 for (i = 0; i < seminfo.semmni; i++)
300 {
301 newSema[i] = sema[i];
302 /* This is a hack. What we really want to be able to
303 * do is change the value a process is waiting on
304 * without waking it up, but I don't know how to do
305 * this with the existing code, so we wake up the
306 * process and let it do a lot of work to determine the
307 * semaphore set is really not available yet, and then
308 * sleep on the correct, reallocated user_semid_ds pointer.
309 */
310 if (sema[i].sem_perm.mode & SEM_ALLOC)
311 wakeup((caddr_t)&sema[i]);
312 }
313 /*
314 * The new elements (from newSema[i] to newSema[newSize-1]) have their
315 * "sem_base" and "sem_perm.mode" set to 0 (i.e. NULL) by the M_ZERO
316 * flag to MALLOC() above, so they're already marked as "not in use".
317 */
318
319 /* Clean up the old array */
320 if (sema)
321 FREE(sema, M_SYSVSEM);
322
323 sema = newSema;
324 seminfo.semmni = newSize;
325 #ifdef SEM_DEBUG
326 printf("expansion successful\n");
327 #endif
328 return 1;
329 }
330
331 /*
332 * Expand the sem_pool array to the given capacity. If the expansion fails
333 * we return 0 (fail), otherwise we return 1 (success).
334 *
335 * Assumes we already hold the subsystem lock.
336 */
337 static int
338 grow_sem_pool(int new_pool_size)
339 {
340 struct sem *new_sem_pool = NULL;
341 struct sem *sem_free;
342 int i;
343
344 if (new_pool_size < semtot)
345 return 0;
346 /* enforce hard limit */
347 if (new_pool_size > limitseminfo.semmns) {
348 #ifdef SEM_DEBUG
349 printf("semaphore hard limit of %d reached, requested %d\n",
350 limitseminfo.semmns, new_pool_size);
351 #endif
352 return 0;
353 }
354
355 new_pool_size = (new_pool_size/SEMMNS_INC + 1) * SEMMNS_INC;
356 new_pool_size = new_pool_size > limitseminfo.semmns ? limitseminfo.semmns : new_pool_size;
357
358 #ifdef SEM_DEBUG
359 printf("growing sem_pool array from %d to %d\n", seminfo.semmns, new_pool_size);
360 #endif
361 MALLOC(new_sem_pool, struct sem *, sizeof (struct sem) * new_pool_size,
362 M_SYSVSEM, M_WAITOK | M_ZERO);
363 if (NULL == new_sem_pool) {
364 #ifdef SEM_DEBUG
365 printf("allocation failed. no changes made.\n");
366 #endif
367 return 0;
368 }
369
370 /* We have our new memory, now copy the old contents over */
371 if (sem_pool)
372 for(i = 0; i < seminfo.semmns; i++)
373 new_sem_pool[i] = sem_pool[i];
374
375 /* Update our id structures to point to the new semaphores */
376 for(i = 0; i < seminfo.semmni; i++) {
377 if (sema[i].sem_perm.mode & SEM_ALLOC) /* ID in use */
378 sema[i].sem_base += (new_sem_pool - sem_pool);
379 }
380
381 sem_free = sem_pool;
382 sem_pool = new_sem_pool;
383
384 /* clean up the old array */
385 if (sem_free != NULL)
386 FREE(sem_free, M_SYSVSEM);
387
388 seminfo.semmns = new_pool_size;
389 #ifdef SEM_DEBUG
390 printf("expansion complete\n");
391 #endif
392 return 1;
393 }
394
395 /*
396 * Allocate a new sem_undo structure for a process
397 * (returns ptr to structure or NULL if no more room)
398 *
399 * Assumes we already hold the subsystem lock.
400 */
401
402 static struct sem_undo *
403 semu_alloc(struct proc *p)
404 {
405 register int i;
406 register struct sem_undo *suptr;
407 register struct sem_undo **supptr;
408 int attempt;
409
410 /*
411 * Try twice to allocate something.
412 * (we'll purge any empty structures after the first pass so
413 * two passes are always enough)
414 */
415
416 for (attempt = 0; attempt < 2; attempt++) {
417 /*
418 * Look for a free structure.
419 * Fill it in and return it if we find one.
420 */
421
422 for (i = 0; i < seminfo.semmnu; i++) {
423 suptr = SEMU(i);
424 if (suptr->un_proc == NULL) {
425 suptr->un_next = semu_list;
426 semu_list = suptr;
427 suptr->un_cnt = 0;
428 suptr->un_ent = NULL;
429 suptr->un_proc = p;
430 return(suptr);
431 }
432 }
433
434 /*
435 * We didn't find a free one, if this is the first attempt
436 * then try to free some structures.
437 */
438
439 if (attempt == 0) {
440 /* All the structures are in use - try to free some */
441 int did_something = 0;
442
443 supptr = &semu_list;
444 while ((suptr = *supptr) != NULL) {
445 if (suptr->un_cnt == 0) {
446 suptr->un_proc = NULL;
447 *supptr = suptr->un_next;
448 did_something = 1;
449 } else
450 supptr = &(suptr->un_next);
451 }
452
453 /* If we didn't free anything. Try expanding
454 * the semu[] array. If that doesn't work
455 * then fail. We expand last to get the
456 * most reuse out of existing resources.
457 */
458 if (!did_something)
459 if (!grow_semu_array(seminfo.semmnu + 1))
460 return(NULL);
461 } else {
462 /*
463 * The second pass failed even though we freed
464 * something after the first pass!
465 * This is IMPOSSIBLE!
466 */
467 panic("semu_alloc - second attempt failed");
468 }
469 }
470 return (NULL);
471 }
472
473 /*
474 * Adjust a particular entry for a particular proc
475 *
476 * Assumes we already hold the subsystem lock.
477 */
478 static int
479 semundo_adjust(struct proc *p, struct sem_undo **supptr, int semid,
480 int semnum, int adjval)
481 {
482 register struct sem_undo *suptr;
483 register struct undo *sueptr, **suepptr, *new_sueptr;
484 int i;
485
486 /*
487 * Look for and remember the sem_undo if the caller doesn't provide it
488 */
489
490 suptr = *supptr;
491 if (suptr == NULL) {
492 for (suptr = semu_list; suptr != NULL;
493 suptr = suptr->un_next) {
494 if (suptr->un_proc == p) {
495 *supptr = suptr;
496 break;
497 }
498 }
499 if (suptr == NULL) {
500 if (adjval == 0)
501 return(0);
502 suptr = semu_alloc(p);
503 if (suptr == NULL)
504 return(ENOSPC);
505 *supptr = suptr;
506 }
507 }
508
509 /*
510 * Look for the requested entry and adjust it (delete if adjval becomes
511 * 0).
512 */
513 new_sueptr = NULL;
514 for (i = 0, suepptr = &suptr->un_ent, sueptr = suptr->un_ent;
515 i < suptr->un_cnt;
516 i++, suepptr = &sueptr->une_next, sueptr = sueptr->une_next) {
517 if (sueptr->une_id != semid || sueptr->une_num != semnum)
518 continue;
519 if (adjval == 0)
520 sueptr->une_adjval = 0;
521 else
522 sueptr->une_adjval += adjval;
523 if (sueptr->une_adjval == 0) {
524 suptr->un_cnt--;
525 *suepptr = sueptr->une_next;
526 FREE(sueptr, M_SYSVSEM);
527 sueptr = NULL;
528 }
529 return 0;
530 }
531
532 /* Didn't find the right entry - create it */
533 if (adjval == 0) {
534 /* no adjustment: no need for a new entry */
535 return 0;
536 }
537
538 if (suptr->un_cnt == limitseminfo.semume) {
539 /* reached the limit number of semaphore undo entries */
540 return EINVAL;
541 }
542
543 /* allocate a new semaphore undo entry */
544 MALLOC(new_sueptr, struct undo *, sizeof (struct undo),
545 M_SYSVSEM, M_WAITOK);
546 if (new_sueptr == NULL) {
547 return ENOMEM;
548 }
549
550 /* fill in the new semaphore undo entry */
551 new_sueptr->une_next = suptr->un_ent;
552 suptr->un_ent = new_sueptr;
553 suptr->un_cnt++;
554 new_sueptr->une_adjval = adjval;
555 new_sueptr->une_id = semid;
556 new_sueptr->une_num = semnum;
557
558 return 0;
559 }
560
561 /* Assumes we already hold the subsystem lock.
562 */
563 static void
564 semundo_clear(int semid, int semnum)
565 {
566 struct sem_undo *suptr;
567
568 for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next) {
569 struct undo *sueptr;
570 struct undo **suepptr;
571 int i = 0;
572
573 sueptr = suptr->un_ent;
574 suepptr = &suptr->un_ent;
575 while (i < suptr->un_cnt) {
576 if (sueptr->une_id == semid) {
577 if (semnum == -1 || sueptr->une_num == semnum) {
578 suptr->un_cnt--;
579 *suepptr = sueptr->une_next;
580 FREE(sueptr, M_SYSVSEM);
581 sueptr = *suepptr;
582 continue;
583 }
584 if (semnum != -1)
585 break;
586 }
587 i++;
588 suepptr = &sueptr->une_next;
589 sueptr = sueptr->une_next;
590 }
591 }
592 }
593
594 /*
595 * Note that the user-mode half of this passes a union coerced to a
596 * user_addr_t. The union contains either an int or a pointer, and
597 * so we have to coerce it back, variant on whether the calling
598 * process is 64 bit or not. The coercion works for the 'val' element
599 * because the alignment is the same in user and kernel space.
600 */
601 int
602 semctl(struct proc *p, struct semctl_args *uap, register_t *retval)
603 {
604 int semid = uap->semid;
605 int semnum = uap->semnum;
606 int cmd = uap->cmd;
607 user_semun_t user_arg = (user_semun_t)uap->arg;
608 kauth_cred_t cred = kauth_cred_get();
609 int i, rval, eval;
610 struct user_semid_ds sbuf;
611 struct user_semid_ds *semaptr;
612 struct user_semid_ds uds;
613
614
615 AUDIT_ARG(svipc_cmd, cmd);
616 AUDIT_ARG(svipc_id, semid);
617
618 SYSV_SEM_SUBSYS_LOCK();
619
620 #ifdef SEM_DEBUG
621 printf("call to semctl(%d, %d, %d, 0x%qx)\n", semid, semnum, cmd, user_arg);
622 #endif
623
624 semid = IPCID_TO_IX(semid);
625
626 if (semid < 0 || semid >= seminfo.semmni) {
627 #ifdef SEM_DEBUG
628 printf("Invalid semid\n");
629 #endif
630 eval = EINVAL;
631 goto semctlout;
632 }
633
634 semaptr = &sema[semid];
635 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
636 semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
637 eval = EINVAL;
638 goto semctlout;
639 }
640
641 eval = 0;
642 rval = 0;
643
644 switch (cmd) {
645 case IPC_RMID:
646 if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_M)))
647 goto semctlout;
648
649 semaptr->sem_perm.cuid = kauth_cred_getuid(cred);
650 semaptr->sem_perm.uid = kauth_cred_getuid(cred);
651 semtot -= semaptr->sem_nsems;
652 for (i = semaptr->sem_base - sem_pool; i < semtot; i++)
653 sem_pool[i] = sem_pool[i + semaptr->sem_nsems];
654 for (i = 0; i < seminfo.semmni; i++) {
655 if ((sema[i].sem_perm.mode & SEM_ALLOC) &&
656 sema[i].sem_base > semaptr->sem_base)
657 sema[i].sem_base -= semaptr->sem_nsems;
658 }
659 semaptr->sem_perm.mode = 0;
660 semundo_clear(semid, -1);
661 wakeup((caddr_t)semaptr);
662 break;
663
664 case IPC_SET:
665 if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_M)))
666 goto semctlout;
667
668 if (IS_64BIT_PROCESS(p)) {
669 eval = copyin(user_arg.buf, &sbuf, sizeof(struct user_semid_ds));
670 } else {
671 eval = copyin(user_arg.buf, &sbuf, sizeof(struct semid_ds));
672 /* convert in place; ugly, but safe */
673 semid_ds_32to64((struct semid_ds *)&sbuf, &sbuf);
674 }
675
676 if (eval != 0) {
677 goto semctlout;
678 }
679
680 semaptr->sem_perm.uid = sbuf.sem_perm.uid;
681 semaptr->sem_perm.gid = sbuf.sem_perm.gid;
682 semaptr->sem_perm.mode = (semaptr->sem_perm.mode & ~0777) |
683 (sbuf.sem_perm.mode & 0777);
684 semaptr->sem_ctime = sysv_semtime();
685 break;
686
687 case IPC_STAT:
688 if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
689 goto semctlout;
690 bcopy(semaptr, &uds, sizeof(struct user_semid_ds));
691 if (IS_64BIT_PROCESS(p)) {
692 eval = copyout(&uds, user_arg.buf, sizeof(struct user_semid_ds));
693 } else {
694 struct semid_ds semid_ds32;
695 semid_ds_64to32(&uds, &semid_ds32);
696 eval = copyout(&semid_ds32, user_arg.buf, sizeof(struct semid_ds));
697 }
698 break;
699
700 case GETNCNT:
701 if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
702 goto semctlout;
703 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
704 eval = EINVAL;
705 goto semctlout;
706 }
707 rval = semaptr->sem_base[semnum].semncnt;
708 break;
709
710 case GETPID:
711 if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
712 goto semctlout;
713 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
714 eval = EINVAL;
715 goto semctlout;
716 }
717 rval = semaptr->sem_base[semnum].sempid;
718 break;
719
720 case GETVAL:
721 if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
722 goto semctlout;
723 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
724 eval = EINVAL;
725 goto semctlout;
726 }
727 rval = semaptr->sem_base[semnum].semval;
728 break;
729
730 case GETALL:
731 if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
732 goto semctlout;
733 /* XXXXXXXXXXXXXXXX TBD XXXXXXXXXXXXXXXX */
734 for (i = 0; i < semaptr->sem_nsems; i++) {
735 /* XXX could be done in one go... */
736 eval = copyout((caddr_t)&semaptr->sem_base[i].semval,
737 user_arg.array + (i * sizeof(unsigned short)),
738 sizeof(unsigned short));
739 if (eval != 0)
740 break;
741 }
742 break;
743
744 case GETZCNT:
745 if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
746 goto semctlout;
747 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
748 eval = EINVAL;
749 goto semctlout;
750 }
751 rval = semaptr->sem_base[semnum].semzcnt;
752 break;
753
754 case SETVAL:
755 if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
756 {
757 #ifdef SEM_DEBUG
758 printf("Invalid credentials for write\n");
759 #endif
760 goto semctlout;
761 }
762 if (semnum < 0 || semnum >= semaptr->sem_nsems)
763 {
764 #ifdef SEM_DEBUG
765 printf("Invalid number out of range for set\n");
766 #endif
767 eval = EINVAL;
768 goto semctlout;
769 }
770 /*
771 * Cast down a pointer instead of using 'val' member directly
772 * to avoid introducing endieness and a pad field into the
773 * header file. Ugly, but it works.
774 */
775 semaptr->sem_base[semnum].semval = CAST_DOWN(int,user_arg.buf);
776 semundo_clear(semid, semnum);
777 wakeup((caddr_t)semaptr);
778 break;
779
780 case SETALL:
781 if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
782 goto semctlout;
783 /*** XXXXXXXXXXXX TBD ********/
784 for (i = 0; i < semaptr->sem_nsems; i++) {
785 /* XXX could be done in one go... */
786 eval = copyin(user_arg.array + (i * sizeof(unsigned short)),
787 (caddr_t)&semaptr->sem_base[i].semval,
788 sizeof(unsigned short));
789 if (eval != 0)
790 break;
791 }
792 semundo_clear(semid, -1);
793 wakeup((caddr_t)semaptr);
794 break;
795
796 default:
797 eval = EINVAL;
798 goto semctlout;
799 }
800
801 if (eval == 0)
802 *retval = rval;
803 semctlout:
804 SYSV_SEM_SUBSYS_UNLOCK();
805 return(eval);
806 }
807
808 int
809 semget(__unused struct proc *p, struct semget_args *uap, register_t *retval)
810 {
811 int semid, eval;
812 int key = uap->key;
813 int nsems = uap->nsems;
814 int semflg = uap->semflg;
815 kauth_cred_t cred = kauth_cred_get();
816
817 #ifdef SEM_DEBUG
818 if (key != IPC_PRIVATE)
819 printf("semget(0x%x, %d, 0%o)\n", key, nsems, semflg);
820 else
821 printf("semget(IPC_PRIVATE, %d, 0%o)\n", nsems, semflg);
822 #endif
823
824
825 SYSV_SEM_SUBSYS_LOCK();
826
827
828 if (key != IPC_PRIVATE) {
829 for (semid = 0; semid < seminfo.semmni; semid++) {
830 if ((sema[semid].sem_perm.mode & SEM_ALLOC) &&
831 sema[semid].sem_perm.key == key)
832 break;
833 }
834 if (semid < seminfo.semmni) {
835 #ifdef SEM_DEBUG
836 printf("found public key\n");
837 #endif
838 if ((eval = ipcperm(cred, &sema[semid].sem_perm,
839 semflg & 0700)))
840 goto semgetout;
841 if (nsems < 0 || sema[semid].sem_nsems < nsems) {
842 #ifdef SEM_DEBUG
843 printf("too small\n");
844 #endif
845 eval = EINVAL;
846 goto semgetout;
847 }
848 if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
849 #ifdef SEM_DEBUG
850 printf("not exclusive\n");
851 #endif
852 eval = EEXIST;
853 goto semgetout;
854 }
855 goto found;
856 }
857 }
858
859 #ifdef SEM_DEBUG
860 printf("need to allocate an id for the request\n");
861 #endif
862 if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
863 if (nsems <= 0 || nsems > limitseminfo.semmsl) {
864 #ifdef SEM_DEBUG
865 printf("nsems out of range (0<%d<=%d)\n", nsems,
866 seminfo.semmsl);
867 #endif
868 eval = EINVAL;
869 goto semgetout;
870 }
871 if (nsems > seminfo.semmns - semtot) {
872 #ifdef SEM_DEBUG
873 printf("not enough semaphores left (need %d, got %d)\n",
874 nsems, seminfo.semmns - semtot);
875 #endif
876 if (!grow_sem_pool(semtot + nsems)) {
877 #ifdef SEM_DEBUG
878 printf("failed to grow the sem array\n");
879 #endif
880 eval = ENOSPC;
881 goto semgetout;
882 }
883 }
884 for (semid = 0; semid < seminfo.semmni; semid++) {
885 if ((sema[semid].sem_perm.mode & SEM_ALLOC) == 0)
886 break;
887 }
888 if (semid == seminfo.semmni) {
889 #ifdef SEM_DEBUG
890 printf("no more id's available\n");
891 #endif
892 if (!grow_sema_array(seminfo.semmni + 1))
893 {
894 #ifdef SEM_DEBUG
895 printf("failed to grow sema array\n");
896 #endif
897 eval = ENOSPC;
898 goto semgetout;
899 }
900 }
901 #ifdef SEM_DEBUG
902 printf("semid %d is available\n", semid);
903 #endif
904 sema[semid].sem_perm.key = key;
905 sema[semid].sem_perm.cuid = kauth_cred_getuid(cred);
906 sema[semid].sem_perm.uid = kauth_cred_getuid(cred);
907 sema[semid].sem_perm.cgid = cred->cr_gid;
908 sema[semid].sem_perm.gid = cred->cr_gid;
909 sema[semid].sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
910 sema[semid].sem_perm.seq =
911 (sema[semid].sem_perm.seq + 1) & 0x7fff;
912 sema[semid].sem_nsems = nsems;
913 sema[semid].sem_otime = 0;
914 sema[semid].sem_ctime = sysv_semtime();
915 sema[semid].sem_base = &sem_pool[semtot];
916 semtot += nsems;
917 bzero(sema[semid].sem_base,
918 sizeof(sema[semid].sem_base[0])*nsems);
919 #ifdef SEM_DEBUG
920 printf("sembase = 0x%x, next = 0x%x\n", sema[semid].sem_base,
921 &sem_pool[semtot]);
922 #endif
923 } else {
924 #ifdef SEM_DEBUG
925 printf("didn't find it and wasn't asked to create it\n");
926 #endif
927 eval = ENOENT;
928 goto semgetout;
929 }
930
931 found:
932 *retval = IXSEQ_TO_IPCID(semid, sema[semid].sem_perm);
933 AUDIT_ARG(svipc_id, *retval);
934 #ifdef SEM_DEBUG
935 printf("semget is done, returning %d\n", *retval);
936 #endif
937 eval = 0;
938
939 semgetout:
940 SYSV_SEM_SUBSYS_UNLOCK();
941 return(eval);
942 }
943
944 int
945 semop(struct proc *p, struct semop_args *uap, register_t *retval)
946 {
947 int semid = uap->semid;
948 int nsops = uap->nsops;
949 struct sembuf sops[MAX_SOPS];
950 register struct user_semid_ds *semaptr;
951 register struct sembuf *sopptr = NULL; /* protected by 'semptr' */
952 register struct sem *semptr = NULL; /* protected by 'if' */
953 struct sem_undo *suptr = NULL;
954 int i, j, eval;
955 int do_wakeup, do_undos;
956
957 AUDIT_ARG(svipc_id, uap->semid);
958
959 SYSV_SEM_SUBSYS_LOCK();
960
961 #ifdef SEM_DEBUG
962 printf("call to semop(%d, 0x%x, %d)\n", semid, sops, nsops);
963 #endif
964
965 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
966
967 if (semid < 0 || semid >= seminfo.semmni) {
968 eval = EINVAL;
969 goto semopout;
970 }
971
972 semaptr = &sema[semid];
973 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0) {
974 eval = EINVAL;
975 goto semopout;
976 }
977 if (semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
978 eval = EINVAL;
979 goto semopout;
980 }
981
982 if ((eval = ipcperm(kauth_cred_get(), &semaptr->sem_perm, IPC_W))) {
983 #ifdef SEM_DEBUG
984 printf("eval = %d from ipaccess\n", eval);
985 #endif
986 goto semopout;
987 }
988
989 if (nsops < 0 || nsops > MAX_SOPS) {
990 #ifdef SEM_DEBUG
991 printf("too many sops (max=%d, nsops=%d)\n", MAX_SOPS, nsops);
992 #endif
993 eval = E2BIG;
994 goto semopout;
995 }
996
997 /* OK for LP64, since sizeof(struct sembuf) is currently invariant */
998 if ((eval = copyin(uap->sops, &sops, nsops * sizeof(struct sembuf))) != 0) {
999 #ifdef SEM_DEBUG
1000 printf("eval = %d from copyin(%08x, %08x, %ld)\n", eval,
1001 uap->sops, &sops, nsops * sizeof(struct sembuf));
1002 #endif
1003 goto semopout;
1004 }
1005
1006 /*
1007 * Loop trying to satisfy the vector of requests.
1008 * If we reach a point where we must wait, any requests already
1009 * performed are rolled back and we go to sleep until some other
1010 * process wakes us up. At this point, we start all over again.
1011 *
1012 * This ensures that from the perspective of other tasks, a set
1013 * of requests is atomic (never partially satisfied).
1014 */
1015 do_undos = 0;
1016
1017 for (;;) {
1018 do_wakeup = 0;
1019
1020 for (i = 0; i < nsops; i++) {
1021 sopptr = &sops[i];
1022
1023 if (sopptr->sem_num >= semaptr->sem_nsems) {
1024 eval = EFBIG;
1025 goto semopout;
1026 }
1027
1028 semptr = &semaptr->sem_base[sopptr->sem_num];
1029
1030 #ifdef SEM_DEBUG
1031 printf("semop: semaptr=%x, sem_base=%x, semptr=%x, sem[%d]=%d : op=%d, flag=%s\n",
1032 semaptr, semaptr->sem_base, semptr,
1033 sopptr->sem_num, semptr->semval, sopptr->sem_op,
1034 (sopptr->sem_flg & IPC_NOWAIT) ? "nowait" : "wait");
1035 #endif
1036
1037 if (sopptr->sem_op < 0) {
1038 if (semptr->semval + sopptr->sem_op < 0) {
1039 #ifdef SEM_DEBUG
1040 printf("semop: can't do it now\n");
1041 #endif
1042 break;
1043 } else {
1044 semptr->semval += sopptr->sem_op;
1045 if (semptr->semval == 0 &&
1046 semptr->semzcnt > 0)
1047 do_wakeup = 1;
1048 }
1049 if (sopptr->sem_flg & SEM_UNDO)
1050 do_undos = 1;
1051 } else if (sopptr->sem_op == 0) {
1052 if (semptr->semval > 0) {
1053 #ifdef SEM_DEBUG
1054 printf("semop: not zero now\n");
1055 #endif
1056 break;
1057 }
1058 } else {
1059 if (semptr->semncnt > 0)
1060 do_wakeup = 1;
1061 semptr->semval += sopptr->sem_op;
1062 if (sopptr->sem_flg & SEM_UNDO)
1063 do_undos = 1;
1064 }
1065 }
1066
1067 /*
1068 * Did we get through the entire vector?
1069 */
1070 if (i >= nsops)
1071 goto done;
1072
1073 /*
1074 * No ... rollback anything that we've already done
1075 */
1076 #ifdef SEM_DEBUG
1077 printf("semop: rollback 0 through %d\n", i-1);
1078 #endif
1079 for (j = 0; j < i; j++)
1080 semaptr->sem_base[sops[j].sem_num].semval -=
1081 sops[j].sem_op;
1082
1083 /*
1084 * If the request that we couldn't satisfy has the
1085 * NOWAIT flag set then return with EAGAIN.
1086 */
1087 if (sopptr->sem_flg & IPC_NOWAIT) {
1088 eval = EAGAIN;
1089 goto semopout;
1090 }
1091
1092 if (sopptr->sem_op == 0)
1093 semptr->semzcnt++;
1094 else
1095 semptr->semncnt++;
1096
1097 #ifdef SEM_DEBUG
1098 printf("semop: good night!\n");
1099 #endif
1100 /* Release our lock on the semaphore subsystem so
1101 * another thread can get at the semaphore we are
1102 * waiting for. We will get the lock back after we
1103 * wake up.
1104 */
1105 eval = msleep((caddr_t)semaptr, &sysv_sem_subsys_mutex , (PZERO - 4) | PCATCH,
1106 "semwait", 0);
1107
1108 #ifdef SEM_DEBUG
1109 printf("semop: good morning (eval=%d)!\n", eval);
1110 #endif
1111 if (eval != 0) {
1112 eval = EINTR;
1113 }
1114
1115 /*
1116 * IMPORTANT: while we were asleep, the semaphore array might
1117 * have been reallocated somewhere else (see grow_sema_array()).
1118 * When we wake up, we have to re-lookup the semaphore
1119 * structures and re-validate them.
1120 */
1121
1122 suptr = NULL; /* sem_undo may have been reallocated */
1123 semaptr = &sema[semid]; /* sema may have been reallocated */
1124
1125 /*
1126 * Make sure that the semaphore still exists
1127 */
1128 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
1129 semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid) ||
1130 sopptr->sem_num >= semaptr->sem_nsems) {
1131 if (eval == EINTR) {
1132 /*
1133 * EINTR takes precedence over the fact that
1134 * the semaphore disappeared while we were
1135 * sleeping...
1136 */
1137 } else {
1138 /*
1139 * The man page says to return EIDRM.
1140 * Unfortunately, BSD doesn't define that code!
1141 */
1142 #ifdef EIDRM
1143 eval = EIDRM;
1144 #else
1145 eval = EINVAL;
1146 #endif
1147 }
1148 goto semopout;
1149 }
1150
1151 /*
1152 * The semaphore is still alive. Readjust the count of
1153 * waiting processes. semptr needs to be recomputed
1154 * because the sem[] may have been reallocated while
1155 * we were sleeping, updating our sem_base pointer.
1156 */
1157 semptr = &semaptr->sem_base[sopptr->sem_num];
1158 if (sopptr->sem_op == 0)
1159 semptr->semzcnt--;
1160 else
1161 semptr->semncnt--;
1162
1163 if (eval != 0) { /* EINTR */
1164 goto semopout;
1165 }
1166 }
1167
1168 done:
1169 /*
1170 * Process any SEM_UNDO requests.
1171 */
1172 if (do_undos) {
1173 for (i = 0; i < nsops; i++) {
1174 /*
1175 * We only need to deal with SEM_UNDO's for non-zero
1176 * op's.
1177 */
1178 int adjval;
1179
1180 if ((sops[i].sem_flg & SEM_UNDO) == 0)
1181 continue;
1182 adjval = sops[i].sem_op;
1183 if (adjval == 0)
1184 continue;
1185 eval = semundo_adjust(p, &suptr, semid,
1186 sops[i].sem_num, -adjval);
1187 if (eval == 0)
1188 continue;
1189
1190 /*
1191 * Oh-Oh! We ran out of either sem_undo's or undo's.
1192 * Rollback the adjustments to this point and then
1193 * rollback the semaphore ups and down so we can return
1194 * with an error with all structures restored. We
1195 * rollback the undo's in the exact reverse order that
1196 * we applied them. This guarantees that we won't run
1197 * out of space as we roll things back out.
1198 */
1199 for (j = i - 1; j >= 0; j--) {
1200 if ((sops[j].sem_flg & SEM_UNDO) == 0)
1201 continue;
1202 adjval = sops[j].sem_op;
1203 if (adjval == 0)
1204 continue;
1205 if (semundo_adjust(p, &suptr, semid,
1206 sops[j].sem_num, adjval) != 0)
1207 panic("semop - can't undo undos");
1208 }
1209
1210 for (j = 0; j < nsops; j++)
1211 semaptr->sem_base[sops[j].sem_num].semval -=
1212 sops[j].sem_op;
1213
1214 #ifdef SEM_DEBUG
1215 printf("eval = %d from semundo_adjust\n", eval);
1216 #endif
1217 goto semopout;
1218 } /* loop through the sops */
1219 } /* if (do_undos) */
1220
1221 /* We're definitely done - set the sempid's */
1222 for (i = 0; i < nsops; i++) {
1223 sopptr = &sops[i];
1224 semptr = &semaptr->sem_base[sopptr->sem_num];
1225 semptr->sempid = p->p_pid;
1226 }
1227
1228 if (do_wakeup) {
1229 #ifdef SEM_DEBUG
1230 printf("semop: doing wakeup\n");
1231 #ifdef SEM_WAKEUP
1232 sem_wakeup((caddr_t)semaptr);
1233 #else
1234 wakeup((caddr_t)semaptr);
1235 #endif
1236 printf("semop: back from wakeup\n");
1237 #else
1238 wakeup((caddr_t)semaptr);
1239 #endif
1240 }
1241 #ifdef SEM_DEBUG
1242 printf("semop: done\n");
1243 #endif
1244 *retval = 0;
1245 eval = 0;
1246 semopout:
1247 SYSV_SEM_SUBSYS_UNLOCK();
1248 return(eval);
1249 }
1250
1251 /*
1252 * Go through the undo structures for this process and apply the adjustments to
1253 * semaphores.
1254 */
1255 void
1256 semexit(struct proc *p)
1257 {
1258 register struct sem_undo *suptr;
1259 register struct sem_undo **supptr;
1260 int did_something;
1261
1262 /* If we have not allocated our semaphores yet there can't be
1263 * anything to undo, but we need the lock to prevent
1264 * dynamic memory race conditions.
1265 */
1266 SYSV_SEM_SUBSYS_LOCK();
1267
1268 if (!sem_pool)
1269 {
1270 SYSV_SEM_SUBSYS_UNLOCK();
1271 return;
1272 }
1273 did_something = 0;
1274
1275 /*
1276 * Go through the chain of undo vectors looking for one
1277 * associated with this process.
1278 */
1279
1280 for (supptr = &semu_list; (suptr = *supptr) != NULL;
1281 supptr = &suptr->un_next) {
1282 if (suptr->un_proc == p)
1283 break;
1284 }
1285
1286 if (suptr == NULL)
1287 goto unlock;
1288
1289 #ifdef SEM_DEBUG
1290 printf("proc @%08x has undo structure with %d entries\n", p,
1291 suptr->un_cnt);
1292 #endif
1293
1294 /*
1295 * If there are any active undo elements then process them.
1296 */
1297 if (suptr->un_cnt > 0) {
1298 while (suptr->un_ent != NULL) {
1299 struct undo *sueptr;
1300 int semid;
1301 int semnum;
1302 int adjval;
1303 struct user_semid_ds *semaptr;
1304
1305 sueptr = suptr->un_ent;
1306 semid = sueptr->une_id;
1307 semnum = sueptr->une_num;
1308 adjval = sueptr->une_adjval;
1309
1310 semaptr = &sema[semid];
1311 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
1312 panic("semexit - semid not allocated");
1313 if (semnum >= semaptr->sem_nsems)
1314 panic("semexit - semnum out of range");
1315
1316 #ifdef SEM_DEBUG
1317 printf("semexit: %08x id=%d num=%d(adj=%d) ; sem=%d\n",
1318 suptr->un_proc,
1319 semid,
1320 semnum,
1321 adjval,
1322 semaptr->sem_base[semnum].semval);
1323 #endif
1324
1325 if (adjval < 0) {
1326 if (semaptr->sem_base[semnum].semval < -adjval)
1327 semaptr->sem_base[semnum].semval = 0;
1328 else
1329 semaptr->sem_base[semnum].semval +=
1330 adjval;
1331 } else
1332 semaptr->sem_base[semnum].semval += adjval;
1333
1334 /* Maybe we should build a list of semaptr's to wake
1335 * up, finish all access to data structures, release the
1336 * subsystem lock, and wake all the processes. Something
1337 * to think about. It wouldn't buy us anything unless
1338 * wakeup had the potential to block, or the syscall
1339 * funnel state was changed to allow multiple threads
1340 * in the BSD code at once.
1341 */
1342 #ifdef SEM_WAKEUP
1343 sem_wakeup((caddr_t)semaptr);
1344 #else
1345 wakeup((caddr_t)semaptr);
1346 #endif
1347 #ifdef SEM_DEBUG
1348 printf("semexit: back from wakeup\n");
1349 #endif
1350 suptr->un_cnt--;
1351 suptr->un_ent = sueptr->une_next;
1352 FREE(sueptr, M_SYSVSEM);
1353 sueptr = NULL;
1354 }
1355 }
1356
1357 /*
1358 * Deallocate the undo vector.
1359 */
1360 #ifdef SEM_DEBUG
1361 printf("removing vector\n");
1362 #endif
1363 suptr->un_proc = NULL;
1364 *supptr = suptr->un_next;
1365
1366 unlock:
1367 /*
1368 * There is a semaphore leak (i.e. memory leak) in this code.
1369 * We should be deleting the IPC_PRIVATE semaphores when they are
1370 * no longer needed, and we dont. We would have to track which processes
1371 * know about which IPC_PRIVATE semaphores, updating the list after
1372 * every fork. We can't just delete them semaphore when the process
1373 * that created it dies, because that process may well have forked
1374 * some children. So we need to wait until all of it's children have
1375 * died, and so on. Maybe we should tag each IPC_PRIVATE sempahore
1376 * with the creating group ID, count the number of processes left in
1377 * that group, and delete the semaphore when the group is gone.
1378 * Until that code gets implemented we will leak IPC_PRIVATE semaphores.
1379 * There is an upper bound on the size of our semaphore array, so
1380 * leaking the semaphores should not work as a DOS attack.
1381 *
1382 * Please note that the original BSD code this file is based on had the
1383 * same leaky semaphore problem.
1384 */
1385
1386 SYSV_SEM_SUBSYS_UNLOCK();
1387 }
1388
1389
1390 /* (struct sysctl_oid *oidp, void *arg1, int arg2, \
1391 struct sysctl_req *req) */
1392 static int
1393 sysctl_seminfo(__unused struct sysctl_oid *oidp, void *arg1,
1394 __unused int arg2, struct sysctl_req *req)
1395 {
1396 int error = 0;
1397
1398 error = SYSCTL_OUT(req, arg1, sizeof(int));
1399 if (error || req->newptr == USER_ADDR_NULL)
1400 return(error);
1401
1402 SYSV_SEM_SUBSYS_LOCK();
1403
1404 /* Set the values only if shared memory is not initialised */
1405 if ((sem_pool == NULL) &&
1406 (sema == NULL) &&
1407 (semu == NULL) &&
1408 (semu_list == NULL)) {
1409 if ((error = SYSCTL_IN(req, arg1, sizeof(int)))) {
1410 goto out;
1411 }
1412 } else
1413 error = EINVAL;
1414 out:
1415 SYSV_SEM_SUBSYS_UNLOCK();
1416 return(error);
1417
1418 }
1419
1420 /* SYSCTL_NODE(_kern, KERN_SYSV, sysv, CTLFLAG_RW, 0, "SYSV"); */
1421 extern struct sysctl_oid_list sysctl__kern_sysv_children;
1422 SYSCTL_PROC(_kern_sysv, KSYSV_SEMMNI, semmni, CTLTYPE_INT | CTLFLAG_RW,
1423 &limitseminfo.semmni, 0, &sysctl_seminfo ,"I","semmni");
1424
1425 SYSCTL_PROC(_kern_sysv, KSYSV_SEMMNS, semmns, CTLTYPE_INT | CTLFLAG_RW,
1426 &limitseminfo.semmns, 0, &sysctl_seminfo ,"I","semmns");
1427
1428 SYSCTL_PROC(_kern_sysv, KSYSV_SEMMNU, semmnu, CTLTYPE_INT | CTLFLAG_RW,
1429 &limitseminfo.semmnu, 0, &sysctl_seminfo ,"I","semmnu");
1430
1431 SYSCTL_PROC(_kern_sysv, KSYSV_SEMMSL, semmsl, CTLTYPE_INT | CTLFLAG_RW,
1432 &limitseminfo.semmsl, 0, &sysctl_seminfo ,"I","semmsl");
1433
1434 SYSCTL_PROC(_kern_sysv, KSYSV_SEMUNE, semume, CTLTYPE_INT | CTLFLAG_RW,
1435 &limitseminfo.semume, 0, &sysctl_seminfo ,"I","semume");
1436
1437
1438 static int
1439 IPCS_sem_sysctl(__unused struct sysctl_oid *oidp, __unused void *arg1,
1440 __unused int arg2, struct sysctl_req *req)
1441 {
1442 int error;
1443 int cursor;
1444 union {
1445 struct IPCS_command u32;
1446 struct user_IPCS_command u64;
1447 } ipcs;
1448 struct semid_ds semid_ds32; /* post conversion, 32 bit version */
1449 void *semid_dsp;
1450 size_t ipcs_sz = sizeof(struct user_IPCS_command);
1451 size_t semid_ds_sz = sizeof(struct user_semid_ds);
1452 struct proc *p = current_proc();
1453
1454 /* Copy in the command structure */
1455 if ((error = SYSCTL_IN(req, &ipcs, ipcs_sz)) != 0) {
1456 return(error);
1457 }
1458
1459 if (!IS_64BIT_PROCESS(p)) {
1460 ipcs_sz = sizeof(struct IPCS_command);
1461 semid_ds_sz = sizeof(struct semid_ds);
1462 }
1463
1464 /* Let us version this interface... */
1465 if (ipcs.u64.ipcs_magic != IPCS_MAGIC) {
1466 return(EINVAL);
1467 }
1468
1469 SYSV_SEM_SUBSYS_LOCK();
1470 switch(ipcs.u64.ipcs_op) {
1471 case IPCS_SEM_CONF: /* Obtain global configuration data */
1472 if (ipcs.u64.ipcs_datalen != sizeof(struct seminfo)) {
1473 error = ERANGE;
1474 break;
1475 }
1476 if (ipcs.u64.ipcs_cursor != 0) { /* fwd. compat. */
1477 error = EINVAL;
1478 break;
1479 }
1480 error = copyout(&seminfo, ipcs.u64.ipcs_data, ipcs.u64.ipcs_datalen);
1481 break;
1482
1483 case IPCS_SEM_ITER: /* Iterate over existing segments */
1484 cursor = ipcs.u64.ipcs_cursor;
1485 if (cursor < 0 || cursor >= seminfo.semmni) {
1486 error = ERANGE;
1487 break;
1488 }
1489 if (ipcs.u64.ipcs_datalen != (int)semid_ds_sz ) {
1490 error = EINVAL;
1491 break;
1492 }
1493 for( ; cursor < seminfo.semmni; cursor++) {
1494 if (sema[cursor].sem_perm.mode & SEM_ALLOC)
1495 break;
1496 continue;
1497 }
1498 if (cursor == seminfo.semmni) {
1499 error = ENOENT;
1500 break;
1501 }
1502
1503 semid_dsp = &sema[cursor]; /* default: 64 bit */
1504
1505 /*
1506 * If necessary, convert the 64 bit kernel segment
1507 * descriptor to a 32 bit user one.
1508 */
1509 if (!IS_64BIT_PROCESS(p)) {
1510 semid_ds_64to32(semid_dsp, &semid_ds32);
1511 semid_dsp = &semid_ds32;
1512 }
1513 error = copyout(semid_dsp, ipcs.u64.ipcs_data, ipcs.u64.ipcs_datalen);
1514 if (!error) {
1515 /* update cursor */
1516 ipcs.u64.ipcs_cursor = cursor + 1;
1517 error = SYSCTL_OUT(req, &ipcs, ipcs_sz);
1518 }
1519 break;
1520
1521 default:
1522 error = EINVAL;
1523 break;
1524 }
1525 SYSV_SEM_SUBSYS_UNLOCK();
1526 return(error);
1527 }
1528
1529 SYSCTL_DECL(_kern_sysv_ipcs);
1530 SYSCTL_PROC(_kern_sysv_ipcs, OID_AUTO, sem, CTLFLAG_RW|CTLFLAG_ANYBODY,
1531 0, 0, IPCS_sem_sysctl,
1532 "S,IPCS_sem_command",
1533 "ipcs sem command interface");