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>
58 static void error (guestfs_h *g, const char *fs, ...);
59 static void perrorf (guestfs_h *g, const char *fs, ...);
60 static void *safe_malloc (guestfs_h *g, int nbytes);
61 static void *safe_realloc (guestfs_h *g, void *ptr, int nbytes);
62 static char *safe_strdup (guestfs_h *g, const char *str);
64 static void default_error_cb (guestfs_h *g, void *data, const char *msg);
65 static void stdout_event (void *data, int watch, int fd, int events);
66 static void sock_read_event (void *data, int watch, int fd, int events);
67 //static void sock_write_event (void *data, int watch, int fd, int events);
69 static int select_add_handle (guestfs_h *g, int fd, int events, guestfs_handle_event_cb cb, void *data);
70 static int select_remove_handle (guestfs_h *g, int watch);
71 static int select_add_timeout (guestfs_h *g, int interval, guestfs_handle_timeout_cb cb, void *data);
72 static int select_remove_timeout (guestfs_h *g, int timer);
73 static void select_main_loop_run (guestfs_h *g);
74 static void select_main_loop_quit (guestfs_h *g);
76 #define UNIX_PATH_MAX 108
78 #define VMCHANNEL_PORT 6666
79 #define VMCHANNEL_ADDR "10.0.2.4"
81 /* Current main loop. */
82 static guestfs_main_loop main_loop = {
83 .add_handle = select_add_handle,
84 .remove_handle = select_remove_handle,
85 .add_timeout = select_add_timeout,
86 .remove_timeout = select_remove_timeout,
87 .main_loop_run = select_main_loop_run,
88 .main_loop_quit = select_main_loop_quit,
91 /* GuestFS handle and connection. */
92 enum state { CONFIG, LAUNCHING, READY, BUSY, NO_HANDLE };
96 /* State: see the state machine diagram in the man page guestfs(3). */
99 int fd[2]; /* Stdin/stdout of qemu. */
100 int sock; /* Daemon communications socket. */
101 int pid; /* Qemu PID. */
102 time_t start_t; /* The time when we started qemu. */
104 int stdout_watch; /* Watches qemu stdout for log messages. */
105 int sock_watch; /* Watches daemon comm socket. */
107 char *tmpdir; /* Temporary directory containing socket. */
109 char **cmdline; /* Qemu command line. */
115 guestfs_abort_cb abort_cb;
116 guestfs_error_handler_cb error_cb;
117 void * error_cb_data;
118 guestfs_reply_cb reply_cb;
119 void * reply_cb_data;
120 guestfs_log_message_cb log_message_cb;
121 void * log_message_cb_data;
122 guestfs_subprocess_quit_cb subprocess_quit_cb;
123 void * subprocess_quit_cb_data;
124 guestfs_launch_done_cb launch_done_cb;
125 void * launch_done_cb_data;
127 /* These callbacks are called before reply_cb and launch_done_cb,
128 * and are used to implement the high-level API without needing to
129 * interfere with callbacks that the user might have set.
131 guestfs_reply_cb reply_cb_internal;
132 void * reply_cb_internal_data;
133 guestfs_launch_done_cb launch_done_cb_internal;
134 void * launch_done_cb_internal_data;
136 /* Messages sent and received from the daemon. */
138 int msg_in_size, msg_in_allocated;
144 guestfs_create (void)
149 g = malloc (sizeof (*g));
152 memset (g, 0, sizeof (*g));
159 g->stdout_watch = -1;
163 g->error_cb = default_error_cb;
164 g->error_cb_data = NULL;
166 str = getenv ("LIBGUESTFS_DEBUG");
167 g->verbose = str != NULL && strcmp (str, "1") == 0;
173 guestfs_close (guestfs_h *g)
178 if (g->state == NO_HANDLE) {
179 /* Not safe to call 'error' here, so ... */
180 fprintf (stderr, "guestfs_close: called twice on the same handle\n");
184 /* Remove any handlers that might be called back before we kill the
187 g->log_message_cb = NULL;
189 if (g->state != CONFIG)
190 guestfs_kill_subprocess (g);
193 snprintf (filename, sizeof filename, "%s/sock", g->tmpdir);
202 for (i = 0; i < g->cmdline_size; ++i)
203 free (g->cmdline[i]);
207 /* Mark the handle as dead before freeing it. */
208 g->state = NO_HANDLE;
214 default_error_cb (guestfs_h *g, void *data, const char *msg)
216 fprintf (stderr, "libguestfs: error: %s\n", msg);
220 error (guestfs_h *g, const char *fs, ...)
225 if (!g->error_cb) return;
228 vasprintf (&msg, fs, args);
231 g->error_cb (g, g->error_cb_data, msg);
237 perrorf (guestfs_h *g, const char *fs, ...)
243 if (!g->error_cb) return;
246 vasprintf (&msg, fs, args);
251 strerror_r (err, buf, sizeof buf);
255 buf = strerror_r (err, _buf, sizeof _buf);
258 msg = safe_realloc (g, msg, strlen (msg) + 2 + strlen (buf) + 1);
262 g->error_cb (g, g->error_cb_data, msg);
268 safe_malloc (guestfs_h *g, int nbytes)
270 void *ptr = malloc (nbytes);
271 if (!ptr) g->abort_cb ();
276 safe_realloc (guestfs_h *g, void *ptr, int nbytes)
278 void *p = realloc (ptr, nbytes);
279 if (!p) g->abort_cb ();
284 safe_strdup (guestfs_h *g, const char *str)
286 char *s = strdup (str);
287 if (!s) g->abort_cb ();
292 guestfs_set_out_of_memory_handler (guestfs_h *g, guestfs_abort_cb cb)
298 guestfs_get_out_of_memory_handler (guestfs_h *g)
304 guestfs_set_error_handler (guestfs_h *g, guestfs_error_handler_cb cb, void *data)
307 g->error_cb_data = data;
310 guestfs_error_handler_cb
311 guestfs_get_error_handler (guestfs_h *g, void **data_rtn)
313 if (data_rtn) *data_rtn = g->error_cb_data;
318 guestfs_set_verbose (guestfs_h *g, int v)
324 guestfs_get_verbose (guestfs_h *g)
329 /* Add a string to the current command line. */
331 incr_cmdline_size (guestfs_h *g)
333 if (g->cmdline == NULL) {
334 /* g->cmdline[0] is reserved for argv[0], set in guestfs_launch. */
336 g->cmdline = safe_malloc (g, sizeof (char *));
337 g->cmdline[0] = NULL;
341 g->cmdline = safe_realloc (g, g->cmdline, sizeof (char *) * g->cmdline_size);
345 add_cmdline (guestfs_h *g, const char *str)
347 if (g->state != CONFIG) {
348 error (g, "command line cannot be altered after qemu subprocess launched");
352 incr_cmdline_size (g);
353 g->cmdline[g->cmdline_size-1] = safe_strdup (g, str);
358 guestfs_config (guestfs_h *g,
359 const char *qemu_param, const char *qemu_value)
361 if (qemu_param[0] != '-') {
362 error (g, "guestfs_config: parameter must begin with '-' character");
366 /* A bit fascist, but the user will probably break the extra
367 * parameters that we add if they try to set any of these.
369 if (strcmp (qemu_param, "-kernel") == 0 ||
370 strcmp (qemu_param, "-initrd") == 0 ||
371 strcmp (qemu_param, "-nographic") == 0 ||
372 strcmp (qemu_param, "-serial") == 0 ||
373 strcmp (qemu_param, "-vnc") == 0 ||
374 strcmp (qemu_param, "-full-screen") == 0 ||
375 strcmp (qemu_param, "-std-vga") == 0 ||
376 strcmp (qemu_param, "-vnc") == 0) {
377 error (g, "guestfs_config: parameter '%s' isn't allowed", qemu_param);
381 if (add_cmdline (g, qemu_param) != 0) return -1;
383 if (qemu_value != NULL) {
384 if (add_cmdline (g, qemu_value) != 0) return -1;
391 guestfs_add_drive (guestfs_h *g, const char *filename)
393 int len = strlen (filename) + 64;
396 if (strchr (filename, ',') != NULL) {
397 error (g, "filename cannot contain ',' (comma) character");
401 snprintf (buf, len, "file=%s", filename);
403 return guestfs_config (g, "-drive", buf);
407 guestfs_add_cdrom (guestfs_h *g, const char *filename)
409 if (strchr (filename, ',') != NULL) {
410 error (g, "filename cannot contain ',' (comma) character");
414 return guestfs_config (g, "-cdrom", filename);
418 guestfs_launch (guestfs_h *g)
420 static const char *dir_template = "/tmp/libguestfsXXXXXX";
424 /*const char *qemu = QEMU;*/ /* XXX */
425 const char *qemu = "/usr/bin/qemu-system-x86_64";
426 const char *kernel = "vmlinuz.fedora-10.x86_64";
427 const char *initrd = "initramfs.fedora-10.x86_64.img";
429 struct sockaddr_un addr;
431 /* XXX Choose which qemu to run. */
432 /* XXX Choose initrd, etc. */
436 error (g, "you must call guestfs_add_drive before guestfs_launch");
440 if (g->state != CONFIG) {
441 error (g, "qemu has already been launched");
445 /* Make the temporary directory containing the socket. */
447 g->tmpdir = safe_strdup (g, dir_template);
448 if (mkdtemp (g->tmpdir) == NULL) {
449 perrorf (g, "%s: cannot create temporary directory", dir_template);
454 snprintf (unixsock, sizeof unixsock, "%s/sock", g->tmpdir);
457 if (pipe (wfd) == -1 || pipe (rfd) == -1) {
472 if (r == 0) { /* Child (qemu). */
476 /* Set up the full command line. Do this in the subprocess so we
477 * don't need to worry about cleaning up.
479 g->cmdline[0] = (char *) qemu;
481 /* Construct the -net channel parameter for qemu. */
482 snprintf (vmchannel, sizeof vmchannel,
483 "channel,%d:unix:%s,server,nowait",
484 VMCHANNEL_PORT, unixsock);
486 /* Linux kernel command line. */
487 snprintf (append, sizeof append,
488 "console=ttyS0 guestfs=%s:%d", VMCHANNEL_ADDR, VMCHANNEL_PORT);
490 add_cmdline (g, "-m");
491 add_cmdline (g, "384"); /* XXX Choose best size. */
492 add_cmdline (g, "-kernel");
493 add_cmdline (g, (char *) kernel);
494 add_cmdline (g, "-initrd");
495 add_cmdline (g, (char *) initrd);
496 add_cmdline (g, "-append");
497 add_cmdline (g, append);
498 add_cmdline (g, "-nographic");
499 add_cmdline (g, "-serial");
500 add_cmdline (g, "stdio");
501 add_cmdline (g, "-net");
502 add_cmdline (g, vmchannel);
503 add_cmdline (g, "-net");
504 add_cmdline (g, "user,vlan=0");
505 add_cmdline (g, "-net");
506 add_cmdline (g, "nic,vlan=0");
507 incr_cmdline_size (g);
508 g->cmdline[g->cmdline_size-1] = NULL;
511 fprintf (stderr, "%s", qemu);
512 for (i = 0; g->cmdline[i]; ++i)
513 fprintf (stderr, " %s", g->cmdline[i]);
514 fprintf (stderr, "\n");
517 /* Set up stdin, stdout. */
526 /* Set up a new process group, so we can signal this process
527 * and all subprocesses (eg. if qemu is really a shell script).
532 execv (qemu, g->cmdline); /* Run qemu. */
537 /* Parent (library). */
540 /* Start the clock ... */
543 /* Close the other ends of the pipe. */
547 if (fcntl (wfd[1], F_SETFL, O_NONBLOCK) == -1 ||
548 fcntl (rfd[0], F_SETFL, O_NONBLOCK) == -1) {
549 perrorf (g, "fcntl");
553 g->fd[0] = wfd[1]; /* stdin of child */
554 g->fd[1] = rfd[0]; /* stdout of child */
556 /* Open the Unix socket. The vmchannel implementation that got
557 * merged with qemu sucks in a number of ways. Both ends do
558 * connect(2), which means that no one knows what, if anything, is
559 * connected to the other end, or if it becomes disconnected. Even
560 * worse, we have to wait some indeterminate time for qemu to create
561 * the socket and connect to it (which happens very early in qemu's
562 * start-up), so any code that uses vmchannel is inherently racy.
563 * Hence this silly loop.
565 g->sock = socket (AF_UNIX, SOCK_STREAM, 0);
567 perrorf (g, "socket");
571 if (fcntl (g->sock, F_SETFL, O_NONBLOCK) == -1) {
572 perrorf (g, "fcntl");
576 addr.sun_family = AF_UNIX;
577 strncpy (addr.sun_path, unixsock, UNIX_PATH_MAX);
578 addr.sun_path[UNIX_PATH_MAX-1] = '\0';
582 /* Always sleep at least once to give qemu a small chance to start up. */
585 r = connect (g->sock, (struct sockaddr *) &addr, sizeof addr);
586 if ((r == -1 && errno == EINPROGRESS) || r == 0)
589 perrorf (g, "connect");
593 error (g, "failed to connect to vmchannel socket");
597 /* Watch the file descriptors. */
600 g->msg_in_size = g->msg_in_allocated = 0;
607 main_loop.add_handle (g, g->fd[1],
608 GUESTFS_HANDLE_READABLE,
610 if (g->stdout_watch == -1) {
611 error (g, "could not watch qemu stdout");
616 main_loop.add_handle (g, g->sock,
617 GUESTFS_HANDLE_READABLE |
618 GUESTFS_HANDLE_HANGUP |
619 GUESTFS_HANDLE_ERROR,
621 if (g->sock_watch == -1) {
622 error (g, "could not watch daemon communications socket");
626 g->state = LAUNCHING;
630 if (g->stdout_watch >= 0)
631 main_loop.remove_handle (g, g->stdout_watch);
632 if (g->sock_watch >= 0)
633 main_loop.remove_handle (g, g->sock_watch);
642 waitpid (g->pid, NULL, 0);
648 g->stdout_watch = -1;
654 finish_wait_ready (guestfs_h *g, void *vp)
657 main_loop.main_loop_quit (g);
661 guestfs_wait_ready (guestfs_h *g)
665 if (g->state == READY) return 0;
667 if (g->state == BUSY) {
668 error (g, "qemu has finished launching already");
672 if (g->state != LAUNCHING) {
673 error (g, "qemu has not been launched yet");
677 g->launch_done_cb_internal = finish_wait_ready;
678 g->launch_done_cb_internal_data = &r;
679 main_loop.main_loop_run (g);
680 g->launch_done_cb_internal = NULL;
681 g->launch_done_cb_internal_data = NULL;
684 error (g, "guestfs_wait_ready failed, see earlier error messages");
688 /* This is possible in some really strange situations, such as
689 * guestfsd starts up OK but then qemu immediately exits. Check for
690 * it because the caller is probably expecting to be able to send
691 * commands after this function returns.
693 if (g->state != READY) {
694 error (g, "qemu launched and contacted daemon, but state != READY");
702 guestfs_kill_subprocess (guestfs_h *g)
704 if (g->state == CONFIG) {
705 error (g, "no subprocess to kill");
710 fprintf (stderr, "sending SIGTERM to process group %d\n", g->pid);
712 kill (g->pid, SIGTERM);
717 /* This function is called whenever qemu prints something on stdout.
718 * Qemu's stdout is also connected to the guest's serial console, so
719 * we see kernel messages here too.
722 stdout_event (void *data, int watch, int fd, int events)
724 guestfs_h *g = (guestfs_h *) data;
731 "stdout_event: %p g->state = %d, fd = %d, events = 0x%x\n",
732 g, g->state, fd, events);
735 if (g->fd[1] != fd) {
736 error (g, "stdout_event: internal error: %d != %d", g->fd[1], fd);
740 n = read (fd, buf, sizeof buf);
742 /* Hopefully this indicates the qemu child process has died. */
744 fprintf (stderr, "stdout_event: %p: child process died\n", g);
745 /*kill (g->pid, SIGTERM);*/
746 waitpid (g->pid, NULL, 0);
747 if (g->stdout_watch >= 0)
748 main_loop.remove_handle (g, g->stdout_watch);
749 if (g->sock_watch >= 0)
750 main_loop.remove_handle (g, g->sock_watch);
759 g->stdout_watch = -1;
762 if (g->subprocess_quit_cb)
763 g->subprocess_quit_cb (g, g->subprocess_quit_cb_data);
773 /* In verbose mode, copy all log messages to stderr. */
777 /* It's an actual log message, send it upwards if anyone is listening. */
778 if (g->log_message_cb)
779 g->log_message_cb (g, g->log_message_cb_data, buf, n);
782 /* The function is called whenever we can read something on the
783 * guestfsd (daemon inside the guest) communication socket.
786 sock_read_event (void *data, int watch, int fd, int events)
788 guestfs_h *g = (guestfs_h *) data;
795 "sock_event: %p g->state = %d, fd = %d, events = 0x%x\n",
796 g, g->state, fd, events);
799 error (g, "sock_read_event: internal error: %d != %d", g->sock, fd);
803 if (g->msg_in_size <= g->msg_in_allocated) {
804 g->msg_in_allocated += 4096;
805 g->msg_in = safe_realloc (g, g->msg_in, g->msg_in_allocated);
807 n = read (g->sock, g->msg_in + g->msg_in_size,
808 g->msg_in_allocated - g->msg_in_size);
810 /* Disconnected? Ignore it because stdout_watch will get called
811 * and will do the cleanup.
823 /* Have we got enough of a message to be able to process it yet? */
824 if (g->msg_in_size < 4) return;
826 xdrmem_create (&xdr, g->msg_in, g->msg_in_size, XDR_DECODE);
827 if (!xdr_uint32_t (&xdr, &len)) {
828 error (g, "can't decode length word");
832 /* Length is normally the length of the message, but when guestfsd
833 * starts up it sends a "magic" value (longer than any possible
834 * message). Check for this.
836 if (len == 0xf5f5f5f5) {
837 if (g->state != LAUNCHING)
838 error (g, "received magic signature from guestfsd, but in state %d",
840 else if (g->msg_in_size != 4)
841 error (g, "received magic signature from guestfsd, but msg size is %d",
845 if (g->launch_done_cb_internal)
846 g->launch_done_cb_internal (g, g->launch_done_cb_internal_data);
847 if (g->launch_done_cb)
848 g->launch_done_cb (g, g->launch_done_cb_data);
854 if (g->msg_in_size < len) return; /* Need more of this message. */
856 /* This should not happen, and if it does it probably means we've
857 * lost all hope of synchronization.
859 if (g->msg_in_size > len) {
860 error (g, "len = %d, but msg_in_size = %d", len, g->msg_in_size);
864 /* Not in the expected state. */
865 if (g->state != BUSY)
866 error (g, "state %d != BUSY", g->state);
868 /* Push the message up to the higher layer. Note that unlike
869 * launch_done_cb / launch_done_cb_internal, we only call at
870 * most one of the callback functions here.
873 if (g->reply_cb_internal)
874 g->reply_cb_internal (g, g->reply_cb_internal_data, &xdr);
875 else if (g->reply_cb)
876 g->reply_cb (g, g->reply_cb, &xdr);
879 /* Free the message buffer if it's grown excessively large. */
880 if (g->msg_in_allocated > 65536) {
883 g->msg_in_size = g->msg_in_allocated = 0;
890 /* This is the default main loop implementation, using select(2). */
892 struct handle_cb_data {
893 guestfs_handle_event_cb cb;
900 static int select_init_done = 0;
901 static int max_fd = -1;
902 static int nr_fds = 0;
903 static struct handle_cb_data *handle_cb_data = NULL;
908 if (!select_init_done) {
913 select_init_done = 1;
918 select_add_handle (guestfs_h *g, int fd, int events,
919 guestfs_handle_event_cb cb, void *data)
923 if (fd < 0 || fd >= FD_SETSIZE) {
924 error (g, "fd %d is out of range", fd);
928 if ((events & ~(GUESTFS_HANDLE_READABLE |
929 GUESTFS_HANDLE_WRITABLE |
930 GUESTFS_HANDLE_HANGUP |
931 GUESTFS_HANDLE_ERROR)) != 0) {
932 error (g, "set of events (0x%x) contains unknown events", events);
937 error (g, "set of events is empty");
941 if (FD_ISSET (fd, &rset) || FD_ISSET (fd, &wset) || FD_ISSET (fd, &xset)) {
942 error (g, "fd %d is already registered", fd);
947 error (g, "callback is NULL");
951 if ((events & GUESTFS_HANDLE_READABLE))
953 if ((events & GUESTFS_HANDLE_WRITABLE))
955 if ((events & GUESTFS_HANDLE_HANGUP) || (events & GUESTFS_HANDLE_ERROR))
960 handle_cb_data = safe_realloc (g, handle_cb_data,
961 sizeof (struct handle_cb_data) * (max_fd+1));
963 handle_cb_data[fd].cb = cb;
964 handle_cb_data[fd].data = data;
968 /* Any integer >= 0 can be the handle, and this is as good as any ... */
973 select_remove_handle (guestfs_h *g, int fd)
977 if (fd < 0 || fd >= FD_SETSIZE) {
978 error (g, "fd %d is out of range", fd);
982 if (!FD_ISSET (fd, &rset) && !FD_ISSET (fd, &wset) && !FD_ISSET (fd, &xset)) {
983 error (g, "fd %d was not registered", fd);
993 handle_cb_data = safe_realloc (g, handle_cb_data,
994 sizeof (struct handle_cb_data) * (max_fd+1));
1003 select_add_timeout (guestfs_h *g, int interval,
1004 guestfs_handle_timeout_cb cb, void *data)
1008 abort (); /* XXX not implemented yet */
1012 select_remove_timeout (guestfs_h *g, int timer)
1016 abort (); /* XXX not implemented yet */
1019 /* Note that main loops can be nested. */
1020 static int level = 0;
1023 select_main_loop_run (guestfs_h *g)
1025 int old_level, fd, r, events;
1026 fd_set rset2, wset2, xset2;
1030 old_level = level++;
1031 while (level > old_level) {
1040 r = select (max_fd+1, &rset2, &wset2, &xset2, NULL);
1042 perrorf (g, "select");
1047 for (fd = 0; r > 0 && fd <= max_fd; ++fd) {
1049 if (FD_ISSET (fd, &rset2))
1050 events |= GUESTFS_HANDLE_READABLE;
1051 if (FD_ISSET (fd, &wset2))
1052 events |= GUESTFS_HANDLE_WRITABLE;
1053 if (FD_ISSET (fd, &xset2))
1054 events |= GUESTFS_HANDLE_ERROR | GUESTFS_HANDLE_HANGUP;
1057 handle_cb_data[fd].cb (handle_cb_data[fd].data,
1065 select_main_loop_quit (guestfs_h *g)
1070 error (g, "cannot quit, we are not in a main loop");