summaryrefslogtreecommitdiffstats
path: root/daemon/guestfsd.c
blob: c27d1b6e12082781e4e7b322a88f8a17775bb66f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* libguestfs - the guestfsd daemon
 * Copyright (C) 2009 Red Hat Inc. 
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <rpc/types.h>
#include <rpc/xdr.h>
#include <getopt.h>
#include <netdb.h>

static void xwrite (int sock, const void *buf, size_t len);
static void usage (void);

/* Also in guestfs.c */
#define VMCHANNEL_PORT "6666"
#define VMCHANNEL_ADDR "10.0.2.4"

int
main (int argc, char *argv[])
{
  static const char *options = "fh:p:?";
  static struct option long_options[] = {
    { "foreground", 0, 0, 'f' },
    { "help", 0, 0, '?' },
    { "host", 1, 0, 'h' },
    { "port", 1, 0, 'p' },
    { 0, 0, 0, 0 }
  };
  int c, n, r;
  int dont_fork = 0;
  const char *host = NULL;
  const char *port = NULL;
  FILE *fp;
  char buf[4096];
  char *p, *p2;
  int sock;
  struct addrinfo *res, *rr;
  struct addrinfo hints;
  XDR xdr;
  unsigned len;

  for (;;) {
    c = getopt_long (argc, argv, options, long_options, NULL);
    if (c == -1) break;

    switch (c) {
    case 'f':
      dont_fork = 1;
      break;

    case 'h':
      host = optarg;
      break;

    case 'p':
      port = optarg;
      break;

    case '?':
      usage ();
      exit (0);

    default:
      fprintf (stderr, "guestfsd: unexpected command line option 0x%x\n", c);
      exit (1);
    }
  }

  if (optind < argc) {
    usage ();
    exit (1);
  }

  /* If host and port aren't set yet, try /proc/cmdline. */
  if (!host || !port) {
    fp = fopen ("/proc/cmdline", "r");
    if (fp == NULL) {
      perror ("/proc/cmdline");
      goto next;
    }
    n = fread (buf, 1, sizeof buf - 1, fp);
    fclose (fp);
    buf[n] = '\0';

    p = strstr (buf, "guestfs=");

    if (p) {
      p += 8;
      p2 = strchr (p, ':');
      if (p2) {
	*p2++ = '\0';
	host = p;
	r = strcspn (p2, " \n");
	p2[r] = '\0';
	port = p2;
      }
    }
  }

 next:
  /* Can't parse /proc/cmdline, so use built-in defaults. */
  if (!host || !port) {
    host = VMCHANNEL_ADDR;
    port = VMCHANNEL_PORT;
  }

  /* Resolve the hostname. */
  memset (&hints, 0, sizeof hints);
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_ADDRCONFIG;
  r = getaddrinfo (host, port, &hints, &res);
  if (r != 0) {
    fprintf (stderr, "%s:%s: %s\n", host, port, gai_strerror (r));
    exit (1);
  }

  /* Connect to the given TCP socket. */
  sock = -1;
  for (rr = res; rr != NULL; rr = rr->ai_next) {
    sock = socket (rr->ai_family, rr->ai_socktype, rr->ai_protocol);
    if (sock != -1) {
      if (connect (sock, rr->ai_addr, rr->ai_addrlen) == 0)
	break;
      perror ("connect");

      close (sock);
      sock = -1;
    }
  }
  freeaddrinfo (res);

  if (sock == -1) {
    fprintf (stderr, "connection to %s:%s failed\n", host, port);
    exit (1);
  }

  /* Send the magic length message which indicates that
   * userspace is up inside the guest.
   */
  len = 0xf5f55ff5;
  xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
  if (!xdr_uint32_t (&xdr, &len)) {
    fprintf (stderr, "xdr_uint32_t failed\n");
    exit (1);
  }

  xwrite (sock, buf, xdr_getpos (&xdr));

  xdr_destroy (&xdr);

  /* XXX Fork into the background. */









  sleep (1000000);

  exit (0);
}

static void
xwrite (int sock, const void *buf, size_t len)
{
  int r;

  while (len > 0) {
    r = write (sock, buf, len);
    if (r == -1) {
      perror ("write");
      exit (1);
    }
    buf += r;
    len -= r;
  }
}

static void
usage (void)
{
  fprintf (stderr, "guestfsd [-f] [-h host -p port]\n");
}