diff options
author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-09-01 11:27:49 +0200 |
---|---|---|
committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-09-01 11:57:36 +0200 |
commit | 28bb2fa0a499b46e279244990b8268c1ed8823bc (patch) | |
tree | 06b9ab4d0773f8011f8e263db1111b2c873bf98f | |
parent | 888cd29202df5b99d4cd7834b11b030f2c39859f (diff) | |
download | monitoring-plugins-28bb2fa0a499b46e279244990b8268c1ed8823bc.tar.gz |
lib/utils_base.c: small refactoring
-rw-r--r-- | lib/perfdata.h | 2 | ||||
-rw-r--r-- | lib/utils_base.c | 195 |
2 files changed, 94 insertions, 103 deletions
diff --git a/lib/perfdata.h b/lib/perfdata.h index 7fd908a9..c5d4a61d 100644 --- a/lib/perfdata.h +++ b/lib/perfdata.h | |||
@@ -45,7 +45,7 @@ typedef struct range_struct { | |||
45 | double start; | 45 | double start; |
46 | bool start_infinity; | 46 | bool start_infinity; |
47 | double end; | 47 | double end; |
48 | int end_infinity; | 48 | bool end_infinity; |
49 | int alert_on; /* OUTSIDE (default) or INSIDE */ | 49 | int alert_on; /* OUTSIDE (default) or INSIDE */ |
50 | char *text; /* original unparsed text input */ | 50 | char *text; /* original unparsed text input */ |
51 | } range; | 51 | } range; |
diff --git a/lib/utils_base.c b/lib/utils_base.c index 5ba865c9..43e88e7a 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c | |||
@@ -46,7 +46,7 @@ monitoring_plugin *this_monitoring_plugin = NULL; | |||
46 | int timeout_state = STATE_CRITICAL; | 46 | int timeout_state = STATE_CRITICAL; |
47 | unsigned int timeout_interval = DEFAULT_SOCKET_TIMEOUT; | 47 | unsigned int timeout_interval = DEFAULT_SOCKET_TIMEOUT; |
48 | 48 | ||
49 | bool _np_state_read_file(FILE *); | 49 | bool _np_state_read_file(FILE *state_file); |
50 | 50 | ||
51 | void np_init(char *plugin_name, int argc, char **argv) { | 51 | void np_init(char *plugin_name, int argc, char **argv) { |
52 | if (this_monitoring_plugin == NULL) { | 52 | if (this_monitoring_plugin == NULL) { |
@@ -153,7 +153,7 @@ range *parse_range_string(char *str) { | |||
153 | set_range_end(temp_range, end); | 153 | set_range_end(temp_range, end); |
154 | } | 154 | } |
155 | 155 | ||
156 | if (temp_range->start_infinity == true || temp_range->end_infinity == true || | 156 | if (temp_range->start_infinity || temp_range->end_infinity || |
157 | temp_range->start <= temp_range->end) { | 157 | temp_range->start <= temp_range->end) { |
158 | return temp_range; | 158 | return temp_range; |
159 | } | 159 | } |
@@ -261,21 +261,21 @@ bool check_range(double value, range *my_range) { | |||
261 | yes = false; | 261 | yes = false; |
262 | } | 262 | } |
263 | 263 | ||
264 | if (my_range->end_infinity == false && my_range->start_infinity == false) { | 264 | if (!my_range->end_infinity && !my_range->start_infinity) { |
265 | if ((my_range->start <= value) && (value <= my_range->end)) { | 265 | if ((my_range->start <= value) && (value <= my_range->end)) { |
266 | return no; | 266 | return no; |
267 | } | 267 | } |
268 | return yes; | 268 | return yes; |
269 | } | 269 | } |
270 | 270 | ||
271 | if (my_range->start_infinity == false && my_range->end_infinity == true) { | 271 | if (!my_range->start_infinity && my_range->end_infinity) { |
272 | if (my_range->start <= value) { | 272 | if (my_range->start <= value) { |
273 | return no; | 273 | return no; |
274 | } | 274 | } |
275 | return yes; | 275 | return yes; |
276 | } | 276 | } |
277 | 277 | ||
278 | if (my_range->start_infinity == true && my_range->end_infinity == false) { | 278 | if (my_range->start_infinity && !my_range->end_infinity) { |
279 | if (value <= my_range->end) { | 279 | if (value <= my_range->end) { |
280 | return no; | 280 | return no; |
281 | } | 281 | } |
@@ -287,12 +287,12 @@ bool check_range(double value, range *my_range) { | |||
287 | /* Returns status */ | 287 | /* Returns status */ |
288 | int get_status(double value, thresholds *my_thresholds) { | 288 | int get_status(double value, thresholds *my_thresholds) { |
289 | if (my_thresholds->critical != NULL) { | 289 | if (my_thresholds->critical != NULL) { |
290 | if (check_range(value, my_thresholds->critical) == true) { | 290 | if (check_range(value, my_thresholds->critical)) { |
291 | return STATE_CRITICAL; | 291 | return STATE_CRITICAL; |
292 | } | 292 | } |
293 | } | 293 | } |
294 | if (my_thresholds->warning != NULL) { | 294 | if (my_thresholds->warning != NULL) { |
295 | if (check_range(value, my_thresholds->warning) == true) { | 295 | if (check_range(value, my_thresholds->warning)) { |
296 | return STATE_WARNING; | 296 | return STATE_WARNING; |
297 | } | 297 | } |
298 | } | 298 | } |
@@ -301,32 +301,31 @@ int get_status(double value, thresholds *my_thresholds) { | |||
301 | 301 | ||
302 | char *np_escaped_string(const char *string) { | 302 | char *np_escaped_string(const char *string) { |
303 | char *data; | 303 | char *data; |
304 | int i; | 304 | int write_index = 0; |
305 | int j = 0; | ||
306 | data = strdup(string); | 305 | data = strdup(string); |
307 | for (i = 0; data[i]; i++) { | 306 | for (int i = 0; data[i]; i++) { |
308 | if (data[i] == '\\') { | 307 | if (data[i] == '\\') { |
309 | switch (data[++i]) { | 308 | switch (data[++i]) { |
310 | case 'n': | 309 | case 'n': |
311 | data[j++] = '\n'; | 310 | data[write_index++] = '\n'; |
312 | break; | 311 | break; |
313 | case 'r': | 312 | case 'r': |
314 | data[j++] = '\r'; | 313 | data[write_index++] = '\r'; |
315 | break; | 314 | break; |
316 | case 't': | 315 | case 't': |
317 | data[j++] = '\t'; | 316 | data[write_index++] = '\t'; |
318 | break; | 317 | break; |
319 | case '\\': | 318 | case '\\': |
320 | data[j++] = '\\'; | 319 | data[write_index++] = '\\'; |
321 | break; | 320 | break; |
322 | default: | 321 | default: |
323 | data[j++] = data[i]; | 322 | data[write_index++] = data[i]; |
324 | } | 323 | } |
325 | } else { | 324 | } else { |
326 | data[j++] = data[i]; | 325 | data[write_index++] = data[i]; |
327 | } | 326 | } |
328 | } | 327 | } |
329 | data[j] = '\0'; | 328 | data[write_index] = '\0'; |
330 | return data; | 329 | return data; |
331 | } | 330 | } |
332 | 331 | ||
@@ -341,33 +340,35 @@ int np_check_if_root(void) { return (geteuid() == 0); } | |||
341 | char *np_extract_value(const char *varlist, const char *name, char sep) { | 340 | char *np_extract_value(const char *varlist, const char *name, char sep) { |
342 | char *tmp = NULL; | 341 | char *tmp = NULL; |
343 | char *value = NULL; | 342 | char *value = NULL; |
344 | int i; | ||
345 | 343 | ||
346 | while (1) { | 344 | while (true) { |
347 | /* Strip any leading space */ | 345 | /* Strip any leading space */ |
348 | for (; isspace(varlist[0]); varlist++) | 346 | for (; isspace(varlist[0]); varlist++) { |
349 | ; | 347 | ; |
348 | } | ||
350 | 349 | ||
351 | if (strncmp(name, varlist, strlen(name)) == 0) { | 350 | if (strncmp(name, varlist, strlen(name)) == 0) { |
352 | varlist += strlen(name); | 351 | varlist += strlen(name); |
353 | /* strip trailing spaces */ | 352 | /* strip trailing spaces */ |
354 | for (; isspace(varlist[0]); varlist++) | 353 | for (; isspace(varlist[0]); varlist++) { |
355 | ; | 354 | ; |
355 | } | ||
356 | 356 | ||
357 | if (varlist[0] == '=') { | 357 | if (varlist[0] == '=') { |
358 | /* We matched the key, go past the = sign */ | 358 | /* We matched the key, go past the = sign */ |
359 | varlist++; | 359 | varlist++; |
360 | /* strip leading spaces */ | 360 | /* strip leading spaces */ |
361 | for (; isspace(varlist[0]); varlist++) | 361 | for (; isspace(varlist[0]); varlist++) { |
362 | ; | 362 | ; |
363 | } | ||
363 | 364 | ||
364 | if ((tmp = index(varlist, sep))) { | 365 | if ((tmp = index(varlist, sep))) { |
365 | /* Value is delimited by a comma */ | 366 | /* Value is delimited by a comma */ |
366 | if (tmp - varlist == 0) { | 367 | if (tmp - varlist == 0) { |
367 | continue; | 368 | continue; |
368 | } | 369 | } |
369 | value = (char *)calloc(1, tmp - varlist + 1); | 370 | value = (char *)calloc(1, (unsigned long)(tmp - varlist + 1)); |
370 | strncpy(value, varlist, tmp - varlist); | 371 | strncpy(value, varlist, (unsigned long)(tmp - varlist)); |
371 | value[tmp - varlist] = '\0'; | 372 | value[tmp - varlist] = '\0'; |
372 | } else { | 373 | } else { |
373 | /* Value is delimited by a \0 */ | 374 | /* Value is delimited by a \0 */ |
@@ -392,7 +393,7 @@ char *np_extract_value(const char *varlist, const char *name, char sep) { | |||
392 | 393 | ||
393 | /* Clean-up trailing spaces/newlines */ | 394 | /* Clean-up trailing spaces/newlines */ |
394 | if (value) { | 395 | if (value) { |
395 | for (i = strlen(value) - 1; isspace(value[i]); i--) { | 396 | for (unsigned long i = strlen(value) - 1; isspace(value[i]); i--) { |
396 | value[i] = '\0'; | 397 | value[i] = '\0'; |
397 | } | 398 | } |
398 | } | 399 | } |
@@ -441,11 +442,7 @@ int mp_translate_state(char *state_text) { | |||
441 | * parse of argv, so that uniqueness in parameters are reflected there. | 442 | * parse of argv, so that uniqueness in parameters are reflected there. |
442 | */ | 443 | */ |
443 | char *_np_state_generate_key(void) { | 444 | char *_np_state_generate_key(void) { |
444 | int i; | ||
445 | char **argv = this_monitoring_plugin->argv; | 445 | char **argv = this_monitoring_plugin->argv; |
446 | char keyname[41]; | ||
447 | char *p = NULL; | ||
448 | |||
449 | unsigned char result[256]; | 446 | unsigned char result[256]; |
450 | 447 | ||
451 | #ifdef USE_OPENSSL | 448 | #ifdef USE_OPENSSL |
@@ -458,7 +455,7 @@ char *_np_state_generate_key(void) { | |||
458 | 455 | ||
459 | EVP_DigestInit(ctx, EVP_sha256()); | 456 | EVP_DigestInit(ctx, EVP_sha256()); |
460 | 457 | ||
461 | for (i = 0; i < this_monitoring_plugin->argc; i++) { | 458 | for (int i = 0; i < this_monitoring_plugin->argc; i++) { |
462 | EVP_DigestUpdate(ctx, argv[i], strlen(argv[i])); | 459 | EVP_DigestUpdate(ctx, argv[i], strlen(argv[i])); |
463 | } | 460 | } |
464 | 461 | ||
@@ -467,24 +464,26 @@ char *_np_state_generate_key(void) { | |||
467 | 464 | ||
468 | struct sha256_ctx ctx; | 465 | struct sha256_ctx ctx; |
469 | 466 | ||
470 | for (i = 0; i < this_monitoring_plugin->argc; i++) { | 467 | for (int i = 0; i < this_monitoring_plugin->argc; i++) { |
471 | sha256_process_bytes(argv[i], strlen(argv[i]), &ctx); | 468 | sha256_process_bytes(argv[i], strlen(argv[i]), &ctx); |
472 | } | 469 | } |
473 | 470 | ||
474 | sha256_finish_ctx(&ctx, result); | 471 | sha256_finish_ctx(&ctx, result); |
475 | #endif // FOUNDOPENSSL | 472 | #endif // FOUNDOPENSSL |
476 | 473 | ||
477 | for (i = 0; i < 20; ++i) { | 474 | char keyname[41]; |
475 | for (int i = 0; i < 20; ++i) { | ||
478 | sprintf(&keyname[2 * i], "%02x", result[i]); | 476 | sprintf(&keyname[2 * i], "%02x", result[i]); |
479 | } | 477 | } |
480 | 478 | ||
481 | keyname[40] = '\0'; | 479 | keyname[40] = '\0'; |
482 | 480 | ||
483 | p = strdup(keyname); | 481 | char *keyname_copy = strdup(keyname); |
484 | if (p == NULL) { | 482 | if (keyname_copy == NULL) { |
485 | die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); | 483 | die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); |
486 | } | 484 | } |
487 | return p; | 485 | |
486 | return keyname_copy; | ||
488 | } | 487 | } |
489 | 488 | ||
490 | void _cleanup_state_data(void) { | 489 | void _cleanup_state_data(void) { |
@@ -525,21 +524,16 @@ char *_np_state_calculate_location_prefix(void) { | |||
525 | * UNKNOWN if exception | 524 | * UNKNOWN if exception |
526 | */ | 525 | */ |
527 | void np_enable_state(char *keyname, int expected_data_version) { | 526 | void np_enable_state(char *keyname, int expected_data_version) { |
528 | state_key *this_state = NULL; | ||
529 | char *temp_filename = NULL; | ||
530 | char *temp_keyname = NULL; | ||
531 | char *p = NULL; | ||
532 | int ret; | ||
533 | |||
534 | if (this_monitoring_plugin == NULL) { | 527 | if (this_monitoring_plugin == NULL) { |
535 | die(STATE_UNKNOWN, _("This requires np_init to be called")); | 528 | die(STATE_UNKNOWN, _("This requires np_init to be called")); |
536 | } | 529 | } |
537 | 530 | ||
538 | this_state = (state_key *)calloc(1, sizeof(state_key)); | 531 | state_key *this_state = (state_key *)calloc(1, sizeof(state_key)); |
539 | if (this_state == NULL) { | 532 | if (this_state == NULL) { |
540 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | 533 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); |
541 | } | 534 | } |
542 | 535 | ||
536 | char *temp_keyname = NULL; | ||
543 | if (keyname == NULL) { | 537 | if (keyname == NULL) { |
544 | temp_keyname = _np_state_generate_key(); | 538 | temp_keyname = _np_state_generate_key(); |
545 | } else { | 539 | } else { |
@@ -548,13 +542,14 @@ void np_enable_state(char *keyname, int expected_data_version) { | |||
548 | die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); | 542 | die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); |
549 | } | 543 | } |
550 | } | 544 | } |
545 | |||
551 | /* Die if invalid characters used for keyname */ | 546 | /* Die if invalid characters used for keyname */ |
552 | p = temp_keyname; | 547 | char *tmp_char = temp_keyname; |
553 | while (*p != '\0') { | 548 | while (*tmp_char != '\0') { |
554 | if (!(isalnum(*p) || *p == '_')) { | 549 | if (!(isalnum(*tmp_char) || *tmp_char == '_')) { |
555 | die(STATE_UNKNOWN, _("Invalid character for keyname - only alphanumerics or '_'")); | 550 | die(STATE_UNKNOWN, _("Invalid character for keyname - only alphanumerics or '_'")); |
556 | } | 551 | } |
557 | p++; | 552 | tmp_char++; |
558 | } | 553 | } |
559 | this_state->name = temp_keyname; | 554 | this_state->name = temp_keyname; |
560 | this_state->plugin_name = this_monitoring_plugin->plugin_name; | 555 | this_state->plugin_name = this_monitoring_plugin->plugin_name; |
@@ -562,9 +557,11 @@ void np_enable_state(char *keyname, int expected_data_version) { | |||
562 | this_state->state_data = NULL; | 557 | this_state->state_data = NULL; |
563 | 558 | ||
564 | /* Calculate filename */ | 559 | /* Calculate filename */ |
565 | ret = asprintf(&temp_filename, "%s/%lu/%s/%s", _np_state_calculate_location_prefix(), | 560 | char *temp_filename = NULL; |
566 | (unsigned long)geteuid(), this_monitoring_plugin->plugin_name, this_state->name); | 561 | int error = |
567 | if (ret < 0) { | 562 | asprintf(&temp_filename, "%s/%lu/%s/%s", _np_state_calculate_location_prefix(), |
563 | (unsigned long)geteuid(), this_monitoring_plugin->plugin_name, this_state->name); | ||
564 | if (error < 0) { | ||
568 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | 565 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); |
569 | } | 566 | } |
570 | 567 | ||
@@ -581,19 +578,17 @@ void np_enable_state(char *keyname, int expected_data_version) { | |||
581 | * if exceptional error. | 578 | * if exceptional error. |
582 | */ | 579 | */ |
583 | state_data *np_state_read(void) { | 580 | state_data *np_state_read(void) { |
584 | state_data *this_state_data = NULL; | ||
585 | FILE *statefile; | ||
586 | bool rc = false; | ||
587 | |||
588 | if (this_monitoring_plugin == NULL) { | 581 | if (this_monitoring_plugin == NULL) { |
589 | die(STATE_UNKNOWN, _("This requires np_init to be called")); | 582 | die(STATE_UNKNOWN, _("This requires np_init to be called")); |
590 | } | 583 | } |
591 | 584 | ||
585 | bool error_code = false; | ||
586 | |||
592 | /* Open file. If this fails, no previous state found */ | 587 | /* Open file. If this fails, no previous state found */ |
593 | statefile = fopen(this_monitoring_plugin->state->_filename, "r"); | 588 | FILE *statefile = fopen(this_monitoring_plugin->state->_filename, "r"); |
594 | if (statefile != NULL) { | 589 | if (statefile != NULL) { |
595 | 590 | ||
596 | this_state_data = (state_data *)calloc(1, sizeof(state_data)); | 591 | state_data *this_state_data = (state_data *)calloc(1, sizeof(state_data)); |
597 | if (this_state_data == NULL) { | 592 | if (this_state_data == NULL) { |
598 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | 593 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); |
599 | } | 594 | } |
@@ -601,12 +596,12 @@ state_data *np_state_read(void) { | |||
601 | this_state_data->data = NULL; | 596 | this_state_data->data = NULL; |
602 | this_monitoring_plugin->state->state_data = this_state_data; | 597 | this_monitoring_plugin->state->state_data = this_state_data; |
603 | 598 | ||
604 | rc = _np_state_read_file(statefile); | 599 | error_code = _np_state_read_file(statefile); |
605 | 600 | ||
606 | fclose(statefile); | 601 | fclose(statefile); |
607 | } | 602 | } |
608 | 603 | ||
609 | if (!rc) { | 604 | if (!error_code) { |
610 | _cleanup_state_data(); | 605 | _cleanup_state_data(); |
611 | } | 606 | } |
612 | 607 | ||
@@ -616,13 +611,17 @@ state_data *np_state_read(void) { | |||
616 | /* | 611 | /* |
617 | * Read the state file | 612 | * Read the state file |
618 | */ | 613 | */ |
619 | bool _np_state_read_file(FILE *f) { | 614 | bool _np_state_read_file(FILE *state_file) { |
615 | time_t current_time; | ||
616 | time(¤t_time); | ||
617 | |||
618 | /* Note: This introduces a limit of 1024 bytes in the string data */ | ||
619 | char *line = (char *)calloc(1, 1024); | ||
620 | if (line == NULL) { | ||
621 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
622 | } | ||
623 | |||
620 | bool status = false; | 624 | bool status = false; |
621 | size_t pos; | ||
622 | char *line; | ||
623 | int i; | ||
624 | int failure = 0; | ||
625 | time_t current_time, data_time; | ||
626 | enum { | 625 | enum { |
627 | STATE_FILE_VERSION, | 626 | STATE_FILE_VERSION, |
628 | STATE_DATA_VERSION, | 627 | STATE_DATA_VERSION, |
@@ -631,16 +630,9 @@ bool _np_state_read_file(FILE *f) { | |||
631 | STATE_DATA_END | 630 | STATE_DATA_END |
632 | } expected = STATE_FILE_VERSION; | 631 | } expected = STATE_FILE_VERSION; |
633 | 632 | ||
634 | time(¤t_time); | 633 | int failure = 0; |
635 | 634 | while (!failure && (fgets(line, 1024, state_file)) != NULL) { | |
636 | /* Note: This introduces a limit of 1024 bytes in the string data */ | 635 | size_t pos = strlen(line); |
637 | line = (char *)calloc(1, 1024); | ||
638 | if (line == NULL) { | ||
639 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
640 | } | ||
641 | |||
642 | while (!failure && (fgets(line, 1024, f)) != NULL) { | ||
643 | pos = strlen(line); | ||
644 | if (line[pos - 1] == '\n') { | 636 | if (line[pos - 1] == '\n') { |
645 | line[pos - 1] = '\0'; | 637 | line[pos - 1] = '\0'; |
646 | } | 638 | } |
@@ -650,32 +642,32 @@ bool _np_state_read_file(FILE *f) { | |||
650 | } | 642 | } |
651 | 643 | ||
652 | switch (expected) { | 644 | switch (expected) { |
653 | case STATE_FILE_VERSION: | 645 | case STATE_FILE_VERSION: { |
654 | i = atoi(line); | 646 | int i = atoi(line); |
655 | if (i != NP_STATE_FORMAT_VERSION) { | 647 | if (i != NP_STATE_FORMAT_VERSION) { |
656 | failure++; | 648 | failure++; |
657 | } else { | 649 | } else { |
658 | expected = STATE_DATA_VERSION; | 650 | expected = STATE_DATA_VERSION; |
659 | } | 651 | } |
660 | break; | 652 | } break; |
661 | case STATE_DATA_VERSION: | 653 | case STATE_DATA_VERSION: { |
662 | i = atoi(line); | 654 | int i = atoi(line); |
663 | if (i != this_monitoring_plugin->state->data_version) { | 655 | if (i != this_monitoring_plugin->state->data_version) { |
664 | failure++; | 656 | failure++; |
665 | } else { | 657 | } else { |
666 | expected = STATE_DATA_TIME; | 658 | expected = STATE_DATA_TIME; |
667 | } | 659 | } |
668 | break; | 660 | } break; |
669 | case STATE_DATA_TIME: | 661 | case STATE_DATA_TIME: { |
670 | /* If time > now, error */ | 662 | /* If time > now, error */ |
671 | data_time = strtoul(line, NULL, 10); | 663 | time_t data_time = strtoul(line, NULL, 10); |
672 | if (data_time > current_time) { | 664 | if (data_time > current_time) { |
673 | failure++; | 665 | failure++; |
674 | } else { | 666 | } else { |
675 | this_monitoring_plugin->state->state_data->time = data_time; | 667 | this_monitoring_plugin->state->state_data->time = data_time; |
676 | expected = STATE_DATA_TEXT; | 668 | expected = STATE_DATA_TEXT; |
677 | } | 669 | } |
678 | break; | 670 | } break; |
679 | case STATE_DATA_TEXT: | 671 | case STATE_DATA_TEXT: |
680 | this_monitoring_plugin->state->state_data->data = strdup(line); | 672 | this_monitoring_plugin->state->state_data->data = strdup(line); |
681 | if (this_monitoring_plugin->state->state_data->data == NULL) { | 673 | if (this_monitoring_plugin->state->state_data->data == NULL) { |
@@ -700,27 +692,24 @@ bool _np_state_read_file(FILE *f) { | |||
700 | * Will die with UNKNOWN if errors | 692 | * Will die with UNKNOWN if errors |
701 | */ | 693 | */ |
702 | void np_state_write_string(time_t data_time, char *data_string) { | 694 | void np_state_write_string(time_t data_time, char *data_string) { |
703 | FILE *fp; | ||
704 | char *temp_file = NULL; | ||
705 | int fd = 0, result = 0; | ||
706 | time_t current_time; | 695 | time_t current_time; |
707 | char *directories = NULL; | ||
708 | char *p = NULL; | ||
709 | |||
710 | if (data_time == 0) { | 696 | if (data_time == 0) { |
711 | time(¤t_time); | 697 | time(¤t_time); |
712 | } else { | 698 | } else { |
713 | current_time = data_time; | 699 | current_time = data_time; |
714 | } | 700 | } |
715 | 701 | ||
702 | int result = 0; | ||
703 | |||
716 | /* If file doesn't currently exist, create directories */ | 704 | /* If file doesn't currently exist, create directories */ |
717 | if (access(this_monitoring_plugin->state->_filename, F_OK) != 0) { | 705 | if (access(this_monitoring_plugin->state->_filename, F_OK) != 0) { |
706 | char *directories = NULL; | ||
718 | result = asprintf(&directories, "%s", this_monitoring_plugin->state->_filename); | 707 | result = asprintf(&directories, "%s", this_monitoring_plugin->state->_filename); |
719 | if (result < 0) { | 708 | if (result < 0) { |
720 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | 709 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); |
721 | } | 710 | } |
722 | 711 | ||
723 | for (p = directories + 1; *p; p++) { | 712 | for (char *p = directories + 1; *p; p++) { |
724 | if (*p == '/') { | 713 | if (*p == '/') { |
725 | *p = '\0'; | 714 | *p = '\0'; |
726 | if ((access(directories, F_OK) != 0) && (mkdir(directories, S_IRWXU) != 0)) { | 715 | if ((access(directories, F_OK) != 0) && (mkdir(directories, S_IRWXU) != 0)) { |
@@ -734,37 +723,39 @@ void np_state_write_string(time_t data_time, char *data_string) { | |||
734 | np_free(directories); | 723 | np_free(directories); |
735 | } | 724 | } |
736 | 725 | ||
726 | char *temp_file = NULL; | ||
737 | result = asprintf(&temp_file, "%s.XXXXXX", this_monitoring_plugin->state->_filename); | 727 | result = asprintf(&temp_file, "%s.XXXXXX", this_monitoring_plugin->state->_filename); |
738 | if (result < 0) { | 728 | if (result < 0) { |
739 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | 729 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); |
740 | } | 730 | } |
741 | 731 | ||
742 | if ((fd = mkstemp(temp_file)) == -1) { | 732 | int temp_file_desc = 0; |
733 | if ((temp_file_desc = mkstemp(temp_file)) == -1) { | ||
743 | np_free(temp_file); | 734 | np_free(temp_file); |
744 | die(STATE_UNKNOWN, _("Cannot create temporary filename")); | 735 | die(STATE_UNKNOWN, _("Cannot create temporary filename")); |
745 | } | 736 | } |
746 | 737 | ||
747 | fp = (FILE *)fdopen(fd, "w"); | 738 | FILE *temp_file_pointer = (FILE *)fdopen(temp_file_desc, "w"); |
748 | if (fp == NULL) { | 739 | if (temp_file_pointer == NULL) { |
749 | close(fd); | 740 | close(temp_file_desc); |
750 | unlink(temp_file); | 741 | unlink(temp_file); |
751 | np_free(temp_file); | 742 | np_free(temp_file); |
752 | die(STATE_UNKNOWN, _("Unable to open temporary state file")); | 743 | die(STATE_UNKNOWN, _("Unable to open temporary state file")); |
753 | } | 744 | } |
754 | 745 | ||
755 | fprintf(fp, "# NP State file\n"); | 746 | fprintf(temp_file_pointer, "# NP State file\n"); |
756 | fprintf(fp, "%d\n", NP_STATE_FORMAT_VERSION); | 747 | fprintf(temp_file_pointer, "%d\n", NP_STATE_FORMAT_VERSION); |
757 | fprintf(fp, "%d\n", this_monitoring_plugin->state->data_version); | 748 | fprintf(temp_file_pointer, "%d\n", this_monitoring_plugin->state->data_version); |
758 | fprintf(fp, "%lu\n", current_time); | 749 | fprintf(temp_file_pointer, "%lu\n", current_time); |
759 | fprintf(fp, "%s\n", data_string); | 750 | fprintf(temp_file_pointer, "%s\n", data_string); |
760 | 751 | ||
761 | fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP); | 752 | fchmod(temp_file_desc, S_IRUSR | S_IWUSR | S_IRGRP); |
762 | 753 | ||
763 | fflush(fp); | 754 | fflush(temp_file_pointer); |
764 | 755 | ||
765 | result = fclose(fp); | 756 | result = fclose(temp_file_pointer); |
766 | 757 | ||
767 | fsync(fd); | 758 | fsync(temp_file_desc); |
768 | 759 | ||
769 | if (result != 0) { | 760 | if (result != 0) { |
770 | unlink(temp_file); | 761 | unlink(temp_file); |