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"
45 main (int argc, char *argv[])
47 static const char *options = "fh:p:?";
48 static struct option long_options[] = {
49 { "foreground", 0, 0, 'f' },
50 { "help", 0, 0, '?' },
51 { "host", 1, 0, 'h' },
52 { "port", 1, 0, 'p' },
57 const char *host = NULL;
58 const char *port = NULL;
63 struct addrinfo *res, *rr;
64 struct addrinfo hints;
69 c = getopt_long (argc, argv, options, long_options, NULL);
90 fprintf (stderr, "guestfsd: unexpected command line option 0x%x\n", c);
100 /* If host and port aren't set yet, try /proc/cmdline. */
101 if (!host || !port) {
102 fp = fopen ("/proc/cmdline", "r");
104 perror ("/proc/cmdline");
107 n = fread (buf, 1, sizeof buf - 1, fp);
111 p = strstr (buf, "guestfs=");
115 p2 = strchr (p, ':');
119 r = strcspn (p2, " \n");
127 /* Can't parse /proc/cmdline, so use built-in defaults. */
128 if (!host || !port) {
129 host = VMCHANNEL_ADDR;
130 port = VMCHANNEL_PORT;
133 /* Resolve the hostname. */
134 memset (&hints, 0, sizeof hints);
135 hints.ai_socktype = SOCK_STREAM;
136 hints.ai_flags = AI_ADDRCONFIG;
137 r = getaddrinfo (host, port, &hints, &res);
139 fprintf (stderr, "%s:%s: %s\n", host, port, gai_strerror (r));
143 /* Connect to the given TCP socket. */
145 for (rr = res; rr != NULL; rr = rr->ai_next) {
146 sock = socket (rr->ai_family, rr->ai_socktype, rr->ai_protocol);
148 if (connect (sock, rr->ai_addr, rr->ai_addrlen) == 0)
159 fprintf (stderr, "connection to %s:%s failed\n", host, port);
163 /* Send the magic length message which indicates that
164 * userspace is up inside the guest.
167 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
168 if (!xdr_uint32_t (&xdr, &len)) {
169 fprintf (stderr, "xdr_uint32_t failed\n");
173 xwrite (sock, buf, xdr_getpos (&xdr));
177 /* Fork into the background. */
179 if (daemon (0, 1) == -1) {
185 /* Enter the main loop, reading and performing actions. */
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");
234 add_string (char ***argv, int *size, int *alloc, const char *str)
239 if (*size >= *alloc) {
241 new_argv = realloc (*argv, *alloc * sizeof (char *));
242 if (new_argv == NULL) {
243 reply_with_perror ("realloc");
244 free_strings (*argv);
251 new_str = strdup (str);
252 if (new_str == NULL) {
253 reply_with_perror ("strdup");
254 free_strings (*argv);
259 (*argv)[*size] = new_str;
266 count_strings (char **argv)
270 for (argc = 0; argv[argc] != NULL; ++argc)
276 compare (const void *vp1, const void *vp2)
278 char * const *p1 = (char * const *) vp1;
279 char * const *p2 = (char * const *) vp2;
280 return strcmp (*p1, *p2);
284 sort_strings (char **argv, int len)
286 qsort (argv, len, sizeof (char *), compare);
290 free_strings (char **argv)
294 for (argc = 0; argv[argc] != NULL; ++argc)
300 free_stringslen (char **argv, int len)
304 for (i = 0; i < len; ++i)
309 /* This is a more sane version of 'system(3)' for running external
310 * commands. It uses fork/execvp, so we don't need to worry about
311 * quoting of parameters, and it allows us to capture any error
312 * messages in a buffer.
315 command (char **stdoutput, char **stderror, const char *name, ...)
317 int so_size = 0, se_size = 0;
318 int so_fd[2], se_fd[2];
323 if (stdoutput) *stdoutput = NULL;
324 if (stderror) *stderror = NULL;
326 if (pipe (so_fd) == -1 || pipe (se_fd) == -1) {
337 if (pid == 0) { /* Child process. */
343 /* Collect the command line arguments into an array. */
344 va_start (args, name);
347 argv = malloc (sizeof (char *) * i);
348 argv[0] = (char *) name;
351 while ((s = va_arg (args, char *)) != NULL) {
352 argv = realloc (argv, sizeof (char *) * (++i));
370 /* Parent process. */
375 FD_SET (so_fd[0], &rset);
376 FD_SET (se_fd[0], &rset);
381 r = select (MAX (so_fd[0], se_fd[0]) + 1, &rset2, NULL, NULL, NULL);
384 waitpid (pid, NULL, 0);
388 if (FD_ISSET (so_fd[0], &rset2)) { /* something on stdout */
389 r = read (so_fd[0], buf, sizeof buf);
392 waitpid (pid, NULL, 0);
395 if (r == 0) quit = 1;
397 if (r > 0 && stdoutput) {
399 *stdoutput = realloc (*stdoutput, so_size);
400 if (*stdoutput == NULL) {
405 memcpy (*stdoutput + so_size - r, buf, r);
409 if (FD_ISSET (se_fd[0], &rset2)) { /* something on stderr */
410 r = read (se_fd[0], buf, sizeof buf);
413 waitpid (pid, NULL, 0);
416 if (r == 0) quit = 1;
418 if (r > 0 && stderror) {
420 *stderror = realloc (*stderror, se_size);
421 if (*stderror == NULL) {
426 memcpy (*stderror + se_size - r, buf, r);
431 /* Make sure the output buffers are \0-terminated. Also remove any
432 * trailing \n characters from the error buffer (not from stdout).
435 *stdoutput = realloc (*stdoutput, so_size+1);
436 if (*stdoutput == NULL) {
440 (*stdoutput)[so_size] = '\0';
443 *stderror = realloc (*stderror, se_size+1);
444 if (*stderror == NULL) {
448 (*stderror)[se_size] = '\0';
450 while (se_size >= 0 && (*stderror)[se_size] == '\n')
451 (*stderror)[se_size--] = '\0';
455 /* Get the exit status of the command. */
456 waitpid (pid, &r, 0);
459 if (WEXITSTATUS (r) == 0)