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 <sys/select.h>
31 #include <rpc/types.h>
39 #include "ignore-value.h"
42 #include "guestfs_protocol.h"
43 #include "errnostring.h"
45 /* The message currently being processed. */
49 /* Hint for implementing progress messages for uploaded/incoming data.
50 * The caller sets this to a value > 0 if it knows or can estimate how
51 * much data will be sent (this is not always known, eg. for uploads
52 * coming from a pipe). If this is known then we can emit progress
53 * messages as we write the data.
55 uint64_t progress_hint;
57 /* Optional arguments bitmask. Caller sets this to indicate which
58 * optional arguments in the guestfs_<foo>_args structure are
59 * meaningful. Optional arguments not covered by the bitmask are set
60 * to arbitrary values and the daemon should ignore them. If the
61 * bitmask has bits set that the daemon doesn't understand, then the
62 * whole call is rejected early in processing.
64 uint64_t optargs_bitmask;
66 /* Time at which we received the current request. */
67 static struct timeval start_t;
69 /* Time at which the last progress notification was sent. */
70 static struct timeval last_progress_t;
72 /* Counts the number of progress notifications sent during this call. */
73 static int count_progress;
75 /* The daemon communications socket. */
85 struct guestfs_message_header hdr;
90 /* Most common errors are leaked memory and leaked file descriptors,
91 * so run this between each command:
94 ignore_value (system ("ls -l /proc/self/fd"));
96 /* Read the length word. */
97 if (xread (sock, lenbuf, 4) == -1)
100 xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
101 xdr_u_int (&xdr, &len);
106 "guestfsd: main_loop: new request, len 0x%" PRIx32 "\n",
109 if (len > GUESTFS_MESSAGE_MAX) {
110 fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n",
117 reply_with_perror ("malloc");
121 if (xread (sock, buf, len) == -1)
124 #ifdef ENABLE_PACKET_DUMP
128 for (i = 0; i < len; i += 16) {
129 printf ("%04zx: ", i);
130 for (j = i; j < MIN (i+16, len); ++j)
131 printf ("%02x ", (unsigned char) buf[j]);
132 for (; j < i+16; ++j)
135 for (j = i; j < MIN (i+16, len); ++j)
136 if (c_isprint (buf[j]))
137 printf ("%c", buf[j]);
140 for (; j < i+16; ++j)
147 gettimeofday (&start_t, NULL);
148 last_progress_t = start_t;
151 /* Decode the message header. */
152 xdrmem_create (&xdr, buf, len, XDR_DECODE);
153 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
154 fprintf (stderr, "guestfsd: could not decode message header\n");
158 /* Check the version etc. */
159 if (hdr.prog != GUESTFS_PROGRAM) {
160 reply_with_error ("wrong program (%d)", hdr.prog);
163 if (hdr.vers != GUESTFS_PROTOCOL_VERSION) {
164 reply_with_error ("wrong protocol version (%d)", hdr.vers);
167 if (hdr.direction != GUESTFS_DIRECTION_CALL) {
168 reply_with_error ("unexpected message direction (%d)", hdr.direction);
171 if (hdr.status != GUESTFS_STATUS_OK) {
172 reply_with_error ("unexpected message status (%d)", hdr.status);
178 progress_hint = hdr.progress_hint;
179 optargs_bitmask = hdr.optargs_bitmask;
181 /* Clear errors before we call the stub functions. This is just
182 * to ensure that we can accurately report errors in cases where
183 * error handling paths don't set errno correctly.
191 /* Now start to process this message. */
192 dispatch_incoming_message (&xdr);
193 /* Note that dispatch_incoming_message will also send a reply. */
195 /* In verbose mode, display the time taken to run each command. */
197 struct timeval end_t;
198 gettimeofday (&end_t, NULL);
200 int64_t start_us, end_us, elapsed_us;
201 start_us = (int64_t) start_t.tv_sec * 1000000 + start_t.tv_usec;
202 end_us = (int64_t) end_t.tv_sec * 1000000 + end_t.tv_usec;
203 elapsed_us = end_us - start_us;
206 "guestfsd: main_loop: proc %d (%s) took %d.%02d seconds\n",
208 proc_nr >= 0 && proc_nr < GUESTFS_PROC_NR_PROCS
209 ? function_names[proc_nr] : "UNKNOWN PROCEDURE",
210 (int) (elapsed_us / 1000000),
211 (int) ((elapsed_us / 10000) % 100));
220 static void send_error (int errnum, const char *msg);
223 reply_with_error (const char *fs, ...)
225 char err[GUESTFS_ERROR_LEN];
229 vsnprintf (err, sizeof err, fs, args);
236 reply_with_perror_errno (int err, const char *fs, ...)
238 char buf1[GUESTFS_ERROR_LEN];
239 char buf2[GUESTFS_ERROR_LEN];
243 vsnprintf (buf1, sizeof buf1, fs, args);
246 snprintf (buf2, sizeof buf2, "%s: %s", buf1, strerror (err));
248 send_error (err, buf2);
252 send_error (int errnum, const char *msg)
255 char buf[GUESTFS_ERROR_LEN + 200];
257 struct guestfs_message_header hdr;
258 struct guestfs_message_error err;
261 fprintf (stderr, "guestfsd: error: %s\n", msg);
263 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
265 hdr.prog = GUESTFS_PROGRAM;
266 hdr.vers = GUESTFS_PROTOCOL_VERSION;
267 hdr.direction = GUESTFS_DIRECTION_REPLY;
268 hdr.status = GUESTFS_STATUS_ERROR;
272 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
273 fprintf (stderr, "guestfsd: failed to encode error message header\n");
277 /* These strings are not going to be freed. We just cast them
278 * to (char *) because they are defined that way in the XDR structs.
281 (char *) (errnum > 0 ? guestfs___errno_to_string (errnum) : "");
282 err.error_message = (char *) msg;
284 if (!xdr_guestfs_message_error (&xdr, &err)) {
285 fprintf (stderr, "guestfsd: failed to encode error message body\n");
289 len = xdr_getpos (&xdr);
292 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
293 xdr_u_int (&xdr, &len);
296 if (xwrite (sock, lenbuf, 4) == -1) {
297 fprintf (stderr, "guestfsd: xwrite failed\n");
300 if (xwrite (sock, buf, len) == -1) {
301 fprintf (stderr, "guestfsd: xwrite failed\n");
307 reply (xdrproc_t xdrp, char *ret)
310 char buf[GUESTFS_MESSAGE_MAX];
312 struct guestfs_message_header hdr;
315 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
317 hdr.prog = GUESTFS_PROGRAM;
318 hdr.vers = GUESTFS_PROTOCOL_VERSION;
319 hdr.direction = GUESTFS_DIRECTION_REPLY;
320 hdr.status = GUESTFS_STATUS_OK;
324 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
325 fprintf (stderr, "guestfsd: failed to encode reply header\n");
330 /* This can fail if the reply body is too large, for example
331 * if it exceeds the maximum message size. In that case
332 * we want to return an error message instead. (RHBZ#509597).
334 if (!(*xdrp) (&xdr, ret)) {
335 reply_with_error ("guestfsd: failed to encode reply body\n(maybe the reply exceeds the maximum message size in the protocol?)");
341 len = xdr_getpos (&xdr);
344 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
345 xdr_u_int (&xdr, &len);
348 if (xwrite (sock, lenbuf, 4) == -1) {
349 fprintf (stderr, "guestfsd: xwrite failed\n");
352 if (xwrite (sock, buf, len) == -1) {
353 fprintf (stderr, "guestfsd: xwrite failed\n");
358 /* Receive file chunks, repeatedly calling 'cb'. */
360 receive_file (receive_cb cb, void *opaque)
371 fprintf (stderr, "guestfsd: receive_file: reading length word\n");
373 /* Read the length word. */
374 if (xread (sock, lenbuf, 4) == -1)
377 xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
378 xdr_u_int (&xdr, &len);
381 if (len == GUESTFS_CANCEL_FLAG)
382 continue; /* Just ignore it. */
384 if (len > GUESTFS_MESSAGE_MAX) {
385 fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n",
396 if (xread (sock, buf, len) == -1)
399 xdrmem_create (&xdr, buf, len, XDR_DECODE);
400 memset (&chunk, 0, sizeof chunk);
401 if (!xdr_guestfs_chunk (&xdr, &chunk)) {
411 "guestfsd: receive_file: got chunk: cancel = 0x%x, len = %d, buf = %p\n",
412 chunk.cancel, chunk.data.data_len, chunk.data.data_val);
414 if (chunk.cancel != 0 && chunk.cancel != 1) {
416 "guestfsd: receive_file: chunk.cancel != [0|1] ... "
417 "continuing even though we have probably lost synchronization with the library\n");
424 "guestfsd: receive_file: received cancellation from library\n");
425 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
428 if (chunk.data.data_len == 0) {
431 "guestfsd: receive_file: end of file, leaving function\n");
432 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
433 return 0; /* end of file */
436 /* Note that the callback can generate progress messages. */
438 r = cb (opaque, chunk.data.data_val, chunk.data.data_len);
442 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
443 if (r == -1) { /* write error */
445 fprintf (stderr, "guestfsd: receive_file: write error\n");
451 /* Send a cancellation flag back to the library. */
453 cancel_receive (void)
457 uint32_t flag = GUESTFS_CANCEL_FLAG;
459 xdrmem_create (&xdr, fbuf, sizeof fbuf, XDR_ENCODE);
460 xdr_u_int (&xdr, &flag);
463 if (xwrite (sock, fbuf, sizeof fbuf) == -1) {
464 perror ("write to socket");
468 /* Keep receiving chunks and discarding, until library sees cancel. */
469 return receive_file (NULL, NULL);
472 static int check_for_library_cancellation (void);
473 static int send_chunk (const guestfs_chunk *);
475 /* Also check if the library sends us a cancellation message. */
477 send_file_write (const void *buf, int len)
482 if (len > GUESTFS_MAX_CHUNK_SIZE) {
483 fprintf (stderr, "guestfsd: send_file_write: len (%d) > GUESTFS_MAX_CHUNK_SIZE (%d)\n",
484 len, GUESTFS_MAX_CHUNK_SIZE);
488 cancel = check_for_library_cancellation ();
492 chunk.data.data_len = 0;
493 chunk.data.data_val = NULL;
496 chunk.data.data_len = len;
497 chunk.data.data_val = (char *) buf;
500 if (send_chunk (&chunk) == -1)
503 if (cancel) return -2;
508 check_for_library_cancellation (void)
518 FD_SET (sock, &rset);
521 r = select (sock+1, &rset, NULL, NULL, &tv);
529 /* Read the message from the daemon. */
530 r = xread (sock, buf, sizeof buf);
534 xdrmem_create (&xdr, buf, sizeof buf, XDR_DECODE);
535 xdr_u_int (&xdr, &flag);
538 if (flag != GUESTFS_CANCEL_FLAG) {
539 fprintf (stderr, "guestfsd: check_for_library_cancellation: read 0x%x from library, expected 0x%x\n",
540 flag, GUESTFS_CANCEL_FLAG);
548 send_file_end (int cancel)
552 chunk.cancel = cancel;
553 chunk.data.data_len = 0;
554 chunk.data.data_val = NULL;
555 return send_chunk (&chunk);
559 send_chunk (const guestfs_chunk *chunk)
561 char buf[GUESTFS_MAX_CHUNK_SIZE + 48];
566 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
567 if (!xdr_guestfs_chunk (&xdr, (guestfs_chunk *) chunk)) {
568 fprintf (stderr, "guestfsd: send_chunk: failed to encode chunk\n");
573 len = xdr_getpos (&xdr);
576 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
577 xdr_u_int (&xdr, &len);
580 int err = (xwrite (sock, lenbuf, 4) == 0
581 && xwrite (sock, buf, len) == 0 ? 0 : -1);
583 fprintf (stderr, "guestfsd: send_chunk: write failed\n");
590 /* Initial delay before sending notification messages, and
591 * the period at which we send them thereafter. These times
592 * are in microseconds.
594 #define NOTIFICATION_INITIAL_DELAY 2000000
595 #define NOTIFICATION_PERIOD 333333
598 notify_progress (uint64_t position, uint64_t total)
600 struct timeval now_t;
601 gettimeofday (&now_t, NULL);
603 /* Always send a notification at 100%. This simplifies callers by
604 * allowing them to 'finish' the progress bar at 100% without
605 * needing special code.
607 if (count_progress > 0 && position == total)
610 /* Calculate time in microseconds since the last progress message
611 * was sent out (or since the start of the call).
613 int64_t last_us, now_us, elapsed_us;
615 (int64_t) last_progress_t.tv_sec * 1000000 + last_progress_t.tv_usec;
616 now_us = (int64_t) now_t.tv_sec * 1000000 + now_t.tv_usec;
617 elapsed_us = now_us - last_us;
620 if ((count_progress == 0 && elapsed_us < NOTIFICATION_INITIAL_DELAY) ||
621 (count_progress > 0 && elapsed_us < NOTIFICATION_PERIOD))
625 /* We're going to send a message now ... */
627 last_progress_t = now_t;
629 /* Send the header word. */
632 uint32_t i = GUESTFS_PROGRESS_FLAG;
634 xdrmem_create (&xdr, buf, 4, XDR_ENCODE);
635 xdr_u_int (&xdr, &i);
638 if (xwrite (sock, buf, 4) == -1) {
639 fprintf (stderr, "guestfsd: xwrite failed\n");
643 guestfs_progress message = {
646 .position = position,
650 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
651 if (!xdr_guestfs_progress (&xdr, &message)) {
652 fprintf (stderr, "guestfsd: xdr_guestfs_progress: failed to encode message\n");
656 len = xdr_getpos (&xdr);
659 if (xwrite (sock, buf, len) == -1) {
660 fprintf (stderr, "guestfsd: xwrite failed\n");