-
Notifications
You must be signed in to change notification settings - Fork 26
/
telem_record_gen.c
435 lines (364 loc) · 13.5 KB
/
telem_record_gen.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
* This program is part of the Clear Linux Project
*
* Copyright 2019 Intel Corporation
*
* This program is free software; you can redistribute it and/or modify it under
* the terms and conditions of the GNU Lesser General Public License, as
* published by the Free Software Foundation; either version 2.1 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
#define _GNU_SOURCE
#include <ctype.h>
#include <getopt.h>
#include <poll.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include "config.h"
#include "log.h"
#include "common.h"
#include "telemetry.h"
static uint32_t severity = 1;
static char *opt_class = NULL;
static char *opt_payload = NULL;
static uint32_t payload_version = 1;
static char *opt_payload_file = NULL;
static char *opt_event_id = NULL;
static bool opt_echo = false;
static bool opt_nopost = false;
static const struct option prog_opts[] = {
{ "help", no_argument, 0, 'h' },
{ "echo", no_argument, 0, 'o' },
{ "no-post", no_argument, 0, 'n' },
{ "version", no_argument, 0, 'V' },
{ "severity", required_argument, 0, 's' },
{ "class", required_argument, 0, 'c' },
{ "payload", required_argument, 0, 'p' },
{ "payload-file", required_argument, 0, 'P' },
{ "record-version", required_argument, 0, 'R' },
{ "event-id", required_argument, 0, 'e' },
{ "config-file", required_argument, 0, 'f' },
{ 0, 0, 0, 0 }
};
static void print_help(void)
{
printf("Usage:\n");
printf(" telem-record-gen [OPTIONS] - create and send a custom telemetry record\n");
printf("\n");
printf("Help Options:\n");
printf(" -h, --help Show help options\n");
printf("\n");
printf("Application Options:\n");
printf(" -V, --version Print the program version\n");
printf(" -s, --severity Severity level (1-4) - (default 1)\n");
printf(" -c, --class Classification level_1/level_2/level_3 (required)\n");
printf(" -p, --payload Record body (max size = 8k) (required)\n");
printf(" -P, --payload-file File to read payload from\n");
printf(" -R, --record-version Version number for format of payload (default 1)\n");
printf(" -e, --event-id Event id to use in the record\n");
printf(" -o, --echo Echo record to stdout\n");
printf(" -n, --no-post Do not post record just print\n");
printf(" -f, --config_file Specify a configuration file other than default\n");
printf("\n");
}
static bool parse_options(int argc, char **argv)
{
bool ret = false;
char *endptr = NULL;
long unsigned int tmp = 0;
int opt;
while ((opt = getopt_long(argc, argv, "hc:Vs:c:p:P:R:e:onf:", prog_opts, NULL)) != -1) {
switch (opt) {
case 'h':
print_help();
exit(EXIT_SUCCESS);
case 'V':
printf(PACKAGE_VERSION "\n");
exit(EXIT_SUCCESS);
case 's':
errno = 0;
tmp = strtoul(optarg, &endptr, 10);
if (errno != 0) {
telem_perror("Failed to convert severity number");
goto fail;
}
if (endptr && *endptr != '\0') {
telem_log(LOG_ERR, "Invalid severity number. Must be an integer\n");
goto fail;
}
severity = (uint32_t)tmp;
break;
case 'c':
if (opt_class != NULL) {
free(opt_class);
}
opt_class = strdup(optarg);
break;
case 'p':
if (opt_payload != NULL) {
free(opt_payload);
}
opt_payload = strdup(optarg);
break;
case 'P':
if (opt_payload_file != NULL) {
free(opt_payload_file);
}
opt_payload_file = strdup(optarg);
break;
case 'R':
errno = 0;
tmp = strtoul(optarg, &endptr, 10);
if (errno != 0) {
telem_perror("Failed to convert record-version number");
goto fail;
}
if (endptr && *endptr != '\0') {
telem_log(LOG_ERR, "Invalid record-version number. Must be an integer\n");
goto fail;
}
payload_version = (uint32_t)tmp;
break;
case 'e':
if (opt_event_id != NULL) {
free(opt_event_id);
}
opt_event_id = strdup(optarg);
if (opt_event_id == NULL) {
goto fail;
}
break;
case 'o':
opt_echo = true;
break;
case 'n':
opt_nopost = true;
break;
case 'f':
if (tm_set_config_file(optarg) != 0) {
telem_log(LOG_ERR, "Configuration file path not valid\n");
exit(EXIT_FAILURE);
}
break;
}
}
ret = true;
fail:
return ret;
}
static bool validate_opts(void)
{
size_t i, len;
int x, slashes = 0;
const char alphab[] = EVENT_ID_ALPHAB;
bool ret = false;
/* classification */
if (opt_class == NULL) {
fprintf(stderr, "Error: Classification required. See --help.\n");
return ret;
}
len = strlen(opt_class);
if ((len == 0) || (len > MAX_CLASS_LENGTH)) {
fprintf(stderr, "Error: Valid size for classification "
"is 1-%d chars\n", MAX_CLASS_LENGTH);
return ret;
}
for (int c = 0; c < len; c++) {
if (isascii(opt_class[c]) == 0) {
fprintf(stderr, "Error: Non-ascii characters detected "
"in classification - aborting\n");
return ret;
}
}
for (i = 0, x = 0; i <= (len - 1); i++, x++) {
if (opt_class[i] == '/') {
slashes++;
x = 0;
} else {
if (x > MAX_SUBCAT_LENGTH) {
fprintf(stderr, "Error: Classification strings"
" between slashes should have at most"
" %d chars\n", MAX_SUBCAT_LENGTH);
return ret;
}
}
}
if (slashes != 2) {
fprintf(stderr, "Error: Classification needs to be in "
"most/to/least specific format, 2 \'/\' required.\n");
return ret;
}
/* Severity */
if ((severity) < 1 || (severity > 4)) {
fprintf(stderr, "Error: Valid range for severity is 1-4\n");
return ret;
}
/* Event id */
if (opt_event_id) {
size_t result = 0;
if ((result = strlen(opt_event_id)) != EVENT_ID_LEN) {
fprintf(stderr, "Error: event_id length %zu it should have %d characters\n", result, EVENT_ID_LEN);
return ret;
}
if ((result = strspn(opt_event_id, alphab)) != EVENT_ID_LEN) {
fprintf(stderr, "Error: event_id contains invalid character '%c' at position %zu, valid characters are %s\n",
opt_event_id[result], result, alphab);
return ret;
}
}
return true;
}
static bool get_payload_from_file(char **payload)
{
bool ret = false;
FILE *fp = NULL;
size_t bytes_in = 0;
fp = fopen(opt_payload_file, "r");
if (!fp) {
fprintf(stderr, "Error: file %s could not be opened "
"for reading\n", opt_payload_file);
goto out;
}
bytes_in = fread(*payload, 1,
MAX_PAYLOAD_LENGTH - 1, fp);
/* if fread fails */
if (bytes_in == 0 && ferror(fp) != 0) {
goto out;
}
ret = true;
out:
if (fp) {
fclose(fp);
}
return ret;
}
static void get_payload_from_opt(char **payload)
{
size_t len = 0;
len = strlen(opt_payload);
if (len >= MAX_PAYLOAD_LENGTH) {
len = MAX_PAYLOAD_LENGTH - 1;
}
/* "payload" is pre-allocated zeroed buffer of MAX_PAYLOAD_LENGTH */
memcpy(*payload, opt_payload, len);
}
static void get_payload_from_stdin(char **payload)
{
size_t bytes_in = 0;
int c;
bytes_in = fread(*payload, 1, MAX_PAYLOAD_LENGTH - 1, stdin);
if (bytes_in == MAX_PAYLOAD_LENGTH - 1) {
/* Throw away the rest of stdin */
while (EOF != (c = fgetc(stdin))) {
continue;
}
}
}
static bool get_payload(char **payload)
{
bool ret = false;
*payload = (char *)calloc(sizeof(char), MAX_PAYLOAD_LENGTH);
if (*payload == NULL)
return false;
if (opt_payload_file) {
if (get_payload_from_file(payload)) {
ret = true;
}
} else if (opt_payload) {
get_payload_from_opt(payload);
ret = true;
} else {
get_payload_from_stdin(payload);
ret = true;
}
if (ret == false) {
free(*payload);
*payload = NULL;
}
return ret;
}
static int instanciate_record(struct telem_ref **t_ref, char *payload)
{
int ret = 0;
if ((ret = tm_create_record(t_ref, (uint32_t)severity,
opt_class, payload_version)) < 0) {
goto out1;
}
if (opt_event_id) {
if ((ret = tm_set_event_id(*t_ref, opt_event_id)) < 0) {
goto out1;
}
}
ret = tm_set_payload(*t_ref, payload);
out1:
return ret;
}
static int send_record(char *payload)
{
struct telem_ref *t_ref = NULL;
int ret = 0;
if ((ret = instanciate_record(&t_ref, payload)) < 0) {
goto out;
}
if ((ret = tm_send_record(t_ref)) < 0) {
if (ret == -ECONNREFUSED) {
fprintf(stderr, "Unable to send record. Make sure to opt-in to telemetry first 'telemctl opt-in'\n");
}
goto out;
}
out:
tm_free_record(t_ref);
return ret;
}
static int print_record(char *payload)
{
struct telem_ref *t_ref = NULL;
int ret;
if ((ret = instanciate_record(&t_ref, payload)) == 0) {
int i;
for (i = 0; i < NUM_HEADERS; i++) {
fprintf(stdout, "%s", t_ref->record->headers[i]);
}
fprintf(stdout, "%s\n", t_ref->record->payload);
}
tm_free_record(t_ref);
return ret;
}
int main(int argc, char **argv)
{
int ret = EXIT_FAILURE;
char *payload = NULL;
if (!parse_options(argc, argv)) {
goto fail;
}
if (!validate_opts()) {
goto fail;
}
if (!get_payload(&payload)) {
goto fail;
}
if (opt_echo == true) {
print_record(payload);
}
if (opt_nopost == false && send_record(payload) != 0) {
goto fail;
}
ret = EXIT_SUCCESS;
fail:
free(opt_class);
free(opt_payload);
free(opt_event_id);
free(payload);
return ret;
}
/* vi: set ts=8 sw=8 sts=4 et tw=80 cino=(0: */