2 * Copyright (C) 2009 Red Hat Inc.
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.
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.
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
21 #define _BSD_SOURCE /* for mkdtemp, usleep */
22 #define _GNU_SOURCE /* for vasprintf, GNU strerror_r, strchrnul */
33 #include <sys/select.h>
35 #include <rpc/types.h>
42 #ifdef HAVE_SYS_TYPES_H
43 #include <sys/types.h>
46 #ifdef HAVE_SYS_WAIT_H
50 #ifdef HAVE_SYS_SOCKET_H
51 #include <sys/socket.h>
59 #include "guestfs_protocol.h"
61 #define error guestfs_error
62 #define perrorf guestfs_perrorf
63 #define safe_malloc guestfs_safe_malloc
64 #define safe_realloc guestfs_safe_realloc
65 #define safe_strdup guestfs_safe_strdup
66 #define safe_memdup guestfs_safe_memdup
68 static void default_error_cb (guestfs_h *g, void *data, const char *msg);
69 static void stdout_event (struct guestfs_main_loop *ml, guestfs_h *g, void *data, int watch, int fd, int events);
70 static void sock_read_event (struct guestfs_main_loop *ml, guestfs_h *g, void *data, int watch, int fd, int events);
71 static void sock_write_event (struct guestfs_main_loop *ml, guestfs_h *g, void *data, int watch, int fd, int events);
73 static void close_handles (void);
75 static int select_add_handle (guestfs_main_loop *ml, guestfs_h *g, int fd, int events, guestfs_handle_event_cb cb, void *data);
76 static int select_remove_handle (guestfs_main_loop *ml, guestfs_h *g, int watch);
77 static int select_add_timeout (guestfs_main_loop *ml, guestfs_h *g, int interval, guestfs_handle_timeout_cb cb, void *data);
78 static int select_remove_timeout (guestfs_main_loop *ml, guestfs_h *g, int timer);
79 static int select_main_loop_run (guestfs_main_loop *ml, guestfs_h *g);
80 static int select_main_loop_quit (guestfs_main_loop *ml, guestfs_h *g);
82 /* Default select-based main loop. */
83 struct select_handle_cb_data {
84 guestfs_handle_event_cb cb;
89 struct select_main_loop {
90 /* NB. These fields must be the same as in struct guestfs_main_loop: */
91 guestfs_add_handle_cb add_handle;
92 guestfs_remove_handle_cb remove_handle;
93 guestfs_add_timeout_cb add_timeout;
94 guestfs_remove_timeout_cb remove_timeout;
95 guestfs_main_loop_run_cb main_loop_run;
96 guestfs_main_loop_quit_cb main_loop_quit;
98 /* Additional private data: */
107 struct select_handle_cb_data *handle_cb_data;
110 /* Default main loop. */
111 static struct select_main_loop default_main_loop = {
112 .add_handle = select_add_handle,
113 .remove_handle = select_remove_handle,
114 .add_timeout = select_add_timeout,
115 .remove_timeout = select_remove_timeout,
116 .main_loop_run = select_main_loop_run,
117 .main_loop_quit = select_main_loop_quit,
119 /* XXX hopefully .rset, .wset, .xset are initialized to the empty
120 * set by the normal action of everything being initialized to zero.
125 .handle_cb_data = NULL,
128 #define UNIX_PATH_MAX 108
130 /* Also in guestfsd.c */
131 #define VMCHANNEL_PORT 6666
132 #define VMCHANNEL_ADDR "10.0.2.4"
134 /* GuestFS handle and connection. */
135 enum state { CONFIG, LAUNCHING, READY, BUSY, NO_HANDLE };
139 struct guestfs_h *next; /* Linked list of open handles. */
141 /* State: see the state machine diagram in the man page guestfs(3). */
144 int fd[2]; /* Stdin/stdout of qemu. */
145 int sock; /* Daemon communications socket. */
146 pid_t pid; /* Qemu PID. */
147 pid_t recoverypid; /* Recovery process PID. */
148 time_t start_t; /* The time when we started qemu. */
150 int stdout_watch; /* Watches qemu stdout for log messages. */
151 int sock_watch; /* Watches daemon comm socket. */
153 char *tmpdir; /* Temporary directory containing socket. */
155 char **cmdline; /* Qemu command line. */
167 guestfs_abort_cb abort_cb;
168 guestfs_error_handler_cb error_cb;
169 void * error_cb_data;
170 guestfs_send_cb send_cb;
172 guestfs_reply_cb reply_cb;
173 void * reply_cb_data;
174 guestfs_log_message_cb log_message_cb;
175 void * log_message_cb_data;
176 guestfs_subprocess_quit_cb subprocess_quit_cb;
177 void * subprocess_quit_cb_data;
178 guestfs_launch_done_cb launch_done_cb;
179 void * launch_done_cb_data;
181 /* Main loop used by this handle. */
182 guestfs_main_loop *main_loop;
184 /* Messages sent and received from the daemon. */
186 int msg_in_size, msg_in_allocated;
188 int msg_out_size, msg_out_pos;
193 static guestfs_h *handles = NULL;
194 static int atexit_handler_set = 0;
197 guestfs_create (void)
202 g = malloc (sizeof (*g));
205 memset (g, 0, sizeof (*g));
212 g->stdout_watch = -1;
216 g->error_cb = default_error_cb;
217 g->error_cb_data = NULL;
219 str = getenv ("LIBGUESTFS_DEBUG");
220 g->verbose = str != NULL && strcmp (str, "1") == 0;
222 str = getenv ("LIBGUESTFS_PATH");
223 g->path = str != NULL ? str : GUESTFS_DEFAULT_PATH;
225 str = getenv ("LIBGUESTFS_QEMU");
226 g->qemu = str != NULL ? str : QEMU;
228 g->main_loop = guestfs_get_default_main_loop ();
230 /* Start with large serial numbers so they are easy to spot
231 * inside the protocol.
233 g->msg_next_serial = 0x00123400;
235 /* Link the handles onto a global list. This is the one area
236 * where the library needs to be made thread-safe. (XXX)
238 /* acquire mutex (XXX) */
241 if (!atexit_handler_set) {
242 atexit (close_handles);
243 atexit_handler_set = 1;
245 /* release mutex (XXX) */
248 fprintf (stderr, "new guestfs handle %p\n", g);
254 guestfs_close (guestfs_h *g)
260 if (g->state == NO_HANDLE) {
261 /* Not safe to call 'error' here, so ... */
262 fprintf (stderr, "guestfs_close: called twice on the same handle\n");
267 fprintf (stderr, "closing guestfs handle %p (state %d)\n", g, g->state);
269 /* Try to sync if autosync flag is set. */
270 if (g->autosync && g->state == READY)
273 /* Remove any handlers that might be called back before we kill the
276 g->log_message_cb = NULL;
278 if (g->state != CONFIG)
279 guestfs_kill_subprocess (g);
282 snprintf (filename, sizeof filename, "%s/sock", g->tmpdir);
291 for (i = 0; i < g->cmdline_size; ++i)
292 free (g->cmdline[i]);
296 /* Mark the handle as dead before freeing it. */
297 g->state = NO_HANDLE;
299 /* acquire mutex (XXX) */
303 for (gg = handles; gg->next != g; gg = gg->next)
307 /* release mutex (XXX) */
309 free (g->last_error);
313 /* Close all open handles (called from atexit(3)). */
317 while (handles) guestfs_close (handles);
321 guestfs_last_error (guestfs_h *g)
323 return g->last_error;
327 set_last_error (guestfs_h *g, const char *msg)
329 free (g->last_error);
330 g->last_error = strdup (msg);
334 default_error_cb (guestfs_h *g, void *data, const char *msg)
336 fprintf (stderr, "libguestfs: error: %s\n", msg);
340 guestfs_error (guestfs_h *g, const char *fs, ...)
346 vasprintf (&msg, fs, args);
349 if (g->error_cb) g->error_cb (g, g->error_cb_data, msg);
350 set_last_error (g, msg);
356 guestfs_perrorf (guestfs_h *g, const char *fs, ...)
363 vasprintf (&msg, fs, args);
368 strerror_r (err, buf, sizeof buf);
372 buf = strerror_r (err, _buf, sizeof _buf);
375 msg = safe_realloc (g, msg, strlen (msg) + 2 + strlen (buf) + 1);
379 if (g->error_cb) g->error_cb (g, g->error_cb_data, msg);
380 set_last_error (g, msg);
386 guestfs_safe_malloc (guestfs_h *g, size_t nbytes)
388 void *ptr = malloc (nbytes);
389 if (!ptr) g->abort_cb ();
394 guestfs_safe_realloc (guestfs_h *g, void *ptr, int nbytes)
396 void *p = realloc (ptr, nbytes);
397 if (!p) g->abort_cb ();
402 guestfs_safe_strdup (guestfs_h *g, const char *str)
404 char *s = strdup (str);
405 if (!s) g->abort_cb ();
410 guestfs_safe_memdup (guestfs_h *g, void *ptr, size_t size)
412 void *p = malloc (size);
413 if (!p) g->abort_cb ();
414 memcpy (p, ptr, size);
419 xwrite (int fd, const void *buf, size_t len)
424 r = write (fd, buf, len);
436 xread (int fd, void *buf, size_t len)
441 r = read (fd, buf, len);
453 guestfs_set_out_of_memory_handler (guestfs_h *g, guestfs_abort_cb cb)
459 guestfs_get_out_of_memory_handler (guestfs_h *g)
465 guestfs_set_error_handler (guestfs_h *g, guestfs_error_handler_cb cb, void *data)
468 g->error_cb_data = data;
471 guestfs_error_handler_cb
472 guestfs_get_error_handler (guestfs_h *g, void **data_rtn)
474 if (data_rtn) *data_rtn = g->error_cb_data;
479 guestfs_set_verbose (guestfs_h *g, int v)
486 guestfs_get_verbose (guestfs_h *g)
492 guestfs_set_autosync (guestfs_h *g, int a)
499 guestfs_get_autosync (guestfs_h *g)
505 guestfs_set_path (guestfs_h *g, const char *path)
508 g->path = GUESTFS_DEFAULT_PATH;
515 guestfs_get_path (guestfs_h *g)
521 guestfs_set_qemu (guestfs_h *g, const char *qemu)
531 guestfs_get_qemu (guestfs_h *g)
536 /* Add a string to the current command line. */
538 incr_cmdline_size (guestfs_h *g)
540 if (g->cmdline == NULL) {
541 /* g->cmdline[0] is reserved for argv[0], set in guestfs_launch. */
543 g->cmdline = safe_malloc (g, sizeof (char *));
544 g->cmdline[0] = NULL;
548 g->cmdline = safe_realloc (g, g->cmdline, sizeof (char *) * g->cmdline_size);
552 add_cmdline (guestfs_h *g, const char *str)
554 if (g->state != CONFIG) {
555 error (g, "command line cannot be altered after qemu subprocess launched");
559 incr_cmdline_size (g);
560 g->cmdline[g->cmdline_size-1] = safe_strdup (g, str);
565 guestfs_config (guestfs_h *g,
566 const char *qemu_param, const char *qemu_value)
568 if (qemu_param[0] != '-') {
569 error (g, "guestfs_config: parameter must begin with '-' character");
573 /* A bit fascist, but the user will probably break the extra
574 * parameters that we add if they try to set any of these.
576 if (strcmp (qemu_param, "-kernel") == 0 ||
577 strcmp (qemu_param, "-initrd") == 0 ||
578 strcmp (qemu_param, "-nographic") == 0 ||
579 strcmp (qemu_param, "-serial") == 0 ||
580 strcmp (qemu_param, "-vnc") == 0 ||
581 strcmp (qemu_param, "-full-screen") == 0 ||
582 strcmp (qemu_param, "-std-vga") == 0 ||
583 strcmp (qemu_param, "-vnc") == 0) {
584 error (g, "guestfs_config: parameter '%s' isn't allowed", qemu_param);
588 if (add_cmdline (g, qemu_param) != 0) return -1;
590 if (qemu_value != NULL) {
591 if (add_cmdline (g, qemu_value) != 0) return -1;
598 guestfs_add_drive (guestfs_h *g, const char *filename)
600 int len = strlen (filename) + 64;
603 if (strchr (filename, ',') != NULL) {
604 error (g, "filename cannot contain ',' (comma) character");
608 if (access (filename, F_OK) == -1) {
609 perrorf (g, "%s", filename);
613 snprintf (buf, len, "file=%s", filename);
615 return guestfs_config (g, "-drive", buf);
619 guestfs_add_cdrom (guestfs_h *g, const char *filename)
621 if (strchr (filename, ',') != NULL) {
622 error (g, "filename cannot contain ',' (comma) character");
626 if (access (filename, F_OK) == -1) {
627 perrorf (g, "%s", filename);
631 return guestfs_config (g, "-cdrom", filename);
635 guestfs_launch (guestfs_h *g)
637 static const char *dir_template = "/tmp/libguestfsXXXXXX";
638 int r, i, len, pmore, memsize;
641 const char *kernel_name = "vmlinuz." REPO "." host_cpu;
642 const char *initrd_name = "initramfs." REPO "." host_cpu ".img";
643 char *path, *pelem, *pend;
644 char *kernel = NULL, *initrd = NULL;
646 struct sockaddr_un addr;
651 error (g, "you must call guestfs_add_drive before guestfs_launch");
655 if (g->state != CONFIG) {
656 error (g, "qemu has already been launched");
660 /* Search g->path for the kernel and initrd. */
661 pelem = path = safe_strdup (g, g->path);
663 pend = strchrnul (pelem, ':');
664 pmore = *pend == ':';
668 /* Empty element or "." means cwd. */
669 if (len == 0 || (len == 1 && *pelem == '.')) {
672 "looking for kernel and initrd in current directory\n");
673 if (access (kernel_name, F_OK) == 0 && access (initrd_name, F_OK) == 0) {
674 kernel = safe_strdup (g, kernel_name);
675 initrd = safe_strdup (g, initrd_name);
679 /* Look at <path>/kernel etc. */
681 kernel = safe_malloc (g, len + strlen (kernel_name) + 2);
682 initrd = safe_malloc (g, len + strlen (initrd_name) + 2);
683 sprintf (kernel, "%s/%s", pelem, kernel_name);
684 sprintf (initrd, "%s/%s", pelem, initrd_name);
687 fprintf (stderr, "looking for %s and %s\n", kernel, initrd);
689 if (access (kernel, F_OK) == 0 && access (initrd, F_OK) == 0)
693 kernel = initrd = NULL;
701 if (kernel == NULL || initrd == NULL) {
702 error (g, "cannot find %s or %s on LIBGUESTFS_PATH (current path = %s)",
703 kernel_name, initrd_name, g->path);
707 /* Choose a suitable memory size (in MB). This is more art
708 * than science, but you can help by doing
709 * ./configure --enable-debug-command
713 * and seeing how much free memory is left for particular
716 * It's also helpful to report both the compressed and uncompressed
717 * size of the initramfs (ls -lh initramfs*.img; du -sh initramfs).
719 * XXX KVM virtio balloon driver?
721 if (stat (initrd, &statbuf) != -1) {
722 /* Approximate size of the initramfs after it is decompressed
723 * in kernel memory. The compression factor is ~2.5-3.
725 memsize = 3 * statbuf.st_size / 1024 / 1024;
727 /* Approximate size used by the kernel. */
730 /* Want to give userspace some room, so: */
734 /* On 64 bit, assume some overhead. */
741 /* Make the temporary directory containing the socket. */
743 g->tmpdir = safe_strdup (g, dir_template);
744 if (mkdtemp (g->tmpdir) == NULL) {
745 perrorf (g, "%s: cannot create temporary directory", dir_template);
750 snprintf (unixsock, sizeof unixsock, "%s/sock", g->tmpdir);
753 if (pipe (wfd) == -1 || pipe (rfd) == -1) {
768 if (r == 0) { /* Child (qemu). */
771 char memsize_str[256];
773 /* Set up the full command line. Do this in the subprocess so we
774 * don't need to worry about cleaning up.
776 g->cmdline[0] = (char *) g->qemu;
778 /* Construct the -net channel parameter for qemu. */
779 snprintf (vmchannel, sizeof vmchannel,
780 "channel,%d:unix:%s,server,nowait",
781 VMCHANNEL_PORT, unixsock);
783 /* Linux kernel command line. */
784 snprintf (append, sizeof append,
785 "console=ttyS0 guestfs=%s:%d%s",
786 VMCHANNEL_ADDR, VMCHANNEL_PORT,
787 g->verbose ? " guestfs_verbose=1" : "");
789 snprintf (memsize_str, sizeof memsize_str, "%d", memsize);
791 add_cmdline (g, "-m");
792 add_cmdline (g, memsize_str);
794 add_cmdline (g, "-no-kqemu"); /* Avoids a warning. */
796 add_cmdline (g, "-kernel");
797 add_cmdline (g, (char *) kernel);
798 add_cmdline (g, "-initrd");
799 add_cmdline (g, (char *) initrd);
800 add_cmdline (g, "-append");
801 add_cmdline (g, append);
802 add_cmdline (g, "-nographic");
803 add_cmdline (g, "-serial");
804 add_cmdline (g, "stdio");
805 add_cmdline (g, "-net");
806 add_cmdline (g, vmchannel);
807 add_cmdline (g, "-net");
808 add_cmdline (g, "user,vlan=0");
809 add_cmdline (g, "-net");
810 add_cmdline (g, "nic,model=rtl8139,vlan=0");
811 incr_cmdline_size (g);
812 g->cmdline[g->cmdline_size-1] = NULL;
815 fprintf (stderr, "%s", g->qemu);
816 for (i = 0; g->cmdline[i]; ++i)
817 fprintf (stderr, " %s", g->cmdline[i]);
818 fprintf (stderr, "\n");
821 /* Set up stdin, stdout. */
832 /* Set up a new process group, so we can signal this process
833 * and all subprocesses (eg. if qemu is really a shell script).
838 execv (g->qemu, g->cmdline); /* Run qemu. */
843 /* Parent (library). */
846 /* Fork the recovery process off which will kill qemu if the parent
847 * process fails to do so (eg. if the parent segfaults).
851 pid_t qemu_pid = g->pid;
852 pid_t parent_pid = getppid ();
854 /* Writing to argv is hideously complicated and error prone. See:
855 * http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/backend/utils/misc/ps_status.c?rev=1.33.2.1;content-type=text%2Fplain
858 /* Loop around waiting for one or both of the other processes to
859 * disappear. It's fair to say this is very hairy. The PIDs that
860 * we are looking at might be reused by another process. We are
861 * effectively polling. Is the cure worse than the disease?
864 if (kill (qemu_pid, 0) == -1) /* qemu's gone away, we aren't needed */
866 if (kill (parent_pid, 0) == -1) {
867 /* Parent's gone away, qemu still around, so kill qemu. */
875 /* Don't worry, if the fork failed, this will be -1. The recovery
876 * process isn't essential.
880 /* Start the clock ... */
883 /* Close the other ends of the pipe. */
887 if (fcntl (wfd[1], F_SETFL, O_NONBLOCK) == -1 ||
888 fcntl (rfd[0], F_SETFL, O_NONBLOCK) == -1) {
889 perrorf (g, "fcntl");
893 g->fd[0] = wfd[1]; /* stdin of child */
894 g->fd[1] = rfd[0]; /* stdout of child */
896 /* Open the Unix socket. The vmchannel implementation that got
897 * merged with qemu sucks in a number of ways. Both ends do
898 * connect(2), which means that no one knows what, if anything, is
899 * connected to the other end, or if it becomes disconnected. Even
900 * worse, we have to wait some indeterminate time for qemu to create
901 * the socket and connect to it (which happens very early in qemu's
902 * start-up), so any code that uses vmchannel is inherently racy.
903 * Hence this silly loop.
905 g->sock = socket (AF_UNIX, SOCK_STREAM, 0);
907 perrorf (g, "socket");
911 if (fcntl (g->sock, F_SETFL, O_NONBLOCK) == -1) {
912 perrorf (g, "fcntl");
916 addr.sun_family = AF_UNIX;
917 strncpy (addr.sun_path, unixsock, UNIX_PATH_MAX);
918 addr.sun_path[UNIX_PATH_MAX-1] = '\0';
922 /* Always sleep at least once to give qemu a small chance to start up. */
925 r = connect (g->sock, (struct sockaddr *) &addr, sizeof addr);
926 if ((r == -1 && errno == EINPROGRESS) || r == 0)
930 perrorf (g, "connect");
934 error (g, "failed to connect to vmchannel socket");
938 /* Watch the file descriptors. */
941 g->msg_in_size = g->msg_in_allocated = 0;
949 g->main_loop->add_handle (g->main_loop, g, g->fd[1],
950 GUESTFS_HANDLE_READABLE,
952 if (g->stdout_watch == -1) {
953 error (g, "could not watch qemu stdout");
957 if (guestfs__switch_to_receiving (g) == -1)
960 g->state = LAUNCHING;
964 if (g->stdout_watch >= 0)
965 g->main_loop->remove_handle (g->main_loop, g, g->stdout_watch);
966 if (g->sock_watch >= 0)
967 g->main_loop->remove_handle (g->main_loop, g, g->sock_watch);
976 if (g->recoverypid > 0) kill (g->recoverypid, 9);
977 waitpid (g->pid, NULL, 0);
978 if (g->recoverypid > 0) waitpid (g->recoverypid, NULL, 0);
985 g->stdout_watch = -1;
995 finish_wait_ready (guestfs_h *g, void *vp)
998 fprintf (stderr, "finish_wait_ready called, %p, vp = %p\n", g, vp);
1001 g->main_loop->main_loop_quit (g->main_loop, g);
1005 guestfs_wait_ready (guestfs_h *g)
1007 int finished = 0, r;
1009 if (g->state == READY) return 0;
1011 if (g->state == BUSY) {
1012 error (g, "qemu has finished launching already");
1016 if (g->state != LAUNCHING) {
1017 error (g, "qemu has not been launched yet");
1021 g->launch_done_cb = finish_wait_ready;
1022 g->launch_done_cb_data = &finished;
1023 r = g->main_loop->main_loop_run (g->main_loop, g);
1024 g->launch_done_cb = NULL;
1025 g->launch_done_cb_data = NULL;
1027 if (r == -1) return -1;
1029 if (finished != 1) {
1030 error (g, "guestfs_wait_ready failed, see earlier error messages");
1034 /* This is possible in some really strange situations, such as
1035 * guestfsd starts up OK but then qemu immediately exits. Check for
1036 * it because the caller is probably expecting to be able to send
1037 * commands after this function returns.
1039 if (g->state != READY) {
1040 error (g, "qemu launched and contacted daemon, but state != READY");
1048 guestfs_kill_subprocess (guestfs_h *g)
1050 if (g->state == CONFIG) {
1051 error (g, "no subprocess to kill");
1056 fprintf (stderr, "sending SIGTERM to process %d\n", g->pid);
1058 kill (g->pid, SIGTERM);
1059 if (g->recoverypid > 0) kill (g->recoverypid, 9);
1064 /* Access current state. */
1066 guestfs_is_config (guestfs_h *g)
1068 return g->state == CONFIG;
1072 guestfs_is_launching (guestfs_h *g)
1074 return g->state == LAUNCHING;
1078 guestfs_is_ready (guestfs_h *g)
1080 return g->state == READY;
1084 guestfs_is_busy (guestfs_h *g)
1086 return g->state == BUSY;
1090 guestfs_get_state (guestfs_h *g)
1096 guestfs_set_ready (guestfs_h *g)
1098 if (g->state != BUSY) {
1099 error (g, "guestfs_set_ready: called when in state %d != BUSY", g->state);
1107 guestfs_set_busy (guestfs_h *g)
1109 if (g->state != READY) {
1110 error (g, "guestfs_set_busy: called when in state %d != READY", g->state);
1117 /* Structure-freeing functions. These rely on the fact that the
1118 * structure format is identical to the XDR format. See note in
1122 guestfs_free_int_bool (struct guestfs_int_bool *x)
1128 guestfs_free_lvm_pv_list (struct guestfs_lvm_pv_list *x)
1130 xdr_free ((xdrproc_t) xdr_guestfs_lvm_int_pv_list, (char *) x);
1135 guestfs_free_lvm_vg_list (struct guestfs_lvm_vg_list *x)
1137 xdr_free ((xdrproc_t) xdr_guestfs_lvm_int_vg_list, (char *) x);
1142 guestfs_free_lvm_lv_list (struct guestfs_lvm_lv_list *x)
1144 xdr_free ((xdrproc_t) xdr_guestfs_lvm_int_lv_list, (char *) x);
1148 /* This function is called whenever qemu prints something on stdout.
1149 * Qemu's stdout is also connected to the guest's serial console, so
1150 * we see kernel messages here too.
1153 stdout_event (struct guestfs_main_loop *ml, guestfs_h *g, void *data,
1154 int watch, int fd, int events)
1162 "stdout_event: %p g->state = %d, fd = %d, events = 0x%x\n",
1163 g, g->state, fd, events);
1166 if (g->fd[1] != fd) {
1167 error (g, "stdout_event: internal error: %d != %d", g->fd[1], fd);
1171 n = read (fd, buf, sizeof buf);
1173 /* Hopefully this indicates the qemu child process has died. */
1175 fprintf (stderr, "stdout_event: %p: child process died\n", g);
1176 /*kill (g->pid, SIGTERM);*/
1177 if (g->recoverypid > 0) kill (g->recoverypid, 9);
1178 waitpid (g->pid, NULL, 0);
1179 if (g->recoverypid > 0) waitpid (g->recoverypid, NULL, 0);
1180 if (g->stdout_watch >= 0)
1181 g->main_loop->remove_handle (g->main_loop, g, g->stdout_watch);
1182 if (g->sock_watch >= 0)
1183 g->main_loop->remove_handle (g->main_loop, g, g->sock_watch);
1193 g->stdout_watch = -1;
1196 if (g->subprocess_quit_cb)
1197 g->subprocess_quit_cb (g, g->subprocess_quit_cb_data);
1202 if (errno != EAGAIN)
1203 perrorf (g, "read");
1207 /* In verbose mode, copy all log messages to stderr. */
1211 /* It's an actual log message, send it upwards if anyone is listening. */
1212 if (g->log_message_cb)
1213 g->log_message_cb (g, g->log_message_cb_data, buf, n);
1216 /* The function is called whenever we can read something on the
1217 * guestfsd (daemon inside the guest) communication socket.
1220 sock_read_event (struct guestfs_main_loop *ml, guestfs_h *g, void *data,
1221 int watch, int fd, int events)
1229 "sock_read_event: %p g->state = %d, fd = %d, events = 0x%x\n",
1230 g, g->state, fd, events);
1232 if (g->sock != fd) {
1233 error (g, "sock_read_event: internal error: %d != %d", g->sock, fd);
1237 if (g->msg_in_size <= g->msg_in_allocated) {
1238 g->msg_in_allocated += 4096;
1239 g->msg_in = safe_realloc (g, g->msg_in, g->msg_in_allocated);
1241 n = read (g->sock, g->msg_in + g->msg_in_size,
1242 g->msg_in_allocated - g->msg_in_size);
1244 /* Disconnected? Ignore it because stdout_watch will get called
1245 * and will do the cleanup.
1250 if (errno != EAGAIN)
1251 perrorf (g, "read");
1255 g->msg_in_size += n;
1257 /* Have we got enough of a message to be able to process it yet? */
1259 if (g->msg_in_size < 4) return;
1261 xdrmem_create (&xdr, g->msg_in, g->msg_in_size, XDR_DECODE);
1262 if (!xdr_uint32_t (&xdr, &len)) {
1263 error (g, "can't decode length word");
1267 /* Length is normally the length of the message, but when guestfsd
1268 * starts up it sends a "magic" value (longer than any possible
1269 * message). Check for this.
1271 if (len == GUESTFS_LAUNCH_FLAG) {
1272 if (g->state != LAUNCHING)
1273 error (g, "received magic signature from guestfsd, but in state %d",
1275 else if (g->msg_in_size != 4)
1276 error (g, "received magic signature from guestfsd, but msg size is %d",
1280 if (g->launch_done_cb)
1281 g->launch_done_cb (g, g->launch_done_cb_data);
1287 /* This can happen if a cancellation happens right at the end
1288 * of us sending a FileIn parameter to the daemon. Discard. The
1289 * daemon should send us an error message next.
1291 if (len == GUESTFS_CANCEL_FLAG) {
1292 g->msg_in_size -= 4;
1293 memmove (g->msg_in, g->msg_in+4, g->msg_in_size);
1297 /* If this happens, it's pretty bad and we've probably lost
1300 if (len > GUESTFS_MESSAGE_MAX) {
1301 error (g, "message length (%u) > maximum possible size (%d)",
1302 len, GUESTFS_MESSAGE_MAX);
1306 if (g->msg_in_size-4 < len) return; /* Need more of this message. */
1308 /* Got the full message, begin processing it. */
1312 for (i = 0; i < g->msg_in_size; i += 16) {
1313 printf ("%04x: ", i);
1314 for (j = i; j < MIN (i+16, g->msg_in_size); ++j)
1315 printf ("%02x ", (unsigned char) g->msg_in[j]);
1316 for (; j < i+16; ++j)
1319 for (j = i; j < MIN (i+16, g->msg_in_size); ++j)
1320 if (isprint (g->msg_in[j]))
1321 printf ("%c", g->msg_in[j]);
1324 for (; j < i+16; ++j)
1330 /* Not in the expected state. */
1331 if (g->state != BUSY)
1332 error (g, "state %d != BUSY", g->state);
1334 /* Push the message up to the higher layer. */
1336 g->reply_cb (g, g->reply_cb_data, &xdr);
1338 g->msg_in_size -= len + 4;
1339 memmove (g->msg_in, g->msg_in+len+4, g->msg_in_size);
1340 if (g->msg_in_size > 0) goto again;
1343 /* Free the message buffer if it's grown excessively large. */
1344 if (g->msg_in_allocated > 65536) {
1347 g->msg_in_size = g->msg_in_allocated = 0;
1354 /* The function is called whenever we can write something on the
1355 * guestfsd (daemon inside the guest) communication socket.
1358 sock_write_event (struct guestfs_main_loop *ml, guestfs_h *g, void *data,
1359 int watch, int fd, int events)
1365 "sock_write_event: %p g->state = %d, fd = %d, events = 0x%x\n",
1366 g, g->state, fd, events);
1368 if (g->sock != fd) {
1369 error (g, "sock_write_event: internal error: %d != %d", g->sock, fd);
1373 if (g->state != BUSY) {
1374 error (g, "sock_write_event: state %d != BUSY", g->state);
1379 fprintf (stderr, "sock_write_event: writing %d bytes ...\n",
1380 g->msg_out_size - g->msg_out_pos);
1382 n = write (g->sock, g->msg_out + g->msg_out_pos,
1383 g->msg_out_size - g->msg_out_pos);
1385 if (errno != EAGAIN)
1386 perrorf (g, "write");
1391 fprintf (stderr, "sock_write_event: wrote %d bytes\n", n);
1393 g->msg_out_pos += n;
1395 /* More to write? */
1396 if (g->msg_out_pos < g->msg_out_size)
1400 fprintf (stderr, "sock_write_event: done writing, calling send_cb\n");
1404 g->msg_out_pos = g->msg_out_size = 0;
1406 /* Done writing, call the higher layer. */
1408 g->send_cb (g, g->send_cb_data);
1412 guestfs_set_send_callback (guestfs_h *g,
1413 guestfs_send_cb cb, void *opaque)
1416 g->send_cb_data = opaque;
1420 guestfs_set_reply_callback (guestfs_h *g,
1421 guestfs_reply_cb cb, void *opaque)
1424 g->reply_cb_data = opaque;
1428 guestfs_set_log_message_callback (guestfs_h *g,
1429 guestfs_log_message_cb cb, void *opaque)
1431 g->log_message_cb = cb;
1432 g->log_message_cb_data = opaque;
1436 guestfs_set_subprocess_quit_callback (guestfs_h *g,
1437 guestfs_subprocess_quit_cb cb, void *opaque)
1439 g->subprocess_quit_cb = cb;
1440 g->subprocess_quit_cb_data = opaque;
1444 guestfs_set_launch_done_callback (guestfs_h *g,
1445 guestfs_launch_done_cb cb, void *opaque)
1447 g->launch_done_cb = cb;
1448 g->launch_done_cb_data = opaque;
1451 /* Access to the handle's main loop and the default main loop. */
1453 guestfs_set_main_loop (guestfs_h *g, guestfs_main_loop *main_loop)
1455 g->main_loop = main_loop;
1459 guestfs_get_main_loop (guestfs_h *g)
1461 return g->main_loop;
1465 guestfs_get_default_main_loop (void)
1467 return (guestfs_main_loop *) &default_main_loop;
1470 /* Change the daemon socket handler so that we are now writing.
1471 * This sets the handle to sock_write_event.
1474 guestfs__switch_to_sending (guestfs_h *g)
1476 if (g->sock_watch >= 0) {
1477 if (g->main_loop->remove_handle (g->main_loop, g, g->sock_watch) == -1) {
1478 error (g, "remove_handle failed");
1485 g->main_loop->add_handle (g->main_loop, g, g->sock,
1486 GUESTFS_HANDLE_WRITABLE,
1487 sock_write_event, NULL);
1488 if (g->sock_watch == -1) {
1489 error (g, "add_handle failed");
1497 guestfs__switch_to_receiving (guestfs_h *g)
1499 if (g->sock_watch >= 0) {
1500 if (g->main_loop->remove_handle (g->main_loop, g, g->sock_watch) == -1) {
1501 error (g, "remove_handle failed");
1508 g->main_loop->add_handle (g->main_loop, g, g->sock,
1509 GUESTFS_HANDLE_READABLE,
1510 sock_read_event, NULL);
1511 if (g->sock_watch == -1) {
1512 error (g, "add_handle failed");
1519 /* Dispatch a call (len + header + args) to the remote daemon,
1520 * synchronously (ie. using the guest's main loop to wait until
1521 * it has been sent). Returns -1 for error, or the serial
1522 * number of the message.
1525 send_cb (guestfs_h *g, void *data)
1527 guestfs_main_loop *ml = guestfs_get_main_loop (g);
1530 ml->main_loop_quit (ml, g);
1534 guestfs__send_sync (guestfs_h *g, int proc_nr,
1535 xdrproc_t xdrp, char *args)
1537 struct guestfs_message_header hdr;
1540 int serial = g->msg_next_serial++;
1542 guestfs_main_loop *ml = guestfs_get_main_loop (g);
1544 if (g->state != BUSY) {
1545 error (g, "guestfs__send_sync: state %d != BUSY", g->state);
1549 /* This is probably an internal error. Or perhaps we should just
1550 * free the buffer anyway?
1552 if (g->msg_out != NULL) {
1553 error (g, "guestfs__send_sync: msg_out should be NULL");
1557 /* We have to allocate this message buffer on the heap because
1558 * it is quite large (although will be mostly unused). We
1559 * can't allocate it on the stack because in some environments
1560 * we have quite limited stack space available, notably when
1561 * running in the JVM.
1563 g->msg_out = safe_malloc (g, GUESTFS_MESSAGE_MAX + 4);
1564 xdrmem_create (&xdr, g->msg_out + 4, GUESTFS_MESSAGE_MAX, XDR_ENCODE);
1566 /* Serialize the header. */
1567 hdr.prog = GUESTFS_PROGRAM;
1568 hdr.vers = GUESTFS_PROTOCOL_VERSION;
1570 hdr.direction = GUESTFS_DIRECTION_CALL;
1571 hdr.serial = serial;
1572 hdr.status = GUESTFS_STATUS_OK;
1574 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
1575 error (g, "xdr_guestfs_message_header failed");
1579 /* Serialize the args. If any, because some message types
1580 * have no parameters.
1583 if (!(*xdrp) (&xdr, args)) {
1584 error (g, "dispatch failed to marshal args");
1589 /* Get the actual length of the message, resize the buffer to match
1590 * the actual length, and write the length word at the beginning.
1592 len = xdr_getpos (&xdr);
1595 g->msg_out = safe_realloc (g, g->msg_out, len + 4);
1596 g->msg_out_size = len + 4;
1599 xdrmem_create (&xdr, g->msg_out, 4, XDR_ENCODE);
1600 xdr_uint32_t (&xdr, &len);
1602 if (guestfs__switch_to_sending (g) == -1)
1606 guestfs_set_send_callback (g, send_cb, &sent);
1607 if (ml->main_loop_run (ml, g) == -1)
1610 error (g, "send failed, see earlier error messages");
1619 g->msg_out_size = 0;
1623 static int cancel = 0; /* XXX Implement file cancellation. */
1624 static int send_file_chunk_sync (guestfs_h *g, int cancel, const char *buf, size_t len);
1625 static int send_file_data_sync (guestfs_h *g, const char *buf, size_t len);
1626 static int send_file_cancellation_sync (guestfs_h *g);
1627 static int send_file_complete_sync (guestfs_h *g);
1629 /* Synchronously send a file.
1633 * -2 daemon cancelled (we must read the error message)
1636 guestfs__send_file_sync (guestfs_h *g, const char *filename)
1638 char buf[GUESTFS_MAX_CHUNK_SIZE];
1641 fd = open (filename, O_RDONLY);
1643 perrorf (g, "open: %s", filename);
1644 send_file_cancellation_sync (g);
1645 /* Daemon sees cancellation and won't reply, so caller can
1651 /* Send file in chunked encoding. */
1652 while (!cancel && (r = read (fd, buf, sizeof buf)) > 0) {
1653 err = send_file_data_sync (g, buf, r);
1655 if (err == -2) /* daemon sent cancellation */
1656 send_file_cancellation_sync (g);
1661 if (cancel) { /* cancel from either end */
1662 send_file_cancellation_sync (g);
1667 perrorf (g, "read: %s", filename);
1668 send_file_cancellation_sync (g);
1672 /* End of file, but before we send that, we need to close
1673 * the file and check for errors.
1675 if (close (fd) == -1) {
1676 perrorf (g, "close: %s", filename);
1677 send_file_cancellation_sync (g);
1681 return send_file_complete_sync (g);
1684 /* Send a chunk of file data. */
1686 send_file_data_sync (guestfs_h *g, const char *buf, size_t len)
1688 return send_file_chunk_sync (g, 0, buf, len);
1691 /* Send a cancellation message. */
1693 send_file_cancellation_sync (guestfs_h *g)
1695 return send_file_chunk_sync (g, 1, NULL, 0);
1698 /* Send a file complete chunk. */
1700 send_file_complete_sync (guestfs_h *g)
1703 return send_file_chunk_sync (g, 0, buf, 0);
1706 /* Send a chunk, cancellation or end of file, synchronously (ie. wait
1709 static int check_for_daemon_cancellation (guestfs_h *g);
1712 send_file_chunk_sync (guestfs_h *g, int cancel, const char *buf, size_t buflen)
1716 guestfs_chunk chunk;
1718 guestfs_main_loop *ml = guestfs_get_main_loop (g);
1720 if (g->state != BUSY) {
1721 error (g, "send_file_chunk_sync: state %d != READY", g->state);
1725 /* This is probably an internal error. Or perhaps we should just
1726 * free the buffer anyway?
1728 if (g->msg_out != NULL) {
1729 error (g, "guestfs__send_sync: msg_out should be NULL");
1733 /* Did the daemon send a cancellation message? */
1734 if (check_for_daemon_cancellation (g)) {
1736 fprintf (stderr, "got daemon cancellation\n");
1740 /* Allocate the chunk buffer. Don't use the stack to avoid
1741 * excessive stack usage and unnecessary copies.
1743 g->msg_out = safe_malloc (g, GUESTFS_MAX_CHUNK_SIZE + 4 + 48);
1744 xdrmem_create (&xdr, g->msg_out + 4, GUESTFS_MAX_CHUNK_SIZE + 48, XDR_ENCODE);
1746 /* Serialize the chunk. */
1747 chunk.cancel = cancel;
1748 chunk.data.data_len = buflen;
1749 chunk.data.data_val = (char *) buf;
1751 if (!xdr_guestfs_chunk (&xdr, &chunk)) {
1752 error (g, "xdr_guestfs_chunk failed (buf = %p, buflen = %zu)",
1758 len = xdr_getpos (&xdr);
1761 /* Reduce the size of the outgoing message buffer to the real length. */
1762 g->msg_out = safe_realloc (g, g->msg_out, len + 4);
1763 g->msg_out_size = len + 4;
1766 xdrmem_create (&xdr, g->msg_out, 4, XDR_ENCODE);
1767 xdr_uint32_t (&xdr, &len);
1769 if (guestfs__switch_to_sending (g) == -1)
1773 guestfs_set_send_callback (g, send_cb, &sent);
1774 if (ml->main_loop_run (ml, g) == -1)
1777 error (g, "send file chunk failed, see earlier error messages");
1786 g->msg_out_size = 0;
1790 /* At this point we are sending FileIn file(s) to the guest, and not
1791 * expecting to read anything, so if we do read anything, it must be
1792 * a cancellation message. This checks for this case without blocking.
1795 check_for_daemon_cancellation (guestfs_h *g)
1805 FD_SET (g->sock, &rset);
1808 r = select (g->sock+1, &rset, NULL, NULL, &tv);
1810 perrorf (g, "select");
1816 /* Read the message from the daemon. */
1817 r = xread (g->sock, buf, sizeof buf);
1819 perrorf (g, "read");
1823 xdrmem_create (&xdr, buf, sizeof buf, XDR_DECODE);
1824 xdr_uint32_t (&xdr, &flag);
1827 if (flag != GUESTFS_CANCEL_FLAG) {
1828 error (g, "check_for_daemon_cancellation: read 0x%x from daemon, expected 0x%x\n",
1829 flag, GUESTFS_CANCEL_FLAG);
1836 /* Synchronously receive a file. */
1838 static int receive_file_data_sync (guestfs_h *g, void **buf);
1841 guestfs__receive_file_sync (guestfs_h *g, const char *filename)
1846 fd = open (filename, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, 0666);
1848 perrorf (g, "open: %s", filename);
1852 /* Receive the file in chunked encoding. */
1853 while ((r = receive_file_data_sync (g, &buf)) > 0) {
1854 if (xwrite (fd, buf, r) == -1) {
1855 perrorf (g, "%s: write", filename);
1863 error (g, "%s: error in chunked encoding", filename);
1867 if (close (fd) == -1) {
1868 perrorf (g, "close: %s", filename);
1875 /* Send cancellation message to daemon, then wait until it
1876 * cancels (just throwing away data).
1880 uint32_t flag = GUESTFS_CANCEL_FLAG;
1882 xdrmem_create (&xdr, fbuf, sizeof fbuf, XDR_ENCODE);
1883 xdr_uint32_t (&xdr, &flag);
1886 if (xwrite (g->sock, fbuf, sizeof fbuf) == -1) {
1887 perrorf (g, "write to daemon socket");
1891 while ((r = receive_file_data_sync (g, &buf)) > 0)
1892 free (buf); /* just discard it */
1897 struct receive_file_ctx {
1903 receive_file_cb (guestfs_h *g, void *data, XDR *xdr)
1905 guestfs_main_loop *ml = guestfs_get_main_loop (g);
1906 struct receive_file_ctx *ctx = (struct receive_file_ctx *) data;
1907 guestfs_chunk chunk;
1909 ml->main_loop_quit (ml, g);
1911 memset (&chunk, 0, sizeof chunk);
1913 if (!xdr_guestfs_chunk (xdr, &chunk)) {
1914 error (g, "failed to parse file chunk");
1919 error (g, "file receive cancelled by daemon");
1923 if (chunk.data.data_len == 0) { /* end of transfer */
1928 ctx->code = chunk.data.data_len;
1929 *ctx->buf = chunk.data.data_val; /* caller frees */
1932 /* Receive a chunk of file data. */
1934 receive_file_data_sync (guestfs_h *g, void **buf)
1936 struct receive_file_ctx ctx;
1937 guestfs_main_loop *ml = guestfs_get_main_loop (g);
1942 guestfs_set_reply_callback (g, receive_file_cb, &ctx);
1943 (void) ml->main_loop_run (ml, g);
1944 guestfs_set_reply_callback (g, NULL, NULL);
1947 fprintf (stderr, "receive_file_data_sync: code %d\n", ctx.code);
1950 case 0: /* end of file */
1955 error (g, "failed to call receive_file_cb");
1957 default: /* received n bytes of data */
1962 /* This is the default main loop implementation, using select(2). */
1965 select_add_handle (guestfs_main_loop *mlv, guestfs_h *g, int fd, int events,
1966 guestfs_handle_event_cb cb, void *data)
1968 struct select_main_loop *ml = (struct select_main_loop *) mlv;
1970 if (fd < 0 || fd >= FD_SETSIZE) {
1971 error (g, "fd %d is out of range", fd);
1975 if ((events & ~(GUESTFS_HANDLE_READABLE |
1976 GUESTFS_HANDLE_WRITABLE |
1977 GUESTFS_HANDLE_HANGUP |
1978 GUESTFS_HANDLE_ERROR)) != 0) {
1979 error (g, "set of events (0x%x) contains unknown events", events);
1984 error (g, "set of events is empty");
1988 if (FD_ISSET (fd, &ml->rset) ||
1989 FD_ISSET (fd, &ml->wset) ||
1990 FD_ISSET (fd, &ml->xset)) {
1991 error (g, "fd %d is already registered", fd);
1996 error (g, "callback is NULL");
2000 if ((events & GUESTFS_HANDLE_READABLE))
2001 FD_SET (fd, &ml->rset);
2002 if ((events & GUESTFS_HANDLE_WRITABLE))
2003 FD_SET (fd, &ml->wset);
2004 if ((events & GUESTFS_HANDLE_HANGUP) || (events & GUESTFS_HANDLE_ERROR))
2005 FD_SET (fd, &ml->xset);
2007 if (fd > ml->max_fd) {
2009 ml->handle_cb_data =
2010 safe_realloc (g, ml->handle_cb_data,
2011 sizeof (struct select_handle_cb_data) * (ml->max_fd+1));
2013 ml->handle_cb_data[fd].cb = cb;
2014 ml->handle_cb_data[fd].g = g;
2015 ml->handle_cb_data[fd].data = data;
2019 /* Any integer >= 0 can be the handle, and this is as good as any ... */
2024 select_remove_handle (guestfs_main_loop *mlv, guestfs_h *g, int fd)
2026 struct select_main_loop *ml = (struct select_main_loop *) mlv;
2028 if (fd < 0 || fd >= FD_SETSIZE) {
2029 error (g, "fd %d is out of range", fd);
2033 if (!FD_ISSET (fd, &ml->rset) &&
2034 !FD_ISSET (fd, &ml->wset) &&
2035 !FD_ISSET (fd, &ml->xset)) {
2036 error (g, "fd %d was not registered", fd);
2040 FD_CLR (fd, &ml->rset);
2041 FD_CLR (fd, &ml->wset);
2042 FD_CLR (fd, &ml->xset);
2044 if (fd == ml->max_fd) {
2046 ml->handle_cb_data =
2047 safe_realloc (g, ml->handle_cb_data,
2048 sizeof (struct select_handle_cb_data) * (ml->max_fd+1));
2057 select_add_timeout (guestfs_main_loop *mlv, guestfs_h *g, int interval,
2058 guestfs_handle_timeout_cb cb, void *data)
2060 //struct select_main_loop *ml = (struct select_main_loop *) mlv;
2062 abort (); /* XXX not implemented yet */
2066 select_remove_timeout (guestfs_main_loop *mlv, guestfs_h *g, int timer)
2068 //struct select_main_loop *ml = (struct select_main_loop *) mlv;
2070 abort (); /* XXX not implemented yet */
2073 /* The 'g' parameter is just used for error reporting. Events
2074 * for multiple handles can be dispatched by running the main
2078 select_main_loop_run (guestfs_main_loop *mlv, guestfs_h *g)
2080 struct select_main_loop *ml = (struct select_main_loop *) mlv;
2082 fd_set rset2, wset2, xset2;
2084 if (ml->is_running) {
2085 error (g, "select_main_loop_run: this cannot be called recursively");
2091 while (ml->is_running) {
2092 if (ml->nr_fds == 0)
2098 r = select (ml->max_fd+1, &rset2, &wset2, &xset2, NULL);
2100 perrorf (g, "select");
2105 for (fd = 0; r > 0 && fd <= ml->max_fd; ++fd) {
2107 if (FD_ISSET (fd, &rset2))
2108 events |= GUESTFS_HANDLE_READABLE;
2109 if (FD_ISSET (fd, &wset2))
2110 events |= GUESTFS_HANDLE_WRITABLE;
2111 if (FD_ISSET (fd, &xset2))
2112 events |= GUESTFS_HANDLE_ERROR | GUESTFS_HANDLE_HANGUP;
2115 ml->handle_cb_data[fd].cb ((guestfs_main_loop *) ml,
2116 ml->handle_cb_data[fd].g,
2117 ml->handle_cb_data[fd].data,
2128 select_main_loop_quit (guestfs_main_loop *mlv, guestfs_h *g)
2130 struct select_main_loop *ml = (struct select_main_loop *) mlv;
2132 /* Note that legitimately ml->is_running can be zero when
2133 * this function is called.