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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
#include "qemu/osdep.h"
#include "block/qdict.h" /* for qdict_extract_subqdict() */
#include "qapi/error.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qmp/qlist.h"
#include "qemu/error-report.h"
#include "qemu/option.h"
#include "qemu/config-file.h"
QemuOptsList *vm_config_groups[48];
QemuOptsList *drive_config_groups[5];
static QemuOptsList *find_list(QemuOptsList **lists, const char *group,
Error **errp)
{
int i;
qemu_load_module_for_opts(group);
for (i = 0; lists[i] != NULL; i++) {
if (strcmp(lists[i]->name, group) == 0)
break;
}
if (lists[i] == NULL) {
error_setg(errp, "There is no option group '%s'", group);
}
return lists[i];
}
QemuOptsList *qemu_find_opts(const char *group)
{
QemuOptsList *ret;
Error *local_err = NULL;
ret = find_list(vm_config_groups, group, &local_err);
if (local_err) {
error_report_err(local_err);
}
return ret;
}
QemuOpts *qemu_find_opts_singleton(const char *group)
{
QemuOptsList *list;
QemuOpts *opts;
list = qemu_find_opts(group);
assert(list);
opts = qemu_opts_find(list, NULL);
if (!opts) {
opts = qemu_opts_create(list, NULL, 0, &error_abort);
}
return opts;
}
QemuOptsList *qemu_find_opts_err(const char *group, Error **errp)
{
return find_list(vm_config_groups, group, errp);
}
void qemu_add_drive_opts(QemuOptsList *list)
{
int entries, i;
entries = ARRAY_SIZE(drive_config_groups);
entries--; /* keep list NULL terminated */
for (i = 0; i < entries; i++) {
if (drive_config_groups[i] == NULL) {
drive_config_groups[i] = list;
return;
}
}
fprintf(stderr, "ran out of space in drive_config_groups");
abort();
}
void qemu_add_opts(QemuOptsList *list)
{
int entries, i;
entries = ARRAY_SIZE(vm_config_groups);
entries--; /* keep list NULL terminated */
for (i = 0; i < entries; i++) {
if (vm_config_groups[i] == NULL) {
vm_config_groups[i] = list;
return;
}
}
fprintf(stderr, "ran out of space in vm_config_groups");
abort();
}
/* Returns number of config groups on success, -errno on error */
static int qemu_config_foreach(FILE *fp, QEMUConfigCB *cb, void *opaque,
const char *fname, Error **errp)
{
ERRP_GUARD();
char line[1024], prev_group[64], group[64], arg[64], value[1024];
Location loc;
QDict *qdict = NULL;
int res = -EINVAL, lno = 0;
int count = 0;
loc_push_none(&loc);
while (fgets(line, sizeof(line), fp) != NULL) {
++lno;
if (line[0] == '\n') {
/* skip empty lines */
continue;
}
if (line[0] == '#') {
/* comment */
continue;
}
if (line[0] == '[') {
QDict *prev = qdict;
if (sscanf(line, "[%63s \"%63[^\"]\"]", group, value) == 2) {
qdict = qdict_new();
qdict_put_str(qdict, "id", value);
count++;
} else if (sscanf(line, "[%63[^]]]", group) == 1) {
qdict = qdict_new();
count++;
}
if (qdict != prev) {
if (prev) {
cb(prev_group, prev, opaque, errp);
qobject_unref(prev);
if (*errp) {
goto out;
}
}
strcpy(prev_group, group);
continue;
}
}
loc_set_file(fname, lno);
value[0] = '\0';
if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2 ||
sscanf(line, " %63s = \"\"", arg) == 1) {
/* arg = value */
if (qdict == NULL) {
error_setg(errp, "no group defined");
goto out;
}
qdict_put_str(qdict, arg, value);
continue;
}
error_setg(errp, "parse error");
goto out;
}
if (ferror(fp)) {
loc_pop(&loc);
error_setg_errno(errp, errno, "Cannot read config file");
goto out_no_loc;
}
res = count;
if (qdict) {
cb(group, qdict, opaque, errp);
}
out:
loc_pop(&loc);
out_no_loc:
qobject_unref(qdict);
return res;
}
void qemu_config_do_parse(const char *group, QDict *qdict, void *opaque, Error **errp)
{
QemuOptsList **lists = opaque;
QemuOptsList *list;
list = find_list(lists, group, errp);
if (!list) {
return;
}
qemu_opts_from_qdict(list, qdict, errp);
}
int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname, Error **errp)
{
return qemu_config_foreach(fp, qemu_config_do_parse, lists, fname, errp);
}
int qemu_read_config_file(const char *filename, QEMUConfigCB *cb, Error **errp)
{
FILE *f = fopen(filename, "r");
int ret;
if (f == NULL) {
error_setg_file_open(errp, errno, filename);
return -errno;
}
ret = qemu_config_foreach(f, cb, vm_config_groups, filename, errp);
fclose(f);
return ret;
}
static bool config_parse_qdict_section(QDict *options, QemuOptsList *opts,
Error **errp)
{
QemuOpts *subopts;
g_autoptr(QDict) subqdict = NULL;
g_autoptr(QList) list = NULL;
size_t orig_size, enum_size;
char *prefix;
prefix = g_strdup_printf("%s.", opts->name);
qdict_extract_subqdict(options, &subqdict, prefix);
g_free(prefix);
orig_size = qdict_size(subqdict);
if (!orig_size) {
return true;
}
subopts = qemu_opts_create(opts, NULL, 0, errp);
if (!subopts) {
return false;
}
if (!qemu_opts_absorb_qdict(subopts, subqdict, errp)) {
return false;
}
enum_size = qdict_size(subqdict);
if (enum_size < orig_size && enum_size) {
error_setg(errp, "Unknown option '%s' for [%s]",
qdict_first(subqdict)->key, opts->name);
return false;
}
if (enum_size) {
/* Multiple, enumerated sections */
QListEntry *list_entry;
unsigned i = 0;
/* Not required anymore */
qemu_opts_del(subopts);
qdict_array_split(subqdict, &list);
if (qdict_size(subqdict)) {
error_setg(errp, "Unused option '%s' for [%s]",
qdict_first(subqdict)->key, opts->name);
return false;
}
QLIST_FOREACH_ENTRY(list, list_entry) {
QDict *section = qobject_to(QDict, qlist_entry_obj(list_entry));
char *opt_name;
if (!section) {
error_setg(errp, "[%s] section (index %u) does not consist of "
"keys", opts->name, i);
return false;
}
opt_name = g_strdup_printf("%s.%u", opts->name, i++);
subopts = qemu_opts_create(opts, opt_name, 1, errp);
g_free(opt_name);
if (!subopts) {
return false;
}
if (!qemu_opts_absorb_qdict(subopts, section, errp)) {
qemu_opts_del(subopts);
return false;
}
if (qdict_size(section)) {
error_setg(errp, "[%s] section doesn't support the option '%s'",
opts->name, qdict_first(section)->key);
qemu_opts_del(subopts);
return false;
}
}
}
return true;
}
bool qemu_config_parse_qdict(QDict *options, QemuOptsList **lists,
Error **errp)
{
int i;
for (i = 0; lists[i]; i++) {
if (!config_parse_qdict_section(options, lists[i], errp)) {
return false;
}
}
return true;
}
|