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
|
/*
* Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
*/
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <xenctrl.h>
#include <xen/errno.h>
#include <xen-tools/common-macros.h>
static xc_interface *xch;
void show_help(void)
{
fprintf(stderr,
"xen-diag: xen diagnostic utility\n"
"Usage: xen-diag command [args]\n"
"Commands:\n"
" help display this help\n"
" gnttab_query_size <domid> dump the current and max grant frames for <domid>\n");
}
/* wrapper function */
static int help_func(int argc, char *argv[])
{
show_help();
return 0;
}
static int gnttab_query_size_func(int argc, char *argv[])
{
int domid, rc = 1;
struct gnttab_query_size query;
if ( argc != 1 )
{
show_help();
return rc;
}
domid = strtol(argv[0], NULL, 10);
query.dom = domid;
rc = xc_gnttab_query_size(xch, &query);
if ( rc == 0 && (query.status == GNTST_okay) )
printf("domid=%d: nr_frames=%d, max_nr_frames=%d\n",
query.dom, query.nr_frames, query.max_nr_frames);
return rc == 0 && (query.status == GNTST_okay) ? 0 : 1;
}
struct {
const char *name;
int (*function)(int argc, char *argv[]);
} main_options[] = {
{ "help", help_func },
{ "gnttab_query_size", gnttab_query_size_func},
};
int main(int argc, char *argv[])
{
int ret, i;
/*
* Set stdout to be unbuffered to avoid having to fflush when
* printing without a newline.
*/
setvbuf(stdout, NULL, _IONBF, 0);
if ( argc <= 1 )
{
show_help();
return 0;
}
for ( i = 0; i < ARRAY_SIZE(main_options); i++ )
if ( !strncmp(main_options[i].name, argv[1], strlen(argv[1])) )
break;
if ( i == ARRAY_SIZE(main_options) )
{
show_help();
return 0;
}
else
{
xch = xc_interface_open(0, 0, 0);
if ( !xch )
{
fprintf(stderr, "failed to get the handler\n");
return 0;
}
ret = main_options[i].function(argc - 2, argv + 2);
xc_interface_close(xch);
}
/*
* Exitcode 0 for success.
* Exitcode 1 for an error.
* Exitcode 2 if the operation should be retried for any reason (e.g. a
* timeout or because another operation was in progress).
*/
#define EXIT_TIMEOUT (EXIT_FAILURE + 1)
BUILD_BUG_ON(EXIT_SUCCESS != 0);
BUILD_BUG_ON(EXIT_FAILURE != 1);
BUILD_BUG_ON(EXIT_TIMEOUT != 2);
switch ( ret )
{
case 0:
return EXIT_SUCCESS;
case EAGAIN:
case EBUSY:
return EXIT_TIMEOUT;
default:
return EXIT_FAILURE;
}
}
|