blob: 47d2f3b8fb02d3250ab64b32e607df5ea0ea956b [file] [log] [blame]
aliguori065e2812008-11-11 16:46:33 +00001/*
2 * QEMU live migration
3 *
4 * Copyright IBM, Corp. 2008
5 * Copyright Dell MessageOne 2008
Daniel P. Berrange527792f2016-04-27 11:05:06 +01006 * Copyright Red Hat, Inc. 2015-2016
aliguori065e2812008-11-11 16:46:33 +00007 *
8 * Authors:
9 * Anthony Liguori <aliguori@us.ibm.com>
10 * Charles Duffy <charles_duffy@messageone.com>
Daniel P. Berrange527792f2016-04-27 11:05:06 +010011 * Daniel P. Berrange <berrange@redhat.com>
aliguori065e2812008-11-11 16:46:33 +000012 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010016 * Contributions after 2012-01-13 are licensed under the terms of the
17 * GNU GPL, version 2 or (at your option) any later version.
aliguori065e2812008-11-11 16:46:33 +000018 */
19
Peter Maydell1393a482016-01-26 18:16:54 +000020#include "qemu/osdep.h"
Richard Hendersoncc37d982023-03-15 17:43:13 +000021#include "qemu/error-report.h"
Juan Quinteladd4339c2017-04-17 17:07:04 +020022#include "channel.h"
Juan Quintelaf4dbe1b2017-04-05 15:54:10 +020023#include "exec.h"
Juan Quintela0efc9142018-05-23 11:14:11 +020024#include "migration.h"
Daniel P. Berrange527792f2016-04-27 11:05:06 +010025#include "io/channel-command.h"
26#include "trace.h"
John Berberian, Jrc31772a2023-01-15 20:34:21 -050027#include "qemu/cutils.h"
aliguori065e2812008-11-11 16:46:33 +000028
John Berberian, Jrc31772a2023-01-15 20:34:21 -050029#ifdef WIN32
John Berberian, Jrc31772a2023-01-15 20:34:21 -050030const char *exec_get_cmd_path(void)
31{
32 g_autofree char *detected_path = g_new(char, MAX_PATH);
33 if (GetSystemDirectoryA(detected_path, MAX_PATH) == 0) {
34 warn_report("Could not detect cmd.exe path, using default.");
35 return "C:\\Windows\\System32\\cmd.exe";
36 }
37 pstrcat(detected_path, MAX_PATH, "\\cmd.exe");
38 return g_steal_pointer(&detected_path);
39}
40#endif
aliguori065e2812008-11-11 16:46:33 +000041
Het Galacbab4fa2023-10-23 15:20:46 -030042/* provides the length of strList */
43static int
44str_list_length(strList *list)
45{
46 int len = 0;
47 strList *elem;
48
49 for (elem = list; elem != NULL; elem = elem->next) {
50 len++;
51 }
52
53 return len;
54}
55
56static void
57init_exec_array(strList *command, char **argv, Error **errp)
58{
59 int i = 0;
60 strList *lst;
61
62 for (lst = command; lst; lst = lst->next) {
63 argv[i++] = lst->value;
64 }
65
66 argv[i] = NULL;
67 return;
68}
69
70void exec_start_outgoing_migration(MigrationState *s, strList *command,
71 Error **errp)
aliguori065e2812008-11-11 16:46:33 +000072{
Daniel P. Berrange527792f2016-04-27 11:05:06 +010073 QIOChannel *ioc;
John Berberian, Jrc31772a2023-01-15 20:34:21 -050074
Het Galacbab4fa2023-10-23 15:20:46 -030075 int length = str_list_length(command);
76 g_auto(GStrv) argv = (char **) g_new0(const char *, length + 1);
Daniel P. Berrange527792f2016-04-27 11:05:06 +010077
Het Galacbab4fa2023-10-23 15:20:46 -030078 init_exec_array(command, argv, errp);
79 g_autofree char *new_command = g_strjoinv(" ", (char **)argv);
80
81 trace_migration_exec_outgoing(new_command);
82 ioc = QIO_CHANNEL(
83 qio_channel_command_new_spawn(
84 (const char * const *) g_steal_pointer(&argv),
85 O_RDWR,
86 errp));
Daniel P. Berrange527792f2016-04-27 11:05:06 +010087 if (!ioc) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +020088 return;
aliguori065e2812008-11-11 16:46:33 +000089 }
90
Daniel P. Berrange6f01f132016-09-30 11:57:14 +010091 qio_channel_set_name(ioc, "migration-exec-outgoing");
Dr. David Alan Gilbert688a3dc2017-12-15 17:16:55 +000092 migration_channel_connect(s, ioc, NULL, NULL);
Daniel P. Berrange527792f2016-04-27 11:05:06 +010093 object_unref(OBJECT(ioc));
aliguori065e2812008-11-11 16:46:33 +000094}
95
Daniel P. Berrange527792f2016-04-27 11:05:06 +010096static gboolean exec_accept_incoming_migration(QIOChannel *ioc,
97 GIOCondition condition,
98 gpointer opaque)
aliguori065e2812008-11-11 16:46:33 +000099{
Juan Quintela54314712017-04-17 17:15:02 +0200100 migration_channel_process_incoming(ioc);
Daniel P. Berrange527792f2016-04-27 11:05:06 +0100101 object_unref(OBJECT(ioc));
Juan Quintela2a543bf2017-07-24 12:51:59 +0200102 return G_SOURCE_REMOVE;
Chris Lalancette8a43b1e2009-05-25 16:38:23 +0200103}
104
Het Galacbab4fa2023-10-23 15:20:46 -0300105void exec_start_incoming_migration(strList *command, Error **errp)
Chris Lalancette8a43b1e2009-05-25 16:38:23 +0200106{
Daniel P. Berrange527792f2016-04-27 11:05:06 +0100107 QIOChannel *ioc;
John Berberian, Jrc31772a2023-01-15 20:34:21 -0500108
Het Galacbab4fa2023-10-23 15:20:46 -0300109 int length = str_list_length(command);
110 g_auto(GStrv) argv = (char **) g_new0(const char *, length + 1);
Chris Lalancette8a43b1e2009-05-25 16:38:23 +0200111
Het Galacbab4fa2023-10-23 15:20:46 -0300112 init_exec_array(command, argv, errp);
113 g_autofree char *new_command = g_strjoinv(" ", (char **)argv);
114
115 trace_migration_exec_incoming(new_command);
116 ioc = QIO_CHANNEL(
117 qio_channel_command_new_spawn(
118 (const char * const *) g_steal_pointer(&argv),
119 O_RDWR,
120 errp));
Daniel P. Berrange527792f2016-04-27 11:05:06 +0100121 if (!ioc) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200122 return;
Chris Lalancette8a43b1e2009-05-25 16:38:23 +0200123 }
124
Daniel P. Berrange6f01f132016-09-30 11:57:14 +0100125 qio_channel_set_name(ioc, "migration-exec-incoming");
Peter Xue89f5ff2018-05-02 18:47:17 +0800126 qio_channel_add_watch_full(ioc, G_IO_IN,
127 exec_accept_incoming_migration,
128 NULL, NULL,
129 g_main_context_get_thread_default());
aliguori065e2812008-11-11 16:46:33 +0000130}