forked from h0tw1r3/pixelserv
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutil.c
executable file
·124 lines (110 loc) · 3.61 KB
/
util.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "util.h"
// make gcc happy
#ifdef DEBUG
void dummy() {
SET_LINE_NUMBER(__LINE__)
}
#endif
// stats data
// note that child processes inherit a snapshot copy
// public data (should probably change to a struct)
volatile sig_atomic_t count = 0;
volatile sig_atomic_t avg = 0;
volatile sig_atomic_t act = 0;
volatile sig_atomic_t rmx = 0;
volatile sig_atomic_t tct = 0;
volatile sig_atomic_t tav = 0;
volatile sig_atomic_t tmx = 0;
volatile sig_atomic_t err = 0;
volatile sig_atomic_t tmo = 0;
volatile sig_atomic_t cls = 0;
volatile sig_atomic_t nou = 0;
volatile sig_atomic_t pth = 0;
volatile sig_atomic_t nfe = 0;
volatile sig_atomic_t ufe = 0;
volatile sig_atomic_t gif = 0;
volatile sig_atomic_t bad = 0;
volatile sig_atomic_t txt = 0;
volatile sig_atomic_t jpg = 0;
volatile sig_atomic_t png = 0;
volatile sig_atomic_t swf = 0;
volatile sig_atomic_t ico = 0;
volatile sig_atomic_t ssl = 0;
volatile sig_atomic_t sta = 0;
volatile sig_atomic_t stt = 0;
volatile sig_atomic_t noc = 0;
volatile sig_atomic_t rdr = 0;
volatile sig_atomic_t pst = 0;
volatile sig_atomic_t hed = 0;
// private data
static struct timespec startup_time = {0, 0};
static clockid_t clock_source = CLOCK_MONOTONIC;
void get_time(struct timespec *time) {
if (clock_gettime(clock_source, time) < 0) {
if (errno == EINVAL &&
clock_source == CLOCK_MONOTONIC) {
clock_source = CLOCK_REALTIME;
syslog(LOG_WARNING, "clock_gettime() reports CLOCK_MONOTONIC not supported; switching to less accurate CLOCK_REALTIME");
get_time(time); // try again with new clock setting
} else {
// this should never happen
syslog(LOG_ERR, "clock_gettime() reported failure getting time: %m");
time->tv_sec = time->tv_nsec = 0;
}
}
}
char* get_version(int argc, char* argv[]) {
char* retbuf = NULL;
char* optbuf = NULL;
unsigned int optlen = 0, i = 1, freeoptbuf = 0;
unsigned int arglen[argc];
// capture startup_time if not yet set
if (!startup_time.tv_sec) {
get_time(&startup_time);
}
// determine total size of all arguments
for (i = 1; i < argc; ++i) {
arglen[i] = strlen(argv[i]) + 1; // add 1 for leading space
optlen += arglen[i];
}
if (optlen > 0) {
// allocate a buffer to hold all arguments
optbuf = malloc((optlen * sizeof(char)) + 1);
if (optbuf) {
freeoptbuf = 1;
// concatenate arguments into buffer
for (i = 1, optlen = 0; i < argc; ++i) {
optbuf[optlen] = ' '; // prepend a space to each argument
strncpy(optbuf + optlen + 1, argv[i], arglen[i]);
optlen += arglen[i];
}
optbuf[optlen] = '\0';
} else {
optbuf = " <malloc error>";
}
} else {
optbuf = " <none>";
}
if (asprintf(&retbuf, "%s version: %s compiled: %s options:%s", argv[0], VERSION, __DATE__ " " __TIME__, optbuf) < 1) {
retbuf = " <asprintf error>";
}
if (freeoptbuf) {
free(optbuf);
freeoptbuf = 0;
}
return retbuf;
}
char* get_stats(const int sta_offset, const int stt_offset) {
char* retbuf = NULL;
struct timespec current_time;
double uptime;
get_time(¤t_time);
uptime = difftime(current_time.tv_sec, startup_time.tv_sec);
if (asprintf(&retbuf
, "%.0f uts, %d req, %d avg, %d rmx, %d tav, %d tmx, %d err, %d tmo, %d cls, %d nou, %d pth, %d nfe, %d ufe, %d gif, %d bad, %d txt, %d jpg, %d png, %d swf, %d ico, %d ssl, %d sta, %d stt, %d 204, %d rdr, %d pst, %d hed"
, uptime, count, avg, rmx, tav, tmx, err, tmo, cls, nou, pth, nfe, ufe, gif, bad, txt, jpg, png, swf, ico, ssl, sta + sta_offset, stt + stt_offset, noc, rdr, pst, hed
) < 1) {
retbuf = " <asprintf error>";
}
return retbuf;
}