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