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) {
271 guestfs_umount_all (g);
275 /* Remove any handlers that might be called back before we kill the
278 g->log_message_cb = NULL;
280 if (g->state != CONFIG)
281 guestfs_kill_subprocess (g);
284 snprintf (filename, sizeof filename, "%s/sock", g->tmpdir);
293 for (i = 0; i < g->cmdline_size; ++i)
294 free (g->cmdline[i]);
298 /* Mark the handle as dead before freeing it. */
299 g->state = NO_HANDLE;
301 /* acquire mutex (XXX) */
305 for (gg = handles; gg->next != g; gg = gg->next)
309 /* release mutex (XXX) */
311 free (g->last_error);
315 /* Close all open handles (called from atexit(3)). */
319 while (handles) guestfs_close (handles);
323 guestfs_last_error (guestfs_h *g)
325 return g->last_error;
329 set_last_error (guestfs_h *g, const char *msg)
331 free (g->last_error);
332 g->last_error = strdup (msg);
336 default_error_cb (guestfs_h *g, void *data, const char *msg)
338 fprintf (stderr, "libguestfs: error: %s\n", msg);
342 guestfs_error (guestfs_h *g, const char *fs, ...)
348 vasprintf (&msg, fs, args);
351 if (g->error_cb) g->error_cb (g, g->error_cb_data, msg);
352 set_last_error (g, msg);
358 guestfs_perrorf (guestfs_h *g, const char *fs, ...)
365 vasprintf (&msg, fs, args);
370 strerror_r (err, buf, sizeof buf);
374 buf = strerror_r (err, _buf, sizeof _buf);
377 msg = safe_realloc (g, msg, strlen (msg) + 2 + strlen (buf) + 1);
381 if (g->error_cb) g->error_cb (g, g->error_cb_data, msg);
382 set_last_error (g, msg);
388 guestfs_safe_malloc (guestfs_h *g, size_t nbytes)
390 void *ptr = malloc (nbytes);
391 if (!ptr) g->abort_cb ();
396 guestfs_safe_realloc (guestfs_h *g, void *ptr, int nbytes)
398 void *p = realloc (ptr, nbytes);
399 if (!p) g->abort_cb ();
404 guestfs_safe_strdup (guestfs_h *g, const char *str)
406 char *s = strdup (str);
407 if (!s) g->abort_cb ();
412 guestfs_safe_memdup (guestfs_h *g, void *ptr, size_t size)
414 void *p = malloc (size);
415 if (!p) g->abort_cb ();
416 memcpy (p, ptr, size);
421 xwrite (int fd, const void *buf, size_t len)
426 r = write (fd, buf, len);
438 xread (int fd, void *buf, size_t len)
443 r = read (fd, buf, len);
445 if (errno == EINTR || errno == EAGAIN)
458 guestfs_set_out_of_memory_handler (guestfs_h *g, guestfs_abort_cb cb)
464 guestfs_get_out_of_memory_handler (guestfs_h *g)
470 guestfs_set_error_handler (guestfs_h *g, guestfs_error_handler_cb cb, void *data)
473 g->error_cb_data = data;
476 guestfs_error_handler_cb
477 guestfs_get_error_handler (guestfs_h *g, void **data_rtn)
479 if (data_rtn) *data_rtn = g->error_cb_data;
484 guestfs_set_verbose (guestfs_h *g, int v)
491 guestfs_get_verbose (guestfs_h *g)
497 guestfs_set_autosync (guestfs_h *g, int a)
504 guestfs_get_autosync (guestfs_h *g)
510 guestfs_set_path (guestfs_h *g, const char *path)
513 g->path = GUESTFS_DEFAULT_PATH;
520 guestfs_get_path (guestfs_h *g)
526 guestfs_set_qemu (guestfs_h *g, const char *qemu)
536 guestfs_get_qemu (guestfs_h *g)
541 /* Add a string to the current command line. */
543 incr_cmdline_size (guestfs_h *g)
545 if (g->cmdline == NULL) {
546 /* g->cmdline[0] is reserved for argv[0], set in guestfs_launch. */
548 g->cmdline = safe_malloc (g, sizeof (char *));
549 g->cmdline[0] = NULL;
553 g->cmdline = safe_realloc (g, g->cmdline, sizeof (char *) * g->cmdline_size);
557 add_cmdline (guestfs_h *g, const char *str)
559 if (g->state != CONFIG) {
560 error (g, "command line cannot be altered after qemu subprocess launched");
564 incr_cmdline_size (g);
565 g->cmdline[g->cmdline_size-1] = safe_strdup (g, str);
570 guestfs_config (guestfs_h *g,
571 const char *qemu_param, const char *qemu_value)
573 if (qemu_param[0] != '-') {
574 error (g, "guestfs_config: parameter must begin with '-' character");
578 /* A bit fascist, but the user will probably break the extra
579 * parameters that we add if they try to set any of these.
581 if (strcmp (qemu_param, "-kernel") == 0 ||
582 strcmp (qemu_param, "-initrd") == 0 ||
583 strcmp (qemu_param, "-nographic") == 0 ||
584 strcmp (qemu_param, "-serial") == 0 ||
585 strcmp (qemu_param, "-vnc") == 0 ||
586 strcmp (qemu_param, "-full-screen") == 0 ||
587 strcmp (qemu_param, "-std-vga") == 0 ||
588 strcmp (qemu_param, "-vnc") == 0) {
589 error (g, "guestfs_config: parameter '%s' isn't allowed", qemu_param);
593 if (add_cmdline (g, qemu_param) != 0) return -1;
595 if (qemu_value != NULL) {
596 if (add_cmdline (g, qemu_value) != 0) return -1;
603 guestfs_add_drive (guestfs_h *g, const char *filename)
605 size_t len = strlen (filename) + 64;
608 if (strchr (filename, ',') != NULL) {
609 error (g, "filename cannot contain ',' (comma) character");
613 if (access (filename, F_OK) == -1) {
614 perrorf (g, "%s", filename);
618 snprintf (buf, len, "file=%s", filename);
620 return guestfs_config (g, "-drive", buf);
624 guestfs_add_cdrom (guestfs_h *g, const char *filename)
626 if (strchr (filename, ',') != NULL) {
627 error (g, "filename cannot contain ',' (comma) character");
631 if (access (filename, F_OK) == -1) {
632 perrorf (g, "%s", filename);
636 return guestfs_config (g, "-cdrom", filename);
640 guestfs_launch (guestfs_h *g)
642 static const char *dir_template = "/tmp/libguestfsXXXXXX";
643 int r, i, pmore, memsize;
647 const char *kernel_name = "vmlinuz." REPO "." host_cpu;
648 const char *initrd_name = "initramfs." REPO "." host_cpu ".img";
649 char *path, *pelem, *pend;
650 char *kernel = NULL, *initrd = NULL;
652 struct sockaddr_un addr;
657 error (g, "you must call guestfs_add_drive before guestfs_launch");
661 if (g->state != CONFIG) {
662 error (g, "qemu has already been launched");
666 /* Search g->path for the kernel and initrd. */
667 pelem = path = safe_strdup (g, g->path);
669 pend = strchrnul (pelem, ':');
670 pmore = *pend == ':';
674 /* Empty element or "." means cwd. */
675 if (len == 0 || (len == 1 && *pelem == '.')) {
678 "looking for kernel and initrd in current directory\n");
679 if (access (kernel_name, F_OK) == 0 && access (initrd_name, F_OK) == 0) {
680 kernel = safe_strdup (g, kernel_name);
681 initrd = safe_strdup (g, initrd_name);
685 /* Look at <path>/kernel etc. */
687 kernel = safe_malloc (g, len + strlen (kernel_name) + 2);
688 initrd = safe_malloc (g, len + strlen (initrd_name) + 2);
689 sprintf (kernel, "%s/%s", pelem, kernel_name);
690 sprintf (initrd, "%s/%s", pelem, initrd_name);
693 fprintf (stderr, "looking for %s and %s\n", kernel, initrd);
695 if (access (kernel, F_OK) == 0 && access (initrd, F_OK) == 0)
699 kernel = initrd = NULL;
707 if (kernel == NULL || initrd == NULL) {
708 error (g, "cannot find %s or %s on LIBGUESTFS_PATH (current path = %s)",
709 kernel_name, initrd_name, g->path);
713 /* Choose a suitable memory size (in MB). This is more art
714 * than science, but you can help by doing
715 * ./configure --enable-debug-command
719 * and seeing how much free memory is left for particular
722 * It's also helpful to report both the compressed and uncompressed
723 * size of the initramfs (ls -lh initramfs*.img; du -sh initramfs).
725 * XXX KVM virtio balloon driver?
727 if (stat (initrd, &statbuf) != -1) {
728 /* Approximate size of the initramfs after it is decompressed
729 * in kernel memory. The compression factor is ~2.5-3.
731 memsize = 3 * statbuf.st_size / 1024 / 1024;
733 /* Approximate size used by the kernel. */
736 /* Want to give userspace some room, so: */
740 /* On 64 bit, assume some overhead. */
747 /* Make the temporary directory containing the socket. */
749 g->tmpdir = safe_strdup (g, dir_template);
750 if (mkdtemp (g->tmpdir) == NULL) {
751 perrorf (g, "%s: cannot create temporary directory", dir_template);
756 snprintf (unixsock, sizeof unixsock, "%s/sock", g->tmpdir);
759 if (pipe (wfd) == -1 || pipe (rfd) == -1) {
774 if (r == 0) { /* Child (qemu). */
777 char memsize_str[256];
779 /* Set up the full command line. Do this in the subprocess so we
780 * don't need to worry about cleaning up.
782 g->cmdline[0] = (char *) g->qemu;
784 /* Construct the -net channel parameter for qemu. */
785 snprintf (vmchannel, sizeof vmchannel,
786 "channel,%d:unix:%s,server,nowait",
787 VMCHANNEL_PORT, unixsock);
789 /* Linux kernel command line. */
790 snprintf (append, sizeof append,
791 "console=ttyS0 guestfs=%s:%d%s",
792 VMCHANNEL_ADDR, VMCHANNEL_PORT,
793 g->verbose ? " guestfs_verbose=1" : "");
795 snprintf (memsize_str, sizeof memsize_str, "%d", memsize);
797 add_cmdline (g, "-m");
798 add_cmdline (g, memsize_str);
800 add_cmdline (g, "-no-kqemu"); /* Avoids a warning. */
802 add_cmdline (g, "-kernel");
803 add_cmdline (g, (char *) kernel);
804 add_cmdline (g, "-initrd");
805 add_cmdline (g, (char *) initrd);
806 add_cmdline (g, "-append");
807 add_cmdline (g, append);
808 add_cmdline (g, "-nographic");
809 add_cmdline (g, "-serial");
810 add_cmdline (g, "stdio");
811 add_cmdline (g, "-net");
812 add_cmdline (g, vmchannel);
813 add_cmdline (g, "-net");
814 add_cmdline (g, "user,vlan=0");
815 add_cmdline (g, "-net");
816 add_cmdline (g, "nic,model=virtio,vlan=0");
817 incr_cmdline_size (g);
818 g->cmdline[g->cmdline_size-1] = NULL;
821 fprintf (stderr, "%s", g->qemu);
822 for (i = 0; g->cmdline[i]; ++i)
823 fprintf (stderr, " %s", g->cmdline[i]);
824 fprintf (stderr, "\n");
827 /* Set up stdin, stdout. */
838 /* Set up a new process group, so we can signal this process
839 * and all subprocesses (eg. if qemu is really a shell script).
844 execv (g->qemu, g->cmdline); /* Run qemu. */
849 /* Parent (library). */
852 /* Fork the recovery process off which will kill qemu if the parent
853 * process fails to do so (eg. if the parent segfaults).
857 pid_t qemu_pid = g->pid;
858 pid_t parent_pid = getppid ();
860 /* Writing to argv is hideously complicated and error prone. See:
861 * http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/backend/utils/misc/ps_status.c?rev=1.33.2.1;content-type=text%2Fplain
864 /* Loop around waiting for one or both of the other processes to
865 * disappear. It's fair to say this is very hairy. The PIDs that
866 * we are looking at might be reused by another process. We are
867 * effectively polling. Is the cure worse than the disease?
870 if (kill (qemu_pid, 0) == -1) /* qemu's gone away, we aren't needed */
872 if (kill (parent_pid, 0) == -1) {
873 /* Parent's gone away, qemu still around, so kill qemu. */
881 /* Don't worry, if the fork failed, this will be -1. The recovery
882 * process isn't essential.
886 /* Start the clock ... */
889 /* Close the other ends of the pipe. */
893 if (fcntl (wfd[1], F_SETFL, O_NONBLOCK) == -1 ||
894 fcntl (rfd[0], F_SETFL, O_NONBLOCK) == -1) {
895 perrorf (g, "fcntl");
899 g->fd[0] = wfd[1]; /* stdin of child */
900 g->fd[1] = rfd[0]; /* stdout of child */
902 /* Open the Unix socket. The vmchannel implementation that got
903 * merged with qemu sucks in a number of ways. Both ends do
904 * connect(2), which means that no one knows what, if anything, is
905 * connected to the other end, or if it becomes disconnected. Even
906 * worse, we have to wait some indeterminate time for qemu to create
907 * the socket and connect to it (which happens very early in qemu's
908 * start-up), so any code that uses vmchannel is inherently racy.
909 * Hence this silly loop.
911 g->sock = socket (AF_UNIX, SOCK_STREAM, 0);
913 perrorf (g, "socket");
917 if (fcntl (g->sock, F_SETFL, O_NONBLOCK) == -1) {
918 perrorf (g, "fcntl");
922 addr.sun_family = AF_UNIX;
923 strncpy (addr.sun_path, unixsock, UNIX_PATH_MAX);
924 addr.sun_path[UNIX_PATH_MAX-1] = '\0';
928 /* Always sleep at least once to give qemu a small chance to start up. */
931 r = connect (g->sock, (struct sockaddr *) &addr, sizeof addr);
932 if ((r == -1 && errno == EINPROGRESS) || r == 0)
936 perrorf (g, "connect");
940 error (g, "failed to connect to vmchannel socket");
944 /* Watch the file descriptors. */
947 g->msg_in_size = g->msg_in_allocated = 0;
955 g->main_loop->add_handle (g->main_loop, g, g->fd[1],
956 GUESTFS_HANDLE_READABLE,
958 if (g->stdout_watch == -1) {
959 error (g, "could not watch qemu stdout");
963 if (guestfs__switch_to_receiving (g) == -1)
966 g->state = LAUNCHING;
970 if (g->stdout_watch >= 0)
971 g->main_loop->remove_handle (g->main_loop, g, g->stdout_watch);
972 if (g->sock_watch >= 0)
973 g->main_loop->remove_handle (g->main_loop, g, g->sock_watch);
982 if (g->recoverypid > 0) kill (g->recoverypid, 9);
983 waitpid (g->pid, NULL, 0);
984 if (g->recoverypid > 0) waitpid (g->recoverypid, NULL, 0);
991 g->stdout_watch = -1;
1001 finish_wait_ready (guestfs_h *g, void *vp)
1004 fprintf (stderr, "finish_wait_ready called, %p, vp = %p\n", g, vp);
1007 g->main_loop->main_loop_quit (g->main_loop, g);
1011 guestfs_wait_ready (guestfs_h *g)
1013 int finished = 0, r;
1015 if (g->state == READY) return 0;
1017 if (g->state == BUSY) {
1018 error (g, "qemu has finished launching already");
1022 if (g->state != LAUNCHING) {
1023 error (g, "qemu has not been launched yet");
1027 g->launch_done_cb = finish_wait_ready;
1028 g->launch_done_cb_data = &finished;
1029 r = g->main_loop->main_loop_run (g->main_loop, g);
1030 g->launch_done_cb = NULL;
1031 g->launch_done_cb_data = NULL;
1033 if (r == -1) return -1;
1035 if (finished != 1) {
1036 error (g, "guestfs_wait_ready failed, see earlier error messages");
1040 /* This is possible in some really strange situations, such as
1041 * guestfsd starts up OK but then qemu immediately exits. Check for
1042 * it because the caller is probably expecting to be able to send
1043 * commands after this function returns.
1045 if (g->state != READY) {
1046 error (g, "qemu launched and contacted daemon, but state != READY");
1054 guestfs_kill_subprocess (guestfs_h *g)
1056 if (g->state == CONFIG) {
1057 error (g, "no subprocess to kill");
1062 fprintf (stderr, "sending SIGTERM to process %d\n", g->pid);
1064 kill (g->pid, SIGTERM);
1065 if (g->recoverypid > 0) kill (g->recoverypid, 9);
1070 /* Access current state. */
1072 guestfs_is_config (guestfs_h *g)
1074 return g->state == CONFIG;
1078 guestfs_is_launching (guestfs_h *g)
1080 return g->state == LAUNCHING;
1084 guestfs_is_ready (guestfs_h *g)
1086 return g->state == READY;
1090 guestfs_is_busy (guestfs_h *g)
1092 return g->state == BUSY;
1096 guestfs_get_state (guestfs_h *g)
1102 guestfs_set_ready (guestfs_h *g)
1104 if (g->state != BUSY) {
1105 error (g, "guestfs_set_ready: called when in state %d != BUSY", g->state);
1113 guestfs_set_busy (guestfs_h *g)
1115 if (g->state != READY) {
1116 error (g, "guestfs_set_busy: called when in state %d != READY", g->state);
1123 /* Structure-freeing functions. These rely on the fact that the
1124 * structure format is identical to the XDR format. See note in
1128 guestfs_free_int_bool (struct guestfs_int_bool *x)
1134 guestfs_free_lvm_pv_list (struct guestfs_lvm_pv_list *x)
1136 xdr_free ((xdrproc_t) xdr_guestfs_lvm_int_pv_list, (char *) x);
1141 guestfs_free_lvm_vg_list (struct guestfs_lvm_vg_list *x)
1143 xdr_free ((xdrproc_t) xdr_guestfs_lvm_int_vg_list, (char *) x);
1148 guestfs_free_lvm_lv_list (struct guestfs_lvm_lv_list *x)
1150 xdr_free ((xdrproc_t) xdr_guestfs_lvm_int_lv_list, (char *) x);
1154 /* This function is called whenever qemu prints something on stdout.
1155 * Qemu's stdout is also connected to the guest's serial console, so
1156 * we see kernel messages here too.
1159 stdout_event (struct guestfs_main_loop *ml, guestfs_h *g, void *data,
1160 int watch, int fd, int events)
1168 "stdout_event: %p g->state = %d, fd = %d, events = 0x%x\n",
1169 g, g->state, fd, events);
1172 if (g->fd[1] != fd) {
1173 error (g, "stdout_event: internal error: %d != %d", g->fd[1], fd);
1177 n = read (fd, buf, sizeof buf);
1179 /* Hopefully this indicates the qemu child process has died. */
1181 fprintf (stderr, "stdout_event: %p: child process died\n", g);
1182 /*kill (g->pid, SIGTERM);*/
1183 if (g->recoverypid > 0) kill (g->recoverypid, 9);
1184 waitpid (g->pid, NULL, 0);
1185 if (g->recoverypid > 0) waitpid (g->recoverypid, NULL, 0);
1186 if (g->stdout_watch >= 0)
1187 g->main_loop->remove_handle (g->main_loop, g, g->stdout_watch);
1188 if (g->sock_watch >= 0)
1189 g->main_loop->remove_handle (g->main_loop, g, g->sock_watch);
1199 g->stdout_watch = -1;
1202 if (g->subprocess_quit_cb)
1203 g->subprocess_quit_cb (g, g->subprocess_quit_cb_data);
1208 if (errno != EINTR && errno != EAGAIN)
1209 perrorf (g, "read");
1213 /* In verbose mode, copy all log messages to stderr. */
1217 /* It's an actual log message, send it upwards if anyone is listening. */
1218 if (g->log_message_cb)
1219 g->log_message_cb (g, g->log_message_cb_data, buf, n);
1222 /* The function is called whenever we can read something on the
1223 * guestfsd (daemon inside the guest) communication socket.
1226 sock_read_event (struct guestfs_main_loop *ml, guestfs_h *g, void *data,
1227 int watch, int fd, int events)
1235 "sock_read_event: %p g->state = %d, fd = %d, events = 0x%x\n",
1236 g, g->state, fd, events);
1238 if (g->sock != fd) {
1239 error (g, "sock_read_event: internal error: %d != %d", g->sock, fd);
1243 if (g->msg_in_size <= g->msg_in_allocated) {
1244 g->msg_in_allocated += 4096;
1245 g->msg_in = safe_realloc (g, g->msg_in, g->msg_in_allocated);
1247 n = read (g->sock, g->msg_in + g->msg_in_size,
1248 g->msg_in_allocated - g->msg_in_size);
1250 /* Disconnected? Ignore it because stdout_watch will get called
1251 * and will do the cleanup.
1256 if (errno != EINTR && errno != EAGAIN)
1257 perrorf (g, "read");
1261 g->msg_in_size += n;
1263 /* Have we got enough of a message to be able to process it yet? */
1265 if (g->msg_in_size < 4) return;
1267 xdrmem_create (&xdr, g->msg_in, g->msg_in_size, XDR_DECODE);
1268 if (!xdr_uint32_t (&xdr, &len)) {
1269 error (g, "can't decode length word");
1273 /* Length is normally the length of the message, but when guestfsd
1274 * starts up it sends a "magic" value (longer than any possible
1275 * message). Check for this.
1277 if (len == GUESTFS_LAUNCH_FLAG) {
1278 if (g->state != LAUNCHING)
1279 error (g, "received magic signature from guestfsd, but in state %d",
1281 else if (g->msg_in_size != 4)
1282 error (g, "received magic signature from guestfsd, but msg size is %d",
1286 if (g->launch_done_cb)
1287 g->launch_done_cb (g, g->launch_done_cb_data);
1293 /* This can happen if a cancellation happens right at the end
1294 * of us sending a FileIn parameter to the daemon. Discard. The
1295 * daemon should send us an error message next.
1297 if (len == GUESTFS_CANCEL_FLAG) {
1298 g->msg_in_size -= 4;
1299 memmove (g->msg_in, g->msg_in+4, g->msg_in_size);
1303 /* If this happens, it's pretty bad and we've probably lost
1306 if (len > GUESTFS_MESSAGE_MAX) {
1307 error (g, "message length (%u) > maximum possible size (%d)",
1308 len, GUESTFS_MESSAGE_MAX);
1312 if (g->msg_in_size-4 < len) return; /* Need more of this message. */
1314 /* Got the full message, begin processing it. */
1319 for (i = 0; i < g->msg_in_size; i += 16) {
1320 printf ("%04x: ", i);
1321 for (j = i; j < MIN (i+16, g->msg_in_size); ++j)
1322 printf ("%02x ", (unsigned char) g->msg_in[j]);
1323 for (; j < i+16; ++j)
1326 for (j = i; j < MIN (i+16, g->msg_in_size); ++j)
1327 if (isprint (g->msg_in[j]))
1328 printf ("%c", g->msg_in[j]);
1331 for (; j < i+16; ++j)
1338 /* Not in the expected state. */
1339 if (g->state != BUSY)
1340 error (g, "state %d != BUSY", g->state);
1342 /* Push the message up to the higher layer. */
1344 g->reply_cb (g, g->reply_cb_data, &xdr);
1346 /* This message (probably) should never be printed. */
1347 fprintf (stderr, "libguesfs: sock_read_event: !!! dropped message !!!\n");
1349 g->msg_in_size -= len + 4;
1350 memmove (g->msg_in, g->msg_in+len+4, g->msg_in_size);
1351 if (g->msg_in_size > 0) goto again;
1354 /* Free the message buffer if it's grown excessively large. */
1355 if (g->msg_in_allocated > 65536) {
1358 g->msg_in_size = g->msg_in_allocated = 0;
1365 /* The function is called whenever we can write something on the
1366 * guestfsd (daemon inside the guest) communication socket.
1369 sock_write_event (struct guestfs_main_loop *ml, guestfs_h *g, void *data,
1370 int watch, int fd, int events)
1376 "sock_write_event: %p g->state = %d, fd = %d, events = 0x%x\n",
1377 g, g->state, fd, events);
1379 if (g->sock != fd) {
1380 error (g, "sock_write_event: internal error: %d != %d", g->sock, fd);
1384 if (g->state != BUSY) {
1385 error (g, "sock_write_event: state %d != BUSY", g->state);
1390 fprintf (stderr, "sock_write_event: writing %d bytes ...\n",
1391 g->msg_out_size - g->msg_out_pos);
1393 n = write (g->sock, g->msg_out + g->msg_out_pos,
1394 g->msg_out_size - g->msg_out_pos);
1396 if (errno != EAGAIN)
1397 perrorf (g, "write");
1402 fprintf (stderr, "sock_write_event: wrote %d bytes\n", n);
1404 g->msg_out_pos += n;
1406 /* More to write? */
1407 if (g->msg_out_pos < g->msg_out_size)
1411 fprintf (stderr, "sock_write_event: done writing, calling send_cb\n");
1415 g->msg_out_pos = g->msg_out_size = 0;
1417 /* Done writing, call the higher layer. */
1419 g->send_cb (g, g->send_cb_data);
1423 guestfs_set_send_callback (guestfs_h *g,
1424 guestfs_send_cb cb, void *opaque)
1427 g->send_cb_data = opaque;
1431 guestfs_set_reply_callback (guestfs_h *g,
1432 guestfs_reply_cb cb, void *opaque)
1435 g->reply_cb_data = opaque;
1439 guestfs_set_log_message_callback (guestfs_h *g,
1440 guestfs_log_message_cb cb, void *opaque)
1442 g->log_message_cb = cb;
1443 g->log_message_cb_data = opaque;
1447 guestfs_set_subprocess_quit_callback (guestfs_h *g,
1448 guestfs_subprocess_quit_cb cb, void *opaque)
1450 g->subprocess_quit_cb = cb;
1451 g->subprocess_quit_cb_data = opaque;
1455 guestfs_set_launch_done_callback (guestfs_h *g,
1456 guestfs_launch_done_cb cb, void *opaque)
1458 g->launch_done_cb = cb;
1459 g->launch_done_cb_data = opaque;
1462 /* Access to the handle's main loop and the default main loop. */
1464 guestfs_set_main_loop (guestfs_h *g, guestfs_main_loop *main_loop)
1466 g->main_loop = main_loop;
1470 guestfs_get_main_loop (guestfs_h *g)
1472 return g->main_loop;
1476 guestfs_get_default_main_loop (void)
1478 return (guestfs_main_loop *) &default_main_loop;
1481 /* Change the daemon socket handler so that we are now writing.
1482 * This sets the handle to sock_write_event.
1485 guestfs__switch_to_sending (guestfs_h *g)
1487 if (g->sock_watch >= 0) {
1488 if (g->main_loop->remove_handle (g->main_loop, g, g->sock_watch) == -1) {
1489 error (g, "remove_handle failed");
1496 g->main_loop->add_handle (g->main_loop, g, g->sock,
1497 GUESTFS_HANDLE_WRITABLE,
1498 sock_write_event, NULL);
1499 if (g->sock_watch == -1) {
1500 error (g, "add_handle failed");
1508 guestfs__switch_to_receiving (guestfs_h *g)
1510 if (g->sock_watch >= 0) {
1511 if (g->main_loop->remove_handle (g->main_loop, g, g->sock_watch) == -1) {
1512 error (g, "remove_handle failed");
1519 g->main_loop->add_handle (g->main_loop, g, g->sock,
1520 GUESTFS_HANDLE_READABLE,
1521 sock_read_event, NULL);
1522 if (g->sock_watch == -1) {
1523 error (g, "add_handle failed");
1530 /* Dispatch a call (len + header + args) to the remote daemon,
1531 * synchronously (ie. using the guest's main loop to wait until
1532 * it has been sent). Returns -1 for error, or the serial
1533 * number of the message.
1536 send_cb (guestfs_h *g, void *data)
1538 guestfs_main_loop *ml = guestfs_get_main_loop (g);
1541 ml->main_loop_quit (ml, g);
1545 guestfs__send_sync (guestfs_h *g, int proc_nr,
1546 xdrproc_t xdrp, char *args)
1548 struct guestfs_message_header hdr;
1551 int serial = g->msg_next_serial++;
1553 guestfs_main_loop *ml = guestfs_get_main_loop (g);
1555 if (g->state != BUSY) {
1556 error (g, "guestfs__send_sync: state %d != BUSY", g->state);
1560 /* This is probably an internal error. Or perhaps we should just
1561 * free the buffer anyway?
1563 if (g->msg_out != NULL) {
1564 error (g, "guestfs__send_sync: msg_out should be NULL");
1568 /* We have to allocate this message buffer on the heap because
1569 * it is quite large (although will be mostly unused). We
1570 * can't allocate it on the stack because in some environments
1571 * we have quite limited stack space available, notably when
1572 * running in the JVM.
1574 g->msg_out = safe_malloc (g, GUESTFS_MESSAGE_MAX + 4);
1575 xdrmem_create (&xdr, g->msg_out + 4, GUESTFS_MESSAGE_MAX, XDR_ENCODE);
1577 /* Serialize the header. */
1578 hdr.prog = GUESTFS_PROGRAM;
1579 hdr.vers = GUESTFS_PROTOCOL_VERSION;
1581 hdr.direction = GUESTFS_DIRECTION_CALL;
1582 hdr.serial = serial;
1583 hdr.status = GUESTFS_STATUS_OK;
1585 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
1586 error (g, "xdr_guestfs_message_header failed");
1590 /* Serialize the args. If any, because some message types
1591 * have no parameters.
1594 if (!(*xdrp) (&xdr, args)) {
1595 error (g, "dispatch failed to marshal args");
1600 /* Get the actual length of the message, resize the buffer to match
1601 * the actual length, and write the length word at the beginning.
1603 len = xdr_getpos (&xdr);
1606 g->msg_out = safe_realloc (g, g->msg_out, len + 4);
1607 g->msg_out_size = len + 4;
1610 xdrmem_create (&xdr, g->msg_out, 4, XDR_ENCODE);
1611 xdr_uint32_t (&xdr, &len);
1613 if (guestfs__switch_to_sending (g) == -1)
1617 guestfs_set_send_callback (g, send_cb, &sent);
1618 if (ml->main_loop_run (ml, g) == -1)
1621 error (g, "send failed, see earlier error messages");
1630 g->msg_out_size = 0;
1634 static int cancel = 0; /* XXX Implement file cancellation. */
1635 static int send_file_chunk_sync (guestfs_h *g, int cancel, const char *buf, size_t len);
1636 static int send_file_data_sync (guestfs_h *g, const char *buf, size_t len);
1637 static int send_file_cancellation_sync (guestfs_h *g);
1638 static int send_file_complete_sync (guestfs_h *g);
1640 /* Synchronously send a file.
1644 * -2 daemon cancelled (we must read the error message)
1647 guestfs__send_file_sync (guestfs_h *g, const char *filename)
1649 char buf[GUESTFS_MAX_CHUNK_SIZE];
1652 fd = open (filename, O_RDONLY);
1654 perrorf (g, "open: %s", filename);
1655 send_file_cancellation_sync (g);
1656 /* Daemon sees cancellation and won't reply, so caller can
1662 /* Send file in chunked encoding. */
1664 r = read (fd, buf, sizeof buf);
1665 if (r == -1 && (errno == EINTR || errno == EAGAIN))
1668 err = send_file_data_sync (g, buf, r);
1670 if (err == -2) /* daemon sent cancellation */
1671 send_file_cancellation_sync (g);
1676 if (cancel) { /* cancel from either end */
1677 send_file_cancellation_sync (g);
1682 perrorf (g, "read: %s", filename);
1683 send_file_cancellation_sync (g);
1687 /* End of file, but before we send that, we need to close
1688 * the file and check for errors.
1690 if (close (fd) == -1) {
1691 perrorf (g, "close: %s", filename);
1692 send_file_cancellation_sync (g);
1696 return send_file_complete_sync (g);
1699 /* Send a chunk of file data. */
1701 send_file_data_sync (guestfs_h *g, const char *buf, size_t len)
1703 return send_file_chunk_sync (g, 0, buf, len);
1706 /* Send a cancellation message. */
1708 send_file_cancellation_sync (guestfs_h *g)
1710 return send_file_chunk_sync (g, 1, NULL, 0);
1713 /* Send a file complete chunk. */
1715 send_file_complete_sync (guestfs_h *g)
1718 return send_file_chunk_sync (g, 0, buf, 0);
1721 /* Send a chunk, cancellation or end of file, synchronously (ie. wait
1724 static int check_for_daemon_cancellation (guestfs_h *g);
1727 send_file_chunk_sync (guestfs_h *g, int cancel, const char *buf, size_t buflen)
1731 guestfs_chunk chunk;
1733 guestfs_main_loop *ml = guestfs_get_main_loop (g);
1735 if (g->state != BUSY) {
1736 error (g, "send_file_chunk_sync: state %d != READY", g->state);
1740 /* This is probably an internal error. Or perhaps we should just
1741 * free the buffer anyway?
1743 if (g->msg_out != NULL) {
1744 error (g, "guestfs__send_sync: msg_out should be NULL");
1748 /* Did the daemon send a cancellation message? */
1749 if (check_for_daemon_cancellation (g)) {
1751 fprintf (stderr, "got daemon cancellation\n");
1755 /* Allocate the chunk buffer. Don't use the stack to avoid
1756 * excessive stack usage and unnecessary copies.
1758 g->msg_out = safe_malloc (g, GUESTFS_MAX_CHUNK_SIZE + 4 + 48);
1759 xdrmem_create (&xdr, g->msg_out + 4, GUESTFS_MAX_CHUNK_SIZE + 48, XDR_ENCODE);
1761 /* Serialize the chunk. */
1762 chunk.cancel = cancel;
1763 chunk.data.data_len = buflen;
1764 chunk.data.data_val = (char *) buf;
1766 if (!xdr_guestfs_chunk (&xdr, &chunk)) {
1767 error (g, "xdr_guestfs_chunk failed (buf = %p, buflen = %zu)",
1773 len = xdr_getpos (&xdr);
1776 /* Reduce the size of the outgoing message buffer to the real length. */
1777 g->msg_out = safe_realloc (g, g->msg_out, len + 4);
1778 g->msg_out_size = len + 4;
1781 xdrmem_create (&xdr, g->msg_out, 4, XDR_ENCODE);
1782 xdr_uint32_t (&xdr, &len);
1784 if (guestfs__switch_to_sending (g) == -1)
1788 guestfs_set_send_callback (g, send_cb, &sent);
1789 if (ml->main_loop_run (ml, g) == -1)
1792 error (g, "send file chunk failed, see earlier error messages");
1801 g->msg_out_size = 0;
1805 /* At this point we are sending FileIn file(s) to the guest, and not
1806 * expecting to read anything, so if we do read anything, it must be
1807 * a cancellation message. This checks for this case without blocking.
1810 check_for_daemon_cancellation (guestfs_h *g)
1820 FD_SET (g->sock, &rset);
1823 r = select (g->sock+1, &rset, NULL, NULL, &tv);
1825 perrorf (g, "select");
1831 /* Read the message from the daemon. */
1832 r = xread (g->sock, buf, sizeof buf);
1834 perrorf (g, "read");
1838 xdrmem_create (&xdr, buf, sizeof buf, XDR_DECODE);
1839 xdr_uint32_t (&xdr, &flag);
1842 if (flag != GUESTFS_CANCEL_FLAG) {
1843 error (g, "check_for_daemon_cancellation: read 0x%x from daemon, expected 0x%x\n",
1844 flag, GUESTFS_CANCEL_FLAG);
1851 /* Synchronously receive a file. */
1853 /* Returns -1 = error, 0 = EOF, 1 = more data */
1854 static int receive_file_data_sync (guestfs_h *g, void **buf, size_t *len);
1857 guestfs__receive_file_sync (guestfs_h *g, const char *filename)
1863 fd = open (filename, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, 0666);
1865 perrorf (g, "open: %s", filename);
1869 /* Receive the file in chunked encoding. */
1870 while ((r = receive_file_data_sync (g, &buf, &len)) >= 0) {
1871 if (xwrite (fd, buf, len) == -1) {
1872 perrorf (g, "%s: write", filename);
1877 if (r == 0) break; /* End of file. */
1881 error (g, "%s: error in chunked encoding", filename);
1885 if (close (fd) == -1) {
1886 perrorf (g, "close: %s", filename);
1893 /* Send cancellation message to daemon, then wait until it
1894 * cancels (just throwing away data).
1898 uint32_t flag = GUESTFS_CANCEL_FLAG;
1900 xdrmem_create (&xdr, fbuf, sizeof fbuf, XDR_ENCODE);
1901 xdr_uint32_t (&xdr, &flag);
1904 if (xwrite (g->sock, fbuf, sizeof fbuf) == -1) {
1905 perrorf (g, "write to daemon socket");
1909 while ((r = receive_file_data_sync (g, NULL, NULL)) > 0)
1910 ; /* just discard it */
1915 /* Note that the reply callback can be called multiple times before
1916 * the main loop quits and we get back to the synchronous code. So
1917 * we have to be prepared to save multiple chunks on a list here.
1919 struct receive_file_ctx {
1920 int count; /* 0 if receive_file_cb not called, or
1921 * else count number of chunks.
1923 guestfs_chunk *chunks; /* Array of chunks. */
1927 free_chunks (struct receive_file_ctx *ctx)
1931 for (i = 0; i < ctx->count; ++i)
1932 free (ctx->chunks[i].data.data_val);
1938 receive_file_cb (guestfs_h *g, void *data, XDR *xdr)
1940 guestfs_main_loop *ml = guestfs_get_main_loop (g);
1941 struct receive_file_ctx *ctx = (struct receive_file_ctx *) data;
1942 guestfs_chunk chunk;
1944 if (ctx->count == -1) /* Parse error occurred previously. */
1947 ml->main_loop_quit (ml, g);
1949 memset (&chunk, 0, sizeof chunk);
1951 if (!xdr_guestfs_chunk (xdr, &chunk)) {
1952 error (g, "failed to parse file chunk");
1959 /* Copy the chunk to the list. */
1960 ctx->chunks = safe_realloc (g, ctx->chunks,
1961 sizeof (guestfs_chunk) * (ctx->count+1));
1962 ctx->chunks[ctx->count] = chunk;
1966 /* Receive a chunk of file data. */
1967 /* Returns -1 = error, 0 = EOF, 1 = more data */
1969 receive_file_data_sync (guestfs_h *g, void **buf, size_t *len_r)
1971 struct receive_file_ctx ctx;
1972 guestfs_main_loop *ml = guestfs_get_main_loop (g);
1979 guestfs_set_reply_callback (g, receive_file_cb, &ctx);
1980 (void) ml->main_loop_run (ml, g);
1981 guestfs_set_reply_callback (g, NULL, NULL);
1983 if (ctx.count == 0) {
1984 error (g, "receive_file_data_sync: reply callback not called\n");
1988 if (ctx.count == -1) {
1989 error (g, "receive_file_data_sync: parse error in reply callback\n");
1990 /* callback already freed the chunks */
1995 fprintf (stderr, "receive_file_data_sync: got %d chunks\n", ctx.count);
1997 /* Process each chunk in the list. */
1998 if (buf) *buf = NULL; /* Accumulate data in this buffer. */
2001 for (i = 0; i < ctx.count; ++i) {
2002 if (ctx.chunks[i].cancel) {
2003 error (g, "file receive cancelled by daemon");
2005 if (buf) free (*buf);
2006 if (len_r) *len_r = 0;
2010 if (ctx.chunks[i].data.data_len == 0) { /* end of transfer */
2012 if (len_r) *len_r = len;
2017 *buf = safe_realloc (g, *buf, len + ctx.chunks[i].data.data_len);
2018 memcpy (*buf+len, ctx.chunks[i].data.data_val,
2019 ctx.chunks[i].data.data_len);
2021 len += ctx.chunks[i].data.data_len;
2024 if (len_r) *len_r = len;
2029 /* This is the default main loop implementation, using select(2). */
2032 select_add_handle (guestfs_main_loop *mlv, guestfs_h *g, int fd, int events,
2033 guestfs_handle_event_cb cb, void *data)
2035 struct select_main_loop *ml = (struct select_main_loop *) mlv;
2037 if (fd < 0 || fd >= FD_SETSIZE) {
2038 error (g, "fd %d is out of range", fd);
2042 if ((events & ~(GUESTFS_HANDLE_READABLE |
2043 GUESTFS_HANDLE_WRITABLE |
2044 GUESTFS_HANDLE_HANGUP |
2045 GUESTFS_HANDLE_ERROR)) != 0) {
2046 error (g, "set of events (0x%x) contains unknown events", events);
2051 error (g, "set of events is empty");
2055 if (FD_ISSET (fd, &ml->rset) ||
2056 FD_ISSET (fd, &ml->wset) ||
2057 FD_ISSET (fd, &ml->xset)) {
2058 error (g, "fd %d is already registered", fd);
2063 error (g, "callback is NULL");
2067 if ((events & GUESTFS_HANDLE_READABLE))
2068 FD_SET (fd, &ml->rset);
2069 if ((events & GUESTFS_HANDLE_WRITABLE))
2070 FD_SET (fd, &ml->wset);
2071 if ((events & GUESTFS_HANDLE_HANGUP) || (events & GUESTFS_HANDLE_ERROR))
2072 FD_SET (fd, &ml->xset);
2074 if (fd > ml->max_fd) {
2076 ml->handle_cb_data =
2077 safe_realloc (g, ml->handle_cb_data,
2078 sizeof (struct select_handle_cb_data) * (ml->max_fd+1));
2080 ml->handle_cb_data[fd].cb = cb;
2081 ml->handle_cb_data[fd].g = g;
2082 ml->handle_cb_data[fd].data = data;
2086 /* Any integer >= 0 can be the handle, and this is as good as any ... */
2091 select_remove_handle (guestfs_main_loop *mlv, guestfs_h *g, int fd)
2093 struct select_main_loop *ml = (struct select_main_loop *) mlv;
2095 if (fd < 0 || fd >= FD_SETSIZE) {
2096 error (g, "fd %d is out of range", fd);
2100 if (!FD_ISSET (fd, &ml->rset) &&
2101 !FD_ISSET (fd, &ml->wset) &&
2102 !FD_ISSET (fd, &ml->xset)) {
2103 error (g, "fd %d was not registered", fd);
2107 FD_CLR (fd, &ml->rset);
2108 FD_CLR (fd, &ml->wset);
2109 FD_CLR (fd, &ml->xset);
2111 if (fd == ml->max_fd) {
2113 ml->handle_cb_data =
2114 safe_realloc (g, ml->handle_cb_data,
2115 sizeof (struct select_handle_cb_data) * (ml->max_fd+1));
2124 select_add_timeout (guestfs_main_loop *mlv, guestfs_h *g, int interval,
2125 guestfs_handle_timeout_cb cb, void *data)
2127 //struct select_main_loop *ml = (struct select_main_loop *) mlv;
2129 abort (); /* XXX not implemented yet */
2133 select_remove_timeout (guestfs_main_loop *mlv, guestfs_h *g, int timer)
2135 //struct select_main_loop *ml = (struct select_main_loop *) mlv;
2137 abort (); /* XXX not implemented yet */
2140 /* The 'g' parameter is just used for error reporting. Events
2141 * for multiple handles can be dispatched by running the main
2145 select_main_loop_run (guestfs_main_loop *mlv, guestfs_h *g)
2147 struct select_main_loop *ml = (struct select_main_loop *) mlv;
2149 fd_set rset2, wset2, xset2;
2151 if (ml->is_running) {
2152 error (g, "select_main_loop_run: this cannot be called recursively");
2158 while (ml->is_running) {
2159 if (ml->nr_fds == 0)
2165 r = select (ml->max_fd+1, &rset2, &wset2, &xset2, NULL);
2167 perrorf (g, "select");
2172 for (fd = 0; r > 0 && fd <= ml->max_fd; ++fd) {
2174 if (FD_ISSET (fd, &rset2))
2175 events |= GUESTFS_HANDLE_READABLE;
2176 if (FD_ISSET (fd, &wset2))
2177 events |= GUESTFS_HANDLE_WRITABLE;
2178 if (FD_ISSET (fd, &xset2))
2179 events |= GUESTFS_HANDLE_ERROR | GUESTFS_HANDLE_HANGUP;
2182 ml->handle_cb_data[fd].cb ((guestfs_main_loop *) ml,
2183 ml->handle_cb_data[fd].g,
2184 ml->handle_cb_data[fd].data,
2195 select_main_loop_quit (guestfs_main_loop *mlv, guestfs_h *g)
2197 struct select_main_loop *ml = (struct select_main_loop *) mlv;
2199 /* Note that legitimately ml->is_running can be zero when
2200 * this function is called.