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