1 /* $Id: sysv_sem.c,v 1.14 1995/12/28 01:31:38 jkh Exp $ */ 2 3 /* 4 * Implementation of SVID semaphores 5 * 6 * Author: Daniel Boulet 7 * 8 * This software is provided ``AS IS'' without any warranties of any kind. 9 */ 10 11 #include "opt_sysvipc.h" 12 13 #include <sys/param.h> 14 #include <sys/systm.h> 15 #include <sys/sysproto.h> 16 #include <sys/kernel.h> 17 #include <sys/proc.h> 18 #include <sys/sem.h> 19 #include <sys/sysent.h> 20 21 static void seminit __P((void *)); 22 SYSINIT(sysv_sem, SI_SUB_SYSV_SEM, SI_ORDER_FIRST, seminit, NULL) 23 24 #ifndef _SYS_SYSPROTO_H_ 25 struct __semctl_args; 26 int __semctl __P((struct proc *p, struct __semctl_args *uap, int *retval)); 27 struct semget_args; 28 int semget __P((struct proc *p, struct semget_args *uap, int *retval)); 29 struct semop_args; 30 int semop __P((struct proc *p, struct semop_args *uap, int *retval)); 31 struct semconfig_args; 32 int semconfig __P((struct proc *p, struct semconfig_args *uap, 33 int *retval)); 34 #endif 35 36 static struct sem_undo *semu_alloc __P((struct proc *p)); 37 static int semundo_adjust __P((struct proc *p, struct sem_undo **supptr, 38 int semid, int semnum, int adjval)); 39 static void semundo_clear __P((int semid, int semnum)); 40 41 /* XXX casting to (sy_call_t *) is bogus, as usual. */ 42 static sy_call_t *semcalls[] = { 43 (sy_call_t *)__semctl, (sy_call_t *)semget, 44 (sy_call_t *)semop, (sy_call_t *)semconfig 45 }; 46 47 static int semtot = 0; 48 struct semid_ds *sema; /* semaphore id pool */ 49 struct sem *sem; /* semaphore pool */ 50 static struct map *semmap; /* semaphore allocation map */ 51 static struct sem_undo *semu_list; /* list of active undo structures */ 52 int *semu; /* undo structure pool */ 53 54 static struct proc *semlock_holder = NULL; 55 56 void 57 seminit(dummy) 58 void *dummy; 59 { 60 register int i; 61 62 if (sema == NULL) 63 panic("sema is NULL"); 64 if (semu == NULL) 65 panic("semu is NULL"); 66 67 for (i = 0; i < seminfo.semmni; i++) { 68 sema[i].sem_base = 0; 69 sema[i].sem_perm.mode = 0; 70 } 71 for (i = 0; i < seminfo.semmnu; i++) { 72 register struct sem_undo *suptr = SEMU(i); 73 suptr->un_proc = NULL; 74 } 75 semu_list = NULL; 76 } 77 78 /* 79 * Entry point for all SEM calls 80 */ 81 int 82 semsys(p, uap, retval) 83 struct proc *p; 84 /* XXX actually varargs. */ 85 struct semsys_args /* { 86 u_int which; 87 int a2; 88 int a3; 89 int a4; 90 int a5; 91 } */ *uap; 92 int *retval; 93 { 94 95 while (semlock_holder != NULL && semlock_holder != p) 96 (void) tsleep((caddr_t)&semlock_holder, (PZERO - 4), "semsys", 0); 97 98 if (uap->which >= sizeof(semcalls)/sizeof(semcalls[0])) 99 return (EINVAL); 100 return ((*semcalls[uap->which])(p, &uap->a2, retval)); 101 } 102 103 /* 104 * Lock or unlock the entire semaphore facility. 105 * 106 * This will probably eventually evolve into a general purpose semaphore 107 * facility status enquiry mechanism (I don't like the "read /dev/kmem" 108 * approach currently taken by ipcs and the amount of info that we want 109 * to be able to extract for ipcs is probably beyond what the capability 110 * of the getkerninfo facility. 111 * 112 * At the time that the current version of semconfig was written, ipcs is 113 * the only user of the semconfig facility. It uses it to ensure that the 114 * semaphore facility data structures remain static while it fishes around 115 * in /dev/kmem. 116 */ 117 118 #ifndef _SYS_SYSPROTO_H_ 119 struct semconfig_args { 120 semconfig_ctl_t flag; 121 }; 122 #endif 123 124 int 125 semconfig(p, uap, retval) 126 struct proc *p; 127 struct semconfig_args *uap; 128 int *retval; 129 { 130 int eval = 0; 131 132 switch (uap->flag) { 133 case SEM_CONFIG_FREEZE: 134 semlock_holder = p; 135 break; 136 137 case SEM_CONFIG_THAW: 138 semlock_holder = NULL; 139 wakeup((caddr_t)&semlock_holder); 140 break; 141 142 default: 143 printf("semconfig: unknown flag parameter value (%d) - ignored\n", 144 uap->flag); 145 eval = EINVAL; 146 break; 147 } 148 149 *retval = 0; 150 return(eval); 151 } 152 153 /* 154 * Allocate a new sem_undo structure for a process 155 * (returns ptr to structure or NULL if no more room) 156 */ 157 158 static struct sem_undo * 159 semu_alloc(p) 160 struct proc *p; 161 { 162 register int i; 163 register struct sem_undo *suptr; 164 register struct sem_undo **supptr; 165 int attempt; 166 167 /* 168 * Try twice to allocate something. 169 * (we'll purge any empty structures after the first pass so 170 * two passes are always enough) 171 */ 172 173 for (attempt = 0; attempt < 2; attempt++) { 174 /* 175 * Look for a free structure. 176 * Fill it in and return it if we find one. 177 */ 178 179 for (i = 0; i < seminfo.semmnu; i++) { 180 suptr = SEMU(i); 181 if (suptr->un_proc == NULL) { 182 suptr->un_next = semu_list; 183 semu_list = suptr; 184 suptr->un_cnt = 0; 185 suptr->un_proc = p; 186 return(suptr); 187 } 188 } 189 190 /* 191 * We didn't find a free one, if this is the first attempt 192 * then try to free some structures. 193 */ 194 195 if (attempt == 0) { 196 /* All the structures are in use - try to free some */ 197 int did_something = 0; 198 199 supptr = &semu_list; 200 while ((suptr = *supptr) != NULL) { 201 if (suptr->un_cnt == 0) { 202 suptr->un_proc = NULL; 203 *supptr = suptr->un_next; 204 did_something = 1; 205 } else 206 supptr = &(suptr->un_next); 207 } 208 209 /* If we didn't free anything then just give-up */ 210 if (!did_something) 211 return(NULL); 212 } else { 213 /* 214 * The second pass failed even though we freed 215 * something after the first pass! 216 * This is IMPOSSIBLE! 217 */ 218 panic("semu_alloc - second attempt failed"); 219 } 220 } 221 return (NULL); 222 } 223 224 /* 225 * Adjust a particular entry for a particular proc 226 */ 227 228 static int 229 semundo_adjust(p, supptr, semid, semnum, adjval) 230 register struct proc *p; 231 struct sem_undo **supptr; 232 int semid, semnum; 233 int adjval; 234 { 235 register struct sem_undo *suptr; 236 register struct undo *sunptr; 237 int i; 238 239 /* Look for and remember the sem_undo if the caller doesn't provide 240 it */ 241 242 suptr = *supptr; 243 if (suptr == NULL) { 244 for (suptr = semu_list; suptr != NULL; 245 suptr = suptr->un_next) { 246 if (suptr->un_proc == p) { 247 *supptr = suptr; 248 break; 249 } 250 } 251 if (suptr == NULL) { 252 if (adjval == 0) 253 return(0); 254 suptr = semu_alloc(p); 255 if (suptr == NULL) 256 return(ENOSPC); 257 *supptr = suptr; 258 } 259 } 260 261 /* 262 * Look for the requested entry and adjust it (delete if adjval becomes 263 * 0). 264 */ 265 sunptr = &suptr->un_ent[0]; 266 for (i = 0; i < suptr->un_cnt; i++, sunptr++) { 267 if (sunptr->un_id != semid || sunptr->un_num != semnum) 268 continue; 269 if (adjval == 0) 270 sunptr->un_adjval = 0; 271 else 272 sunptr->un_adjval += adjval; 273 if (sunptr->un_adjval == 0) { 274 suptr->un_cnt--; 275 if (i < suptr->un_cnt) 276 suptr->un_ent[i] = 277 suptr->un_ent[suptr->un_cnt]; 278 } 279 return(0); 280 } 281 282 /* Didn't find the right entry - create it */ 283 if (adjval == 0) 284 return(0); 285 if (suptr->un_cnt != SEMUME) { 286 sunptr = &suptr->un_ent[suptr->un_cnt]; 287 suptr->un_cnt++; 288 sunptr->un_adjval = adjval; 289 sunptr->un_id = semid; sunptr->un_num = semnum; 290 } else 291 return(EINVAL); 292 return(0); 293 } 294 295 static void 296 semundo_clear(semid, semnum) 297 int semid, semnum; 298 { 299 register struct sem_undo *suptr; 300 301 for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next) { 302 register struct undo *sunptr = &suptr->un_ent[0]; 303 register int i = 0; 304 305 while (i < suptr->un_cnt) { 306 if (sunptr->un_id == semid) { 307 if (semnum == -1 || sunptr->un_num == semnum) { 308 suptr->un_cnt--; 309 if (i < suptr->un_cnt) { 310 suptr->un_ent[i] = 311 suptr->un_ent[suptr->un_cnt]; 312 continue; 313 } 314 } 315 if (semnum != -1) 316 break; 317 } 318 i++, sunptr++; 319 } 320 } 321 } 322 323 /* 324 * Note that the user-mode half of this passes a union, not a pointer 325 */ 326 #ifndef _SYS_SYSPROTO_H_ 327 struct __semctl_args { 328 int semid; 329 int semnum; 330 int cmd; 331 union semun *arg; 332 }; 333 #endif 334 335 int 336 __semctl(p, uap, retval) 337 struct proc *p; 338 register struct __semctl_args *uap; 339 int *retval; 340 { 341 int semid = uap->semid; 342 int semnum = uap->semnum; 343 int cmd = uap->cmd; 344 union semun *arg = uap->arg; 345 union semun real_arg; 346 struct ucred *cred = p->p_ucred; 347 int i, rval, eval; 348 struct semid_ds sbuf; 349 register struct semid_ds *semaptr; 350 351 #ifdef SEM_DEBUG 352 printf("call to semctl(%d, %d, %d, 0x%x)\n", semid, semnum, cmd, arg); 353 #endif 354 355 semid = IPCID_TO_IX(semid); 356 if (semid < 0 || semid >= seminfo.semmsl) 357 return(EINVAL); 358 359 semaptr = &sema[semid]; 360 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 || 361 semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) 362 return(EINVAL); 363 364 eval = 0; 365 rval = 0; 366 367 switch (cmd) { 368 case IPC_RMID: 369 if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_M))) 370 return(eval); 371 semaptr->sem_perm.cuid = cred->cr_uid; 372 semaptr->sem_perm.uid = cred->cr_uid; 373 semtot -= semaptr->sem_nsems; 374 for (i = semaptr->sem_base - sem; i < semtot; i++) 375 sem[i] = sem[i + semaptr->sem_nsems]; 376 for (i = 0; i < seminfo.semmni; i++) { 377 if ((sema[i].sem_perm.mode & SEM_ALLOC) && 378 sema[i].sem_base > semaptr->sem_base) 379 sema[i].sem_base -= semaptr->sem_nsems; 380 } 381 semaptr->sem_perm.mode = 0; 382 semundo_clear(semid, -1); 383 wakeup((caddr_t)semaptr); 384 break; 385 386 case IPC_SET: 387 if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_M))) 388 return(eval); 389 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0) 390 return(eval); 391 if ((eval = copyin(real_arg.buf, (caddr_t)&sbuf, 392 sizeof(sbuf))) != 0) 393 return(eval); 394 semaptr->sem_perm.uid = sbuf.sem_perm.uid; 395 semaptr->sem_perm.gid = sbuf.sem_perm.gid; 396 semaptr->sem_perm.mode = (semaptr->sem_perm.mode & ~0777) | 397 (sbuf.sem_perm.mode & 0777); 398 semaptr->sem_ctime = time.tv_sec; 399 break; 400 401 case IPC_STAT: 402 if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R))) 403 return(eval); 404 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0) 405 return(eval); 406 eval = copyout((caddr_t)semaptr, real_arg.buf, 407 sizeof(struct semid_ds)); 408 break; 409 410 case GETNCNT: 411 if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R))) 412 return(eval); 413 if (semnum < 0 || semnum >= semaptr->sem_nsems) 414 return(EINVAL); 415 rval = semaptr->sem_base[semnum].semncnt; 416 break; 417 418 case GETPID: 419 if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R))) 420 return(eval); 421 if (semnum < 0 || semnum >= semaptr->sem_nsems) 422 return(EINVAL); 423 rval = semaptr->sem_base[semnum].sempid; 424 break; 425 426 case GETVAL: 427 if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R))) 428 return(eval); 429 if (semnum < 0 || semnum >= semaptr->sem_nsems) 430 return(EINVAL); 431 rval = semaptr->sem_base[semnum].semval; 432 break; 433 434 case GETALL: 435 if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R))) 436 return(eval); 437 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0) 438 return(eval); 439 for (i = 0; i < semaptr->sem_nsems; i++) { 440 eval = copyout((caddr_t)&semaptr->sem_base[i].semval, 441 &real_arg.array[i], sizeof(real_arg.array[0])); 442 if (eval != 0) 443 break; 444 } 445 break; 446 447 case GETZCNT: 448 if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R))) 449 return(eval); 450 if (semnum < 0 || semnum >= semaptr->sem_nsems) 451 return(EINVAL); 452 rval = semaptr->sem_base[semnum].semzcnt; 453 break; 454 455 case SETVAL: 456 if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_W))) 457 return(eval); 458 if (semnum < 0 || semnum >= semaptr->sem_nsems) 459 return(EINVAL); 460 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0) 461 return(eval); 462 semaptr->sem_base[semnum].semval = real_arg.val; 463 semundo_clear(semid, semnum); 464 wakeup((caddr_t)semaptr); 465 break; 466 467 case SETALL: 468 if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_W))) 469 return(eval); 470 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0) 471 return(eval); 472 for (i = 0; i < semaptr->sem_nsems; i++) { 473 eval = copyin(&real_arg.array[i], 474 (caddr_t)&semaptr->sem_base[i].semval, 475 sizeof(real_arg.array[0])); 476 if (eval != 0) 477 break; 478 } 479 semundo_clear(semid, -1); 480 wakeup((caddr_t)semaptr); 481 break; 482 483 default: 484 return(EINVAL); 485 } 486 487 if (eval == 0) 488 *retval = rval; 489 return(eval); 490 } 491 492 #ifndef _SYS_SYSPROTO_H_ 493 struct semget_args { 494 key_t key; 495 int nsems; 496 int semflg; 497 }; 498 #endif 499 500 int 501 semget(p, uap, retval) 502 struct proc *p; 503 register struct semget_args *uap; 504 int *retval; 505 { 506 int semid, eval; 507 int key = uap->key; 508 int nsems = uap->nsems; 509 int semflg = uap->semflg; 510 struct ucred *cred = p->p_ucred; 511 512 #ifdef SEM_DEBUG 513 printf("semget(0x%x, %d, 0%o)\n", key, nsems, semflg); 514 #endif 515 516 if (key != IPC_PRIVATE) { 517 for (semid = 0; semid < seminfo.semmni; semid++) { 518 if ((sema[semid].sem_perm.mode & SEM_ALLOC) && 519 sema[semid].sem_perm.key == key) 520 break; 521 } 522 if (semid < seminfo.semmni) { 523 #ifdef SEM_DEBUG 524 printf("found public key\n"); 525 #endif 526 if ((eval = ipcperm(cred, &sema[semid].sem_perm, 527 semflg & 0700))) 528 return(eval); 529 if (nsems > 0 && sema[semid].sem_nsems < nsems) { 530 #ifdef SEM_DEBUG 531 printf("too small\n"); 532 #endif 533 return(EINVAL); 534 } 535 if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) { 536 #ifdef SEM_DEBUG 537 printf("not exclusive\n"); 538 #endif 539 return(EEXIST); 540 } 541 goto found; 542 } 543 } 544 545 #ifdef SEM_DEBUG 546 printf("need to allocate the semid_ds\n"); 547 #endif 548 if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) { 549 if (nsems <= 0 || nsems > seminfo.semmsl) { 550 #ifdef SEM_DEBUG 551 printf("nsems out of range (0<%d<=%d)\n", nsems, 552 seminfo.semmsl); 553 #endif 554 return(EINVAL); 555 } 556 if (nsems > seminfo.semmns - semtot) { 557 #ifdef SEM_DEBUG 558 printf("not enough semaphores left (need %d, got %d)\n", 559 nsems, seminfo.semmns - semtot); 560 #endif 561 return(ENOSPC); 562 } 563 for (semid = 0; semid < seminfo.semmni; semid++) { 564 if ((sema[semid].sem_perm.mode & SEM_ALLOC) == 0) 565 break; 566 } 567 if (semid == seminfo.semmni) { 568 #ifdef SEM_DEBUG 569 printf("no more semid_ds's available\n"); 570 #endif 571 return(ENOSPC); 572 } 573 #ifdef SEM_DEBUG 574 printf("semid %d is available\n", semid); 575 #endif 576 sema[semid].sem_perm.key = key; 577 sema[semid].sem_perm.cuid = cred->cr_uid; 578 sema[semid].sem_perm.uid = cred->cr_uid; 579 sema[semid].sem_perm.cgid = cred->cr_gid; 580 sema[semid].sem_perm.gid = cred->cr_gid; 581 sema[semid].sem_perm.mode = (semflg & 0777) | SEM_ALLOC; 582 sema[semid].sem_perm.seq = 583 (sema[semid].sem_perm.seq + 1) & 0x7fff; 584 sema[semid].sem_nsems = nsems; 585 sema[semid].sem_otime = 0; 586 sema[semid].sem_ctime = time.tv_sec; 587 sema[semid].sem_base = &sem[semtot]; 588 semtot += nsems; 589 bzero(sema[semid].sem_base, 590 sizeof(sema[semid].sem_base[0])*nsems); 591 #ifdef SEM_DEBUG 592 printf("sembase = 0x%x, next = 0x%x\n", sema[semid].sem_base, 593 &sem[semtot]); 594 #endif 595 } else { 596 #ifdef SEM_DEBUG 597 printf("didn't find it and wasn't asked to create it\n"); 598 #endif 599 return(ENOENT); 600 } 601 602 found: 603 *retval = IXSEQ_TO_IPCID(semid, sema[semid].sem_perm); 604 return(0); 605 } 606 607 #ifndef _SYS_SYSPROTO_H_ 608 struct semop_args { 609 int semid; 610 struct sembuf *sops; 611 int nsops; 612 }; 613 #endif 614 615 int 616 semop(p, uap, retval) 617 struct proc *p; 618 register struct semop_args *uap; 619 int *retval; 620 { 621 int semid = uap->semid; 622 int nsops = uap->nsops; 623 struct sembuf sops[MAX_SOPS]; 624 register struct semid_ds *semaptr; 625 register struct sembuf *sopptr; 626 register struct sem *semptr; 627 struct sem_undo *suptr = NULL; 628 struct ucred *cred = p->p_ucred; 629 int i, j, eval; 630 int do_wakeup, do_undos; 631 632 #ifdef SEM_DEBUG 633 printf("call to semop(%d, 0x%x, %d)\n", semid, sops, nsops); 634 #endif 635 636 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */ 637 638 if (semid < 0 || semid >= seminfo.semmsl) 639 return(EINVAL); 640 641 semaptr = &sema[semid]; 642 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0) 643 return(EINVAL); 644 if (semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) 645 return(EINVAL); 646 647 if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_W))) { 648 #ifdef SEM_DEBUG 649 printf("eval = %d from ipaccess\n", eval); 650 #endif 651 return(eval); 652 } 653 654 if (nsops > MAX_SOPS) { 655 #ifdef SEM_DEBUG 656 printf("too many sops (max=%d, nsops=%d)\n", MAX_SOPS, nsops); 657 #endif 658 return(E2BIG); 659 } 660 661 if ((eval = copyin(uap->sops, &sops, nsops * sizeof(sops[0]))) != 0) { 662 #ifdef SEM_DEBUG 663 printf("eval = %d from copyin(%08x, %08x, %d)\n", eval, 664 uap->sops, &sops, nsops * sizeof(sops[0])); 665 #endif 666 return(eval); 667 } 668 669 /* 670 * Loop trying to satisfy the vector of requests. 671 * If we reach a point where we must wait, any requests already 672 * performed are rolled back and we go to sleep until some other 673 * process wakes us up. At this point, we start all over again. 674 * 675 * This ensures that from the perspective of other tasks, a set 676 * of requests is atomic (never partially satisfied). 677 */ 678 do_undos = 0; 679 680 for (;;) { 681 do_wakeup = 0; 682 683 for (i = 0; i < nsops; i++) { 684 sopptr = &sops[i]; 685 686 if (sopptr->sem_num >= semaptr->sem_nsems) 687 return(EFBIG); 688 689 semptr = &semaptr->sem_base[sopptr->sem_num]; 690 691 #ifdef SEM_DEBUG 692 printf("semop: semaptr=%x, sem_base=%x, semptr=%x, sem[%d]=%d : op=%d, flag=%s\n", 693 semaptr, semaptr->sem_base, semptr, 694 sopptr->sem_num, semptr->semval, sopptr->sem_op, 695 (sopptr->sem_flg & IPC_NOWAIT) ? "nowait" : "wait"); 696 #endif 697 698 if (sopptr->sem_op < 0) { 699 if (semptr->semval + sopptr->sem_op < 0) { 700 #ifdef SEM_DEBUG 701 printf("semop: can't do it now\n"); 702 #endif 703 break; 704 } else { 705 semptr->semval += sopptr->sem_op; 706 if (semptr->semval == 0 && 707 semptr->semzcnt > 0) 708 do_wakeup = 1; 709 } 710 if (sopptr->sem_flg & SEM_UNDO) 711 do_undos = 1; 712 } else if (sopptr->sem_op == 0) { 713 if (semptr->semval > 0) { 714 #ifdef SEM_DEBUG 715 printf("semop: not zero now\n"); 716 #endif 717 break; 718 } 719 } else { 720 if (semptr->semncnt > 0) 721 do_wakeup = 1; 722 semptr->semval += sopptr->sem_op; 723 if (sopptr->sem_flg & SEM_UNDO) 724 do_undos = 1; 725 } 726 } 727 728 /* 729 * Did we get through the entire vector? 730 */ 731 if (i >= nsops) 732 goto done; 733 734 /* 735 * No ... rollback anything that we've already done 736 */ 737 #ifdef SEM_DEBUG 738 printf("semop: rollback 0 through %d\n", i-1); 739 #endif 740 for (j = 0; j < i; j++) 741 semaptr->sem_base[sops[j].sem_num].semval -= 742 sops[j].sem_op; 743 744 /* 745 * If the request that we couldn't satisfy has the 746 * NOWAIT flag set then return with EAGAIN. 747 */ 748 if (sopptr->sem_flg & IPC_NOWAIT) 749 return(EAGAIN); 750 751 if (sopptr->sem_op == 0) 752 semptr->semzcnt++; 753 else 754 semptr->semncnt++; 755 756 #ifdef SEM_DEBUG 757 printf("semop: good night!\n"); 758 #endif 759 eval = tsleep((caddr_t)semaptr, (PZERO - 4) | PCATCH, 760 "semwait", 0); 761 #ifdef SEM_DEBUG 762 printf("semop: good morning (eval=%d)!\n", eval); 763 #endif 764 765 suptr = NULL; /* sem_undo may have been reallocated */ 766 767 if (eval != 0) 768 return(EINTR); 769 #ifdef SEM_DEBUG 770 printf("semop: good morning!\n"); 771 #endif 772 773 /* 774 * Make sure that the semaphore still exists 775 */ 776 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 || 777 semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) { 778 /* The man page says to return EIDRM. */ 779 /* Unfortunately, BSD doesn't define that code! */ 780 #ifdef EIDRM 781 return(EIDRM); 782 #else 783 return(EINVAL); 784 #endif 785 } 786 787 /* 788 * The semaphore is still alive. Readjust the count of 789 * waiting processes. 790 */ 791 if (sopptr->sem_op == 0) 792 semptr->semzcnt--; 793 else 794 semptr->semncnt--; 795 } 796 797 done: 798 /* 799 * Process any SEM_UNDO requests. 800 */ 801 if (do_undos) { 802 for (i = 0; i < nsops; i++) { 803 /* 804 * We only need to deal with SEM_UNDO's for non-zero 805 * op's. 806 */ 807 int adjval; 808 809 if ((sops[i].sem_flg & SEM_UNDO) == 0) 810 continue; 811 adjval = sops[i].sem_op; 812 if (adjval == 0) 813 continue; 814 eval = semundo_adjust(p, &suptr, semid, 815 sops[i].sem_num, -adjval); 816 if (eval == 0) 817 continue; 818 819 /* 820 * Oh-Oh! We ran out of either sem_undo's or undo's. 821 * Rollback the adjustments to this point and then 822 * rollback the semaphore ups and down so we can return 823 * with an error with all structures restored. We 824 * rollback the undo's in the exact reverse order that 825 * we applied them. This guarantees that we won't run 826 * out of space as we roll things back out. 827 */ 828 for (j = i - 1; j >= 0; j--) { 829 if ((sops[j].sem_flg & SEM_UNDO) == 0) 830 continue; 831 adjval = sops[j].sem_op; 832 if (adjval == 0) 833 continue; 834 if (semundo_adjust(p, &suptr, semid, 835 sops[j].sem_num, adjval) != 0) 836 panic("semop - can't undo undos"); 837 } 838 839 for (j = 0; j < nsops; j++) 840 semaptr->sem_base[sops[j].sem_num].semval -= 841 sops[j].sem_op; 842 843 #ifdef SEM_DEBUG 844 printf("eval = %d from semundo_adjust\n", eval); 845 #endif 846 return(eval); 847 } /* loop through the sops */ 848 } /* if (do_undos) */ 849 850 /* We're definitely done - set the sempid's */ 851 for (i = 0; i < nsops; i++) { 852 sopptr = &sops[i]; 853 semptr = &semaptr->sem_base[sopptr->sem_num]; 854 semptr->sempid = p->p_pid; 855 } 856 857 /* Do a wakeup if any semaphore was up'd. */ 858 if (do_wakeup) { 859 #ifdef SEM_DEBUG 860 printf("semop: doing wakeup\n"); 861 #ifdef SEM_WAKEUP 862 sem_wakeup((caddr_t)semaptr); 863 #else 864 wakeup((caddr_t)semaptr); 865 #endif 866 printf("semop: back from wakeup\n"); 867 #else 868 wakeup((caddr_t)semaptr); 869 #endif 870 } 871 #ifdef SEM_DEBUG 872 printf("semop: done\n"); 873 #endif 874 *retval = 0; 875 return(0); 876 } 877 878 /* 879 * Go through the undo structures for this process and apply the adjustments to 880 * semaphores. 881 */ 882 void 883 semexit(p) 884 struct proc *p; 885 { 886 register struct sem_undo *suptr; 887 register struct sem_undo **supptr; 888 int did_something; 889 890 /* 891 * If somebody else is holding the global semaphore facility lock 892 * then sleep until it is released. 893 */ 894 while (semlock_holder != NULL && semlock_holder != p) { 895 #ifdef SEM_DEBUG 896 printf("semaphore facility locked - sleeping ...\n"); 897 #endif 898 (void) tsleep((caddr_t)&semlock_holder, (PZERO - 4), "semext", 0); 899 } 900 901 did_something = 0; 902 903 /* 904 * Go through the chain of undo vectors looking for one 905 * associated with this process. 906 */ 907 908 for (supptr = &semu_list; (suptr = *supptr) != NULL; 909 supptr = &suptr->un_next) { 910 if (suptr->un_proc == p) 911 break; 912 } 913 914 if (suptr == NULL) 915 goto unlock; 916 917 #ifdef SEM_DEBUG 918 printf("proc @%08x has undo structure with %d entries\n", p, 919 suptr->un_cnt); 920 #endif 921 922 /* 923 * If there are any active undo elements then process them. 924 */ 925 if (suptr->un_cnt > 0) { 926 int ix; 927 928 for (ix = 0; ix < suptr->un_cnt; ix++) { 929 int semid = suptr->un_ent[ix].un_id; 930 int semnum = suptr->un_ent[ix].un_num; 931 int adjval = suptr->un_ent[ix].un_adjval; 932 struct semid_ds *semaptr; 933 934 semaptr = &sema[semid]; 935 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0) 936 panic("semexit - semid not allocated"); 937 if (semnum >= semaptr->sem_nsems) 938 panic("semexit - semnum out of range"); 939 940 #ifdef SEM_DEBUG 941 printf("semexit: %08x id=%d num=%d(adj=%d) ; sem=%d\n", 942 suptr->un_proc, suptr->un_ent[ix].un_id, 943 suptr->un_ent[ix].un_num, 944 suptr->un_ent[ix].un_adjval, 945 semaptr->sem_base[semnum].semval); 946 #endif 947 948 if (adjval < 0) { 949 if (semaptr->sem_base[semnum].semval < -adjval) 950 semaptr->sem_base[semnum].semval = 0; 951 else 952 semaptr->sem_base[semnum].semval += 953 adjval; 954 } else 955 semaptr->sem_base[semnum].semval += adjval; 956 957 #ifdef SEM_WAKEUP 958 sem_wakeup((caddr_t)semaptr); 959 #else 960 wakeup((caddr_t)semaptr); 961 #endif 962 #ifdef SEM_DEBUG 963 printf("semexit: back from wakeup\n"); 964 #endif 965 } 966 } 967 968 /* 969 * Deallocate the undo vector. 970 */ 971 #ifdef SEM_DEBUG 972 printf("removing vector\n"); 973 #endif 974 suptr->un_proc = NULL; 975 *supptr = suptr->un_next; 976 977 unlock: 978 /* 979 * If the exiting process is holding the global semaphore facility 980 * lock then release it. 981 */ 982 if (semlock_holder == p) { 983 semlock_holder = NULL; 984 wakeup((caddr_t)&semlock_holder); 985 } 986 } 987