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 <rpc/types.h>
32 #include "ignore-value.h"
35 #include "../src/guestfs_protocol.h"
37 /* The message currently being processed. */
41 /* The daemon communications socket. */
51 struct guestfs_message_header hdr;
52 struct timeval start_t, end_t;
53 int64_t start_us, end_us, elapsed_us;
58 /* Most common errors are leaked memory and leaked file descriptors,
59 * so run this between each command:
62 ignore_value (system ("ls -l /proc/self/fd"));
64 /* Read the length word. */
65 if (xread (sock, lenbuf, 4) == -1)
68 xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
69 xdr_uint32_t (&xdr, &len);
72 if (len > GUESTFS_MESSAGE_MAX) {
73 fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n",
80 reply_with_perror ("malloc");
84 if (xread (sock, buf, len) == -1)
87 #ifdef ENABLE_PACKET_DUMP
91 for (i = 0; i < len; i += 16) {
92 printf ("%04zx: ", i);
93 for (j = i; j < MIN (i+16, len); ++j)
94 printf ("%02x ", (unsigned char) buf[j]);
98 for (j = i; j < MIN (i+16, len); ++j)
99 if (c_isprint (buf[j]))
100 printf ("%c", buf[j]);
103 for (; j < i+16; ++j)
110 /* In verbose mode, display the time taken to run each command. */
112 gettimeofday (&start_t, NULL);
114 /* Decode the message header. */
115 xdrmem_create (&xdr, buf, len, XDR_DECODE);
116 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
117 fprintf (stderr, "guestfsd: could not decode message header\n");
121 /* Check the version etc. */
122 if (hdr.prog != GUESTFS_PROGRAM) {
123 reply_with_error ("wrong program (%d)", hdr.prog);
126 if (hdr.vers != GUESTFS_PROTOCOL_VERSION) {
127 reply_with_error ("wrong protocol version (%d)", hdr.vers);
130 if (hdr.direction != GUESTFS_DIRECTION_CALL) {
131 reply_with_error ("unexpected message direction (%d)", hdr.direction);
134 if (hdr.status != GUESTFS_STATUS_OK) {
135 reply_with_error ("unexpected message status (%d)", hdr.status);
139 /* Now start to process this message. */
142 dispatch_incoming_message (&xdr);
143 /* Note that dispatch_incoming_message will also send a reply. */
145 /* In verbose mode, display the time taken to run each command. */
147 gettimeofday (&end_t, NULL);
149 start_us = (int64_t) start_t.tv_sec * 1000000 + start_t.tv_usec;
150 end_us = (int64_t) end_t.tv_sec * 1000000 + end_t.tv_usec;
151 elapsed_us = end_us - start_us;
152 fprintf (stderr, "proc %d (%s) took %d.%02d seconds\n",
154 proc_nr >= 0 && proc_nr < GUESTFS_PROC_NR_PROCS
155 ? function_names[proc_nr] : "UNKNOWN PROCEDURE",
156 (int) (elapsed_us / 1000000),
157 (int) ((elapsed_us / 10000) % 100));
166 static void send_error (const char *msg);
169 reply_with_error (const char *fs, ...)
171 char err[GUESTFS_ERROR_LEN];
175 vsnprintf (err, sizeof err, fs, args);
182 reply_with_perror (const char *fs, ...)
184 char buf1[GUESTFS_ERROR_LEN];
185 char buf2[GUESTFS_ERROR_LEN];
190 vsnprintf (buf1, sizeof buf1, fs, args);
193 snprintf (buf2, sizeof buf2, "%s: %s", buf1, strerror (err));
199 send_error (const char *msg)
202 char buf[GUESTFS_ERROR_LEN + 200];
204 struct guestfs_message_header hdr;
205 struct guestfs_message_error err;
208 fprintf (stderr, "guestfsd: error: %s\n", msg);
210 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
212 hdr.prog = GUESTFS_PROGRAM;
213 hdr.vers = GUESTFS_PROTOCOL_VERSION;
214 hdr.direction = GUESTFS_DIRECTION_REPLY;
215 hdr.status = GUESTFS_STATUS_ERROR;
219 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
220 fprintf (stderr, "guestfsd: failed to encode error message header\n");
224 err.error_message = (char *) msg;
226 if (!xdr_guestfs_message_error (&xdr, &err)) {
227 fprintf (stderr, "guestfsd: failed to encode error message body\n");
231 len = xdr_getpos (&xdr);
234 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
235 xdr_uint32_t (&xdr, &len);
238 if (xwrite (sock, lenbuf, 4) == -1) {
239 fprintf (stderr, "xwrite failed\n");
242 if (xwrite (sock, buf, len) == -1) {
243 fprintf (stderr, "xwrite failed\n");
249 reply (xdrproc_t xdrp, char *ret)
252 char buf[GUESTFS_MESSAGE_MAX];
254 struct guestfs_message_header hdr;
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_OK;
266 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
267 fprintf (stderr, "guestfsd: failed to encode reply header\n");
272 /* This can fail if the reply body is too large, for example
273 * if it exceeds the maximum message size. In that case
274 * we want to return an error message instead. (RHBZ#509597).
276 if (!(*xdrp) (&xdr, ret)) {
277 reply_with_perror ("guestfsd: failed to encode reply body\n");
283 len = xdr_getpos (&xdr);
286 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
287 xdr_uint32_t (&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");
300 /* Receive file chunks, repeatedly calling 'cb'. */
302 receive_file (receive_cb cb, void *opaque)
312 /* Read the length word. */
313 if (xread (sock, lenbuf, 4) == -1)
316 xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
317 xdr_uint32_t (&xdr, &len);
320 if (len == GUESTFS_CANCEL_FLAG)
321 continue; /* Just ignore it. */
323 if (len > GUESTFS_MESSAGE_MAX) {
324 fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n",
335 if (xread (sock, buf, len) == -1)
338 xdrmem_create (&xdr, buf, len, XDR_DECODE);
339 memset (&chunk, 0, sizeof chunk);
340 if (!xdr_guestfs_chunk (&xdr, &chunk)) {
349 printf ("receive_file: got chunk: cancel = %d, len = %d, buf = %p\n",
350 chunk.cancel, chunk.data.data_len, chunk.data.data_val);
353 fprintf (stderr, "receive_file: received cancellation from library\n");
354 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
357 if (chunk.data.data_len == 0) {
358 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
359 return 0; /* end of file */
363 r = cb (opaque, chunk.data.data_val, chunk.data.data_len);
367 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
368 if (r == -1) /* write error */
373 /* Send a cancellation flag back to the library. */
375 cancel_receive (void)
379 uint32_t flag = GUESTFS_CANCEL_FLAG;
381 xdrmem_create (&xdr, fbuf, sizeof fbuf, XDR_ENCODE);
382 xdr_uint32_t (&xdr, &flag);
385 if (xwrite (sock, fbuf, sizeof fbuf) == -1) {
386 perror ("write to socket");
390 /* Keep receiving chunks and discarding, until library sees cancel. */
391 (void) receive_file (NULL, NULL);
394 static int check_for_library_cancellation (void);
395 static int send_chunk (const guestfs_chunk *);
397 /* Also check if the library sends us a cancellation message. */
399 send_file_write (const void *buf, int len)
404 if (len > GUESTFS_MAX_CHUNK_SIZE) {
405 fprintf (stderr, "send_file_write: len (%d) > GUESTFS_MAX_CHUNK_SIZE (%d)\n",
406 len, GUESTFS_MAX_CHUNK_SIZE);
410 cancel = check_for_library_cancellation ();
414 chunk.data.data_len = 0;
415 chunk.data.data_val = NULL;
418 chunk.data.data_len = len;
419 chunk.data.data_val = (char *) buf;
422 if (send_chunk (&chunk) == -1)
425 if (cancel) return -2;
430 check_for_library_cancellation (void)
440 FD_SET (sock, &rset);
443 r = select (sock+1, &rset, NULL, NULL, &tv);
451 /* Read the message from the daemon. */
452 r = xread (sock, buf, sizeof buf);
456 xdrmem_create (&xdr, buf, sizeof buf, XDR_DECODE);
457 xdr_uint32_t (&xdr, &flag);
460 if (flag != GUESTFS_CANCEL_FLAG) {
461 fprintf (stderr, "check_for_library_cancellation: read 0x%x from library, expected 0x%x\n",
462 flag, GUESTFS_CANCEL_FLAG);
470 send_file_end (int cancel)
474 chunk.cancel = cancel;
475 chunk.data.data_len = 0;
476 chunk.data.data_val = NULL;
477 return send_chunk (&chunk);
481 send_chunk (const guestfs_chunk *chunk)
483 char buf[GUESTFS_MAX_CHUNK_SIZE + 48];
488 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
489 if (!xdr_guestfs_chunk (&xdr, (guestfs_chunk *) chunk)) {
490 fprintf (stderr, "send_chunk: failed to encode chunk\n");
495 len = xdr_getpos (&xdr);
498 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
499 xdr_uint32_t (&xdr, &len);
502 int err = (xwrite (sock, lenbuf, 4) == 0
503 && xwrite (sock, buf, len) == 0 ? 0 : -1);
505 fprintf (stderr, "send_chunk: write failed\n");