+#ifdef HAVE_GETTEXT
+#include "gettext.h"
+#define _(str) dgettext(PACKAGE, (str))
+#define N_(str) dgettext(PACKAGE, (str))
+#else
+#define _(str) str
+#define N_(str) str
+#endif
+
+#define TMP_TEMPLATE_ON_STACK(var) \
+ const char *ttos_tmpdir = guestfs_tmpdir (); \
+ char var[strlen (ttos_tmpdir) + 32]; \
+ sprintf (var, "%s/libguestfsXXXXXX", ttos_tmpdir) \
+
+#define UNIX_PATH_MAX 108
+
+#ifndef MAX
+#define MAX(a,b) ((a)>(b)?(a):(b))
+#endif
+
+#ifdef __APPLE__
+#define xdr_uint32_t xdr_u_int32_t
+#endif
+
+/* Some limits on what the inspection code will read, for safety. */
+
+/* Small text configuration files.
+ *
+ * The upper limit is for general files that we grep or download. The
+ * largest such file is probably "txtsetup.sif" from Windows CDs
+ * (~500K). This number has to be larger than any legitimate file and
+ * smaller than the protocol message size.
+ *
+ * The lower limit is for files parsed by Augeas on the daemon side,
+ * where Augeas is running in reduced memory and can potentially
+ * create a lot of metadata so we really need to be careful about
+ * those.
+ */
+#define MAX_SMALL_FILE_SIZE (2 * 1000 * 1000)
+#define MAX_AUGEAS_FILE_SIZE (100 * 1000)
+
+/* Maximum Windows Registry hive that we will download to /tmp. Some
+ * registries can be legitimately very large.
+ */
+#define MAX_REGISTRY_SIZE (100 * 1000 * 1000)
+
+/* Maximum RPM or dpkg database we will download to /tmp. */
+#define MAX_PKG_DB_SIZE (10 * 1000 * 1000)
+
+/* Network configuration of the appliance. Note these addresses are
+ * only meaningful within the context of the running appliance. QEMU
+ * translates network connections to these magic addresses into
+ * userspace calls on the host (eg. connect(2)). qemu-doc has a nice
+ * diagram which is also useful to refer to.
+ *
+ * NETWORK: The network.
+ *
+ * ROUTER: The address of the "host", ie. this library.
+ *
+ * [Note: If you change NETWORK and ROUTER then you also have to
+ * change the network configuration in appliance/init].
+ *
+ * GUESTFWD_ADDR, GUESTFWD_PORT: The guestfwd feature of qemu
+ * magically connects this pseudo-address to the guestfwd channel. In
+ * typical Linux configurations of libguestfs, guestfwd is not
+ * actually used any more.
+ */
+#define NETWORK "169.254.0.0/16"
+#define ROUTER "169.254.2.2"
+
+/* GuestFS handle and connection. */
+enum state { CONFIG, LAUNCHING, READY, BUSY, NO_HANDLE };
+
+/* Attach method. */
+enum attach_method { ATTACH_METHOD_APPLIANCE = 0, ATTACH_METHOD_UNIX };
+
+/* Event. */
+struct event {
+ uint64_t event_bitmask;
+ guestfs_event_callback cb;
+ void *opaque;
+
+ /* opaque2 is not exposed through the API, but is used internally to
+ * emulate the old-style callback API.
+ */
+ void *opaque2;
+};
+
+struct guestfs_h
+{
+ struct guestfs_h *next; /* Linked list of open handles. */
+
+ /* State: see the state machine diagram in the man page guestfs(3). */
+ enum state state;
+
+ int fd[2]; /* Stdin/stdout of qemu. */
+ int sock; /* Daemon communications socket. */
+ pid_t pid; /* Qemu PID. */
+ pid_t recoverypid; /* Recovery process PID. */
+
+ struct timeval launch_t; /* The time that we called guestfs_launch. */
+
+ char *tmpdir; /* Temporary directory containing socket. */
+
+ char *qemu_help, *qemu_version; /* Output of qemu -help, qemu -version. */
+
+ char **cmdline; /* Qemu command line. */
+ size_t cmdline_size;
+
+ int verbose;
+ int trace;
+ int autosync;
+ int direct;
+ int recovery_proc;
+ int enable_network;
+
+ char *path; /* Path to kernel, initrd. */
+ char *qemu; /* Qemu binary. */
+ char *append; /* Append to kernel command line. */
+
+ enum attach_method attach_method;
+ char *attach_method_arg;
+
+ int memsize; /* Size of RAM (megabytes). */
+
+ int selinux; /* selinux enabled? */
+
+ char *last_error;
+ int last_errnum; /* errno, or 0 if there was no errno */
+
+ /* Callbacks. */
+ guestfs_abort_cb abort_cb;
+ guestfs_error_handler_cb error_cb;
+ void * error_cb_data;
+
+ /* Events. */
+ struct event *events;
+ size_t nr_events;
+
+ int msg_next_serial;
+
+ /* Information gathered by inspect_os. Must be freed by calling
+ * guestfs___free_inspect_info.
+ */
+ struct inspect_fs *fses;
+ size_t nr_fses;
+
+ /* Private data area. */
+ struct hash_table *pda;
+ struct pda_entry *pda_next;
+
+ /* Used by src/actions.c:trace_* functions. */
+ FILE *trace_fp;
+ char *trace_buf;
+ size_t trace_len;
+};
+
+/* Per-filesystem data stored for inspect_os. */
+enum inspect_fs_content {
+ FS_CONTENT_UNKNOWN = 0,
+ FS_CONTENT_LINUX_ROOT,
+ FS_CONTENT_WINDOWS_ROOT,
+ FS_CONTENT_WINDOWS_VOLUME_WITH_APPS,
+ FS_CONTENT_WINDOWS_VOLUME,
+ FS_CONTENT_LINUX_BOOT,
+ FS_CONTENT_LINUX_USR,
+ FS_CONTENT_LINUX_USR_LOCAL,
+ FS_CONTENT_LINUX_VAR,
+ FS_CONTENT_FREEBSD_ROOT,
+ FS_CONTENT_INSTALLER,
+};
+
+enum inspect_os_format {
+ OS_FORMAT_UNKNOWN = 0,
+ OS_FORMAT_INSTALLED,
+ OS_FORMAT_INSTALLER,
+ /* in future: supplemental disks */
+};
+
+enum inspect_os_type {
+ OS_TYPE_UNKNOWN = 0,
+ OS_TYPE_LINUX,
+ OS_TYPE_WINDOWS,
+ OS_TYPE_FREEBSD,
+};
+
+enum inspect_os_distro {
+ OS_DISTRO_UNKNOWN = 0,
+ OS_DISTRO_DEBIAN,
+ OS_DISTRO_FEDORA,
+ OS_DISTRO_REDHAT_BASED,
+ OS_DISTRO_RHEL,
+ OS_DISTRO_WINDOWS,
+ OS_DISTRO_PARDUS,
+ OS_DISTRO_ARCHLINUX,
+ OS_DISTRO_GENTOO,
+ OS_DISTRO_UBUNTU,
+ OS_DISTRO_MEEGO,
+ OS_DISTRO_LINUX_MINT,
+ OS_DISTRO_MANDRIVA,
+ OS_DISTRO_SLACKWARE,
+ OS_DISTRO_CENTOS,
+ OS_DISTRO_SCIENTIFIC_LINUX,
+};
+
+enum inspect_os_package_format {
+ OS_PACKAGE_FORMAT_UNKNOWN = 0,
+ OS_PACKAGE_FORMAT_RPM,
+ OS_PACKAGE_FORMAT_DEB,
+ OS_PACKAGE_FORMAT_PACMAN,
+ OS_PACKAGE_FORMAT_EBUILD,
+ OS_PACKAGE_FORMAT_PISI
+};
+
+enum inspect_os_package_management {
+ OS_PACKAGE_MANAGEMENT_UNKNOWN = 0,
+ OS_PACKAGE_MANAGEMENT_YUM,
+ OS_PACKAGE_MANAGEMENT_UP2DATE,
+ OS_PACKAGE_MANAGEMENT_APT,
+ OS_PACKAGE_MANAGEMENT_PACMAN,
+ OS_PACKAGE_MANAGEMENT_PORTAGE,
+ OS_PACKAGE_MANAGEMENT_PISI,
+ OS_PACKAGE_MANAGEMENT_URPMI,
+};
+
+struct inspect_fs {
+ int is_root;
+ char *device;
+ int is_mountable;
+ int is_swap;
+ enum inspect_fs_content content;
+ enum inspect_os_type type;
+ enum inspect_os_distro distro;
+ enum inspect_os_package_format package_format;
+ enum inspect_os_package_management package_management;
+ char *product_name;
+ char *product_variant;
+ int major_version;
+ int minor_version;
+ char *arch;
+ char *hostname;
+ char *windows_systemroot;
+ char *windows_current_control_set;
+ char **drive_mappings;
+ enum inspect_os_format format;
+ int is_live_disk;
+ int is_netinst_disk;
+ int is_multipart_disk;
+ struct inspect_fstab_entry *fstab;
+ size_t nr_fstab;
+};
+
+struct inspect_fstab_entry {
+ char *device;
+ char *mountpoint;
+};
+