debuggers.hg

view tools/libxl/libxl_internal.h @ 22855:1d1eec7e1fb4

xl: Perform minimal validation of virtual disk file while parsing config file

This patch performs some very basic validation on the virtual disk
file passed through the config file. This validation ensures that we
don't go too far with the initialization like spawn qemu and more
while there could be some potentially fundamental issues.

[ Patch fixed up to work with PHYSTYPE_EMPTY 22808:6ec61438713a -iwj ]

Signed-off-by: Kamala Narasimhan <kamala.narasimhan@citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
Committed-by: Ian Jackson <ian.jackson@eu.citrix.com>
author Kamala Narasimhan <kamala.narasimhan@gmail.com>
date Tue Jan 25 18:09:49 2011 +0000 (2011-01-25)
parents eefb8e971be5
children 5ce41defa1fa
line source
1 /*
2 * Copyright (C) 2009 Citrix Ltd.
3 * Author Vincent Hanquez <vincent.hanquez@eu.citrix.com>
4 * Author Stefano Stabellini <stefano.stabellini@eu.citrix.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation; version 2.1 only. with the special
9 * exception on linking described in file LICENSE.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 */
17 #ifndef LIBXL_INTERNAL_H
18 #define LIBXL_INTERNAL_H
20 #include <stdint.h>
21 #include <stdarg.h>
22 #include <stdlib.h>
24 #include <xs.h>
25 #include <xenctrl.h>
26 #include "xentoollog.h"
28 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
29 #define _hidden __attribute__((visibility("hidden")))
30 #define _protected __attribute__((visibility("protected")))
31 #else
32 #define _hidden
33 #define _protected
34 #endif
36 #include "flexarray.h"
37 #include "libxl_utils.h"
39 #define LIBXL_DESTROY_TIMEOUT 10
40 #define LIBXL_DEVICE_MODEL_START_TIMEOUT 10
41 #define LIBXL_XENCONSOLE_LIMIT 1048576
42 #define LIBXL_XENCONSOLE_PROTOCOL "vt100"
43 #define LIBXL_MAXMEM_CONSTANT 1024
44 #define LIBXL_PV_EXTRA_MEMORY 1024
45 #define LIBXL_HVM_EXTRA_MEMORY 2048
46 #define LIBXL_MIN_DOM0_MEM (128*1024)
47 #define QEMU_SIGNATURE "DeviceModelRecord0002"
48 #define STUBDOM_CONSOLE_LOGGING 0
49 #define STUBDOM_CONSOLE_SAVE 1
50 #define STUBDOM_CONSOLE_RESTORE 2
51 #define STUBDOM_CONSOLE_SERIAL 3
52 #define STUBDOM_SPECIAL_CONSOLES 3
53 #define SAVEFILE "/var/lib/xen/qemu-save"
55 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
57 #define LIBXL__LOGGING_ENABLED
59 #ifdef LIBXL__LOGGING_ENABLED
60 #define LIBXL__LOG(ctx, loglevel, _f, _a...) libxl__log(ctx, loglevel, -1, __FILE__, __LINE__, __func__, _f, ##_a)
61 #define LIBXL__LOG_ERRNO(ctx, loglevel, _f, _a...) libxl__log(ctx, loglevel, errno, __FILE__, __LINE__, __func__, _f, ##_a)
62 #define LIBXL__LOG_ERRNOVAL(ctx, loglevel, errnoval, _f, _a...) libxl__log(ctx, loglevel, errnoval, __FILE__, __LINE__, __func__, _f, ##_a)
63 #else
64 #define LIBXL__LOG(ctx, loglevel, _f, _a...)
65 #define LIBXL__LOG_ERRNO(ctx, loglevel, _f, _a...)
66 #define LIBXL__LOG_ERRNOVAL(ctx, loglevel, errnoval, _f, _a...)
67 #endif
68 /* all of these macros preserve errno (saving and restoring) */
70 /* logging */
71 _hidden void libxl__logv(libxl_ctx *ctx, xentoollog_level msglevel, int errnoval,
72 const char *file /* may be 0 */, int line /* ignored if !file */,
73 const char *func /* may be 0 */,
74 char *fmt, va_list al)
75 __attribute__((format(printf,7,0)));
77 _hidden void libxl__log(libxl_ctx *ctx, xentoollog_level msglevel, int errnoval,
78 const char *file /* may be 0 */, int line /* ignored if !file */,
79 const char *func /* may be 0 */,
80 char *fmt, ...)
81 __attribute__((format(printf,7,8)));
83 /* these functions preserve errno (saving and restoring) */
86 typedef enum {
87 DEVICE_VIF = 1,
88 DEVICE_VIF2,
89 DEVICE_VBD,
90 DEVICE_TAP,
91 DEVICE_QDISK,
92 DEVICE_PCI,
93 DEVICE_VFB,
94 DEVICE_VKBD,
95 DEVICE_CONSOLE,
96 } libxl__device_kinds;
98 #define is_valid_device_kind(kind) (((kind) >= DEVICE_VIF) && ((kind) <= DEVICE_CONSOLE))
100 typedef struct {
101 uint32_t backend_devid;
102 uint32_t backend_domid;
103 uint32_t devid;
104 uint32_t domid;
105 libxl__device_kinds backend_kind;
106 libxl__device_kinds kind;
107 } libxl__device;
109 #define XC_PCI_BDF "0x%x, 0x%x, 0x%x, 0x%x"
110 #define AUTO_PHP_SLOT 0x100
111 #define SYSFS_PCI_DEV "/sys/bus/pci/devices"
112 #define SYSFS_PCIBACK_DRIVER "/sys/bus/pci/drivers/pciback"
113 #define XENSTORE_PID_FILE "/var/run/xenstore.pid"
115 #define PROC_PCI_NUM_RESOURCES 7
116 #define PCI_BAR_IO 0x01
118 #define PRINTF_ATTRIBUTE(x, y) __attribute__((format(printf, x, y)))
120 typedef struct {
121 /* mini-GC */
122 int alloc_maxsize;
123 void **alloc_ptrs;
124 libxl_ctx *owner;
125 } libxl__gc;
127 #define LIBXL_INIT_GC(ctx) (libxl__gc){ .alloc_maxsize = 0, .alloc_ptrs = 0, .owner = ctx }
128 static inline libxl_ctx *libxl__gc_owner(libxl__gc *gc)
129 {
130 return gc->owner;
131 }
133 /* memory allocation tracking/helpers */
134 _hidden int libxl__ptr_add(libxl__gc *gc, void *ptr);
135 _hidden void libxl__free_all(libxl__gc *gc);
136 _hidden void *libxl__zalloc(libxl__gc *gc, int bytes);
137 _hidden void *libxl__calloc(libxl__gc *gc, size_t nmemb, size_t size);
138 _hidden char *libxl__sprintf(libxl__gc *gc, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
139 _hidden char *libxl__strdup(libxl__gc *gc, const char *c);
140 _hidden char *libxl__dirname(libxl__gc *gc, const char *s);
142 _hidden char **libxl__xs_kvs_of_flexarray(libxl__gc *gc, flexarray_t *array, int length);
143 _hidden int libxl__xs_writev(libxl__gc *gc, xs_transaction_t t,
144 char *dir, char **kvs);
145 _hidden int libxl__xs_write(libxl__gc *gc, xs_transaction_t t,
146 char *path, char *fmt, ...) PRINTF_ATTRIBUTE(4, 5);
147 _hidden char *libxl__xs_get_dompath(libxl__gc *gc, uint32_t domid); // logs errs
148 _hidden char *libxl__xs_read(libxl__gc *gc, xs_transaction_t t, char *path);
149 _hidden char **libxl__xs_directory(libxl__gc *gc, xs_transaction_t t, char *path, unsigned int *nb);
151 /* from xl_dom */
152 _hidden int libxl__domain_is_hvm(libxl_ctx *ctx, uint32_t domid);
153 _hidden int libxl__domain_shutdown_reason(libxl_ctx *ctx, uint32_t domid);
155 _hidden int libxl__build_pre(libxl_ctx *ctx, uint32_t domid,
156 libxl_domain_build_info *info, libxl_domain_build_state *state);
157 _hidden int libxl__build_post(libxl_ctx *ctx, uint32_t domid,
158 libxl_domain_build_info *info, libxl_domain_build_state *state,
159 char **vms_ents, char **local_ents);
161 _hidden int libxl__build_pv(libxl_ctx *ctx, uint32_t domid,
162 libxl_domain_build_info *info, libxl_domain_build_state *state);
163 _hidden int libxl__build_hvm(libxl_ctx *ctx, uint32_t domid,
164 libxl_domain_build_info *info, libxl_domain_build_state *state);
166 _hidden int libxl__domain_restore_common(libxl_ctx *ctx, uint32_t domid,
167 libxl_domain_build_info *info, libxl_domain_build_state *state, int fd);
168 _hidden int libxl__domain_suspend_common(libxl_ctx *ctx, uint32_t domid, int fd, int hvm, int live, int debug);
169 _hidden int libxl__domain_save_device_model(libxl_ctx *ctx, uint32_t domid, int fd);
170 _hidden void libxl__userdata_destroyall(libxl_ctx *ctx, uint32_t domid);
172 /* from xl_device */
173 _hidden char *libxl__device_disk_backend_type_of_phystype(libxl_disk_phystype phystype);
174 _hidden char *libxl__device_disk_string_of_phystype(libxl_disk_phystype phystype);
176 _hidden int libxl__device_physdisk_major_minor(const char *physpath, int *major, int *minor);
177 _hidden int libxl__device_disk_dev_number(char *virtpath);
179 _hidden int libxl__device_generic_add(libxl_ctx *ctx, libxl__device *device,
180 char **bents, char **fents);
181 _hidden int libxl__device_del(libxl_ctx *ctx, libxl__device *dev, int wait);
182 _hidden int libxl__device_destroy(libxl_ctx *ctx, char *be_path, int force);
183 _hidden int libxl__devices_destroy(libxl_ctx *ctx, uint32_t domid, int force);
184 _hidden int libxl__wait_for_device_model(libxl_ctx *ctx,
185 uint32_t domid, char *state,
186 int (*check_callback)(libxl_ctx *ctx,
187 uint32_t domid,
188 const char *state,
189 void *userdata),
190 void *check_callback_userdata);
191 _hidden int libxl__wait_for_backend(libxl_ctx *ctx, char *be_path, char *state);
193 /* xl_exec */
195 /* higher-level double-fork and separate detach eg as for device models */
197 typedef struct {
198 /* put this in your own status structure as returned to application */
199 /* all fields are private to libxl_spawn_... */
200 pid_t intermediate;
201 char *what; /* malloc'd in spawn_spawn */
202 } libxl__spawn_starting;
204 typedef struct {
205 libxl__spawn_starting *for_spawn; /* first! */
206 char *dom_path; /* from libxl_malloc, only for dm_xenstore_record_pid */
207 int domid;
208 } libxl__device_model_starting;
210 /* from xl_create */
211 _hidden int libxl__domain_make(libxl_ctx *ctx, libxl_domain_create_info *info, uint32_t *domid);
212 _hidden int libxl__domain_build(libxl_ctx *ctx, libxl_domain_build_info *info, uint32_t domid, /* out */ libxl_domain_build_state *state);
214 /* for device model creation */
215 _hidden int libxl__create_device_model(libxl_ctx *ctx,
216 libxl_device_model_info *info,
217 libxl_device_disk *disk, int num_disks,
218 libxl_device_nic *vifs, int num_vifs,
219 libxl__device_model_starting **starting_r);
220 _hidden int libxl__create_xenpv_qemu(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb,
221 libxl__device_model_starting **starting_r);
222 _hidden int libxl__need_xenpv_qemu(libxl_ctx *ctx,
223 int nr_consoles, libxl_device_console *consoles,
224 int nr_vfbs, libxl_device_vfb *vfbs,
225 int nr_disks, libxl_device_disk *disks);
227 /* Caller must either: pass starting_r==0, or on successful
228 * return pass *starting_r (which will be non-0) to
229 * libxl_confirm_device_model or libxl_detach_device_model. */
230 _hidden int libxl__confirm_device_model_startup(libxl_ctx *ctx,
231 libxl__device_model_starting *starting);
233 _hidden int libxl__spawn_spawn(libxl_ctx *ctx,
234 libxl__device_model_starting *starting,
235 const char *what,
236 void (*intermediate_hook)(void *for_spawn, pid_t innerchild));
237 _hidden int libxl__destroy_device_model(libxl_ctx *ctx, uint32_t domid);
239 /* Logs errors. A copy of "what" is taken. Return values:
240 * < 0 error, for_spawn need not be detached
241 * +1 caller is the parent, must call detach on *for_spawn eventually
242 * 0 caller is now the inner child, should probably call libxl__exec
243 * Caller, may pass 0 for for_spawn, in which case no need to detach.
244 */
245 _hidden int libxl__spawn_detach(libxl_ctx *ctx,
246 libxl__spawn_starting *for_spawn);
247 /* Logs errors. Idempotent, but only permitted after successful
248 * call to libxl__spawn_spawn, and no point calling it again if it fails. */
249 _hidden int libxl__spawn_check(libxl_ctx *ctx,
250 void *for_spawn);
251 /* Logs errors but also returns them.
252 * for_spawn must actually be a libxl__spawn_starting* but
253 * we take void* so you can pass this function directly to
254 * libxl__wait_for_device_model. Caller must still call detach. */
256 /* low-level stuff, for synchronous subprocesses etc. */
258 _hidden void libxl__exec(int stdinfd, int stdoutfd, int stderrfd,
259 const char *arg0, char **args); // logs errors, never returns
260 _hidden void libxl__log_child_exitstatus(libxl__gc *gc,
261 const char *what, pid_t pid, int status);
263 _hidden char *libxl__abs_path(libxl__gc *gc, char *s, const char *path);
265 #define LIBXL__LOG_DEBUG XTL_DEBUG
266 #define LIBXL__LOG_INFO XTL_INFO
267 #define LIBXL__LOG_WARNING XTL_WARN
268 #define LIBXL__LOG_ERROR XTL_ERROR
270 _hidden char *libxl__domid_to_name(libxl__gc *gc, uint32_t domid);
271 _hidden char *libxl__cpupoolid_to_name(libxl__gc *gc, uint32_t poolid);
274 /* holds the CPUID response for a single CPUID leaf
275 * input contains the value of the EAX and ECX register,
276 * and each policy string contains a filter to apply to
277 * the host given values for that particular leaf.
278 */
279 struct libxl__cpuid_policy {
280 uint32_t input[2];
281 char *policy[4];
282 };
284 /*
285 * blktap2 support
286 */
288 /* libxl__blktap_enabled:
289 * return true if blktap/blktap2 support is available.
290 */
291 _hidden int libxl__blktap_enabled(libxl__gc *gc);
293 /* libxl__blktap_devpath:
294 * Argument: path and disk image as specified in config file.
295 * The type specifies whether this is aio, qcow, qcow2, etc.
296 * returns device path xenstore wants to have. returns NULL
297 * if no device corresponds to the disk.
298 */
299 _hidden const char *libxl__blktap_devpath(libxl__gc *gc,
300 const char *disk,
301 libxl_disk_phystype phystype);
303 _hidden char *libxl__uuid2string(libxl__gc *gc, const libxl_uuid uuid);
305 struct libxl__xen_console_reader {
306 char *buffer;
307 unsigned int size;
308 unsigned int count;
309 unsigned int clear;
310 unsigned int incremental;
311 unsigned int index;
312 };
314 _hidden int libxl__error_set(libxl_ctx *ctx, int code);
316 _hidden int libxl__file_reference_map(libxl_file_reference *f);
317 _hidden int libxl__file_reference_unmap(libxl_file_reference *f);
319 #endif