Skip to content

Commit

Permalink
Merge pull request #434 from nodogsplash/4.2.1beta
Browse files Browse the repository at this point in the history
fix: NDS uptime if NTP client enabled
  • Loading branch information
bluewavenet committed Sep 18, 2019
2 parents cc73242 + baf495c commit 63f55c9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,18 @@ main_loop(void)
char *fasssl = NULL;
char *phpcmd = NULL;
char *preauth_dir = NULL;
time_t sysuptime;

config = config_get_config();

sysuptime = get_system_uptime ();
debug(LOG_INFO, "main: System Uptime is %li seconds", sysuptime);

/* Set the time when nodogsplash started */
if (!started_time) {
debug(LOG_INFO, "Setting started_time");
started_time = time(NULL);
} else if (started_time < MINIMUM_STARTED_TIME) {
} else if (started_time < (time(NULL) - sysuptime)) {
debug(LOG_WARNING, "Detected possible clock skew - re-setting started_time");
started_time = time(NULL);
}
Expand Down
56 changes: 48 additions & 8 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,12 +369,44 @@ format_time(time_t time, char buf[64])
return buf;
}

char *
get_uptime_string(char buf[64])
{
return format_duration(started_time, time(NULL), buf);
char * get_uptime_string(char buf[64]) {
time_t sysuptime;
unsigned long int now, uptimesecs;

sysuptime = get_system_uptime ();
now = time(NULL);

debug(LOG_INFO, "Uncorrected NDS Uptime: %li seconds ", (now - started_time));

if ((now - started_time) > sysuptime) {
uptimesecs = sysuptime;
} else {
uptimesecs = now - started_time;
}

return format_duration((now - uptimesecs), now, buf);
}

time_t get_system_uptime() {
time_t sysuptime;
char buf[64];
FILE *pfp;

pfp = fopen ("/proc/uptime", "r");

if (pfp != NULL) {
fgets (buf, sizeof(buf), pfp);
sysuptime = atol(strtok(buf, "."));
debug(LOG_INFO, "Operating System Uptime: %li seconds ", sysuptime);
fclose (pfp);
return sysuptime;
}

debug(LOG_WARNING, "Unable to determine System Uptime.");
return -1;
}


int is_addr(const char* addr) {
struct sockaddr_in sa;
struct sockaddr_in6 sa6;
Expand All @@ -396,19 +428,27 @@ ndsctl_status(FILE *fp)
t_MAC *trust_mac;
t_MAC *allow_mac;
t_MAC *block_mac;
time_t sysuptime;

config = config_get_config();

fprintf(fp, "==================\nNoDogSplash Status\n====\n");

sysuptime = get_system_uptime ();
now = time(NULL);
uptimesecs = now - started_time;

debug(LOG_INFO, "Uncorrected Uptime: %li seconds ", (now - started_time));

if ((now - started_time) > sysuptime) {
uptimesecs = sysuptime;
} else {
uptimesecs = now - started_time;
}

fprintf(fp, "Version: " VERSION "\n");

format_duration(started_time, now, durationbuf);
fprintf(fp, "Uptime: %s\n", durationbuf);
format_duration(0, uptimesecs, durationbuf);

fprintf(fp, "Uptime: %s\n", durationbuf);
fprintf(fp, "Gateway Name: %s\n", config->gw_name);
fprintf(fp, "Managed interface: %s\n", config->gw_interface);
fprintf(fp, "Managed IP range: %s\n", config->gw_iprange);
Expand Down
3 changes: 3 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ char *format_time(time_t time, char buf[64]);
/* @brief Check if the address is a valid IPv4 or IPv6 address */
int is_addr(const char* addr);

/* @brief Returns System Uptime in seconds */
time_t get_system_uptime();

/*
* @brief Mallocs and returns nodogsplash uptime string
*/
Expand Down

0 comments on commit 63f55c9

Please sign in to comment.