2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Implementation of SVID semaphores
33 * Author: Daniel Boulet
35 * This software is provided ``AS IS'' without any warranties of any kind.
38 * John Bellardo modified the implementation for Darwin. 12/2000
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/proc_internal.h>
45 #include <sys/kauth.h>
46 #include <sys/sem_internal.h>
47 #include <sys/malloc.h>
48 #include <mach/mach_types.h>
50 #include <sys/filedesc.h>
51 #include <sys/file_internal.h>
52 #include <sys/sysctl.h>
54 #include <sys/sysent.h>
55 #include <sys/sysproto.h>
57 #include <bsm/audit_kernel.h>
60 /* Uncomment this line to see the debugging output */
61 /* #define SEM_DEBUG */
63 #define M_SYSVSEM M_TEMP
66 /* Hard system limits to avoid resource starvation / DOS attacks.
67 * These are not needed if we can make the semaphore pages swappable.
69 static 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 */
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.
86 struct 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 */
100 static struct sem_undo
*semu_alloc(struct proc
*p
);
101 static int semundo_adjust(struct proc
*p
, struct sem_undo
**supptr
,
102 int semid
, int semnum
, int adjval
);
103 static void semundo_clear(int semid
, int semnum
);
105 /* XXX casting to (sy_call_t *) is bogus, as usual. */
106 static sy_call_t
*semcalls
[] = {
107 (sy_call_t
*)semctl
, (sy_call_t
*)semget
,
111 static int semtot
= 0; /* # of used semaphores */
112 struct user_semid_ds
*sema
= NULL
; /* semaphore id pool */
113 struct sem
*sem_pool
= NULL
; /* semaphore pool */
114 static struct sem_undo
*semu_list
= NULL
; /* active undo structures */
115 struct sem_undo
*semu
= NULL
; /* semaphore undo pool */
118 void sysv_sem_lock_init(void);
119 static lck_grp_t
*sysv_sem_subsys_lck_grp
;
120 static lck_grp_attr_t
*sysv_sem_subsys_lck_grp_attr
;
121 static lck_attr_t
*sysv_sem_subsys_lck_attr
;
122 static lck_mtx_t sysv_sem_subsys_mutex
;
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)
128 __private_extern__
void
129 sysv_sem_lock_init( void )
132 sysv_sem_subsys_lck_grp_attr
= lck_grp_attr_alloc_init();
133 lck_grp_attr_setstat(sysv_sem_subsys_lck_grp_attr
);
135 sysv_sem_subsys_lck_grp
= lck_grp_alloc_init("sysv_shm_subsys_lock", sysv_sem_subsys_lck_grp_attr
);
137 sysv_sem_subsys_lck_attr
= lck_attr_alloc_init();
138 lck_attr_setdebug(sysv_sem_subsys_lck_attr
);
139 lck_mtx_init(&sysv_sem_subsys_mutex
, sysv_sem_subsys_lck_grp
, sysv_sem_subsys_lck_attr
);
142 static __inline__ user_time_t
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.
155 * pad field contents are not moved correspondingly; contents will be lost
157 * NOTE: Source and target may *NOT* overlap! (target is smaller)
160 semid_ds_64to32(struct user_semid_ds
*in
, struct semid_ds
*out
)
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 */
170 * pad field contents are not moved correspondingly; contents will be lost
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
176 * XXX use CAST_USER_ADDR_T() for lack of a CAST_USER_TIME_T(); net effect
180 semid_ds_32to64(struct semid_ds
*in
, struct user_semid_ds
*out
)
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
;
191 * Entry point for all SEM calls
193 * In Darwin this is no longer the entry point. It will be removed after
194 * the code has been tested better.
196 /* XXX actually varargs. */
198 semsys(struct proc
*p
, struct semsys_args
*uap
, register_t
*retval
)
201 /* The individual calls handling the locking now */
203 if (uap
->which
>= sizeof(semcalls
)/sizeof(semcalls
[0]))
205 return ((*semcalls
[uap
->which
])(p
, &uap
->a2
, retval
));
209 * Expand the semu array to the given capacity. If the expansion fails
210 * return 0, otherwise return 1.
212 * Assumes we already have the subsystem lock.
215 grow_semu_array(int newSize
)
218 register struct sem_undo
*newSemu
;
220 if (newSize
<= seminfo
.semmnu
)
222 if (newSize
> limitseminfo
.semmnu
) /* enforce hard limit */
225 printf("undo structure hard limit of %d reached, requested %d\n",
226 limitseminfo
.semmnu
, newSize
);
230 newSize
= (newSize
/SEMMNU_INC
+ 1) * SEMMNU_INC
;
231 newSize
= newSize
> limitseminfo
.semmnu
? limitseminfo
.semmnu
: newSize
;
234 printf("growing semu[] from %d to %d\n", seminfo
.semmnu
, newSize
);
236 MALLOC(newSemu
, struct sem_undo
*, sizeof (struct sem_undo
) * newSize
,
237 M_SYSVSEM
, M_WAITOK
| M_ZERO
);
241 printf("allocation failed. no changes made.\n");
246 /* copy the old data to the new array */
247 for (i
= 0; i
< seminfo
.semmnu
; i
++)
249 newSemu
[i
] = semu
[i
];
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".
257 /* Clean up the old array */
259 FREE(semu
, M_SYSVSEM
);
262 seminfo
.semmnu
= newSize
;
264 printf("expansion successful\n");
270 * Expand the sema array to the given capacity. If the expansion fails
271 * we return 0, otherwise we return 1.
273 * Assumes we already have the subsystem lock.
276 grow_sema_array(int newSize
)
278 register struct user_semid_ds
*newSema
;
281 if (newSize
<= seminfo
.semmni
)
283 if (newSize
> limitseminfo
.semmni
) /* enforce hard limit */
286 printf("identifier hard limit of %d reached, requested %d\n",
287 limitseminfo
.semmni
, newSize
);
291 newSize
= (newSize
/SEMMNI_INC
+ 1) * SEMMNI_INC
;
292 newSize
= newSize
> limitseminfo
.semmni
? limitseminfo
.semmni
: newSize
;
295 printf("growing sema[] from %d to %d\n", seminfo
.semmni
, newSize
);
297 MALLOC(newSema
, struct user_semid_ds
*,
298 sizeof (struct user_semid_ds
) * newSize
,
299 M_SYSVSEM
, M_WAITOK
| M_ZERO
);
303 printf("allocation failed. no changes made.\n");
308 /* copy over the old ids */
309 for (i
= 0; i
< seminfo
.semmni
; i
++)
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
318 * sleep on the correct, reallocated user_semid_ds pointer.
320 if (sema
[i
].sem_perm
.mode
& SEM_ALLOC
)
321 wakeup((caddr_t
)&sema
[i
]);
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".
329 /* Clean up the old array */
331 FREE(sema
, M_SYSVSEM
);
334 seminfo
.semmni
= newSize
;
336 printf("expansion successful\n");
342 * Expand the sem_pool array to the given capacity. If the expansion fails
343 * we return 0 (fail), otherwise we return 1 (success).
345 * Assumes we already hold the subsystem lock.
348 grow_sem_pool(int new_pool_size
)
350 struct sem
*new_sem_pool
= NULL
;
351 struct sem
*sem_free
;
354 if (new_pool_size
< semtot
)
356 /* enforce hard limit */
357 if (new_pool_size
> limitseminfo
.semmns
) {
359 printf("semaphore hard limit of %d reached, requested %d\n",
360 limitseminfo
.semmns
, new_pool_size
);
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
;
369 printf("growing sem_pool array from %d to %d\n", seminfo
.semmns
, new_pool_size
);
371 MALLOC(new_sem_pool
, struct sem
*, sizeof (struct sem
) * new_pool_size
,
372 M_SYSVSEM
, M_WAITOK
| M_ZERO
);
373 if (NULL
== new_sem_pool
) {
375 printf("allocation failed. no changes made.\n");
380 /* We have our new memory, now copy the old contents over */
382 for(i
= 0; i
< seminfo
.semmns
; i
++)
383 new_sem_pool
[i
] = sem_pool
[i
];
385 /* Update our id structures to point to the new semaphores */
386 for(i
= 0; i
< seminfo
.semmni
; i
++) {
387 if (sema
[i
].sem_perm
.mode
& SEM_ALLOC
) /* ID in use */
388 sema
[i
].sem_base
+= (new_sem_pool
- sem_pool
);
392 sem_pool
= new_sem_pool
;
394 /* clean up the old array */
395 if (sem_free
!= NULL
)
396 FREE(sem_free
, M_SYSVSEM
);
398 seminfo
.semmns
= new_pool_size
;
400 printf("expansion complete\n");
406 * Allocate a new sem_undo structure for a process
407 * (returns ptr to structure or NULL if no more room)
409 * Assumes we already hold the subsystem lock.
412 static struct sem_undo
*
413 semu_alloc(struct proc
*p
)
416 register struct sem_undo
*suptr
;
417 register struct sem_undo
**supptr
;
421 * Try twice to allocate something.
422 * (we'll purge any empty structures after the first pass so
423 * two passes are always enough)
426 for (attempt
= 0; attempt
< 2; attempt
++) {
428 * Look for a free structure.
429 * Fill it in and return it if we find one.
432 for (i
= 0; i
< seminfo
.semmnu
; i
++) {
434 if (suptr
->un_proc
== NULL
) {
435 suptr
->un_next
= semu_list
;
438 suptr
->un_ent
= NULL
;
445 * We didn't find a free one, if this is the first attempt
446 * then try to free some structures.
450 /* All the structures are in use - try to free some */
451 int did_something
= 0;
454 while ((suptr
= *supptr
) != NULL
) {
455 if (suptr
->un_cnt
== 0) {
456 suptr
->un_proc
= NULL
;
457 *supptr
= suptr
->un_next
;
460 supptr
= &(suptr
->un_next
);
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.
469 if (!grow_semu_array(seminfo
.semmnu
+ 1))
473 * The second pass failed even though we freed
474 * something after the first pass!
475 * This is IMPOSSIBLE!
477 panic("semu_alloc - second attempt failed");
484 * Adjust a particular entry for a particular proc
486 * Assumes we already hold the subsystem lock.
489 semundo_adjust(struct proc
*p
, struct sem_undo
**supptr
, int semid
,
490 int semnum
, int adjval
)
492 register struct sem_undo
*suptr
;
493 register struct undo
*sueptr
, **suepptr
, *new_sueptr
;
497 * Look for and remember the sem_undo if the caller doesn't provide it
502 for (suptr
= semu_list
; suptr
!= NULL
;
503 suptr
= suptr
->un_next
) {
504 if (suptr
->un_proc
== p
) {
512 suptr
= semu_alloc(p
);
520 * Look for the requested entry and adjust it (delete if adjval becomes
524 for (i
= 0, suepptr
= &suptr
->un_ent
, sueptr
= suptr
->un_ent
;
526 i
++, suepptr
= &sueptr
->une_next
, sueptr
= sueptr
->une_next
) {
527 if (sueptr
->une_id
!= semid
|| sueptr
->une_num
!= semnum
)
530 sueptr
->une_adjval
= 0;
532 sueptr
->une_adjval
+= adjval
;
533 if (sueptr
->une_adjval
== 0) {
535 *suepptr
= sueptr
->une_next
;
536 FREE(sueptr
, M_SYSVSEM
);
542 /* Didn't find the right entry - create it */
544 /* no adjustment: no need for a new entry */
548 if (suptr
->un_cnt
== limitseminfo
.semume
) {
549 /* reached the limit number of semaphore undo entries */
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
) {
560 /* fill in the new semaphore undo entry */
561 new_sueptr
->une_next
= suptr
->un_ent
;
562 suptr
->un_ent
= new_sueptr
;
564 new_sueptr
->une_adjval
= adjval
;
565 new_sueptr
->une_id
= semid
;
566 new_sueptr
->une_num
= semnum
;
571 /* Assumes we already hold the subsystem lock.
574 semundo_clear(int semid
, int semnum
)
576 struct sem_undo
*suptr
;
578 for (suptr
= semu_list
; suptr
!= NULL
; suptr
= suptr
->un_next
) {
580 struct undo
**suepptr
;
583 sueptr
= suptr
->un_ent
;
584 suepptr
= &suptr
->un_ent
;
585 while (i
< suptr
->un_cnt
) {
586 if (sueptr
->une_id
== semid
) {
587 if (semnum
== -1 || sueptr
->une_num
== semnum
) {
589 *suepptr
= sueptr
->une_next
;
590 FREE(sueptr
, M_SYSVSEM
);
598 suepptr
= &sueptr
->une_next
;
599 sueptr
= sueptr
->une_next
;
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.
612 semctl(struct proc
*p
, struct semctl_args
*uap
, register_t
*retval
)
614 int semid
= uap
->semid
;
615 int semnum
= uap
->semnum
;
617 user_semun_t user_arg
= (user_semun_t
)uap
->arg
;
618 kauth_cred_t cred
= kauth_cred_get();
620 struct user_semid_ds sbuf
;
621 struct user_semid_ds
*semaptr
;
622 struct user_semid_ds uds
;
625 AUDIT_ARG(svipc_cmd
, cmd
);
626 AUDIT_ARG(svipc_id
, semid
);
628 SYSV_SEM_SUBSYS_LOCK();
631 printf("call to semctl(%d, %d, %d, 0x%qx)\n", semid
, semnum
, cmd
, user_arg
);
634 semid
= IPCID_TO_IX(semid
);
636 if (semid
< 0 || semid
>= seminfo
.semmni
) {
638 printf("Invalid semid\n");
644 semaptr
= &sema
[semid
];
645 if ((semaptr
->sem_perm
.mode
& SEM_ALLOC
) == 0 ||
646 semaptr
->sem_perm
.seq
!= IPCID_TO_SEQ(uap
->semid
)) {
656 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_M
)))
659 semaptr
->sem_perm
.cuid
= kauth_cred_getuid(cred
);
660 semaptr
->sem_perm
.uid
= kauth_cred_getuid(cred
);
661 semtot
-= semaptr
->sem_nsems
;
662 for (i
= semaptr
->sem_base
- sem_pool
; i
< semtot
; i
++)
663 sem_pool
[i
] = sem_pool
[i
+ semaptr
->sem_nsems
];
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
;
669 semaptr
->sem_perm
.mode
= 0;
670 semundo_clear(semid
, -1);
671 wakeup((caddr_t
)semaptr
);
675 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_M
)))
678 if (IS_64BIT_PROCESS(p
)) {
679 eval
= copyin(user_arg
.buf
, &sbuf
, sizeof(struct user_semid_ds
));
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
);
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);
694 semaptr
->sem_ctime
= sysv_semtime();
698 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
700 bcopy(semaptr
, &uds
, sizeof(struct user_semid_ds
));
701 if (IS_64BIT_PROCESS(p
)) {
702 eval
= copyout(&uds
, user_arg
.buf
, sizeof(struct user_semid_ds
));
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
));
711 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
713 if (semnum
< 0 || semnum
>= semaptr
->sem_nsems
) {
717 rval
= semaptr
->sem_base
[semnum
].semncnt
;
721 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
723 if (semnum
< 0 || semnum
>= semaptr
->sem_nsems
) {
727 rval
= semaptr
->sem_base
[semnum
].sempid
;
731 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
733 if (semnum
< 0 || semnum
>= semaptr
->sem_nsems
) {
737 rval
= semaptr
->sem_base
[semnum
].semval
;
741 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
743 /* XXXXXXXXXXXXXXXX TBD XXXXXXXXXXXXXXXX */
744 for (i
= 0; i
< semaptr
->sem_nsems
; i
++) {
745 /* XXX could be done in one go... */
746 eval
= copyout((caddr_t
)&semaptr
->sem_base
[i
].semval
,
747 user_arg
.array
+ (i
* sizeof(unsigned short)),
748 sizeof(unsigned short));
755 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
757 if (semnum
< 0 || semnum
>= semaptr
->sem_nsems
) {
761 rval
= semaptr
->sem_base
[semnum
].semzcnt
;
765 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_W
)))
768 printf("Invalid credentials for write\n");
772 if (semnum
< 0 || semnum
>= semaptr
->sem_nsems
)
775 printf("Invalid number out of range for set\n");
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.
785 semaptr
->sem_base
[semnum
].semval
= CAST_DOWN(int,user_arg
.buf
);
786 semundo_clear(semid
, semnum
);
787 wakeup((caddr_t
)semaptr
);
791 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_W
)))
793 /*** XXXXXXXXXXXX TBD ********/
794 for (i
= 0; i
< semaptr
->sem_nsems
; i
++) {
795 /* XXX could be done in one go... */
796 eval
= copyin(user_arg
.array
+ (i
* sizeof(unsigned short)),
797 (caddr_t
)&semaptr
->sem_base
[i
].semval
,
798 sizeof(unsigned short));
802 semundo_clear(semid
, -1);
803 wakeup((caddr_t
)semaptr
);
814 SYSV_SEM_SUBSYS_UNLOCK();
819 semget(__unused
struct proc
*p
, struct semget_args
*uap
, register_t
*retval
)
823 int nsems
= uap
->nsems
;
824 int semflg
= uap
->semflg
;
825 kauth_cred_t cred
= kauth_cred_get();
828 if (key
!= IPC_PRIVATE
)
829 printf("semget(0x%x, %d, 0%o)\n", key
, nsems
, semflg
);
831 printf("semget(IPC_PRIVATE, %d, 0%o)\n", nsems
, semflg
);
835 SYSV_SEM_SUBSYS_LOCK();
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
)
844 if (semid
< seminfo
.semmni
) {
846 printf("found public key\n");
848 if ((eval
= ipcperm(cred
, &sema
[semid
].sem_perm
,
851 if (nsems
< 0 || sema
[semid
].sem_nsems
< nsems
) {
853 printf("too small\n");
858 if ((semflg
& IPC_CREAT
) && (semflg
& IPC_EXCL
)) {
860 printf("not exclusive\n");
870 printf("need to allocate an id for the request\n");
872 if (key
== IPC_PRIVATE
|| (semflg
& IPC_CREAT
)) {
873 if (nsems
<= 0 || nsems
> limitseminfo
.semmsl
) {
875 printf("nsems out of range (0<%d<=%d)\n", nsems
,
881 if (nsems
> seminfo
.semmns
- semtot
) {
883 printf("not enough semaphores left (need %d, got %d)\n",
884 nsems
, seminfo
.semmns
- semtot
);
886 if (!grow_sem_pool(semtot
+ nsems
)) {
888 printf("failed to grow the sem array\n");
894 for (semid
= 0; semid
< seminfo
.semmni
; semid
++) {
895 if ((sema
[semid
].sem_perm
.mode
& SEM_ALLOC
) == 0)
898 if (semid
== seminfo
.semmni
) {
900 printf("no more id's available\n");
902 if (!grow_sema_array(seminfo
.semmni
+ 1))
905 printf("failed to grow sema array\n");
912 printf("semid %d is available\n", semid
);
914 sema
[semid
].sem_perm
.key
= key
;
915 sema
[semid
].sem_perm
.cuid
= kauth_cred_getuid(cred
);
916 sema
[semid
].sem_perm
.uid
= kauth_cred_getuid(cred
);
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;
924 sema
[semid
].sem_ctime
= sysv_semtime();
925 sema
[semid
].sem_base
= &sem_pool
[semtot
];
927 bzero(sema
[semid
].sem_base
,
928 sizeof(sema
[semid
].sem_base
[0])*nsems
);
930 printf("sembase = 0x%x, next = 0x%x\n", sema
[semid
].sem_base
,
935 printf("didn't find it and wasn't asked to create it\n");
942 *retval
= IXSEQ_TO_IPCID(semid
, sema
[semid
].sem_perm
);
943 AUDIT_ARG(svipc_id
, *retval
);
945 printf("semget is done, returning %d\n", *retval
);
950 SYSV_SEM_SUBSYS_UNLOCK();
955 semop(struct proc
*p
, struct semop_args
*uap
, register_t
*retval
)
957 int semid
= uap
->semid
;
958 int nsops
= uap
->nsops
;
959 struct sembuf sops
[MAX_SOPS
];
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' */
963 struct sem_undo
*suptr
= NULL
;
965 int do_wakeup
, do_undos
;
967 AUDIT_ARG(svipc_id
, uap
->semid
);
969 SYSV_SEM_SUBSYS_LOCK();
972 printf("call to semop(%d, 0x%x, %d)\n", semid
, sops
, nsops
);
975 semid
= IPCID_TO_IX(semid
); /* Convert back to zero origin */
977 if (semid
< 0 || semid
>= seminfo
.semmni
) {
982 semaptr
= &sema
[semid
];
983 if ((semaptr
->sem_perm
.mode
& SEM_ALLOC
) == 0) {
987 if (semaptr
->sem_perm
.seq
!= IPCID_TO_SEQ(uap
->semid
)) {
992 if ((eval
= ipcperm(kauth_cred_get(), &semaptr
->sem_perm
, IPC_W
))) {
994 printf("eval = %d from ipaccess\n", eval
);
999 if (nsops
< 0 || nsops
> MAX_SOPS
) {
1001 printf("too many sops (max=%d, nsops=%d)\n", MAX_SOPS
, nsops
);
1007 /* OK for LP64, since sizeof(struct sembuf) is currently invariant */
1008 if ((eval
= copyin(uap
->sops
, &sops
, nsops
* sizeof(struct sembuf
))) != 0) {
1010 printf("eval = %d from copyin(%08x, %08x, %ld)\n", eval
,
1011 uap
->sops
, &sops
, nsops
* sizeof(struct sembuf
));
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.
1022 * This ensures that from the perspective of other tasks, a set
1023 * of requests is atomic (never partially satisfied).
1030 for (i
= 0; i
< nsops
; i
++) {
1033 if (sopptr
->sem_num
>= semaptr
->sem_nsems
) {
1038 semptr
= &semaptr
->sem_base
[sopptr
->sem_num
];
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");
1047 if (sopptr
->sem_op
< 0) {
1048 if (semptr
->semval
+ sopptr
->sem_op
< 0) {
1050 printf("semop: can't do it now\n");
1054 semptr
->semval
+= sopptr
->sem_op
;
1055 if (semptr
->semval
== 0 &&
1056 semptr
->semzcnt
> 0)
1059 if (sopptr
->sem_flg
& SEM_UNDO
)
1061 } else if (sopptr
->sem_op
== 0) {
1062 if (semptr
->semval
> 0) {
1064 printf("semop: not zero now\n");
1069 if (semptr
->semncnt
> 0)
1071 semptr
->semval
+= sopptr
->sem_op
;
1072 if (sopptr
->sem_flg
& SEM_UNDO
)
1078 * Did we get through the entire vector?
1084 * No ... rollback anything that we've already done
1087 printf("semop: rollback 0 through %d\n", i
-1);
1089 for (j
= 0; j
< i
; j
++)
1090 semaptr
->sem_base
[sops
[j
].sem_num
].semval
-=
1094 * If the request that we couldn't satisfy has the
1095 * NOWAIT flag set then return with EAGAIN.
1097 if (sopptr
->sem_flg
& IPC_NOWAIT
) {
1102 if (sopptr
->sem_op
== 0)
1108 printf("semop: good night!\n");
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
1115 eval
= msleep((caddr_t
)semaptr
, &sysv_sem_subsys_mutex
, (PZERO
- 4) | PCATCH
,
1119 printf("semop: good morning (eval=%d)!\n", eval
);
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.
1132 suptr
= NULL
; /* sem_undo may have been reallocated */
1133 semaptr
= &sema
[semid
]; /* sema may have been reallocated */
1136 * Make sure that the semaphore still exists
1138 if ((semaptr
->sem_perm
.mode
& SEM_ALLOC
) == 0 ||
1139 semaptr
->sem_perm
.seq
!= IPCID_TO_SEQ(uap
->semid
) ||
1140 sopptr
->sem_num
>= semaptr
->sem_nsems
) {
1141 if (eval
== EINTR
) {
1143 * EINTR takes precedence over the fact that
1144 * the semaphore disappeared while we were
1149 * The man page says to return EIDRM.
1150 * Unfortunately, BSD doesn't define that code!
1162 * The semaphore is still alive. Readjust the count of
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.
1167 semptr
= &semaptr
->sem_base
[sopptr
->sem_num
];
1168 if (sopptr
->sem_op
== 0)
1173 if (eval
!= 0) { /* EINTR */
1180 * Process any SEM_UNDO requests.
1183 for (i
= 0; i
< nsops
; i
++) {
1185 * We only need to deal with SEM_UNDO's for non-zero
1190 if ((sops
[i
].sem_flg
& SEM_UNDO
) == 0)
1192 adjval
= sops
[i
].sem_op
;
1195 eval
= semundo_adjust(p
, &suptr
, semid
,
1196 sops
[i
].sem_num
, -adjval
);
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.
1209 for (j
= i
- 1; j
>= 0; j
--) {
1210 if ((sops
[j
].sem_flg
& SEM_UNDO
) == 0)
1212 adjval
= sops
[j
].sem_op
;
1215 if (semundo_adjust(p
, &suptr
, semid
,
1216 sops
[j
].sem_num
, adjval
) != 0)
1217 panic("semop - can't undo undos");
1220 for (j
= 0; j
< nsops
; j
++)
1221 semaptr
->sem_base
[sops
[j
].sem_num
].semval
-=
1225 printf("eval = %d from semundo_adjust\n", eval
);
1228 } /* loop through the sops */
1229 } /* if (do_undos) */
1231 /* We're definitely done - set the sempid's */
1232 for (i
= 0; i
< nsops
; i
++) {
1234 semptr
= &semaptr
->sem_base
[sopptr
->sem_num
];
1235 semptr
->sempid
= p
->p_pid
;
1240 printf("semop: doing wakeup\n");
1242 sem_wakeup((caddr_t
)semaptr
);
1244 wakeup((caddr_t
)semaptr
);
1246 printf("semop: back from wakeup\n");
1248 wakeup((caddr_t
)semaptr
);
1252 printf("semop: done\n");
1257 SYSV_SEM_SUBSYS_UNLOCK();
1262 * Go through the undo structures for this process and apply the adjustments to
1266 semexit(struct proc
*p
)
1268 register struct sem_undo
*suptr
;
1269 register struct sem_undo
**supptr
;
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.
1276 SYSV_SEM_SUBSYS_LOCK();
1280 SYSV_SEM_SUBSYS_UNLOCK();
1286 * Go through the chain of undo vectors looking for one
1287 * associated with this process.
1290 for (supptr
= &semu_list
; (suptr
= *supptr
) != NULL
;
1291 supptr
= &suptr
->un_next
) {
1292 if (suptr
->un_proc
== p
)
1300 printf("proc @%08x has undo structure with %d entries\n", p
,
1305 * If there are any active undo elements then process them.
1307 if (suptr
->un_cnt
> 0) {
1308 while (suptr
->un_ent
!= NULL
) {
1309 struct undo
*sueptr
;
1313 struct user_semid_ds
*semaptr
;
1315 sueptr
= suptr
->un_ent
;
1316 semid
= sueptr
->une_id
;
1317 semnum
= sueptr
->une_num
;
1318 adjval
= sueptr
->une_adjval
;
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");
1327 printf("semexit: %08x id=%d num=%d(adj=%d) ; sem=%d\n",
1332 semaptr
->sem_base
[semnum
].semval
);
1336 if (semaptr
->sem_base
[semnum
].semval
< -adjval
)
1337 semaptr
->sem_base
[semnum
].semval
= 0;
1339 semaptr
->sem_base
[semnum
].semval
+=
1342 semaptr
->sem_base
[semnum
].semval
+= adjval
;
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.
1353 sem_wakeup((caddr_t
)semaptr
);
1355 wakeup((caddr_t
)semaptr
);
1358 printf("semexit: back from wakeup\n");
1361 suptr
->un_ent
= sueptr
->une_next
;
1362 FREE(sueptr
, M_SYSVSEM
);
1368 * Deallocate the undo vector.
1371 printf("removing vector\n");
1373 suptr
->un_proc
= NULL
;
1374 *supptr
= suptr
->un_next
;
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.
1392 * Please note that the original BSD code this file is based on had the
1393 * same leaky semaphore problem.
1396 SYSV_SEM_SUBSYS_UNLOCK();
1400 /* (struct sysctl_oid *oidp, void *arg1, int arg2, \
1401 struct sysctl_req *req) */
1403 sysctl_seminfo(__unused
struct sysctl_oid
*oidp
, void *arg1
,
1404 __unused
int arg2
, struct sysctl_req
*req
)
1408 error
= SYSCTL_OUT(req
, arg1
, sizeof(int));
1409 if (error
|| req
->newptr
== USER_ADDR_NULL
)
1412 SYSV_SEM_SUBSYS_LOCK();
1414 /* Set the values only if shared memory is not initialised */
1415 if ((sem_pool
== NULL
) &&
1418 (semu_list
== NULL
)) {
1419 if ((error
= SYSCTL_IN(req
, arg1
, sizeof(int)))) {
1425 SYSV_SEM_SUBSYS_UNLOCK();
1430 /* SYSCTL_NODE(_kern, KERN_SYSV, sysv, CTLFLAG_RW, 0, "SYSV"); */
1431 extern struct sysctl_oid_list sysctl__kern_sysv_children
;
1432 SYSCTL_PROC(_kern_sysv
, KSYSV_SEMMNI
, semmni
, CTLTYPE_INT
| CTLFLAG_RW
,
1433 &limitseminfo
.semmni
, 0, &sysctl_seminfo
,"I","semmni");
1435 SYSCTL_PROC(_kern_sysv
, KSYSV_SEMMNS
, semmns
, CTLTYPE_INT
| CTLFLAG_RW
,
1436 &limitseminfo
.semmns
, 0, &sysctl_seminfo
,"I","semmns");
1438 SYSCTL_PROC(_kern_sysv
, KSYSV_SEMMNU
, semmnu
, CTLTYPE_INT
| CTLFLAG_RW
,
1439 &limitseminfo
.semmnu
, 0, &sysctl_seminfo
,"I","semmnu");
1441 SYSCTL_PROC(_kern_sysv
, KSYSV_SEMMSL
, semmsl
, CTLTYPE_INT
| CTLFLAG_RW
,
1442 &limitseminfo
.semmsl
, 0, &sysctl_seminfo
,"I","semmsl");
1444 SYSCTL_PROC(_kern_sysv
, KSYSV_SEMUNE
, semume
, CTLTYPE_INT
| CTLFLAG_RW
,
1445 &limitseminfo
.semume
, 0, &sysctl_seminfo
,"I","semume");
1449 IPCS_sem_sysctl(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
,
1450 __unused
int arg2
, struct sysctl_req
*req
)
1455 struct IPCS_command u32
;
1456 struct user_IPCS_command u64
;
1458 struct semid_ds semid_ds32
; /* post conversion, 32 bit version */
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();
1464 /* Copy in the command structure */
1465 if ((error
= SYSCTL_IN(req
, &ipcs
, ipcs_sz
)) != 0) {
1469 if (!IS_64BIT_PROCESS(p
)) {
1470 ipcs_sz
= sizeof(struct IPCS_command
);
1471 semid_ds_sz
= sizeof(struct semid_ds
);
1474 /* Let us version this interface... */
1475 if (ipcs
.u64
.ipcs_magic
!= IPCS_MAGIC
) {
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
)) {
1486 if (ipcs
.u64
.ipcs_cursor
!= 0) { /* fwd. compat. */
1490 error
= copyout(&seminfo
, ipcs
.u64
.ipcs_data
, ipcs
.u64
.ipcs_datalen
);
1493 case IPCS_SEM_ITER
: /* Iterate over existing segments */
1494 cursor
= ipcs
.u64
.ipcs_cursor
;
1495 if (cursor
< 0 || cursor
>= seminfo
.semmni
) {
1499 if (ipcs
.u64
.ipcs_datalen
!= (int)semid_ds_sz
) {
1503 for( ; cursor
< seminfo
.semmni
; cursor
++) {
1504 if (sema
[cursor
].sem_perm
.mode
& SEM_ALLOC
)
1508 if (cursor
== seminfo
.semmni
) {
1513 semid_dsp
= &sema
[cursor
]; /* default: 64 bit */
1516 * If necessary, convert the 64 bit kernel segment
1517 * descriptor to a 32 bit user one.
1519 if (!IS_64BIT_PROCESS(p
)) {
1520 semid_ds_64to32(semid_dsp
, &semid_ds32
);
1521 semid_dsp
= &semid_ds32
;
1523 error
= copyout(semid_dsp
, ipcs
.u64
.ipcs_data
, ipcs
.u64
.ipcs_datalen
);
1526 ipcs
.u64
.ipcs_cursor
= cursor
+ 1;
1527 error
= SYSCTL_OUT(req
, &ipcs
, ipcs_sz
);
1535 SYSV_SEM_SUBSYS_UNLOCK();
1539 SYSCTL_DECL(_kern_sysv_ipcs
);
1540 SYSCTL_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");