Use time_monotonic for timeouts

This commit is contained in:
Joseph C. Lehner 2016-11-18 17:39:04 +01:00
parent c6d02936e9
commit 564aff28aa
6 changed files with 26 additions and 28 deletions

View file

@ -5,22 +5,15 @@ LIBS = -lpcap
CFLAGS += -Wall -g -DNMRPFLASH_VERSION=\"$(VERSION)\"
LDFLAGS += $(LIBS)
nmrpflash_OBJ = nmrp.o tftp.o ethsock.o main.o util.o
.PHONY: clean install release release/osx release/linux release/win32
nmrpflash: nmrp.o tftp.o ethsock.o main.o
$(CC) $(CFLAGS) -o nmrpflash nmrp.o tftp.o ethsock.o main.o $(LDFLAGS)
nmrpflash: $(nmrpflash_OBJ)
$(CC) $(CFLAGS) -o nmrpflash $(nmrpflash_OBJ) $(LDFLAGS)
nmrp.o: nmrp.c nmrpd.h
$(CC) $(CFLAGS) -c -o nmrp.o nmrp.c
tftp.o: tftp.c nmrpd.h
$(CC) $(CFLAGS) -c -o tftp.o tftp.c
ethsock.o: ethsock.c nmrpd.h
$(CC) $(CFLAGS) -c -o ethsock.o ethsock.c
main.o: main.c nmrpd.h
$(CC) $(CFLAGS) -c -o main.o main.c
%.o: %.c nmrpd.h
$(CC) -c $(CFLAGS) $< -o $@
fuzz: clean
CC=afl-gcc CFLAGS=-DNMRPFLASH_FUZZ make nmrpflash

View file

@ -755,12 +755,12 @@ int ethsock_ip_add(struct ethsock *sock, uint32_t ipaddr, uint32_t ipmask, struc
}
set_addr(&sin, ipaddr);
clock_t now = clock();
time_t beg = time_monotonic();
/* Wait until the new IP has actually been added */
while (bind(fd, (struct sockaddr*)&sin, sizeof(sin)) != 0) {
if (((clock() - now) / CLOCKS_PER_SEC) >= 5) {
if ((time_monotonic() - beg) >= 5) {
fprintf(stderr, "Failed to bind after 5 seconds: ");
sock_perror("bind");
DeleteIPAddress((*undo)->context);

4
nmrp.c
View file

@ -538,7 +538,7 @@ int nmrp_do(struct nmrpd_args *args)
i = 0;
upload_ok = 0;
beg = time(NULL);
beg = time_monotonic();
while (1) {
printf("\rAdvertising NMRP server on %s ... %c",
@ -557,7 +557,7 @@ int nmrp_do(struct nmrpd_args *args)
} else if (status == 1) {
goto out;
} else {
if ((time(NULL) - beg) >= 60) {
if ((time_monotonic() - beg) >= 60) {
printf("\nNo response after 60 seconds. Bailing out.\n");
goto out;
}

View file

@ -129,4 +129,6 @@ int ethsock_for_each_ip(struct ethsock *sock, ethsock_ip_callback_t callback,
int ethsock_ip_add(struct ethsock *sock, uint32_t ipaddr, uint32_t ipmask, struct ethsock_ip_undo **undo);
int ethsock_ip_del(struct ethsock *sock, struct ethsock_ip_undo **undo);
time_t time_monotonic();
#endif

View file

@ -29,7 +29,7 @@ IncludeVersionInfo=0
SupportXPThemes=0
CompilerSet=3
CompilerSettings=0000000100000000000001000
UnitCount=5
UnitCount=6
[VersionInfo]
Major=1
@ -100,3 +100,13 @@ Priority=1000
OverrideBuildCmd=0
BuildCmd=
[Unit6]
FileName=util.c
CompileCpp=0
Folder=
Compile=1
Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=

13
util.c
View file

@ -1,4 +1,6 @@
#include <time.h>
#include <math.h>
#include "nmrpd.h"
#ifdef NMRPFLASH_OSX
#include <mach/mach_time.h>
@ -10,7 +12,7 @@ time_t time_monotonic()
#ifndef NMRPFLASH_OSX
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.ts_sec;
return ts.tv_sec;
#else
static double factor = 0.0;
mach_timebase_info_data_t timebase;
@ -25,12 +27,3 @@ time_t time_monotonic()
return round(GetTickCount() / 1000.0);
#endif
}
int main()
{
time_t beg = time_monotonic();
printf("now: %ld\n", beg);
sleep(2);
printf("+2s: %ld\n", time_monotonic());
return 0;
}