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.
27 #include <sys/param.h> /* defines MIN */
28 #include <sys/select.h>
29 #include <rpc/types.h>
33 #include "ignore-value.h"
36 #include "../src/guestfs_protocol.h"
38 /* The message currently being processed. */
42 /* The daemon communications socket. */
52 struct guestfs_message_header hdr;
53 struct timeval start_t, end_t;
54 int64_t start_us, end_us, elapsed_us;
59 /* Most common errors are leaked memory and leaked file descriptors,
60 * so run this between each command:
63 ignore_value (system ("ls -l /proc/self/fd"));
65 /* Read the length word. */
66 if (xread (sock, lenbuf, 4) == -1)
69 xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
70 xdr_u_int (&xdr, &len);
73 if (len > GUESTFS_MESSAGE_MAX) {
74 fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n",
81 reply_with_perror ("malloc");
85 if (xread (sock, buf, len) == -1)
88 #ifdef ENABLE_PACKET_DUMP
92 for (i = 0; i < len; i += 16) {
93 printf ("%04zx: ", i);
94 for (j = i; j < MIN (i+16, len); ++j)
95 printf ("%02x ", (unsigned char) buf[j]);
99 for (j = i; j < MIN (i+16, len); ++j)
100 if (c_isprint (buf[j]))
101 printf ("%c", buf[j]);
104 for (; j < i+16; ++j)
111 /* In verbose mode, display the time taken to run each command. */
113 gettimeofday (&start_t, NULL);
115 /* Decode the message header. */
116 xdrmem_create (&xdr, buf, len, XDR_DECODE);
117 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
118 fprintf (stderr, "guestfsd: could not decode message header\n");
122 /* Check the version etc. */
123 if (hdr.prog != GUESTFS_PROGRAM) {
124 reply_with_error ("wrong program (%d)", hdr.prog);
127 if (hdr.vers != GUESTFS_PROTOCOL_VERSION) {
128 reply_with_error ("wrong protocol version (%d)", hdr.vers);
131 if (hdr.direction != GUESTFS_DIRECTION_CALL) {
132 reply_with_error ("unexpected message direction (%d)", hdr.direction);
135 if (hdr.status != GUESTFS_STATUS_OK) {
136 reply_with_error ("unexpected message status (%d)", hdr.status);
140 /* Now start to process this message. */
143 dispatch_incoming_message (&xdr);
144 /* Note that dispatch_incoming_message will also send a reply. */
146 /* In verbose mode, display the time taken to run each command. */
148 gettimeofday (&end_t, NULL);
150 start_us = (int64_t) start_t.tv_sec * 1000000 + start_t.tv_usec;
151 end_us = (int64_t) end_t.tv_sec * 1000000 + end_t.tv_usec;
152 elapsed_us = end_us - start_us;
153 fprintf (stderr, "proc %d (%s) took %d.%02d seconds\n",
155 proc_nr >= 0 && proc_nr < GUESTFS_PROC_NR_PROCS
156 ? function_names[proc_nr] : "UNKNOWN PROCEDURE",
157 (int) (elapsed_us / 1000000),
158 (int) ((elapsed_us / 10000) % 100));
167 static void send_error (const char *msg);
170 reply_with_error (const char *fs, ...)
172 char err[GUESTFS_ERROR_LEN];
176 vsnprintf (err, sizeof err, fs, args);
183 reply_with_perror (const char *fs, ...)
185 char buf1[GUESTFS_ERROR_LEN];
186 char buf2[GUESTFS_ERROR_LEN];
191 vsnprintf (buf1, sizeof buf1, fs, args);
194 snprintf (buf2, sizeof buf2, "%s: %s", buf1, strerror (err));
200 send_error (const char *msg)
203 char buf[GUESTFS_ERROR_LEN + 200];
205 struct guestfs_message_header hdr;
206 struct guestfs_message_error err;
209 fprintf (stderr, "guestfsd: error: %s\n", msg);
211 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
213 hdr.prog = GUESTFS_PROGRAM;
214 hdr.vers = GUESTFS_PROTOCOL_VERSION;
215 hdr.direction = GUESTFS_DIRECTION_REPLY;
216 hdr.status = GUESTFS_STATUS_ERROR;
220 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
221 fprintf (stderr, "guestfsd: failed to encode error message header\n");
225 err.error_message = (char *) msg;
227 if (!xdr_guestfs_message_error (&xdr, &err)) {
228 fprintf (stderr, "guestfsd: failed to encode error message body\n");
232 len = xdr_getpos (&xdr);
235 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
236 xdr_u_int (&xdr, &len);
239 if (xwrite (sock, lenbuf, 4) == -1) {
240 fprintf (stderr, "xwrite failed\n");
243 if (xwrite (sock, buf, len) == -1) {
244 fprintf (stderr, "xwrite failed\n");
250 reply (xdrproc_t xdrp, char *ret)
253 char buf[GUESTFS_MESSAGE_MAX];
255 struct guestfs_message_header hdr;
258 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
260 hdr.prog = GUESTFS_PROGRAM;
261 hdr.vers = GUESTFS_PROTOCOL_VERSION;
262 hdr.direction = GUESTFS_DIRECTION_REPLY;
263 hdr.status = GUESTFS_STATUS_OK;
267 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
268 fprintf (stderr, "guestfsd: failed to encode reply header\n");
273 /* This can fail if the reply body is too large, for example
274 * if it exceeds the maximum message size. In that case
275 * we want to return an error message instead. (RHBZ#509597).
277 if (!(*xdrp) (&xdr, ret)) {
278 reply_with_perror ("guestfsd: failed to encode reply body\n");
284 len = xdr_getpos (&xdr);
287 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
288 xdr_u_int (&xdr, &len);
291 if (xwrite (sock, lenbuf, 4) == -1) {
292 fprintf (stderr, "xwrite failed\n");
295 if (xwrite (sock, buf, len) == -1) {
296 fprintf (stderr, "xwrite failed\n");
301 /* Receive file chunks, repeatedly calling 'cb'. */
303 receive_file (receive_cb cb, void *opaque)
313 /* Read the length word. */
314 if (xread (sock, lenbuf, 4) == -1)
317 xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
318 xdr_u_int (&xdr, &len);
321 if (len == GUESTFS_CANCEL_FLAG)
322 continue; /* Just ignore it. */
324 if (len > GUESTFS_MESSAGE_MAX) {
325 fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n",
336 if (xread (sock, buf, len) == -1)
339 xdrmem_create (&xdr, buf, len, XDR_DECODE);
340 memset (&chunk, 0, sizeof chunk);
341 if (!xdr_guestfs_chunk (&xdr, &chunk)) {
350 printf ("receive_file: got chunk: cancel = %d, len = %d, buf = %p\n",
351 chunk.cancel, chunk.data.data_len, chunk.data.data_val);
354 fprintf (stderr, "receive_file: received cancellation from library\n");
355 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
358 if (chunk.data.data_len == 0) {
359 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
360 return 0; /* end of file */
364 r = cb (opaque, chunk.data.data_val, chunk.data.data_len);
368 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
369 if (r == -1) /* write error */
374 /* Send a cancellation flag back to the library. */
376 cancel_receive (void)
380 uint32_t flag = GUESTFS_CANCEL_FLAG;
382 xdrmem_create (&xdr, fbuf, sizeof fbuf, XDR_ENCODE);
383 xdr_u_int (&xdr, &flag);
386 if (xwrite (sock, fbuf, sizeof fbuf) == -1) {
387 perror ("write to socket");
391 /* Keep receiving chunks and discarding, until library sees cancel. */
392 (void) receive_file (NULL, NULL);
395 static int check_for_library_cancellation (void);
396 static int send_chunk (const guestfs_chunk *);
398 /* Also check if the library sends us a cancellation message. */
400 send_file_write (const void *buf, int len)
405 if (len > GUESTFS_MAX_CHUNK_SIZE) {
406 fprintf (stderr, "send_file_write: len (%d) > GUESTFS_MAX_CHUNK_SIZE (%d)\n",
407 len, GUESTFS_MAX_CHUNK_SIZE);
411 cancel = check_for_library_cancellation ();
415 chunk.data.data_len = 0;
416 chunk.data.data_val = NULL;
419 chunk.data.data_len = len;
420 chunk.data.data_val = (char *) buf;
423 if (send_chunk (&chunk) == -1)
426 if (cancel) return -2;
431 check_for_library_cancellation (void)
441 FD_SET (sock, &rset);
444 r = select (sock+1, &rset, NULL, NULL, &tv);
452 /* Read the message from the daemon. */
453 r = xread (sock, buf, sizeof buf);
457 xdrmem_create (&xdr, buf, sizeof buf, XDR_DECODE);
458 xdr_u_int (&xdr, &flag);
461 if (flag != GUESTFS_CANCEL_FLAG) {
462 fprintf (stderr, "check_for_library_cancellation: read 0x%x from library, expected 0x%x\n",
463 flag, GUESTFS_CANCEL_FLAG);
471 send_file_end (int cancel)
475 chunk.cancel = cancel;
476 chunk.data.data_len = 0;
477 chunk.data.data_val = NULL;
478 return send_chunk (&chunk);
482 send_chunk (const guestfs_chunk *chunk)
484 char buf[GUESTFS_MAX_CHUNK_SIZE + 48];
489 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
490 if (!xdr_guestfs_chunk (&xdr, (guestfs_chunk *) chunk)) {
491 fprintf (stderr, "send_chunk: failed to encode chunk\n");
496 len = xdr_getpos (&xdr);
499 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
500 xdr_u_int (&xdr, &len);
503 int err = (xwrite (sock, lenbuf, 4) == 0
504 && xwrite (sock, buf, len) == 0 ? 0 : -1);
506 fprintf (stderr, "send_chunk: write failed\n");