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>
29 #include <rpc/types.h>
37 #include "ignore-value.h"
40 #include "../src/guestfs_protocol.h"
42 /* The message currently being processed. */
46 /* The daemon communications socket. */
56 struct guestfs_message_header hdr;
57 struct timeval start_t, end_t;
58 int64_t start_us, end_us, elapsed_us;
63 /* Most common errors are leaked memory and leaked file descriptors,
64 * so run this between each command:
67 ignore_value (system ("ls -l /proc/self/fd"));
69 /* Read the length word. */
70 if (xread (sock, lenbuf, 4) == -1)
73 xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
74 xdr_u_int (&xdr, &len);
77 if (len > GUESTFS_MESSAGE_MAX) {
78 fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n",
85 reply_with_perror ("malloc");
89 if (xread (sock, buf, len) == -1)
92 #ifdef ENABLE_PACKET_DUMP
96 for (i = 0; i < len; i += 16) {
97 printf ("%04zx: ", i);
98 for (j = i; j < MIN (i+16, len); ++j)
99 printf ("%02x ", (unsigned char) buf[j]);
100 for (; j < i+16; ++j)
103 for (j = i; j < MIN (i+16, len); ++j)
104 if (c_isprint (buf[j]))
105 printf ("%c", buf[j]);
108 for (; j < i+16; ++j)
115 /* In verbose mode, display the time taken to run each command. */
117 gettimeofday (&start_t, NULL);
119 /* Decode the message header. */
120 xdrmem_create (&xdr, buf, len, XDR_DECODE);
121 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
122 fprintf (stderr, "guestfsd: could not decode message header\n");
126 /* Check the version etc. */
127 if (hdr.prog != GUESTFS_PROGRAM) {
128 reply_with_error ("wrong program (%d)", hdr.prog);
131 if (hdr.vers != GUESTFS_PROTOCOL_VERSION) {
132 reply_with_error ("wrong protocol version (%d)", hdr.vers);
135 if (hdr.direction != GUESTFS_DIRECTION_CALL) {
136 reply_with_error ("unexpected message direction (%d)", hdr.direction);
139 if (hdr.status != GUESTFS_STATUS_OK) {
140 reply_with_error ("unexpected message status (%d)", hdr.status);
147 /* Clear errors before we call the stub functions. This is just
148 * to ensure that we can accurately report errors in cases where
149 * error handling paths don't set errno correctly.
157 /* Now start to process this message. */
158 dispatch_incoming_message (&xdr);
159 /* Note that dispatch_incoming_message will also send a reply. */
161 /* In verbose mode, display the time taken to run each command. */
163 gettimeofday (&end_t, NULL);
165 start_us = (int64_t) start_t.tv_sec * 1000000 + start_t.tv_usec;
166 end_us = (int64_t) end_t.tv_sec * 1000000 + end_t.tv_usec;
167 elapsed_us = end_us - start_us;
168 fprintf (stderr, "proc %d (%s) took %d.%02d seconds\n",
170 proc_nr >= 0 && proc_nr < GUESTFS_PROC_NR_PROCS
171 ? function_names[proc_nr] : "UNKNOWN PROCEDURE",
172 (int) (elapsed_us / 1000000),
173 (int) ((elapsed_us / 10000) % 100));
182 static void send_error (const char *msg);
185 reply_with_error (const char *fs, ...)
187 char err[GUESTFS_ERROR_LEN];
191 vsnprintf (err, sizeof err, fs, args);
198 reply_with_perror_errno (int err, const char *fs, ...)
200 char buf1[GUESTFS_ERROR_LEN];
201 char buf2[GUESTFS_ERROR_LEN];
205 vsnprintf (buf1, sizeof buf1, fs, args);
208 snprintf (buf2, sizeof buf2, "%s: %s", buf1, strerror (err));
214 send_error (const char *msg)
217 char buf[GUESTFS_ERROR_LEN + 200];
219 struct guestfs_message_header hdr;
220 struct guestfs_message_error err;
223 fprintf (stderr, "guestfsd: error: %s\n", msg);
225 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
227 hdr.prog = GUESTFS_PROGRAM;
228 hdr.vers = GUESTFS_PROTOCOL_VERSION;
229 hdr.direction = GUESTFS_DIRECTION_REPLY;
230 hdr.status = GUESTFS_STATUS_ERROR;
234 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
235 fprintf (stderr, "guestfsd: failed to encode error message header\n");
239 err.error_message = (char *) msg;
241 if (!xdr_guestfs_message_error (&xdr, &err)) {
242 fprintf (stderr, "guestfsd: failed to encode error message body\n");
246 len = xdr_getpos (&xdr);
249 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
250 xdr_u_int (&xdr, &len);
253 if (xwrite (sock, lenbuf, 4) == -1) {
254 fprintf (stderr, "xwrite failed\n");
257 if (xwrite (sock, buf, len) == -1) {
258 fprintf (stderr, "xwrite failed\n");
264 reply (xdrproc_t xdrp, char *ret)
267 char buf[GUESTFS_MESSAGE_MAX];
269 struct guestfs_message_header hdr;
272 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
274 hdr.prog = GUESTFS_PROGRAM;
275 hdr.vers = GUESTFS_PROTOCOL_VERSION;
276 hdr.direction = GUESTFS_DIRECTION_REPLY;
277 hdr.status = GUESTFS_STATUS_OK;
281 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
282 fprintf (stderr, "guestfsd: failed to encode reply header\n");
287 /* This can fail if the reply body is too large, for example
288 * if it exceeds the maximum message size. In that case
289 * we want to return an error message instead. (RHBZ#509597).
291 if (!(*xdrp) (&xdr, ret)) {
292 reply_with_error ("guestfsd: failed to encode reply body\n(maybe the reply exceeds the maximum message size in the protocol?)");
298 len = xdr_getpos (&xdr);
301 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
302 xdr_u_int (&xdr, &len);
305 if (xwrite (sock, lenbuf, 4) == -1) {
306 fprintf (stderr, "xwrite failed\n");
309 if (xwrite (sock, buf, len) == -1) {
310 fprintf (stderr, "xwrite failed\n");
315 /* Receive file chunks, repeatedly calling 'cb'. */
317 receive_file (receive_cb cb, void *opaque)
328 fprintf (stderr, "receive_file: reading length word\n");
330 /* Read the length word. */
331 if (xread (sock, lenbuf, 4) == -1)
334 xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
335 xdr_u_int (&xdr, &len);
338 if (len == GUESTFS_CANCEL_FLAG)
339 continue; /* Just ignore it. */
341 if (len > GUESTFS_MESSAGE_MAX) {
342 fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n",
353 if (xread (sock, buf, len) == -1)
356 xdrmem_create (&xdr, buf, len, XDR_DECODE);
357 memset (&chunk, 0, sizeof chunk);
358 if (!xdr_guestfs_chunk (&xdr, &chunk)) {
367 fprintf (stderr, "receive_file: got chunk: cancel = %d, len = %d, buf = %p\n",
368 chunk.cancel, chunk.data.data_len, chunk.data.data_val);
372 fprintf (stderr, "receive_file: received cancellation from library\n");
373 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
376 if (chunk.data.data_len == 0) {
378 fprintf (stderr, "receive_file: end of file, leaving function\n");
379 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
380 return 0; /* end of file */
384 r = cb (opaque, chunk.data.data_val, chunk.data.data_len);
388 xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
389 if (r == -1) { /* write error */
391 fprintf (stderr, "receive_file: write error\n");
397 /* Send a cancellation flag back to the library. */
399 cancel_receive (void)
403 uint32_t flag = GUESTFS_CANCEL_FLAG;
405 xdrmem_create (&xdr, fbuf, sizeof fbuf, XDR_ENCODE);
406 xdr_u_int (&xdr, &flag);
409 if (xwrite (sock, fbuf, sizeof fbuf) == -1) {
410 perror ("write to socket");
414 /* Keep receiving chunks and discarding, until library sees cancel. */
415 (void) receive_file (NULL, NULL);
418 static int check_for_library_cancellation (void);
419 static int send_chunk (const guestfs_chunk *);
421 /* Also check if the library sends us a cancellation message. */
423 send_file_write (const void *buf, int len)
428 if (len > GUESTFS_MAX_CHUNK_SIZE) {
429 fprintf (stderr, "send_file_write: len (%d) > GUESTFS_MAX_CHUNK_SIZE (%d)\n",
430 len, GUESTFS_MAX_CHUNK_SIZE);
434 cancel = check_for_library_cancellation ();
438 chunk.data.data_len = 0;
439 chunk.data.data_val = NULL;
442 chunk.data.data_len = len;
443 chunk.data.data_val = (char *) buf;
446 if (send_chunk (&chunk) == -1)
449 if (cancel) return -2;
454 check_for_library_cancellation (void)
464 FD_SET (sock, &rset);
467 r = select (sock+1, &rset, NULL, NULL, &tv);
475 /* Read the message from the daemon. */
476 r = xread (sock, buf, sizeof buf);
480 xdrmem_create (&xdr, buf, sizeof buf, XDR_DECODE);
481 xdr_u_int (&xdr, &flag);
484 if (flag != GUESTFS_CANCEL_FLAG) {
485 fprintf (stderr, "check_for_library_cancellation: read 0x%x from library, expected 0x%x\n",
486 flag, GUESTFS_CANCEL_FLAG);
494 send_file_end (int cancel)
498 chunk.cancel = cancel;
499 chunk.data.data_len = 0;
500 chunk.data.data_val = NULL;
501 return send_chunk (&chunk);
505 send_chunk (const guestfs_chunk *chunk)
507 char buf[GUESTFS_MAX_CHUNK_SIZE + 48];
512 xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
513 if (!xdr_guestfs_chunk (&xdr, (guestfs_chunk *) chunk)) {
514 fprintf (stderr, "send_chunk: failed to encode chunk\n");
519 len = xdr_getpos (&xdr);
522 xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
523 xdr_u_int (&xdr, &len);
526 int err = (xwrite (sock, lenbuf, 4) == 0
527 && xwrite (sock, buf, len) == 0 ? 0 : -1);
529 fprintf (stderr, "send_chunk: write failed\n");