[PATCH 11/16] remote: Convert SSH tunnel to virCommand

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Cole Robinson <crobinso@xxxxxxxxxx>
---
 src/remote/remote_driver.c |   65 ++++++++++++-------------------------------
 1 files changed, 18 insertions(+), 47 deletions(-)

diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c
index 1072f9f..15f9941 100644
--- a/src/remote/remote_driver.c
+++ b/src/remote/remote_driver.c
@@ -433,8 +433,8 @@ doRemoteOpen (virConnectPtr conn,
     char *name = NULL, *command = NULL, *sockname = NULL, *netcat = NULL;
     char *port = NULL, *authtype = NULL, *username = NULL;
     int no_verify = 0, no_tty = 0;
-    char **cmd_argv = NULL;
     char *pkipath = NULL;
+    virCommandPtr cmd = NULL;
 
     /* Return code from this function, and the private data. */
     int retcode = VIR_DRV_OPEN_ERROR;
@@ -748,50 +748,26 @@ doRemoteOpen (virConnectPtr conn,
     }
 
     case trans_ssh: {
-        int j, nr_args = 6;
-
-        if (username) nr_args += 2; /* For -l username */
-        if (no_tty) nr_args += 5;   /* For -T -o BatchMode=yes -e none */
-        if (port) nr_args += 2;     /* For -p port */
-
-        command = command ? command : strdup ("ssh");
-        if (command == NULL)
-            goto out_of_memory;
+        cmd = virCommandNew(command ? command : "ssh");
 
         /* Generate the final command argv[] array.
-         *   ssh [-p $port] [-l $username] $hostname $netcat -U $sockname [NULL] */
-        if (VIR_ALLOC_N(cmd_argv, nr_args) < 0)
-            goto out_of_memory;
+         *   ssh [-p $port] [-l $username] $hostname $netcat -U $sockname */
 
-        j = 0;
-        cmd_argv[j++] = strdup (command);
         if (port) {
-            cmd_argv[j++] = strdup ("-p");
-            cmd_argv[j++] = strdup (port);
+            virCommandAddArgList(cmd, "-p", port, NULL);
         }
         if (username) {
-            cmd_argv[j++] = strdup ("-l");
-            cmd_argv[j++] = strdup (username);
+            virCommandAddArgList(cmd, "-l", username, NULL);
         }
         if (no_tty) {
-            cmd_argv[j++] = strdup ("-T");
-            cmd_argv[j++] = strdup ("-o");
-            cmd_argv[j++] = strdup ("BatchMode=yes");
-            cmd_argv[j++] = strdup ("-e");
-            cmd_argv[j++] = strdup ("none");
+            virCommandAddArgList(cmd, "-T", "-o", "BatchMode=yes", "-e",
+                                 "none", NULL);
         }
-        cmd_argv[j++] = strdup (priv->hostname);
-        cmd_argv[j++] = strdup (netcat ? netcat : "nc");
-        cmd_argv[j++] = strdup ("-U");
-        cmd_argv[j++] = strdup (sockname ? sockname :
-                                (flags & VIR_CONNECT_RO
-                                 ? LIBVIRTD_PRIV_UNIX_SOCKET_RO
-                                 : LIBVIRTD_PRIV_UNIX_SOCKET));
-        cmd_argv[j++] = 0;
-        assert (j == nr_args);
-        for (j = 0; j < (nr_args-1); j++)
-            if (cmd_argv[j] == NULL)
-                goto out_of_memory;
+        virCommandAddArgList(cmd, priv->hostname, netcat ? netcat : "nc",
+                             "-U", (sockname ? sockname :
+                                    (flags & VIR_CONNECT_RO
+                                     ? LIBVIRTD_PRIV_UNIX_SOCKET_RO
+                                     : LIBVIRTD_PRIV_UNIX_SOCKET)), NULL);
 
         priv->is_secure = 1;
     }
@@ -818,9 +794,11 @@ doRemoteOpen (virConnectPtr conn,
             goto failed;
         }
 
-        if (virExec((const char**)cmd_argv, NULL, NULL,
-                    &pid, sv[1], &(sv[1]), &(errfd[1]),
-                    VIR_EXEC_CLEAR_CAPS) < 0)
+        virCommandSetInputFD(cmd, sv[1]);
+        virCommandSetOutputFD(cmd, &(sv[1]));
+        virCommandSetErrorFD(cmd, &(errfd[1]));
+        virCommandClearCaps(cmd);
+        if (virCommandRunAsync(cmd, &pid) < 0)
             goto failed;
 
         /* Parent continues here. */
@@ -957,14 +935,7 @@ doRemoteOpen (virConnectPtr conn,
     VIR_FREE(netcat);
     VIR_FREE(username);
     VIR_FREE(port);
-    if (cmd_argv) {
-        char **cmd_argv_ptr = cmd_argv;
-        while (*cmd_argv_ptr) {
-            VIR_FREE(*cmd_argv_ptr);
-            cmd_argv_ptr++;
-        }
-        VIR_FREE(cmd_argv);
-    }
+    virCommandFree(cmd);
     VIR_FREE(pkipath);
 
     return retcode;
-- 
1.7.4.4

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]