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 */
32 #include <sys/select.h>
33 #include <rpc/types.h>
40 #ifdef HAVE_SYS_TYPES_H
41 #include <sys/types.h>
44 #ifdef HAVE_SYS_WAIT_H
48 #ifdef HAVE_SYS_SOCKET_H
49 #include <sys/socket.h>
57 #include "guestfs_protocol.h"
59 static void error (guestfs_h *g, const char *fs, ...);
60 static void perrorf (guestfs_h *g, const char *fs, ...);
61 static void *safe_malloc (guestfs_h *g, int nbytes);
62 static void *safe_realloc (guestfs_h *g, void *ptr, int nbytes);
63 static char *safe_strdup (guestfs_h *g, const char *str);
65 static void default_error_cb (guestfs_h *g, void *data, const char *msg);
66 static void stdout_event (void *data, int watch, int fd, int events);
67 static void sock_read_event (void *data, int watch, int fd, int events);
68 static void sock_write_event (void *data, int watch, int fd, int events);
70 static int select_add_handle (guestfs_h *g, int fd, int events, guestfs_handle_event_cb cb, void *data);
71 static int select_remove_handle (guestfs_h *g, int watch);
72 static int select_add_timeout (guestfs_h *g, int interval, guestfs_handle_timeout_cb cb, void *data);
73 static int select_remove_timeout (guestfs_h *g, int timer);
74 static void select_main_loop_run (guestfs_h *g);
75 static void select_main_loop_quit (guestfs_h *g);
77 #define UNIX_PATH_MAX 108
79 /* Also in guestfsd.c */
80 #define VMCHANNEL_PORT 6666
81 #define VMCHANNEL_ADDR "10.0.2.4"
83 /* Current main loop. */
84 static guestfs_main_loop main_loop = {
85 .add_handle = select_add_handle,
86 .remove_handle = select_remove_handle,
87 .add_timeout = select_add_timeout,
88 .remove_timeout = select_remove_timeout,
89 .main_loop_run = select_main_loop_run,
90 .main_loop_quit = select_main_loop_quit,
93 /* GuestFS handle and connection. */
94 enum state { CONFIG, LAUNCHING, READY, BUSY, NO_HANDLE };
98 /* State: see the state machine diagram in the man page guestfs(3). */
101 int fd[2]; /* Stdin/stdout of qemu. */
102 int sock; /* Daemon communications socket. */
103 int pid; /* Qemu PID. */
104 time_t start_t; /* The time when we started qemu. */
106 int stdout_watch; /* Watches qemu stdout for log messages. */
107 int sock_watch; /* Watches daemon comm socket. */
109 char *tmpdir; /* Temporary directory containing socket. */
111 char **cmdline; /* Qemu command line. */
117 guestfs_abort_cb abort_cb;
118 guestfs_error_handler_cb error_cb;
119 void * error_cb_data;
120 guestfs_reply_cb reply_cb;
121 void * reply_cb_data;
122 guestfs_log_message_cb log_message_cb;
123 void * log_message_cb_data;
124 guestfs_subprocess_quit_cb subprocess_quit_cb;
125 void * subprocess_quit_cb_data;
126 guestfs_launch_done_cb launch_done_cb;
127 void * launch_done_cb_data;
129 /* These callbacks are called before reply_cb and launch_done_cb,
130 * and are used to implement the high-level API without needing to
131 * interfere with callbacks that the user might have set.
133 guestfs_reply_cb reply_cb_internal;
134 void * reply_cb_internal_data;
135 guestfs_launch_done_cb launch_done_cb_internal;
136 void * launch_done_cb_internal_data;
138 /* Messages sent and received from the daemon. */
140 int msg_in_size, msg_in_allocated;
142 int msg_out_size, msg_out_pos;
148 guestfs_create (void)
153 g = malloc (sizeof (*g));
156 memset (g, 0, sizeof (*g));
163 g->stdout_watch = -1;
167 g->error_cb = default_error_cb;
168 g->error_cb_data = NULL;
170 str = getenv ("LIBGUESTFS_DEBUG");
171 g->verbose = str != NULL && strcmp (str, "1") == 0;
173 /* Start with large serial numbers so they are easy to spot
174 * inside the protocol.
176 g->msg_next_serial = 0x00123400;
182 guestfs_close (guestfs_h *g)
187 if (g->state == NO_HANDLE) {
188 /* Not safe to call 'error' here, so ... */
189 fprintf (stderr, "guestfs_close: called twice on the same handle\n");
193 /* Remove any handlers that might be called back before we kill the
196 g->log_message_cb = NULL;
198 if (g->state != CONFIG)
199 guestfs_kill_subprocess (g);
202 snprintf (filename, sizeof filename, "%s/sock", g->tmpdir);
211 for (i = 0; i < g->cmdline_size; ++i)
212 free (g->cmdline[i]);
216 /* Mark the handle as dead before freeing it. */
217 g->state = NO_HANDLE;
223 default_error_cb (guestfs_h *g, void *data, const char *msg)
225 fprintf (stderr, "libguestfs: error: %s\n", msg);
229 error (guestfs_h *g, const char *fs, ...)
234 if (!g->error_cb) return;
237 vasprintf (&msg, fs, args);
240 g->error_cb (g, g->error_cb_data, msg);
246 perrorf (guestfs_h *g, const char *fs, ...)
252 if (!g->error_cb) return;
255 vasprintf (&msg, fs, args);
260 strerror_r (err, buf, sizeof buf);
264 buf = strerror_r (err, _buf, sizeof _buf);
267 msg = safe_realloc (g, msg, strlen (msg) + 2 + strlen (buf) + 1);
271 g->error_cb (g, g->error_cb_data, msg);
277 safe_malloc (guestfs_h *g, int nbytes)
279 void *ptr = malloc (nbytes);
280 if (!ptr) g->abort_cb ();
285 safe_realloc (guestfs_h *g, void *ptr, int nbytes)
287 void *p = realloc (ptr, nbytes);
288 if (!p) g->abort_cb ();
293 safe_strdup (guestfs_h *g, const char *str)
295 char *s = strdup (str);
296 if (!s) g->abort_cb ();
301 guestfs_set_out_of_memory_handler (guestfs_h *g, guestfs_abort_cb cb)
307 guestfs_get_out_of_memory_handler (guestfs_h *g)
313 guestfs_set_error_handler (guestfs_h *g, guestfs_error_handler_cb cb, void *data)
316 g->error_cb_data = data;
319 guestfs_error_handler_cb
320 guestfs_get_error_handler (guestfs_h *g, void **data_rtn)
322 if (data_rtn) *data_rtn = g->error_cb_data;
327 guestfs_set_verbose (guestfs_h *g, int v)
333 guestfs_get_verbose (guestfs_h *g)
338 /* Add a string to the current command line. */
340 incr_cmdline_size (guestfs_h *g)
342 if (g->cmdline == NULL) {
343 /* g->cmdline[0] is reserved for argv[0], set in guestfs_launch. */
345 g->cmdline = safe_malloc (g, sizeof (char *));
346 g->cmdline[0] = NULL;
350 g->cmdline = safe_realloc (g, g->cmdline, sizeof (char *) * g->cmdline_size);
354 add_cmdline (guestfs_h *g, const char *str)
356 if (g->state != CONFIG) {
357 error (g, "command line cannot be altered after qemu subprocess launched");
361 incr_cmdline_size (g);
362 g->cmdline[g->cmdline_size-1] = safe_strdup (g, str);
367 guestfs_config (guestfs_h *g,
368 const char *qemu_param, const char *qemu_value)
370 if (qemu_param[0] != '-') {
371 error (g, "guestfs_config: parameter must begin with '-' character");
375 /* A bit fascist, but the user will probably break the extra
376 * parameters that we add if they try to set any of these.
378 if (strcmp (qemu_param, "-kernel") == 0 ||
379 strcmp (qemu_param, "-initrd") == 0 ||
380 strcmp (qemu_param, "-nographic") == 0 ||
381 strcmp (qemu_param, "-serial") == 0 ||
382 strcmp (qemu_param, "-vnc") == 0 ||
383 strcmp (qemu_param, "-full-screen") == 0 ||
384 strcmp (qemu_param, "-std-vga") == 0 ||
385 strcmp (qemu_param, "-vnc") == 0) {
386 error (g, "guestfs_config: parameter '%s' isn't allowed", qemu_param);
390 if (add_cmdline (g, qemu_param) != 0) return -1;
392 if (qemu_value != NULL) {
393 if (add_cmdline (g, qemu_value) != 0) return -1;
400 guestfs_add_drive (guestfs_h *g, const char *filename)
402 int len = strlen (filename) + 64;
405 if (strchr (filename, ',') != NULL) {
406 error (g, "filename cannot contain ',' (comma) character");
410 snprintf (buf, len, "file=%s", filename);
412 return guestfs_config (g, "-drive", buf);
416 guestfs_add_cdrom (guestfs_h *g, const char *filename)
418 if (strchr (filename, ',') != NULL) {
419 error (g, "filename cannot contain ',' (comma) character");
423 return guestfs_config (g, "-cdrom", filename);
427 guestfs_launch (guestfs_h *g)
429 static const char *dir_template = "/tmp/libguestfsXXXXXX";
433 /*const char *qemu = QEMU;*/ /* XXX */
434 const char *qemu = "/usr/bin/qemu-system-x86_64";
435 const char *kernel = "vmlinuz.fedora-10.x86_64";
436 const char *initrd = "initramfs.fedora-10.x86_64.img";
438 struct sockaddr_un addr;
440 /* XXX Choose which qemu to run. */
441 /* XXX Choose initrd, etc. */
445 error (g, "you must call guestfs_add_drive before guestfs_launch");
449 if (g->state != CONFIG) {
450 error (g, "qemu has already been launched");
454 /* Make the temporary directory containing the socket. */
456 g->tmpdir = safe_strdup (g, dir_template);
457 if (mkdtemp (g->tmpdir) == NULL) {
458 perrorf (g, "%s: cannot create temporary directory", dir_template);
463 snprintf (unixsock, sizeof unixsock, "%s/sock", g->tmpdir);
466 if (pipe (wfd) == -1 || pipe (rfd) == -1) {
481 if (r == 0) { /* Child (qemu). */
485 /* Set up the full command line. Do this in the subprocess so we
486 * don't need to worry about cleaning up.
488 g->cmdline[0] = (char *) qemu;
490 /* Construct the -net channel parameter for qemu. */
491 snprintf (vmchannel, sizeof vmchannel,
492 "channel,%d:unix:%s,server,nowait",
493 VMCHANNEL_PORT, unixsock);
495 /* Linux kernel command line. */
496 snprintf (append, sizeof append,
497 "console=ttyS0 guestfs=%s:%d", VMCHANNEL_ADDR, VMCHANNEL_PORT);
499 add_cmdline (g, "-m");
500 add_cmdline (g, "384"); /* XXX Choose best size. */
501 add_cmdline (g, "-kernel");
502 add_cmdline (g, (char *) kernel);
503 add_cmdline (g, "-initrd");
504 add_cmdline (g, (char *) initrd);
505 add_cmdline (g, "-append");
506 add_cmdline (g, append);
507 add_cmdline (g, "-nographic");
508 add_cmdline (g, "-serial");
509 add_cmdline (g, "stdio");
510 add_cmdline (g, "-net");
511 add_cmdline (g, vmchannel);
512 add_cmdline (g, "-net");
513 add_cmdline (g, "user,vlan=0");
514 add_cmdline (g, "-net");
515 add_cmdline (g, "nic,vlan=0");
516 incr_cmdline_size (g);
517 g->cmdline[g->cmdline_size-1] = NULL;
520 fprintf (stderr, "%s", qemu);
521 for (i = 0; g->cmdline[i]; ++i)
522 fprintf (stderr, " %s", g->cmdline[i]);
523 fprintf (stderr, "\n");
526 /* Set up stdin, stdout. */
535 /* Set up a new process group, so we can signal this process
536 * and all subprocesses (eg. if qemu is really a shell script).
541 execv (qemu, g->cmdline); /* Run qemu. */
546 /* Parent (library). */
549 /* Start the clock ... */
552 /* Close the other ends of the pipe. */
556 if (fcntl (wfd[1], F_SETFL, O_NONBLOCK) == -1 ||
557 fcntl (rfd[0], F_SETFL, O_NONBLOCK) == -1) {
558 perrorf (g, "fcntl");
562 g->fd[0] = wfd[1]; /* stdin of child */
563 g->fd[1] = rfd[0]; /* stdout of child */
565 /* Open the Unix socket. The vmchannel implementation that got
566 * merged with qemu sucks in a number of ways. Both ends do
567 * connect(2), which means that no one knows what, if anything, is
568 * connected to the other end, or if it becomes disconnected. Even
569 * worse, we have to wait some indeterminate time for qemu to create
570 * the socket and connect to it (which happens very early in qemu's
571 * start-up), so any code that uses vmchannel is inherently racy.
572 * Hence this silly loop.
574 g->sock = socket (AF_UNIX, SOCK_STREAM, 0);
576 perrorf (g, "socket");
580 if (fcntl (g->sock, F_SETFL, O_NONBLOCK) == -1) {
581 perrorf (g, "fcntl");
585 addr.sun_family = AF_UNIX;
586 strncpy (addr.sun_path, unixsock, UNIX_PATH_MAX);
587 addr.sun_path[UNIX_PATH_MAX-1] = '\0';
591 /* Always sleep at least once to give qemu a small chance to start up. */
594 r = connect (g->sock, (struct sockaddr *) &addr, sizeof addr);
595 if ((r == -1 && errno == EINPROGRESS) || r == 0)
599 perrorf (g, "connect");
603 error (g, "failed to connect to vmchannel socket");
607 /* Watch the file descriptors. */
610 g->msg_in_size = g->msg_in_allocated = 0;
618 main_loop.add_handle (g, g->fd[1],
619 GUESTFS_HANDLE_READABLE,
621 if (g->stdout_watch == -1) {
622 error (g, "could not watch qemu stdout");
627 main_loop.add_handle (g, g->sock,
628 GUESTFS_HANDLE_READABLE,
630 if (g->sock_watch == -1) {
631 error (g, "could not watch daemon communications socket");
635 g->state = LAUNCHING;
639 if (g->stdout_watch >= 0)
640 main_loop.remove_handle (g, g->stdout_watch);
641 if (g->sock_watch >= 0)
642 main_loop.remove_handle (g, g->sock_watch);
651 waitpid (g->pid, NULL, 0);
657 g->stdout_watch = -1;
663 finish_wait_ready (guestfs_h *g, void *vp)
666 main_loop.main_loop_quit (g);
670 guestfs_wait_ready (guestfs_h *g)
674 if (g->state == READY) return 0;
676 if (g->state == BUSY) {
677 error (g, "qemu has finished launching already");
681 if (g->state != LAUNCHING) {
682 error (g, "qemu has not been launched yet");
686 g->launch_done_cb_internal = finish_wait_ready;
687 g->launch_done_cb_internal_data = &r;
688 main_loop.main_loop_run (g);
689 g->launch_done_cb_internal = NULL;
690 g->launch_done_cb_internal_data = NULL;
693 error (g, "guestfs_wait_ready failed, see earlier error messages");
697 /* This is possible in some really strange situations, such as
698 * guestfsd starts up OK but then qemu immediately exits. Check for
699 * it because the caller is probably expecting to be able to send
700 * commands after this function returns.
702 if (g->state != READY) {
703 error (g, "qemu launched and contacted daemon, but state != READY");
711 guestfs_kill_subprocess (guestfs_h *g)
713 if (g->state == CONFIG) {
714 error (g, "no subprocess to kill");
719 fprintf (stderr, "sending SIGTERM to process group %d\n", g->pid);
721 kill (g->pid, SIGTERM);
726 /* This function is called whenever qemu prints something on stdout.
727 * Qemu's stdout is also connected to the guest's serial console, so
728 * we see kernel messages here too.
731 stdout_event (void *data, int watch, int fd, int events)
733 guestfs_h *g = (guestfs_h *) data;
740 "stdout_event: %p g->state = %d, fd = %d, events = 0x%x\n",
741 g, g->state, fd, events);
744 if (g->fd[1] != fd) {
745 error (g, "stdout_event: internal error: %d != %d", g->fd[1], fd);
749 n = read (fd, buf, sizeof buf);
751 /* Hopefully this indicates the qemu child process has died. */
753 fprintf (stderr, "stdout_event: %p: child process died\n", g);
754 /*kill (g->pid, SIGTERM);*/
755 waitpid (g->pid, NULL, 0);
756 if (g->stdout_watch >= 0)
757 main_loop.remove_handle (g, g->stdout_watch);
758 if (g->sock_watch >= 0)
759 main_loop.remove_handle (g, g->sock_watch);
768 g->stdout_watch = -1;
771 if (g->subprocess_quit_cb)
772 g->subprocess_quit_cb (g, g->subprocess_quit_cb_data);
782 /* In verbose mode, copy all log messages to stderr. */
786 /* It's an actual log message, send it upwards if anyone is listening. */
787 if (g->log_message_cb)
788 g->log_message_cb (g, g->log_message_cb_data, buf, n);
791 /* The function is called whenever we can read something on the
792 * guestfsd (daemon inside the guest) communication socket.
795 sock_read_event (void *data, int watch, int fd, int events)
797 guestfs_h *g = (guestfs_h *) data;
804 "sock_read_event: %p g->state = %d, fd = %d, events = 0x%x\n",
805 g, g->state, fd, events);
808 error (g, "sock_read_event: internal error: %d != %d", g->sock, fd);
812 if (g->msg_in_size <= g->msg_in_allocated) {
813 g->msg_in_allocated += 4096;
814 g->msg_in = safe_realloc (g, g->msg_in, g->msg_in_allocated);
816 n = read (g->sock, g->msg_in + g->msg_in_size,
817 g->msg_in_allocated - g->msg_in_size);
819 /* Disconnected? Ignore it because stdout_watch will get called
820 * and will do the cleanup.
832 /* Have we got enough of a message to be able to process it yet? */
833 if (g->msg_in_size < 4) return;
835 xdrmem_create (&xdr, g->msg_in, g->msg_in_size, XDR_DECODE);
836 if (!xdr_uint32_t (&xdr, &len)) {
837 error (g, "can't decode length word");
841 /* Length is normally the length of the message, but when guestfsd
842 * starts up it sends a "magic" value (longer than any possible
843 * message). Check for this.
845 if (len == 0xf5f55ff5) {
846 if (g->state != LAUNCHING)
847 error (g, "received magic signature from guestfsd, but in state %d",
849 else if (g->msg_in_size != 4)
850 error (g, "received magic signature from guestfsd, but msg size is %d",
854 if (g->launch_done_cb_internal)
855 g->launch_done_cb_internal (g, g->launch_done_cb_internal_data);
856 if (g->launch_done_cb)
857 g->launch_done_cb (g, g->launch_done_cb_data);
863 /* If this happens, it's pretty bad and we've probably lost synchronization.*/
864 if (len > GUESTFS_MESSAGE_MAX) {
865 error (g, "message length (%u) > maximum possible size (%d)",
866 len, GUESTFS_MESSAGE_MAX);
870 if (g->msg_in_size-4 < len) return; /* Need more of this message. */
872 /* This should not happen, and if it does it probably means we've
873 * lost all hope of synchronization.
875 if (g->msg_in_size-4 > len) {
876 error (g, "len = %d, but msg_in_size-4 = %d", len, g->msg_in_size-4);
880 /* Not in the expected state. */
881 if (g->state != BUSY)
882 error (g, "state %d != BUSY", g->state);
884 /* Push the message up to the higher layer. Note that unlike
885 * launch_done_cb / launch_done_cb_internal, we only call at
886 * most one of the callback functions here.
889 if (g->reply_cb_internal)
890 g->reply_cb_internal (g, g->reply_cb_internal_data, &xdr);
891 else if (g->reply_cb)
892 g->reply_cb (g, g->reply_cb, &xdr);
895 /* Free the message buffer if it's grown excessively large. */
896 if (g->msg_in_allocated > 65536) {
899 g->msg_in_size = g->msg_in_allocated = 0;
906 /* The function is called whenever we can write something on the
907 * guestfsd (daemon inside the guest) communication socket.
910 sock_write_event (void *data, int watch, int fd, int events)
912 guestfs_h *g = (guestfs_h *) data;
917 "sock_write_event: %p g->state = %d, fd = %d, events = 0x%x\n",
918 g, g->state, fd, events);
921 error (g, "sock_write_event: internal error: %d != %d", g->sock, fd);
925 if (g->state != BUSY) {
926 error (g, "sock_write_event: state %d != BUSY", g->state);
931 fprintf (stderr, "sock_write_event: writing %d bytes ...\n",
932 g->msg_out_size - g->msg_out_pos);
934 n = write (g->sock, g->msg_out + g->msg_out_pos,
935 g->msg_out_size - g->msg_out_pos);
938 perrorf (g, "write");
943 fprintf (stderr, "sock_write_event: wrote %d bytes\n", n);
948 if (g->msg_out_pos < g->msg_out_size)
952 fprintf (stderr, "sock_write_event: done writing, switching back to reading events\n");
955 g->msg_out_pos = g->msg_out_size = 0;
957 if (main_loop.remove_handle (g, g->sock_watch) == -1) {
958 error (g, "remove_handle failed in sock_write_event");
962 main_loop.add_handle (g, g->sock,
963 GUESTFS_HANDLE_READABLE,
965 if (g->sock_watch == -1) {
966 error (g, "add_handle failed in sock_write_event");
971 /* Dispatch a call to the remote daemon. This function just queues
972 * the call in msg_out, to be sent when we next enter the main loop.
973 * Returns -1 for error, or the message serial number.
976 dispatch (guestfs_h *g, int proc_nr, xdrproc_t xdrp, char *args)
978 char buffer[GUESTFS_MESSAGE_MAX];
979 struct guestfs_message_header hdr;
982 int serial = g->msg_next_serial++;
984 if (g->state != READY) {
985 error (g, "dispatch: state %d != READY", g->state);
989 /* Serialize the header. */
990 hdr.prog = GUESTFS_PROGRAM;
991 hdr.vers = GUESTFS_PROTOCOL_VERSION;
993 hdr.direction = GUESTFS_DIRECTION_CALL;
995 hdr.status = GUESTFS_STATUS_OK;
997 xdrmem_create (&xdr, buffer, sizeof buffer, XDR_ENCODE);
998 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
999 error (g, "xdr_guestfs_message_header failed");
1003 /* Serialize the args. If any, because some message types
1004 * have no parameters.
1007 if (!(*xdrp) (&xdr, args)) {
1008 error (g, "dispatch failed to marshal args");
1013 len = xdr_getpos (&xdr);
1016 /* Allocate the outgoing message buffer. */
1017 g->msg_out = safe_malloc (g, len + 4);
1019 g->msg_out_size = len + 4;
1023 xdrmem_create (&xdr, g->msg_out, 4, XDR_ENCODE);
1024 if (!xdr_uint32_t (&xdr, &len)) {
1025 error (g, "xdr_uint32_t failed in dispatch");
1029 memcpy (g->msg_out + 4, buffer, len);
1031 /* Change the handle to sock_write_event. */
1032 if (main_loop.remove_handle (g, g->sock_watch) == -1) {
1033 error (g, "remove_handle failed in dispatch");
1037 main_loop.add_handle (g, g->sock,
1038 GUESTFS_HANDLE_WRITABLE,
1039 sock_write_event, g);
1040 if (g->sock_watch == -1) {
1041 error (g, "add_handle failed in dispatch");
1050 g->msg_out_size = 0;
1055 /* The high-level actions are autogenerated by generator.ml. Include
1058 #include "guestfs-actions.c"
1060 /* This is the default main loop implementation, using select(2). */
1062 struct handle_cb_data {
1063 guestfs_handle_event_cb cb;
1070 static int select_init_done = 0;
1071 static int max_fd = -1;
1072 static int nr_fds = 0;
1073 static struct handle_cb_data *handle_cb_data = NULL;
1078 if (!select_init_done) {
1083 select_init_done = 1;
1088 select_add_handle (guestfs_h *g, int fd, int events,
1089 guestfs_handle_event_cb cb, void *data)
1093 if (fd < 0 || fd >= FD_SETSIZE) {
1094 error (g, "fd %d is out of range", fd);
1098 if ((events & ~(GUESTFS_HANDLE_READABLE |
1099 GUESTFS_HANDLE_WRITABLE |
1100 GUESTFS_HANDLE_HANGUP |
1101 GUESTFS_HANDLE_ERROR)) != 0) {
1102 error (g, "set of events (0x%x) contains unknown events", events);
1107 error (g, "set of events is empty");
1111 if (FD_ISSET (fd, &rset) || FD_ISSET (fd, &wset) || FD_ISSET (fd, &xset)) {
1112 error (g, "fd %d is already registered", fd);
1117 error (g, "callback is NULL");
1121 if ((events & GUESTFS_HANDLE_READABLE))
1123 if ((events & GUESTFS_HANDLE_WRITABLE))
1125 if ((events & GUESTFS_HANDLE_HANGUP) || (events & GUESTFS_HANDLE_ERROR))
1130 handle_cb_data = safe_realloc (g, handle_cb_data,
1131 sizeof (struct handle_cb_data) * (max_fd+1));
1133 handle_cb_data[fd].cb = cb;
1134 handle_cb_data[fd].data = data;
1138 /* Any integer >= 0 can be the handle, and this is as good as any ... */
1143 select_remove_handle (guestfs_h *g, int fd)
1147 if (fd < 0 || fd >= FD_SETSIZE) {
1148 error (g, "fd %d is out of range", fd);
1152 if (!FD_ISSET (fd, &rset) && !FD_ISSET (fd, &wset) && !FD_ISSET (fd, &xset)) {
1153 error (g, "fd %d was not registered", fd);
1163 handle_cb_data = safe_realloc (g, handle_cb_data,
1164 sizeof (struct handle_cb_data) * (max_fd+1));
1173 select_add_timeout (guestfs_h *g, int interval,
1174 guestfs_handle_timeout_cb cb, void *data)
1178 abort (); /* XXX not implemented yet */
1182 select_remove_timeout (guestfs_h *g, int timer)
1186 abort (); /* XXX not implemented yet */
1189 /* Note that main loops can be nested. */
1190 static int level = 0;
1193 select_main_loop_run (guestfs_h *g)
1195 int old_level, fd, r, events;
1196 fd_set rset2, wset2, xset2;
1200 old_level = level++;
1201 while (level > old_level) {
1210 r = select (max_fd+1, &rset2, &wset2, &xset2, NULL);
1212 perrorf (g, "select");
1217 for (fd = 0; r > 0 && fd <= max_fd; ++fd) {
1219 if (FD_ISSET (fd, &rset2))
1220 events |= GUESTFS_HANDLE_READABLE;
1221 if (FD_ISSET (fd, &wset2))
1222 events |= GUESTFS_HANDLE_WRITABLE;
1223 if (FD_ISSET (fd, &xset2))
1224 events |= GUESTFS_HANDLE_ERROR | GUESTFS_HANDLE_HANGUP;
1227 handle_cb_data[fd].cb (handle_cb_data[fd].data,
1235 select_main_loop_quit (guestfs_h *g)
1240 error (g, "cannot quit, we are not in a main loop");