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
,
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();
126 sysv_sem_subsys_lck_grp
= lck_grp_alloc_init("sysv_sem_subsys_lock", sysv_sem_subsys_lck_grp_attr
);
128 sysv_sem_subsys_lck_attr
= lck_attr_alloc_init();
129 lck_mtx_init(&sysv_sem_subsys_mutex
, sysv_sem_subsys_lck_grp
, sysv_sem_subsys_lck_attr
);
132 static __inline__ user_time_t
141 * XXX conversion of internal user_time_t to external tume_t loses
142 * XXX precision; not an issue for us now, since we are only ever
143 * XXX setting 32 bits worth of time into it.
145 * pad field contents are not moved correspondingly; contents will be lost
147 * NOTE: Source and target may *NOT* overlap! (target is smaller)
150 semid_ds_64to32(struct user_semid_ds
*in
, struct semid_ds
*out
)
152 out
->sem_perm
= in
->sem_perm
;
153 out
->sem_base
= (__int32_t
)in
->sem_base
;
154 out
->sem_nsems
= in
->sem_nsems
;
155 out
->sem_otime
= in
->sem_otime
; /* XXX loses precision */
156 out
->sem_ctime
= in
->sem_ctime
; /* XXX loses precision */
160 * pad field contents are not moved correspondingly; contents will be lost
162 * NOTE: Source and target may are permitted to overlap! (source is smaller);
163 * this works because we copy fields in order from the end of the struct to
166 * XXX use CAST_USER_ADDR_T() for lack of a CAST_USER_TIME_T(); net effect
170 semid_ds_32to64(struct semid_ds
*in
, struct user_semid_ds
*out
)
172 out
->sem_ctime
= in
->sem_ctime
;
173 out
->sem_otime
= in
->sem_otime
;
174 out
->sem_nsems
= in
->sem_nsems
;
175 out
->sem_base
= (void *)in
->sem_base
;
176 out
->sem_perm
= in
->sem_perm
;
181 * Entry point for all SEM calls
183 * In Darwin this is no longer the entry point. It will be removed after
184 * the code has been tested better.
186 /* XXX actually varargs. */
188 semsys(struct proc
*p
, struct semsys_args
*uap
, register_t
*retval
)
191 /* The individual calls handling the locking now */
193 if (uap
->which
>= sizeof(semcalls
)/sizeof(semcalls
[0]))
195 return ((*semcalls
[uap
->which
])(p
, &uap
->a2
, retval
));
199 * Expand the semu array to the given capacity. If the expansion fails
200 * return 0, otherwise return 1.
202 * Assumes we already have the subsystem lock.
205 grow_semu_array(int newSize
)
208 register struct sem_undo
*newSemu
;
210 if (newSize
<= seminfo
.semmnu
)
212 if (newSize
> limitseminfo
.semmnu
) /* enforce hard limit */
215 printf("undo structure hard limit of %d reached, requested %d\n",
216 limitseminfo
.semmnu
, newSize
);
220 newSize
= (newSize
/SEMMNU_INC
+ 1) * SEMMNU_INC
;
221 newSize
= newSize
> limitseminfo
.semmnu
? limitseminfo
.semmnu
: newSize
;
224 printf("growing semu[] from %d to %d\n", seminfo
.semmnu
, newSize
);
226 MALLOC(newSemu
, struct sem_undo
*, sizeof (struct sem_undo
) * newSize
,
227 M_SYSVSEM
, M_WAITOK
| M_ZERO
);
231 printf("allocation failed. no changes made.\n");
236 /* copy the old data to the new array */
237 for (i
= 0; i
< seminfo
.semmnu
; i
++)
239 newSemu
[i
] = semu
[i
];
242 * The new elements (from newSemu[i] to newSemu[newSize-1]) have their
243 * "un_proc" set to 0 (i.e. NULL) by the M_ZERO flag to MALLOC() above,
244 * so they're already marked as "not in use".
247 /* Clean up the old array */
249 FREE(semu
, M_SYSVSEM
);
252 seminfo
.semmnu
= newSize
;
254 printf("expansion successful\n");
260 * Expand the sema array to the given capacity. If the expansion fails
261 * we return 0, otherwise we return 1.
263 * Assumes we already have the subsystem lock.
266 grow_sema_array(int newSize
)
268 register struct user_semid_ds
*newSema
;
271 if (newSize
<= seminfo
.semmni
)
273 if (newSize
> limitseminfo
.semmni
) /* enforce hard limit */
276 printf("identifier hard limit of %d reached, requested %d\n",
277 limitseminfo
.semmni
, newSize
);
281 newSize
= (newSize
/SEMMNI_INC
+ 1) * SEMMNI_INC
;
282 newSize
= newSize
> limitseminfo
.semmni
? limitseminfo
.semmni
: newSize
;
285 printf("growing sema[] from %d to %d\n", seminfo
.semmni
, newSize
);
287 MALLOC(newSema
, struct user_semid_ds
*,
288 sizeof (struct user_semid_ds
) * newSize
,
289 M_SYSVSEM
, M_WAITOK
| M_ZERO
);
293 printf("allocation failed. no changes made.\n");
298 /* copy over the old ids */
299 for (i
= 0; i
< seminfo
.semmni
; i
++)
301 newSema
[i
] = sema
[i
];
302 /* This is a hack. What we really want to be able to
303 * do is change the value a process is waiting on
304 * without waking it up, but I don't know how to do
305 * this with the existing code, so we wake up the
306 * process and let it do a lot of work to determine the
307 * semaphore set is really not available yet, and then
308 * sleep on the correct, reallocated user_semid_ds pointer.
310 if (sema
[i
].sem_perm
.mode
& SEM_ALLOC
)
311 wakeup((caddr_t
)&sema
[i
]);
314 * The new elements (from newSema[i] to newSema[newSize-1]) have their
315 * "sem_base" and "sem_perm.mode" set to 0 (i.e. NULL) by the M_ZERO
316 * flag to MALLOC() above, so they're already marked as "not in use".
319 /* Clean up the old array */
321 FREE(sema
, M_SYSVSEM
);
324 seminfo
.semmni
= newSize
;
326 printf("expansion successful\n");
332 * Expand the sem_pool array to the given capacity. If the expansion fails
333 * we return 0 (fail), otherwise we return 1 (success).
335 * Assumes we already hold the subsystem lock.
338 grow_sem_pool(int new_pool_size
)
340 struct sem
*new_sem_pool
= NULL
;
341 struct sem
*sem_free
;
344 if (new_pool_size
< semtot
)
346 /* enforce hard limit */
347 if (new_pool_size
> limitseminfo
.semmns
) {
349 printf("semaphore hard limit of %d reached, requested %d\n",
350 limitseminfo
.semmns
, new_pool_size
);
355 new_pool_size
= (new_pool_size
/SEMMNS_INC
+ 1) * SEMMNS_INC
;
356 new_pool_size
= new_pool_size
> limitseminfo
.semmns
? limitseminfo
.semmns
: new_pool_size
;
359 printf("growing sem_pool array from %d to %d\n", seminfo
.semmns
, new_pool_size
);
361 MALLOC(new_sem_pool
, struct sem
*, sizeof (struct sem
) * new_pool_size
,
362 M_SYSVSEM
, M_WAITOK
| M_ZERO
);
363 if (NULL
== new_sem_pool
) {
365 printf("allocation failed. no changes made.\n");
370 /* We have our new memory, now copy the old contents over */
372 for(i
= 0; i
< seminfo
.semmns
; i
++)
373 new_sem_pool
[i
] = sem_pool
[i
];
375 /* Update our id structures to point to the new semaphores */
376 for(i
= 0; i
< seminfo
.semmni
; i
++) {
377 if (sema
[i
].sem_perm
.mode
& SEM_ALLOC
) /* ID in use */
378 sema
[i
].sem_base
+= (new_sem_pool
- sem_pool
);
382 sem_pool
= new_sem_pool
;
384 /* clean up the old array */
385 if (sem_free
!= NULL
)
386 FREE(sem_free
, M_SYSVSEM
);
388 seminfo
.semmns
= new_pool_size
;
390 printf("expansion complete\n");
396 * Allocate a new sem_undo structure for a process
397 * (returns ptr to structure or NULL if no more room)
399 * Assumes we already hold the subsystem lock.
402 static struct sem_undo
*
403 semu_alloc(struct proc
*p
)
406 register struct sem_undo
*suptr
;
407 register struct sem_undo
**supptr
;
411 * Try twice to allocate something.
412 * (we'll purge any empty structures after the first pass so
413 * two passes are always enough)
416 for (attempt
= 0; attempt
< 2; attempt
++) {
418 * Look for a free structure.
419 * Fill it in and return it if we find one.
422 for (i
= 0; i
< seminfo
.semmnu
; i
++) {
424 if (suptr
->un_proc
== NULL
) {
425 suptr
->un_next
= semu_list
;
428 suptr
->un_ent
= NULL
;
435 * We didn't find a free one, if this is the first attempt
436 * then try to free some structures.
440 /* All the structures are in use - try to free some */
441 int did_something
= 0;
444 while ((suptr
= *supptr
) != NULL
) {
445 if (suptr
->un_cnt
== 0) {
446 suptr
->un_proc
= NULL
;
447 *supptr
= suptr
->un_next
;
450 supptr
= &(suptr
->un_next
);
453 /* If we didn't free anything. Try expanding
454 * the semu[] array. If that doesn't work
455 * then fail. We expand last to get the
456 * most reuse out of existing resources.
459 if (!grow_semu_array(seminfo
.semmnu
+ 1))
463 * The second pass failed even though we freed
464 * something after the first pass!
465 * This is IMPOSSIBLE!
467 panic("semu_alloc - second attempt failed");
474 * Adjust a particular entry for a particular proc
476 * Assumes we already hold the subsystem lock.
479 semundo_adjust(struct proc
*p
, struct sem_undo
**supptr
, int semid
,
480 int semnum
, int adjval
)
482 register struct sem_undo
*suptr
;
483 register struct undo
*sueptr
, **suepptr
, *new_sueptr
;
487 * Look for and remember the sem_undo if the caller doesn't provide it
492 for (suptr
= semu_list
; suptr
!= NULL
;
493 suptr
= suptr
->un_next
) {
494 if (suptr
->un_proc
== p
) {
502 suptr
= semu_alloc(p
);
510 * Look for the requested entry and adjust it (delete if adjval becomes
514 for (i
= 0, suepptr
= &suptr
->un_ent
, sueptr
= suptr
->un_ent
;
516 i
++, suepptr
= &sueptr
->une_next
, sueptr
= sueptr
->une_next
) {
517 if (sueptr
->une_id
!= semid
|| sueptr
->une_num
!= semnum
)
520 sueptr
->une_adjval
= 0;
522 sueptr
->une_adjval
+= adjval
;
523 if (sueptr
->une_adjval
== 0) {
525 *suepptr
= sueptr
->une_next
;
526 FREE(sueptr
, M_SYSVSEM
);
532 /* Didn't find the right entry - create it */
534 /* no adjustment: no need for a new entry */
538 if (suptr
->un_cnt
== limitseminfo
.semume
) {
539 /* reached the limit number of semaphore undo entries */
543 /* allocate a new semaphore undo entry */
544 MALLOC(new_sueptr
, struct undo
*, sizeof (struct undo
),
545 M_SYSVSEM
, M_WAITOK
);
546 if (new_sueptr
== NULL
) {
550 /* fill in the new semaphore undo entry */
551 new_sueptr
->une_next
= suptr
->un_ent
;
552 suptr
->un_ent
= new_sueptr
;
554 new_sueptr
->une_adjval
= adjval
;
555 new_sueptr
->une_id
= semid
;
556 new_sueptr
->une_num
= semnum
;
561 /* Assumes we already hold the subsystem lock.
564 semundo_clear(int semid
, int semnum
)
566 struct sem_undo
*suptr
;
568 for (suptr
= semu_list
; suptr
!= NULL
; suptr
= suptr
->un_next
) {
570 struct undo
**suepptr
;
573 sueptr
= suptr
->un_ent
;
574 suepptr
= &suptr
->un_ent
;
575 while (i
< suptr
->un_cnt
) {
576 if (sueptr
->une_id
== semid
) {
577 if (semnum
== -1 || sueptr
->une_num
== semnum
) {
579 *suepptr
= sueptr
->une_next
;
580 FREE(sueptr
, M_SYSVSEM
);
588 suepptr
= &sueptr
->une_next
;
589 sueptr
= sueptr
->une_next
;
595 * Note that the user-mode half of this passes a union coerced to a
596 * user_addr_t. The union contains either an int or a pointer, and
597 * so we have to coerce it back, variant on whether the calling
598 * process is 64 bit or not. The coercion works for the 'val' element
599 * because the alignment is the same in user and kernel space.
602 semctl(struct proc
*p
, struct semctl_args
*uap
, register_t
*retval
)
604 int semid
= uap
->semid
;
605 int semnum
= uap
->semnum
;
607 user_semun_t user_arg
= (user_semun_t
)uap
->arg
;
608 kauth_cred_t cred
= kauth_cred_get();
610 struct user_semid_ds sbuf
;
611 struct user_semid_ds
*semaptr
;
612 struct user_semid_ds uds
;
615 AUDIT_ARG(svipc_cmd
, cmd
);
616 AUDIT_ARG(svipc_id
, semid
);
618 SYSV_SEM_SUBSYS_LOCK();
621 printf("call to semctl(%d, %d, %d, 0x%qx)\n", semid
, semnum
, cmd
, user_arg
);
624 semid
= IPCID_TO_IX(semid
);
626 if (semid
< 0 || semid
>= seminfo
.semmni
) {
628 printf("Invalid semid\n");
634 semaptr
= &sema
[semid
];
635 if ((semaptr
->sem_perm
.mode
& SEM_ALLOC
) == 0 ||
636 semaptr
->sem_perm
.seq
!= IPCID_TO_SEQ(uap
->semid
)) {
646 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_M
)))
649 semaptr
->sem_perm
.cuid
= kauth_cred_getuid(cred
);
650 semaptr
->sem_perm
.uid
= kauth_cred_getuid(cred
);
651 semtot
-= semaptr
->sem_nsems
;
652 for (i
= semaptr
->sem_base
- sem_pool
; i
< semtot
; i
++)
653 sem_pool
[i
] = sem_pool
[i
+ semaptr
->sem_nsems
];
654 for (i
= 0; i
< seminfo
.semmni
; i
++) {
655 if ((sema
[i
].sem_perm
.mode
& SEM_ALLOC
) &&
656 sema
[i
].sem_base
> semaptr
->sem_base
)
657 sema
[i
].sem_base
-= semaptr
->sem_nsems
;
659 semaptr
->sem_perm
.mode
= 0;
660 semundo_clear(semid
, -1);
661 wakeup((caddr_t
)semaptr
);
665 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_M
)))
668 if (IS_64BIT_PROCESS(p
)) {
669 eval
= copyin(user_arg
.buf
, &sbuf
, sizeof(struct user_semid_ds
));
671 eval
= copyin(user_arg
.buf
, &sbuf
, sizeof(struct semid_ds
));
672 /* convert in place; ugly, but safe */
673 semid_ds_32to64((struct semid_ds
*)&sbuf
, &sbuf
);
680 semaptr
->sem_perm
.uid
= sbuf
.sem_perm
.uid
;
681 semaptr
->sem_perm
.gid
= sbuf
.sem_perm
.gid
;
682 semaptr
->sem_perm
.mode
= (semaptr
->sem_perm
.mode
& ~0777) |
683 (sbuf
.sem_perm
.mode
& 0777);
684 semaptr
->sem_ctime
= sysv_semtime();
688 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
690 bcopy(semaptr
, &uds
, sizeof(struct user_semid_ds
));
691 if (IS_64BIT_PROCESS(p
)) {
692 eval
= copyout(&uds
, user_arg
.buf
, sizeof(struct user_semid_ds
));
694 struct semid_ds semid_ds32
;
695 semid_ds_64to32(&uds
, &semid_ds32
);
696 eval
= copyout(&semid_ds32
, user_arg
.buf
, sizeof(struct semid_ds
));
701 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
703 if (semnum
< 0 || semnum
>= semaptr
->sem_nsems
) {
707 rval
= semaptr
->sem_base
[semnum
].semncnt
;
711 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
713 if (semnum
< 0 || semnum
>= semaptr
->sem_nsems
) {
717 rval
= semaptr
->sem_base
[semnum
].sempid
;
721 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
723 if (semnum
< 0 || semnum
>= semaptr
->sem_nsems
) {
727 rval
= semaptr
->sem_base
[semnum
].semval
;
731 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
733 /* XXXXXXXXXXXXXXXX TBD XXXXXXXXXXXXXXXX */
734 for (i
= 0; i
< semaptr
->sem_nsems
; i
++) {
735 /* XXX could be done in one go... */
736 eval
= copyout((caddr_t
)&semaptr
->sem_base
[i
].semval
,
737 user_arg
.array
+ (i
* sizeof(unsigned short)),
738 sizeof(unsigned short));
745 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
747 if (semnum
< 0 || semnum
>= semaptr
->sem_nsems
) {
751 rval
= semaptr
->sem_base
[semnum
].semzcnt
;
755 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_W
)))
758 printf("Invalid credentials for write\n");
762 if (semnum
< 0 || semnum
>= semaptr
->sem_nsems
)
765 printf("Invalid number out of range for set\n");
771 * Cast down a pointer instead of using 'val' member directly
772 * to avoid introducing endieness and a pad field into the
773 * header file. Ugly, but it works.
775 semaptr
->sem_base
[semnum
].semval
= CAST_DOWN(int,user_arg
.buf
);
776 semundo_clear(semid
, semnum
);
777 wakeup((caddr_t
)semaptr
);
781 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_W
)))
783 /*** XXXXXXXXXXXX TBD ********/
784 for (i
= 0; i
< semaptr
->sem_nsems
; i
++) {
785 /* XXX could be done in one go... */
786 eval
= copyin(user_arg
.array
+ (i
* sizeof(unsigned short)),
787 (caddr_t
)&semaptr
->sem_base
[i
].semval
,
788 sizeof(unsigned short));
792 semundo_clear(semid
, -1);
793 wakeup((caddr_t
)semaptr
);
804 SYSV_SEM_SUBSYS_UNLOCK();
809 semget(__unused
struct proc
*p
, struct semget_args
*uap
, register_t
*retval
)
813 int nsems
= uap
->nsems
;
814 int semflg
= uap
->semflg
;
815 kauth_cred_t cred
= kauth_cred_get();
818 if (key
!= IPC_PRIVATE
)
819 printf("semget(0x%x, %d, 0%o)\n", key
, nsems
, semflg
);
821 printf("semget(IPC_PRIVATE, %d, 0%o)\n", nsems
, semflg
);
825 SYSV_SEM_SUBSYS_LOCK();
828 if (key
!= IPC_PRIVATE
) {
829 for (semid
= 0; semid
< seminfo
.semmni
; semid
++) {
830 if ((sema
[semid
].sem_perm
.mode
& SEM_ALLOC
) &&
831 sema
[semid
].sem_perm
.key
== key
)
834 if (semid
< seminfo
.semmni
) {
836 printf("found public key\n");
838 if ((eval
= ipcperm(cred
, &sema
[semid
].sem_perm
,
841 if (nsems
< 0 || sema
[semid
].sem_nsems
< nsems
) {
843 printf("too small\n");
848 if ((semflg
& IPC_CREAT
) && (semflg
& IPC_EXCL
)) {
850 printf("not exclusive\n");
860 printf("need to allocate an id for the request\n");
862 if (key
== IPC_PRIVATE
|| (semflg
& IPC_CREAT
)) {
863 if (nsems
<= 0 || nsems
> limitseminfo
.semmsl
) {
865 printf("nsems out of range (0<%d<=%d)\n", nsems
,
871 if (nsems
> seminfo
.semmns
- semtot
) {
873 printf("not enough semaphores left (need %d, got %d)\n",
874 nsems
, seminfo
.semmns
- semtot
);
876 if (!grow_sem_pool(semtot
+ nsems
)) {
878 printf("failed to grow the sem array\n");
884 for (semid
= 0; semid
< seminfo
.semmni
; semid
++) {
885 if ((sema
[semid
].sem_perm
.mode
& SEM_ALLOC
) == 0)
888 if (semid
== seminfo
.semmni
) {
890 printf("no more id's available\n");
892 if (!grow_sema_array(seminfo
.semmni
+ 1))
895 printf("failed to grow sema array\n");
902 printf("semid %d is available\n", semid
);
904 sema
[semid
].sem_perm
.key
= key
;
905 sema
[semid
].sem_perm
.cuid
= kauth_cred_getuid(cred
);
906 sema
[semid
].sem_perm
.uid
= kauth_cred_getuid(cred
);
907 sema
[semid
].sem_perm
.cgid
= cred
->cr_gid
;
908 sema
[semid
].sem_perm
.gid
= cred
->cr_gid
;
909 sema
[semid
].sem_perm
.mode
= (semflg
& 0777) | SEM_ALLOC
;
910 sema
[semid
].sem_perm
.seq
=
911 (sema
[semid
].sem_perm
.seq
+ 1) & 0x7fff;
912 sema
[semid
].sem_nsems
= nsems
;
913 sema
[semid
].sem_otime
= 0;
914 sema
[semid
].sem_ctime
= sysv_semtime();
915 sema
[semid
].sem_base
= &sem_pool
[semtot
];
917 bzero(sema
[semid
].sem_base
,
918 sizeof(sema
[semid
].sem_base
[0])*nsems
);
920 printf("sembase = 0x%x, next = 0x%x\n", sema
[semid
].sem_base
,
925 printf("didn't find it and wasn't asked to create it\n");
932 *retval
= IXSEQ_TO_IPCID(semid
, sema
[semid
].sem_perm
);
933 AUDIT_ARG(svipc_id
, *retval
);
935 printf("semget is done, returning %d\n", *retval
);
940 SYSV_SEM_SUBSYS_UNLOCK();
945 semop(struct proc
*p
, struct semop_args
*uap
, register_t
*retval
)
947 int semid
= uap
->semid
;
948 int nsops
= uap
->nsops
;
949 struct sembuf sops
[MAX_SOPS
];
950 register struct user_semid_ds
*semaptr
;
951 register struct sembuf
*sopptr
= NULL
; /* protected by 'semptr' */
952 register struct sem
*semptr
= NULL
; /* protected by 'if' */
953 struct sem_undo
*suptr
= NULL
;
955 int do_wakeup
, do_undos
;
957 AUDIT_ARG(svipc_id
, uap
->semid
);
959 SYSV_SEM_SUBSYS_LOCK();
962 printf("call to semop(%d, 0x%x, %d)\n", semid
, sops
, nsops
);
965 semid
= IPCID_TO_IX(semid
); /* Convert back to zero origin */
967 if (semid
< 0 || semid
>= seminfo
.semmni
) {
972 semaptr
= &sema
[semid
];
973 if ((semaptr
->sem_perm
.mode
& SEM_ALLOC
) == 0) {
977 if (semaptr
->sem_perm
.seq
!= IPCID_TO_SEQ(uap
->semid
)) {
982 if ((eval
= ipcperm(kauth_cred_get(), &semaptr
->sem_perm
, IPC_W
))) {
984 printf("eval = %d from ipaccess\n", eval
);
989 if (nsops
< 0 || nsops
> MAX_SOPS
) {
991 printf("too many sops (max=%d, nsops=%d)\n", MAX_SOPS
, nsops
);
997 /* OK for LP64, since sizeof(struct sembuf) is currently invariant */
998 if ((eval
= copyin(uap
->sops
, &sops
, nsops
* sizeof(struct sembuf
))) != 0) {
1000 printf("eval = %d from copyin(%08x, %08x, %ld)\n", eval
,
1001 uap
->sops
, &sops
, nsops
* sizeof(struct sembuf
));
1007 * Loop trying to satisfy the vector of requests.
1008 * If we reach a point where we must wait, any requests already
1009 * performed are rolled back and we go to sleep until some other
1010 * process wakes us up. At this point, we start all over again.
1012 * This ensures that from the perspective of other tasks, a set
1013 * of requests is atomic (never partially satisfied).
1020 for (i
= 0; i
< nsops
; i
++) {
1023 if (sopptr
->sem_num
>= semaptr
->sem_nsems
) {
1028 semptr
= &semaptr
->sem_base
[sopptr
->sem_num
];
1031 printf("semop: semaptr=%x, sem_base=%x, semptr=%x, sem[%d]=%d : op=%d, flag=%s\n",
1032 semaptr
, semaptr
->sem_base
, semptr
,
1033 sopptr
->sem_num
, semptr
->semval
, sopptr
->sem_op
,
1034 (sopptr
->sem_flg
& IPC_NOWAIT
) ? "nowait" : "wait");
1037 if (sopptr
->sem_op
< 0) {
1038 if (semptr
->semval
+ sopptr
->sem_op
< 0) {
1040 printf("semop: can't do it now\n");
1044 semptr
->semval
+= sopptr
->sem_op
;
1045 if (semptr
->semval
== 0 &&
1046 semptr
->semzcnt
> 0)
1049 if (sopptr
->sem_flg
& SEM_UNDO
)
1051 } else if (sopptr
->sem_op
== 0) {
1052 if (semptr
->semval
> 0) {
1054 printf("semop: not zero now\n");
1059 if (semptr
->semncnt
> 0)
1061 semptr
->semval
+= sopptr
->sem_op
;
1062 if (sopptr
->sem_flg
& SEM_UNDO
)
1068 * Did we get through the entire vector?
1074 * No ... rollback anything that we've already done
1077 printf("semop: rollback 0 through %d\n", i
-1);
1079 for (j
= 0; j
< i
; j
++)
1080 semaptr
->sem_base
[sops
[j
].sem_num
].semval
-=
1084 * If the request that we couldn't satisfy has the
1085 * NOWAIT flag set then return with EAGAIN.
1087 if (sopptr
->sem_flg
& IPC_NOWAIT
) {
1092 if (sopptr
->sem_op
== 0)
1098 printf("semop: good night!\n");
1100 /* Release our lock on the semaphore subsystem so
1101 * another thread can get at the semaphore we are
1102 * waiting for. We will get the lock back after we
1105 eval
= msleep((caddr_t
)semaptr
, &sysv_sem_subsys_mutex
, (PZERO
- 4) | PCATCH
,
1109 printf("semop: good morning (eval=%d)!\n", eval
);
1116 * IMPORTANT: while we were asleep, the semaphore array might
1117 * have been reallocated somewhere else (see grow_sema_array()).
1118 * When we wake up, we have to re-lookup the semaphore
1119 * structures and re-validate them.
1122 suptr
= NULL
; /* sem_undo may have been reallocated */
1123 semaptr
= &sema
[semid
]; /* sema may have been reallocated */
1126 * Make sure that the semaphore still exists
1128 if ((semaptr
->sem_perm
.mode
& SEM_ALLOC
) == 0 ||
1129 semaptr
->sem_perm
.seq
!= IPCID_TO_SEQ(uap
->semid
) ||
1130 sopptr
->sem_num
>= semaptr
->sem_nsems
) {
1131 if (eval
== EINTR
) {
1133 * EINTR takes precedence over the fact that
1134 * the semaphore disappeared while we were
1139 * The man page says to return EIDRM.
1140 * Unfortunately, BSD doesn't define that code!
1152 * The semaphore is still alive. Readjust the count of
1153 * waiting processes. semptr needs to be recomputed
1154 * because the sem[] may have been reallocated while
1155 * we were sleeping, updating our sem_base pointer.
1157 semptr
= &semaptr
->sem_base
[sopptr
->sem_num
];
1158 if (sopptr
->sem_op
== 0)
1163 if (eval
!= 0) { /* EINTR */
1170 * Process any SEM_UNDO requests.
1173 for (i
= 0; i
< nsops
; i
++) {
1175 * We only need to deal with SEM_UNDO's for non-zero
1180 if ((sops
[i
].sem_flg
& SEM_UNDO
) == 0)
1182 adjval
= sops
[i
].sem_op
;
1185 eval
= semundo_adjust(p
, &suptr
, semid
,
1186 sops
[i
].sem_num
, -adjval
);
1191 * Oh-Oh! We ran out of either sem_undo's or undo's.
1192 * Rollback the adjustments to this point and then
1193 * rollback the semaphore ups and down so we can return
1194 * with an error with all structures restored. We
1195 * rollback the undo's in the exact reverse order that
1196 * we applied them. This guarantees that we won't run
1197 * out of space as we roll things back out.
1199 for (j
= i
- 1; j
>= 0; j
--) {
1200 if ((sops
[j
].sem_flg
& SEM_UNDO
) == 0)
1202 adjval
= sops
[j
].sem_op
;
1205 if (semundo_adjust(p
, &suptr
, semid
,
1206 sops
[j
].sem_num
, adjval
) != 0)
1207 panic("semop - can't undo undos");
1210 for (j
= 0; j
< nsops
; j
++)
1211 semaptr
->sem_base
[sops
[j
].sem_num
].semval
-=
1215 printf("eval = %d from semundo_adjust\n", eval
);
1218 } /* loop through the sops */
1219 } /* if (do_undos) */
1221 /* We're definitely done - set the sempid's */
1222 for (i
= 0; i
< nsops
; i
++) {
1224 semptr
= &semaptr
->sem_base
[sopptr
->sem_num
];
1225 semptr
->sempid
= p
->p_pid
;
1230 printf("semop: doing wakeup\n");
1232 sem_wakeup((caddr_t
)semaptr
);
1234 wakeup((caddr_t
)semaptr
);
1236 printf("semop: back from wakeup\n");
1238 wakeup((caddr_t
)semaptr
);
1242 printf("semop: done\n");
1247 SYSV_SEM_SUBSYS_UNLOCK();
1252 * Go through the undo structures for this process and apply the adjustments to
1256 semexit(struct proc
*p
)
1258 register struct sem_undo
*suptr
;
1259 register struct sem_undo
**supptr
;
1262 /* If we have not allocated our semaphores yet there can't be
1263 * anything to undo, but we need the lock to prevent
1264 * dynamic memory race conditions.
1266 SYSV_SEM_SUBSYS_LOCK();
1270 SYSV_SEM_SUBSYS_UNLOCK();
1276 * Go through the chain of undo vectors looking for one
1277 * associated with this process.
1280 for (supptr
= &semu_list
; (suptr
= *supptr
) != NULL
;
1281 supptr
= &suptr
->un_next
) {
1282 if (suptr
->un_proc
== p
)
1290 printf("proc @%08x has undo structure with %d entries\n", p
,
1295 * If there are any active undo elements then process them.
1297 if (suptr
->un_cnt
> 0) {
1298 while (suptr
->un_ent
!= NULL
) {
1299 struct undo
*sueptr
;
1303 struct user_semid_ds
*semaptr
;
1305 sueptr
= suptr
->un_ent
;
1306 semid
= sueptr
->une_id
;
1307 semnum
= sueptr
->une_num
;
1308 adjval
= sueptr
->une_adjval
;
1310 semaptr
= &sema
[semid
];
1311 if ((semaptr
->sem_perm
.mode
& SEM_ALLOC
) == 0)
1312 panic("semexit - semid not allocated");
1313 if (semnum
>= semaptr
->sem_nsems
)
1314 panic("semexit - semnum out of range");
1317 printf("semexit: %08x id=%d num=%d(adj=%d) ; sem=%d\n",
1322 semaptr
->sem_base
[semnum
].semval
);
1326 if (semaptr
->sem_base
[semnum
].semval
< -adjval
)
1327 semaptr
->sem_base
[semnum
].semval
= 0;
1329 semaptr
->sem_base
[semnum
].semval
+=
1332 semaptr
->sem_base
[semnum
].semval
+= adjval
;
1334 /* Maybe we should build a list of semaptr's to wake
1335 * up, finish all access to data structures, release the
1336 * subsystem lock, and wake all the processes. Something
1337 * to think about. It wouldn't buy us anything unless
1338 * wakeup had the potential to block, or the syscall
1339 * funnel state was changed to allow multiple threads
1340 * in the BSD code at once.
1343 sem_wakeup((caddr_t
)semaptr
);
1345 wakeup((caddr_t
)semaptr
);
1348 printf("semexit: back from wakeup\n");
1351 suptr
->un_ent
= sueptr
->une_next
;
1352 FREE(sueptr
, M_SYSVSEM
);
1358 * Deallocate the undo vector.
1361 printf("removing vector\n");
1363 suptr
->un_proc
= NULL
;
1364 *supptr
= suptr
->un_next
;
1368 * There is a semaphore leak (i.e. memory leak) in this code.
1369 * We should be deleting the IPC_PRIVATE semaphores when they are
1370 * no longer needed, and we dont. We would have to track which processes
1371 * know about which IPC_PRIVATE semaphores, updating the list after
1372 * every fork. We can't just delete them semaphore when the process
1373 * that created it dies, because that process may well have forked
1374 * some children. So we need to wait until all of it's children have
1375 * died, and so on. Maybe we should tag each IPC_PRIVATE sempahore
1376 * with the creating group ID, count the number of processes left in
1377 * that group, and delete the semaphore when the group is gone.
1378 * Until that code gets implemented we will leak IPC_PRIVATE semaphores.
1379 * There is an upper bound on the size of our semaphore array, so
1380 * leaking the semaphores should not work as a DOS attack.
1382 * Please note that the original BSD code this file is based on had the
1383 * same leaky semaphore problem.
1386 SYSV_SEM_SUBSYS_UNLOCK();
1390 /* (struct sysctl_oid *oidp, void *arg1, int arg2, \
1391 struct sysctl_req *req) */
1393 sysctl_seminfo(__unused
struct sysctl_oid
*oidp
, void *arg1
,
1394 __unused
int arg2
, struct sysctl_req
*req
)
1398 error
= SYSCTL_OUT(req
, arg1
, sizeof(int));
1399 if (error
|| req
->newptr
== USER_ADDR_NULL
)
1402 SYSV_SEM_SUBSYS_LOCK();
1404 /* Set the values only if shared memory is not initialised */
1405 if ((sem_pool
== NULL
) &&
1408 (semu_list
== NULL
)) {
1409 if ((error
= SYSCTL_IN(req
, arg1
, sizeof(int)))) {
1415 SYSV_SEM_SUBSYS_UNLOCK();
1420 /* SYSCTL_NODE(_kern, KERN_SYSV, sysv, CTLFLAG_RW, 0, "SYSV"); */
1421 extern struct sysctl_oid_list sysctl__kern_sysv_children
;
1422 SYSCTL_PROC(_kern_sysv
, KSYSV_SEMMNI
, semmni
, CTLTYPE_INT
| CTLFLAG_RW
,
1423 &limitseminfo
.semmni
, 0, &sysctl_seminfo
,"I","semmni");
1425 SYSCTL_PROC(_kern_sysv
, KSYSV_SEMMNS
, semmns
, CTLTYPE_INT
| CTLFLAG_RW
,
1426 &limitseminfo
.semmns
, 0, &sysctl_seminfo
,"I","semmns");
1428 SYSCTL_PROC(_kern_sysv
, KSYSV_SEMMNU
, semmnu
, CTLTYPE_INT
| CTLFLAG_RW
,
1429 &limitseminfo
.semmnu
, 0, &sysctl_seminfo
,"I","semmnu");
1431 SYSCTL_PROC(_kern_sysv
, KSYSV_SEMMSL
, semmsl
, CTLTYPE_INT
| CTLFLAG_RW
,
1432 &limitseminfo
.semmsl
, 0, &sysctl_seminfo
,"I","semmsl");
1434 SYSCTL_PROC(_kern_sysv
, KSYSV_SEMUNE
, semume
, CTLTYPE_INT
| CTLFLAG_RW
,
1435 &limitseminfo
.semume
, 0, &sysctl_seminfo
,"I","semume");
1439 IPCS_sem_sysctl(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
,
1440 __unused
int arg2
, struct sysctl_req
*req
)
1445 struct IPCS_command u32
;
1446 struct user_IPCS_command u64
;
1448 struct semid_ds semid_ds32
; /* post conversion, 32 bit version */
1450 size_t ipcs_sz
= sizeof(struct user_IPCS_command
);
1451 size_t semid_ds_sz
= sizeof(struct user_semid_ds
);
1452 struct proc
*p
= current_proc();
1454 /* Copy in the command structure */
1455 if ((error
= SYSCTL_IN(req
, &ipcs
, ipcs_sz
)) != 0) {
1459 if (!IS_64BIT_PROCESS(p
)) {
1460 ipcs_sz
= sizeof(struct IPCS_command
);
1461 semid_ds_sz
= sizeof(struct semid_ds
);
1464 /* Let us version this interface... */
1465 if (ipcs
.u64
.ipcs_magic
!= IPCS_MAGIC
) {
1469 SYSV_SEM_SUBSYS_LOCK();
1470 switch(ipcs
.u64
.ipcs_op
) {
1471 case IPCS_SEM_CONF
: /* Obtain global configuration data */
1472 if (ipcs
.u64
.ipcs_datalen
!= sizeof(struct seminfo
)) {
1476 if (ipcs
.u64
.ipcs_cursor
!= 0) { /* fwd. compat. */
1480 error
= copyout(&seminfo
, ipcs
.u64
.ipcs_data
, ipcs
.u64
.ipcs_datalen
);
1483 case IPCS_SEM_ITER
: /* Iterate over existing segments */
1484 cursor
= ipcs
.u64
.ipcs_cursor
;
1485 if (cursor
< 0 || cursor
>= seminfo
.semmni
) {
1489 if (ipcs
.u64
.ipcs_datalen
!= (int)semid_ds_sz
) {
1493 for( ; cursor
< seminfo
.semmni
; cursor
++) {
1494 if (sema
[cursor
].sem_perm
.mode
& SEM_ALLOC
)
1498 if (cursor
== seminfo
.semmni
) {
1503 semid_dsp
= &sema
[cursor
]; /* default: 64 bit */
1506 * If necessary, convert the 64 bit kernel segment
1507 * descriptor to a 32 bit user one.
1509 if (!IS_64BIT_PROCESS(p
)) {
1510 semid_ds_64to32(semid_dsp
, &semid_ds32
);
1511 semid_dsp
= &semid_ds32
;
1513 error
= copyout(semid_dsp
, ipcs
.u64
.ipcs_data
, ipcs
.u64
.ipcs_datalen
);
1516 ipcs
.u64
.ipcs_cursor
= cursor
+ 1;
1517 error
= SYSCTL_OUT(req
, &ipcs
, ipcs_sz
);
1525 SYSV_SEM_SUBSYS_UNLOCK();
1529 SYSCTL_DECL(_kern_sysv_ipcs
);
1530 SYSCTL_PROC(_kern_sysv_ipcs
, OID_AUTO
, sem
, CTLFLAG_RW
|CTLFLAG_ANYBODY
,
1531 0, 0, IPCS_sem_sysctl
,
1532 "S,IPCS_sem_command",
1533 "ipcs sem command interface");