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);
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;
200 fprintf (stderr, "proc %d (%s) took %d.%02d seconds\n",
202 proc_nr >= 0 && proc_nr < GUESTFS_PROC_NR_PROCS
203 ? function_names[proc_nr] : "UNKNOWN PROCEDURE",
204 (int) (elapsed_us / 1000000),
205 (int) ((elapsed_us / 10000) % 100));
214 static void send_error (int errnum, const char *msg);
217 reply_with_error (const char *fs, ...)
219 char err[GUESTFS_ERROR_LEN];
223 vsnprintf (err, sizeof err, fs, args);
230 reply_with_perror_errno (int err, const char *fs, ...)
232 char buf1[GUESTFS_ERROR_LEN];
233 char buf2[GUESTFS_ERROR_LEN];
237 vsnprintf (buf1, sizeof buf1, fs, args);
240 snprintf (buf2, sizeof buf2, "%s: %s", buf1, strerror (err));
242 send_error (err, buf2);
246 send_error (int errnum, const char *msg)
249 char buf[GUESTFS_ERROR_LEN + 200];
251 struct guestfs_message_header hdr;
252 struct guestfs_message_error err;
255 fprintf (stderr, "guestfsd: error: %s\n", msg);
257 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
259 hdr.prog = GUESTFS_PROGRAM;
260 hdr.vers = GUESTFS_PROTOCOL_VERSION;
261 hdr.direction = GUESTFS_DIRECTION_REPLY;
262 hdr.status = GUESTFS_STATUS_ERROR;
266 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
267 fprintf (stderr, "guestfsd: failed to encode error message header\n");
271 /* These strings are not going to be freed. We just cast them
272 * to (char *) because they are defined that way in the XDR structs.
275 (char *) (errnum > 0 ? guestfs___errno_to_string (errnum) : "");
276 err.error_message = (char *) msg;
278 if (!xdr_guestfs_message_error (&xdr, &err)) {
279 fprintf (stderr, "guestfsd: failed to encode error message body\n");
283 len = xdr_getpos (&xdr);
286 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
287 xdr_u_int (&xdr, &len);
290 if (xwrite (sock, lenbuf, 4) == -1) {
291 fprintf (stderr, "xwrite failed\n");
294 if (xwrite (sock, buf, len) == -1) {
295 fprintf (stderr, "xwrite failed\n");
301 reply (xdrproc_t xdrp, char *ret)
304 char buf[GUESTFS_MESSAGE_MAX];
306 struct guestfs_message_header hdr;
309 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
311 hdr.prog = GUESTFS_PROGRAM;
312 hdr.vers = GUESTFS_PROTOCOL_VERSION;
313 hdr.direction = GUESTFS_DIRECTION_REPLY;
314 hdr.status = GUESTFS_STATUS_OK;
318 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
319 fprintf (stderr, "guestfsd: failed to encode reply header\n");
324 /* This can fail if the reply body is too large, for example
325 * if it exceeds the maximum message size. In that case
326 * we want to return an error message instead. (RHBZ#509597).
328 if (!(*xdrp) (&xdr, ret)) {
329 reply_with_error ("guestfsd: failed to encode reply body\n(maybe the reply exceeds the maximum message size in the protocol?)");
335 len = xdr_getpos (&xdr);
338 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
339 xdr_u_int (&xdr, &len);
342 if (xwrite (sock, lenbuf, 4) == -1) {
343 fprintf (stderr, "xwrite failed\n");
346 if (xwrite (sock, buf, len) == -1) {
347 fprintf (stderr, "xwrite failed\n");
352 /* Receive file chunks, repeatedly calling 'cb'. */
354 receive_file (receive_cb cb, void *opaque)
365 fprintf (stderr, "receive_file: reading length word\n");
367 /* Read the length word. */
368 if (xread (sock, lenbuf, 4) == -1)
371 xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
372 xdr_u_int (&xdr, &len);
375 if (len == GUESTFS_CANCEL_FLAG)
376 continue; /* Just ignore it. */
378 if (len > GUESTFS_MESSAGE_MAX) {
379 fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n",
390 if (xread (sock, buf, len) == -1)
393 xdrmem_create (&xdr, buf, len, XDR_DECODE);
394 memset (&chunk, 0, sizeof chunk);
395 if (!xdr_guestfs_chunk (&xdr, &chunk)) {
404 fprintf (stderr, "receive_file: got chunk: cancel = %d, len = %d, buf = %p\n",
405 chunk.cancel, chunk.data.data_len, chunk.data.data_val);
409 fprintf (stderr, "receive_file: received cancellation from library\n");
410 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
413 if (chunk.data.data_len == 0) {
415 fprintf (stderr, "receive_file: end of file, leaving function\n");
416 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
417 return 0; /* end of file */
420 /* Note that the callback can generate progress messages. */
422 r = cb (opaque, chunk.data.data_val, chunk.data.data_len);
426 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
427 if (r == -1) { /* write error */
429 fprintf (stderr, "receive_file: write error\n");
435 /* Send a cancellation flag back to the library. */
437 cancel_receive (void)
441 uint32_t flag = GUESTFS_CANCEL_FLAG;
443 xdrmem_create (&xdr, fbuf, sizeof fbuf, XDR_ENCODE);
444 xdr_u_int (&xdr, &flag);
447 if (xwrite (sock, fbuf, sizeof fbuf) == -1) {
448 perror ("write to socket");
452 /* Keep receiving chunks and discarding, until library sees cancel. */
453 return receive_file (NULL, NULL);
456 static int check_for_library_cancellation (void);
457 static int send_chunk (const guestfs_chunk *);
459 /* Also check if the library sends us a cancellation message. */
461 send_file_write (const void *buf, int len)
466 if (len > GUESTFS_MAX_CHUNK_SIZE) {
467 fprintf (stderr, "send_file_write: len (%d) > GUESTFS_MAX_CHUNK_SIZE (%d)\n",
468 len, GUESTFS_MAX_CHUNK_SIZE);
472 cancel = check_for_library_cancellation ();
476 chunk.data.data_len = 0;
477 chunk.data.data_val = NULL;
480 chunk.data.data_len = len;
481 chunk.data.data_val = (char *) buf;
484 if (send_chunk (&chunk) == -1)
487 if (cancel) return -2;
492 check_for_library_cancellation (void)
502 FD_SET (sock, &rset);
505 r = select (sock+1, &rset, NULL, NULL, &tv);
513 /* Read the message from the daemon. */
514 r = xread (sock, buf, sizeof buf);
518 xdrmem_create (&xdr, buf, sizeof buf, XDR_DECODE);
519 xdr_u_int (&xdr, &flag);
522 if (flag != GUESTFS_CANCEL_FLAG) {
523 fprintf (stderr, "check_for_library_cancellation: read 0x%x from library, expected 0x%x\n",
524 flag, GUESTFS_CANCEL_FLAG);
532 send_file_end (int cancel)
536 chunk.cancel = cancel;
537 chunk.data.data_len = 0;
538 chunk.data.data_val = NULL;
539 return send_chunk (&chunk);
543 send_chunk (const guestfs_chunk *chunk)
545 char buf[GUESTFS_MAX_CHUNK_SIZE + 48];
550 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
551 if (!xdr_guestfs_chunk (&xdr, (guestfs_chunk *) chunk)) {
552 fprintf (stderr, "send_chunk: failed to encode chunk\n");
557 len = xdr_getpos (&xdr);
560 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
561 xdr_u_int (&xdr, &len);
564 int err = (xwrite (sock, lenbuf, 4) == 0
565 && xwrite (sock, buf, len) == 0 ? 0 : -1);
567 fprintf (stderr, "send_chunk: write failed\n");
574 /* Initial delay before sending notification messages, and
575 * the period at which we send them thereafter. These times
576 * are in microseconds.
578 #define NOTIFICATION_INITIAL_DELAY 2000000
579 #define NOTIFICATION_PERIOD 333333
582 notify_progress (uint64_t position, uint64_t total)
584 struct timeval now_t;
585 gettimeofday (&now_t, NULL);
587 /* Always send a notification at 100%. This simplifies callers by
588 * allowing them to 'finish' the progress bar at 100% without
589 * needing special code.
591 if (count_progress > 0 && position == total)
594 /* Calculate time in microseconds since the last progress message
595 * was sent out (or since the start of the call).
597 int64_t last_us, now_us, elapsed_us;
599 (int64_t) last_progress_t.tv_sec * 1000000 + last_progress_t.tv_usec;
600 now_us = (int64_t) now_t.tv_sec * 1000000 + now_t.tv_usec;
601 elapsed_us = now_us - last_us;
604 if ((count_progress == 0 && elapsed_us < NOTIFICATION_INITIAL_DELAY) ||
605 (count_progress > 0 && elapsed_us < NOTIFICATION_PERIOD))
609 /* We're going to send a message now ... */
611 last_progress_t = now_t;
613 /* Send the header word. */
616 uint32_t i = GUESTFS_PROGRESS_FLAG;
618 xdrmem_create (&xdr, buf, 4, XDR_ENCODE);
619 xdr_u_int (&xdr, &i);
622 if (xwrite (sock, buf, 4) == -1) {
623 fprintf (stderr, "xwrite failed\n");
627 guestfs_progress message = {
630 .position = position,
634 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
635 if (!xdr_guestfs_progress (&xdr, &message)) {
636 fprintf (stderr, "xdr_guestfs_progress: failed to encode message\n");
640 len = xdr_getpos (&xdr);
643 if (xwrite (sock, buf, len) == -1) {
644 fprintf (stderr, "xwrite failed\n");