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 /* Cancellation sent from the library and received after the
105 * previous request has finished processing. Just ignore it.
107 if (len == GUESTFS_CANCEL_FLAG)
110 if (len > GUESTFS_MESSAGE_MAX) {
111 fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n",
118 reply_with_perror ("malloc");
122 if (xread (sock, buf, len) == -1)
125 #ifdef ENABLE_PACKET_DUMP
129 for (i = 0; i < len; i += 16) {
130 printf ("%04zx: ", i);
131 for (j = i; j < MIN (i+16, len); ++j)
132 printf ("%02x ", (unsigned char) buf[j]);
133 for (; j < i+16; ++j)
136 for (j = i; j < MIN (i+16, len); ++j)
137 if (c_isprint (buf[j]))
138 printf ("%c", buf[j]);
141 for (; j < i+16; ++j)
148 gettimeofday (&start_t, NULL);
149 last_progress_t = start_t;
152 /* Decode the message header. */
153 xdrmem_create (&xdr, buf, len, XDR_DECODE);
154 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
155 fprintf (stderr, "guestfsd: could not decode message header\n");
159 /* Check the version etc. */
160 if (hdr.prog != GUESTFS_PROGRAM) {
161 reply_with_error ("wrong program (%d)", hdr.prog);
164 if (hdr.vers != GUESTFS_PROTOCOL_VERSION) {
165 reply_with_error ("wrong protocol version (%d)", hdr.vers);
168 if (hdr.direction != GUESTFS_DIRECTION_CALL) {
169 reply_with_error ("unexpected message direction (%d)", hdr.direction);
172 if (hdr.status != GUESTFS_STATUS_OK) {
173 reply_with_error ("unexpected message status (%d)", hdr.status);
179 progress_hint = hdr.progress_hint;
180 optargs_bitmask = hdr.optargs_bitmask;
182 /* Clear errors before we call the stub functions. This is just
183 * to ensure that we can accurately report errors in cases where
184 * error handling paths don't set errno correctly.
192 /* Now start to process this message. */
193 dispatch_incoming_message (&xdr);
194 /* Note that dispatch_incoming_message will also send a reply. */
196 /* In verbose mode, display the time taken to run each command. */
198 struct timeval end_t;
199 gettimeofday (&end_t, NULL);
201 int64_t start_us, end_us, elapsed_us;
202 start_us = (int64_t) start_t.tv_sec * 1000000 + start_t.tv_usec;
203 end_us = (int64_t) end_t.tv_sec * 1000000 + end_t.tv_usec;
204 elapsed_us = end_us - start_us;
207 "guestfsd: main_loop: proc %d (%s) took %d.%02d seconds\n",
209 proc_nr >= 0 && proc_nr < GUESTFS_PROC_NR_PROCS
210 ? function_names[proc_nr] : "UNKNOWN PROCEDURE",
211 (int) (elapsed_us / 1000000),
212 (int) ((elapsed_us / 10000) % 100));
221 static void send_error (int errnum, const char *msg);
224 reply_with_error (const char *fs, ...)
226 char err[GUESTFS_ERROR_LEN];
230 vsnprintf (err, sizeof err, fs, args);
237 reply_with_perror_errno (int err, const char *fs, ...)
239 char buf1[GUESTFS_ERROR_LEN];
240 char buf2[GUESTFS_ERROR_LEN];
244 vsnprintf (buf1, sizeof buf1, fs, args);
247 snprintf (buf2, sizeof buf2, "%s: %s", buf1, strerror (err));
249 send_error (err, buf2);
253 send_error (int errnum, const char *msg)
256 char buf[GUESTFS_ERROR_LEN + 200];
258 struct guestfs_message_header hdr;
259 struct guestfs_message_error err;
262 fprintf (stderr, "guestfsd: error: %s\n", msg);
264 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
266 hdr.prog = GUESTFS_PROGRAM;
267 hdr.vers = GUESTFS_PROTOCOL_VERSION;
268 hdr.direction = GUESTFS_DIRECTION_REPLY;
269 hdr.status = GUESTFS_STATUS_ERROR;
273 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
274 fprintf (stderr, "guestfsd: failed to encode error message header\n");
278 /* These strings are not going to be freed. We just cast them
279 * to (char *) because they are defined that way in the XDR structs.
282 (char *) (errnum > 0 ? guestfs___errno_to_string (errnum) : "");
283 err.error_message = (char *) msg;
285 if (!xdr_guestfs_message_error (&xdr, &err)) {
286 fprintf (stderr, "guestfsd: failed to encode error message body\n");
290 len = xdr_getpos (&xdr);
293 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
294 xdr_u_int (&xdr, &len);
297 if (xwrite (sock, lenbuf, 4) == -1) {
298 fprintf (stderr, "guestfsd: xwrite failed\n");
301 if (xwrite (sock, buf, len) == -1) {
302 fprintf (stderr, "guestfsd: xwrite failed\n");
308 reply (xdrproc_t xdrp, char *ret)
311 char buf[GUESTFS_MESSAGE_MAX];
313 struct guestfs_message_header hdr;
316 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
318 hdr.prog = GUESTFS_PROGRAM;
319 hdr.vers = GUESTFS_PROTOCOL_VERSION;
320 hdr.direction = GUESTFS_DIRECTION_REPLY;
321 hdr.status = GUESTFS_STATUS_OK;
325 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
326 fprintf (stderr, "guestfsd: failed to encode reply header\n");
331 /* This can fail if the reply body is too large, for example
332 * if it exceeds the maximum message size. In that case
333 * we want to return an error message instead. (RHBZ#509597).
335 if (!(*xdrp) (&xdr, ret)) {
336 reply_with_error ("guestfsd: failed to encode reply body\n(maybe the reply exceeds the maximum message size in the protocol?)");
342 len = xdr_getpos (&xdr);
345 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
346 xdr_u_int (&xdr, &len);
349 if (xwrite (sock, lenbuf, 4) == -1) {
350 fprintf (stderr, "guestfsd: xwrite failed\n");
353 if (xwrite (sock, buf, len) == -1) {
354 fprintf (stderr, "guestfsd: xwrite failed\n");
359 /* Receive file chunks, repeatedly calling 'cb'. */
361 receive_file (receive_cb cb, void *opaque)
372 fprintf (stderr, "guestfsd: receive_file: reading length word\n");
374 /* Read the length word. */
375 if (xread (sock, lenbuf, 4) == -1)
378 xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
379 xdr_u_int (&xdr, &len);
382 if (len == GUESTFS_CANCEL_FLAG)
383 continue; /* Just ignore it. */
385 if (len > GUESTFS_MESSAGE_MAX) {
386 fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n",
397 if (xread (sock, buf, len) == -1)
400 xdrmem_create (&xdr, buf, len, XDR_DECODE);
401 memset (&chunk, 0, sizeof chunk);
402 if (!xdr_guestfs_chunk (&xdr, &chunk)) {
412 "guestfsd: receive_file: got chunk: cancel = 0x%x, len = %d, buf = %p\n",
413 chunk.cancel, chunk.data.data_len, chunk.data.data_val);
415 if (chunk.cancel != 0 && chunk.cancel != 1) {
417 "guestfsd: receive_file: chunk.cancel != [0|1] ... "
418 "continuing even though we have probably lost synchronization with the library\n");
425 "guestfsd: receive_file: received cancellation from library\n");
426 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
429 if (chunk.data.data_len == 0) {
432 "guestfsd: receive_file: end of file, leaving function\n");
433 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
434 return 0; /* end of file */
437 /* Note that the callback can generate progress messages. */
439 r = cb (opaque, chunk.data.data_val, chunk.data.data_len);
443 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
444 if (r == -1) { /* write error */
446 fprintf (stderr, "guestfsd: receive_file: write error\n");
452 /* Send a cancellation flag back to the library. */
454 cancel_receive (void)
458 uint32_t flag = GUESTFS_CANCEL_FLAG;
460 xdrmem_create (&xdr, fbuf, sizeof fbuf, XDR_ENCODE);
461 xdr_u_int (&xdr, &flag);
464 if (xwrite (sock, fbuf, sizeof fbuf) == -1) {
465 perror ("write to socket");
469 /* Keep receiving chunks and discarding, until library sees cancel. */
470 return receive_file (NULL, NULL);
473 static int check_for_library_cancellation (void);
474 static int send_chunk (const guestfs_chunk *);
476 /* Also check if the library sends us a cancellation message. */
478 send_file_write (const void *buf, int len)
483 if (len > GUESTFS_MAX_CHUNK_SIZE) {
484 fprintf (stderr, "guestfsd: send_file_write: len (%d) > GUESTFS_MAX_CHUNK_SIZE (%d)\n",
485 len, GUESTFS_MAX_CHUNK_SIZE);
489 cancel = check_for_library_cancellation ();
493 chunk.data.data_len = 0;
494 chunk.data.data_val = NULL;
497 chunk.data.data_len = len;
498 chunk.data.data_val = (char *) buf;
501 if (send_chunk (&chunk) == -1)
504 if (cancel) return -2;
509 check_for_library_cancellation (void)
519 FD_SET (sock, &rset);
522 r = select (sock+1, &rset, NULL, NULL, &tv);
530 /* Read the message from the daemon. */
531 r = xread (sock, buf, sizeof buf);
535 xdrmem_create (&xdr, buf, sizeof buf, XDR_DECODE);
536 xdr_u_int (&xdr, &flag);
539 if (flag != GUESTFS_CANCEL_FLAG) {
540 fprintf (stderr, "guestfsd: check_for_library_cancellation: read 0x%x from library, expected 0x%x\n",
541 flag, GUESTFS_CANCEL_FLAG);
549 send_file_end (int cancel)
553 chunk.cancel = cancel;
554 chunk.data.data_len = 0;
555 chunk.data.data_val = NULL;
556 return send_chunk (&chunk);
560 send_chunk (const guestfs_chunk *chunk)
562 char buf[GUESTFS_MAX_CHUNK_SIZE + 48];
567 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
568 if (!xdr_guestfs_chunk (&xdr, (guestfs_chunk *) chunk)) {
569 fprintf (stderr, "guestfsd: send_chunk: failed to encode chunk\n");
574 len = xdr_getpos (&xdr);
577 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
578 xdr_u_int (&xdr, &len);
581 int err = (xwrite (sock, lenbuf, 4) == 0
582 && xwrite (sock, buf, len) == 0 ? 0 : -1);
584 fprintf (stderr, "guestfsd: send_chunk: write failed\n");
591 /* Initial delay before sending notification messages, and
592 * the period at which we send them thereafter. These times
593 * are in microseconds.
595 #define NOTIFICATION_INITIAL_DELAY 2000000
596 #define NOTIFICATION_PERIOD 333333
599 notify_progress (uint64_t position, uint64_t total)
601 struct timeval now_t;
602 gettimeofday (&now_t, NULL);
604 /* Always send a notification at 100%. This simplifies callers by
605 * allowing them to 'finish' the progress bar at 100% without
606 * needing special code.
608 if (count_progress > 0 && position == total)
611 /* Calculate time in microseconds since the last progress message
612 * was sent out (or since the start of the call).
614 int64_t last_us, now_us, elapsed_us;
616 (int64_t) last_progress_t.tv_sec * 1000000 + last_progress_t.tv_usec;
617 now_us = (int64_t) now_t.tv_sec * 1000000 + now_t.tv_usec;
618 elapsed_us = now_us - last_us;
621 if ((count_progress == 0 && elapsed_us < NOTIFICATION_INITIAL_DELAY) ||
622 (count_progress > 0 && elapsed_us < NOTIFICATION_PERIOD))
626 /* We're going to send a message now ... */
628 last_progress_t = now_t;
630 /* Send the header word. */
633 uint32_t i = GUESTFS_PROGRESS_FLAG;
635 xdrmem_create (&xdr, buf, 4, XDR_ENCODE);
636 xdr_u_int (&xdr, &i);
639 if (xwrite (sock, buf, 4) == -1) {
640 fprintf (stderr, "guestfsd: xwrite failed\n");
644 guestfs_progress message = {
647 .position = position,
651 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
652 if (!xdr_guestfs_progress (&xdr, &message)) {
653 fprintf (stderr, "guestfsd: xdr_guestfs_progress: failed to encode message\n");
657 len = xdr_getpos (&xdr);
660 if (xwrite (sock, buf, len) == -1) {
661 fprintf (stderr, "guestfsd: xwrite failed\n");
666 /* "Pulse mode" progress messages. */
668 #if defined(HAVE_SETITIMER) && defined(HAVE_SIGACTION)
670 static void async_safe_send_pulse (int sig);
673 pulse_mode_start (void)
675 struct sigaction act;
678 memset (&act, 0, sizeof act);
679 act.sa_handler = async_safe_send_pulse;
680 act.sa_flags = SA_RESTART;
682 if (sigaction (SIGALRM, &act, NULL) == -1) {
683 perror ("pulse_mode_start: sigaction");
687 it.it_value.tv_sec = NOTIFICATION_INITIAL_DELAY / 1000000;
688 it.it_value.tv_usec = NOTIFICATION_INITIAL_DELAY % 1000000;
689 it.it_interval.tv_sec = NOTIFICATION_PERIOD / 1000000;
690 it.it_interval.tv_usec = NOTIFICATION_PERIOD % 1000000;
692 if (setitimer (ITIMER_REAL, &it, NULL) == -1)
693 perror ("pulse_mode_start: setitimer");
697 pulse_mode_end (void)
699 pulse_mode_cancel (); /* Cancel the itimer. */
701 notify_progress (1, 1);
705 pulse_mode_cancel (void)
707 int err = errno; /* Function must preserve errno. */
709 struct sigaction act;
711 /* Setting it_value to zero cancels the itimer. */
712 it.it_value.tv_sec = 0;
713 it.it_value.tv_usec = 0;
714 it.it_interval.tv_sec = 0;
715 it.it_interval.tv_usec = 0;
717 if (setitimer (ITIMER_REAL, &it, NULL) == -1)
718 perror ("pulse_mode_cancel: setitimer");
720 memset (&act, 0, sizeof act);
721 act.sa_handler = SIG_DFL;
723 if (sigaction (SIGALRM, &act, NULL) == -1)
724 perror ("pulse_mode_cancel: sigaction");
729 /* Send a position = 0, total = 1 (pulse mode) message. The tricky
730 * part is we have to do it without invoking any non-async-safe
731 * functions (see signal(7) for a list). Therefore, KISS.
734 async_safe_send_pulse (int sig)
736 /* XDR is a RFC ... */
737 unsigned char msg[] = {
738 (GUESTFS_PROGRESS_FLAG & 0xff000000) >> 24,
739 (GUESTFS_PROGRESS_FLAG & 0x00ff0000) >> 16,
740 (GUESTFS_PROGRESS_FLAG & 0x0000ff00) >> 8,
741 GUESTFS_PROGRESS_FLAG & 0x000000ff,
742 (proc_nr & 0xff000000) >> 24,
743 (proc_nr & 0x00ff0000) >> 16,
744 (proc_nr & 0x0000ff00) >> 8,
745 proc_nr & 0x000000ff,
746 (serial & 0xff000000) >> 24,
747 (serial & 0x00ff0000) >> 16,
748 (serial & 0x0000ff00) >> 8,
750 /* 64 bit position = 0 */ 0, 0, 0, 0, 0, 0, 0, 0,
751 /* 64 bit total = 1 */ 0, 0, 0, 0, 0, 0, 0, 1
754 if (xwrite (sock, msg, sizeof msg) == -1)
758 #else /* !HAVE_SETITIMER || !HAVE_SIGACTION */
761 pulse_mode_start (void)
767 pulse_mode_end (void)
773 pulse_mode_cancel (void)
778 #endif /* !HAVE_SETITIMER || !HAVE_SIGACTION */