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;
50 struct timeval start_t, end_t;
51 int64_t start_us, end_us, elapsed_us;
57 /* Most common errors are leaked memory and leaked file descriptors,
58 * so run this between each command:
61 system ("ls -l /proc/self/fd");
64 /* Read the length word. */
65 xread (sock, lenbuf, 4);
66 xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
67 xdr_uint32_t (&xdr, &len);
70 if (len > GUESTFS_MESSAGE_MAX) {
71 fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n",
78 reply_with_perror ("malloc");
82 xread (sock, buf, len);
88 for (i = 0; i < len; i += 16) {
90 for (j = i; j < MIN (i+16, len); ++j)
91 printf ("%02x ", (unsigned char) buf[j]);
95 for (j = i; j < MIN (i+16, len); ++j)
97 printf ("%c", buf[j]);
100 for (; j < i+16; ++j)
107 /* In verbose mode, display the time taken to run each command. */
109 gettimeofday (&start_t, NULL);
111 /* Decode the message header. */
112 xdrmem_create (&xdr, buf, len, XDR_DECODE);
113 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
114 fprintf (stderr, "guestfsd: could not decode message header\n");
118 /* Check the version etc. */
119 if (hdr.prog != GUESTFS_PROGRAM) {
120 reply_with_error ("wrong program (%d)", hdr.prog);
123 if (hdr.vers != GUESTFS_PROTOCOL_VERSION) {
124 reply_with_error ("wrong protocol version (%d)", hdr.vers);
127 if (hdr.direction != GUESTFS_DIRECTION_CALL) {
128 reply_with_error ("unexpected message direction (%d)", hdr.direction);
131 if (hdr.status != GUESTFS_STATUS_OK) {
132 reply_with_error ("unexpected message status (%d)", hdr.status);
136 /* Now start to process this message. */
139 dispatch_incoming_message (&xdr);
140 /* Note that dispatch_incoming_message will also send a reply. */
142 /* In verbose mode, display the time taken to run each command. */
144 gettimeofday (&end_t, NULL);
146 start_us = (int64_t) start_t.tv_sec * 1000000 + start_t.tv_usec;
147 end_us = (int64_t) end_t.tv_sec * 1000000 + end_t.tv_usec;
148 elapsed_us = end_us - start_us;
149 fprintf (stderr, "proc %d (%s) took %d.%02d seconds\n",
151 proc_nr >= 0 && proc_nr < GUESTFS_PROC_NR_PROCS
152 ? function_names[proc_nr] : "UNKNOWN PROCEDURE",
153 (int) (elapsed_us / 1000000),
154 (int) ((elapsed_us / 10000) % 100));
163 static void send_error (const char *msg);
166 reply_with_error (const char *fs, ...)
168 char err[GUESTFS_ERROR_LEN];
172 vsnprintf (err, sizeof err, fs, args);
179 reply_with_perror (const char *fs, ...)
181 char buf1[GUESTFS_ERROR_LEN];
182 char buf2[GUESTFS_ERROR_LEN];
187 vsnprintf (buf1, sizeof buf1, fs, args);
190 snprintf (buf2, sizeof buf2, "%s: %s", buf1, strerror (err));
196 send_error (const char *msg)
199 char buf[GUESTFS_ERROR_LEN + 200];
201 struct guestfs_message_header hdr;
202 struct guestfs_message_error err;
205 fprintf (stderr, "guestfsd: error: %s\n", msg);
207 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
209 hdr.prog = GUESTFS_PROGRAM;
210 hdr.vers = GUESTFS_PROTOCOL_VERSION;
211 hdr.direction = GUESTFS_DIRECTION_REPLY;
212 hdr.status = GUESTFS_STATUS_ERROR;
216 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
217 fprintf (stderr, "guestfsd: failed to encode error message header\n");
221 err.error_message = (char *) msg;
223 if (!xdr_guestfs_message_error (&xdr, &err)) {
224 fprintf (stderr, "guestfsd: failed to encode error message body\n");
228 len = xdr_getpos (&xdr);
231 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
232 xdr_uint32_t (&xdr, &len);
235 if (xwrite (sock, lenbuf, 4) == -1) {
236 fprintf (stderr, "xwrite failed\n");
239 if (xwrite (sock, buf, len) == -1) {
240 fprintf (stderr, "xwrite failed\n");
246 reply (xdrproc_t xdrp, char *ret)
249 char buf[GUESTFS_MESSAGE_MAX];
251 struct guestfs_message_header hdr;
254 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
256 hdr.prog = GUESTFS_PROGRAM;
257 hdr.vers = GUESTFS_PROTOCOL_VERSION;
258 hdr.direction = GUESTFS_DIRECTION_REPLY;
259 hdr.status = GUESTFS_STATUS_OK;
263 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
264 fprintf (stderr, "guestfsd: failed to encode reply header\n");
269 /* This can fail if the reply body is too large, for example
270 * if it exceeds the maximum message size. In that case
271 * we want to return an error message instead. (RHBZ#509597).
273 if (!(*xdrp) (&xdr, ret)) {
274 reply_with_perror ("guestfsd: failed to encode reply body\n");
280 len = xdr_getpos (&xdr);
283 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
284 xdr_uint32_t (&xdr, &len);
287 if (xwrite (sock, lenbuf, 4) == -1) {
288 fprintf (stderr, "xwrite failed\n");
291 if (xwrite (sock, buf, len) == len) {
292 fprintf (stderr, "xwrite failed\n");
297 /* Receive file chunks, repeatedly calling 'cb'. */
299 receive_file (receive_cb cb, void *opaque)
309 /* Read the length word. */
310 xread (sock, lenbuf, 4);
311 xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
312 xdr_uint32_t (&xdr, &len);
315 if (len == GUESTFS_CANCEL_FLAG)
316 continue; /* Just ignore it. */
318 if (len > GUESTFS_MESSAGE_MAX) {
319 fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n",
330 xread (sock, buf, len);
332 xdrmem_create (&xdr, buf, len, XDR_DECODE);
333 memset (&chunk, 0, sizeof chunk);
334 if (!xdr_guestfs_chunk (&xdr, &chunk)) {
343 printf ("receive_file: got chunk: cancel = %d, len = %d, buf = %p\n",
344 chunk.cancel, chunk.data.data_len, chunk.data.data_val);
347 fprintf (stderr, "receive_file: received cancellation from library\n");
348 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
351 if (chunk.data.data_len == 0) {
352 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
353 return 0; /* end of file */
357 r = cb (opaque, chunk.data.data_val, chunk.data.data_len);
361 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
362 if (r == -1) /* write error */
367 /* Send a cancellation flag back to the library. */
369 cancel_receive (void)
373 uint32_t flag = GUESTFS_CANCEL_FLAG;
375 xdrmem_create (&xdr, fbuf, sizeof fbuf, XDR_ENCODE);
376 xdr_uint32_t (&xdr, &flag);
379 if (xwrite (sock, fbuf, sizeof fbuf) == -1) {
380 perror ("write to socket");
384 /* Keep receiving chunks and discarding, until library sees cancel. */
385 (void) receive_file (NULL, NULL);
388 static int check_for_library_cancellation (void);
389 static int send_chunk (const guestfs_chunk *);
391 /* Also check if the library sends us a cancellation message. */
393 send_file_write (const void *buf, int len)
398 if (len > GUESTFS_MAX_CHUNK_SIZE) {
399 fprintf (stderr, "send_file_write: len (%d) > GUESTFS_MAX_CHUNK_SIZE (%d)\n",
400 len, GUESTFS_MAX_CHUNK_SIZE);
404 cancel = check_for_library_cancellation ();
408 chunk.data.data_len = 0;
409 chunk.data.data_val = NULL;
412 chunk.data.data_len = len;
413 chunk.data.data_val = (char *) buf;
416 if (send_chunk (&chunk) == -1)
419 if (cancel) return -2;
424 check_for_library_cancellation (void)
434 FD_SET (sock, &rset);
437 r = select (sock+1, &rset, NULL, NULL, &tv);
445 /* Read the message from the daemon. */
446 r = xread (sock, buf, sizeof buf);
452 xdrmem_create (&xdr, buf, sizeof buf, XDR_DECODE);
453 xdr_uint32_t (&xdr, &flag);
456 if (flag != GUESTFS_CANCEL_FLAG) {
457 fprintf (stderr, "check_for_library_cancellation: read 0x%x from library, expected 0x%x\n",
458 flag, GUESTFS_CANCEL_FLAG);
466 send_file_end (int cancel)
470 chunk.cancel = cancel;
471 chunk.data.data_len = 0;
472 chunk.data.data_val = NULL;
477 send_chunk (const guestfs_chunk *chunk)
479 char buf[GUESTFS_MAX_CHUNK_SIZE + 48];
484 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
485 if (!xdr_guestfs_chunk (&xdr, (guestfs_chunk *) chunk)) {
486 fprintf (stderr, "send_chunk: failed to encode chunk\n");
491 len = xdr_getpos (&xdr);
494 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
495 xdr_uint32_t (&xdr, &len);
498 (void) xwrite (sock, lenbuf, 4);
499 (void) xwrite (sock, buf, len);