]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
ch_monitor: Use automatic mutex management
authorTim Wiederhake <twiederh@redhat.com>
Fri, 25 Mar 2022 09:20:08 +0000 (10:20 +0100)
committerTim Wiederhake <twiederh@redhat.com>
Tue, 5 Apr 2022 13:59:08 +0000 (15:59 +0200)
Signed-off-by: Tim Wiederhake <twiederh@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/ch/ch_monitor.c

index 84085b7991718f00a48813a6dc21ed089ea64fb5..2c6b83a1b503a1760fa7bc7bb448460b2a828afd 100644 (file)
@@ -648,14 +648,13 @@ virCHMonitorCurlPerform(CURL *handle)
 int
 virCHMonitorPutNoContent(virCHMonitor *mon, const char *endpoint)
 {
+    VIR_LOCK_GUARD lock = virObjectLockGuard(mon);
     g_autofree char *url = NULL;
     int responseCode = 0;
     int ret = -1;
 
     url = g_strdup_printf("%s/%s", URL_ROOT, endpoint);
 
-    virObjectLock(mon);
-
     /* reset all options of a libcurl session handle at first */
     curl_easy_reset(mon->handle);
 
@@ -666,8 +665,6 @@ virCHMonitorPutNoContent(virCHMonitor *mon, const char *endpoint)
 
     responseCode = virCHMonitorCurlPerform(mon->handle);
 
-    virObjectUnlock(mon);
-
     if (responseCode == 200 || responseCode == 204)
         ret = 0;
 
@@ -707,26 +704,24 @@ virCHMonitorGet(virCHMonitor *mon, const char *endpoint, virJSONValue **response
 
     url = g_strdup_printf("%s/%s", URL_ROOT, endpoint);
 
-    virObjectLock(mon);
+    VIR_WITH_OBJECT_LOCK_GUARD(mon) {
+        /* reset all options of a libcurl session handle at first */
+        curl_easy_reset(mon->handle);
 
-    /* reset all options of a libcurl session handle at first */
-    curl_easy_reset(mon->handle);
+        curl_easy_setopt(mon->handle, CURLOPT_UNIX_SOCKET_PATH, mon->socketpath);
+        curl_easy_setopt(mon->handle, CURLOPT_URL, url);
 
-    curl_easy_setopt(mon->handle, CURLOPT_UNIX_SOCKET_PATH, mon->socketpath);
-    curl_easy_setopt(mon->handle, CURLOPT_URL, url);
+        if (response) {
+            headers = curl_slist_append(headers, "Accept: application/json");
+            headers = curl_slist_append(headers, "Content-Type: application/json");
+            curl_easy_setopt(mon->handle, CURLOPT_HTTPHEADER, headers);
+            curl_easy_setopt(mon->handle, CURLOPT_WRITEFUNCTION, curl_callback);
+            curl_easy_setopt(mon->handle, CURLOPT_WRITEDATA, (void *)&data);
+        }
 
-    if (response) {
-        headers = curl_slist_append(headers, "Accept: application/json");
-        headers = curl_slist_append(headers, "Content-Type: application/json");
-        curl_easy_setopt(mon->handle, CURLOPT_HTTPHEADER, headers);
-        curl_easy_setopt(mon->handle, CURLOPT_WRITEFUNCTION, curl_callback);
-        curl_easy_setopt(mon->handle, CURLOPT_WRITEDATA, (void *)&data);
+        responseCode = virCHMonitorCurlPerform(mon->handle);
     }
 
-    responseCode = virCHMonitorCurlPerform(mon->handle);
-
-    virObjectUnlock(mon);
-
     if (responseCode == 200 || responseCode == 204) {
         if (response) {
             data.content = g_realloc(data.content, data.size + 1);
@@ -863,20 +858,18 @@ virCHMonitorCreateVM(virCHMonitor *mon,
                                 nnicindexes, nicindexes) != 0)
         return -1;
 
-    virObjectLock(mon);
-
-    /* reset all options of a libcurl session handle at first */
-    curl_easy_reset(mon->handle);
-
-    curl_easy_setopt(mon->handle, CURLOPT_UNIX_SOCKET_PATH, mon->socketpath);
-    curl_easy_setopt(mon->handle, CURLOPT_URL, url);
-    curl_easy_setopt(mon->handle, CURLOPT_CUSTOMREQUEST, "PUT");
-    curl_easy_setopt(mon->handle, CURLOPT_HTTPHEADER, headers);
-    curl_easy_setopt(mon->handle, CURLOPT_POSTFIELDS, payload);
+    VIR_WITH_OBJECT_LOCK_GUARD(mon) {
+        /* reset all options of a libcurl session handle at first */
+        curl_easy_reset(mon->handle);
 
-    responseCode = virCHMonitorCurlPerform(mon->handle);
+        curl_easy_setopt(mon->handle, CURLOPT_UNIX_SOCKET_PATH, mon->socketpath);
+        curl_easy_setopt(mon->handle, CURLOPT_URL, url);
+        curl_easy_setopt(mon->handle, CURLOPT_CUSTOMREQUEST, "PUT");
+        curl_easy_setopt(mon->handle, CURLOPT_HTTPHEADER, headers);
+        curl_easy_setopt(mon->handle, CURLOPT_POSTFIELDS, payload);
 
-    virObjectUnlock(mon);
+        responseCode = virCHMonitorCurlPerform(mon->handle);
+    }
 
     if (responseCode == 200 || responseCode == 204)
         ret = 0;