dryang / rpms / systemd

Forked from rpms/systemd a year ago
Clone
ff6046
From c9290315ce840ed1001b897220f3f733811ffc66 Mon Sep 17 00:00:00 2001
ff6046
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
ff6046
Date: Fri, 7 Dec 2018 12:13:10 +0100
ff6046
Subject: [PATCH] =?UTF-8?q?=C2=B5httpd:=20use=20a=20cleanup=20function=20t?=
ff6046
 =?UTF-8?q?o=20call=20MHD=5Fdestroy=5Fresponse?=
ff6046
MIME-Version: 1.0
ff6046
Content-Type: text/plain; charset=UTF-8
ff6046
Content-Transfer-Encoding: 8bit
ff6046
ff6046
(cherry-picked from commit d101fb24eb1c58c97f2adce1f69f4b61a788933a)
ff6046
ff6046
Related: #1664977
ff6046
---
ff6046
 src/journal-remote/journal-gatewayd.c | 53 +++++++--------------------
ff6046
 src/journal-remote/microhttpd-util.c  | 11 ++----
ff6046
 src/journal-remote/microhttpd-util.h  |  2 +
ff6046
 3 files changed, 19 insertions(+), 47 deletions(-)
ff6046
ff6046
diff --git a/src/journal-remote/journal-gatewayd.c b/src/journal-remote/journal-gatewayd.c
ff6046
index 9e77e314ff..3a167ab890 100644
ff6046
--- a/src/journal-remote/journal-gatewayd.c
ff6046
+++ b/src/journal-remote/journal-gatewayd.c
ff6046
@@ -451,7 +451,7 @@ static int request_handler_entries(
ff6046
                 struct MHD_Connection *connection,
ff6046
                 void *connection_cls) {
ff6046
 
ff6046
-        struct MHD_Response *response;
ff6046
+        _cleanup_(MHD_destroy_responsep) struct MHD_Response *response = NULL;
ff6046
         RequestMeta *m = connection_cls;
ff6046
         int r;
ff6046
 
ff6046
@@ -493,11 +493,7 @@ static int request_handler_entries(
ff6046
                 return respond_oom(connection);
ff6046
 
ff6046
         MHD_add_response_header(response, "Content-Type", mime_types[m->mode]);
ff6046
-
ff6046
-        r = MHD_queue_response(connection, MHD_HTTP_OK, response);
ff6046
-        MHD_destroy_response(response);
ff6046
-
ff6046
-        return r;
ff6046
+        return MHD_queue_response(connection, MHD_HTTP_OK, response);
ff6046
 }
ff6046
 
ff6046
 static int output_field(FILE *f, OutputMode m, const char *d, size_t l) {
ff6046
@@ -609,7 +605,7 @@ static int request_handler_fields(
ff6046
                 const char *field,
ff6046
                 void *connection_cls) {
ff6046
 
ff6046
-        struct MHD_Response *response;
ff6046
+        _cleanup_(MHD_destroy_responsep) struct MHD_Response *response = NULL;
ff6046
         RequestMeta *m = connection_cls;
ff6046
         int r;
ff6046
 
ff6046
@@ -632,11 +628,7 @@ static int request_handler_fields(
ff6046
                 return respond_oom(connection);
ff6046
 
ff6046
         MHD_add_response_header(response, "Content-Type", mime_types[m->mode == OUTPUT_JSON ? OUTPUT_JSON : OUTPUT_SHORT]);
ff6046
-
ff6046
-        r = MHD_queue_response(connection, MHD_HTTP_OK, response);
ff6046
-        MHD_destroy_response(response);
ff6046
-
ff6046
-        return r;
ff6046
+        return MHD_queue_response(connection, MHD_HTTP_OK, response);
ff6046
 }
ff6046
 
ff6046
 static int request_handler_redirect(
ff6046
@@ -644,8 +636,7 @@ static int request_handler_redirect(
ff6046
                 const char *target) {
ff6046
 
ff6046
         char *page;
ff6046
-        struct MHD_Response *response;
ff6046
-        int ret;
ff6046
+        _cleanup_(MHD_destroy_responsep) struct MHD_Response *response = NULL;
ff6046
 
ff6046
         assert(connection);
ff6046
         assert(target);
ff6046
@@ -661,11 +652,7 @@ static int request_handler_redirect(
ff6046
 
ff6046
         MHD_add_response_header(response, "Content-Type", "text/html");
ff6046
         MHD_add_response_header(response, "Location", target);
ff6046
-
ff6046
-        ret = MHD_queue_response(connection, MHD_HTTP_MOVED_PERMANENTLY, response);
ff6046
-        MHD_destroy_response(response);
ff6046
-
ff6046
-        return ret;
ff6046
+        return MHD_queue_response(connection, MHD_HTTP_MOVED_PERMANENTLY, response);
ff6046
 }
ff6046
 
ff6046
 static int request_handler_file(
ff6046
@@ -673,8 +660,7 @@ static int request_handler_file(
ff6046
                 const char *path,
ff6046
                 const char *mime_type) {
ff6046
 
ff6046
-        struct MHD_Response *response;
ff6046
-        int ret;
ff6046
+        _cleanup_(MHD_destroy_responsep) struct MHD_Response *response = NULL;
ff6046
         _cleanup_close_ int fd = -1;
ff6046
         struct stat st;
ff6046
 
ff6046
@@ -692,15 +678,10 @@ static int request_handler_file(
ff6046
         response = MHD_create_response_from_fd_at_offset64(st.st_size, fd, 0);
ff6046
         if (!response)
ff6046
                 return respond_oom(connection);
ff6046
-
ff6046
-        fd = -1;
ff6046
+        TAKE_FD(fd);
ff6046
 
ff6046
         MHD_add_response_header(response, "Content-Type", mime_type);
ff6046
-
ff6046
-        ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
ff6046
-        MHD_destroy_response(response);
ff6046
-
ff6046
-        return ret;
ff6046
+        return MHD_queue_response(connection, MHD_HTTP_OK, response);
ff6046
 }
ff6046
 
ff6046
 static int get_virtualization(char **v) {
ff6046
@@ -737,14 +718,13 @@ static int request_handler_machine(
ff6046
                 struct MHD_Connection *connection,
ff6046
                 void *connection_cls) {
ff6046
 
ff6046
-        struct MHD_Response *response;
ff6046
+        _cleanup_(MHD_destroy_responsep) struct MHD_Response *response = NULL;
ff6046
         RequestMeta *m = connection_cls;
ff6046
         int r;
ff6046
         _cleanup_free_ char* hostname = NULL, *os_name = NULL;
ff6046
         uint64_t cutoff_from = 0, cutoff_to = 0, usage = 0;
ff6046
-        char *json;
ff6046
         sd_id128_t mid, bid;
ff6046
-        _cleanup_free_ char *v = NULL;
ff6046
+        _cleanup_free_ char *v = NULL, *json = NULL;
ff6046
 
ff6046
         assert(connection);
ff6046
         assert(m);
ff6046
@@ -793,21 +773,16 @@ static int request_handler_machine(
ff6046
                      usage,
ff6046
                      cutoff_from,
ff6046
                      cutoff_to);
ff6046
-
ff6046
         if (r < 0)
ff6046
                 return respond_oom(connection);
ff6046
 
ff6046
         response = MHD_create_response_from_buffer(strlen(json), json, MHD_RESPMEM_MUST_FREE);
ff6046
-        if (!response) {
ff6046
-                free(json);
ff6046
+        if (!response)
ff6046
                 return respond_oom(connection);
ff6046
-        }
ff6046
+        TAKE_PTR(json);
ff6046
 
ff6046
         MHD_add_response_header(response, "Content-Type", "application/json");
ff6046
-        r = MHD_queue_response(connection, MHD_HTTP_OK, response);
ff6046
-        MHD_destroy_response(response);
ff6046
-
ff6046
-        return r;
ff6046
+        return MHD_queue_response(connection, MHD_HTTP_OK, response);
ff6046
 }
ff6046
 
ff6046
 static int request_handler(
ff6046
diff --git a/src/journal-remote/microhttpd-util.c b/src/journal-remote/microhttpd-util.c
ff6046
index 34dd9ea555..2ae5172fe9 100644
ff6046
--- a/src/journal-remote/microhttpd-util.c
ff6046
+++ b/src/journal-remote/microhttpd-util.c
ff6046
@@ -32,21 +32,16 @@ static int mhd_respond_internal(struct MHD_Connection *connection,
ff6046
                                 const char *buffer,
ff6046
                                 size_t size,
ff6046
                                 enum MHD_ResponseMemoryMode mode) {
ff6046
-        struct MHD_Response *response;
ff6046
-        int r;
ff6046
-
ff6046
         assert(connection);
ff6046
 
ff6046
-        response = MHD_create_response_from_buffer(size, (char*) buffer, mode);
ff6046
+        _cleanup_(MHD_destroy_responsep) struct MHD_Response *response
ff6046
+                = MHD_create_response_from_buffer(size, (char*) buffer, mode);
ff6046
         if (!response)
ff6046
                 return MHD_NO;
ff6046
 
ff6046
         log_debug("Queueing response %u: %s", code, buffer);
ff6046
         MHD_add_response_header(response, "Content-Type", "text/plain");
ff6046
-        r = MHD_queue_response(connection, code, response);
ff6046
-        MHD_destroy_response(response);
ff6046
-
ff6046
-        return r;
ff6046
+        return MHD_queue_response(connection, code, response);
ff6046
 }
ff6046
 
ff6046
 int mhd_respond(struct MHD_Connection *connection,
ff6046
diff --git a/src/journal-remote/microhttpd-util.h b/src/journal-remote/microhttpd-util.h
ff6046
index a50a2a75c7..26909082a1 100644
ff6046
--- a/src/journal-remote/microhttpd-util.h
ff6046
+++ b/src/journal-remote/microhttpd-util.h
ff6046
@@ -73,3 +73,5 @@ int check_permissions(struct MHD_Connection *connection, int *code, char **hostn
ff6046
  * interesting events without overwhelming detail.
ff6046
  */
ff6046
 int setup_gnutls_logger(char **categories);
ff6046
+
ff6046
+DEFINE_TRIVIAL_CLEANUP_FUNC(struct MHD_Response*,  MHD_destroy_response);