1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2009-2011 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.
29 #include <sys/param.h> /* defines MIN */
30 #include <sys/select.h>
32 #include <rpc/types.h>
40 #include "ignore-value.h"
43 #include "guestfs_protocol.h"
44 #include "errnostring.h"
46 /* The message currently being processed. */
50 /* Hint for implementing progress messages for uploaded/incoming data.
51 * The caller sets this to a value > 0 if it knows or can estimate how
52 * much data will be sent (this is not always known, eg. for uploads
53 * coming from a pipe). If this is known then we can emit progress
54 * messages as we write the data.
56 uint64_t progress_hint;
58 /* Optional arguments bitmask. Caller sets this to indicate which
59 * optional arguments in the guestfs_<foo>_args structure are
60 * meaningful. Optional arguments not covered by the bitmask are set
61 * to arbitrary values and the daemon should ignore them. If the
62 * bitmask has bits set that the daemon doesn't understand, then the
63 * whole call is rejected early in processing.
65 uint64_t optargs_bitmask;
67 /* Time at which we received the current request. */
68 static struct timeval start_t;
70 /* Time at which the last progress notification was sent. */
71 static struct timeval last_progress_t;
73 /* Counts the number of progress notifications sent during this call. */
74 static int count_progress;
76 /* The daemon communications socket. */
86 struct guestfs_message_header hdr;
91 /* Read the length word. */
92 if (xread (sock, lenbuf, 4) == -1)
95 xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
96 xdr_u_int (&xdr, &len);
101 "guestfsd: main_loop: new request, len 0x%" PRIx32 "\n",
104 if (len > GUESTFS_MESSAGE_MAX) {
105 fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n",
112 reply_with_perror ("malloc");
116 if (xread (sock, buf, len) == -1)
119 #ifdef ENABLE_PACKET_DUMP
123 for (i = 0; i < len; i += 16) {
124 printf ("%04zx: ", i);
125 for (j = i; j < MIN (i+16, len); ++j)
126 printf ("%02x ", (unsigned char) buf[j]);
127 for (; j < i+16; ++j)
130 for (j = i; j < MIN (i+16, len); ++j)
131 if (c_isprint (buf[j]))
132 printf ("%c", buf[j]);
135 for (; j < i+16; ++j)
142 gettimeofday (&start_t, NULL);
143 last_progress_t = start_t;
146 /* Decode the message header. */
147 xdrmem_create (&xdr, buf, len, XDR_DECODE);
148 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
149 fprintf (stderr, "guestfsd: could not decode message header\n");
153 /* Check the version etc. */
154 if (hdr.prog != GUESTFS_PROGRAM) {
155 reply_with_error ("wrong program (%d)", hdr.prog);
158 if (hdr.vers != GUESTFS_PROTOCOL_VERSION) {
159 reply_with_error ("wrong protocol version (%d)", hdr.vers);
162 if (hdr.direction != GUESTFS_DIRECTION_CALL) {
163 reply_with_error ("unexpected message direction (%d)", hdr.direction);
166 if (hdr.status != GUESTFS_STATUS_OK) {
167 reply_with_error ("unexpected message status (%d)", hdr.status);
173 progress_hint = hdr.progress_hint;
174 optargs_bitmask = hdr.optargs_bitmask;
176 /* Clear errors before we call the stub functions. This is just
177 * to ensure that we can accurately report errors in cases where
178 * error handling paths don't set errno correctly.
186 /* Now start to process this message. */
187 dispatch_incoming_message (&xdr);
188 /* Note that dispatch_incoming_message will also send a reply. */
190 /* In verbose mode, display the time taken to run each command. */
192 struct timeval end_t;
193 gettimeofday (&end_t, NULL);
195 int64_t start_us, end_us, elapsed_us;
196 start_us = (int64_t) start_t.tv_sec * 1000000 + start_t.tv_usec;
197 end_us = (int64_t) end_t.tv_sec * 1000000 + end_t.tv_usec;
198 elapsed_us = end_us - start_us;
201 "guestfsd: main_loop: proc %d (%s) took %d.%02d seconds\n",
203 proc_nr >= 0 && proc_nr < GUESTFS_PROC_NR_PROCS
204 ? function_names[proc_nr] : "UNKNOWN PROCEDURE",
205 (int) (elapsed_us / 1000000),
206 (int) ((elapsed_us / 10000) % 100));
215 static void send_error (int errnum, const char *msg);
218 reply_with_error (const char *fs, ...)
220 char err[GUESTFS_ERROR_LEN];
224 vsnprintf (err, sizeof err, fs, args);
231 reply_with_perror_errno (int err, const char *fs, ...)
233 char buf1[GUESTFS_ERROR_LEN];
234 char buf2[GUESTFS_ERROR_LEN];
238 vsnprintf (buf1, sizeof buf1, fs, args);
241 snprintf (buf2, sizeof buf2, "%s: %s", buf1, strerror (err));
243 send_error (err, buf2);
247 send_error (int errnum, const char *msg)
250 char buf[GUESTFS_ERROR_LEN + 200];
252 struct guestfs_message_header hdr;
253 struct guestfs_message_error err;
256 fprintf (stderr, "guestfsd: error: %s\n", msg);
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_ERROR;
267 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
268 fprintf (stderr, "guestfsd: failed to encode error message header\n");
272 /* These strings are not going to be freed. We just cast them
273 * to (char *) because they are defined that way in the XDR structs.
276 (char *) (errnum > 0 ? guestfs___errno_to_string (errnum) : "");
277 err.error_message = (char *) msg;
279 if (!xdr_guestfs_message_error (&xdr, &err)) {
280 fprintf (stderr, "guestfsd: failed to encode error message 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, "guestfsd: xwrite failed\n");
295 if (xwrite (sock, buf, len) == -1) {
296 fprintf (stderr, "guestfsd: xwrite failed\n");
302 reply (xdrproc_t xdrp, char *ret)
305 char buf[GUESTFS_MESSAGE_MAX];
307 struct guestfs_message_header hdr;
310 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
312 hdr.prog = GUESTFS_PROGRAM;
313 hdr.vers = GUESTFS_PROTOCOL_VERSION;
314 hdr.direction = GUESTFS_DIRECTION_REPLY;
315 hdr.status = GUESTFS_STATUS_OK;
319 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
320 fprintf (stderr, "guestfsd: failed to encode reply header\n");
325 /* This can fail if the reply body is too large, for example
326 * if it exceeds the maximum message size. In that case
327 * we want to return an error message instead. (RHBZ#509597).
329 if (!(*xdrp) (&xdr, ret)) {
330 reply_with_error ("guestfsd: failed to encode reply body\n(maybe the reply exceeds the maximum message size in the protocol?)");
336 len = xdr_getpos (&xdr);
339 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
340 xdr_u_int (&xdr, &len);
343 if (xwrite (sock, lenbuf, 4) == -1) {
344 fprintf (stderr, "guestfsd: xwrite failed\n");
347 if (xwrite (sock, buf, len) == -1) {
348 fprintf (stderr, "guestfsd: xwrite failed\n");
353 /* Receive file chunks, repeatedly calling 'cb'. */
355 receive_file (receive_cb cb, void *opaque)
366 fprintf (stderr, "guestfsd: receive_file: reading length word\n");
368 /* Read the length word. */
369 if (xread (sock, lenbuf, 4) == -1)
372 xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
373 xdr_u_int (&xdr, &len);
376 if (len == GUESTFS_CANCEL_FLAG)
377 continue; /* Just ignore it. */
379 if (len > GUESTFS_MESSAGE_MAX) {
380 fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n",
391 if (xread (sock, buf, len) == -1)
394 xdrmem_create (&xdr, buf, len, XDR_DECODE);
395 memset (&chunk, 0, sizeof chunk);
396 if (!xdr_guestfs_chunk (&xdr, &chunk)) {
406 "guestfsd: receive_file: got chunk: cancel = 0x%x, len = %d, buf = %p\n",
407 chunk.cancel, chunk.data.data_len, chunk.data.data_val);
409 if (chunk.cancel != 0 && chunk.cancel != 1) {
411 "guestfsd: receive_file: chunk.cancel != [0|1] ... "
412 "continuing even though we have probably lost synchronization with the library\n");
419 "guestfsd: receive_file: received cancellation from library\n");
420 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
423 if (chunk.data.data_len == 0) {
426 "guestfsd: receive_file: end of file, leaving function\n");
427 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
428 return 0; /* end of file */
431 /* Note that the callback can generate progress messages. */
433 r = cb (opaque, chunk.data.data_val, chunk.data.data_len);
437 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
438 if (r == -1) { /* write error */
440 fprintf (stderr, "guestfsd: receive_file: write error\n");
446 /* Send a cancellation flag back to the library. */
448 cancel_receive (void)
452 uint32_t flag = GUESTFS_CANCEL_FLAG;
454 xdrmem_create (&xdr, fbuf, sizeof fbuf, XDR_ENCODE);
455 xdr_u_int (&xdr, &flag);
458 if (xwrite (sock, fbuf, sizeof fbuf) == -1) {
459 perror ("write to socket");
463 /* Keep receiving chunks and discarding, until library sees cancel. */
464 return receive_file (NULL, NULL);
467 static int check_for_library_cancellation (void);
468 static int send_chunk (const guestfs_chunk *);
470 /* Also check if the library sends us a cancellation message. */
472 send_file_write (const void *buf, int len)
477 if (len > GUESTFS_MAX_CHUNK_SIZE) {
478 fprintf (stderr, "guestfsd: send_file_write: len (%d) > GUESTFS_MAX_CHUNK_SIZE (%d)\n",
479 len, GUESTFS_MAX_CHUNK_SIZE);
483 cancel = check_for_library_cancellation ();
487 chunk.data.data_len = 0;
488 chunk.data.data_val = NULL;
491 chunk.data.data_len = len;
492 chunk.data.data_val = (char *) buf;
495 if (send_chunk (&chunk) == -1)
498 if (cancel) return -2;
503 check_for_library_cancellation (void)
513 FD_SET (sock, &rset);
516 r = select (sock+1, &rset, NULL, NULL, &tv);
524 /* Read the message from the daemon. */
525 r = xread (sock, buf, sizeof buf);
529 xdrmem_create (&xdr, buf, sizeof buf, XDR_DECODE);
530 xdr_u_int (&xdr, &flag);
533 if (flag != GUESTFS_CANCEL_FLAG) {
534 fprintf (stderr, "guestfsd: check_for_library_cancellation: read 0x%x from library, expected 0x%x\n",
535 flag, GUESTFS_CANCEL_FLAG);
543 send_file_end (int cancel)
547 chunk.cancel = cancel;
548 chunk.data.data_len = 0;
549 chunk.data.data_val = NULL;
550 return send_chunk (&chunk);
554 send_chunk (const guestfs_chunk *chunk)
556 char buf[GUESTFS_MAX_CHUNK_SIZE + 48];
561 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
562 if (!xdr_guestfs_chunk (&xdr, (guestfs_chunk *) chunk)) {
563 fprintf (stderr, "guestfsd: send_chunk: failed to encode chunk\n");
568 len = xdr_getpos (&xdr);
571 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
572 xdr_u_int (&xdr, &len);
575 int err = (xwrite (sock, lenbuf, 4) == 0
576 && xwrite (sock, buf, len) == 0 ? 0 : -1);
578 fprintf (stderr, "guestfsd: send_chunk: write failed\n");
585 /* Initial delay before sending notification messages, and
586 * the period at which we send them thereafter. These times
587 * are in microseconds.
589 #define NOTIFICATION_INITIAL_DELAY 2000000
590 #define NOTIFICATION_PERIOD 333333
593 notify_progress (uint64_t position, uint64_t total)
595 struct timeval now_t;
596 gettimeofday (&now_t, NULL);
598 /* Always send a notification at 100%. This simplifies callers by
599 * allowing them to 'finish' the progress bar at 100% without
600 * needing special code.
602 if (count_progress > 0 && position == total)
605 /* Calculate time in microseconds since the last progress message
606 * was sent out (or since the start of the call).
608 int64_t last_us, now_us, elapsed_us;
610 (int64_t) last_progress_t.tv_sec * 1000000 + last_progress_t.tv_usec;
611 now_us = (int64_t) now_t.tv_sec * 1000000 + now_t.tv_usec;
612 elapsed_us = now_us - last_us;
615 if ((count_progress == 0 && elapsed_us < NOTIFICATION_INITIAL_DELAY) ||
616 (count_progress > 0 && elapsed_us < NOTIFICATION_PERIOD))
620 /* We're going to send a message now ... */
622 last_progress_t = now_t;
624 /* Send the header word. */
627 uint32_t i = GUESTFS_PROGRESS_FLAG;
629 xdrmem_create (&xdr, buf, 4, XDR_ENCODE);
630 xdr_u_int (&xdr, &i);
633 if (xwrite (sock, buf, 4) == -1) {
634 fprintf (stderr, "guestfsd: xwrite failed\n");
638 guestfs_progress message = {
641 .position = position,
645 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
646 if (!xdr_guestfs_progress (&xdr, &message)) {
647 fprintf (stderr, "guestfsd: xdr_guestfs_progress: failed to encode message\n");
651 len = xdr_getpos (&xdr);
654 if (xwrite (sock, buf, len) == -1) {
655 fprintf (stderr, "guestfsd: xwrite failed\n");
660 /* "Pulse mode" progress messages. */
662 #if defined(HAVE_SETITIMER) && defined(HAVE_SIGACTION)
664 static void async_safe_send_pulse (int sig);
667 pulse_mode_start (void)
669 struct sigaction act;
672 memset (&act, 0, sizeof act);
673 act.sa_handler = async_safe_send_pulse;
674 act.sa_flags = SA_RESTART;
676 if (sigaction (SIGALRM, &act, NULL) == -1) {
677 perror ("pulse_mode_start: sigaction");
681 it.it_value.tv_sec = NOTIFICATION_INITIAL_DELAY / 1000000;
682 it.it_value.tv_usec = NOTIFICATION_INITIAL_DELAY % 1000000;
683 it.it_interval.tv_sec = NOTIFICATION_PERIOD / 1000000;
684 it.it_interval.tv_usec = NOTIFICATION_PERIOD % 1000000;
686 if (setitimer (ITIMER_REAL, &it, NULL) == -1)
687 perror ("pulse_mode_start: setitimer");
691 pulse_mode_end (void)
693 pulse_mode_cancel (); /* Cancel the itimer. */
695 notify_progress (1, 1);
699 pulse_mode_cancel (void)
701 int err = errno; /* Function must preserve errno. */
703 struct sigaction act;
705 /* Setting it_value to zero cancels the itimer. */
706 it.it_value.tv_sec = 0;
707 it.it_value.tv_usec = 0;
708 it.it_interval.tv_sec = 0;
709 it.it_interval.tv_usec = 0;
711 if (setitimer (ITIMER_REAL, &it, NULL) == -1)
712 perror ("pulse_mode_cancel: setitimer");
714 memset (&act, 0, sizeof act);
715 act.sa_handler = SIG_DFL;
717 if (sigaction (SIGALRM, &act, NULL) == -1)
718 perror ("pulse_mode_cancel: sigaction");
723 /* Send a position = 0, total = 1 (pulse mode) message. The tricky
724 * part is we have to do it without invoking any non-async-safe
725 * functions (see signal(7) for a list). Therefore, KISS.
728 async_safe_send_pulse (int sig)
730 /* XDR is a RFC ... */
731 unsigned char msg[] = {
732 (GUESTFS_PROGRESS_FLAG & 0xff000000) >> 24,
733 (GUESTFS_PROGRESS_FLAG & 0x00ff0000) >> 16,
734 (GUESTFS_PROGRESS_FLAG & 0x0000ff00) >> 8,
735 GUESTFS_PROGRESS_FLAG & 0x000000ff,
736 (proc_nr & 0xff000000) >> 24,
737 (proc_nr & 0x00ff0000) >> 16,
738 (proc_nr & 0x0000ff00) >> 8,
739 proc_nr & 0x000000ff,
740 (serial & 0xff000000) >> 24,
741 (serial & 0x00ff0000) >> 16,
742 (serial & 0x0000ff00) >> 8,
744 /* 64 bit position = 0 */ 0, 0, 0, 0, 0, 0, 0, 0,
745 /* 64 bit total = 1 */ 0, 0, 0, 0, 0, 0, 0, 1
748 if (xwrite (sock, msg, sizeof msg) == -1)
752 #else /* !HAVE_SETITIMER || !HAVE_SIGACTION */
755 pulse_mode_start (void)
761 pulse_mode_end (void)
767 pulse_mode_cancel (void)
772 #endif /* !HAVE_SETITIMER || !HAVE_SIGACTION */