2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
23 * Implementation of SVID semaphores
25 * Author: Daniel Boulet
27 * This software is provided ``AS IS'' without any warranties of any kind.
30 * John Bellardo modified the implementation for Darwin. 12/2000
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>
42 #include <sys/filedesc.h>
43 #include <sys/file_internal.h>
44 #include <sys/sysctl.h>
46 #include <sys/sysent.h>
47 #include <sys/sysproto.h>
49 #include <bsm/audit_kernel.h>
52 /* Uncomment this line to see the debugging output */
53 /* #define SEM_DEBUG */
55 #define M_SYSVSEM M_TEMP
58 /* Hard system limits to avoid resource starvation / DOS attacks.
59 * These are not needed if we can make the semaphore pages swappable.
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 */
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.
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 */
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
);
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
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 */
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
;
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)
120 __private_extern__
void
121 sysv_sem_lock_init( void )
124 sysv_sem_subsys_lck_grp_attr
= lck_grp_attr_alloc_init();
125 lck_grp_attr_setstat(sysv_sem_subsys_lck_grp_attr
);
127 sysv_sem_subsys_lck_grp
= lck_grp_alloc_init("sysv_shm_subsys_lock", sysv_sem_subsys_lck_grp_attr
);
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
);
134 static __inline__ user_time_t
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.
147 * pad field contents are not moved correspondingly; contents will be lost
149 * NOTE: Source and target may *NOT* overlap! (target is smaller)
152 semid_ds_64to32(struct user_semid_ds
*in
, struct semid_ds
*out
)
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 */
162 * pad field contents are not moved correspondingly; contents will be lost
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
168 * XXX use CAST_USER_ADDR_T() for lack of a CAST_USER_TIME_T(); net effect
172 semid_ds_32to64(struct semid_ds
*in
, struct user_semid_ds
*out
)
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
;
183 * Entry point for all SEM calls
185 * In Darwin this is no longer the entry point. It will be removed after
186 * the code has been tested better.
188 /* XXX actually varargs. */
190 semsys(struct proc
*p
, struct semsys_args
*uap
, register_t
*retval
)
193 /* The individual calls handling the locking now */
195 if (uap
->which
>= sizeof(semcalls
)/sizeof(semcalls
[0]))
197 return ((*semcalls
[uap
->which
])(p
, &uap
->a2
, retval
));
201 * Lock or unlock the entire semaphore facility.
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.
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
216 semconfig(__unused
struct proc
*p
, struct semconfig_args
*uap
, register_t
*retval
)
221 case SEM_CONFIG_FREEZE
:
222 SYSV_SEM_SUBSYS_LOCK();
225 case SEM_CONFIG_THAW
:
226 SYSV_SEM_SUBSYS_UNLOCK();
230 printf("semconfig: unknown flag parameter value (%d) - ignored\n",
241 * Expand the semu array to the given capacity. If the expansion fails
242 * return 0, otherwise return 1.
244 * Assumes we already have the subsystem lock.
247 grow_semu_array(int newSize
)
250 register struct sem_undo
*newSemu
;
252 if (newSize
<= seminfo
.semmnu
)
254 if (newSize
> limitseminfo
.semmnu
) /* enforce hard limit */
257 printf("undo structure hard limit of %d reached, requested %d\n",
258 limitseminfo
.semmnu
, newSize
);
262 newSize
= (newSize
/SEMMNU_INC
+ 1) * SEMMNU_INC
;
263 newSize
= newSize
> limitseminfo
.semmnu
? limitseminfo
.semmnu
: newSize
;
266 printf("growing semu[] from %d to %d\n", seminfo
.semmnu
, newSize
);
268 MALLOC(newSemu
, struct sem_undo
*, sizeof (struct sem_undo
) * newSize
,
269 M_SYSVSEM
, M_WAITOK
| M_ZERO
);
273 printf("allocation failed. no changes made.\n");
278 /* copy the old data to the new array */
279 for (i
= 0; i
< seminfo
.semmnu
; i
++)
281 newSemu
[i
] = semu
[i
];
284 * The new elements (from newSemu[i] to newSemu[newSize-1]) have their
285 * "un_proc" set to 0 (i.e. NULL) by the M_ZERO flag to MALLOC() above,
286 * so they're already marked as "not in use".
289 /* Clean up the old array */
291 FREE(semu
, M_SYSVSEM
);
294 seminfo
.semmnu
= newSize
;
296 printf("expansion successful\n");
302 * Expand the sema array to the given capacity. If the expansion fails
303 * we return 0, otherwise we return 1.
305 * Assumes we already have the subsystem lock.
308 grow_sema_array(int newSize
)
310 register struct user_semid_ds
*newSema
;
313 if (newSize
<= seminfo
.semmni
)
315 if (newSize
> limitseminfo
.semmni
) /* enforce hard limit */
318 printf("identifier hard limit of %d reached, requested %d\n",
319 limitseminfo
.semmni
, newSize
);
323 newSize
= (newSize
/SEMMNI_INC
+ 1) * SEMMNI_INC
;
324 newSize
= newSize
> limitseminfo
.semmni
? limitseminfo
.semmni
: newSize
;
327 printf("growing sema[] from %d to %d\n", seminfo
.semmni
, newSize
);
329 MALLOC(newSema
, struct user_semid_ds
*,
330 sizeof (struct user_semid_ds
) * newSize
,
331 M_SYSVSEM
, M_WAITOK
| M_ZERO
);
335 printf("allocation failed. no changes made.\n");
340 /* copy over the old ids */
341 for (i
= 0; i
< seminfo
.semmni
; i
++)
343 newSema
[i
] = sema
[i
];
344 /* This is a hack. What we really want to be able to
345 * do is change the value a process is waiting on
346 * without waking it up, but I don't know how to do
347 * this with the existing code, so we wake up the
348 * process and let it do a lot of work to determine the
349 * semaphore set is really not available yet, and then
350 * sleep on the correct, reallocated user_semid_ds pointer.
352 if (sema
[i
].sem_perm
.mode
& SEM_ALLOC
)
353 wakeup((caddr_t
)&sema
[i
]);
356 * The new elements (from newSema[i] to newSema[newSize-1]) have their
357 * "sem_base" and "sem_perm.mode" set to 0 (i.e. NULL) by the M_ZERO
358 * flag to MALLOC() above, so they're already marked as "not in use".
361 /* Clean up the old array */
363 FREE(sema
, M_SYSVSEM
);
366 seminfo
.semmni
= newSize
;
368 printf("expansion successful\n");
374 * Expand the sem_pool array to the given capacity. If the expansion fails
375 * we return 0 (fail), otherwise we return 1 (success).
377 * Assumes we already hold the subsystem lock.
380 grow_sem_pool(int new_pool_size
)
382 struct sem
*new_sem_pool
= NULL
;
383 struct sem
*sem_free
;
386 if (new_pool_size
< semtot
)
388 /* enforce hard limit */
389 if (new_pool_size
> limitseminfo
.semmns
) {
391 printf("semaphore hard limit of %d reached, requested %d\n",
392 limitseminfo
.semmns
, new_pool_size
);
397 new_pool_size
= (new_pool_size
/SEMMNS_INC
+ 1) * SEMMNS_INC
;
398 new_pool_size
= new_pool_size
> limitseminfo
.semmns
? limitseminfo
.semmns
: new_pool_size
;
401 printf("growing sem_pool array from %d to %d\n", seminfo
.semmns
, new_pool_size
);
403 MALLOC(new_sem_pool
, struct sem
*, sizeof (struct sem
) * new_pool_size
,
404 M_SYSVSEM
, M_WAITOK
| M_ZERO
);
405 if (NULL
== new_sem_pool
) {
407 printf("allocation failed. no changes made.\n");
412 /* We have our new memory, now copy the old contents over */
414 for(i
= 0; i
< seminfo
.semmns
; i
++)
415 new_sem_pool
[i
] = sem_pool
[i
];
417 /* Update our id structures to point to the new semaphores */
418 for(i
= 0; i
< seminfo
.semmni
; i
++) {
419 if (sema
[i
].sem_perm
.mode
& SEM_ALLOC
) /* ID in use */
420 sema
[i
].sem_base
+= (new_sem_pool
- sem_pool
);
424 sem_pool
= new_sem_pool
;
426 /* clean up the old array */
427 if (sem_free
!= NULL
)
428 FREE(sem_free
, M_SYSVSEM
);
430 seminfo
.semmns
= new_pool_size
;
432 printf("expansion complete\n");
438 * Allocate a new sem_undo structure for a process
439 * (returns ptr to structure or NULL if no more room)
441 * Assumes we already hold the subsystem lock.
444 static struct sem_undo
*
445 semu_alloc(struct proc
*p
)
448 register struct sem_undo
*suptr
;
449 register struct sem_undo
**supptr
;
453 * Try twice to allocate something.
454 * (we'll purge any empty structures after the first pass so
455 * two passes are always enough)
458 for (attempt
= 0; attempt
< 2; attempt
++) {
460 * Look for a free structure.
461 * Fill it in and return it if we find one.
464 for (i
= 0; i
< seminfo
.semmnu
; i
++) {
466 if (suptr
->un_proc
== NULL
) {
467 suptr
->un_next
= semu_list
;
470 suptr
->un_ent
= NULL
;
477 * We didn't find a free one, if this is the first attempt
478 * then try to free some structures.
482 /* All the structures are in use - try to free some */
483 int did_something
= 0;
486 while ((suptr
= *supptr
) != NULL
) {
487 if (suptr
->un_cnt
== 0) {
488 suptr
->un_proc
= NULL
;
489 *supptr
= suptr
->un_next
;
492 supptr
= &(suptr
->un_next
);
495 /* If we didn't free anything. Try expanding
496 * the semu[] array. If that doesn't work
497 * then fail. We expand last to get the
498 * most reuse out of existing resources.
501 if (!grow_semu_array(seminfo
.semmnu
+ 1))
505 * The second pass failed even though we freed
506 * something after the first pass!
507 * This is IMPOSSIBLE!
509 panic("semu_alloc - second attempt failed");
516 * Adjust a particular entry for a particular proc
518 * Assumes we already hold the subsystem lock.
521 semundo_adjust(struct proc
*p
, struct sem_undo
**supptr
, int semid
,
522 int semnum
, int adjval
)
524 register struct sem_undo
*suptr
;
525 register struct undo
*sueptr
, **suepptr
, *new_sueptr
;
529 * Look for and remember the sem_undo if the caller doesn't provide it
534 for (suptr
= semu_list
; suptr
!= NULL
;
535 suptr
= suptr
->un_next
) {
536 if (suptr
->un_proc
== p
) {
544 suptr
= semu_alloc(p
);
552 * Look for the requested entry and adjust it (delete if adjval becomes
556 for (i
= 0, suepptr
= &suptr
->un_ent
, sueptr
= suptr
->un_ent
;
558 i
++, suepptr
= &sueptr
->une_next
, sueptr
= sueptr
->une_next
) {
559 if (sueptr
->une_id
!= semid
|| sueptr
->une_num
!= semnum
)
562 sueptr
->une_adjval
= 0;
564 sueptr
->une_adjval
+= adjval
;
565 if (sueptr
->une_adjval
== 0) {
567 *suepptr
= sueptr
->une_next
;
568 FREE(sueptr
, M_SYSVSEM
);
574 /* Didn't find the right entry - create it */
576 /* no adjustment: no need for a new entry */
580 if (suptr
->un_cnt
== limitseminfo
.semume
) {
581 /* reached the limit number of semaphore undo entries */
585 /* allocate a new semaphore undo entry */
586 MALLOC(new_sueptr
, struct undo
*, sizeof (struct undo
),
587 M_SYSVSEM
, M_WAITOK
);
588 if (new_sueptr
== NULL
) {
592 /* fill in the new semaphore undo entry */
593 new_sueptr
->une_next
= suptr
->un_ent
;
594 suptr
->un_ent
= new_sueptr
;
596 new_sueptr
->une_adjval
= adjval
;
597 new_sueptr
->une_id
= semid
;
598 new_sueptr
->une_num
= semnum
;
603 /* Assumes we already hold the subsystem lock.
606 semundo_clear(int semid
, int semnum
)
608 struct sem_undo
*suptr
;
610 for (suptr
= semu_list
; suptr
!= NULL
; suptr
= suptr
->un_next
) {
612 struct undo
**suepptr
;
615 sueptr
= suptr
->un_ent
;
616 suepptr
= &suptr
->un_ent
;
617 while (i
< suptr
->un_cnt
) {
618 if (sueptr
->une_id
== semid
) {
619 if (semnum
== -1 || sueptr
->une_num
== semnum
) {
621 *suepptr
= sueptr
->une_next
;
622 FREE(sueptr
, M_SYSVSEM
);
630 suepptr
= &sueptr
->une_next
;
631 sueptr
= sueptr
->une_next
;
637 * Note that the user-mode half of this passes a union coerced to a
638 * user_addr_t. The union contains either an int or a pointer, and
639 * so we have to coerce it back, variant on whether the calling
640 * process is 64 bit or not. The coercion works for the 'val' element
641 * because the alignment is the same in user and kernel space.
644 semctl(struct proc
*p
, struct semctl_args
*uap
, register_t
*retval
)
646 int semid
= uap
->semid
;
647 int semnum
= uap
->semnum
;
649 user_semun_t user_arg
= (user_semun_t
)uap
->arg
;
650 kauth_cred_t cred
= kauth_cred_get();
652 struct user_semid_ds sbuf
;
653 struct user_semid_ds
*semaptr
;
654 struct user_semid_ds uds
;
657 AUDIT_ARG(svipc_cmd
, cmd
);
658 AUDIT_ARG(svipc_id
, semid
);
660 SYSV_SEM_SUBSYS_LOCK();
663 printf("call to semctl(%d, %d, %d, 0x%qx)\n", semid
, semnum
, cmd
, user_arg
);
666 semid
= IPCID_TO_IX(semid
);
668 if (semid
< 0 || semid
>= seminfo
.semmni
) {
670 printf("Invalid semid\n");
676 semaptr
= &sema
[semid
];
677 if ((semaptr
->sem_perm
.mode
& SEM_ALLOC
) == 0 ||
678 semaptr
->sem_perm
.seq
!= IPCID_TO_SEQ(uap
->semid
)) {
688 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_M
)))
691 semaptr
->sem_perm
.cuid
= kauth_cred_getuid(cred
);
692 semaptr
->sem_perm
.uid
= kauth_cred_getuid(cred
);
693 semtot
-= semaptr
->sem_nsems
;
694 for (i
= semaptr
->sem_base
- sem_pool
; i
< semtot
; i
++)
695 sem_pool
[i
] = sem_pool
[i
+ semaptr
->sem_nsems
];
696 for (i
= 0; i
< seminfo
.semmni
; i
++) {
697 if ((sema
[i
].sem_perm
.mode
& SEM_ALLOC
) &&
698 sema
[i
].sem_base
> semaptr
->sem_base
)
699 sema
[i
].sem_base
-= semaptr
->sem_nsems
;
701 semaptr
->sem_perm
.mode
= 0;
702 semundo_clear(semid
, -1);
703 wakeup((caddr_t
)semaptr
);
707 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_M
)))
710 if (IS_64BIT_PROCESS(p
)) {
711 eval
= copyin(user_arg
.buf
, &sbuf
, sizeof(struct user_semid_ds
));
713 eval
= copyin(user_arg
.buf
, &sbuf
, sizeof(struct semid_ds
));
714 /* convert in place; ugly, but safe */
715 semid_ds_32to64((struct semid_ds
*)&sbuf
, &sbuf
);
722 semaptr
->sem_perm
.uid
= sbuf
.sem_perm
.uid
;
723 semaptr
->sem_perm
.gid
= sbuf
.sem_perm
.gid
;
724 semaptr
->sem_perm
.mode
= (semaptr
->sem_perm
.mode
& ~0777) |
725 (sbuf
.sem_perm
.mode
& 0777);
726 semaptr
->sem_ctime
= sysv_semtime();
730 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
732 bcopy(semaptr
, &uds
, sizeof(struct user_semid_ds
));
733 if (IS_64BIT_PROCESS(p
)) {
734 eval
= copyout(&uds
, user_arg
.buf
, sizeof(struct user_semid_ds
));
736 struct semid_ds semid_ds32
;
737 semid_ds_64to32(&uds
, &semid_ds32
);
738 eval
= copyout(&semid_ds32
, user_arg
.buf
, sizeof(struct semid_ds
));
743 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
745 if (semnum
< 0 || semnum
>= semaptr
->sem_nsems
) {
749 rval
= semaptr
->sem_base
[semnum
].semncnt
;
753 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
755 if (semnum
< 0 || semnum
>= semaptr
->sem_nsems
) {
759 rval
= semaptr
->sem_base
[semnum
].sempid
;
763 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
765 if (semnum
< 0 || semnum
>= semaptr
->sem_nsems
) {
769 rval
= semaptr
->sem_base
[semnum
].semval
;
773 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
775 /* XXXXXXXXXXXXXXXX TBD XXXXXXXXXXXXXXXX */
776 for (i
= 0; i
< semaptr
->sem_nsems
; i
++) {
777 /* XXX could be done in one go... */
778 eval
= copyout((caddr_t
)&semaptr
->sem_base
[i
].semval
,
779 user_arg
.array
+ (i
* sizeof(unsigned short)),
780 sizeof(unsigned short));
787 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
789 if (semnum
< 0 || semnum
>= semaptr
->sem_nsems
) {
793 rval
= semaptr
->sem_base
[semnum
].semzcnt
;
797 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_W
)))
800 printf("Invalid credentials for write\n");
804 if (semnum
< 0 || semnum
>= semaptr
->sem_nsems
)
807 printf("Invalid number out of range for set\n");
813 * Cast down a pointer instead of using 'val' member directly
814 * to avoid introducing endieness and a pad field into the
815 * header file. Ugly, but it works.
817 semaptr
->sem_base
[semnum
].semval
= CAST_DOWN(int,user_arg
.buf
);
818 semundo_clear(semid
, semnum
);
819 wakeup((caddr_t
)semaptr
);
823 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_W
)))
825 /*** XXXXXXXXXXXX TBD ********/
826 for (i
= 0; i
< semaptr
->sem_nsems
; i
++) {
827 /* XXX could be done in one go... */
828 eval
= copyin(user_arg
.array
+ (i
* sizeof(unsigned short)),
829 (caddr_t
)&semaptr
->sem_base
[i
].semval
,
830 sizeof(unsigned short));
834 semundo_clear(semid
, -1);
835 wakeup((caddr_t
)semaptr
);
846 SYSV_SEM_SUBSYS_UNLOCK();
851 semget(__unused
struct proc
*p
, struct semget_args
*uap
, register_t
*retval
)
855 int nsems
= uap
->nsems
;
856 int semflg
= uap
->semflg
;
857 kauth_cred_t cred
= kauth_cred_get();
860 if (key
!= IPC_PRIVATE
)
861 printf("semget(0x%x, %d, 0%o)\n", key
, nsems
, semflg
);
863 printf("semget(IPC_PRIVATE, %d, 0%o)\n", nsems
, semflg
);
867 SYSV_SEM_SUBSYS_LOCK();
870 if (key
!= IPC_PRIVATE
) {
871 for (semid
= 0; semid
< seminfo
.semmni
; semid
++) {
872 if ((sema
[semid
].sem_perm
.mode
& SEM_ALLOC
) &&
873 sema
[semid
].sem_perm
.key
== key
)
876 if (semid
< seminfo
.semmni
) {
878 printf("found public key\n");
880 if ((eval
= ipcperm(cred
, &sema
[semid
].sem_perm
,
883 if (nsems
< 0 || sema
[semid
].sem_nsems
< nsems
) {
885 printf("too small\n");
890 if ((semflg
& IPC_CREAT
) && (semflg
& IPC_EXCL
)) {
892 printf("not exclusive\n");
902 printf("need to allocate an id for the request\n");
904 if (key
== IPC_PRIVATE
|| (semflg
& IPC_CREAT
)) {
905 if (nsems
<= 0 || nsems
> limitseminfo
.semmsl
) {
907 printf("nsems out of range (0<%d<=%d)\n", nsems
,
913 if (nsems
> seminfo
.semmns
- semtot
) {
915 printf("not enough semaphores left (need %d, got %d)\n",
916 nsems
, seminfo
.semmns
- semtot
);
918 if (!grow_sem_pool(semtot
+ nsems
)) {
920 printf("failed to grow the sem array\n");
926 for (semid
= 0; semid
< seminfo
.semmni
; semid
++) {
927 if ((sema
[semid
].sem_perm
.mode
& SEM_ALLOC
) == 0)
930 if (semid
== seminfo
.semmni
) {
932 printf("no more id's available\n");
934 if (!grow_sema_array(seminfo
.semmni
+ 1))
937 printf("failed to grow sema array\n");
944 printf("semid %d is available\n", semid
);
946 sema
[semid
].sem_perm
.key
= key
;
947 sema
[semid
].sem_perm
.cuid
= kauth_cred_getuid(cred
);
948 sema
[semid
].sem_perm
.uid
= kauth_cred_getuid(cred
);
949 sema
[semid
].sem_perm
.cgid
= cred
->cr_gid
;
950 sema
[semid
].sem_perm
.gid
= cred
->cr_gid
;
951 sema
[semid
].sem_perm
.mode
= (semflg
& 0777) | SEM_ALLOC
;
952 sema
[semid
].sem_perm
.seq
=
953 (sema
[semid
].sem_perm
.seq
+ 1) & 0x7fff;
954 sema
[semid
].sem_nsems
= nsems
;
955 sema
[semid
].sem_otime
= 0;
956 sema
[semid
].sem_ctime
= sysv_semtime();
957 sema
[semid
].sem_base
= &sem_pool
[semtot
];
959 bzero(sema
[semid
].sem_base
,
960 sizeof(sema
[semid
].sem_base
[0])*nsems
);
962 printf("sembase = 0x%x, next = 0x%x\n", sema
[semid
].sem_base
,
967 printf("didn't find it and wasn't asked to create it\n");
974 *retval
= IXSEQ_TO_IPCID(semid
, sema
[semid
].sem_perm
);
975 AUDIT_ARG(svipc_id
, *retval
);
977 printf("semget is done, returning %d\n", *retval
);
982 SYSV_SEM_SUBSYS_UNLOCK();
987 semop(struct proc
*p
, struct semop_args
*uap
, register_t
*retval
)
989 int semid
= uap
->semid
;
990 int nsops
= uap
->nsops
;
991 struct sembuf sops
[MAX_SOPS
];
992 register struct user_semid_ds
*semaptr
;
993 register struct sembuf
*sopptr
= NULL
; /* protected by 'semptr' */
994 register struct sem
*semptr
= NULL
; /* protected by 'if' */
995 struct sem_undo
*suptr
= NULL
;
997 int do_wakeup
, do_undos
;
999 AUDIT_ARG(svipc_id
, uap
->semid
);
1001 SYSV_SEM_SUBSYS_LOCK();
1004 printf("call to semop(%d, 0x%x, %d)\n", semid
, sops
, nsops
);
1007 semid
= IPCID_TO_IX(semid
); /* Convert back to zero origin */
1009 if (semid
< 0 || semid
>= seminfo
.semmni
) {
1014 semaptr
= &sema
[semid
];
1015 if ((semaptr
->sem_perm
.mode
& SEM_ALLOC
) == 0) {
1019 if (semaptr
->sem_perm
.seq
!= IPCID_TO_SEQ(uap
->semid
)) {
1024 if ((eval
= ipcperm(kauth_cred_get(), &semaptr
->sem_perm
, IPC_W
))) {
1026 printf("eval = %d from ipaccess\n", eval
);
1031 if (nsops
< 0 || nsops
> MAX_SOPS
) {
1033 printf("too many sops (max=%d, nsops=%d)\n", MAX_SOPS
, nsops
);
1039 /* OK for LP64, since sizeof(struct sembuf) is currently invariant */
1040 if ((eval
= copyin(uap
->sops
, &sops
, nsops
* sizeof(struct sembuf
))) != 0) {
1042 printf("eval = %d from copyin(%08x, %08x, %ld)\n", eval
,
1043 uap
->sops
, &sops
, nsops
* sizeof(struct sembuf
));
1049 * Loop trying to satisfy the vector of requests.
1050 * If we reach a point where we must wait, any requests already
1051 * performed are rolled back and we go to sleep until some other
1052 * process wakes us up. At this point, we start all over again.
1054 * This ensures that from the perspective of other tasks, a set
1055 * of requests is atomic (never partially satisfied).
1062 for (i
= 0; i
< nsops
; i
++) {
1065 if (sopptr
->sem_num
>= semaptr
->sem_nsems
) {
1070 semptr
= &semaptr
->sem_base
[sopptr
->sem_num
];
1073 printf("semop: semaptr=%x, sem_base=%x, semptr=%x, sem[%d]=%d : op=%d, flag=%s\n",
1074 semaptr
, semaptr
->sem_base
, semptr
,
1075 sopptr
->sem_num
, semptr
->semval
, sopptr
->sem_op
,
1076 (sopptr
->sem_flg
& IPC_NOWAIT
) ? "nowait" : "wait");
1079 if (sopptr
->sem_op
< 0) {
1080 if (semptr
->semval
+ sopptr
->sem_op
< 0) {
1082 printf("semop: can't do it now\n");
1086 semptr
->semval
+= sopptr
->sem_op
;
1087 if (semptr
->semval
== 0 &&
1088 semptr
->semzcnt
> 0)
1091 if (sopptr
->sem_flg
& SEM_UNDO
)
1093 } else if (sopptr
->sem_op
== 0) {
1094 if (semptr
->semval
> 0) {
1096 printf("semop: not zero now\n");
1101 if (semptr
->semncnt
> 0)
1103 semptr
->semval
+= sopptr
->sem_op
;
1104 if (sopptr
->sem_flg
& SEM_UNDO
)
1110 * Did we get through the entire vector?
1116 * No ... rollback anything that we've already done
1119 printf("semop: rollback 0 through %d\n", i
-1);
1121 for (j
= 0; j
< i
; j
++)
1122 semaptr
->sem_base
[sops
[j
].sem_num
].semval
-=
1126 * If the request that we couldn't satisfy has the
1127 * NOWAIT flag set then return with EAGAIN.
1129 if (sopptr
->sem_flg
& IPC_NOWAIT
) {
1134 if (sopptr
->sem_op
== 0)
1140 printf("semop: good night!\n");
1142 /* Release our lock on the semaphore subsystem so
1143 * another thread can get at the semaphore we are
1144 * waiting for. We will get the lock back after we
1147 eval
= msleep((caddr_t
)semaptr
, &sysv_sem_subsys_mutex
, (PZERO
- 4) | PCATCH
,
1151 printf("semop: good morning (eval=%d)!\n", eval
);
1158 * IMPORTANT: while we were asleep, the semaphore array might
1159 * have been reallocated somewhere else (see grow_sema_array()).
1160 * When we wake up, we have to re-lookup the semaphore
1161 * structures and re-validate them.
1164 suptr
= NULL
; /* sem_undo may have been reallocated */
1165 semaptr
= &sema
[semid
]; /* sema may have been reallocated */
1168 * Make sure that the semaphore still exists
1170 if ((semaptr
->sem_perm
.mode
& SEM_ALLOC
) == 0 ||
1171 semaptr
->sem_perm
.seq
!= IPCID_TO_SEQ(uap
->semid
) ||
1172 sopptr
->sem_num
>= semaptr
->sem_nsems
) {
1173 if (eval
== EINTR
) {
1175 * EINTR takes precedence over the fact that
1176 * the semaphore disappeared while we were
1181 * The man page says to return EIDRM.
1182 * Unfortunately, BSD doesn't define that code!
1194 * The semaphore is still alive. Readjust the count of
1195 * waiting processes. semptr needs to be recomputed
1196 * because the sem[] may have been reallocated while
1197 * we were sleeping, updating our sem_base pointer.
1199 semptr
= &semaptr
->sem_base
[sopptr
->sem_num
];
1200 if (sopptr
->sem_op
== 0)
1205 if (eval
!= 0) { /* EINTR */
1212 * Process any SEM_UNDO requests.
1215 for (i
= 0; i
< nsops
; i
++) {
1217 * We only need to deal with SEM_UNDO's for non-zero
1222 if ((sops
[i
].sem_flg
& SEM_UNDO
) == 0)
1224 adjval
= sops
[i
].sem_op
;
1227 eval
= semundo_adjust(p
, &suptr
, semid
,
1228 sops
[i
].sem_num
, -adjval
);
1233 * Oh-Oh! We ran out of either sem_undo's or undo's.
1234 * Rollback the adjustments to this point and then
1235 * rollback the semaphore ups and down so we can return
1236 * with an error with all structures restored. We
1237 * rollback the undo's in the exact reverse order that
1238 * we applied them. This guarantees that we won't run
1239 * out of space as we roll things back out.
1241 for (j
= i
- 1; j
>= 0; j
--) {
1242 if ((sops
[j
].sem_flg
& SEM_UNDO
) == 0)
1244 adjval
= sops
[j
].sem_op
;
1247 if (semundo_adjust(p
, &suptr
, semid
,
1248 sops
[j
].sem_num
, adjval
) != 0)
1249 panic("semop - can't undo undos");
1252 for (j
= 0; j
< nsops
; j
++)
1253 semaptr
->sem_base
[sops
[j
].sem_num
].semval
-=
1257 printf("eval = %d from semundo_adjust\n", eval
);
1260 } /* loop through the sops */
1261 } /* if (do_undos) */
1263 /* We're definitely done - set the sempid's */
1264 for (i
= 0; i
< nsops
; i
++) {
1266 semptr
= &semaptr
->sem_base
[sopptr
->sem_num
];
1267 semptr
->sempid
= p
->p_pid
;
1272 printf("semop: doing wakeup\n");
1274 sem_wakeup((caddr_t
)semaptr
);
1276 wakeup((caddr_t
)semaptr
);
1278 printf("semop: back from wakeup\n");
1280 wakeup((caddr_t
)semaptr
);
1284 printf("semop: done\n");
1289 SYSV_SEM_SUBSYS_UNLOCK();
1294 * Go through the undo structures for this process and apply the adjustments to
1298 semexit(struct proc
*p
)
1300 register struct sem_undo
*suptr
;
1301 register struct sem_undo
**supptr
;
1304 /* If we have not allocated our semaphores yet there can't be
1305 * anything to undo, but we need the lock to prevent
1306 * dynamic memory race conditions.
1308 SYSV_SEM_SUBSYS_LOCK();
1312 SYSV_SEM_SUBSYS_UNLOCK();
1318 * Go through the chain of undo vectors looking for one
1319 * associated with this process.
1322 for (supptr
= &semu_list
; (suptr
= *supptr
) != NULL
;
1323 supptr
= &suptr
->un_next
) {
1324 if (suptr
->un_proc
== p
)
1332 printf("proc @%08x has undo structure with %d entries\n", p
,
1337 * If there are any active undo elements then process them.
1339 if (suptr
->un_cnt
> 0) {
1340 while (suptr
->un_ent
!= NULL
) {
1341 struct undo
*sueptr
;
1345 struct user_semid_ds
*semaptr
;
1347 sueptr
= suptr
->un_ent
;
1348 semid
= sueptr
->une_id
;
1349 semnum
= sueptr
->une_num
;
1350 adjval
= sueptr
->une_adjval
;
1352 semaptr
= &sema
[semid
];
1353 if ((semaptr
->sem_perm
.mode
& SEM_ALLOC
) == 0)
1354 panic("semexit - semid not allocated");
1355 if (semnum
>= semaptr
->sem_nsems
)
1356 panic("semexit - semnum out of range");
1359 printf("semexit: %08x id=%d num=%d(adj=%d) ; sem=%d\n",
1364 semaptr
->sem_base
[semnum
].semval
);
1368 if (semaptr
->sem_base
[semnum
].semval
< -adjval
)
1369 semaptr
->sem_base
[semnum
].semval
= 0;
1371 semaptr
->sem_base
[semnum
].semval
+=
1374 semaptr
->sem_base
[semnum
].semval
+= adjval
;
1376 /* Maybe we should build a list of semaptr's to wake
1377 * up, finish all access to data structures, release the
1378 * subsystem lock, and wake all the processes. Something
1379 * to think about. It wouldn't buy us anything unless
1380 * wakeup had the potential to block, or the syscall
1381 * funnel state was changed to allow multiple threads
1382 * in the BSD code at once.
1385 sem_wakeup((caddr_t
)semaptr
);
1387 wakeup((caddr_t
)semaptr
);
1390 printf("semexit: back from wakeup\n");
1393 suptr
->un_ent
= sueptr
->une_next
;
1394 FREE(sueptr
, M_SYSVSEM
);
1400 * Deallocate the undo vector.
1403 printf("removing vector\n");
1405 suptr
->un_proc
= NULL
;
1406 *supptr
= suptr
->un_next
;
1410 * There is a semaphore leak (i.e. memory leak) in this code.
1411 * We should be deleting the IPC_PRIVATE semaphores when they are
1412 * no longer needed, and we dont. We would have to track which processes
1413 * know about which IPC_PRIVATE semaphores, updating the list after
1414 * every fork. We can't just delete them semaphore when the process
1415 * that created it dies, because that process may well have forked
1416 * some children. So we need to wait until all of it's children have
1417 * died, and so on. Maybe we should tag each IPC_PRIVATE sempahore
1418 * with the creating group ID, count the number of processes left in
1419 * that group, and delete the semaphore when the group is gone.
1420 * Until that code gets implemented we will leak IPC_PRIVATE semaphores.
1421 * There is an upper bound on the size of our semaphore array, so
1422 * leaking the semaphores should not work as a DOS attack.
1424 * Please note that the original BSD code this file is based on had the
1425 * same leaky semaphore problem.
1428 SYSV_SEM_SUBSYS_UNLOCK();
1432 /* (struct sysctl_oid *oidp, void *arg1, int arg2, \
1433 struct sysctl_req *req) */
1435 sysctl_seminfo(__unused
struct sysctl_oid
*oidp
, void *arg1
,
1436 __unused
int arg2
, struct sysctl_req
*req
)
1440 error
= SYSCTL_OUT(req
, arg1
, sizeof(int));
1441 if (error
|| req
->newptr
== USER_ADDR_NULL
)
1444 SYSV_SEM_SUBSYS_LOCK();
1446 /* Set the values only if shared memory is not initialised */
1447 if ((sem_pool
== NULL
) &&
1450 (semu_list
== NULL
)) {
1451 if ((error
= SYSCTL_IN(req
, arg1
, sizeof(int)))) {
1457 SYSV_SEM_SUBSYS_UNLOCK();
1462 /* SYSCTL_NODE(_kern, KERN_SYSV, sysv, CTLFLAG_RW, 0, "SYSV"); */
1463 extern struct sysctl_oid_list sysctl__kern_sysv_children
;
1464 SYSCTL_PROC(_kern_sysv
, KSYSV_SEMMNI
, semmni
, CTLTYPE_INT
| CTLFLAG_RW
,
1465 &limitseminfo
.semmni
, 0, &sysctl_seminfo
,"I","semmni");
1467 SYSCTL_PROC(_kern_sysv
, KSYSV_SEMMNS
, semmns
, CTLTYPE_INT
| CTLFLAG_RW
,
1468 &limitseminfo
.semmns
, 0, &sysctl_seminfo
,"I","semmns");
1470 SYSCTL_PROC(_kern_sysv
, KSYSV_SEMMNU
, semmnu
, CTLTYPE_INT
| CTLFLAG_RW
,
1471 &limitseminfo
.semmnu
, 0, &sysctl_seminfo
,"I","semmnu");
1473 SYSCTL_PROC(_kern_sysv
, KSYSV_SEMMSL
, semmsl
, CTLTYPE_INT
| CTLFLAG_RW
,
1474 &limitseminfo
.semmsl
, 0, &sysctl_seminfo
,"I","semmsl");
1476 SYSCTL_PROC(_kern_sysv
, KSYSV_SEMUNE
, semume
, CTLTYPE_INT
| CTLFLAG_RW
,
1477 &limitseminfo
.semume
, 0, &sysctl_seminfo
,"I","semume");
1481 IPCS_sem_sysctl(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
,
1482 __unused
int arg2
, struct sysctl_req
*req
)
1487 struct IPCS_command u32
;
1488 struct user_IPCS_command u64
;
1490 struct semid_ds semid_ds32
; /* post conversion, 32 bit version */
1492 size_t ipcs_sz
= sizeof(struct user_IPCS_command
);
1493 size_t semid_ds_sz
= sizeof(struct user_semid_ds
);
1494 struct proc
*p
= current_proc();
1496 /* Copy in the command structure */
1497 if ((error
= SYSCTL_IN(req
, &ipcs
, ipcs_sz
)) != 0) {
1501 if (!IS_64BIT_PROCESS(p
)) {
1502 ipcs_sz
= sizeof(struct IPCS_command
);
1503 semid_ds_sz
= sizeof(struct semid_ds
);
1506 /* Let us version this interface... */
1507 if (ipcs
.u64
.ipcs_magic
!= IPCS_MAGIC
) {
1511 SYSV_SEM_SUBSYS_LOCK();
1512 switch(ipcs
.u64
.ipcs_op
) {
1513 case IPCS_SEM_CONF
: /* Obtain global configuration data */
1514 if (ipcs
.u64
.ipcs_datalen
!= sizeof(struct seminfo
)) {
1518 if (ipcs
.u64
.ipcs_cursor
!= 0) { /* fwd. compat. */
1522 error
= copyout(&seminfo
, ipcs
.u64
.ipcs_data
, ipcs
.u64
.ipcs_datalen
);
1525 case IPCS_SEM_ITER
: /* Iterate over existing segments */
1526 cursor
= ipcs
.u64
.ipcs_cursor
;
1527 if (cursor
< 0 || cursor
>= seminfo
.semmni
) {
1531 if (ipcs
.u64
.ipcs_datalen
!= (int)semid_ds_sz
) {
1535 for( ; cursor
< seminfo
.semmni
; cursor
++) {
1536 if (sema
[cursor
].sem_perm
.mode
& SEM_ALLOC
)
1540 if (cursor
== seminfo
.semmni
) {
1545 semid_dsp
= &sema
[cursor
]; /* default: 64 bit */
1548 * If necessary, convert the 64 bit kernel segment
1549 * descriptor to a 32 bit user one.
1551 if (!IS_64BIT_PROCESS(p
)) {
1552 semid_ds_64to32(semid_dsp
, &semid_ds32
);
1553 semid_dsp
= &semid_ds32
;
1555 error
= copyout(semid_dsp
, ipcs
.u64
.ipcs_data
, ipcs
.u64
.ipcs_datalen
);
1558 ipcs
.u64
.ipcs_cursor
= cursor
+ 1;
1559 error
= SYSCTL_OUT(req
, &ipcs
, ipcs_sz
);
1567 SYSV_SEM_SUBSYS_UNLOCK();
1571 SYSCTL_DECL(_kern_sysv_ipcs
);
1572 SYSCTL_PROC(_kern_sysv_ipcs
, OID_AUTO
, sem
, CTLFLAG_RW
|CTLFLAG_ANYBODY
,
1573 0, 0, IPCS_sem_sysctl
,
1574 "S,IPCS_sem_command",
1575 "ipcs sem command interface");