This commit is contained in:
Axel Kittenberger 2010-10-24 21:35:29 +00:00
parent 1c1c0ee542
commit bc4e1de6f5
1 changed files with 25 additions and 18 deletions

View File

@ -125,6 +125,15 @@ static volatile sig_atomic_t reset = 0;
*/ */
static long clocks_per_sec; static long clocks_per_sec;
/**
* signal handler
*/
void
sig_child(int sig)
{
/* nothing */
}
/** /**
* predeclerations -- see belorw. * predeclerations -- see belorw.
*/ */
@ -854,7 +863,7 @@ masterloop(lua_State *L)
bool have_alarm; bool have_alarm;
clock_t now = times(NULL); clock_t now = times(NULL);
clock_t alarm_time; clock_t alarm_time;
bool do_read = false; int do_read;
ssize_t len; ssize_t len;
/* query runner about soonest alarm */ /* query runner about soonest alarm */
@ -869,7 +878,7 @@ masterloop(lua_State *L)
* thus do not read from inotify_fd and jump directly to its handling */ * thus do not read from inotify_fd and jump directly to its handling */
logstring(DEBUG, "immediately handling delayed entries."); logstring(DEBUG, "immediately handling delayed entries.");
do_read = 0; do_read = 0;
} else if (have_alarm) { } else {
/* use select() to determine what happens next /* use select() to determine what happens next
* + a new event on inotify * + a new event on inotify
* + an alarm on timeout * + an alarm on timeout
@ -877,33 +886,28 @@ masterloop(lua_State *L)
fd_set readfds; fd_set readfds;
struct timeval tv; struct timeval tv;
if (time_before(alarm_time, now)) { if (have_alarm) {
/* should never happen */ logstring(DEBUG, "going into timed select.");
logstring(ERROR, "critical failure, alarm_time is in past!\n"); tv.tv_sec = (alarm_time - now) / clocks_per_sec;
exit(-1); //ERRNO tv.tv_usec = (alarm_time - now) * 1000000 / clocks_per_sec % 1000000;
} else {
logstring(DEBUG, "going into blocking select.");
} }
tv.tv_sec = (alarm_time - now) / clocks_per_sec;
tv.tv_usec = (alarm_time - now) * 1000000 / clocks_per_sec % 1000000;
/* if select returns a positive number there is data on inotify /* if select returns a positive number there is data on inotify
* on zero the timemout occured. */ * on zero the timemout occured. */
FD_ZERO(&readfds); FD_ZERO(&readfds);
FD_SET(inotify_fd, &readfds); FD_SET(inotify_fd, &readfds);
do_read = select(inotify_fd + 1, &readfds, NULL, NULL, &tv); do_read = select(inotify_fd + 1, &readfds, NULL, NULL, have_alarm ? &tv : NULL);
if (do_read) { if (do_read > 0) {
logstring(DEBUG, "theres data on inotify."); logstring(DEBUG, "theres data on inotify.");
} else { } else {
logstring(DEBUG, "core: select() timeout or signal."); logstring(DEBUG, "core: select() timeout or signal.");
} }
} else { }
/* if nothing to wait for, enter a blocking read */
logstring(DEBUG, "gone blocking.");
do_read = 1;
}
/* reads possible events from inotify stream */ /* reads possible events from inotify stream */
while(do_read) { while(do_read > 0) {
int i = 0; int i = 0;
do { do {
len = read (inotify_fd, readbuf, readbuf_size); len = read (inotify_fd, readbuf, readbuf_size);
@ -930,7 +934,7 @@ masterloop(lua_State *L)
FD_ZERO(&readfds); FD_ZERO(&readfds);
FD_SET(inotify_fd, &readfds); FD_SET(inotify_fd, &readfds);
do_read = select(inotify_fd + 1, &readfds, NULL, NULL, &tv); do_read = select(inotify_fd + 1, &readfds, NULL, NULL, &tv);
if (do_read) { if (do_read > 0) {
logstring(DEBUG, "there is more data on inotify."); logstring(DEBUG, "there is more data on inotify.");
} }
} }
@ -1079,6 +1083,9 @@ main(int argc, char *argv[])
return -1; // ERRNO return -1; // ERRNO
} }
/* add signal handlers */
signal(SIGCHLD, sig_child);
/* initialize */ /* initialize */
/* lua code will set configuration and add watches */ /* lua code will set configuration and add watches */
lua_getglobal(L, "lsyncd_initialize"); lua_getglobal(L, "lsyncd_initialize");