Use timevals everywhere where it is possible, including API between main

loop and event dispatchers. This simplifies code and eliminates a bug,
when kevent dispatcher is called with 0 timeout.

While here, in the main loop call gettimeofday() right after event
dispatcher returns. Otherwise, we are using outdated "timeofday" in
second part of the loop. I don't know any bugs because of that, but
they are possible.
This commit is contained in:
Gleb Smirnoff
2019-05-08 14:19:46 -07:00
parent cad8c922f0
commit 1d363c209f
4 changed files with 31 additions and 47 deletions

View File

@@ -1112,7 +1112,6 @@ main(int argc, char **argv)
struct upnphttp * next;
struct timeval tv, timeofday, lastnotifytime = {0, 0};
time_t lastupdatetime = 0, lastdbtime = 0;
u_long timeout; /* in milliseconds */
int last_changecnt = 0;
pid_t scanner_pid = 0;
struct event ssdpev, httpev, monev;
@@ -1184,6 +1183,9 @@ main(int argc, char **argv)
httpev = (struct event ){ .fd = shttpl, .rdwr = EVENT_READ, .process = ProcessListen };
event_module.add(&httpev);
if (gettimeofday(&timeofday, 0) < 0)
DPRINTF(E_FATAL, L_GENERAL, "gettimeofday(): %s\n", strerror(errno));
#ifdef TIVO_SUPPORT
if (GETFLAG(TIVO_MASK))
{
@@ -1208,18 +1210,17 @@ main(int argc, char **argv)
tivo_bcast.sin_family = AF_INET;
tivo_bcast.sin_addr.s_addr = htonl(getBcastAddress());
tivo_bcast.sin_port = htons(2190);
lastbeacontime = timeofday;
}
}
#endif
reload_ifaces(0);
lastnotifytime.tv_sec = time(NULL) + runtime_vars.notify_interval;
reload_ifaces(0); /* sends SSDP notifies */
lastnotifytime = timeofday;
/* main loop */
while (!quitting)
{
if (gettimeofday(&timeofday, 0) < 0)
DPRINTF(E_FATAL, L_GENERAL, "gettimeofday(): %s\n", strerror(errno));
/* Check if we need to send SSDP NOTIFY messages and do it if
* needed */
tv = lastnotifytime;
@@ -1233,41 +1234,38 @@ main(int argc, char **argv)
runtime_vars.port, runtime_vars.notify_interval);
}
lastnotifytime = timeofday;
timeout = runtime_vars.notify_interval * 1000;
tv.tv_sec = runtime_vars.notify_interval;
tv.tv_usec = 0;
}
else
{
timevalsub(&tv, &timeofday);
timeout = tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
#ifdef TIVO_SUPPORT
if (sbeacon >= 0)
{
u_long beacontimeout;
struct timeval beacontv;
tv = lastbeacontime;
tv.tv_sec += beacon_interval;
if (timevalcmp(&timeofday, &tv, >=))
beacontv = lastbeacontime;
beacontv.tv_sec += beacon_interval;
if (timevalcmp(&timeofday, &beacontv, >=))
{
sendBeaconMessage(sbeacon, &tivo_bcast, sizeof(struct sockaddr_in), 1);
lastbeacontime = timeofday;
beacontimeout = beacon_interval * 1000;
if (timeout > beacon_interval * 1000)
timeout = beacon_interval * 1000;
/* Beacons should be sent every 5 seconds or
* so for the first minute, then every minute
* or so thereafter. */
if (beacon_interval == 5 && (timeofday.tv_sec - startup_time) > 60)
beacon_interval = 60;
beacontv.tv_sec = beacon_interval;
beacontv.tv_usec = 0;
}
else
{
timevalsub(&tv, &timeofday);
beacontimeout = tv.tv_sec * 1000 +
tv.tv_usec / 1000;
timevalsub(&beacontv, &timeofday);
}
if (timeout > beacontimeout)
timeout = beacontimeout;
if (timevalcmp(&tv, &beacontv, >))
tv = beacontv;
}
#endif
@@ -1282,13 +1280,16 @@ main(int argc, char **argv)
#endif
} else
/* Keep checking for the scanner every sec. */
timeout = 1000;
tv.tv_sec = 1;
}
event_module.process(timeout);
event_module.process(&tv);
if (quitting)
goto shutdown;
if (gettimeofday(&timeofday, 0) < 0)
DPRINTF(E_FATAL, L_GENERAL, "gettimeofday(): %s\n", strerror(errno));
upnpevents_gc();
/* increment SystemUpdateID if the content database has changed,