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>
30 #include <rpc/types.h>
38 #include "ignore-value.h"
41 #include "guestfs_protocol.h"
43 /* The message currently being processed. */
47 /* Time at which we received the current request. */
48 static struct timeval start_t;
50 /* Time at which the last progress notification was sent. */
51 static struct timeval last_progress_t;
53 /* Counts the number of progress notifications sent during this call. */
54 static int count_progress;
56 /* The daemon communications socket. */
66 struct guestfs_message_header hdr;
71 /* Most common errors are leaked memory and leaked file descriptors,
72 * so run this between each command:
75 ignore_value (system ("ls -l /proc/self/fd"));
77 /* Read the length word. */
78 if (xread (sock, lenbuf, 4) == -1)
81 xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
82 xdr_u_int (&xdr, &len);
85 if (len > GUESTFS_MESSAGE_MAX) {
86 fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n",
93 reply_with_perror ("malloc");
97 if (xread (sock, buf, len) == -1)
100 #ifdef ENABLE_PACKET_DUMP
104 for (i = 0; i < len; i += 16) {
105 printf ("%04zx: ", i);
106 for (j = i; j < MIN (i+16, len); ++j)
107 printf ("%02x ", (unsigned char) buf[j]);
108 for (; j < i+16; ++j)
111 for (j = i; j < MIN (i+16, len); ++j)
112 if (c_isprint (buf[j]))
113 printf ("%c", buf[j]);
116 for (; j < i+16; ++j)
123 gettimeofday (&start_t, NULL);
124 last_progress_t = start_t;
127 /* Decode the message header. */
128 xdrmem_create (&xdr, buf, len, XDR_DECODE);
129 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
130 fprintf (stderr, "guestfsd: could not decode message header\n");
134 /* Check the version etc. */
135 if (hdr.prog != GUESTFS_PROGRAM) {
136 reply_with_error ("wrong program (%d)", hdr.prog);
139 if (hdr.vers != GUESTFS_PROTOCOL_VERSION) {
140 reply_with_error ("wrong protocol version (%d)", hdr.vers);
143 if (hdr.direction != GUESTFS_DIRECTION_CALL) {
144 reply_with_error ("unexpected message direction (%d)", hdr.direction);
147 if (hdr.status != GUESTFS_STATUS_OK) {
148 reply_with_error ("unexpected message status (%d)", hdr.status);
155 /* Clear errors before we call the stub functions. This is just
156 * to ensure that we can accurately report errors in cases where
157 * error handling paths don't set errno correctly.
165 /* Now start to process this message. */
166 dispatch_incoming_message (&xdr);
167 /* Note that dispatch_incoming_message will also send a reply. */
169 /* In verbose mode, display the time taken to run each command. */
171 struct timeval end_t;
172 gettimeofday (&end_t, NULL);
174 int64_t start_us, end_us, elapsed_us;
175 start_us = (int64_t) start_t.tv_sec * 1000000 + start_t.tv_usec;
176 end_us = (int64_t) end_t.tv_sec * 1000000 + end_t.tv_usec;
177 elapsed_us = end_us - start_us;
179 fprintf (stderr, "proc %d (%s) took %d.%02d seconds\n",
181 proc_nr >= 0 && proc_nr < GUESTFS_PROC_NR_PROCS
182 ? function_names[proc_nr] : "UNKNOWN PROCEDURE",
183 (int) (elapsed_us / 1000000),
184 (int) ((elapsed_us / 10000) % 100));
193 static void send_error (int errnum, const char *msg);
196 reply_with_error (const char *fs, ...)
198 char err[GUESTFS_ERROR_LEN];
202 vsnprintf (err, sizeof err, fs, args);
209 reply_with_perror_errno (int err, const char *fs, ...)
211 char buf1[GUESTFS_ERROR_LEN];
212 char buf2[GUESTFS_ERROR_LEN];
216 vsnprintf (buf1, sizeof buf1, fs, args);
219 snprintf (buf2, sizeof buf2, "%s: %s", buf1, strerror (err));
221 send_error (err, buf2);
225 send_error (int errnum, const char *msg)
228 char buf[GUESTFS_ERROR_LEN + 200];
230 struct guestfs_message_header hdr;
231 struct guestfs_message_error err;
234 fprintf (stderr, "guestfsd: error: %s\n", msg);
236 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
238 hdr.prog = GUESTFS_PROGRAM;
239 hdr.vers = GUESTFS_PROTOCOL_VERSION;
240 hdr.direction = GUESTFS_DIRECTION_REPLY;
241 hdr.status = GUESTFS_STATUS_ERROR;
245 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
246 fprintf (stderr, "guestfsd: failed to encode error message header\n");
250 err.linux_errno = errnum;
251 err.error_message = (char *) msg;
253 if (!xdr_guestfs_message_error (&xdr, &err)) {
254 fprintf (stderr, "guestfsd: failed to encode error message body\n");
258 len = xdr_getpos (&xdr);
261 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
262 xdr_u_int (&xdr, &len);
265 if (xwrite (sock, lenbuf, 4) == -1) {
266 fprintf (stderr, "xwrite failed\n");
269 if (xwrite (sock, buf, len) == -1) {
270 fprintf (stderr, "xwrite failed\n");
276 reply (xdrproc_t xdrp, char *ret)
279 char buf[GUESTFS_MESSAGE_MAX];
281 struct guestfs_message_header hdr;
284 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
286 hdr.prog = GUESTFS_PROGRAM;
287 hdr.vers = GUESTFS_PROTOCOL_VERSION;
288 hdr.direction = GUESTFS_DIRECTION_REPLY;
289 hdr.status = GUESTFS_STATUS_OK;
293 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
294 fprintf (stderr, "guestfsd: failed to encode reply header\n");
299 /* This can fail if the reply body is too large, for example
300 * if it exceeds the maximum message size. In that case
301 * we want to return an error message instead. (RHBZ#509597).
303 if (!(*xdrp) (&xdr, ret)) {
304 reply_with_error ("guestfsd: failed to encode reply body\n(maybe the reply exceeds the maximum message size in the protocol?)");
310 len = xdr_getpos (&xdr);
313 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
314 xdr_u_int (&xdr, &len);
317 if (xwrite (sock, lenbuf, 4) == -1) {
318 fprintf (stderr, "xwrite failed\n");
321 if (xwrite (sock, buf, len) == -1) {
322 fprintf (stderr, "xwrite failed\n");
327 /* Receive file chunks, repeatedly calling 'cb'. */
329 receive_file (receive_cb cb, void *opaque)
340 fprintf (stderr, "receive_file: reading length word\n");
342 /* Read the length word. */
343 if (xread (sock, lenbuf, 4) == -1)
346 xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
347 xdr_u_int (&xdr, &len);
350 if (len == GUESTFS_CANCEL_FLAG)
351 continue; /* Just ignore it. */
353 if (len > GUESTFS_MESSAGE_MAX) {
354 fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n",
365 if (xread (sock, buf, len) == -1)
368 xdrmem_create (&xdr, buf, len, XDR_DECODE);
369 memset (&chunk, 0, sizeof chunk);
370 if (!xdr_guestfs_chunk (&xdr, &chunk)) {
379 fprintf (stderr, "receive_file: got chunk: cancel = %d, len = %d, buf = %p\n",
380 chunk.cancel, chunk.data.data_len, chunk.data.data_val);
384 fprintf (stderr, "receive_file: received cancellation from library\n");
385 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
388 if (chunk.data.data_len == 0) {
390 fprintf (stderr, "receive_file: end of file, leaving function\n");
391 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
392 return 0; /* end of file */
396 r = cb (opaque, chunk.data.data_val, chunk.data.data_len);
400 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
401 if (r == -1) { /* write error */
403 fprintf (stderr, "receive_file: write error\n");
409 /* Send a cancellation flag back to the library. */
411 cancel_receive (void)
415 uint32_t flag = GUESTFS_CANCEL_FLAG;
417 xdrmem_create (&xdr, fbuf, sizeof fbuf, XDR_ENCODE);
418 xdr_u_int (&xdr, &flag);
421 if (xwrite (sock, fbuf, sizeof fbuf) == -1) {
422 perror ("write to socket");
426 /* Keep receiving chunks and discarding, until library sees cancel. */
427 return receive_file (NULL, NULL);
430 static int check_for_library_cancellation (void);
431 static int send_chunk (const guestfs_chunk *);
433 /* Also check if the library sends us a cancellation message. */
435 send_file_write (const void *buf, int len)
440 if (len > GUESTFS_MAX_CHUNK_SIZE) {
441 fprintf (stderr, "send_file_write: len (%d) > GUESTFS_MAX_CHUNK_SIZE (%d)\n",
442 len, GUESTFS_MAX_CHUNK_SIZE);
446 cancel = check_for_library_cancellation ();
450 chunk.data.data_len = 0;
451 chunk.data.data_val = NULL;
454 chunk.data.data_len = len;
455 chunk.data.data_val = (char *) buf;
458 if (send_chunk (&chunk) == -1)
461 if (cancel) return -2;
466 check_for_library_cancellation (void)
476 FD_SET (sock, &rset);
479 r = select (sock+1, &rset, NULL, NULL, &tv);
487 /* Read the message from the daemon. */
488 r = xread (sock, buf, sizeof buf);
492 xdrmem_create (&xdr, buf, sizeof buf, XDR_DECODE);
493 xdr_u_int (&xdr, &flag);
496 if (flag != GUESTFS_CANCEL_FLAG) {
497 fprintf (stderr, "check_for_library_cancellation: read 0x%x from library, expected 0x%x\n",
498 flag, GUESTFS_CANCEL_FLAG);
506 send_file_end (int cancel)
510 chunk.cancel = cancel;
511 chunk.data.data_len = 0;
512 chunk.data.data_val = NULL;
513 return send_chunk (&chunk);
517 send_chunk (const guestfs_chunk *chunk)
519 char buf[GUESTFS_MAX_CHUNK_SIZE + 48];
524 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
525 if (!xdr_guestfs_chunk (&xdr, (guestfs_chunk *) chunk)) {
526 fprintf (stderr, "send_chunk: failed to encode chunk\n");
531 len = xdr_getpos (&xdr);
534 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
535 xdr_u_int (&xdr, &len);
538 int err = (xwrite (sock, lenbuf, 4) == 0
539 && xwrite (sock, buf, len) == 0 ? 0 : -1);
541 fprintf (stderr, "send_chunk: write failed\n");
548 /* Initial delay before sending notification messages, and
549 * the period at which we send them thereafter. These times
550 * are in microseconds.
552 #define NOTIFICATION_INITIAL_DELAY 2000000
553 #define NOTIFICATION_PERIOD 333333
556 notify_progress (uint64_t position, uint64_t total)
558 struct timeval now_t;
559 gettimeofday (&now_t, NULL);
561 /* Always send a notification at 100%. This simplifies callers by
562 * allowing them to 'finish' the progress bar at 100% without
563 * needing special code.
565 if (count_progress > 0 && position == total)
568 /* Calculate time in microseconds since the last progress message
569 * was sent out (or since the start of the call).
571 int64_t last_us, now_us, elapsed_us;
573 (int64_t) last_progress_t.tv_sec * 1000000 + last_progress_t.tv_usec;
574 now_us = (int64_t) now_t.tv_sec * 1000000 + now_t.tv_usec;
575 elapsed_us = now_us - last_us;
578 if ((count_progress == 0 && elapsed_us < NOTIFICATION_INITIAL_DELAY) ||
579 (count_progress > 0 && elapsed_us < NOTIFICATION_PERIOD))
583 /* We're going to send a message now ... */
585 last_progress_t = now_t;
587 /* Send the header word. */
590 uint32_t i = GUESTFS_PROGRESS_FLAG;
592 xdrmem_create (&xdr, buf, 4, XDR_ENCODE);
593 xdr_u_int (&xdr, &i);
596 if (xwrite (sock, buf, 4) == -1) {
597 fprintf (stderr, "xwrite failed\n");
601 guestfs_progress message = {
604 .position = position,
608 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
609 if (!xdr_guestfs_progress (&xdr, &message)) {
610 fprintf (stderr, "xdr_guestfs_progress: failed to encode message\n");
614 len = xdr_getpos (&xdr);
617 if (xwrite (sock, buf, len) == -1) {
618 fprintf (stderr, "xwrite failed\n");