1 /*
2 * Copyright 2009-2017 Citrix Ltd and other contributors
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; version 2.1 only. with the special
7 * exception on linking described in file LICENSE.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 */
14
15 #include <limits.h>
16 #include <stdlib.h>
17
18 #include <libxl.h>
19 #include <libxl_utils.h>
20 #include <libxlutil.h>
21
22 #include "xl.h"
23 #include "xl_utils.h"
24 #include "xl_parse.h"
25
button_press(uint32_t domid,const char * b)26 static void button_press(uint32_t domid, const char *b)
27 {
28 libxl_trigger trigger;
29
30 if (!strcmp(b, "power")) {
31 trigger = LIBXL_TRIGGER_POWER;
32 } else if (!strcmp(b, "sleep")) {
33 trigger = LIBXL_TRIGGER_SLEEP;
34 } else {
35 fprintf(stderr, "%s is an invalid button identifier\n", b);
36 exit(EXIT_FAILURE);
37 }
38
39 libxl_send_trigger(ctx, domid, trigger, 0, NULL);
40 }
41
main_button_press(int argc,char ** argv)42 int main_button_press(int argc, char **argv)
43 {
44 int opt;
45
46 fprintf(stderr, "WARNING: \"button-press\" is deprecated. "
47 "Please use \"trigger\"\n");
48
49
50 SWITCH_FOREACH_OPT(opt, "", NULL, "button-press", 2) {
51 /* No options */
52 }
53
54 button_press(find_domain(argv[optind]), argv[optind + 1]);
55
56 return 0;
57 }
58
main_rename(int argc,char ** argv)59 int main_rename(int argc, char **argv)
60 {
61 uint32_t domid;
62 int opt;
63 const char *dom, *new_name;
64
65 SWITCH_FOREACH_OPT(opt, "", NULL, "rename", 2) {
66 /* No options */
67 }
68
69 dom = argv[optind++];
70 new_name = argv[optind];
71
72 domid = find_domain(dom);
73 if (libxl_domain_rename(ctx, domid, common_domname, new_name)) {
74 fprintf(stderr, "Can't rename domain '%s'.\n", dom);
75 return 1;
76 }
77
78 return 0;
79 }
80
main_trigger(int argc,char ** argv)81 int main_trigger(int argc, char **argv)
82 {
83 uint32_t domid;
84 int opt;
85 char *endptr = NULL;
86 int vcpuid = 0;
87 const char *trigger_name = NULL;
88 libxl_trigger trigger;
89
90 SWITCH_FOREACH_OPT(opt, "", NULL, "trigger", 2) {
91 /* No options */
92 }
93
94 domid = find_domain(argv[optind++]);
95
96 trigger_name = argv[optind++];
97 if (libxl_trigger_from_string(trigger_name, &trigger)) {
98 fprintf(stderr, "Invalid trigger \"%s\"\n", trigger_name);
99 return EXIT_FAILURE;
100 }
101
102 if (argv[optind]) {
103 vcpuid = strtol(argv[optind], &endptr, 10);
104 if (vcpuid == 0 && !strcmp(endptr, argv[optind])) {
105 fprintf(stderr, "Invalid vcpuid, using default vcpuid=0.\n\n");
106 }
107 }
108
109 libxl_send_trigger(ctx, domid, trigger, vcpuid, NULL);
110
111 return EXIT_SUCCESS;
112 }
113
main_sysrq(int argc,char ** argv)114 int main_sysrq(int argc, char **argv)
115 {
116 uint32_t domid;
117 int opt;
118 const char *sysrq = NULL;
119
120 SWITCH_FOREACH_OPT(opt, "", NULL, "sysrq", 2) {
121 /* No options */
122 }
123
124 domid = find_domain(argv[optind++]);
125
126 sysrq = argv[optind];
127
128 if (sysrq[1] != '\0') {
129 fprintf(stderr, "Invalid sysrq.\n\n");
130 help("sysrq");
131 return EXIT_FAILURE;
132 }
133
134 libxl_send_sysrq(ctx, domid, sysrq[0]);
135
136 return EXIT_SUCCESS;
137 }
138
main_debug_keys(int argc,char ** argv)139 int main_debug_keys(int argc, char **argv)
140 {
141 int opt;
142 char *keys;
143
144 SWITCH_FOREACH_OPT(opt, "", NULL, "debug-keys", 1) {
145 /* No options */
146 }
147
148 keys = argv[optind];
149
150 if (libxl_send_debug_keys(ctx, keys)) {
151 fprintf(stderr, "cannot send debug keys: %s\n", keys);
152 return EXIT_FAILURE;
153 }
154
155 return EXIT_SUCCESS;
156 }
157
main_set_parameters(int argc,char ** argv)158 int main_set_parameters(int argc, char **argv)
159 {
160 int opt;
161 char *params;
162
163 SWITCH_FOREACH_OPT(opt, "", NULL, "set-parameters", 1) {
164 /* No options */
165 }
166
167 params = argv[optind];
168
169 if (libxl_set_parameters(ctx, params)) {
170 fprintf(stderr, "cannot set parameters: %s\n", params);
171 return EXIT_FAILURE;
172 }
173
174 return EXIT_SUCCESS;
175 }
176
main_devd(int argc,char ** argv)177 int main_devd(int argc, char **argv)
178 {
179 int ret = 0, opt = 0, daemonize = 1;
180 const char *pidfile = NULL;
181 static const struct option opts[] = {
182 {"pidfile", 1, 0, 'p'},
183 COMMON_LONG_OPTS,
184 {0, 0, 0, 0}
185 };
186
187 SWITCH_FOREACH_OPT(opt, "Fp:", opts, "devd", 0) {
188 case 'F':
189 daemonize = 0;
190 break;
191 case 'p':
192 pidfile = optarg;
193 break;
194 }
195
196 if (daemonize) {
197 ret = do_daemonize("xldevd", pidfile);
198 if (ret) {
199 ret = (ret == 1) ? 0 : ret;
200 goto out;
201 }
202 }
203
204 ret = libxl_device_events_handler(ctx, 0) ? EXIT_FAILURE : EXIT_SUCCESS;
205
206 out:
207 return ret;
208 }
209
main_qemu_monitor_command(int argc,char ** argv)210 int main_qemu_monitor_command(int argc, char **argv)
211 {
212 int opt;
213 uint32_t domid;
214 char *cmd;
215 char *output;
216 int ret;
217
218 SWITCH_FOREACH_OPT(opt, "", NULL, "qemu-monitor-command", 2) {
219 /* No options */
220 }
221
222 domid = find_domain(argv[optind]);
223 cmd = argv[optind + 1];
224
225 if (argc - optind > 2) {
226 fprintf(stderr, "Invalid arguments.\n");
227 return EXIT_FAILURE;
228 }
229
230 ret = libxl_qemu_monitor_command(ctx, domid, cmd, &output, NULL);
231 if (!ret && output) {
232 printf("%s\n", output);
233 free(output);
234 }
235
236 return ret ? EXIT_FAILURE : EXIT_SUCCESS;
237 }
238
core_dump_domain(uint32_t domid,const char * filename)239 static void core_dump_domain(uint32_t domid, const char *filename)
240 {
241 int rc;
242
243 rc=libxl_domain_core_dump(ctx, domid, filename, NULL);
244 if (rc) { fprintf(stderr,"core dump failed (rc=%d)\n",rc);exit(EXIT_FAILURE); }
245 }
246
main_dump_core(int argc,char ** argv)247 int main_dump_core(int argc, char **argv)
248 {
249 int opt;
250
251 SWITCH_FOREACH_OPT(opt, "", NULL, "dump-core", 2) {
252 /* No options */
253 }
254
255 core_dump_domain(find_domain(argv[optind]), argv[optind + 1]);
256 return EXIT_SUCCESS;
257 }
258
259 extern void printf_info(enum output_format output_format,
260 int domid,
261 libxl_domain_config *d_config, FILE *fh);
main_config_update(int argc,char ** argv)262 int main_config_update(int argc, char **argv)
263 {
264 uint32_t domid;
265 const char *filename = NULL;
266 char *extra_config = NULL;
267 void *config_data = 0;
268 int config_len = 0;
269 libxl_domain_config d_config;
270 int opt, rc;
271 int debug = 0;
272 static struct option opts[] = {
273 {"defconfig", 1, 0, 'f'},
274 COMMON_LONG_OPTS
275 };
276
277 if (argc < 2) {
278 fprintf(stderr, "xl config-update requires a domain argument\n");
279 help("config-update");
280 exit(1);
281 }
282
283 fprintf(stderr, "WARNING: xl now has better capability to manage domain configuration, "
284 "avoid using this command when possible\n");
285
286 domid = find_domain(argv[1]);
287 argc--; argv++;
288
289 if (argv[1] && argv[1][0] != '-' && !strchr(argv[1], '=')) {
290 filename = argv[1];
291 argc--; argv++;
292 }
293
294 SWITCH_FOREACH_OPT(opt, "dqf:", opts, "config_update", 0) {
295 case 'd':
296 debug = 1;
297 break;
298 case 'f':
299 filename = optarg;
300 break;
301 }
302
303 for (; optind < argc; optind++) {
304 if (strchr(argv[optind], '=') != NULL) {
305 string_realloc_append(&extra_config, argv[optind]);
306 string_realloc_append(&extra_config, "\n");
307 } else if (!filename) {
308 filename = argv[optind];
309 } else {
310 help("create");
311 free(extra_config);
312 return 2;
313 }
314 }
315 if (filename) {
316 free(config_data); config_data = 0;
317 rc = libxl_read_file_contents(ctx, filename,
318 &config_data, &config_len);
319 if (rc) { fprintf(stderr, "Failed to read config file: %s: %s\n",
320 filename, strerror(errno));
321 free(extra_config); return ERROR_FAIL; }
322 if (extra_config && strlen(extra_config)) {
323 if (config_len > INT_MAX - (strlen(extra_config) + 2 + 1)) {
324 fprintf(stderr, "Failed to attach extra configuration\n");
325 exit(1);
326 }
327 /* allocate space for the extra config plus two EOLs plus \0 */
328 config_data = realloc(config_data, config_len
329 + strlen(extra_config) + 2 + 1);
330 if (!config_data) {
331 fprintf(stderr, "Failed to realloc config_data\n");
332 exit(1);
333 }
334 config_len += sprintf(config_data + config_len, "\n%s\n",
335 extra_config);
336 }
337 } else {
338 fprintf(stderr, "Config file not specified\n");
339 exit(1);
340 }
341
342 libxl_domain_config_init(&d_config);
343
344 parse_config_data(filename, config_data, config_len, &d_config);
345
346 if (debug || dryrun_only)
347 printf_info(default_output_format, -1, &d_config, stdout);
348
349 if (!dryrun_only) {
350 fprintf(stderr, "setting dom%u configuration\n", domid);
351 rc = libxl_userdata_store(ctx, domid, "xl",
352 config_data, config_len);
353 if (rc) {
354 fprintf(stderr, "failed to update configuration\n");
355 exit(1);
356 }
357 }
358
359 libxl_domain_config_dispose(&d_config);
360
361 free(config_data);
362 free(extra_config);
363 return 0;
364 }
365
366 /*
367 * Local variables:
368 * mode: C
369 * c-basic-offset: 4
370 * indent-tabs-mode: nil
371 * End:
372 */
373