Aggiornamenti + alcune sockopt di IP
[gapil.git] / listati / sleep.c
1 void alarm_hand(int);
2 unsigned int sleep(unsigned int seconds)
3 {
4     struct sigaction new_action, old_action;
5     sigset_t old_mask, stop_mask, sleep_mask;
6     /* set the signal handler */
7     sigemptyset(&new_action.sa_mask);              /* no signal blocked */
8     new_action.sa_handler = alarm_hand;            /* set handler */
9     new_action.sa_flags = 0;                       /* no flags */
10     sigaction(SIGALRM, &new_action, &old_action);  /* install action */
11     /* block SIGALRM to avoid race conditions */
12     sigemptyset(&stop_mask);                       /* init mask to empty */
13     sigaddset(&stop_mask, SIGALRM);                /* add SIGALRM */
14     sigprocmask(SIG_BLOCK, &stop_mask, &old_mask); /* add SIGALRM to blocked */
15     /* send the alarm */
16     alarm(seconds); 
17     /* going to sleep enabling SIGALRM */
18     sleep_mask = old_mask;                         /* take mask */
19     sigdelset(&sleep_mask, SIGALRM);               /* remove SIGALRM */
20     sigsuspend(&sleep_mask);                       /* go to sleep */
21     /* restore previous settings */
22     sigprocmask(SIG_SETMASK, &old_mask, NULL);     /* reset signal mask */    
23     sigaction(SIGALRM, &old_action, NULL);         /* reset signal action */
24     /* return remaining time */
25     return alarm(0);
26 }
27 void alarm_hand(int sig) 
28 {
29     return;     /* just return to interrupt sigsuspend */
30 }