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