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);
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);
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         fprintf(stderr, "Use \"xl dmesg\" to look for possible reason.\n");
172         return EXIT_FAILURE;
173     }
174 
175     return EXIT_SUCCESS;
176 }
177 
main_devd(int argc,char ** argv)178 int main_devd(int argc, char **argv)
179 {
180     int ret = 0, opt = 0, daemonize = 1;
181     const char *pidfile = NULL;
182     static const struct option opts[] = {
183         {"pidfile", 1, 0, 'p'},
184         COMMON_LONG_OPTS,
185         {0, 0, 0, 0}
186     };
187 
188     SWITCH_FOREACH_OPT(opt, "Fp:", opts, "devd", 0) {
189     case 'F':
190         daemonize = 0;
191         break;
192     case 'p':
193         pidfile = optarg;
194         break;
195     }
196 
197     if (daemonize) {
198         ret = do_daemonize("xldevd", pidfile);
199         if (ret) {
200             ret = (ret == 1) ? 0 : ret;
201             goto out;
202         }
203     }
204 
205     ret = libxl_device_events_handler(ctx, 0) ? EXIT_FAILURE : EXIT_SUCCESS;
206 
207 out:
208     return ret;
209 }
210 
main_qemu_monitor_command(int argc,char ** argv)211 int main_qemu_monitor_command(int argc, char **argv)
212 {
213     int opt;
214     uint32_t domid;
215     char *cmd;
216     char *output;
217     int ret;
218 
219     SWITCH_FOREACH_OPT(opt, "", NULL, "qemu-monitor-command", 2) {
220         /* No options */
221     }
222 
223     domid = find_domain(argv[optind]);
224     cmd = argv[optind + 1];
225 
226     if (argc - optind > 2) {
227         fprintf(stderr, "Invalid arguments.\n");
228         return EXIT_FAILURE;
229     }
230 
231     ret = libxl_qemu_monitor_command(ctx, domid, cmd, &output);
232     if (!ret && output) {
233         printf("%s\n", output);
234         free(output);
235     }
236 
237     return ret ? EXIT_FAILURE : EXIT_SUCCESS;
238 }
239 
core_dump_domain(uint32_t domid,const char * filename)240 static void core_dump_domain(uint32_t domid, const char *filename)
241 {
242     int rc;
243 
244     rc=libxl_domain_core_dump(ctx, domid, filename, NULL);
245     if (rc) { fprintf(stderr,"core dump failed (rc=%d)\n",rc);exit(EXIT_FAILURE); }
246 }
247 
main_dump_core(int argc,char ** argv)248 int main_dump_core(int argc, char **argv)
249 {
250     int opt;
251 
252     SWITCH_FOREACH_OPT(opt, "", NULL, "dump-core", 2) {
253         /* No options */
254     }
255 
256     core_dump_domain(find_domain(argv[optind]), argv[optind + 1]);
257     return EXIT_SUCCESS;
258 }
259 
260 extern void printf_info(enum output_format output_format,
261                         int domid,
262                         libxl_domain_config *d_config, FILE *fh);
main_config_update(int argc,char ** argv)263 int main_config_update(int argc, char **argv)
264 {
265     uint32_t domid;
266     const char *filename = NULL;
267     char *extra_config = NULL;
268     void *config_data = 0;
269     int config_len = 0;
270     libxl_domain_config d_config;
271     int opt, rc;
272     int debug = 0;
273     static struct option opts[] = {
274         {"defconfig", 1, 0, 'f'},
275         COMMON_LONG_OPTS
276     };
277 
278     if (argc < 2) {
279         fprintf(stderr, "xl config-update requires a domain argument\n");
280         help("config-update");
281         exit(1);
282     }
283 
284     fprintf(stderr, "WARNING: xl now has better capability to manage domain configuration, "
285             "avoid using this command when possible\n");
286 
287     domid = find_domain(argv[1]);
288     argc--; argv++;
289 
290     if (argv[1] && argv[1][0] != '-' && !strchr(argv[1], '=')) {
291         filename = argv[1];
292         argc--; argv++;
293     }
294 
295     SWITCH_FOREACH_OPT(opt, "dqf:", opts, "config_update", 0) {
296     case 'd':
297         debug = 1;
298         break;
299     case 'f':
300         filename = optarg;
301         break;
302     }
303 
304     for (; optind < argc; optind++) {
305         if (strchr(argv[optind], '=') != NULL) {
306             string_realloc_append(&extra_config, argv[optind]);
307             string_realloc_append(&extra_config, "\n");
308         } else if (!filename) {
309             filename = argv[optind];
310         } else {
311             help("create");
312             free(extra_config);
313             return 2;
314         }
315     }
316     if (filename) {
317         free(config_data);  config_data = 0;
318         rc = libxl_read_file_contents(ctx, filename,
319                                       &config_data, &config_len);
320         if (rc) { fprintf(stderr, "Failed to read config file: %s: %s\n",
321                            filename, strerror(errno));
322                   free(extra_config); return ERROR_FAIL; }
323         if (extra_config && strlen(extra_config)) {
324             if (config_len > INT_MAX - (strlen(extra_config) + 2 + 1)) {
325                 fprintf(stderr, "Failed to attach extra configuration\n");
326                 exit(1);
327             }
328             /* allocate space for the extra config plus two EOLs plus \0 */
329             config_data = realloc(config_data, config_len
330                 + strlen(extra_config) + 2 + 1);
331             if (!config_data) {
332                 fprintf(stderr, "Failed to realloc config_data\n");
333                 exit(1);
334             }
335             config_len += sprintf(config_data + config_len, "\n%s\n",
336                 extra_config);
337         }
338     } else {
339         fprintf(stderr, "Config file not specified\n");
340         exit(1);
341     }
342 
343     libxl_domain_config_init(&d_config);
344 
345     parse_config_data(filename, config_data, config_len, &d_config);
346 
347     if (debug || dryrun_only)
348         printf_info(default_output_format, -1, &d_config, stdout);
349 
350     if (!dryrun_only) {
351         fprintf(stderr, "setting dom%u configuration\n", domid);
352         rc = libxl_userdata_store(ctx, domid, "xl",
353                                    config_data, config_len);
354         if (rc) {
355             fprintf(stderr, "failed to update configuration\n");
356             exit(1);
357         }
358     }
359 
360     libxl_domain_config_dispose(&d_config);
361 
362     free(config_data);
363     free(extra_config);
364     return 0;
365 }
366 
367 /*
368  * Local variables:
369  * mode: C
370  * c-basic-offset: 4
371  * indent-tabs-mode: nil
372  * End:
373  */
374