c6d7c87116dae027c1ab57bc71d542d99ce84976
[libguestfs.git] / src / guestfs-internal.h
1 /* libguestfs
2  * Copyright (C) 2009-2011 Red Hat Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #ifndef GUESTFS_INTERNAL_H_
20 #define GUESTFS_INTERNAL_H_
21
22 #include <rpc/types.h>
23 #include <rpc/xdr.h>
24
25 #ifdef HAVE_PCRE
26 #include <pcre.h>
27 #endif
28
29 #define STREQ(a,b) (strcmp((a),(b)) == 0)
30 #define STRCASEEQ(a,b) (strcasecmp((a),(b)) == 0)
31 #define STRNEQ(a,b) (strcmp((a),(b)) != 0)
32 #define STRCASENEQ(a,b) (strcasecmp((a),(b)) != 0)
33 #define STREQLEN(a,b,n) (strncmp((a),(b),(n)) == 0)
34 #define STRCASEEQLEN(a,b,n) (strncasecmp((a),(b),(n)) == 0)
35 #define STRNEQLEN(a,b,n) (strncmp((a),(b),(n)) != 0)
36 #define STRCASENEQLEN(a,b,n) (strncasecmp((a),(b),(n)) != 0)
37 #define STRPREFIX(a,b) (strncmp((a),(b),strlen((b))) == 0)
38
39 #ifdef HAVE_GETTEXT
40 #include "gettext.h"
41 #define _(str) dgettext(PACKAGE, (str))
42 #define N_(str) dgettext(PACKAGE, (str))
43 #else
44 #define _(str) str
45 #define N_(str) str
46 #endif
47
48 #define TMP_TEMPLATE_ON_STACK(var)                        \
49   const char *ttos_tmpdir = guestfs_tmpdir ();            \
50   char var[strlen (ttos_tmpdir) + 32];                    \
51   sprintf (var, "%s/libguestfsXXXXXX", ttos_tmpdir)       \
52
53 #define UNIX_PATH_MAX 108
54
55 #ifndef MAX
56 #define MAX(a,b) ((a)>(b)?(a):(b))
57 #endif
58
59 #ifdef __APPLE__
60 #define xdr_uint32_t xdr_u_int32_t
61 #endif
62
63 /* Some limits on what the inspection code will read, for safety. */
64
65 /* Small text configuration files.
66  *
67  * The upper limit is for general files that we grep or download.  The
68  * largest such file is probably "txtsetup.sif" from Windows CDs
69  * (~500K).  This number has to be larger than any legitimate file and
70  * smaller than the protocol message size.
71  *
72  * The lower limit is for files parsed by Augeas on the daemon side,
73  * where Augeas is running in reduced memory and can potentially
74  * create a lot of metadata so we really need to be careful about
75  * those.
76  */
77 #define MAX_SMALL_FILE_SIZE    (2 * 1000 * 1000)
78 #define MAX_AUGEAS_FILE_SIZE        (100 * 1000)
79
80 /* Maximum Windows Registry hive that we will download to /tmp.  Some
81  * registries can be legitimately very large.
82  */
83 #define MAX_REGISTRY_SIZE    (100 * 1000 * 1000)
84
85 /* Maximum RPM or dpkg database we will download to /tmp.  RPM
86  * 'Packages' database can get very large: 70 MB is roughly the
87  * standard size for a new Fedora install, and after lots of package
88  * installation/removal I have seen well over 100 MB databases.
89  */
90 #define MAX_PKG_DB_SIZE       (300 * 1000 * 1000)
91
92 /* Network configuration of the appliance.  Note these addresses are
93  * only meaningful within the context of the running appliance.  QEMU
94  * translates network connections to these magic addresses into
95  * userspace calls on the host (eg. connect(2)).  qemu-doc has a nice
96  * diagram which is also useful to refer to.
97  *
98  * NETWORK: The network.
99  *
100  * ROUTER: The address of the "host", ie. this library.
101  *
102  * [Note: If you change NETWORK and ROUTER then you also have to
103  * change the network configuration in appliance/init].
104  *
105  * GUESTFWD_ADDR, GUESTFWD_PORT: The guestfwd feature of qemu
106  * magically connects this pseudo-address to the guestfwd channel.  In
107  * typical Linux configurations of libguestfs, guestfwd is not
108  * actually used any more.
109  */
110 #define NETWORK "169.254.0.0/16"
111 #define ROUTER "169.254.2.2"
112
113 /* GuestFS handle and connection. */
114 enum state { CONFIG, LAUNCHING, READY, BUSY, NO_HANDLE };
115
116 /* Attach method. */
117 enum attach_method { ATTACH_METHOD_APPLIANCE = 0, ATTACH_METHOD_UNIX };
118
119 /* Event. */
120 struct event {
121   uint64_t event_bitmask;
122   guestfs_event_callback cb;
123   void *opaque;
124
125   /* opaque2 is not exposed through the API, but is used internally to
126    * emulate the old-style callback API.
127    */
128   void *opaque2;
129 };
130
131 struct guestfs_h
132 {
133   struct guestfs_h *next;       /* Linked list of open handles. */
134
135   /* State: see the state machine diagram in the man page guestfs(3). */
136   enum state state;
137
138   int fd[2];                    /* Stdin/stdout of qemu. */
139   int sock;                     /* Daemon communications socket. */
140   pid_t pid;                    /* Qemu PID. */
141   pid_t recoverypid;            /* Recovery process PID. */
142
143   struct timeval launch_t;      /* The time that we called guestfs_launch. */
144
145   char *tmpdir;                 /* Temporary directory containing socket. */
146
147   char *qemu_help, *qemu_version; /* Output of qemu -help, qemu -version. */
148
149   char **cmdline;               /* Qemu command line. */
150   int cmdline_size;
151
152   int verbose;
153   int trace;
154   int autosync;
155   int direct;
156   int recovery_proc;
157   int enable_network;
158
159   char *path;                   /* Path to kernel, initrd. */
160   char *qemu;                   /* Qemu binary. */
161   char *append;                 /* Append to kernel command line. */
162
163   enum attach_method attach_method;
164   char *attach_method_arg;
165
166   int memsize;                  /* Size of RAM (megabytes). */
167
168   int selinux;                  /* selinux enabled? */
169
170   char *last_error;
171   int last_errnum;              /* errno, or 0 if there was no errno */
172
173   /* Callbacks. */
174   guestfs_abort_cb           abort_cb;
175   guestfs_error_handler_cb   error_cb;
176   void *                     error_cb_data;
177
178   /* Events. */
179   struct event *events;
180   size_t nr_events;
181
182   int msg_next_serial;
183
184   /* Information gathered by inspect_os.  Must be freed by calling
185    * guestfs___free_inspect_info.
186    */
187   struct inspect_fs *fses;
188   size_t nr_fses;
189
190   /* Private data area. */
191   struct hash_table *pda;
192   struct pda_entry *pda_next;
193
194   /* Used by src/actions.c:trace_* functions. */
195   FILE *trace_fp;
196   char *trace_buf;
197   size_t trace_len;
198 };
199
200 /* Per-filesystem data stored for inspect_os. */
201 enum inspect_fs_content {
202   FS_CONTENT_UNKNOWN = 0,
203   FS_CONTENT_LINUX_ROOT,
204   FS_CONTENT_WINDOWS_ROOT,
205   FS_CONTENT_WINDOWS_VOLUME_WITH_APPS,
206   FS_CONTENT_WINDOWS_VOLUME,
207   FS_CONTENT_LINUX_BOOT,
208   FS_CONTENT_LINUX_USR,
209   FS_CONTENT_LINUX_USR_LOCAL,
210   FS_CONTENT_LINUX_VAR,
211   FS_CONTENT_FREEBSD_ROOT,
212   FS_CONTENT_INSTALLER,
213 };
214
215 enum inspect_os_format {
216   OS_FORMAT_UNKNOWN = 0,
217   OS_FORMAT_INSTALLED,
218   OS_FORMAT_INSTALLER,
219   /* in future: supplemental disks */
220 };
221
222 enum inspect_os_type {
223   OS_TYPE_UNKNOWN = 0,
224   OS_TYPE_LINUX,
225   OS_TYPE_WINDOWS,
226   OS_TYPE_FREEBSD,
227 };
228
229 enum inspect_os_distro {
230   OS_DISTRO_UNKNOWN = 0,
231   OS_DISTRO_DEBIAN,
232   OS_DISTRO_FEDORA,
233   OS_DISTRO_REDHAT_BASED,
234   OS_DISTRO_RHEL,
235   OS_DISTRO_WINDOWS,
236   OS_DISTRO_PARDUS,
237   OS_DISTRO_ARCHLINUX,
238   OS_DISTRO_GENTOO,
239   OS_DISTRO_UBUNTU,
240   OS_DISTRO_MEEGO,
241   OS_DISTRO_LINUX_MINT,
242   OS_DISTRO_MANDRIVA,
243   OS_DISTRO_SLACKWARE,
244 };
245
246 enum inspect_os_package_format {
247   OS_PACKAGE_FORMAT_UNKNOWN = 0,
248   OS_PACKAGE_FORMAT_RPM,
249   OS_PACKAGE_FORMAT_DEB,
250   OS_PACKAGE_FORMAT_PACMAN,
251   OS_PACKAGE_FORMAT_EBUILD,
252   OS_PACKAGE_FORMAT_PISI
253 };
254
255 enum inspect_os_package_management {
256   OS_PACKAGE_MANAGEMENT_UNKNOWN = 0,
257   OS_PACKAGE_MANAGEMENT_YUM,
258   OS_PACKAGE_MANAGEMENT_UP2DATE,
259   OS_PACKAGE_MANAGEMENT_APT,
260   OS_PACKAGE_MANAGEMENT_PACMAN,
261   OS_PACKAGE_MANAGEMENT_PORTAGE,
262   OS_PACKAGE_MANAGEMENT_PISI,
263   OS_PACKAGE_MANAGEMENT_URPMI,
264 };
265
266 struct inspect_fs {
267   int is_root;
268   char *device;
269   int is_mountable;
270   int is_swap;
271   enum inspect_fs_content content;
272   enum inspect_os_type type;
273   enum inspect_os_distro distro;
274   enum inspect_os_package_format package_format;
275   enum inspect_os_package_management package_management;
276   char *product_name;
277   char *product_variant;
278   int major_version;
279   int minor_version;
280   char *arch;
281   char *hostname;
282   char *windows_systemroot;
283   char *windows_current_control_set;
284   char **drive_mappings;
285   enum inspect_os_format format;
286   int is_live_disk;
287   int is_netinst_disk;
288   int is_multipart_disk;
289   struct inspect_fstab_entry *fstab;
290   size_t nr_fstab;
291 };
292
293 struct inspect_fstab_entry {
294   char *device;
295   char *mountpoint;
296 };
297
298 struct guestfs_message_header;
299 struct guestfs_message_error;
300 struct guestfs_progress;
301
302 extern void guestfs_error (guestfs_h *g, const char *fs, ...)
303   __attribute__((format (printf,2,3)));
304 extern void guestfs_error_errno (guestfs_h *g, int errnum, const char *fs, ...)
305   __attribute__((format (printf,3,4)));
306 extern void guestfs_perrorf (guestfs_h *g, const char *fs, ...)
307   __attribute__((format (printf,2,3)));
308 extern void *guestfs_safe_realloc (guestfs_h *g, void *ptr, int nbytes);
309 extern char *guestfs_safe_strdup (guestfs_h *g, const char *str);
310 extern char *guestfs_safe_strndup (guestfs_h *g, const char *str, size_t n);
311 extern void *guestfs_safe_memdup (guestfs_h *g, void *ptr, size_t size);
312 extern char *guestfs_safe_asprintf (guestfs_h *g, const char *fs, ...)
313   __attribute__((format (printf,2,3)));
314 extern void guestfs___warning (guestfs_h *g, const char *fs, ...)
315   __attribute__((format (printf,2,3)));
316 extern void guestfs___debug (guestfs_h *g, const char *fs, ...)
317   __attribute__((format (printf,2,3)));
318 extern void guestfs___trace (guestfs_h *g, const char *fs, ...)
319   __attribute__((format (printf,2,3)));
320 extern const char *guestfs___persistent_tmpdir (void);
321 extern void guestfs___print_timestamped_argv (guestfs_h *g, const char *argv[]);
322 extern void guestfs___print_timestamped_message (guestfs_h *g, const char *fs, ...);
323 extern void guestfs___free_inspect_info (guestfs_h *g);
324 extern int guestfs___set_busy (guestfs_h *g);
325 extern int guestfs___end_busy (guestfs_h *g);
326 extern int guestfs___send (guestfs_h *g, int proc_nr, uint64_t progress_hint, uint64_t optargs_bitmask, xdrproc_t xdrp, char *args);
327 extern int guestfs___recv (guestfs_h *g, const char *fn, struct guestfs_message_header *hdr, struct guestfs_message_error *err, xdrproc_t xdrp, char *ret);
328 extern int guestfs___recv_discard (guestfs_h *g, const char *fn);
329 extern int guestfs___send_file (guestfs_h *g, const char *filename);
330 extern int guestfs___recv_file (guestfs_h *g, const char *filename);
331 extern int guestfs___send_to_daemon (guestfs_h *g, const void *v_buf, size_t n);
332 extern int guestfs___recv_from_daemon (guestfs_h *g, uint32_t *size_rtn, void **buf_rtn);
333 extern int guestfs___accept_from_daemon (guestfs_h *g);
334 extern void guestfs___progress_message_callback (guestfs_h *g, const struct guestfs_progress *message);
335 extern int guestfs___build_appliance (guestfs_h *g, char **kernel, char **initrd, char **appliance);
336 extern void guestfs___launch_send_progress (guestfs_h *g, int perdozen);
337 extern void guestfs___print_BufferIn (FILE *out, const char *buf, size_t buf_size);
338 extern void guestfs___print_BufferOut (FILE *out, const char *buf, size_t buf_size);
339 #ifdef HAVE_PCRE
340 extern int guestfs___match (guestfs_h *g, const char *str, const pcre *re);
341 extern char *guestfs___match1 (guestfs_h *g, const char *str, const pcre *re);
342 extern int guestfs___match2 (guestfs_h *g, const char *str, const pcre *re, char **ret1, char **ret2);
343 extern int guestfs___match3 (guestfs_h *g, const char *str, const pcre *re, char **ret1, char **ret2, char **ret3);
344 #endif
345 extern int guestfs___feature_available (guestfs_h *g, const char *feature);
346 extern void guestfs___free_string_list (char **);
347 extern int guestfs___checkpoint_cmdline (guestfs_h *g);
348 extern void guestfs___rollback_cmdline (guestfs_h *g, int pos);
349 extern void guestfs___call_callbacks_void (guestfs_h *g, uint64_t event);
350 extern void guestfs___call_callbacks_message (guestfs_h *g, uint64_t event, const char *buf, size_t buf_len);
351 extern void guestfs___call_callbacks_array (guestfs_h *g, uint64_t event, const uint64_t *array, size_t array_len);
352 #if defined(HAVE_PCRE) && defined(HAVE_HIVEX)
353 extern int guestfs___check_for_filesystem_on (guestfs_h *g, const char *device, int is_block, int is_partnum);
354 extern int guestfs___download_to_tmp (guestfs_h *g, const char *filename, const char *basename, int64_t max_size);
355 extern char *guestfs___case_sensitive_path_silently (guestfs_h *g, const char *);
356 extern struct inspect_fs *guestfs___search_for_root (guestfs_h *g, const char *root);
357 extern int guestfs___parse_unsigned_int (guestfs_h *g, const char *str);
358 extern int guestfs___parse_unsigned_int_ignore_trailing (guestfs_h *g, const char *str);
359 extern int guestfs___parse_major_minor (guestfs_h *g, struct inspect_fs *fs);
360 extern char *guestfs___first_line_of_file (guestfs_h *g, const char *filename);
361 extern int guestfs___first_egrep_of_file (guestfs_h *g, const char *filename, const char *eregex, int iflag, char **ret);
362 typedef int (*guestfs___db_dump_callback) (guestfs_h *g, const unsigned char *key, size_t keylen, const unsigned char *value, size_t valuelen, void *opaque);
363 extern int guestfs___read_db_dump (guestfs_h *g, const char *dumpfile, void *opaque, guestfs___db_dump_callback callback);
364 extern int guestfs___check_installer_root (guestfs_h *g, struct inspect_fs *fs);
365 extern int guestfs___check_linux_root (guestfs_h *g, struct inspect_fs *fs);
366 extern int guestfs___check_freebsd_root (guestfs_h *g, struct inspect_fs *fs);
367 extern int guestfs___check_windows_root (guestfs_h *g, struct inspect_fs *fs);
368 #endif
369
370 #define error(g,...) guestfs_error_errno((g),0,__VA_ARGS__)
371 #define perrorf guestfs_perrorf
372 #define warning(g,...) guestfs___warning((g),__VA_ARGS__)
373 #define debug(g,...) \
374   do { if ((g)->verbose) guestfs___debug ((g),__VA_ARGS__); } while (0)
375 #define safe_calloc guestfs_safe_calloc
376 #define safe_malloc guestfs_safe_malloc
377 #define safe_realloc guestfs_safe_realloc
378 #define safe_strdup guestfs_safe_strdup
379 #define safe_strndup guestfs_safe_strndup
380 #define safe_memdup guestfs_safe_memdup
381 #define safe_asprintf guestfs_safe_asprintf
382 #ifdef HAVE_PCRE
383 #define match guestfs___match
384 #define match1 guestfs___match1
385 #define match2 guestfs___match2
386 #define match3 guestfs___match3
387 #endif
388
389 #endif /* GUESTFS_INTERNAL_H_ */