1 /*****************************************************************************
4 idle.c -- code for interruptible delays without sleep(3).
7 interruptible_idle() -- delay for some time, interruptible by signal.
10 Sometimes you need more than one time delay per program, so alarm(3)
11 won't cut it. This code illustrates time delays with select(2).
14 Eric S. Raymond <esr@thyrsus.com>, 1997. This source code example
15 is part of fetchmail and the Unix Cookbook, and are released under the
16 MIT license. Compile with -DMAIN to build the demonstrator.
18 ******************************************************************************/
24 #include <fetchmail.h> /* for ROOT_UID */
31 volatile int lastsig; /* last signal received */
33 #ifdef SLEEP_WITH_ALARM
35 * The function of this variable is to remove the window during which a
36 * SIGALRM can hose the code (ALARM is triggered *before* pause() is called).
37 * This is a bit of a kluge; the real right thing would use sigprocmask(),
38 * sigsuspend(). This workaround lets the interval timer trigger the first
39 * alarm after the required interval and will then generate alarms
40 * seconds until it is certain that the critical section (ie., the window)
43 static sig_atomic_t alarm_latch = FALSE;
45 void gotsigalrm(int sig)
47 set_signal_handler(sig, gotsigalrm);
51 #endif /* SLEEP_WITH_ALARM */
53 int interruptible_idle(int seconds)
54 /* time for a pause in the action; return TRUE if awakened by signal */
58 #ifdef SLEEP_WITH_ALARM /* not normally on */
60 * We can't use sleep(3) here because we need an alarm(3)
61 * equivalent in order to implement server nonresponse timeout.
62 * We'll just assume setitimer(2) is available since fetchmail
63 * has to have a BSDoid socket layer to work at all.
66 * This code stopped working under glibc-2, apparently due
67 * to the change in signal(2) semantics. (The siginterrupt
68 * line, added later, should fix this problem.) John Stracke
69 * <francis@netscape.com> wrote:
71 * The problem seems to be that, after hitting the interval
72 * timer while talking to the server, the process no longer
73 * responds to SIGALRM. I put in printf()s to see when it
74 * reached the pause() for the poll interval, and I checked
75 * the return from setitimer(), and everything seemed to be
76 * working fine, except that the pause() just ignored SIGALRM.
77 * I thought maybe the itimer wasn't being fired, so I hit
78 * it with a SIGALRM from the command line, and it ignored
79 * that, too. SIGUSR1 woke it up just fine, and it proceeded
80 * to repoll--but, when the dummy server didn't respond, it
81 * never timed out, and SIGALRM wouldn't make it.
83 * (continued below...)
86 struct itimerval ntimeout;
88 ntimeout.it_interval.tv_sec = 5; /* repeat alarm every 5 secs */
89 ntimeout.it_interval.tv_usec = 0;
90 ntimeout.it_value.tv_sec = seconds;
91 ntimeout.it_value.tv_usec = 0;
94 set_signal_handler(SIGALRM, gotsigalrm); /* first trap signals */
95 setitimer(ITIMER_REAL,&ntimeout,NULL); /* then start timer */
96 /* there is a very small window between the next two lines */
97 /* which could result in a deadlock. But this will now be */
98 /* caught by periodic alarms (see it_interval) */
102 ntimeout.it_interval.tv_sec = ntimeout.it_interval.tv_usec = 0;
103 ntimeout.it_value.tv_sec = ntimeout.it_value.tv_usec = 0;
104 setitimer(ITIMER_REAL,&ntimeout,NULL); /* now stop timer */
105 set_signal_handler(SIGALRM, SIG_IGN);
109 * So the workaround I used is to make it sleep by using
110 * select() instead of setitimer()/pause(). select() is
111 * perfectly happy being called with a timeout and
112 * no file descriptors; it just sleeps until it hits the
113 * timeout. The only concern I had was that it might
114 * implement its timeout with SIGALRM--there are some
115 * Unices where this is done, because select() is a library
116 * function--but apparently not.
119 struct timeval timeout;
121 timeout.tv_sec = seconds;
125 select(0,0,0,0, &timeout);
126 } while (lastsig == SIGCHLD);
129 if (lastsig == SIGUSR1 || ((seconds && getuid() == ROOT_UID)
130 && lastsig == SIGHUP))
133 /* now lock out interrupts again */
134 set_signal_handler(SIGUSR1, SIG_IGN);
135 if (getuid() == ROOT_UID)
136 set_signal_handler(SIGHUP, SIG_IGN);
138 return(awoken ? lastsig : 0);
142 int main(int argc, char **argv)
146 printf("How may I serve you, master?\n");
147 interruptible_idle(5);
152 /* idle.c ends here */