ae23c9
From 50e7aa5e8892cd2f0e3197cc5a0bcf289af7f16d Mon Sep 17 00:00:00 2001
ae23c9
From: Paolo Bonzini <pbonzini@redhat.com>
ae23c9
Date: Fri, 6 Jul 2018 17:56:58 +0200
ae23c9
Subject: [PATCH 198/268] pr-helper: Rework socket path handling
ae23c9
ae23c9
RH-Author: Paolo Bonzini <pbonzini@redhat.com>
ae23c9
Message-id: <20180706175659.30615-9-pbonzini@redhat.com>
ae23c9
Patchwork-id: 81252
ae23c9
O-Subject: [RHEL7.6 qemu-kvm-rhev PATCH 8/9] pr-helper: Rework socket path handling
ae23c9
Bugzilla: 1533158
ae23c9
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
ae23c9
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
ae23c9
RH-Acked-by: Michal Privoznik <mprivozn@redhat.com>
ae23c9
ae23c9
From: Michal Privoznik <mprivozn@redhat.com>
ae23c9
ae23c9
When reviewing Paolo's pr-helper patches I've noticed couple of
ae23c9
problems:
ae23c9
ae23c9
1) socket_path needs to be calculated at two different places
ae23c9
(one for printing out help, the other if socket activation is NOT
ae23c9
used),
ae23c9
ae23c9
2) even though the default socket_path is allocated in
ae23c9
compute_default_paths() it is the only default path the function
ae23c9
handles. For instance, pidfile is allocated outside of this
ae23c9
function. And yet again, at different places than 1)
ae23c9
ae23c9
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
ae23c9
Message-Id: <c791ba035f26ea957e8f3602e3009b621769b1ba.1530611283.git.mprivozn@redhat.com>
ae23c9
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
ae23c9
(cherry picked from commit 2729d79d4993099782002c9a218de1fc12c32c69)
ae23c9
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
ae23c9
---
ae23c9
 scsi/qemu-pr-helper.c | 36 ++++++++++--------------------------
ae23c9
 1 file changed, 10 insertions(+), 26 deletions(-)
ae23c9
ae23c9
diff --git a/scsi/qemu-pr-helper.c b/scsi/qemu-pr-helper.c
ae23c9
index c89a446..1528a71 100644
ae23c9
--- a/scsi/qemu-pr-helper.c
ae23c9
+++ b/scsi/qemu-pr-helper.c
ae23c9
@@ -76,14 +76,12 @@ static int gid = -1;
ae23c9
 
ae23c9
 static void compute_default_paths(void)
ae23c9
 {
ae23c9
-    if (!socket_path) {
ae23c9
-        socket_path = qemu_get_local_state_pathname("run/qemu-pr-helper.sock");
ae23c9
-    }
ae23c9
+    socket_path = qemu_get_local_state_pathname("run/qemu-pr-helper.sock");
ae23c9
+    pidfile = qemu_get_local_state_pathname("run/qemu-pr-helper.pid");
ae23c9
 }
ae23c9
 
ae23c9
 static void usage(const char *name)
ae23c9
 {
ae23c9
-    compute_default_paths();
ae23c9
     (printf) (
ae23c9
 "Usage: %s [OPTIONS] FILE\n"
ae23c9
 "Persistent Reservation helper program for QEMU\n"
ae23c9
@@ -841,19 +839,6 @@ static gboolean accept_client(QIOChannel *ioc, GIOCondition cond, gpointer opaqu
ae23c9
     return TRUE;
ae23c9
 }
ae23c9
 
ae23c9
-
ae23c9
-/*
ae23c9
- * Check socket parameters compatibility when socket activation is used.
ae23c9
- */
ae23c9
-static const char *socket_activation_validate_opts(void)
ae23c9
-{
ae23c9
-    if (socket_path != NULL) {
ae23c9
-        return "Unix socket can't be set when using socket activation";
ae23c9
-    }
ae23c9
-
ae23c9
-    return NULL;
ae23c9
-}
ae23c9
-
ae23c9
 static void termsig_handler(int signum)
ae23c9
 {
ae23c9
     atomic_cmpxchg(&state, RUNNING, TERMINATE);
ae23c9
@@ -927,6 +912,7 @@ int main(int argc, char **argv)
ae23c9
     char *trace_file = NULL;
ae23c9
     bool daemonize = false;
ae23c9
     bool pidfile_specified = false;
ae23c9
+    bool socket_path_specified = false;
ae23c9
     unsigned socket_activation;
ae23c9
 
ae23c9
     struct sigaction sa_sigterm;
ae23c9
@@ -943,12 +929,14 @@ int main(int argc, char **argv)
ae23c9
     qemu_add_opts(&qemu_trace_opts);
ae23c9
     qemu_init_exec_dir(argv[0]);
ae23c9
 
ae23c9
-    pidfile = qemu_get_local_state_pathname("run/qemu-pr-helper.pid");
ae23c9
+    compute_default_paths();
ae23c9
 
ae23c9
     while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
ae23c9
         switch (ch) {
ae23c9
         case 'k':
ae23c9
-            socket_path = optarg;
ae23c9
+            g_free(socket_path);
ae23c9
+            socket_path = g_strdup(optarg);
ae23c9
+            socket_path_specified = true;
ae23c9
             if (socket_path[0] != '/') {
ae23c9
                 error_report("socket path must be absolute");
ae23c9
                 exit(EXIT_FAILURE);
ae23c9
@@ -1039,10 +1027,9 @@ int main(int argc, char **argv)
ae23c9
     socket_activation = check_socket_activation();
ae23c9
     if (socket_activation == 0) {
ae23c9
         SocketAddress saddr;
ae23c9
-        compute_default_paths();
ae23c9
         saddr = (SocketAddress){
ae23c9
             .type = SOCKET_ADDRESS_TYPE_UNIX,
ae23c9
-            .u.q_unix.path = g_strdup(socket_path)
ae23c9
+            .u.q_unix.path = socket_path,
ae23c9
         };
ae23c9
         server_ioc = qio_channel_socket_new();
ae23c9
         if (qio_channel_socket_listen_sync(server_ioc, &saddr, &local_err) < 0) {
ae23c9
@@ -1050,12 +1037,10 @@ int main(int argc, char **argv)
ae23c9
             error_report_err(local_err);
ae23c9
             return 1;
ae23c9
         }
ae23c9
-        g_free(saddr.u.q_unix.path);
ae23c9
     } else {
ae23c9
         /* Using socket activation - check user didn't use -p etc. */
ae23c9
-        const char *err_msg = socket_activation_validate_opts();
ae23c9
-        if (err_msg != NULL) {
ae23c9
-            error_report("%s", err_msg);
ae23c9
+        if (socket_path_specified) {
ae23c9
+            error_report("Unix socket can't be set when using socket activation");
ae23c9
             exit(EXIT_FAILURE);
ae23c9
         }
ae23c9
 
ae23c9
@@ -1072,7 +1057,6 @@ int main(int argc, char **argv)
ae23c9
                          error_get_pretty(local_err));
ae23c9
             exit(EXIT_FAILURE);
ae23c9
         }
ae23c9
-        socket_path = NULL;
ae23c9
     }
ae23c9
 
ae23c9
     if (qemu_init_main_loop(&local_err)) {
ae23c9
-- 
ae23c9
1.8.3.1
ae23c9