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"
42 #include "errnostring.h"
44 /* The message currently being processed. */
48 /* Time at which we received the current request. */
49 static struct timeval start_t;
51 /* Time at which the last progress notification was sent. */
52 static struct timeval last_progress_t;
54 /* Counts the number of progress notifications sent during this call. */
55 static int count_progress;
57 /* The daemon communications socket. */
67 struct guestfs_message_header hdr;
72 /* Most common errors are leaked memory and leaked file descriptors,
73 * so run this between each command:
76 ignore_value (system ("ls -l /proc/self/fd"));
78 /* Read the length word. */
79 if (xread (sock, lenbuf, 4) == -1)
82 xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
83 xdr_u_int (&xdr, &len);
86 if (len > GUESTFS_MESSAGE_MAX) {
87 fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n",
94 reply_with_perror ("malloc");
98 if (xread (sock, buf, len) == -1)
101 #ifdef ENABLE_PACKET_DUMP
105 for (i = 0; i < len; i += 16) {
106 printf ("%04zx: ", i);
107 for (j = i; j < MIN (i+16, len); ++j)
108 printf ("%02x ", (unsigned char) buf[j]);
109 for (; j < i+16; ++j)
112 for (j = i; j < MIN (i+16, len); ++j)
113 if (c_isprint (buf[j]))
114 printf ("%c", buf[j]);
117 for (; j < i+16; ++j)
124 gettimeofday (&start_t, NULL);
125 last_progress_t = start_t;
128 /* Decode the message header. */
129 xdrmem_create (&xdr, buf, len, XDR_DECODE);
130 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
131 fprintf (stderr, "guestfsd: could not decode message header\n");
135 /* Check the version etc. */
136 if (hdr.prog != GUESTFS_PROGRAM) {
137 reply_with_error ("wrong program (%d)", hdr.prog);
140 if (hdr.vers != GUESTFS_PROTOCOL_VERSION) {
141 reply_with_error ("wrong protocol version (%d)", hdr.vers);
144 if (hdr.direction != GUESTFS_DIRECTION_CALL) {
145 reply_with_error ("unexpected message direction (%d)", hdr.direction);
148 if (hdr.status != GUESTFS_STATUS_OK) {
149 reply_with_error ("unexpected message status (%d)", hdr.status);
156 /* Clear errors before we call the stub functions. This is just
157 * to ensure that we can accurately report errors in cases where
158 * error handling paths don't set errno correctly.
166 /* Now start to process this message. */
167 dispatch_incoming_message (&xdr);
168 /* Note that dispatch_incoming_message will also send a reply. */
170 /* In verbose mode, display the time taken to run each command. */
172 struct timeval end_t;
173 gettimeofday (&end_t, NULL);
175 int64_t start_us, end_us, elapsed_us;
176 start_us = (int64_t) start_t.tv_sec * 1000000 + start_t.tv_usec;
177 end_us = (int64_t) end_t.tv_sec * 1000000 + end_t.tv_usec;
178 elapsed_us = end_us - start_us;
180 fprintf (stderr, "proc %d (%s) took %d.%02d seconds\n",
182 proc_nr >= 0 && proc_nr < GUESTFS_PROC_NR_PROCS
183 ? function_names[proc_nr] : "UNKNOWN PROCEDURE",
184 (int) (elapsed_us / 1000000),
185 (int) ((elapsed_us / 10000) % 100));
194 static void send_error (int errnum, const char *msg);
197 reply_with_error (const char *fs, ...)
199 char err[GUESTFS_ERROR_LEN];
203 vsnprintf (err, sizeof err, fs, args);
210 reply_with_perror_errno (int err, const char *fs, ...)
212 char buf1[GUESTFS_ERROR_LEN];
213 char buf2[GUESTFS_ERROR_LEN];
217 vsnprintf (buf1, sizeof buf1, fs, args);
220 snprintf (buf2, sizeof buf2, "%s: %s", buf1, strerror (err));
222 send_error (err, buf2);
226 send_error (int errnum, const char *msg)
229 char buf[GUESTFS_ERROR_LEN + 200];
231 struct guestfs_message_header hdr;
232 struct guestfs_message_error err;
235 fprintf (stderr, "guestfsd: error: %s\n", msg);
237 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
239 hdr.prog = GUESTFS_PROGRAM;
240 hdr.vers = GUESTFS_PROTOCOL_VERSION;
241 hdr.direction = GUESTFS_DIRECTION_REPLY;
242 hdr.status = GUESTFS_STATUS_ERROR;
246 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
247 fprintf (stderr, "guestfsd: failed to encode error message header\n");
251 /* These strings are not going to be freed. We just cast them
252 * to (char *) because they are defined that way in the XDR structs.
255 (char *) (errnum > 0 ? guestfs___errno_to_string (errnum) : "");
256 err.error_message = (char *) msg;
258 if (!xdr_guestfs_message_error (&xdr, &err)) {
259 fprintf (stderr, "guestfsd: failed to encode error message body\n");
263 len = xdr_getpos (&xdr);
266 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
267 xdr_u_int (&xdr, &len);
270 if (xwrite (sock, lenbuf, 4) == -1) {
271 fprintf (stderr, "xwrite failed\n");
274 if (xwrite (sock, buf, len) == -1) {
275 fprintf (stderr, "xwrite failed\n");
281 reply (xdrproc_t xdrp, char *ret)
284 char buf[GUESTFS_MESSAGE_MAX];
286 struct guestfs_message_header hdr;
289 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
291 hdr.prog = GUESTFS_PROGRAM;
292 hdr.vers = GUESTFS_PROTOCOL_VERSION;
293 hdr.direction = GUESTFS_DIRECTION_REPLY;
294 hdr.status = GUESTFS_STATUS_OK;
298 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
299 fprintf (stderr, "guestfsd: failed to encode reply header\n");
304 /* This can fail if the reply body is too large, for example
305 * if it exceeds the maximum message size. In that case
306 * we want to return an error message instead. (RHBZ#509597).
308 if (!(*xdrp) (&xdr, ret)) {
309 reply_with_error ("guestfsd: failed to encode reply body\n(maybe the reply exceeds the maximum message size in the protocol?)");
315 len = xdr_getpos (&xdr);
318 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
319 xdr_u_int (&xdr, &len);
322 if (xwrite (sock, lenbuf, 4) == -1) {
323 fprintf (stderr, "xwrite failed\n");
326 if (xwrite (sock, buf, len) == -1) {
327 fprintf (stderr, "xwrite failed\n");
332 /* Receive file chunks, repeatedly calling 'cb'. */
334 receive_file (receive_cb cb, void *opaque)
345 fprintf (stderr, "receive_file: reading length word\n");
347 /* Read the length word. */
348 if (xread (sock, lenbuf, 4) == -1)
351 xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
352 xdr_u_int (&xdr, &len);
355 if (len == GUESTFS_CANCEL_FLAG)
356 continue; /* Just ignore it. */
358 if (len > GUESTFS_MESSAGE_MAX) {
359 fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n",
370 if (xread (sock, buf, len) == -1)
373 xdrmem_create (&xdr, buf, len, XDR_DECODE);
374 memset (&chunk, 0, sizeof chunk);
375 if (!xdr_guestfs_chunk (&xdr, &chunk)) {
384 fprintf (stderr, "receive_file: got chunk: cancel = %d, len = %d, buf = %p\n",
385 chunk.cancel, chunk.data.data_len, chunk.data.data_val);
389 fprintf (stderr, "receive_file: received cancellation from library\n");
390 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
393 if (chunk.data.data_len == 0) {
395 fprintf (stderr, "receive_file: end of file, leaving function\n");
396 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
397 return 0; /* end of file */
401 r = cb (opaque, chunk.data.data_val, chunk.data.data_len);
405 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
406 if (r == -1) { /* write error */
408 fprintf (stderr, "receive_file: write error\n");
414 /* Send a cancellation flag back to the library. */
416 cancel_receive (void)
420 uint32_t flag = GUESTFS_CANCEL_FLAG;
422 xdrmem_create (&xdr, fbuf, sizeof fbuf, XDR_ENCODE);
423 xdr_u_int (&xdr, &flag);
426 if (xwrite (sock, fbuf, sizeof fbuf) == -1) {
427 perror ("write to socket");
431 /* Keep receiving chunks and discarding, until library sees cancel. */
432 return receive_file (NULL, NULL);
435 static int check_for_library_cancellation (void);
436 static int send_chunk (const guestfs_chunk *);
438 /* Also check if the library sends us a cancellation message. */
440 send_file_write (const void *buf, int len)
445 if (len > GUESTFS_MAX_CHUNK_SIZE) {
446 fprintf (stderr, "send_file_write: len (%d) > GUESTFS_MAX_CHUNK_SIZE (%d)\n",
447 len, GUESTFS_MAX_CHUNK_SIZE);
451 cancel = check_for_library_cancellation ();
455 chunk.data.data_len = 0;
456 chunk.data.data_val = NULL;
459 chunk.data.data_len = len;
460 chunk.data.data_val = (char *) buf;
463 if (send_chunk (&chunk) == -1)
466 if (cancel) return -2;
471 check_for_library_cancellation (void)
481 FD_SET (sock, &rset);
484 r = select (sock+1, &rset, NULL, NULL, &tv);
492 /* Read the message from the daemon. */
493 r = xread (sock, buf, sizeof buf);
497 xdrmem_create (&xdr, buf, sizeof buf, XDR_DECODE);
498 xdr_u_int (&xdr, &flag);
501 if (flag != GUESTFS_CANCEL_FLAG) {
502 fprintf (stderr, "check_for_library_cancellation: read 0x%x from library, expected 0x%x\n",
503 flag, GUESTFS_CANCEL_FLAG);
511 send_file_end (int cancel)
515 chunk.cancel = cancel;
516 chunk.data.data_len = 0;
517 chunk.data.data_val = NULL;
518 return send_chunk (&chunk);
522 send_chunk (const guestfs_chunk *chunk)
524 char buf[GUESTFS_MAX_CHUNK_SIZE + 48];
529 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
530 if (!xdr_guestfs_chunk (&xdr, (guestfs_chunk *) chunk)) {
531 fprintf (stderr, "send_chunk: failed to encode chunk\n");
536 len = xdr_getpos (&xdr);
539 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
540 xdr_u_int (&xdr, &len);
543 int err = (xwrite (sock, lenbuf, 4) == 0
544 && xwrite (sock, buf, len) == 0 ? 0 : -1);
546 fprintf (stderr, "send_chunk: write failed\n");
553 /* Initial delay before sending notification messages, and
554 * the period at which we send them thereafter. These times
555 * are in microseconds.
557 #define NOTIFICATION_INITIAL_DELAY 2000000
558 #define NOTIFICATION_PERIOD 333333
561 notify_progress (uint64_t position, uint64_t total)
563 struct timeval now_t;
564 gettimeofday (&now_t, NULL);
566 /* Always send a notification at 100%. This simplifies callers by
567 * allowing them to 'finish' the progress bar at 100% without
568 * needing special code.
570 if (count_progress > 0 && position == total)
573 /* Calculate time in microseconds since the last progress message
574 * was sent out (or since the start of the call).
576 int64_t last_us, now_us, elapsed_us;
578 (int64_t) last_progress_t.tv_sec * 1000000 + last_progress_t.tv_usec;
579 now_us = (int64_t) now_t.tv_sec * 1000000 + now_t.tv_usec;
580 elapsed_us = now_us - last_us;
583 if ((count_progress == 0 && elapsed_us < NOTIFICATION_INITIAL_DELAY) ||
584 (count_progress > 0 && elapsed_us < NOTIFICATION_PERIOD))
588 /* We're going to send a message now ... */
590 last_progress_t = now_t;
592 /* Send the header word. */
595 uint32_t i = GUESTFS_PROGRESS_FLAG;
597 xdrmem_create (&xdr, buf, 4, XDR_ENCODE);
598 xdr_u_int (&xdr, &i);
601 if (xwrite (sock, buf, 4) == -1) {
602 fprintf (stderr, "xwrite failed\n");
606 guestfs_progress message = {
609 .position = position,
613 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
614 if (!xdr_guestfs_progress (&xdr, &message)) {
615 fprintf (stderr, "xdr_guestfs_progress: failed to encode message\n");
619 len = xdr_getpos (&xdr);
622 if (xwrite (sock, buf, len) == -1) {
623 fprintf (stderr, "xwrite failed\n");