forked from Bareflank/MicroV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargs.h
157 lines (123 loc) · 4.12 KB
/
args.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// Copyright (C) 2019 Assured Information Security, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef UVCTL_ARGS_H
#define UVCTL_ARGS_H
#ifdef _WIN64
#pragma warning(push)
#pragma warning(disable : 4267)
#include <malloc.h>
#endif
#include "cxxopts.hpp"
#include "log.h"
#ifdef _WIN64
#pragma warning(pop)
#endif
#include <microv/hypercall.h>
#include <cstdlib>
#include <mutex>
using args_type = cxxopts::ParseResult;
inline bool verbose = false;
inline std::mutex orig_arg_mutex;
inline int orig_argc = 0;
inline char **orig_argv = nullptr;
inline args_type parse_args(int argc, char *argv[])
{
using namespace cxxopts;
cxxopts::Options options("uvctl", "control a microv virtual machine");
// clang-format off
options.add_options()
("h,help", "Print this help menu")
("v,verbose", "Enable verbose output")
("affinity", "The host CPU to execute the VM on", value<uint64_t>(), "[core #]")
("kernel", "The VM's kernel", value<std::string>(), "[path]")
("initrd", "The VM's initrd", value<std::string>(), "[path]")
("ram", "The VM's total RAM", value<uint64_t>(), "[bytes]")
("cmdline", "Additional Linux command line arguments", value<std::string>(), "[text]")
("uart", "Give the VM an emulated UART", value<uint64_t>(), "[port #]")
("pt_uart", "Pass-through a host UART to the VM", value<uint64_t>(), "[port #]")
("xsvm", "The VM is a xenstore VM")
("ndvm", "The VM is a network device VM")
("hvc", "Use the hvc console")
#ifdef _WIN64
("windows-svc", "Run uvctl as a Windows service")
#endif
#ifdef XEN_READCONSOLE_ROOTVM
("dmesg", "Read the hypervisor console")
#endif
("high-priority", "Run VM threads at high priority");
// clang-format on
auto args = options.parse(argc, argv);
if (args.count("help")) {
log_msg("%s\n", options.help().c_str());
exit(EXIT_SUCCESS);
}
if (args.count("verbose")) {
verbose = true;
}
return args;
}
inline args_type parse_orig_args()
{
std::lock_guard lock(orig_arg_mutex);
return parse_args(orig_argc, orig_argv);
}
inline int copy_args(int argc, char **argv)
{
if (argc == 0) {
return -1;
}
std::lock_guard lock(orig_arg_mutex);
orig_argc = argc;
orig_argv = (char **)malloc(sizeof(char *) * argc);
if (!orig_argv) {
log_msg("%s: failed to malloc orig_argv\n", __func__);
return -1;
}
for (int i = 0; i < argc; i++) {
orig_argv[i] = (char *)malloc(strlen(argv[i]) + 1);
if (!orig_argv[i]) {
log_msg("%s: failed to malloc orig_argv[%d]\n", __func__, i);
for (int j = i - 1; j >= 0; j--) {
free(orig_argv[j]);
}
goto free_argv;
}
memcpy(orig_argv[i], argv[i], strlen(argv[i]) + 1);
}
return 0;
free_argv:
free(orig_argv);
orig_argv = nullptr;
return -1;
}
inline void free_args()
{
std::lock_guard lock(orig_arg_mutex);
if (!orig_argv) {
return;
}
for (int i = 0; i < orig_argc; i++) {
free(orig_argv[i]);
}
free(orig_argv);
orig_argv = nullptr;
}
#endif