1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
/* 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 <sys/socket.h>
#include <sys/un.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.
*/
static int
launch_unix (guestfs_h *g, const char *sockpath)
{
int r;
struct sockaddr_un addr;
if (g->qemu_params) {
error (g, _("cannot set qemu parameters with the 'unix:' attach method"));
return -1;
}
/* Set these to nothing so we don't try to read from random file
* descriptors.
*/
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;
}
static int
shutdown_unix (guestfs_h *g)
{
/* Merely closing g->sock is sufficient and that is already done
* in the calling code.
*/
return 0;
}
struct attach_ops attach_ops_unix = {
.launch = launch_unix,
.shutdown = shutdown_unix,
};
|