Add internal facility to checkpoint and roll back the command line.
[libguestfs.git] / src / guestfs-internal.h
1 /* libguestfs
2  * Copyright (C) 2009-2010 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 #ifdef HAVE_PCRE
23 #include <pcre.h>
24 #endif
25
26 #define STREQ(a,b) (strcmp((a),(b)) == 0)
27 #define STRCASEEQ(a,b) (strcasecmp((a),(b)) == 0)
28 #define STRNEQ(a,b) (strcmp((a),(b)) != 0)
29 #define STRCASENEQ(a,b) (strcasecmp((a),(b)) != 0)
30 #define STREQLEN(a,b,n) (strncmp((a),(b),(n)) == 0)
31 #define STRCASEEQLEN(a,b,n) (strncasecmp((a),(b),(n)) == 0)
32 #define STRNEQLEN(a,b,n) (strncmp((a),(b),(n)) != 0)
33 #define STRCASENEQLEN(a,b,n) (strncasecmp((a),(b),(n)) != 0)
34 #define STRPREFIX(a,b) (strncmp((a),(b),strlen((b))) == 0)
35
36 #ifdef HAVE_GETTEXT
37 #include "gettext.h"
38 #define _(str) dgettext(PACKAGE, (str))
39 #define N_(str) dgettext(PACKAGE, (str))
40 #else
41 #define _(str) str
42 #define N_(str) str
43 #endif
44
45 #define TMP_TEMPLATE_ON_STACK(var)                        \
46   const char *ttos_tmpdir = guestfs_tmpdir ();            \
47   char var[strlen (ttos_tmpdir) + 32];                    \
48   sprintf (var, "%s/libguestfsXXXXXX", ttos_tmpdir)       \
49
50 #define UNIX_PATH_MAX 108
51
52 #ifndef MAX
53 #define MAX(a,b) ((a)>(b)?(a):(b))
54 #endif
55
56 #ifdef __APPLE__
57 #define xdr_uint32_t xdr_u_int32_t
58 #endif
59
60 /* Network configuration of the appliance.  Note these addresses are
61  * only meaningful within the context of the running appliance.  QEMU
62  * translates network connections to these magic addresses into
63  * userspace calls on the host (eg. connect(2)).  qemu-doc has a nice
64  * diagram which is also useful to refer to.
65  *
66  * NETWORK: The network.
67  *
68  * ROUTER: The address of the "host", ie. this library.
69  *
70  * [Note: If you change NETWORK and ROUTER then you also have to
71  * change the network configuration in appliance/init].
72  *
73  * GUESTFWD_ADDR, GUESTFWD_PORT: The guestfwd feature of qemu
74  * magically connects this pseudo-address to the guestfwd channel.  In
75  * typical Linux configurations of libguestfs, guestfwd is not
76  * actually used any more.
77  */
78 #define NETWORK "169.254.0.0/16"
79 #define ROUTER "169.254.2.2"
80
81 /* GuestFS handle and connection. */
82 enum state { CONFIG, LAUNCHING, READY, BUSY, NO_HANDLE };
83
84 struct guestfs_h
85 {
86   struct guestfs_h *next;       /* Linked list of open handles. */
87
88   /* State: see the state machine diagram in the man page guestfs(3). */
89   enum state state;
90
91   int fd[2];                    /* Stdin/stdout of qemu. */
92   int sock;                     /* Daemon communications socket. */
93   pid_t pid;                    /* Qemu PID. */
94   pid_t recoverypid;            /* Recovery process PID. */
95
96   struct timeval launch_t;      /* The time that we called guestfs_launch. */
97
98   char *tmpdir;                 /* Temporary directory containing socket. */
99
100   char *qemu_help, *qemu_version; /* Output of qemu -help, qemu -version. */
101
102   char **cmdline;               /* Qemu command line. */
103   int cmdline_size;
104
105   int verbose;
106   int trace;
107   int autosync;
108   int direct;
109   int recovery_proc;
110   int enable_network;
111
112   char *path;                   /* Path to kernel, initrd. */
113   char *qemu;                   /* Qemu binary. */
114   char *append;                 /* Append to kernel command line. */
115
116   int memsize;                  /* Size of RAM (megabytes). */
117
118   int selinux;                  /* selinux enabled? */
119
120   char *last_error;
121   int last_errnum;              /* errno, or 0 if there was no errno */
122
123   /* Callbacks. */
124   guestfs_abort_cb           abort_cb;
125   guestfs_error_handler_cb   error_cb;
126   void *                     error_cb_data;
127   guestfs_log_message_cb     log_message_cb;
128   void *                     log_message_cb_data;
129   guestfs_subprocess_quit_cb subprocess_quit_cb;
130   void *                     subprocess_quit_cb_data;
131   guestfs_launch_done_cb     launch_done_cb;
132   void *                     launch_done_cb_data;
133   guestfs_close_cb           close_cb;
134   void *                     close_cb_data;
135   guestfs_progress_cb        progress_cb;
136   void *                     progress_cb_data;
137
138   int msg_next_serial;
139
140   /* Information gathered by inspect_os.  Must be freed by calling
141    * guestfs___free_inspect_info.
142    */
143   struct inspect_fs *fses;
144   size_t nr_fses;
145
146   /* Private data area. */
147   struct hash_table *pda;
148 };
149
150 /* Per-filesystem data stored for inspect_os. */
151 enum inspect_fs_content {
152   FS_CONTENT_UNKNOWN = 0,
153   FS_CONTENT_LINUX_ROOT,
154   FS_CONTENT_WINDOWS_ROOT,
155   FS_CONTENT_LINUX_BOOT,
156   FS_CONTENT_LINUX_USR,
157   FS_CONTENT_LINUX_USR_LOCAL,
158   FS_CONTENT_LINUX_VAR,
159 };
160
161 enum inspect_os_type {
162   OS_TYPE_UNKNOWN = 0,
163   OS_TYPE_LINUX,
164   OS_TYPE_WINDOWS,
165 };
166
167 enum inspect_os_distro {
168   OS_DISTRO_UNKNOWN = 0,
169   OS_DISTRO_DEBIAN,
170   OS_DISTRO_FEDORA,
171   OS_DISTRO_REDHAT_BASED,
172   OS_DISTRO_RHEL,
173   OS_DISTRO_WINDOWS,
174   OS_DISTRO_PARDUS,
175   OS_DISTRO_ARCHLINUX,
176   OS_DISTRO_GENTOO,
177   OS_DISTRO_UBUNTU,
178   OS_DISTRO_MEEGO,
179 };
180
181 struct inspect_fs {
182   int is_root;
183   char *device;
184   int is_mountable;
185   int is_swap;
186   enum inspect_fs_content content;
187   enum inspect_os_type type;
188   enum inspect_os_distro distro;
189   char *product_name;
190   int major_version;
191   int minor_version;
192   char *arch;
193   char *windows_systemroot;
194   struct inspect_fstab_entry *fstab;
195   size_t nr_fstab;
196 };
197
198 struct inspect_fstab_entry {
199   char *device;
200   char *mountpoint;
201 };
202
203 struct guestfs_message_header;
204 struct guestfs_message_error;
205
206 extern void guestfs_error (guestfs_h *g, const char *fs, ...)
207   __attribute__((format (printf,2,3)));
208 extern void guestfs_error_errno (guestfs_h *g, int errnum, const char *fs, ...)
209   __attribute__((format (printf,3,4)));
210 extern void guestfs_perrorf (guestfs_h *g, const char *fs, ...)
211   __attribute__((format (printf,2,3)));
212 extern void *guestfs_safe_realloc (guestfs_h *g, void *ptr, int nbytes);
213 extern char *guestfs_safe_strdup (guestfs_h *g, const char *str);
214 extern char *guestfs_safe_strndup (guestfs_h *g, const char *str, size_t n);
215 extern void *guestfs_safe_memdup (guestfs_h *g, void *ptr, size_t size);
216 extern void guestfs___print_timestamped_argv (guestfs_h *g, const char *argv[]);
217 extern void guestfs___print_timestamped_message (guestfs_h *g, const char *fs, ...);
218 extern void guestfs___free_inspect_info (guestfs_h *g);
219 extern int guestfs___set_busy (guestfs_h *g);
220 extern int guestfs___end_busy (guestfs_h *g);
221 extern int guestfs___send (guestfs_h *g, int proc_nr, xdrproc_t xdrp, char *args);
222 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);
223 extern int guestfs___send_file (guestfs_h *g, const char *filename);
224 extern int guestfs___recv_file (guestfs_h *g, const char *filename);
225 extern int guestfs___send_to_daemon (guestfs_h *g, const void *v_buf, size_t n);
226 extern int guestfs___recv_from_daemon (guestfs_h *g, uint32_t *size_rtn, void **buf_rtn);
227 extern int guestfs___accept_from_daemon (guestfs_h *g);
228 extern int guestfs___build_appliance (guestfs_h *g, char **kernel, char **initrd, char **appliance);
229 extern void guestfs___print_BufferIn (FILE *out, const char *buf, size_t buf_size);
230 #ifdef HAVE_PCRE
231 extern int guestfs___match (guestfs_h *g, const char *str, const pcre *re);
232 extern char *guestfs___match1 (guestfs_h *g, const char *str, const pcre *re);
233 extern int guestfs___match2 (guestfs_h *g, const char *str, const pcre *re, char **ret1, char **ret2);
234 #endif
235 extern int guestfs___feature_available (guestfs_h *g, const char *feature);
236 extern void guestfs___free_string_list (char **);
237 extern int guestfs___checkpoint_cmdline (guestfs_h *g);
238 extern void guestfs___rollback_cmdline (guestfs_h *g, int pos);
239
240 #define error(g,...) guestfs_error_errno((g),0,__VA_ARGS__)
241 #define perrorf guestfs_perrorf
242 #define safe_calloc guestfs_safe_calloc
243 #define safe_malloc guestfs_safe_malloc
244 #define safe_realloc guestfs_safe_realloc
245 #define safe_strdup guestfs_safe_strdup
246 #define safe_strndup guestfs_safe_strndup
247 #define safe_memdup guestfs_safe_memdup
248 #ifdef HAVE_PCRE
249 #define match guestfs___match
250 #define match1 guestfs___match1
251 #define match2 guestfs___match2
252 #endif
253
254 #endif /* GUESTFS_INTERNAL_H_ */