1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2009 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program 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
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <rpc/types.h>
30 static void xwrite (int sock, const void *buf, size_t len);
31 static void usage (void);
33 /* Also in guestfs.c */
34 #define VMCHANNEL_PORT "6666"
35 #define VMCHANNEL_ADDR "10.0.2.4"
38 main (int argc, char *argv[])
40 static const char *options = "fh:p:?";
41 static struct option long_options[] = {
42 { "foreground", 0, 0, 'f' },
43 { "help", 0, 0, '?' },
44 { "host", 1, 0, 'h' },
45 { "port", 1, 0, 'p' },
50 const char *host = NULL;
51 const char *port = NULL;
56 struct addrinfo *res, *rr;
57 struct addrinfo hints;
62 c = getopt_long (argc, argv, options, long_options, NULL);
83 fprintf (stderr, "guestfsd: unexpected command line option 0x%x\n", c);
93 /* If host and port aren't set yet, try /proc/cmdline. */
95 fp = fopen ("/proc/cmdline", "r");
97 perror ("/proc/cmdline");
100 n = fread (buf, 1, sizeof buf - 1, fp);
104 p = strstr (buf, "guestfs=");
108 p2 = strchr (p, ':');
112 r = strcspn (p2, " \n");
120 /* Can't parse /proc/cmdline, so use built-in defaults. */
121 if (!host || !port) {
122 host = VMCHANNEL_ADDR;
123 port = VMCHANNEL_PORT;
126 /* Resolve the hostname. */
127 memset (&hints, 0, sizeof hints);
128 hints.ai_socktype = SOCK_STREAM;
129 hints.ai_flags = AI_ADDRCONFIG;
130 r = getaddrinfo (host, port, &hints, &res);
132 fprintf (stderr, "%s:%s: %s\n", host, port, gai_strerror (r));
136 /* Connect to the given TCP socket. */
138 for (rr = res; rr != NULL; rr = rr->ai_next) {
139 sock = socket (rr->ai_family, rr->ai_socktype, rr->ai_protocol);
141 if (connect (sock, rr->ai_addr, rr->ai_addrlen) == 0)
152 fprintf (stderr, "connection to %s:%s failed\n", host, port);
156 /* Send the magic length message which indicates that
157 * userspace is up inside the guest.
160 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
161 if (!xdr_uint32_t (&xdr, &len)) {
162 fprintf (stderr, "xdr_uint32_t failed\n");
166 xwrite (sock, buf, xdr_getpos (&xdr));
170 /* XXX Fork into the background. */
186 xwrite (int sock, const void *buf, size_t len)
191 r = write (sock, buf, len);
204 fprintf (stderr, "guestfsd [-f] [-h host -p port]\n");