1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 
5 #include "xenstore.h"
6 
7 
main(int argc,char ** argv)8 int main(int argc, char **argv)
9 {
10     struct xs_handle *xsh;
11     char *par = NULL;
12     char *ret;
13     unsigned int p, len = 0;
14     int rc = 0;
15 
16     if (argc < 2) {
17         fprintf(stderr, "Usage:\n"
18                 "%s <command> [<arg>...]\n", argv[0]);
19         rc = 2;
20         goto out;
21     }
22 
23     for (p = 2; p < argc; p++)
24         len += strlen(argv[p]) + 1;
25     if (len) {
26         par = malloc(len);
27         if (!par) {
28             fprintf(stderr, "Allocation error.\n");
29             rc = 1;
30             goto out;
31         }
32         len = 0;
33         for (p = 2; p < argc; p++) {
34             memcpy(par + len, argv[p], strlen(argv[p]) + 1);
35             len += strlen(argv[p]) + 1;
36         }
37     }
38 
39     xsh = xs_open(0);
40     if (xsh == NULL) {
41         fprintf(stderr, "Failed to contact Xenstored.\n");
42         rc = 1;
43         goto out;
44     }
45 
46     ret = xs_control_command(xsh, argv[1], par, len);
47     if (!ret) {
48         rc = 3;
49         if (errno == EINVAL) {
50             ret = xs_control_command(xsh, "help", NULL, 0);
51             if (ret)
52                 fprintf(stderr, "Command not supported. Valid commands are:\n"
53                                 "%s\n", ret);
54             else
55                 fprintf(stderr, "Error when executing command.\n");
56         } else
57             fprintf(stderr, "Error %d when trying to execute command.\n",
58                     errno);
59     } else if (strlen(ret) > 0)
60         printf("%s\n", ret);
61 
62     xs_close(xsh);
63 
64  out:
65     free(par);
66     return rc;
67 }
68