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.
21 #define _BSD_SOURCE /* for daemon(3) */
27 #include <rpc/types.h>
31 #include <sys/param.h>
32 #include <sys/select.h>
33 #include <sys/types.h>
38 void xwrite (int sock, const void *buf, size_t len);
40 static void usage (void);
42 /* Also in guestfs.c */
43 #define VMCHANNEL_PORT "6666"
44 #define VMCHANNEL_ADDR "10.0.2.4"
47 main (int argc, char *argv[])
49 static const char *options = "fh:p:?";
50 static struct option long_options[] = {
51 { "foreground", 0, 0, 'f' },
52 { "help", 0, 0, '?' },
53 { "host", 1, 0, 'h' },
54 { "port", 1, 0, 'p' },
59 const char *host = NULL;
60 const char *port = NULL;
65 struct addrinfo *res, *rr;
66 struct addrinfo hints;
71 c = getopt_long (argc, argv, options, long_options, NULL);
92 fprintf (stderr, "guestfsd: unexpected command line option 0x%x\n", c);
102 /* If host and port aren't set yet, try /proc/cmdline. */
103 if (!host || !port) {
104 fp = fopen ("/proc/cmdline", "r");
106 perror ("/proc/cmdline");
109 n = fread (buf, 1, sizeof buf - 1, fp);
113 p = strstr (buf, "guestfs=");
117 p2 = strchr (p, ':');
121 r = strcspn (p2, " \n");
129 /* Can't parse /proc/cmdline, so use built-in defaults. */
130 if (!host || !port) {
131 host = VMCHANNEL_ADDR;
132 port = VMCHANNEL_PORT;
135 /* Resolve the hostname. */
136 memset (&hints, 0, sizeof hints);
137 hints.ai_socktype = SOCK_STREAM;
138 hints.ai_flags = AI_ADDRCONFIG;
139 r = getaddrinfo (host, port, &hints, &res);
141 fprintf (stderr, "%s:%s: %s\n", host, port, gai_strerror (r));
145 /* Connect to the given TCP socket. */
147 for (rr = res; rr != NULL; rr = rr->ai_next) {
148 sock = socket (rr->ai_family, rr->ai_socktype, rr->ai_protocol);
150 if (connect (sock, rr->ai_addr, rr->ai_addrlen) == 0)
161 fprintf (stderr, "connection to %s:%s failed\n", host, port);
165 /* Send the magic length message which indicates that
166 * userspace is up inside the guest.
169 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
170 if (!xdr_uint32_t (&xdr, &len)) {
171 fprintf (stderr, "xdr_uint32_t failed\n");
175 xwrite (sock, buf, xdr_getpos (&xdr));
179 /* Fork into the background. */
181 if (daemon (0, 1) == -1) {
187 /* Enter the main loop, reading and performing actions. */
194 xwrite (int sock, const void *buf, size_t len)
199 r = write (sock, buf, len);
210 xread (int sock, void *buf, size_t len)
215 r = read (sock, buf, len);
221 fprintf (stderr, "read: unexpected end of file on comms socket\n");
232 fprintf (stderr, "guestfsd [-f] [-h host -p port]\n");
235 /* This is a more sane version of 'system(3)' for running external
236 * commands. It uses fork/execvp, so we don't need to worry about
237 * quoting of parameters, and it allows us to capture any error
238 * messages in a buffer.
241 command (char **stdoutput, char **stderror, const char *name, ...)
243 int so_size = 0, se_size = 0;
244 int so_fd[2], se_fd[2];
249 if (stdoutput) *stdoutput = NULL;
250 if (stderror) *stderror = NULL;
252 if (pipe (so_fd) == -1 || pipe (se_fd) == -1) {
263 if (pid == 0) { /* Child process. */
269 /* Collect the command line arguments into an array. */
270 va_start (args, name);
273 argv = malloc (sizeof (char *) * i);
274 argv[0] = (char *) name;
277 while ((s = va_arg (args, char *)) != NULL) {
278 argv = realloc (argv, sizeof (char *) * (++i));
296 /* Parent process. */
301 FD_SET (so_fd[0], &rset);
302 FD_SET (se_fd[0], &rset);
307 r = select (MAX (so_fd[0], se_fd[0]) + 1, &rset2, NULL, NULL, NULL);
310 waitpid (pid, NULL, 0);
314 if (FD_ISSET (so_fd[0], &rset2)) { /* something on stdout */
315 r = read (so_fd[0], buf, sizeof buf);
318 waitpid (pid, NULL, 0);
321 if (r == 0) quit = 1;
323 if (r > 0 && stdoutput) {
325 *stdoutput = realloc (*stdoutput, so_size);
326 if (*stdoutput == NULL) {
331 memcpy (*stdoutput + so_size - r, buf, r);
335 if (FD_ISSET (se_fd[0], &rset2)) { /* something on stderr */
336 r = read (se_fd[0], buf, sizeof buf);
339 waitpid (pid, NULL, 0);
342 if (r == 0) quit = 1;
344 if (r > 0 && stderror) {
346 *stderror = realloc (*stderror, se_size);
347 if (*stderror == NULL) {
352 memcpy (*stderror + se_size - r, buf, r);
357 /* Make sure the output buffers are \0-terminated. Also remove any
358 * trailing \n characters.
361 *stdoutput = realloc (*stdoutput, so_size+1);
362 if (*stdoutput == NULL) {
366 (*stdoutput)[so_size] = '\0';
368 while (so_size >= 0 && (*stdoutput)[so_size] == '\n')
369 (*stdoutput)[so_size--] = '\0';
373 *stderror = realloc (*stderror, se_size+1);
374 if (*stderror == NULL) {
378 (*stderror)[se_size] = '\0';
380 while (se_size >= 0 && (*stderror)[se_size] == '\n')
381 (*stderror)[se_size--] = '\0';
385 /* Get the exit status of the command. */
386 waitpid (pid, &r, 0);
389 if (WEXITSTATUS (r) == 0)