aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU live migration |
| 3 | * |
| 4 | * Copyright IBM, Corp. 2008 |
| 5 | * Copyright Dell MessageOne 2008 |
Daniel P. Berrange | 527792f | 2016-04-27 11:05:06 +0100 | [diff] [blame] | 6 | * Copyright Red Hat, Inc. 2015-2016 |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 7 | * |
| 8 | * Authors: |
| 9 | * Anthony Liguori <aliguori@us.ibm.com> |
| 10 | * Charles Duffy <charles_duffy@messageone.com> |
Daniel P. Berrange | 527792f | 2016-04-27 11:05:06 +0100 | [diff] [blame] | 11 | * Daniel P. Berrange <berrange@redhat.com> |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 12 | * |
| 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 Bonzini | 6b620ca | 2012-01-13 17:44:23 +0100 | [diff] [blame] | 16 | * Contributions after 2012-01-13 are licensed under the terms of the |
| 17 | * GNU GPL, version 2 or (at your option) any later version. |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
Peter Maydell | 1393a48 | 2016-01-26 18:16:54 +0000 | [diff] [blame] | 20 | #include "qemu/osdep.h" |
Richard Henderson | cc37d98 | 2023-03-15 17:43:13 +0000 | [diff] [blame] | 21 | #include "qemu/error-report.h" |
Juan Quintela | dd4339c | 2017-04-17 17:07:04 +0200 | [diff] [blame] | 22 | #include "channel.h" |
Juan Quintela | f4dbe1b | 2017-04-05 15:54:10 +0200 | [diff] [blame] | 23 | #include "exec.h" |
Juan Quintela | 0efc914 | 2018-05-23 11:14:11 +0200 | [diff] [blame] | 24 | #include "migration.h" |
Daniel P. Berrange | 527792f | 2016-04-27 11:05:06 +0100 | [diff] [blame] | 25 | #include "io/channel-command.h" |
| 26 | #include "trace.h" |
John Berberian, Jr | c31772a | 2023-01-15 20:34:21 -0500 | [diff] [blame] | 27 | #include "qemu/cutils.h" |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 28 | |
John Berberian, Jr | c31772a | 2023-01-15 20:34:21 -0500 | [diff] [blame] | 29 | #ifdef WIN32 |
John Berberian, Jr | c31772a | 2023-01-15 20:34:21 -0500 | [diff] [blame] | 30 | const 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 |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 41 | |
Het Gala | cbab4fa | 2023-10-23 15:20:46 -0300 | [diff] [blame] | 42 | /* provides the length of strList */ |
| 43 | static int |
| 44 | str_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 | |
| 56 | static void |
| 57 | init_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 | |
| 70 | void exec_start_outgoing_migration(MigrationState *s, strList *command, |
| 71 | Error **errp) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 72 | { |
Daniel P. Berrange | 527792f | 2016-04-27 11:05:06 +0100 | [diff] [blame] | 73 | QIOChannel *ioc; |
John Berberian, Jr | c31772a | 2023-01-15 20:34:21 -0500 | [diff] [blame] | 74 | |
Het Gala | cbab4fa | 2023-10-23 15:20:46 -0300 | [diff] [blame] | 75 | int length = str_list_length(command); |
| 76 | g_auto(GStrv) argv = (char **) g_new0(const char *, length + 1); |
Daniel P. Berrange | 527792f | 2016-04-27 11:05:06 +0100 | [diff] [blame] | 77 | |
Het Gala | cbab4fa | 2023-10-23 15:20:46 -0300 | [diff] [blame] | 78 | 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. Berrange | 527792f | 2016-04-27 11:05:06 +0100 | [diff] [blame] | 87 | if (!ioc) { |
Paolo Bonzini | f37afb5 | 2012-10-02 10:02:46 +0200 | [diff] [blame] | 88 | return; |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Daniel P. Berrange | 6f01f13 | 2016-09-30 11:57:14 +0100 | [diff] [blame] | 91 | qio_channel_set_name(ioc, "migration-exec-outgoing"); |
Dr. David Alan Gilbert | 688a3dc | 2017-12-15 17:16:55 +0000 | [diff] [blame] | 92 | migration_channel_connect(s, ioc, NULL, NULL); |
Daniel P. Berrange | 527792f | 2016-04-27 11:05:06 +0100 | [diff] [blame] | 93 | object_unref(OBJECT(ioc)); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Daniel P. Berrange | 527792f | 2016-04-27 11:05:06 +0100 | [diff] [blame] | 96 | static gboolean exec_accept_incoming_migration(QIOChannel *ioc, |
| 97 | GIOCondition condition, |
| 98 | gpointer opaque) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 99 | { |
Juan Quintela | 5431471 | 2017-04-17 17:15:02 +0200 | [diff] [blame] | 100 | migration_channel_process_incoming(ioc); |
Daniel P. Berrange | 527792f | 2016-04-27 11:05:06 +0100 | [diff] [blame] | 101 | object_unref(OBJECT(ioc)); |
Juan Quintela | 2a543bf | 2017-07-24 12:51:59 +0200 | [diff] [blame] | 102 | return G_SOURCE_REMOVE; |
Chris Lalancette | 8a43b1e | 2009-05-25 16:38:23 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Het Gala | cbab4fa | 2023-10-23 15:20:46 -0300 | [diff] [blame] | 105 | void exec_start_incoming_migration(strList *command, Error **errp) |
Chris Lalancette | 8a43b1e | 2009-05-25 16:38:23 +0200 | [diff] [blame] | 106 | { |
Daniel P. Berrange | 527792f | 2016-04-27 11:05:06 +0100 | [diff] [blame] | 107 | QIOChannel *ioc; |
John Berberian, Jr | c31772a | 2023-01-15 20:34:21 -0500 | [diff] [blame] | 108 | |
Het Gala | cbab4fa | 2023-10-23 15:20:46 -0300 | [diff] [blame] | 109 | int length = str_list_length(command); |
| 110 | g_auto(GStrv) argv = (char **) g_new0(const char *, length + 1); |
Chris Lalancette | 8a43b1e | 2009-05-25 16:38:23 +0200 | [diff] [blame] | 111 | |
Het Gala | cbab4fa | 2023-10-23 15:20:46 -0300 | [diff] [blame] | 112 | 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. Berrange | 527792f | 2016-04-27 11:05:06 +0100 | [diff] [blame] | 121 | if (!ioc) { |
Paolo Bonzini | 43eaae2 | 2012-10-02 18:21:18 +0200 | [diff] [blame] | 122 | return; |
Chris Lalancette | 8a43b1e | 2009-05-25 16:38:23 +0200 | [diff] [blame] | 123 | } |
| 124 | |
Daniel P. Berrange | 6f01f13 | 2016-09-30 11:57:14 +0100 | [diff] [blame] | 125 | qio_channel_set_name(ioc, "migration-exec-incoming"); |
Peter Xu | e89f5ff | 2018-05-02 18:47:17 +0800 | [diff] [blame] | 126 | qio_channel_add_watch_full(ioc, G_IO_IN, |
| 127 | exec_accept_incoming_migration, |
| 128 | NULL, NULL, |
| 129 | g_main_context_get_thread_default()); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 130 | } |