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