]> sourceware.org Git - newlib-cygwin.git/blame - winsup/cygserver/sysv_sem.cc
Cygwin: cygserver: build with -Wimplicit-fallthrough=4 -Werror
[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"
fb7331e3 15#include <stdio.h>
282113ba
CV
16#include <sys/cygwin.h>
17#include <sys/cdefs.h>
18#ifndef __FBSDID
19#define __FBSDID(s) const char version[] = (s)
20#endif
c6ef5fb7 21__FBSDID("$FreeBSD: /repoman/r/ncvs/src/sys/kern/sysv_sem.c,v 1.70 2004/05/30 20:34:58 phk Exp $");
e3786825 22/* CV, 2006-01-09: Inspected upstream up to version 1.78. */
282113ba
CV
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"
dafef5e2 40#include <sys/smallprint.h>
282113ba
CV
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
54static int semvalid(int semid, struct semid_ds *semaptr);
55
56static struct sem_undo *semu_alloc(struct thread *td);
57static int semundo_adjust(struct thread *td, struct sem_undo **supptr,
58 int semid, int semnum, int adjval);
dafef5e2 59static void semundo_clear(int semid, int semnum, struct thread *td);
282113ba
CV
60
61#ifndef _SYS_SYSPROTO_H_
62struct __semctl_args;
63int __semctl(struct thread *td, struct __semctl_args *uap);
64struct semget_args;
65int semget(struct thread *td, struct semget_args *uap);
66struct semop_args;
67int 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. */
72static sy_call_t *semcalls[] = {
73 (sy_call_t *)__semctl, (sy_call_t *)semget,
74 (sy_call_t *)semop
75};
76#endif
77
78static struct mtx sem_mtx; /* semaphore global lock */
79static int semtots = 0;
80static int semtot = 0;
81static struct semid_ds *sema; /* semaphore id pool */
82static struct mtx *sema_mtx; /* semaphore id pool mutexes*/
83static struct sem *sem; /* semaphore pool */
43522735 84static SLIST_HEAD(, sem_undo) semu_list; /* list of active undo structures */
282113ba
CV
85static int *semu; /* undo structure pool */
86#ifndef __CYGWIN__
87static 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);
dafef5e2 94#define SEMUNDO_LOCKASSERT(how,pid) mtx_assert(&SEMUNDO_MTX, (how), (pid));
282113ba
CV
95
96struct 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 */
106struct 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
112struct sem_undo {
113 SLIST_ENTRY(sem_undo) un_next; /* ptr to next active undo structure */
dafef5e2
CV
114#ifdef __CYGWIN__
115 DWORD un_proc; /* owner of this structure */
116#else
282113ba 117 struct proc *un_proc; /* owner of this structure */
dafef5e2 118#endif
282113ba
CV
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
52fa622a
CV
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__ */
282113ba
CV
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 */
185struct 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__
199SYSCTL_DECL(_kern_ipc);
200SYSCTL_INT(_kern_ipc, OID_AUTO, semmap, CTLFLAG_RW, &seminfo.semmap, 0, "");
201SYSCTL_INT(_kern_ipc, OID_AUTO, semmni, CTLFLAG_RDTUN, &seminfo.semmni, 0, "");
202SYSCTL_INT(_kern_ipc, OID_AUTO, semmns, CTLFLAG_RDTUN, &seminfo.semmns, 0, "");
203SYSCTL_INT(_kern_ipc, OID_AUTO, semmnu, CTLFLAG_RDTUN, &seminfo.semmnu, 0, "");
204SYSCTL_INT(_kern_ipc, OID_AUTO, semmsl, CTLFLAG_RW, &seminfo.semmsl, 0, "");
205SYSCTL_INT(_kern_ipc, OID_AUTO, semopm, CTLFLAG_RDTUN, &seminfo.semopm, 0, "");
206SYSCTL_INT(_kern_ipc, OID_AUTO, semume, CTLFLAG_RDTUN, &seminfo.semume, 0, "");
207SYSCTL_INT(_kern_ipc, OID_AUTO, semusz, CTLFLAG_RDTUN, &seminfo.semusz, 0, "");
208SYSCTL_INT(_kern_ipc, OID_AUTO, semvmx, CTLFLAG_RW, &seminfo.semvmx, 0, "");
209SYSCTL_INT(_kern_ipc, OID_AUTO, semaem, CTLFLAG_RW, &seminfo.semaem, 0, "");
210SYSCTL_PROC(_kern_ipc, OID_AUTO, sema, CTLFLAG_RD,
211 NULL, 0, sysctl_sema, "", "");
212#endif /* __CYGWIN__ */
213
214void
215seminit(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;
1d88f8ce 247 sema[i].sem_perm.seq = 0;
282113ba
CV
248 }
249 for (i = 0; i < seminfo.semmni; i++)
dafef5e2 250 {
fb7331e3 251 char *buf = (char *) sys_malloc(16, M_SEM, M_WAITOK);
6497fdfa 252 snprintf(buf, 16, "semid[%d]", (short) i);
dafef5e2
CV
253 mtx_init(&sema_mtx[i], buf, NULL, MTX_DEF);
254 }
282113ba
CV
255 for (i = 0; i < seminfo.semmnu; i++) {
256 struct sem_undo *suptr = SEMU(i);
dafef5e2
CV
257#ifdef __CYGWIN__
258 suptr->un_proc = 0;
259#else
282113ba 260 suptr->un_proc = NULL;
dafef5e2 261#endif
282113ba
CV
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
271int
272semunload(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);
fb7331e3
CV
284 for (int i = 0; i < seminfo.semmni; i++) {
285 sys_free((void *) sema_mtx[i].name, M_SEM);
282113ba 286 mtx_destroy(&sema_mtx[i]);
fb7331e3 287 }
282113ba
CV
288 mtx_destroy(&sem_mtx);
289 return (0);
290}
291
292#ifndef __CYGWIN__
293static int
294sysvsem_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
314static moduledata_t sysvsem_mod = {
315 "sysvsem",
316 &sysvsem_modload,
317 NULL
318};
319
320SYSCALL_MODULE_HELPER(semsys);
321SYSCALL_MODULE_HELPER(__semctl);
322SYSCALL_MODULE_HELPER(semget);
323SYSCALL_MODULE_HELPER(semop);
324
325DECLARE_MODULE(sysvsem, sysvsem_mod,
326 SI_SUB_SYSV_SEM, SI_ORDER_FIRST);
327MODULE_VERSION(sysvsem, 1);
328
329/*
330 * Entry point for all SEM calls
331 *
332 * MPSAFE
333 */
334int
335semsys(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
363static struct sem_undo *
364semu_alloc(struct thread *td)
365{
366 int i;
367 struct sem_undo *suptr;
368 struct sem_undo **supptr;
369 int attempt;
370
dafef5e2 371 SEMUNDO_LOCKASSERT(MA_OWNED, td->td_proc->winpid);
282113ba
CV
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);
dafef5e2
CV
386#ifdef __CYGWIN__
387 if (suptr->un_proc == 0) {
388#else
282113ba 389 if (suptr->un_proc == NULL) {
dafef5e2 390#endif
282113ba
CV
391 SLIST_INSERT_HEAD(&semu_list, suptr, un_next);
392 suptr->un_cnt = 0;
dafef5e2 393 suptr->un_proc = td->td_proc->winpid;
282113ba
CV
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) {
dafef5e2
CV
410#ifdef __CYGWIN__
411 suptr->un_proc = 0;
412#else
282113ba 413 suptr->un_proc = NULL;
dafef5e2 414#endif
282113ba
CV
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
440static int
441semundo_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
dafef5e2 449 SEMUNDO_LOCKASSERT(MA_OWNED, td->td_proc->winpid);
282113ba
CV
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) {
ddb1a4c1 456#ifdef __CYGWIN__
dafef5e2 457 if (suptr->un_proc == p->winpid) {
ddb1a4c1 458#else
282113ba 459 if (suptr->un_proc == p) {
ddb1a4c1 460#endif
282113ba
CV
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
513static void
dafef5e2 514semundo_clear(int semid, int semnum, struct thread *td)
282113ba
CV
515{
516 struct sem_undo *suptr;
517
dafef5e2 518 SEMUNDO_LOCKASSERT(MA_OWNED, td->td_proc->winpid);
282113ba
CV
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 }
102bf650
CV
532 if (semnum != -1)
533 break;
282113ba 534 }
282113ba
CV
535 }
536 i++, sunptr++;
537 }
538 }
539}
540
541static int
542semvalid(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_
553struct __semctl_args {
554 int semid;
555 int semnum;
556 int cmd;
557 union semun *arg;
558};
559#endif
560
561/*
562 * MPSAFE
563 */
564int
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();
dafef5e2 675 semundo_clear(semid, -1, td);
282113ba
CV
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();
dafef5e2 798 semundo_clear(semid, semnum, td);
282113ba
CV
799 SEMUNDO_UNLOCK();
800 wakeup(semaptr);
801 break;
802
803 case SETALL:
804 mtx_lock(sema_mtxp);
805raced:
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);
e3786825 813 error = copyin(real_arg.array, array, count * sizeof(*array));
282113ba
CV
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();
dafef5e2 836 semundo_clear(semid, -1, td);
282113ba
CV
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;
848done2:
dafef5e2 849 if (mtx_owned(sema_mtxp, td->td_proc->winpid))
282113ba
CV
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_
857struct semget_args {
858 key_t key;
859 int nsems;
860 int semflg;
861};
862#endif
863
864/*
865 * MPSAFE
866 */
867int
868semget(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
62688407 878 DPRINTF(("semget(0x%llx, %d, 0%o)\n", key, nsems, semflg));
282113ba
CV
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
965found:
966 td->td_retval[0] = IXSEQ_TO_IPCID(semid, sema[semid].sem_perm);
967done2:
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_
977struct semop_args {
978 int semid;
979 struct sembuf *sops;
980 size_t nsops;
981};
982#endif
983
984/*
985 * MPSAFE
986 */
987int
988semop(struct thread *td, struct semop_args *uap)
989{
c6ef5fb7
CV
990#define SMALL_SOPS 8
991 struct sembuf small_sops[SMALL_SOPS];
282113ba
CV
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 */
c6ef5fb7
CV
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 {
282113ba
CV
1020 DPRINTF(("too many sops (max=%d, nsops=%d)\n", seminfo.semopm,
1021 nsops));
1022 return (E2BIG);
1023 }
282113ba
CV
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])));
c6ef5fb7
CV
1027 if (sops != small_sops)
1028 sys_free(sops, M_SEM);
282113ba
CV
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((
dafef5e2 1084 "semop: semaptr=%x, sem_base=%x, "
282113ba
CV
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));
c6ef5fb7 1153 /* return code is checked below, after sem[nz]cnt-- */
282113ba
CV
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--;
c6ef5fb7
CV
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__
6699e21f
PF
1180 if (error == EIDRM)
1181 goto done2;
c6ef5fb7
CV
1182#endif /* __CYGWIN__ */
1183 error = EINTR;
1184 goto done2;
1185 }
1186 DPRINTF(("semop: good morning!\n"));
282113ba
CV
1187 }
1188
1189done:
1190 /*
1191 * Process any SEM_UNDO requests.
1192 */
1193 if (do_undos) {
1194 SEMUNDO_LOCK();
1195 suptr = NULL;
1196 for (i = 0; i < nsops; i++) {
1197 /*
1198 * We only need to deal with SEM_UNDO's for non-zero
1199 * op's.
1200 */
1201 int adjval;
1202
1203 if ((sops[i].sem_flg & SEM_UNDO) == 0)
1204 continue;
1205 adjval = sops[i].sem_op;
1206 if (adjval == 0)
1207 continue;
1208 error = semundo_adjust(td, &suptr, semid,
1209 sops[i].sem_num, -adjval);
1210 if (error == 0)
1211 continue;
1212
1213 /*
1214 * Oh-Oh! We ran out of either sem_undo's or undo's.
1215 * Rollback the adjustments to this point and then
1216 * rollback the semaphore ups and down so we can return
1217 * with an error with all structures restored. We
1218 * rollback the undo's in the exact reverse order that
1219 * we applied them. This guarantees that we won't run
1220 * out of space as we roll things back out.
1221 */
1222 for (j = 0; j < i; j++) {
1223 k = i - j - 1;
1224 if ((sops[k].sem_flg & SEM_UNDO) == 0)
1225 continue;
1226 adjval = sops[k].sem_op;
1227 if (adjval == 0)
1228 continue;
1229 if (semundo_adjust(td, &suptr, semid,
1230 sops[k].sem_num, adjval) != 0)
1231 panic("semop - can't undo undos");
1232 }
1233
1234 for (j = 0; j < nsops; j++)
1235 semaptr->sem_base[sops[j].sem_num].semval -=
1236 sops[j].sem_op;
1237
1238 DPRINTF(("error = %d from semundo_adjust\n", error));
1239 SEMUNDO_UNLOCK();
1240 goto done2;
1241 } /* loop through the sops */
1242 SEMUNDO_UNLOCK();
1243 } /* if (do_undos) */
1244
1245 /* We're definitely done - set the sempid's and time */
1246 for (i = 0; i < nsops; i++) {
1247 sopptr = &sops[i];
1248 semptr = &semaptr->sem_base[sopptr->sem_num];
1249 semptr->sempid = td->td_proc->p_pid;
1250 }
1251 semaptr->sem_otime = time (NULL);
1252
1253 /*
1254 * Do a wakeup if any semaphore was up'd whilst something was
1255 * sleeping on it.
1256 */
1257 if (do_wakeup) {
1258 DPRINTF(("semop: doing wakeup\n"));
1259 wakeup(semaptr);
1260 DPRINTF(("semop: back from wakeup\n"));
1261 }
1262 DPRINTF(("semop: done\n"));
1263 td->td_retval[0] = 0;
1264done2:
1265 mtx_unlock(sema_mtxp);
c6ef5fb7
CV
1266 if (sops != small_sops)
1267 sys_free(sops, M_SEM);
282113ba
CV
1268 return (error);
1269}
1270
1271/*
1272 * Go through the undo structures for this process and apply the adjustments to
1273 * semaphores.
1274 */
1275void
1276semexit_myhook(void *arg, struct proc *p)
1277{
1278 struct sem_undo *suptr;
1279 struct sem_undo **supptr;
1280
282113ba
CV
1281 /*
1282 * Go through the chain of undo vectors looking for one
1283 * associated with this process.
1284 */
1285 SEMUNDO_HOOKLOCK();
1286 SLIST_FOREACH_PREVPTR(suptr, supptr, &semu_list, un_next) {
ddb1a4c1 1287#ifdef __CYGWIN__
dafef5e2 1288 if (suptr->un_proc == p->winpid)
ddb1a4c1 1289#else
282113ba 1290 if (suptr->un_proc == p)
ddb1a4c1 1291#endif
282113ba
CV
1292 break;
1293 }
dafef5e2 1294#ifndef __CYGWIN__
282113ba 1295 SEMUNDO_UNLOCK();
dafef5e2 1296#endif
282113ba 1297
dafef5e2
CV
1298 if (suptr == NULL) {
1299 SEMUNDO_UNLOCK();
282113ba 1300 return;
dafef5e2 1301 }
282113ba 1302
ddb1a4c1
CV
1303#ifdef __CYGWIN__
1304 DPRINTF(("proc @%u(%u) has undo structure with %d entries\n",
1305 p->cygpid, p->winpid, suptr->un_cnt));
1306#else
282113ba
CV
1307 DPRINTF(("proc @%08x has undo structure with %d entries\n", p,
1308 suptr->un_cnt));
ddb1a4c1 1309#endif
282113ba
CV
1310
1311 /*
1312 * If there are any active undo elements then process them.
1313 */
1314 if (suptr->un_cnt > 0) {
1315 int ix;
1316
1317 for (ix = 0; ix < suptr->un_cnt; ix++) {
1318 int semid = suptr->un_ent[ix].un_id;
1319 int semnum = suptr->un_ent[ix].un_num;
1320 int adjval = suptr->un_ent[ix].un_adjval;
1321 struct semid_ds *semaptr;
1322 struct mtx *sema_mtxp;
1323
1324 semaptr = &sema[semid];
1325 sema_mtxp = &sema_mtx[semid];
1326#ifdef __CYGWIN__
1327 _mtx_lock(sema_mtxp, p->winpid, __FILE__, __LINE__);
1328#else
1329 mtx_lock(sema_mtxp);
282113ba 1330 SEMUNDO_HOOKLOCK();
dafef5e2 1331#endif
282113ba
CV
1332 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
1333 panic("semexit - semid not allocated");
1334 if (semnum >= semaptr->sem_nsems)
1335 panic("semexit - semnum out of range");
1336
1337 DPRINTF((
ddb1a4c1 1338#ifdef __CYGWIN__
dafef5e2 1339 "semexit: %u id=%d num=%d(adj=%d) ; sem=%d\n",
ddb1a4c1 1340#else
282113ba 1341 "semexit: %08x id=%d num=%d(adj=%d) ; sem=%d\n",
ddb1a4c1 1342#endif
dafef5e2 1343 suptr->un_proc, suptr->un_ent[ix].un_id,
282113ba
CV
1344 suptr->un_ent[ix].un_num,
1345 suptr->un_ent[ix].un_adjval,
1346 semaptr->sem_base[semnum].semval));
1347
1348 if (adjval < 0) {
1349 if (semaptr->sem_base[semnum].semval < -adjval)
1350 semaptr->sem_base[semnum].semval = 0;
1351 else
1352 semaptr->sem_base[semnum].semval +=
1353 adjval;
1354 } else
1355 semaptr->sem_base[semnum].semval += adjval;
1356
1357 wakeup(semaptr);
1358 DPRINTF(("semexit: back from wakeup\n"));
dafef5e2
CV
1359 _mtx_unlock(sema_mtxp, __FILE__, __LINE__);
1360#ifndef __CYGWIN__
282113ba 1361 SEMUNDO_UNLOCK();
dafef5e2 1362#endif
282113ba
CV
1363 }
1364 }
1365
1366 /*
1367 * Deallocate the undo vector.
1368 */
dafef5e2
CV
1369 DPRINTF(("removing vector (%u)\n", suptr->un_proc));
1370#ifdef __CYGWIN__
1371 suptr->un_proc = 0;
1372#else
282113ba 1373 suptr->un_proc = NULL;
dafef5e2 1374#endif
282113ba 1375 *supptr = SLIST_NEXT(suptr, un_next);
dafef5e2
CV
1376#ifdef __CYGWIN__
1377 SEMUNDO_UNLOCK();
1378#endif
282113ba
CV
1379}
1380
1381#ifndef __CYGWIN__
1382static int
1383sysctl_sema(SYSCTL_HANDLER_ARGS)
1384{
1385
1386 return (SYSCTL_OUT(req, sema,
1387 sizeof(struct semid_ds) * seminfo.semmni));
1388}
1389#endif /* __CYGWIN__ */
1390#endif /* __OUTSIDE_CYGWIN__ */
This page took 0.319487 seconds and 5 git commands to generate.