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