-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
745 lines (693 loc) · 18.7 KB
/
main.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
/*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License") 1.1!
* You may not use this file except in compliance with the License.
*
* See https://spdx.org/licenses/CDDL-1.1.html for the specific
* language governing permissions and limitations under the License.
*
* Copyright 2021 Jens Elkner (jel+ipmimex-src@cs.ovgu.de)
*/
#include <getopt.h>
#include <errno.h>
#include <arpa/inet.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <regex.h>
#include <prom.h>
#include <microhttpd.h>
#include "common.h"
#include "init.h"
#include "prom_ipmi.h"
#include "ipmi_sdr.h"
#include "ipmi_if.h"
typedef enum {
SMF_EXIT_OK = 0,
SMF_EXIT_ERR_OTHER,
SMF_EXIT_ERR_FATAL = 95,
SMF_EXIT_ERR_CONFIG,
SMF_EXIT_MON_DEGRADE,
SMF_EXIT_MON_OFFLINE,
SMF_EXIT_ERR_NOSMF,
SMF_EXIT_ERR_PERM,
SMF_EXIT_TEMP_DISABLE,
SMF_EXIT_TEMP_TRANSIENT
} SMF_EXIT_CODE;
static struct option options[] = {
{"ignore-disabled-flag",no_argument, NULL, 'D'},
{"no-scrapetime", no_argument, NULL, 'L'},
{"drop-no-read", no_argument, NULL, 'N'},
{"no-powerstats", no_argument, NULL, 'P'},
{"no-scrapetime-all", no_argument, NULL, 'S'},
{"no-thresholds", no_argument, NULL, 'T'},
{"no-state", no_argument, NULL, 'U'},
{"version", no_argument, NULL, 'V'},
{"bmc", required_argument, NULL, 'b'},
{"compact", no_argument, NULL, 'c'},
{"daemon", no_argument, NULL, 'd'},
{"foreground", no_argument, NULL, 'f'},
{"help", no_argument, NULL, 'h'},
{"logfile", required_argument, NULL, 'l'},
{"no-metrics", required_argument, NULL, 'n'},
{"overview", no_argument, NULL, 'o'},
{"port", required_argument, NULL, 'p'},
{"source", required_argument, NULL, 's'},
{"verbosity", required_argument, NULL, 'v'},
{"exclude-metrics", required_argument, NULL, 'x'},
{"exclude-sensors", required_argument, NULL, 'X'},
{"include-metrics", required_argument, NULL, 'i'},
{"include-sensors", required_argument, NULL, 'I'},
{0, 0, 0, 0}
};
static const char *shortUsage = {
"[-DLNSVcdfho] [-b path] [-l file] [-s ip] [-p port] [-v DEBUG|INFO|WARN|ERROR|FATAL] [-x mregex] [-X sregex] [-i mregex] [-I sregex]"
};
static struct {
uint32_t promflags;
bool versionInfo;
prom_counter_t *req_counter;
prom_counter_t *res_counter;
struct MHD_Daemon *daemon;
uint16_t port;
struct in6_addr *addr;
bool ipv6;
int MHD_error;
char *logfile;
sensor_t *sensor_list;
bool no_powerstats;
bool ipmitool;
scan_cfg_t scfg;
} global = {
.promflags = PROM_PROCESS | PROM_SCRAPETIME | PROM_SCRAPETIME_ALL,
.versionInfo = true,
.req_counter = NULL,
.res_counter = NULL,
.daemon = NULL,
.port = 9290,
.addr = NULL,
.ipv6 = false,
.MHD_error = -1,
.logfile = NULL,
.sensor_list = NULL,
.no_powerstats = false,
.ipmitool = false,
.scfg = {
.bmc = NULL,
.drop_no_read = false,
.ignore_disabled_flag = false,
.no_state = false,
.no_thresholds = false,
.no_ipmi = false,
.no_dcmi = false,
.exc_metrics = NULL,
.exc_sensors = NULL,
.inc_metrics = NULL,
.inc_sensors = NULL
}
};
static int
disableMetrics(char *skipList) {
char *clist, *s, *e;
int res = 0;
size_t len = strlen(skipList);
if (len == 0)
return 0;
clist = strdup(skipList); // preserve original
e = clist + len;
s = e;
while (s > clist) {
s--;
if (*s != ',' && s != clist)
continue;
if (s != clist)
s++;
if (s != e) {
if (strcmp(s, "process") == 0)
global.promflags &= ~PROM_PROCESS;
else if (strcmp(s, "version") == 0)
global.versionInfo = false;
else if (strcmp(s, "dcmi") == 0)
global.scfg.no_dcmi = true;
else if (strcmp(s, "ipmi") == 0)
global.scfg.no_ipmi = true;
else {
PROM_WARN("Unknown metrics '%s'", s);
res++;
}
}
if (s != clist)
s--;
*s = '\0';
e = s;
}
free(clist);
return res;
}
// Just in case, someone switches to MHD_USE_THREAD_PER_CONNECTION
static _Thread_local psb_t *sb = NULL;
static prom_map_t *
collect(prom_collector_t *self) {
bool compact = global.promflags & PROM_COMPACT;
PROM_DEBUG("collector: %p sb: %p", self, sb);
if (global.versionInfo)
getVersions(sb, compact);
if (!global.scfg.no_ipmi) {
if (sdrs_changed(global.sensor_list)) {
uint32_t n;
PROM_INFO("SDR repo changed. Reloading ...", "");
stop(global.sensor_list);
global.sensor_list =
start(&(global.scfg), global.promflags & PROM_COMPACT, &n);
}
collect_ipmi(sb, global.sensor_list);
}
if (!global.scfg.no_dcmi)
collect_dcmi(sb, global.promflags & PROM_COMPACT, global.no_powerstats);
if (sb != NULL && !compact)
psb_add_char(sb, '\n');
return NULL;
}
// generate the short option string for getopts from <opts>
static char *
getShortOpts(const struct option *opts) {
int i, k = 0, len = 0;
char *str;
while (opts[len].name != NULL)
len++;
str = malloc(sizeof(char) * len * 2 + 1);
if (str == NULL)
return NULL;
str[k++] = '+'; // POSIXLY_CORRECT
for (i = 0; i < len; i++) {
str[k++] = opts[i].val;
if (opts[i].has_arg == required_argument)
str[k++] = ':';
}
str[k] = '\0';
return str;
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
static int
http_handler(void *cls, struct MHD_Connection *connection, const char *url,
const char *method, const char *version, const char *upload_data,
size_t *upload_data_size, void **con_cls)
{
#pragma GCC diagnostic pop
char *body, *s;
size_t len;
struct MHD_Response *response;
enum MHD_ResponseMemoryMode mode = MHD_RESPMEM_PERSISTENT;
unsigned int status = MHD_HTTP_BAD_REQUEST;
static const char *labels[] = { "" };
static char *RESP[] = { NULL, NULL, NULL };
static int rlen[] = { 0, 0, 0 };
int ret;
if (RESP[0] == NULL) {
RESP[0]= strdup("Invalid HTTP Method\n");
rlen[0] = strlen(RESP[0]);
RESP[1]= strdup("<html><body>See <a href='/metrics'>/metrics</a>.\r\n");
rlen[1] = strlen(RESP[1]);
RESP[2]= strdup("Bad Request\n");
rlen[2] = strlen(RESP[2]);
}
if (strcmp(method, "GET") != 0) {
body = RESP[0];
len = rlen[0];
labels[0] = "other";
} else if (strcmp(url, "/") == 0) {
body = RESP[1];
len = rlen[1];
status = MHD_HTTP_OK;
labels[0] = "/";
} else if (strcmp(url, "/metrics") == 0) {
// trick 17: collect() adds stuff to sb directly, when it gets invoked
// indirectly by pcr_bridge(). Therefore: thread local
if (sb != NULL)
PROM_WARN("stringBuilder %p is already there =8-(", sb);
sb = psb_new();
s = pcr_bridge(PROM_COLLECTOR_REGISTRY);
psb_add_str(sb, s); // add libprom metrics
free(s); // avoid mem leaks
body = psb_dump(sb);
len = psb_len(sb);
psb_destroy(sb); // avoid mem leaks on thread exit
sb = NULL;
labels[0] = "/metrics";
mode = MHD_RESPMEM_MUST_FREE;
status = MHD_HTTP_OK;
} else if (global.ipmitool && (strcmp(url, "/overview") == 0)) {
if (sb != NULL)
PROM_WARN("stringBuilder %p is already there =8-(", sb);
sb = psb_new();
show_ipmitool_sensors(global.sensor_list, sb, true);
body = psb_dump(sb);
len = psb_len(sb);
psb_destroy(sb); // avoid mem leaks on thread exit
sb = NULL;
labels[0] = "/overview";
mode = MHD_RESPMEM_MUST_FREE;
status = MHD_HTTP_OK;
} else {
body = RESP[2];
len = rlen[2];
labels[0] = "other";
}
prom_counter_inc(global.req_counter, labels);
response = MHD_create_response_from_buffer(len, body, mode);
if (response == NULL) {
if (mode == MHD_RESPMEM_MUST_FREE)
free(body);
ret = MHD_NO;
} else {
labels[0] = "count";
prom_counter_inc(global.res_counter, labels);
labels[0] = "bytes";
prom_counter_add(global.res_counter, len, labels);
ret = MHD_queue_response(connection, status, response);
MHD_destroy_response(response);
}
return ret;
}
// redirect MHD_DLOG to prom_log
static void
MHD_logger(void *cls, const char *fmt, va_list ap) {
static char s[256];
// the experimental API has loglevel decision support, but it is usually n/a
(void) cls; // unused
vsnprintf(s, sizeof(s), fmt, ap);
// since MHD does not return error details but usually logs the reason for
// an error before polluting errno again, we capture it here. At least for
// MHD_start_daemon() it should be sufficient.
global.MHD_error = errno;
prom_log(PLL_WARN, (const char*) s);
}
static int
setupProm(void) {
static const char *keys[] = { NULL };
prom_collector_t* pc = NULL;
prom_counter_t *reqc, *resc;
reqc = resc = NULL;
if (pcr_init(global.promflags, "ipmimex_"))
return 1;
keys[0] = "url";
if((global.req_counter = prom_counter_new("request_total",
"Number of HTTP requests seen since the start of the exporter "
"excl. the current one.",
1, keys)) == NULL)
goto fail;
reqc = global.req_counter;
if (pcr_register_metric(global.req_counter))
goto fail;
reqc = NULL;
keys[0] = "type";
if ((global.res_counter = prom_counter_new("response_total",
"HTTP responses by count and bytes excl. this response and "
"HTTP headers seen since the start of the exporter.",
1, keys)) == NULL)
goto fail;
resc = global.res_counter;
if (pcr_register_metric(global.res_counter))
goto fail;
resc = NULL;
pc = prom_collector_new("ipmi");
if (pc == NULL)
goto fail;
prom_collector_set_collect_fn(pc, &collect);
if (pcr_register_collector(PROM_COLLECTOR_REGISTRY, pc) == 0)
pc = NULL;
else
goto fail;
return 0;
fail:
if (pc != NULL)
prom_collector_destroy(pc);
if (reqc != NULL)
prom_counter_destroy(reqc);
if (resc != NULL)
prom_counter_destroy(resc);
global.req_counter = global.res_counter = NULL;
pcr_destroy(PROM_COLLECTOR_REGISTRY);
return 1;
}
static void
cleanupProm(void) {
pcr_destroy(PROM_COLLECTOR_REGISTRY);
global.req_counter = global.res_counter = NULL;
}
static int
startHttpServer(void) {
struct sockaddr *addr = NULL;
uint32_t flags = MHD_USE_DEBUG; // same as MHD_USE_ERROR_LOG
// since there is no way to use a blocking, i.e. one (this) thread only
// MHD_run(), or MHD_{e?poll|select}, or MHD_polling_thread.
// same as MHD_USE_INTERNAL_POLLING_THREAD but backward compatible
flags |= MHD_USE_SELECT_INTERNALLY;
if (MHD_is_feature_supported(MHD_FEATURE_EPOLL) == MHD_YES)
flags |= MHD_USE_EPOLL;
else if (MHD_is_feature_supported(MHD_FEATURE_POLL) == MHD_YES)
flags |= MHD_USE_POLL;
if (global.addr != NULL) {
struct sockaddr_in v4addr;
struct sockaddr_in6 v6addr;
size_t len;
char buf[64];
buf[0] = '\0';
if (global.ipv6) {
flags |= MHD_USE_IPv6;
len = sizeof (struct sockaddr_in6);
inet_ntop(AF_INET6, global.addr, buf, len);
memset(&v6addr, 0, len);
v6addr.sin6_family = AF_INET6;
v6addr.sin6_port = htons (global.port);
memcpy(&(v6addr.sin6_addr), global.addr, sizeof(struct in6_addr));
addr = (struct sockaddr *) &v6addr;
} else {
len = sizeof (struct sockaddr_in);
inet_ntop(AF_INET, global.addr, buf, len);
memset(&v4addr, 0, len);
v4addr.sin_family = AF_INET;
v4addr.sin_port = htons (global.port);
memcpy(&(v4addr.sin_addr), global.addr, sizeof(struct in_addr));
addr = (struct sockaddr *) &v4addr;
}
PROM_INFO("Listening on IP%s: %s:%u", global.ipv6 ? "v6" : "v4", buf,
global.port);
} else {
PROM_INFO("Listening on IPv4: 0.0.0.0:%u", global.port);
}
global.daemon = MHD_start_daemon(flags, global.port,
/* checkClientFN */ NULL, /* checkClientFN arg */ NULL,
/* requestHandler */ &http_handler, /* requestHandler arg */ NULL,
MHD_OPTION_EXTERNAL_LOGGER, &MHD_logger, /* logstream */ NULL,
MHD_OPTION_SOCK_ADDR, addr,
MHD_OPTION_END);
if (global.daemon == NULL) {
PROM_FATAL("Unable to start http daemon.", "");
return global.MHD_error == EACCES
? SMF_EXIT_ERR_PERM
: SMF_EXIT_ERR_OTHER;
}
return SMF_EXIT_OK;
}
static int
daemonize(void) {
int status;
int pfd[2];
pid_t pid;
sigset_t sset;
sigset_t oset;
// During init phase block all sigs except ABRT. They get unblocked on the
// child once it has notified the parent about its status, and the parent
// exits.
(void) sigfillset(&sset);
(void) sigdelset(&sset, SIGABRT);
(void) sigprocmask(SIG_BLOCK, &sset, &oset);
// comm channel between parent and child
if (pipe(pfd) == -1) {
PROM_FATAL("Unable to create pipe (%s)", strerror(errno));
exit(SMF_EXIT_ERR_OTHER);
}
if ((pid = fork()) == -1) {
PROM_FATAL("Unable to fork process (%s)", strerror(errno));
exit(SMF_EXIT_ERR_OTHER);
}
// parent: wait for the status message from the child and exit immediately
if (pid > 0) {
(void) close(pfd[1]);
if (read(pfd[0], &status, sizeof (status)) == sizeof (status))
_exit(status);
// just in case, comm failed
if ((waitpid(pid, &status, 0) == pid) && WIFEXITED(status))
_exit(WEXITSTATUS(status));
PROM_FATAL("Failed to spawn daemon process.", "");
_exit(SMF_EXIT_ERR_OTHER);
}
// child: cleanup and detach
(void) setsid();
(void) chdir("/");
(void) umask(022);
(void) sigprocmask(SIG_SETMASK, &oset, NULL);
(void) close(pfd[0]);
(void) close(0);
(void) close(1);
(void) close(2);
// just in case NVML or microhttpd use it somewhere
(void) open("/dev/null", O_RDONLY);
if (global.logfile == NULL) {
(void) open("/dev/null", O_WRONLY);
(void) open("/dev/null", O_WRONLY);
} else {
(void) open(global.logfile, O_WRONLY | O_APPEND);
(void) open(global.logfile, O_WRONLY | O_APPEND);
}
return pfd[1];
}
static regex_t *
get_regex(int *res, char *regex, const char *target) {
*res = 0;
if (regex == NULL)
return NULL;
regex_t *r = malloc(sizeof(regex_t));
if (r == NULL) {
perror(target);
*res = 1;
return NULL;
}
*res = regcomp(r, regex, REG_EXTENDED|REG_NOSUB);
if (*res == 0)
return r;
size_t sz = regerror(*res, r, (char *)NULL, (size_t)0);
char *s = malloc(sz);
if (s == NULL) {
perror(target);
free(r);
*res = 1;
return NULL;
}
regerror(*res, r, s, sz);
fprintf(stderr, "Unable to compile regex for %s%s", target, s);
free(s);
free(r);
// since state of preg is undefined, we prefer to not call
// regfree() here.
*res = 1;
return NULL;
}
int
main(int argc, char **argv) {
uint32_t n, mode = 0; // 0 .. oneshot 1 .. foreground 2 .. daemon
int err = 0, res, pfd = -1, status = 0;
struct in_addr inaddr;
struct in6_addr in6addr;
struct in6_addr *addr = malloc(sizeof(struct in6_addr));
psb_t *buf;
char *str = getShortOpts(options);
char *exm = NULL, *exs = NULL, *inm = NULL, *ins = NULL;
while (1) {
int c, optidx = 0;
if (str == NULL)
break;
c = getopt_long (argc, argv, str, options, &optidx);
if (c == -1)
break;
switch (c) {
case 'D':
global.scfg.ignore_disabled_flag = true;
break;
case 'L':
global.promflags &= ~PROM_SCRAPETIME;
break;
case 'N':
global.scfg.drop_no_read = true;
break;
case 'P':
global.no_powerstats = true;
break;
case 'S':
global.promflags &= ~PROM_SCRAPETIME_ALL;
break;
case 'T':
global.scfg.no_thresholds = true;
break;
case 'U':
global.scfg.no_state = true;
break;
case 'V':
getVersions(NULL, 1);
return 0;
case 'b':
if (global.scfg.bmc)
free(global.scfg.bmc);
global.scfg.bmc = strdup(optarg);
break;
case 'c':
global.promflags |= PROM_COMPACT;
break;
case 'd':
mode = 2;
break;
case 'f':
mode = 1;
break;
case 'h':
fprintf(stderr, "Usage: %s %s\n", argv[0], shortUsage);
return 0;
case 'l':
if (global.logfile != NULL)
free(global.logfile);
global.logfile = strdup(optarg);
break;
case 'n':
err += disableMetrics(optarg);
break;
case 'o':
global.ipmitool = true;
break;
case 'p':
if ((sscanf(optarg, "%u", &n) != 1) || n == 0) {
fprintf(stderr, "Invalid port '%s'.\n", optarg);
err++;
} else {
global.port = n;
}
break;
case 's':
if (strstr(optarg, ":") == NULL) {
if ((res = inet_pton(AF_INET, optarg, &inaddr)) == 1)
memcpy(addr, &inaddr, sizeof(struct in_addr));
} else if ((res = inet_pton(AF_INET6, optarg, &in6addr)) == 1) {
if (MHD_is_feature_supported(MHD_FEATURE_IPv6) == MHD_NO) {
fprintf(stderr, "libmicrohttpd has no IPv6 support");
res = 0;
} else {
memcpy(addr, &in6addr, sizeof(struct in6_addr));
global.ipv6 = true;
}
}
if (res != 1) {
fprintf(stderr, "Invalid IP address '%s'.", optarg);
err++;
} else {
global.addr = addr;
addr = NULL;
}
break;
case 'v':
n = prom_log_level_parse(optarg);
if (n == 0) {
fprintf(stderr,"Invalid log level '%s'.\n",optarg);
err++;
} else {
ipmi_verbose++;
prom_log_level(n);
}
break;
case 'x':
if (exm)
free(exm);
exm = strdup(optarg);
break;
case 'X':
if (exs)
free(exs);
exs = strdup(optarg);
break;
case 'i':
if (inm)
free(inm);
inm = strdup(optarg);
break;
case 'I':
if (ins)
free(ins);
ins = strdup(optarg);
break;
case '?':
fprintf(stderr, "Usage: %s %s\n", argv[0], shortUsage);
return(1);
}
}
free(str);
free(addr);
global.scfg.exc_metrics = get_regex(&res, exm, "exclude metrics: ");
free(exm);
err += res;
global.scfg.exc_sensors = get_regex(&res, exs, "exclude sensors: ");
free(exs);
err += res;
global.scfg.inc_metrics = get_regex(&res, inm, "include metrics: ");
free(inm);
err += res;
global.scfg.inc_sensors = get_regex(&res, ins, "include sensors: ");
free(ins);
err += res;
if (err)
return SMF_EXIT_ERR_CONFIG;
if (global.logfile != NULL) {
FILE *logfile = fopen(global.logfile, "a");
if (logfile != NULL)
prom_log_use(logfile);
else {
fprintf(stderr, "Unable to open logfile '%s': %s\n",
global.logfile, strerror(errno));
return (errno == EACCES) ? SMF_EXIT_ERR_PERM : SMF_EXIT_ERR_CONFIG;
}
}
if (mode == 2)
pfd = daemonize();
global.sensor_list =
start(&(global.scfg), global.promflags & PROM_COMPACT, &n);
if (n == 0) {
status = SMF_EXIT_TEMP_DISABLE;
if (mode == 2) {
(void) write(pfd, &status, sizeof (status));
(void) close(pfd);
}
return status;
}
// init
buf = psb_new(); // prevent that prom formatted output goes to stdout
str = getVersions(buf, global.promflags & PROM_COMPACT);
if (mode != 0)
fprintf(stderr, "%s", str);
if (strlen(str)) {
if (mode == 0) {
collect(NULL);
status = SMF_EXIT_OK;
} else if (setupProm() == 0) {
fputs("\n", stderr);
status = startHttpServer();
// let the parent exit
if (mode == 2) {
(void) write(pfd, &status, sizeof (status));
(void) close(pfd);
}
// because libmicrohttpd does not expose blocking calls =8-((((
if (status == SMF_EXIT_OK)
pause();
} else {
status = SMF_EXIT_ERR_OTHER;
if (mode == 2) {
(void) write(pfd, &status, sizeof (status));
(void) close(pfd);
}
}
} else {
fputs("Nothing todo - exiting.\n", stderr);
status = SMF_EXIT_TEMP_DISABLE;
if (mode == 2) {
(void) write(pfd, &status, sizeof (status));
(void) close(pfd);
}
}
// finally
psb_destroy(buf);
cleanupProm();
stop(global.sensor_list);
global.sensor_list = NULL;
free(global.addr);
return status;
}