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 static void usage (void);
40 /* Also in guestfs.c */
41 #define VMCHANNEL_PORT "6666"
42 #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 /* Set the verbose flag. Not quite right because this will only
114 * set the flag if host and port aren't set on the command line.
115 * Don't worry about this for now. (XXX)
117 verbose = strstr (buf, "guestfs_verbose=1") != NULL;
119 printf ("verbose daemon enabled\n");
121 p = strstr (buf, "guestfs=");
125 p2 = strchr (p, ':');
129 r = strcspn (p2, " \n");
137 /* Can't parse /proc/cmdline, so use built-in defaults. */
138 if (!host || !port) {
139 host = VMCHANNEL_ADDR;
140 port = VMCHANNEL_PORT;
143 /* Resolve the hostname. */
144 memset (&hints, 0, sizeof hints);
145 hints.ai_socktype = SOCK_STREAM;
146 hints.ai_flags = AI_ADDRCONFIG;
147 r = getaddrinfo (host, port, &hints, &res);
149 fprintf (stderr, "%s:%s: %s\n", host, port, gai_strerror (r));
153 /* Connect to the given TCP socket. */
155 for (rr = res; rr != NULL; rr = rr->ai_next) {
156 sock = socket (rr->ai_family, rr->ai_socktype, rr->ai_protocol);
158 if (connect (sock, rr->ai_addr, rr->ai_addrlen) == 0)
169 fprintf (stderr, "connection to %s:%s failed\n", host, port);
173 /* Send the magic length message which indicates that
174 * userspace is up inside the guest.
176 len = GUESTFS_LAUNCH_FLAG;
177 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
178 if (!xdr_uint32_t (&xdr, &len)) {
179 fprintf (stderr, "xdr_uint32_t failed\n");
183 (void) xwrite (sock, buf, xdr_getpos (&xdr));
187 /* Fork into the background. */
189 if (daemon (0, 1) == -1) {
195 /* Enter the main loop, reading and performing actions. */
202 xwrite (int sock, const void *buf, size_t len)
207 r = write (sock, buf, len);
220 xread (int sock, void *buf, size_t len)
225 r = read (sock, buf, len);
231 fprintf (stderr, "read: unexpected end of file on fd %d\n", sock);
244 fprintf (stderr, "guestfsd [-f] [-h host -p port]\n");
248 add_string (char ***argv, int *size, int *alloc, const char *str)
253 if (*size >= *alloc) {
255 new_argv = realloc (*argv, *alloc * sizeof (char *));
256 if (new_argv == NULL) {
257 reply_with_perror ("realloc");
258 free_strings (*argv);
265 new_str = strdup (str);
266 if (new_str == NULL) {
267 reply_with_perror ("strdup");
268 free_strings (*argv);
273 (*argv)[*size] = new_str;
280 count_strings (char * const* const argv)
284 for (argc = 0; argv[argc] != NULL; ++argc)
290 compare (const void *vp1, const void *vp2)
292 char * const *p1 = (char * const *) vp1;
293 char * const *p2 = (char * const *) vp2;
294 return strcmp (*p1, *p2);
298 sort_strings (char **argv, int len)
300 qsort (argv, len, sizeof (char *), compare);
304 free_strings (char **argv)
308 for (argc = 0; argv[argc] != NULL; ++argc)
314 free_stringslen (char **argv, int len)
318 for (i = 0; i < len; ++i)
323 /* This is a more sane version of 'system(3)' for running external
324 * commands. It uses fork/execvp, so we don't need to worry about
325 * quoting of parameters, and it allows us to capture any error
326 * messages in a buffer.
329 command (char **stdoutput, char **stderror, const char *name, ...)
336 /* Collect the command line arguments into an array. */
337 va_start (args, name);
340 argv = malloc (sizeof (char *) * i);
341 argv[0] = (char *) name;
344 while ((s = va_arg (args, char *)) != NULL) {
345 argv = realloc (argv, sizeof (char *) * (++i));
352 r = commandv (stdoutput, stderror, argv);
354 /* NB: Mustn't free the strings which are on the stack. */
361 commandv (char **stdoutput, char **stderror, char * const* const argv)
363 int so_size = 0, se_size = 0;
364 int so_fd[2], se_fd[2];
369 if (stdoutput) *stdoutput = NULL;
370 if (stderror) *stderror = NULL;
373 printf ("%s", argv[0]);
374 for (i = 1; argv[i] != NULL; ++i)
375 printf (" %s", argv[i]);
379 if (pipe (so_fd) == -1 || pipe (se_fd) == -1) {
394 if (pid == 0) { /* Child process. */
403 execvp (argv[0], argv);
408 /* Parent process. */
413 FD_SET (so_fd[0], &rset);
414 FD_SET (se_fd[0], &rset);
419 r = select (MAX (so_fd[0], se_fd[0]) + 1, &rset2, NULL, NULL, NULL);
424 waitpid (pid, NULL, 0);
428 if (FD_ISSET (so_fd[0], &rset2)) { /* something on stdout */
429 r = read (so_fd[0], buf, sizeof buf);
434 waitpid (pid, NULL, 0);
437 if (r == 0) { FD_CLR (so_fd[0], &rset); quit++; }
439 if (r > 0 && stdoutput) {
441 *stdoutput = realloc (*stdoutput, so_size);
442 if (*stdoutput == NULL) {
447 memcpy (*stdoutput + so_size - r, buf, r);
451 if (FD_ISSET (se_fd[0], &rset2)) { /* something on stderr */
452 r = read (se_fd[0], buf, sizeof buf);
457 waitpid (pid, NULL, 0);
460 if (r == 0) { FD_CLR (se_fd[0], &rset); quit++; }
462 if (r > 0 && stderror) {
464 *stderror = realloc (*stderror, se_size);
465 if (*stderror == NULL) {
470 memcpy (*stderror + se_size - r, buf, r);
478 /* Make sure the output buffers are \0-terminated. Also remove any
479 * trailing \n characters from the error buffer (not from stdout).
482 *stdoutput = realloc (*stdoutput, so_size+1);
483 if (*stdoutput == NULL) {
487 (*stdoutput)[so_size] = '\0';
490 *stderror = realloc (*stderror, se_size+1);
491 if (*stderror == NULL) {
495 (*stderror)[se_size] = '\0';
497 while (se_size >= 0 && (*stderror)[se_size] == '\n')
498 (*stderror)[se_size--] = '\0';
502 /* Get the exit status of the command. */
503 waitpid (pid, &r, 0);
506 if (WEXITSTATUS (r) == 0)