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>
32 void xwrite (int sock, const void *buf, size_t len);
34 static void usage (void);
36 /* Also in guestfs.c */
37 #define VMCHANNEL_PORT "6666"
38 #define VMCHANNEL_ADDR "10.0.2.4"
41 main (int argc, char *argv[])
43 static const char *options = "fh:p:?";
44 static struct option long_options[] = {
45 { "foreground", 0, 0, 'f' },
46 { "help", 0, 0, '?' },
47 { "host", 1, 0, 'h' },
48 { "port", 1, 0, 'p' },
53 const char *host = NULL;
54 const char *port = NULL;
59 struct addrinfo *res, *rr;
60 struct addrinfo hints;
65 c = getopt_long (argc, argv, options, long_options, NULL);
86 fprintf (stderr, "guestfsd: unexpected command line option 0x%x\n", c);
96 /* If host and port aren't set yet, try /proc/cmdline. */
98 fp = fopen ("/proc/cmdline", "r");
100 perror ("/proc/cmdline");
103 n = fread (buf, 1, sizeof buf - 1, fp);
107 p = strstr (buf, "guestfs=");
111 p2 = strchr (p, ':');
115 r = strcspn (p2, " \n");
123 /* Can't parse /proc/cmdline, so use built-in defaults. */
124 if (!host || !port) {
125 host = VMCHANNEL_ADDR;
126 port = VMCHANNEL_PORT;
129 /* Resolve the hostname. */
130 memset (&hints, 0, sizeof hints);
131 hints.ai_socktype = SOCK_STREAM;
132 hints.ai_flags = AI_ADDRCONFIG;
133 r = getaddrinfo (host, port, &hints, &res);
135 fprintf (stderr, "%s:%s: %s\n", host, port, gai_strerror (r));
139 /* Connect to the given TCP socket. */
141 for (rr = res; rr != NULL; rr = rr->ai_next) {
142 sock = socket (rr->ai_family, rr->ai_socktype, rr->ai_protocol);
144 if (connect (sock, rr->ai_addr, rr->ai_addrlen) == 0)
155 fprintf (stderr, "connection to %s:%s failed\n", host, port);
159 /* Send the magic length message which indicates that
160 * userspace is up inside the guest.
163 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
164 if (!xdr_uint32_t (&xdr, &len)) {
165 fprintf (stderr, "xdr_uint32_t failed\n");
169 xwrite (sock, buf, xdr_getpos (&xdr));
173 /* XXX Fork into the background. */
192 xwrite (int sock, const void *buf, size_t len)
197 r = write (sock, buf, len);
208 xread (int sock, void *buf, size_t len)
213 r = read (sock, buf, len);
219 fprintf (stderr, "read: unexpected end of file on comms socket\n");
230 fprintf (stderr, "guestfsd [-f] [-h host -p port]\n");
233 /* Some unimplemented actions. */
235 do_mount (const char *device, const char *mountpoint)
237 reply_with_error ("mount not implemented");
242 do_touch (const char *path)
244 reply_with_error ("touch not implemented");