17c478bd9Sstevel@tonic-gate /* 2*5d54f3d8Smuffin * copyright (c) 1980 Regents of the University of California. 37c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement 47c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution. 57c478bd9Sstevel@tonic-gate */ 67c478bd9Sstevel@tonic-gate 7*5d54f3d8Smuffin #pragma ident "%Z%%M% %I% %E% SMI" 8*5d54f3d8Smuffin 97c478bd9Sstevel@tonic-gate #include <sys/time.h> 107c478bd9Sstevel@tonic-gate #include <signal.h> 117c478bd9Sstevel@tonic-gate 127c478bd9Sstevel@tonic-gate #define setvec(vec, a) \ 137c478bd9Sstevel@tonic-gate vec.sv_handler = a; vec.sv_mask = vec.sv_onstack = 0 147c478bd9Sstevel@tonic-gate 157c478bd9Sstevel@tonic-gate static int ringring; 167c478bd9Sstevel@tonic-gate 17*5d54f3d8Smuffin static void sleepx(void); 18*5d54f3d8Smuffin 19*5d54f3d8Smuffin void 20*5d54f3d8Smuffin sleep(unsigned n) 217c478bd9Sstevel@tonic-gate { 227c478bd9Sstevel@tonic-gate int omask; 237c478bd9Sstevel@tonic-gate struct itimerval itv, oitv; 24*5d54f3d8Smuffin struct itimerval *itp = &itv; 257c478bd9Sstevel@tonic-gate struct sigvec vec, ovec; 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate if (n == 0) 287c478bd9Sstevel@tonic-gate return; 297c478bd9Sstevel@tonic-gate timerclear(&itp->it_interval); 307c478bd9Sstevel@tonic-gate timerclear(&itp->it_value); 317c478bd9Sstevel@tonic-gate if (setitimer(ITIMER_REAL, itp, &oitv) < 0) 327c478bd9Sstevel@tonic-gate return; 337c478bd9Sstevel@tonic-gate itp->it_value.tv_sec = n; 347c478bd9Sstevel@tonic-gate if (timerisset(&oitv.it_value)) { 357c478bd9Sstevel@tonic-gate if (timercmp(&oitv.it_value, &itp->it_value, >)) 367c478bd9Sstevel@tonic-gate oitv.it_value.tv_sec -= itp->it_value.tv_sec; 377c478bd9Sstevel@tonic-gate else { 387c478bd9Sstevel@tonic-gate itp->it_value = oitv.it_value; 397c478bd9Sstevel@tonic-gate /* 407c478bd9Sstevel@tonic-gate * This is a hack, but we must have time to 417c478bd9Sstevel@tonic-gate * return from the setitimer after the alarm 427c478bd9Sstevel@tonic-gate * or else it'll be restarted. And, anyway, 437c478bd9Sstevel@tonic-gate * sleep never did anything more than this before. 447c478bd9Sstevel@tonic-gate */ 457c478bd9Sstevel@tonic-gate oitv.it_value.tv_sec = 1; 467c478bd9Sstevel@tonic-gate oitv.it_value.tv_usec = 0; 477c478bd9Sstevel@tonic-gate } 487c478bd9Sstevel@tonic-gate } 497c478bd9Sstevel@tonic-gate setvec(vec, sleepx); 507c478bd9Sstevel@tonic-gate (void) sigvec(SIGALRM, &vec, &ovec); 517c478bd9Sstevel@tonic-gate omask = sigblock(sigmask(SIGALRM)); 527c478bd9Sstevel@tonic-gate ringring = 0; 537c478bd9Sstevel@tonic-gate (void) setitimer(ITIMER_REAL, itp, (struct itimerval *)0); 547c478bd9Sstevel@tonic-gate while (!ringring) 557c478bd9Sstevel@tonic-gate sigpause(omask &~ sigmask(SIGALRM)); 567c478bd9Sstevel@tonic-gate (void) sigvec(SIGALRM, &ovec, (struct sigvec *)0); 577c478bd9Sstevel@tonic-gate (void) sigsetmask(omask); 587c478bd9Sstevel@tonic-gate (void) setitimer(ITIMER_REAL, &oitv, (struct itimerval *)0); 597c478bd9Sstevel@tonic-gate } 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate static void 62*5d54f3d8Smuffin sleepx(void) 637c478bd9Sstevel@tonic-gate { 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate ringring = 1; 667c478bd9Sstevel@tonic-gate } 67