summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2023-02-12 11:11:38 (GMT)
committerAndreas Baumann <mail@andreasbaumann.cc>2023-02-12 11:11:38 (GMT)
commit40da85e6913ba4898f5a80772c7b3ea0cba0d3eb (patch)
treea278b550cdb02baebfbc28a46b43fdc33c6fa1ef
parentf6978deaa1bf7c6a7196363104ebfcef143080ab (diff)
downloadmonitoring-plugins-40da85e.tar.gz
better cleanup of curl structures and buffers
-rw-r--r--plugins/check_curl.c31
1 files changed, 20 insertions, 11 deletions
diff --git a/plugins/check_curl.c b/plugins/check_curl.c
index 35d1237..a49cac8 100644
--- a/plugins/check_curl.c
+++ b/plugins/check_curl.c
@@ -161,9 +161,13 @@ char *http_post_data = NULL;
161char *http_content_type = NULL; 161char *http_content_type = NULL;
162CURL *curl; 162CURL *curl;
163struct curl_slist *header_list = NULL; 163struct curl_slist *header_list = NULL;
164int body_buf_initialized = 0;
164curlhelp_write_curlbuf body_buf; 165curlhelp_write_curlbuf body_buf;
166int header_buf_initialized = 0;
165curlhelp_write_curlbuf header_buf; 167curlhelp_write_curlbuf header_buf;
168int status_line_initialized = 0;
166curlhelp_statusline status_line; 169curlhelp_statusline status_line;
170int put_buf_initialized = 0;
167curlhelp_read_curlbuf put_buf; 171curlhelp_read_curlbuf put_buf;
168char http_header[DEFAULT_BUFFER_SIZE]; 172char http_header[DEFAULT_BUFFER_SIZE];
169long code; 173long code;
@@ -416,14 +420,12 @@ lookup_host (const char *host, char *buf, size_t buflen)
416static void 420static void
417cleanup (void) 421cleanup (void)
418{ 422{
419 curlhelp_free_statusline(&status_line); 423 if (status_line_initialized) curlhelp_free_statusline(&status_line);
420 curl_easy_cleanup (curl); 424 curl_easy_cleanup (curl);
421 curl_global_cleanup (); 425 curl_global_cleanup ();
422 curlhelp_freewritebuffer (&body_buf); 426 if (body_buf_initialized) curlhelp_freewritebuffer (&body_buf);
423 curlhelp_freewritebuffer (&header_buf); 427 if (header_buf_initialized) curlhelp_freewritebuffer (&header_buf);
424 if (!strcmp (http_method, "PUT")) { 428 if (put_buf_initialized) curlhelp_freereadbuffer (&put_buf);
425 curlhelp_freereadbuffer (&put_buf);
426 }
427} 429}
428 430
429int 431int
@@ -441,9 +443,14 @@ check_http (void)
441 if (curl_global_init (CURL_GLOBAL_DEFAULT) != CURLE_OK) 443 if (curl_global_init (CURL_GLOBAL_DEFAULT) != CURLE_OK)
442 die (STATE_UNKNOWN, "HTTP UNKNOWN - curl_global_init failed\n"); 444 die (STATE_UNKNOWN, "HTTP UNKNOWN - curl_global_init failed\n");
443 445
444 if ((curl = curl_easy_init()) == NULL) 446 if ((curl = curl_easy_init()) == NULL) {
447 curl_global_cleanup ();
445 die (STATE_UNKNOWN, "HTTP UNKNOWN - curl_easy_init failed\n"); 448 die (STATE_UNKNOWN, "HTTP UNKNOWN - curl_easy_init failed\n");
449 }
446 450
451 /* register cleanup function to shut down libcurl properly */
452 atexit (cleanup);
453
447 if (verbose >= 1) 454 if (verbose >= 1)
448 handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_VERBOSE, TRUE), "CURLOPT_VERBOSE"); 455 handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_VERBOSE, TRUE), "CURLOPT_VERBOSE");
449 456
@@ -460,12 +467,14 @@ check_http (void)
460 /* initialize buffer for body of the answer */ 467 /* initialize buffer for body of the answer */
461 if (curlhelp_initwritebuffer(&body_buf) < 0) 468 if (curlhelp_initwritebuffer(&body_buf) < 0)
462 die (STATE_UNKNOWN, "HTTP CRITICAL - out of memory allocating buffer for body\n"); 469 die (STATE_UNKNOWN, "HTTP CRITICAL - out of memory allocating buffer for body\n");
470 body_buf_initialized = 1;
463 handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, (curl_write_callback)curlhelp_buffer_write_callback), "CURLOPT_WRITEFUNCTION"); 471 handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, (curl_write_callback)curlhelp_buffer_write_callback), "CURLOPT_WRITEFUNCTION");
464 handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_WRITEDATA, (void *)&body_buf), "CURLOPT_WRITEDATA"); 472 handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_WRITEDATA, (void *)&body_buf), "CURLOPT_WRITEDATA");
465 473
466 /* initialize buffer for header of the answer */ 474 /* initialize buffer for header of the answer */
467 if (curlhelp_initwritebuffer( &header_buf ) < 0) 475 if (curlhelp_initwritebuffer( &header_buf ) < 0)
468 die (STATE_UNKNOWN, "HTTP CRITICAL - out of memory allocating buffer for header\n" ); 476 die (STATE_UNKNOWN, "HTTP CRITICAL - out of memory allocating buffer for header\n" );
477 header_buf_initialized = 1;
469 handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_HEADERFUNCTION, (curl_write_callback)curlhelp_buffer_write_callback), "CURLOPT_HEADERFUNCTION"); 478 handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_HEADERFUNCTION, (curl_write_callback)curlhelp_buffer_write_callback), "CURLOPT_HEADERFUNCTION");
470 handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_WRITEHEADER, (void *)&header_buf), "CURLOPT_WRITEHEADER"); 479 handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_WRITEHEADER, (void *)&header_buf), "CURLOPT_WRITEHEADER");
471 480
@@ -752,7 +761,9 @@ check_http (void)
752 handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_POSTFIELDS, http_post_data), "CURLOPT_POSTFIELDS"); 761 handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_POSTFIELDS, http_post_data), "CURLOPT_POSTFIELDS");
753 } else if (!strcmp(http_method, "PUT")) { 762 } else if (!strcmp(http_method, "PUT")) {
754 handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_READFUNCTION, (curl_read_callback)curlhelp_buffer_read_callback), "CURLOPT_READFUNCTION"); 763 handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_READFUNCTION, (curl_read_callback)curlhelp_buffer_read_callback), "CURLOPT_READFUNCTION");
755 curlhelp_initreadbuffer (&put_buf, http_post_data, strlen (http_post_data)); 764 if (curlhelp_initreadbuffer (&put_buf, http_post_data, strlen (http_post_data)) < 0)
765 die (STATE_UNKNOWN, "HTTP CRITICAL - out of memory allocating read buffer for PUT\n");
766 put_buf_initialized = 1;
756 handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_READDATA, (void *)&put_buf), "CURLOPT_READDATA"); 767 handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_READDATA, (void *)&put_buf), "CURLOPT_READDATA");
757 handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_INFILESIZE, (curl_off_t)strlen (http_post_data)), "CURLOPT_INFILESIZE"); 768 handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_INFILESIZE, (curl_off_t)strlen (http_post_data)), "CURLOPT_INFILESIZE");
758 } 769 }
@@ -764,9 +775,6 @@ check_http (void)
764 handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_COOKIEFILE, cookie_jar_file), "CURLOPT_COOKIEFILE"); 775 handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_COOKIEFILE, cookie_jar_file), "CURLOPT_COOKIEFILE");
765 } 776 }
766 777
767 /* register cleanup function to shut down libcurl properly */
768 atexit (cleanup);
769
770 /* do the request */ 778 /* do the request */
771 res = curl_easy_perform(curl); 779 res = curl_easy_perform(curl);
772 780
@@ -2159,6 +2167,7 @@ curlhelp_parse_statusline (const char *buf, curlhelp_statusline *status_line)
2159 2167
2160 first_line_len = (size_t)(first_line_end - buf); 2168 first_line_len = (size_t)(first_line_end - buf);
2161 status_line->first_line = (char *)malloc (first_line_len + 1); 2169 status_line->first_line = (char *)malloc (first_line_len + 1);
2170 status_line_initialized = 1;
2162 if (status_line->first_line == NULL) return -1; 2171 if (status_line->first_line == NULL) return -1;
2163 memcpy (status_line->first_line, buf, first_line_len); 2172 memcpy (status_line->first_line, buf, first_line_len);
2164 status_line->first_line[first_line_len] = '\0'; 2173 status_line->first_line[first_line_len] = '\0';