Allow configuration of number of retconnect-attempts/interval

This commit is contained in:
Harald Welte 2014-12-20 15:44:30 +01:00
parent c77a9b3342
commit 75cbc3d0e5
1 changed files with 31 additions and 5 deletions

View File

@ -1,5 +1,5 @@
/* gpsdate - small utility to set system RTC based on gpsd time /* gpsdate - small utility to set system RTC based on gpsd time
* (C) 2013 by sysmocom - s.f.m.c. GmbH, Author: Harald Welte * (C) 2013-2015 by sysmocom - s.f.m.c. GmbH, Author: Harald Welte
* All Rights Reserved * All Rights Reserved
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -34,6 +34,7 @@
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <syslog.h> #include <syslog.h>
#include <getopt.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/types.h> #include <sys/types.h>
@ -167,23 +168,48 @@ enum state {
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
char *host = "localhost"; char *host = "localhost";
int num_retries = NUM_RETRIES;
int retry_sleep = RETRY_SLEEP;
int i, rc; int i, rc;
enum state state; enum state state;
openlog("gpsdate", LOG_PERROR, LOG_CRON); openlog("gpsdate", LOG_PERROR, LOG_CRON);
if (argc > 1) while (1) {
host = argv[1]; int option_index = 0, c;
static struct option long_options[] = {
{"num-retries", 1, 0, 'n'},
{"retry-sleep", 1, 0, 's'},
{0,0,0,0}
};
c = getopt_long(argc, argv, "n:s:",
long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 'n':
num_retries = atoi(optarg);
break;
case 's':
retry_sleep = atoi(optarg);
break;
}
}
if (optind < argc)
host = argv[optind];
/* attempt up to NUM_RETRIES times to connect to gpsd while we are /* attempt up to NUM_RETRIES times to connect to gpsd while we are
* still running in foreground. The idea is that we will block the * still running in foreground. The idea is that we will block the
* boot process (init scripts) until we have a connection */ * boot process (init scripts) until we have a connection */
for (i = 1; i <= NUM_RETRIES; i++) { for (i = 1; i <= num_retries; i++) {
printf("Attempt #%d to connect to gpsd at %s...\n", i, host); printf("Attempt #%d to connect to gpsd at %s...\n", i, host);
rc = attempt_reconnect(host, DEFAULT_GPSD_PORT, &gpsdata); rc = attempt_reconnect(host, DEFAULT_GPSD_PORT, &gpsdata);
if (rc >= 0) if (rc >= 0)
break; break;
sleep(RETRY_SLEEP); sleep(retry_sleep);
} }
if (rc < 0) { if (rc < 0) {