1 /* 2 * Copyright (c) 1989 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 */ 17 18 #if defined(LIBC_SCCS) && !defined(lint) 19 static char sccsid[] = "@(#)sleep.c 5.4 (Berkeley) 09/18/89"; 20 #endif /* LIBC_SCCS and not lint */ 21 22 #include <sys/time.h> 23 #include <sys/signal.h> 24 25 #define setvec(vec, a) \ 26 vec.sv_handler = a; vec.sv_mask = vec.sv_onstack = 0 27 28 static int ringring; 29 30 sleep(seconds) 31 unsigned int seconds; 32 { 33 register struct itimerval *itp; 34 struct itimerval itv, oitv; 35 struct sigvec vec, ovec; 36 long omask; 37 void sleephandler(); 38 39 itp = &itv; 40 if (!seconds) 41 return; 42 timerclear(&itp->it_interval); 43 timerclear(&itp->it_value); 44 if (setitimer(ITIMER_REAL, itp, &oitv) < 0) 45 return; 46 itp->it_value.tv_sec = seconds; 47 if (timerisset(&oitv.it_value)) { 48 if (timercmp(&oitv.it_value, &itp->it_value, >)) 49 oitv.it_value.tv_sec -= itp->it_value.tv_sec; 50 else { 51 itp->it_value = oitv.it_value; 52 /* 53 * This is a hack, but we must have time to return 54 * from the setitimer after the alarm or else it'll 55 * be restarted. And, anyway, sleep never did 56 * anything more than this before. 57 */ 58 oitv.it_value.tv_sec = 1; 59 oitv.it_value.tv_usec = 0; 60 } 61 } 62 setvec(vec, sleephandler); 63 (void) sigvec(SIGALRM, &vec, &ovec); 64 omask = sigblock(sigmask(SIGALRM)); 65 ringring = 0; 66 (void) setitimer(ITIMER_REAL, itp, (struct itimerval *)0); 67 while (!ringring) 68 sigpause(omask &~ sigmask(SIGALRM)); 69 (void) sigvec(SIGALRM, &ovec, (struct sigvec *)0); 70 (void) sigsetmask(omask); 71 (void) setitimer(ITIMER_REAL, &oitv, (struct itimerval *)0); 72 } 73 74 static void 75 sleephandler() 76 { 77 ringring = 1; 78 } 79