diff options
author | Richard W.M. Jones <rjones@redhat.com> | 2012-07-19 17:08:51 +0100 |
---|---|---|
committer | Richard W.M. Jones <rjones@redhat.com> | 2012-07-19 17:49:25 +0100 |
commit | 4c261da5ba8a7679709bf6c8efefc2598c5f5f51 (patch) | |
tree | 026fbf8b9a79c2f961c3287d962c7159d51a4ec7 /src/launch-unix.c | |
parent | 80d102c49df440c64b744a14bf1d5f46a1c5b82b (diff) | |
download | libguestfs-4c261da5ba8a7679709bf6c8efefc2598c5f5f51.tar.gz libguestfs-4c261da5ba8a7679709bf6c8efefc2598c5f5f51.tar.xz libguestfs-4c261da5ba8a7679709bf6c8efefc2598c5f5f51.zip |
lib: Split launch.c into separate files.
launch-appliance.c contains the code associated with the 'appliance'
attach-method. Mostly. In fact there are a few APIs which don't fit
so nicely:
- config: deprecated API which fiddles with the qemu command
line directly
- max-disks: depends on the qemu implementation (virtio-scsi
or not)
- debug-drives: used for testing only
launch-unix.c contains the code associated with 'unix:<path>'.
launch.c is the common code for launching, along with a few other APIs
such as guestfs_add_drive_opts.
This commit also reduces the number of headers to just those
which are required.
Diffstat (limited to 'src/launch-unix.c')
-rw-r--r-- | src/launch-unix.c | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/src/launch-unix.c b/src/launch-unix.c new file mode 100644 index 00000000..a6384dfe --- /dev/null +++ b/src/launch-unix.c @@ -0,0 +1,101 @@ +/* libguestfs + * Copyright (C) 2009-2012 Red Hat Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <config.h> + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <fcntl.h> + +#include "guestfs.h" +#include "guestfs-internal.h" +#include "guestfs-internal-actions.h" +#include "guestfs_protocol.h" + +/* Alternate attach method: instead of launching the appliance, + * connect to an existing unix socket. + */ +int +guestfs___launch_unix (guestfs_h *g, const char *sockpath) +{ + int r; + struct sockaddr_un addr; + + /* Start the clock ... */ + gettimeofday (&g->launch_t, NULL); + + /* Set these to nothing so we don't try to kill random processes or + * read from random file descriptors. + */ + g->pid = 0; + g->recoverypid = 0; + g->fd[0] = -1; + g->fd[1] = -1; + + if (g->verbose) + guestfs___print_timestamped_message (g, "connecting to %s", sockpath); + + g->sock = socket (AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0); + if (g->sock == -1) { + perrorf (g, "socket"); + return -1; + } + + addr.sun_family = AF_UNIX; + strncpy (addr.sun_path, sockpath, UNIX_PATH_MAX); + addr.sun_path[UNIX_PATH_MAX-1] = '\0'; + + g->state = LAUNCHING; + + if (connect (g->sock, &addr, sizeof addr) == -1) { + perrorf (g, "bind"); + goto cleanup; + } + + if (fcntl (g->sock, F_SETFL, O_NONBLOCK) == -1) { + perrorf (g, "fcntl"); + goto cleanup; + } + + uint32_t size; + void *buf = NULL; + r = guestfs___recv_from_daemon (g, &size, &buf); + free (buf); + + if (r == -1) return -1; + + if (size != GUESTFS_LAUNCH_FLAG) { + error (g, _("guestfs_launch failed, unexpected initial message from guestfsd")); + goto cleanup; + } + + if (g->verbose) + guestfs___print_timestamped_message (g, "connected"); + + if (g->state != READY) { + error (g, _("contacted guestfsd, but state != READY")); + goto cleanup; + } + + return 0; + + cleanup: + close (g->sock); + return -1; +} |