OSDN Git Service

* transport_pipes.cc: Include ntdef.h to accommodate cygerrno.h.
[pf3gnuchains/pf3gnuchains4x.git] / winsup / cygserver / sysv_sem.cc
1 /*
2  * Implementation of SVID semaphores
3  *
4  * Author:  Daniel Boulet
5  *
6  * This software is provided ``AS IS'' without any warranties of any kind.
7  */
8
9 /*
10  * This file is heavily changed to become part of Cygwin's cygserver.
11  */
12
13 #ifdef __OUTSIDE_CYGWIN__
14 #include "woutsup.h"
15 #include <stdio.h>
16 #include <sys/cygwin.h>
17 #include <sys/cdefs.h>
18 #ifndef __FBSDID
19 #define __FBSDID(s)     const char version[] = (s)
20 #endif
21 __FBSDID("$FreeBSD: /repoman/r/ncvs/src/sys/kern/sysv_sem.c,v 1.70 2004/05/30 20:34:58 phk Exp $");
22 /* CV, 2006-01-09: Inspected upstream up to version 1.78. */
23
24 #define _KERNEL 1
25 #define __BSD_VISIBLE 1
26 #include <sys/types.h>
27 #include <sys/ipc.h>
28
29 #include <sys/param.h>
30 #include <sys/sysproto.h>
31 #include <sys/lock.h>
32 #include <sys/sem.h>
33 #include <sys/queue.h>
34 #include <malloc.h>
35 #include <errno.h>
36 #include <time.h>
37 #include "cygserver.h"
38 #include "process.h"
39 #include "cygserver_ipc.h"
40 #include <sys/smallprint.h>
41
42 #ifdef __CYGWIN__
43 #define __semctl semctl
44 #define __semctl_args semctl_args
45 #define SEM_DEBUG
46 #endif /* __CYGWIN__ */
47
48 #ifdef SEM_DEBUG
49 #define DPRINTF(a)      debug_printf a
50 #else
51 #define DPRINTF(a)
52 #endif
53
54 static int semvalid(int semid, struct semid_ds *semaptr);
55
56 static struct sem_undo *semu_alloc(struct thread *td);
57 static int semundo_adjust(struct thread *td, struct sem_undo **supptr,
58                 int semid, int semnum, int adjval);
59 static void semundo_clear(int semid, int semnum, struct thread *td);
60
61 #ifndef _SYS_SYSPROTO_H_
62 struct __semctl_args;
63 int __semctl(struct thread *td, struct __semctl_args *uap);
64 struct semget_args;
65 int semget(struct thread *td, struct semget_args *uap);
66 struct semop_args;
67 int semop(struct thread *td, struct semop_args *uap);
68 #endif
69
70 #ifndef __CYGWIN__
71 /* XXX casting to (sy_call_t *) is bogus, as usual. */
72 static sy_call_t *semcalls[] = {
73         (sy_call_t *)__semctl, (sy_call_t *)semget,
74         (sy_call_t *)semop
75 };
76 #endif
77
78 static struct mtx       sem_mtx;        /* semaphore global lock */
79 static int      semtots = 0;
80 static int      semtot = 0;
81 static struct semid_ds *sema;   /* semaphore id pool */
82 static struct mtx *sema_mtx;    /* semaphore id pool mutexes*/
83 static struct sem *sem;         /* semaphore pool */
84 static SLIST_HEAD(, sem_undo) semu_list;        /* list of active undo structures */
85 static int      *semu;          /* undo structure pool */
86 #ifndef __CYGWIN__
87 static eventhandler_tag semexit_tag;
88 #endif /* __CYGWIN__ */
89
90 #define SEMUNDO_MTX             sem_mtx
91 #define SEMUNDO_LOCK()          mtx_lock(&SEMUNDO_MTX);
92 #define SEMUNDO_HOOKLOCK()      _mtx_lock(&SEMUNDO_MTX, p->winpid, __FILE__, __LINE__);
93 #define SEMUNDO_UNLOCK()        mtx_unlock(&SEMUNDO_MTX);
94 #define SEMUNDO_LOCKASSERT(how,pid)     mtx_assert(&SEMUNDO_MTX, (how), (pid));
95
96 struct sem {
97         u_short semval;         /* semaphore value */
98         pid_t   sempid;         /* pid of last operation */
99         u_short semncnt;        /* # awaiting semval > cval */
100         u_short semzcnt;        /* # awaiting semval = 0 */
101 };
102
103 /*
104  * Undo structure (one per process)
105  */
106 struct undo {
107         short   un_adjval;      /* adjust on exit values */
108         short   un_num;         /* semaphore # */
109         int     un_id;          /* semid */
110 } un_ent[1];                    /* undo entries */
111
112 struct sem_undo {
113         SLIST_ENTRY(sem_undo) un_next;  /* ptr to next active undo structure */
114 #ifdef __CYGWIN__
115         DWORD   un_proc;                /* owner of this structure */
116 #else
117         struct  proc *un_proc;          /* owner of this structure */
118 #endif
119         short   un_cnt;                 /* # of active entries */
120         struct undo un_ent[1];          /* undo entries */
121 };
122
123 /*
124  * Configuration parameters
125  */
126 #ifndef SEMMNI
127 #define SEMMNI  10              /* # of semaphore identifiers */
128 #endif
129 #ifndef SEMMNS
130 #define SEMMNS  60              /* # of semaphores in system */
131 #endif
132 #ifndef SEMUME
133 #define SEMUME  10              /* max # of undo entries per process */
134 #endif
135 #ifndef SEMMNU
136 #define SEMMNU  30              /* # of undo structures in system */
137 #endif
138
139 /* shouldn't need tuning */
140 #ifndef SEMMAP
141 #define SEMMAP  30              /* # of entries in semaphore map */
142 #endif
143 #ifndef SEMMSL
144 #define SEMMSL  SEMMNS          /* max # of semaphores per id */
145 #endif
146 #ifndef SEMOPM
147 #define SEMOPM  100             /* max # of operations per semop call */
148 #endif
149
150 #ifndef SEMVMX
151 #define SEMVMX  32767           /* semaphore maximum value */
152 #endif
153 #ifndef SEMAEM
154 #define SEMAEM  16384           /* adjust on exit max value */
155 #endif
156
157 #ifdef __CYGWIN__
158 /* gcc 3.4 defines a new offsetof which is different for C++.  Since this
159    file is just a derived plain-C file, we need to revert to the plain-C
160    definition of offsetof. */
161 #ifdef offsetof
162 #undef offsetof
163 #endif
164 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
165 #endif /* __CYGWIN__ */
166 /*
167  * Due to the way semaphore memory is allocated, we have to ensure that
168  * SEMUSZ is properly aligned.
169  */
170
171 #define SEM_ALIGN(bytes) (((bytes) + (sizeof(long) - 1)) & ~(sizeof(long) - 1))
172
173 /* actual size of an undo structure */
174 #define SEMUSZ  SEM_ALIGN(offsetof(struct sem_undo, un_ent[SEMUME]))
175
176 /*
177  * Macro to find a particular sem_undo vector
178  */
179 #define SEMU(ix) \
180         ((struct sem_undo *)(((intptr_t)semu)+ix * seminfo.semusz))
181
182 /*
183  * semaphore info struct
184  */
185 struct seminfo seminfo = {
186                 SEMMNI,         /* # of semaphore identifiers */
187                 SEMMNS,         /* # of semaphores in system */
188                 SEMMSL,         /* max # of semaphores per id */
189                 SEMOPM,         /* max # of operations per semop call */
190                 SEMMNU,         /* # of undo structures in system */
191                 SEMUME,         /* max # of undo entries per process */
192                 SEMVMX,         /* semaphore maximum value */
193                 SEMAEM,         /* adjust on exit max value */
194                 SEMMAP,         /* # of entries in semaphore map */
195                 SEMUSZ          /* size in bytes of undo structure */
196 };
197
198 #ifndef __CYGWIN__
199 SYSCTL_DECL(_kern_ipc);
200 SYSCTL_INT(_kern_ipc, OID_AUTO, semmap, CTLFLAG_RW, &seminfo.semmap, 0, "");
201 SYSCTL_INT(_kern_ipc, OID_AUTO, semmni, CTLFLAG_RDTUN, &seminfo.semmni, 0, "");
202 SYSCTL_INT(_kern_ipc, OID_AUTO, semmns, CTLFLAG_RDTUN, &seminfo.semmns, 0, "");
203 SYSCTL_INT(_kern_ipc, OID_AUTO, semmnu, CTLFLAG_RDTUN, &seminfo.semmnu, 0, "");
204 SYSCTL_INT(_kern_ipc, OID_AUTO, semmsl, CTLFLAG_RW, &seminfo.semmsl, 0, "");
205 SYSCTL_INT(_kern_ipc, OID_AUTO, semopm, CTLFLAG_RDTUN, &seminfo.semopm, 0, "");
206 SYSCTL_INT(_kern_ipc, OID_AUTO, semume, CTLFLAG_RDTUN, &seminfo.semume, 0, "");
207 SYSCTL_INT(_kern_ipc, OID_AUTO, semusz, CTLFLAG_RDTUN, &seminfo.semusz, 0, "");
208 SYSCTL_INT(_kern_ipc, OID_AUTO, semvmx, CTLFLAG_RW, &seminfo.semvmx, 0, "");
209 SYSCTL_INT(_kern_ipc, OID_AUTO, semaem, CTLFLAG_RW, &seminfo.semaem, 0, "");
210 SYSCTL_PROC(_kern_ipc, OID_AUTO, sema, CTLFLAG_RD,
211     NULL, 0, sysctl_sema, "", "");
212 #endif /* __CYGWIN__ */
213
214 void
215 seminit(void)
216 {
217         int i;
218
219         TUNABLE_INT_FETCH("kern.ipc.semmap", &seminfo.semmap);
220         TUNABLE_INT_FETCH("kern.ipc.semmni", &seminfo.semmni);
221         TUNABLE_INT_FETCH("kern.ipc.semmns", &seminfo.semmns);
222         TUNABLE_INT_FETCH("kern.ipc.semmnu", &seminfo.semmnu);
223         TUNABLE_INT_FETCH("kern.ipc.semmsl", &seminfo.semmsl);
224         TUNABLE_INT_FETCH("kern.ipc.semopm", &seminfo.semopm);
225         TUNABLE_INT_FETCH("kern.ipc.semume", &seminfo.semume);
226         TUNABLE_INT_FETCH("kern.ipc.semusz", &seminfo.semusz);
227         TUNABLE_INT_FETCH("kern.ipc.semvmx", &seminfo.semvmx);
228         TUNABLE_INT_FETCH("kern.ipc.semaem", &seminfo.semaem);
229
230 #ifdef __CYGWIN__
231         /* It's too dangerous a setting to leave it alone. 
232            Keep that clean here. */
233         seminfo.semusz = SEM_ALIGN(offsetof(struct sem_undo,
234                                             un_ent[seminfo.semume]));
235 #endif /* __CYGWIN__ */
236
237         sem = (struct sem *) sys_malloc(sizeof(struct sem) * seminfo.semmns, M_SEM, M_WAITOK);
238         sema = (struct semid_ds *) sys_malloc(sizeof(struct semid_ds) * seminfo.semmni, M_SEM,
239             M_WAITOK);
240         sema_mtx = (struct mtx *) sys_malloc(sizeof(struct mtx) * seminfo.semmni, M_SEM,
241             M_WAITOK | M_ZERO);
242         semu = (int *) sys_malloc(seminfo.semmnu * seminfo.semusz, M_SEM, M_WAITOK);
243
244         for (i = 0; i < seminfo.semmni; i++) {
245                 sema[i].sem_base = 0;
246                 sema[i].sem_perm.mode = 0;
247                 sema[i].sem_perm.seq = 0;
248         }
249         for (i = 0; i < seminfo.semmni; i++)
250         {
251                 char *buf = (char *) sys_malloc(16, M_SEM, M_WAITOK);
252                 snprintf(buf, 16, "semid[%d]", i);
253                 mtx_init(&sema_mtx[i], buf, NULL, MTX_DEF);
254         }
255         for (i = 0; i < seminfo.semmnu; i++) {
256                 struct sem_undo *suptr = SEMU(i);
257 #ifdef __CYGWIN__
258                 suptr->un_proc = 0;
259 #else
260                 suptr->un_proc = NULL;
261 #endif
262         }
263         SLIST_INIT(&semu_list);
264         mtx_init(&sem_mtx, "sem", NULL, MTX_DEF);
265 #ifndef __CYGWIN__
266         semexit_tag = EVENTHANDLER_REGISTER(process_exit, semexit_myhook, NULL,
267             EVENTHANDLER_PRI_ANY);
268 #endif /* __CYGWIN__ */
269 }
270
271 int
272 semunload(void)
273 {
274 #ifndef __CYGWIN__      /* Would result in being unable to shutdown the
275                            server gracefully. */
276         if (semtot != 0)
277                 return (EBUSY);
278
279         EVENTHANDLER_DEREGISTER(process_exit, semexit_tag);
280 #endif /* __CYGWIN__ */
281         sys_free(sem, M_SEM);
282         sys_free(sema, M_SEM);
283         sys_free(semu, M_SEM);
284         for (int i = 0; i < seminfo.semmni; i++) {
285                 sys_free((void *) sema_mtx[i].name, M_SEM);
286                 mtx_destroy(&sema_mtx[i]);
287         }
288         mtx_destroy(&sem_mtx);
289         return (0);
290 }
291
292 #ifndef __CYGWIN__
293 static int
294 sysvsem_modload(struct module *module, int cmd, void *arg)
295 {
296         int error = 0;
297
298         switch (cmd) {
299         case MOD_LOAD:
300                 seminit();
301                 break;
302         case MOD_UNLOAD:
303                 error = semunload();
304                 break;
305         case MOD_SHUTDOWN:
306                 break;
307         default:
308                 error = EINVAL;
309                 break;
310         }
311         return (error);
312 }
313
314 static moduledata_t sysvsem_mod = {
315         "sysvsem",
316         &sysvsem_modload,
317         NULL
318 };
319
320 SYSCALL_MODULE_HELPER(semsys);
321 SYSCALL_MODULE_HELPER(__semctl);
322 SYSCALL_MODULE_HELPER(semget);
323 SYSCALL_MODULE_HELPER(semop);
324
325 DECLARE_MODULE(sysvsem, sysvsem_mod,
326         SI_SUB_SYSV_SEM, SI_ORDER_FIRST);
327 MODULE_VERSION(sysvsem, 1);
328
329 /*
330  * Entry point for all SEM calls
331  *
332  * MPSAFE
333  */
334 int
335 semsys(td, uap)
336         struct thread *td;
337         /* XXX actually varargs. */
338         struct semsys_args /* {
339                 int     which;
340                 int     a2;
341                 int     a3;
342                 int     a4;
343                 int     a5;
344         } */ *uap;
345 {
346         int error;
347
348         if (!jail_sysvipc_allowed && jailed(td->td_ucred))
349                 return (ENOSYS);
350         if (uap->which < 0 ||
351             uap->which >= sizeof(semcalls)/sizeof(semcalls[0]))
352                 return (EINVAL);
353         error = (*semcalls[uap->which])(td, &uap->a2);
354         return (error);
355 }
356 #endif /* __CYGWIN__ */
357
358 /*
359  * Allocate a new sem_undo structure for a process
360  * (returns ptr to structure or NULL if no more room)
361  */
362
363 static struct sem_undo *
364 semu_alloc(struct thread *td)
365 {
366         int i;
367         struct sem_undo *suptr;
368         struct sem_undo **supptr;
369         int attempt;
370
371         SEMUNDO_LOCKASSERT(MA_OWNED, td->td_proc->winpid);
372         /*
373          * Try twice to allocate something.
374          * (we'll purge an empty structure after the first pass so
375          * two passes are always enough)
376          */
377
378         for (attempt = 0; attempt < 2; attempt++) {
379                 /*
380                  * Look for a free structure.
381                  * Fill it in and return it if we find one.
382                  */
383
384                 for (i = 0; i < seminfo.semmnu; i++) {
385                         suptr = SEMU(i);
386 #ifdef __CYGWIN__
387                         if (suptr->un_proc == 0) {
388 #else
389                         if (suptr->un_proc == NULL) {
390 #endif
391                                 SLIST_INSERT_HEAD(&semu_list, suptr, un_next);
392                                 suptr->un_cnt = 0;
393                                 suptr->un_proc = td->td_proc->winpid;
394                                 return(suptr);
395                         }
396                 }
397
398                 /*
399                  * We didn't find a free one, if this is the first attempt
400                  * then try to free a structure.
401                  */
402
403                 if (attempt == 0) {
404                         /* All the structures are in use - try to free one */
405                         int did_something = 0;
406
407                         SLIST_FOREACH_PREVPTR(suptr, supptr, &semu_list,
408                             un_next) {
409                                 if (suptr->un_cnt == 0) {
410 #ifdef __CYGWIN__
411                                         suptr->un_proc = 0;
412 #else
413                                         suptr->un_proc = NULL;
414 #endif
415                                         did_something = 1;
416                                         *supptr = SLIST_NEXT(suptr, un_next);
417                                         break;
418                                 }
419                         }
420
421                         /* If we didn't free anything then just give-up */
422                         if (!did_something)
423                                 return(NULL);
424                 } else {
425                         /*
426                          * The second pass failed even though we freed
427                          * something after the first pass!
428                          * This is IMPOSSIBLE!
429                          */
430                         panic("semu_alloc - second attempt failed");
431                 }
432         }
433         return (NULL);
434 }
435
436 /*
437  * Adjust a particular entry for a particular proc
438  */
439
440 static int
441 semundo_adjust(struct thread *td, struct sem_undo **supptr, int semid,
442                int semnum, int adjval)
443 {
444         struct proc *p = td->td_proc;
445         struct sem_undo *suptr;
446         struct undo *sunptr;
447         int i;
448
449         SEMUNDO_LOCKASSERT(MA_OWNED, td->td_proc->winpid);
450         /* Look for and remember the sem_undo if the caller doesn't provide
451            it */
452
453         suptr = *supptr;
454         if (suptr == NULL) {
455                 SLIST_FOREACH(suptr, &semu_list, un_next) {
456 #ifdef __CYGWIN__
457                         if (suptr->un_proc == p->winpid) {
458 #else
459                         if (suptr->un_proc == p) {
460 #endif
461                                 *supptr = suptr;
462                                 break;
463                         }
464                 }
465                 if (suptr == NULL) {
466                         if (adjval == 0)
467                                 return(0);
468                         suptr = semu_alloc(td);
469                         if (suptr == NULL)
470                                 return(ENOSPC);
471                         *supptr = suptr;
472                 }
473         }
474
475         /*
476          * Look for the requested entry and adjust it (delete if adjval becomes
477          * 0).
478          */
479         sunptr = &suptr->un_ent[0];
480         for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
481                 if (sunptr->un_id != semid || sunptr->un_num != semnum)
482                         continue;
483                 if (adjval != 0) {
484                         adjval += sunptr->un_adjval;
485                         if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
486                                 return (ERANGE);
487                 }
488                 sunptr->un_adjval = adjval;
489                 if (sunptr->un_adjval == 0) {
490                         suptr->un_cnt--;
491                         if (i < suptr->un_cnt)
492                                 suptr->un_ent[i] =
493                                     suptr->un_ent[suptr->un_cnt];
494                 }
495                 return(0);
496         }
497
498         /* Didn't find the right entry - create it */
499         if (adjval == 0)
500                 return(0);
501         if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
502                 return (ERANGE);
503         if (suptr->un_cnt != seminfo.semume) {
504                 sunptr = &suptr->un_ent[suptr->un_cnt];
505                 suptr->un_cnt++;
506                 sunptr->un_adjval = adjval;
507                 sunptr->un_id = semid; sunptr->un_num = semnum;
508         } else
509                 return(EINVAL);
510         return(0);
511 }
512
513 static void
514 semundo_clear(int semid, int semnum, struct thread *td)
515 {
516         struct sem_undo *suptr;
517
518         SEMUNDO_LOCKASSERT(MA_OWNED, td->td_proc->winpid);
519         SLIST_FOREACH(suptr, &semu_list, un_next) {
520                 struct undo *sunptr = &suptr->un_ent[0];
521                 int i = 0;
522
523                 while (i < suptr->un_cnt) {
524                         if (sunptr->un_id == semid) {
525                                 if (semnum == -1 || sunptr->un_num == semnum) {
526                                         suptr->un_cnt--;
527                                         if (i < suptr->un_cnt) {
528                                                 suptr->un_ent[i] =
529                                                   suptr->un_ent[suptr->un_cnt];
530                                                 continue;
531                                         }
532                                 }
533                                 if (semnum != -1)
534                                         break;
535                         }
536                         i++, sunptr++;
537                 }
538         }
539 }
540
541 static int
542 semvalid(int semid, struct semid_ds *semaptr)
543 {
544
545         return ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
546             semaptr->sem_perm.seq != IPCID_TO_SEQ(semid) ? EINVAL : 0);
547 }
548
549 /*
550  * Note that the user-mode half of this passes a union, not a pointer
551  */
552 #ifndef _SYS_SYSPROTO_H_
553 struct __semctl_args {
554         int     semid;
555         int     semnum;
556         int     cmd;
557         union   semun *arg;
558 };
559 #endif
560
561 /*
562  * MPSAFE
563  */
564 int
565 __semctl(struct thread *td, struct __semctl_args *uap)
566 {
567         int semid = uap->semid;
568         int semnum = uap->semnum;
569         int cmd = uap->cmd;
570         u_short *array;
571         union semun *arg = uap->arg;
572         union semun real_arg;
573 #ifndef __CYGWIN__
574         struct ucred *cred = td->td_ucred;
575 #endif
576         int i, rval, error;
577         struct semid_ds sbuf;
578         struct semid_ds *semaptr;
579         struct mtx *sema_mtxp;
580         u_short usval, count;
581
582         DPRINTF(("call to semctl(%d, %d, %d, 0x%x)\n",
583             semid, semnum, cmd, arg));
584         if (!jail_sysvipc_allowed && jailed(td->td_ucred))
585                 return (ENOSYS);
586
587         array = NULL;
588
589         switch(cmd) {
590 #ifdef __CYGWIN__
591         case IPC_INFO:
592                 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
593                         return (error);
594                 if (!semid) {
595                         error = copyout(&seminfo, real_arg.buf,
596                                         sizeof(struct seminfo));
597                         td->td_retval[0] = error ? -1 : 0;
598                         return (error);
599                 }
600                 if (semid > seminfo.semmni)
601                         semid = seminfo.semmni;
602                 error = copyout(sema, real_arg.buf,
603                                 semid * sizeof(struct semid_ds));
604                 td->td_retval[0] = error ? -1 : 0;
605                 return (error);
606         case SEM_INFO:
607                 if (!(error = copyin(arg, &real_arg, sizeof(real_arg)))) {
608                         struct sem_info sem_info;
609                         sem_info.sem_ids = semtots;
610                         sem_info.sem_num = semtot;
611                         error = copyout(&sem_info, real_arg.buf,
612                                         sizeof(struct sem_info));
613                 }
614                 td->td_retval[0] = error ? -1 : 0;
615                 return (error);
616
617 #endif /* __CYGWIN__ */
618         case SEM_STAT:
619                 if (semid < 0 || semid >= seminfo.semmni)
620                         return (EINVAL);
621                 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
622                         return (error);
623                 semaptr = &sema[semid];
624                 sema_mtxp = &sema_mtx[semid];
625                 mtx_lock(sema_mtxp);
626                 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0) {
627                         error = EINVAL;
628                         goto done2;
629                 }
630                 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
631                         goto done2;
632                 mtx_unlock(sema_mtxp);
633                 error = copyout(semaptr, real_arg.buf, sizeof(struct semid_ds));
634                 rval = IXSEQ_TO_IPCID(semid,semaptr->sem_perm);
635                 if (error == 0)
636                         td->td_retval[0] = rval;
637                 return (error);
638         }
639
640         semid = IPCID_TO_IX(semid);
641         if (semid < 0 || semid >= seminfo.semmni)
642                 return (EINVAL);
643
644         semaptr = &sema[semid];
645         sema_mtxp = &sema_mtx[semid];
646                 
647         error = 0;
648         rval = 0;
649
650         switch (cmd) {
651         case IPC_RMID:
652                 mtx_lock(sema_mtxp);
653                 if ((error = semvalid(uap->semid, semaptr)) != 0)
654                         goto done2;
655                 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_M)))
656                         goto done2;
657 #ifdef __CYGWIN__
658                 semaptr->sem_perm.cuid = td->ipcblk->uid;
659                 semaptr->sem_perm.uid = td->ipcblk->uid;
660 #else
661                 semaptr->sem_perm.cuid = cred->cr_uid;
662                 semaptr->sem_perm.uid = cred->cr_uid;
663 #endif
664                 semtot -= semaptr->sem_nsems;
665                 semtots--;
666                 for (i = semaptr->sem_base - sem; i < semtot; i++)
667                         sem[i] = sem[i + semaptr->sem_nsems];
668                 for (i = 0; i < seminfo.semmni; i++) {
669                         if ((sema[i].sem_perm.mode & SEM_ALLOC) &&
670                             sema[i].sem_base > semaptr->sem_base)
671                                 sema[i].sem_base -= semaptr->sem_nsems;
672                 }
673                 semaptr->sem_perm.mode = 0;
674                 SEMUNDO_LOCK();
675                 semundo_clear(semid, -1, td);
676                 SEMUNDO_UNLOCK();
677                 wakeup(semaptr);
678                 break;
679
680         case IPC_SET:
681                 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
682                         goto done2;
683                 if ((error = copyin(real_arg.buf, &sbuf, sizeof(sbuf))) != 0)
684                         goto done2;
685                 mtx_lock(sema_mtxp);
686                 if ((error = semvalid(uap->semid, semaptr)) != 0)
687                         goto done2;
688                 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_M)))
689                         goto done2;
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 = time (NULL);
695                 break;
696
697         case IPC_STAT:
698                 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
699                         goto done2;
700                 mtx_lock(sema_mtxp);
701                 if ((error = semvalid(uap->semid, semaptr)) != 0)
702                         goto done2;
703                 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
704                         goto done2;
705                 sbuf = *semaptr;
706                 mtx_unlock(sema_mtxp);
707                 error = copyout(semaptr, real_arg.buf,
708                                 sizeof(struct semid_ds));
709                 break;
710
711         case GETNCNT:
712                 mtx_lock(sema_mtxp);
713                 if ((error = semvalid(uap->semid, semaptr)) != 0)
714                         goto done2;
715                 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
716                         goto done2;
717                 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
718                         error = EINVAL;
719                         goto done2;
720                 }
721                 rval = semaptr->sem_base[semnum].semncnt;
722                 break;
723
724         case GETPID:
725                 mtx_lock(sema_mtxp);
726                 if ((error = semvalid(uap->semid, semaptr)) != 0)
727                         goto done2;
728                 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
729                         goto done2;
730                 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
731                         error = EINVAL;
732                         goto done2;
733                 }
734                 rval = semaptr->sem_base[semnum].sempid;
735                 break;
736
737         case GETVAL:
738                 mtx_lock(sema_mtxp);
739                 if ((error = semvalid(uap->semid, semaptr)) != 0)
740                         goto done2;
741                 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
742                         goto done2;
743                 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
744                         error = EINVAL;
745                         goto done2;
746                 }
747                 rval = semaptr->sem_base[semnum].semval;
748                 break;
749
750         case GETALL:
751                 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
752                         goto done2;
753                 array = (u_short *) sys_malloc(sizeof(*array) * semaptr->sem_nsems, M_TEMP,
754                     M_WAITOK);
755                 mtx_lock(sema_mtxp);
756                 if ((error = semvalid(uap->semid, semaptr)) != 0)
757                         goto done2;
758                 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
759                         goto done2;
760                 for (i = 0; i < semaptr->sem_nsems; i++)
761                         array[i] = semaptr->sem_base[i].semval;
762                 mtx_unlock(sema_mtxp);
763                 error = copyout(array, real_arg.array,
764                     i * sizeof(real_arg.array[0]));
765                 break;
766
767         case GETZCNT:
768                 mtx_lock(sema_mtxp);
769                 if ((error = semvalid(uap->semid, semaptr)) != 0)
770                         goto done2;
771                 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
772                         goto done2;
773                 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
774                         error = EINVAL;
775                         goto done2;
776                 }
777                 rval = semaptr->sem_base[semnum].semzcnt;
778                 break;
779
780         case SETVAL:
781                 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
782                         goto done2;
783                 mtx_lock(sema_mtxp);
784                 if ((error = semvalid(uap->semid, semaptr)) != 0)
785                         goto done2;
786                 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_W)))
787                         goto done2;
788                 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
789                         error = EINVAL;
790                         goto done2;
791                 }
792                 if (real_arg.val < 0 || real_arg.val > seminfo.semvmx) {
793                         error = ERANGE;
794                         goto done2;
795                 }
796                 semaptr->sem_base[semnum].semval = real_arg.val;
797                 SEMUNDO_LOCK();
798                 semundo_clear(semid, semnum, td);
799                 SEMUNDO_UNLOCK();
800                 wakeup(semaptr);
801                 break;
802
803         case SETALL:
804                 mtx_lock(sema_mtxp);
805 raced:
806                 if ((error = semvalid(uap->semid, semaptr)) != 0)
807                         goto done2;
808                 count = semaptr->sem_nsems;
809                 mtx_unlock(sema_mtxp);
810                 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
811                         goto done2;
812                 array = (u_short *) sys_malloc(sizeof(*array) * count, M_TEMP, M_WAITOK);
813                 error = copyin(real_arg.array, array, count * sizeof(*array));
814                 if (error)
815                         break;
816                 mtx_lock(sema_mtxp);
817                 if ((error = semvalid(uap->semid, semaptr)) != 0)
818                         goto done2;
819                 /* we could have raced? */
820                 if (count != semaptr->sem_nsems) {
821                         sys_free(array, M_TEMP);
822                         array = NULL;
823                         goto raced;
824                 }
825                 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_W)))
826                         goto done2;
827                 for (i = 0; i < semaptr->sem_nsems; i++) {
828                         usval = array[i];
829                         if (usval > seminfo.semvmx) {
830                                 error = ERANGE;
831                                 break;
832                         }
833                         semaptr->sem_base[i].semval = usval;
834                 }
835                 SEMUNDO_LOCK();
836                 semundo_clear(semid, -1, td);
837                 SEMUNDO_UNLOCK();
838                 wakeup(semaptr);
839                 break;
840
841         default:
842                 error = EINVAL;
843                 break;
844         }
845
846         if (error == 0)
847                 td->td_retval[0] = rval;
848 done2:
849         if (mtx_owned(sema_mtxp, td->td_proc->winpid))
850                 mtx_unlock(sema_mtxp);
851         if (array != NULL)
852                 sys_free(array, M_TEMP);
853         return(error);
854 }
855
856 #ifndef _SYS_SYSPROTO_H_
857 struct semget_args {
858         key_t   key;
859         int     nsems;
860         int     semflg;
861 };
862 #endif
863
864 /*
865  * MPSAFE
866  */
867 int
868 semget(struct thread *td, struct semget_args *uap)
869 {
870         int semid, error = 0;
871         key_t key = uap->key;
872         int nsems = uap->nsems;
873         int semflg = uap->semflg;
874 #ifndef __CYGWIN__
875         struct ucred *cred = td->td_ucred;
876 #endif
877
878         DPRINTF(("semget(0x%llx, %d, 0%o)\n", key, nsems, semflg));
879         if (!jail_sysvipc_allowed && jailed(td->td_ucred))
880                 return (ENOSYS);
881
882         mtx_lock(&Giant);
883         if (key != IPC_PRIVATE) {
884                 for (semid = 0; semid < seminfo.semmni; semid++) {
885                         if ((sema[semid].sem_perm.mode & SEM_ALLOC) &&
886                             sema[semid].sem_perm.key == key)
887                                 break;
888                 }
889                 if (semid < seminfo.semmni) {
890                         DPRINTF(("found public key\n"));
891                         if ((error = ipcperm(td, &sema[semid].sem_perm,
892                             semflg & 0700))) {
893                                 goto done2;
894                         }
895                         if (nsems > 0 && sema[semid].sem_nsems < nsems) {
896                                 DPRINTF(("too small\n"));
897                                 error = EINVAL;
898                                 goto done2;
899                         }
900                         if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
901                                 DPRINTF(("not exclusive\n"));
902                                 error = EEXIST;
903                                 goto done2;
904                         }
905                         goto found;
906                 }
907         }
908
909         DPRINTF(("need to allocate the semid_ds\n"));
910         if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
911                 if (nsems <= 0 || nsems > seminfo.semmsl) {
912                         DPRINTF(("nsems out of range (0<%d<=%d)\n", nsems,
913                             seminfo.semmsl));
914                         error = EINVAL;
915                         goto done2;
916                 }
917                 if (nsems > seminfo.semmns - semtot) {
918                         DPRINTF((
919                             "not enough semaphores left (need %d, got %d)\n",
920                             nsems, seminfo.semmns - semtot));
921                         error = ENOSPC;
922                         goto done2;
923                 }
924                 for (semid = 0; semid < seminfo.semmni; semid++) {
925                         if ((sema[semid].sem_perm.mode & SEM_ALLOC) == 0)
926                                 break;
927                 }
928                 if (semid == seminfo.semmni) {
929                         DPRINTF(("no more semid_ds's available\n"));
930                         error = ENOSPC;
931                         goto done2;
932                 }
933                 DPRINTF(("semid %d is available\n", semid));
934                 sema[semid].sem_perm.key = key;
935 #ifdef __CYGWIN__
936                 sema[semid].sem_perm.cuid = td->ipcblk->uid;
937                 sema[semid].sem_perm.uid = td->ipcblk->uid;
938                 sema[semid].sem_perm.cgid = td->ipcblk->gid;
939                 sema[semid].sem_perm.gid = td->ipcblk->gid;
940 #else
941                 sema[semid].sem_perm.cuid = cred->cr_uid;
942                 sema[semid].sem_perm.uid = cred->cr_uid;
943                 sema[semid].sem_perm.cgid = cred->cr_gid;
944                 sema[semid].sem_perm.gid = cred->cr_gid;
945 #endif
946                 sema[semid].sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
947                 sema[semid].sem_perm.seq =
948                     (sema[semid].sem_perm.seq + 1) & 0x7fff;
949                 sema[semid].sem_nsems = nsems;
950                 sema[semid].sem_otime = 0;
951                 sema[semid].sem_ctime = time (NULL);
952                 sema[semid].sem_base = &sem[semtot];
953                 semtot += nsems;
954                 semtots++;
955                 bzero(sema[semid].sem_base,
956                     sizeof(sema[semid].sem_base[0])*nsems);
957                 DPRINTF(("sembase = 0x%x, next = 0x%x\n", sema[semid].sem_base,
958                     &sem[semtot]));
959         } else {
960                 DPRINTF(("didn't find it and wasn't asked to create it\n"));
961                 error = ENOENT;
962                 goto done2;
963         }
964
965 found:
966         td->td_retval[0] = IXSEQ_TO_IPCID(semid, sema[semid].sem_perm);
967 done2:
968 #ifdef __CYGWIN__
969         if (!error)
970                 ipcexit_creat_hookthread (td);
971 #endif
972         mtx_unlock(&Giant);
973         return (error);
974 }
975
976 #ifndef _SYS_SYSPROTO_H_
977 struct semop_args {
978         int     semid;
979         struct  sembuf *sops;
980         size_t  nsops;
981 };
982 #endif
983
984 /*
985  * MPSAFE
986  */
987 int
988 semop(struct thread *td, struct semop_args *uap)
989 {
990 #define SMALL_SOPS      8
991         struct sembuf small_sops[SMALL_SOPS];
992         int semid = uap->semid;
993         size_t nsops = uap->nsops;
994         struct sembuf *sops;
995         struct semid_ds *semaptr;
996         struct sembuf *sopptr = 0;
997         struct sem *semptr = 0;
998         struct sem_undo *suptr;
999         struct mtx *sema_mtxp;
1000         size_t i, j, k;
1001         int error;
1002         int do_wakeup, do_undos;
1003
1004         DPRINTF(("call to semop(%d, 0x%x, %u)\n", semid, uap->sops, nsops));
1005
1006         if (!jail_sysvipc_allowed && jailed(td->td_ucred))
1007                 return (ENOSYS);
1008
1009         semid = IPCID_TO_IX(semid);     /* Convert back to zero origin */
1010
1011         if (semid < 0 || semid >= seminfo.semmni)
1012                 return (EINVAL);
1013
1014         /* Allocate memory for sem_ops */
1015         if (nsops <= SMALL_SOPS)
1016                 sops = small_sops;
1017         else if (nsops <= (unsigned long) seminfo.semopm)
1018                 sops = (struct sembuf *) sys_malloc(nsops * sizeof(*sops), M_SEM, M_WAITOK);
1019         else {
1020                 DPRINTF(("too many sops (max=%d, nsops=%d)\n", seminfo.semopm,
1021                     nsops));
1022                 return (E2BIG);
1023         }
1024         if ((error = copyin(uap->sops, sops, nsops * sizeof(sops[0]))) != 0) {
1025                 DPRINTF(("error = %d from copyin(%08x, %08x, %d)\n", error,
1026                     uap->sops, sops, nsops * sizeof(sops[0])));
1027                 if (sops != small_sops)
1028                         sys_free(sops, M_SEM);
1029                 return (error);
1030         }
1031
1032         semaptr = &sema[semid];
1033         sema_mtxp = &sema_mtx[semid];
1034         mtx_lock(sema_mtxp);
1035         if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0) {
1036                 error = EINVAL;
1037                 goto done2;
1038         }
1039         if (semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
1040                 error = EINVAL;
1041                 goto done2;
1042         }
1043         /*
1044          * Initial pass thru sops to see what permissions are needed.
1045          * Also perform any checks that don't need repeating on each
1046          * attempt to satisfy the request vector.
1047          */
1048         j = 0;          /* permission needed */
1049         do_undos = 0;
1050         for (i = 0; i < nsops; i++) {
1051                 sopptr = &sops[i];
1052                 if (sopptr->sem_num >= semaptr->sem_nsems) {
1053                         error = EFBIG;
1054                         goto done2;
1055                 }
1056                 if (sopptr->sem_flg & SEM_UNDO && sopptr->sem_op != 0)
1057                         do_undos = 1;
1058                 j |= (sopptr->sem_op == 0) ? SEM_R : SEM_A;
1059         }
1060
1061         if ((error = ipcperm(td, &semaptr->sem_perm, j))) {
1062                 DPRINTF(("error = %d from ipaccess\n", error));
1063                 goto done2;
1064         }
1065
1066         /*
1067          * Loop trying to satisfy the vector of requests.
1068          * If we reach a point where we must wait, any requests already
1069          * performed are rolled back and we go to sleep until some other
1070          * process wakes us up.  At this point, we start all over again.
1071          *
1072          * This ensures that from the perspective of other tasks, a set
1073          * of requests is atomic (never partially satisfied).
1074          */
1075         for (;;) {
1076                 do_wakeup = 0;
1077                 error = 0;      /* error return if necessary */
1078
1079                 for (i = 0; i < nsops; i++) {
1080                         sopptr = &sops[i];
1081                         semptr = &semaptr->sem_base[sopptr->sem_num];
1082
1083                         DPRINTF((
1084                             "semop: semaptr=%x, sem_base=%x, "
1085                             "semptr=%x, sem[%d]=%d : op=%d, flag=%s\n",
1086                             semaptr, semaptr->sem_base, semptr,
1087                             sopptr->sem_num, semptr->semval, sopptr->sem_op,
1088                             (sopptr->sem_flg & IPC_NOWAIT) ?
1089                             "nowait" : "wait"));
1090
1091                         if (sopptr->sem_op < 0) {
1092                                 if (semptr->semval + sopptr->sem_op < 0) {
1093                                         DPRINTF(("semop:  can't do it now\n"));
1094                                         break;
1095                                 } else {
1096                                         semptr->semval += sopptr->sem_op;
1097                                         if (semptr->semval == 0 &&
1098                                             semptr->semzcnt > 0)
1099                                                 do_wakeup = 1;
1100                                 }
1101                         } else if (sopptr->sem_op == 0) {
1102                                 if (semptr->semval != 0) {
1103                                         DPRINTF(("semop:  not zero now\n"));
1104                                         break;
1105                                 }
1106                         } else if (semptr->semval + sopptr->sem_op >
1107                             seminfo.semvmx) {
1108                                 error = ERANGE;
1109                                 break;
1110                         } else {
1111                                 if (semptr->semncnt > 0)
1112                                         do_wakeup = 1;
1113                                 semptr->semval += sopptr->sem_op;
1114                         }
1115                 }
1116
1117                 /*
1118                  * Did we get through the entire vector?
1119                  */
1120                 if (i >= nsops)
1121                         goto done;
1122
1123                 /*
1124                  * No ... rollback anything that we've already done
1125                  */
1126                 DPRINTF(("semop:  rollback 0 through %d\n", i-1));
1127                 for (j = 0; j < i; j++)
1128                         semaptr->sem_base[sops[j].sem_num].semval -=
1129                             sops[j].sem_op;
1130
1131                 /* If we detected an error, return it */
1132                 if (error != 0)
1133                         goto done2;
1134
1135                 /*
1136                  * If the request that we couldn't satisfy has the
1137                  * NOWAIT flag set then return with EAGAIN.
1138                  */
1139                 if (sopptr->sem_flg & IPC_NOWAIT) {
1140                         error = EAGAIN;
1141                         goto done2;
1142                 }
1143
1144                 if (sopptr->sem_op == 0)
1145                         semptr->semzcnt++;
1146                 else
1147                         semptr->semncnt++;
1148
1149                 DPRINTF(("semop:  good night!\n"));
1150                 error = msleep(semaptr, sema_mtxp, (PZERO - 4) | PCATCH,
1151                     "semwait", 0);
1152                 DPRINTF(("semop:  good morning (error=%d)!\n", error));
1153                 /* return code is checked below, after sem[nz]cnt-- */
1154
1155                 /*
1156                  * Make sure that the semaphore still exists
1157                  */
1158                 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
1159                     semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
1160                         error = EIDRM;
1161                         goto done2;
1162                 }
1163
1164                 /*
1165                  * The semaphore is still alive.  Readjust the count of
1166                  * waiting processes.
1167                  */
1168                 if (sopptr->sem_op == 0)
1169                         semptr->semzcnt--;
1170                 else
1171                         semptr->semncnt--;
1172
1173                 /*
1174                  * Is it really morning, or was our sleep interrupted?
1175                  * (Delayed check of msleep() return code because we
1176                  * need to decrement sem[nz]cnt either way.)
1177                  */
1178                 if (error != 0) {
1179 #ifdef __CYGWIN__
1180                     if (error != EIDRM)
1181 #endif /* __CYGWIN__ */
1182                         error = EINTR;
1183                         goto done2;
1184                 }
1185                 DPRINTF(("semop:  good morning!\n"));
1186         }
1187
1188 done:
1189         /*
1190          * Process any SEM_UNDO requests.
1191          */
1192         if (do_undos) {
1193                 SEMUNDO_LOCK();
1194                 suptr = NULL;
1195                 for (i = 0; i < nsops; i++) {
1196                         /*
1197                          * We only need to deal with SEM_UNDO's for non-zero
1198                          * op's.
1199                          */
1200                         int adjval;
1201
1202                         if ((sops[i].sem_flg & SEM_UNDO) == 0)
1203                                 continue;
1204                         adjval = sops[i].sem_op;
1205                         if (adjval == 0)
1206                                 continue;
1207                         error = semundo_adjust(td, &suptr, semid,
1208                             sops[i].sem_num, -adjval);
1209                         if (error == 0)
1210                                 continue;
1211
1212                         /*
1213                          * Oh-Oh!  We ran out of either sem_undo's or undo's.
1214                          * Rollback the adjustments to this point and then
1215                          * rollback the semaphore ups and down so we can return
1216                          * with an error with all structures restored.  We
1217                          * rollback the undo's in the exact reverse order that
1218                          * we applied them.  This guarantees that we won't run
1219                          * out of space as we roll things back out.
1220                          */
1221                         for (j = 0; j < i; j++) {
1222                                 k = i - j - 1;
1223                                 if ((sops[k].sem_flg & SEM_UNDO) == 0)
1224                                         continue;
1225                                 adjval = sops[k].sem_op;
1226                                 if (adjval == 0)
1227                                         continue;
1228                                 if (semundo_adjust(td, &suptr, semid,
1229                                     sops[k].sem_num, adjval) != 0)
1230                                         panic("semop - can't undo undos");
1231                         }
1232
1233                         for (j = 0; j < nsops; j++)
1234                                 semaptr->sem_base[sops[j].sem_num].semval -=
1235                                     sops[j].sem_op;
1236
1237                         DPRINTF(("error = %d from semundo_adjust\n", error));
1238                         SEMUNDO_UNLOCK();
1239                         goto done2;
1240                 } /* loop through the sops */
1241                 SEMUNDO_UNLOCK();
1242         } /* if (do_undos) */
1243
1244         /* We're definitely done - set the sempid's and time */
1245         for (i = 0; i < nsops; i++) {
1246                 sopptr = &sops[i];
1247                 semptr = &semaptr->sem_base[sopptr->sem_num];
1248                 semptr->sempid = td->td_proc->p_pid;
1249         }
1250         semaptr->sem_otime = time (NULL);
1251
1252         /*
1253          * Do a wakeup if any semaphore was up'd whilst something was
1254          * sleeping on it.
1255          */
1256         if (do_wakeup) {
1257                 DPRINTF(("semop:  doing wakeup\n"));
1258                 wakeup(semaptr);
1259                 DPRINTF(("semop:  back from wakeup\n"));
1260         }
1261         DPRINTF(("semop:  done\n"));
1262         td->td_retval[0] = 0;
1263 done2:
1264         mtx_unlock(sema_mtxp);
1265         if (sops != small_sops)
1266                 sys_free(sops, M_SEM);
1267         return (error);
1268 }
1269
1270 /*
1271  * Go through the undo structures for this process and apply the adjustments to
1272  * semaphores.
1273  */
1274 void
1275 semexit_myhook(void *arg, struct proc *p)
1276 {
1277         struct sem_undo *suptr;
1278         struct sem_undo **supptr;
1279
1280         /*
1281          * Go through the chain of undo vectors looking for one
1282          * associated with this process.
1283          */
1284         SEMUNDO_HOOKLOCK();
1285         SLIST_FOREACH_PREVPTR(suptr, supptr, &semu_list, un_next) {
1286 #ifdef __CYGWIN__
1287                 if (suptr->un_proc == p->winpid)
1288 #else
1289                 if (suptr->un_proc == p)
1290 #endif
1291                         break;
1292         }
1293 #ifndef __CYGWIN__
1294         SEMUNDO_UNLOCK();
1295 #endif
1296
1297         if (suptr == NULL) {
1298                 SEMUNDO_UNLOCK();
1299                 return;
1300         }
1301
1302 #ifdef __CYGWIN__
1303         DPRINTF(("proc @%u(%u) has undo structure with %d entries\n",
1304             p->cygpid, p->winpid, suptr->un_cnt));
1305 #else
1306         DPRINTF(("proc @%08x has undo structure with %d entries\n", p,
1307             suptr->un_cnt));
1308 #endif
1309
1310         /*
1311          * If there are any active undo elements then process them.
1312          */
1313         if (suptr->un_cnt > 0) {
1314                 int ix;
1315
1316                 for (ix = 0; ix < suptr->un_cnt; ix++) {
1317                         int semid = suptr->un_ent[ix].un_id;
1318                         int semnum = suptr->un_ent[ix].un_num;
1319                         int adjval = suptr->un_ent[ix].un_adjval;
1320                         struct semid_ds *semaptr;
1321                         struct mtx *sema_mtxp;
1322
1323                         semaptr = &sema[semid];
1324                         sema_mtxp = &sema_mtx[semid];
1325 #ifdef __CYGWIN__
1326                         _mtx_lock(sema_mtxp, p->winpid, __FILE__, __LINE__);
1327 #else
1328                         mtx_lock(sema_mtxp);
1329                         SEMUNDO_HOOKLOCK();
1330 #endif
1331                         if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
1332                                 panic("semexit - semid not allocated");
1333                         if (semnum >= semaptr->sem_nsems)
1334                                 panic("semexit - semnum out of range");
1335
1336                         DPRINTF((
1337 #ifdef __CYGWIN__
1338                             "semexit:  %u id=%d num=%d(adj=%d) ; sem=%d\n",
1339 #else
1340                             "semexit:  %08x id=%d num=%d(adj=%d) ; sem=%d\n",
1341 #endif
1342                             suptr->un_proc, suptr->un_ent[ix].un_id,
1343                             suptr->un_ent[ix].un_num,
1344                             suptr->un_ent[ix].un_adjval,
1345                             semaptr->sem_base[semnum].semval));
1346
1347                         if (adjval < 0) {
1348                                 if (semaptr->sem_base[semnum].semval < -adjval)
1349                                         semaptr->sem_base[semnum].semval = 0;
1350                                 else
1351                                         semaptr->sem_base[semnum].semval +=
1352                                             adjval;
1353                         } else
1354                                 semaptr->sem_base[semnum].semval += adjval;
1355
1356                         wakeup(semaptr);
1357                         DPRINTF(("semexit:  back from wakeup\n"));
1358                         _mtx_unlock(sema_mtxp, __FILE__, __LINE__);
1359 #ifndef __CYGWIN__
1360                         SEMUNDO_UNLOCK();
1361 #endif
1362                 }
1363         }
1364
1365         /*
1366          * Deallocate the undo vector.
1367          */
1368         DPRINTF(("removing vector (%u)\n", suptr->un_proc));
1369 #ifdef __CYGWIN__
1370         suptr->un_proc = 0;
1371 #else
1372         suptr->un_proc = NULL;
1373 #endif
1374         *supptr = SLIST_NEXT(suptr, un_next);
1375 #ifdef __CYGWIN__
1376         SEMUNDO_UNLOCK();
1377 #endif
1378 }
1379
1380 #ifndef __CYGWIN__
1381 static int
1382 sysctl_sema(SYSCTL_HANDLER_ARGS)
1383 {
1384
1385         return (SYSCTL_OUT(req, sema,
1386             sizeof(struct semid_ds) * seminfo.semmni));
1387 }
1388 #endif /* __CYGWIN__ */
1389 #endif /* __OUTSIDE_CYGWIN__ */