1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include "qemu/osdep.h"
21#include "qemu/error-report.h"
22#include "channel.h"
23#include "exec.h"
24#include "migration.h"
25#include "io/channel-command.h"
26#include "trace.h"
27#include "qemu/cutils.h"
28
29#ifdef WIN32
30const char *exec_get_cmd_path(void);
31const char *exec_get_cmd_path(void)
32{
33 g_autofree char *detected_path = g_new(char, MAX_PATH);
34 if (GetSystemDirectoryA(detected_path, MAX_PATH) == 0) {
35 warn_report("Could not detect cmd.exe path, using default.");
36 return "C:\\Windows\\System32\\cmd.exe";
37 }
38 pstrcat(detected_path, MAX_PATH, "\\cmd.exe");
39 return g_steal_pointer(&detected_path);
40}
41#endif
42
43void exec_start_outgoing_migration(MigrationState *s, const char *command, Error **errp)
44{
45 QIOChannel *ioc;
46
47#ifdef WIN32
48 const char *argv[] = { exec_get_cmd_path(), "/c", command, NULL };
49#else
50 const char *argv[] = { "/bin/sh", "-c", command, NULL };
51#endif
52
53 trace_migration_exec_outgoing(command);
54 ioc = QIO_CHANNEL(qio_channel_command_new_spawn(argv,
55 O_RDWR,
56 errp));
57 if (!ioc) {
58 return;
59 }
60
61 qio_channel_set_name(ioc, "migration-exec-outgoing");
62 migration_channel_connect(s, ioc, NULL, NULL);
63 object_unref(OBJECT(ioc));
64}
65
66static gboolean exec_accept_incoming_migration(QIOChannel *ioc,
67 GIOCondition condition,
68 gpointer opaque)
69{
70 migration_channel_process_incoming(ioc);
71 object_unref(OBJECT(ioc));
72 return G_SOURCE_REMOVE;
73}
74
75void exec_start_incoming_migration(const char *command, Error **errp)
76{
77 QIOChannel *ioc;
78
79#ifdef WIN32
80 const char *argv[] = { exec_get_cmd_path(), "/c", command, NULL };
81#else
82 const char *argv[] = { "/bin/sh", "-c", command, NULL };
83#endif
84
85 trace_migration_exec_incoming(command);
86 ioc = QIO_CHANNEL(qio_channel_command_new_spawn(argv,
87 O_RDWR,
88 errp));
89 if (!ioc) {
90 return;
91 }
92
93 qio_channel_set_name(ioc, "migration-exec-incoming");
94 qio_channel_add_watch_full(ioc, G_IO_IN,
95 exec_accept_incoming_migration,
96 NULL, NULL,
97 g_main_context_get_thread_default());
98}
99