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.
28 #include <sys/param.h> /* defines MIN */
29 #include <rpc/types.h>
33 #include "../src/guestfs_protocol.h"
35 /* The message currently being processed. */
39 /* The daemon communications socket. */
49 struct guestfs_message_header hdr;
55 /* Most common errors are leaked memory and leaked file descriptors,
56 * so run this between each command:
59 system ("ls -l /proc/self/fd");
62 /* Read the length word. */
63 xread (sock, lenbuf, 4);
64 xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
65 xdr_uint32_t (&xdr, &len);
68 if (len > GUESTFS_MESSAGE_MAX) {
69 fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n",
76 reply_with_perror ("malloc");
80 xread (sock, buf, len);
86 for (i = 0; i < len; i += 16) {
88 for (j = i; j < MIN (i+16, len); ++j)
89 printf ("%02x ", (unsigned char) buf[j]);
93 for (j = i; j < MIN (i+16, len); ++j)
95 printf ("%c", buf[j]);
105 /* Decode the message header. */
106 xdrmem_create (&xdr, buf, len, XDR_DECODE);
107 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
108 fprintf (stderr, "guestfsd: could not decode message header\n");
112 /* Check the version etc. */
113 if (hdr.prog != GUESTFS_PROGRAM) {
114 reply_with_error ("wrong program (%d)", hdr.prog);
117 if (hdr.vers != GUESTFS_PROTOCOL_VERSION) {
118 reply_with_error ("wrong protocol version (%d)", hdr.vers);
121 if (hdr.direction != GUESTFS_DIRECTION_CALL) {
122 reply_with_error ("unexpected message direction (%d)", hdr.direction);
125 if (hdr.status != GUESTFS_STATUS_OK) {
126 reply_with_error ("unexpected message status (%d)", hdr.status);
130 /* Now start to process this message. */
133 dispatch_incoming_message (&xdr);
134 /* Note that dispatch_incoming_message will also send a reply. */
142 static void send_error (const char *msg);
145 reply_with_error (const char *fs, ...)
147 char err[GUESTFS_ERROR_LEN];
151 vsnprintf (err, sizeof err, fs, args);
158 reply_with_perror (const char *fs, ...)
160 char buf1[GUESTFS_ERROR_LEN];
161 char buf2[GUESTFS_ERROR_LEN];
166 vsnprintf (buf1, sizeof buf1, fs, args);
169 snprintf (buf2, sizeof buf2, "%s: %s", buf1, strerror (err));
175 send_error (const char *msg)
178 char buf[GUESTFS_ERROR_LEN + 200];
180 struct guestfs_message_header hdr;
181 struct guestfs_message_error err;
184 fprintf (stderr, "guestfsd: error: %s\n", msg);
186 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
188 hdr.prog = GUESTFS_PROGRAM;
189 hdr.vers = GUESTFS_PROTOCOL_VERSION;
190 hdr.direction = GUESTFS_DIRECTION_REPLY;
191 hdr.status = GUESTFS_STATUS_ERROR;
195 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
196 fprintf (stderr, "guestfsd: failed to encode error message header\n");
200 err.error_message = (char *) msg;
202 if (!xdr_guestfs_message_error (&xdr, &err)) {
203 fprintf (stderr, "guestfsd: failed to encode error message body\n");
207 len = xdr_getpos (&xdr);
210 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
211 xdr_uint32_t (&xdr, &len);
214 if (xwrite (sock, lenbuf, 4) == -1) {
215 fprintf (stderr, "xwrite failed\n");
218 if (xwrite (sock, buf, len) == -1) {
219 fprintf (stderr, "xwrite failed\n");
225 reply (xdrproc_t xdrp, char *ret)
228 char buf[GUESTFS_MESSAGE_MAX];
230 struct guestfs_message_header hdr;
233 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
235 hdr.prog = GUESTFS_PROGRAM;
236 hdr.vers = GUESTFS_PROTOCOL_VERSION;
237 hdr.direction = GUESTFS_DIRECTION_REPLY;
238 hdr.status = GUESTFS_STATUS_OK;
242 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
243 fprintf (stderr, "guestfsd: failed to encode reply header\n");
248 if (!(*xdrp) (&xdr, ret)) {
249 fprintf (stderr, "guestfsd: failed to encode reply body\n");
254 len = xdr_getpos (&xdr);
257 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
258 xdr_uint32_t (&xdr, &len);
261 if (xwrite (sock, lenbuf, 4) == -1) {
262 fprintf (stderr, "xwrite failed\n");
265 if (xwrite (sock, buf, len) == len) {
266 fprintf (stderr, "xwrite failed\n");
271 /* Receive file chunks, repeatedly calling 'cb'. */
273 receive_file (receive_cb cb, void *opaque)
283 /* Read the length word. */
284 xread (sock, lenbuf, 4);
285 xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
286 xdr_uint32_t (&xdr, &len);
289 if (len == GUESTFS_CANCEL_FLAG)
290 continue; /* Just ignore it. */
292 if (len > GUESTFS_MESSAGE_MAX) {
293 fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n",
304 xread (sock, buf, len);
306 xdrmem_create (&xdr, buf, len, XDR_DECODE);
307 memset (&chunk, 0, sizeof chunk);
308 if (!xdr_guestfs_chunk (&xdr, &chunk)) {
317 printf ("receive_file: got chunk: cancel = %d, len = %d, buf = %p\n",
318 chunk.cancel, chunk.data.data_len, chunk.data.data_val);
321 fprintf (stderr, "receive_file: received cancellation from library\n");
322 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
325 if (chunk.data.data_len == 0) {
326 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
327 return 0; /* end of file */
331 r = cb (opaque, chunk.data.data_val, chunk.data.data_len);
335 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
336 if (r == -1) /* write error */
341 /* Send a cancellation flag back to the library. */
343 cancel_receive (void)
347 uint32_t flag = GUESTFS_CANCEL_FLAG;
349 xdrmem_create (&xdr, fbuf, sizeof fbuf, XDR_ENCODE);
350 xdr_uint32_t (&xdr, &flag);
353 if (xwrite (sock, fbuf, sizeof fbuf) == -1) {
354 perror ("write to socket");
358 /* Keep receiving chunks and discarding, until library sees cancel. */
359 (void) receive_file (NULL, NULL);
362 static int check_for_library_cancellation (void);
363 static int send_chunk (const guestfs_chunk *);
365 /* Also check if the library sends us a cancellation message. */
367 send_file_write (const void *buf, int len)
372 if (len > GUESTFS_MAX_CHUNK_SIZE) {
373 fprintf (stderr, "send_file_write: len (%d) > GUESTFS_MAX_CHUNK_SIZE (%d)\n",
374 len, GUESTFS_MAX_CHUNK_SIZE);
378 cancel = check_for_library_cancellation ();
382 chunk.data.data_len = 0;
383 chunk.data.data_val = NULL;
386 chunk.data.data_len = len;
387 chunk.data.data_val = (char *) buf;
390 if (send_chunk (&chunk) == -1)
393 if (cancel) return -2;
398 check_for_library_cancellation (void)
408 FD_SET (sock, &rset);
411 r = select (sock+1, &rset, NULL, NULL, &tv);
419 /* Read the message from the daemon. */
420 r = xread (sock, buf, sizeof buf);
426 xdrmem_create (&xdr, buf, sizeof buf, XDR_DECODE);
427 xdr_uint32_t (&xdr, &flag);
430 if (flag != GUESTFS_CANCEL_FLAG) {
431 fprintf (stderr, "check_for_library_cancellation: read 0x%x from library, expected 0x%x\n",
432 flag, GUESTFS_CANCEL_FLAG);
440 send_file_end (int cancel)
444 chunk.cancel = cancel;
445 chunk.data.data_len = 0;
446 chunk.data.data_val = NULL;
451 send_chunk (const guestfs_chunk *chunk)
453 char buf[GUESTFS_MAX_CHUNK_SIZE + 48];
458 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
459 if (!xdr_guestfs_chunk (&xdr, (guestfs_chunk *) chunk)) {
460 fprintf (stderr, "send_chunk: failed to encode chunk\n");
465 len = xdr_getpos (&xdr);
468 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
469 xdr_uint32_t (&xdr, &len);
472 (void) xwrite (sock, lenbuf, 4);
473 (void) xwrite (sock, buf, len);