-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
174 lines (150 loc) · 4.74 KB
/
Copy pathmain.c
File metadata and controls
174 lines (150 loc) · 4.74 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <getopt.h>
#include <ctype.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include "measurement.h"
#include "publish.h"
static bool running = true;
static unsigned int reflections;
static pthread_mutex_t lock;
static const unsigned int PUBLISH_INTERVAL_YIELD_S = 1;
static const unsigned int DEFAULT_INTERVAL_S = 15 * 60;
static void onReflection(void){
fprintf(stdout, "Reflection detected\n");
pthread_mutex_lock(&lock);
++reflections;
pthread_mutex_unlock(&lock);
}
static void sigHandler(int signum){
switch (signum){
case SIGINT:
case SIGTERM:
running = 0;
fprintf(stderr, "signal %d results in running = %d\r\n", signum, running);
break;
case SIGHUP:
fprintf(stderr, "signal SIGHUP does nothing\r\n");
break;
default:
fprintf(stderr, "signal %d not handled\r\n", signum);
}
}
static bool parseOptions(int argc,
char **argv,
measurementConfig_t *measurementConfig,
publishConfig_t *publishConfig,
unsigned int *interval,
bool *daemon,
bool *fake){
int opt;
while (-1 != (opt = getopt(argc, argv, "fdh:p:a:k:c:t:i"))) {
switch (opt) {
case 'i':{
int tmp = atoi(optarg);
if (tmp > 10){
*interval = tmp;
} else {
fprintf(stderr, "Invalid interval\n");
}
break;
}
case 'd':
*daemon = true;
break;
case 'f':
*fake = true;
break;
case 'h':
publishConfig->hostAddress = optarg;
break;
case 'p':
publishConfig->port = atoi(optarg);
break;
case 'a':
publishConfig->caFilename = optarg;
break;
case 'k':
publishConfig->clientKeyFilename = optarg;
break;
case 'c':
publishConfig->clientCertFilename = optarg;
break;
case 't':
measurementConfig->reflectionMeterThreshold = atoi(optarg);
measurementConfig->reflectionMeterPort = atoi(optarg);
break;
case '?':
if (isprint(optopt)) {
fprintf(stderr,"Unknown option `-%c'.", optopt);
} else {
fprintf(stderr, "Unknown option character `\\x%x'.", optopt);
}
return false;
default:
fprintf(stderr, "Error in command line argument parsing");
return false;
}
}
return true;
}
int main(int argc, char **argv){
unsigned int lastCount = 0;
bool daemonize = false;
bool fake = false;
unsigned int interval = DEFAULT_INTERVAL_S;
struct sigaction sa = { .sa_handler = sigHandler };
time_t lastPublish = (time_t)0;
setvbuf(stdout, NULL, _IONBF, 0);
publishConfig_t publishConfig;
publishConfigDefault(&publishConfig);
measurementConfig_t measurementConfig;
measurementConfigDefault(&measurementConfig);
measurementConfig.onReflection = onReflection;
if (parseOptions(argc, argv, &measurementConfig, &publishConfig, &interval, &daemonize, &fake) == false){
fprintf(stderr, "Could not parse options\r\n");
return EXIT_FAILURE;
}
if (daemonize == true){
if (daemon(1, 1) < 0){
fprintf(stderr, "Could not daemonize (%s)\r\n", strerror(errno));
}
}
if (publishInit(&publishConfig) == false){
fprintf(stderr, "Could not init publish\r\n");
return EXIT_FAILURE;
}
if (measurementInit(&measurementConfig) == false){
fprintf(stderr, "Could not init measure\r\n");
if (fake == false){
return EXIT_FAILURE;
}
}
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
while (running){
time_t now;
pthread_mutex_lock(&lock);
unsigned int count = reflections;
pthread_mutex_unlock(&lock);
if (difftime(time(&now), lastPublish) > (double)interval){
lastPublish = now;
if (publishReflections(count - lastCount) == false){
fprintf(stderr, "Could not publish single reflection\r\n");
}
lastCount = count;
}
if (fake == true){
onReflection();
}
publishProcess(PUBLISH_INTERVAL_YIELD_S);
}
measurementDestroy();
publishDestroy();
return 0;
}