-
Notifications
You must be signed in to change notification settings - Fork 9
/
uptime.c
62 lines (54 loc) · 1.53 KB
/
uptime.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* uptime.c
* Uptime over DNS
*
* Copyright (C) 2000,2001,2002 by Salvatore Sanfilippo
*
* This code is under the GPL license
* See the COPYING file for more information
*/
/* ens.h must be included before all other includes */
#include "ens.h"
#include <time.h>
#include <stdlib.h>
#define YAKU_UPTIME_RR_NAME "uptime.yaku"
/* global vars */
int opt_uptime = 0;
time_t ens_start;
/* not exported functions */
/* exported functions */
int uptime_refresh(void);
/* -------------------------------------------------------------------------- */
/* Usually called once every second, this function updates the
* uptime.yaku TXT CHAOS RR in the local table. */
int uptime_refresh(void)
{
struct RRentry *uptimerr = NULL;
char buffer[1024];
byte *encoded_uptime;
int encoded_uptime_size;
time_t uptime;
uptimerr = local_search(YAKU_UPTIME_RR_NAME".", T_TXT, C_CHAOS, 0);
if (uptimerr == NULL) {
uptimerr = alloc_rr(YAKU_UPTIME_RR_NAME, T_TXT, C_CHAOS, 1);
if (uptimerr == NULL)
return -1;
uptimerr->data[0] = 0;
uptimerr->ttl = 0;
local_add_entry(uptimerr);
}
uptime = get_sec() - ens_start;
snprintf(buffer, 1024, "%ld days,%ld hours,%ld minutes,%ld seconds",
uptime/86400,
(uptime%86400)/3600,
((uptime%86400)%3600)/60,
((uptime%86400)%3600)%60);
encoded_uptime = name_encode(buffer, &encoded_uptime_size, ',');
if (encoded_uptime == NULL)
return encoded_uptime_size;
encoded_uptime_size--;
free(uptimerr->data);
uptimerr->data = encoded_uptime;
uptimerr->size = encoded_uptime_size;
return 0;
}