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