aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric S. Raymond <esr@thyrsus.com>2002-08-26 18:47:09 +0000
committerEric S. Raymond <esr@thyrsus.com>2002-08-26 18:47:09 +0000
commitc80d3c6b9cde8128a702b13215fd5c1c60d54103 (patch)
tree8821c6dcc957551ed0cc00f207a2ecfc26b3403a
parentdc3858c16e0d02f1e0f8b5f7dd8f0b787bbdcbfd (diff)
downloadfetchmail-c80d3c6b9cde8128a702b13215fd5c1c60d54103.tar.gz
fetchmail-c80d3c6b9cde8128a702b13215fd5c1c60d54103.tar.bz2
fetchmail-c80d3c6b9cde8128a702b13215fd5c1c60d54103.zip
Refactor so we can use idle.c in the cookbook.
svn path=/trunk/; revision=3688
-rw-r--r--fetchmail.c17
-rw-r--r--idle.c74
2 files changed, 55 insertions, 36 deletions
diff --git a/fetchmail.c b/fetchmail.c
index ade69cc2..93f4754a 100644
--- a/fetchmail.c
+++ b/fetchmail.c
@@ -116,6 +116,12 @@ static char *timestamp (void)
#define timestamp rfc822timestamp
#endif
+static RETSIGTYPE donothing(int sig)
+{
+ extern volatile int lastsig; /* declared in idle.c */
+ signal(sig, donothing); lastsig = sig;
+}
+
int main(int argc, char **argv)
{
int bkgd = FALSE;
@@ -719,6 +725,17 @@ int main(int argc, char **argv)
GT_("sleeping at %s\n"), timestamp());
/*
+ * With this simple hack, we make it possible for a foreground
+ * fetchmail to wake up one in daemon mode. What we want is the
+ * side effect of interrupting any sleep that may be going on,
+ * forcing fetchmail to re-poll its hosts. The second line is
+ * for people who think all system daemons wake up on SIGHUP.
+ */
+ signal(SIGUSR1, donothing);
+ if (!getuid())
+ signal(SIGHUP, donothing);
+
+ /*
* OK, now pause until it's time for the next poll cycle.
* A nonzero return indicates we received a wakeup signal;
* unwedge all servers in case the problem has been
diff --git a/idle.c b/idle.c
index b3ecd503..d91e10a2 100644
--- a/idle.c
+++ b/idle.c
@@ -1,23 +1,32 @@
-/*
- * idle.c -- pause code for fetchmail
- *
- * For license terms, see the file COPYING in this directory.
- */
-#include "config.h"
+/*****************************************************************************
+
+NAME:
+ idle.c -- code for interruptible delays without sleep(3).
+
+ENTRY POINTS:
+ interruptible_idle() -- delay for some time, interruptible by signal.
+
+THEORY:
+ Sometimes you need more than one time delay per program, so alarm(3)
+won't cut it. This code illustrates time delays with select(2).
+
+AUTHOR:
+ Eric S. Raymond <esr@thyrsus.com>, 1997. This source code example
+is part of fetchmail and the Unix Cookbook, and are released under the
+MIT license. Compile with -DMAIN to build the demonstrator.
+******************************************************************************/
#include <stdio.h>
-#if defined(STDC_HEADERS)
#include <stdlib.h>
-#endif
-#if defined(HAVE_UNISTD_H)
#include <unistd.h>
-#endif
#include <signal.h>
#include <errno.h>
#include <sys/time.h>
-#include "fetchmail.h"
-#include "i18n.h"
+#ifndef TRUE
+#define TRUE 1
+#define FALSE 0
+#endif
volatile int lastsig; /* last signal received */
@@ -27,18 +36,13 @@ volatile int lastsig; /* last signal received */
* SIGALRM can hose the code (ALARM is triggered *before* pause() is called).
* This is a bit of a kluge; the real right thing would use sigprocmask(),
* sigsuspend(). This workaround lets the interval timer trigger the first
- * alarm after the required interval and will then generate alarms all 5
- * seconds, until it is certain, that the critical section (ie., the window)
- * is left.
+ * alarm after the required interval and will then generate alarms
+ * seconds until it is certain that the critical section (ie., the window)
+ * is exited.
*/
-#if defined(STDC_HEADERS)
static sig_atomic_t alarm_latch = FALSE;
-#else
-/* assume int can be written in one atomic operation on non ANSI-C systems */
-static int alarm_latch = FALSE;
-#endif
-RETSIGTYPE gotsigalrm(int sig)
+void gotsigalrm(int sig)
{
signal(sig, gotsigalrm);
lastsig = sig;
@@ -63,24 +67,11 @@ void itimerthread(void* dummy)
}
#endif
-RETSIGTYPE donothing(int sig) {signal(sig, donothing); lastsig = sig;}
-
int interruptible_idle(int seconds)
/* time for a pause in the action; return TRUE if awakened by signal */
{
int awoken = FALSE;
- /*
- * With this simple hack, we make it possible for a foreground
- * fetchmail to wake up one in daemon mode. What we want is the
- * side effect of interrupting any sleep that may be going on,
- * forcing fetchmail to re-poll its hosts. The second line is
- * for people who think all system daemons wake up on SIGHUP.
- */
- signal(SIGUSR1, donothing);
- if (!getuid())
- signal(SIGHUP, donothing);
-
#ifndef __EMX__
#ifdef SLEEP_WITH_ALARM /* not normally on */
/*
@@ -123,7 +114,7 @@ int interruptible_idle(int seconds)
setitimer(ITIMER_REAL,&ntimeout,NULL); /* then start timer */
/* there is a very small window between the next two lines */
/* which could result in a deadlock. But this will now be */
- /* caught by periodical alarms (see it_interval) */
+ /* caught by periodic alarms (see it_interval) */
if (!alarm_latch)
pause();
/* stop timer */
@@ -146,7 +137,7 @@ int interruptible_idle(int seconds)
{
struct timeval timeout;
- timeout.tv_sec = run.poll_interval;
+ timeout.tv_sec = seconds;
timeout.tv_usec = 0;
do {
lastsig = 0;
@@ -174,4 +165,15 @@ int interruptible_idle(int seconds)
return(awoken ? lastsig : 0);
}
+#ifdef MAIN
+int main(int argc, **argv)
+{
+ for (;;)
+ {
+ printf("How may I serve you, master?\n");
+ interruptible_idle(5);
+ }
+}
+#endif /* MAIN */
+
/* idle.c ends here */