diff options
author | Richard Jones <rjones@redhat.com> | 2009-04-20 00:22:02 +0100 |
---|---|---|
committer | Richard Jones <rjones@redhat.com> | 2009-04-20 00:22:02 +0100 |
commit | 170f262f0413de843af62b968f6d12c1c476ae7f (patch) | |
tree | b9be7ae0e59f784dfdd57ef063536218ee3f0c7d /daemon | |
parent | d5151686d82b66c50935010fd5458be0e4386bab (diff) | |
download | libguestfs-170f262f0413de843af62b968f6d12c1c476ae7f.tar.gz libguestfs-170f262f0413de843af62b968f6d12c1c476ae7f.tar.xz libguestfs-170f262f0413de843af62b968f6d12c1c476ae7f.zip |
Implement upload and download commands.
Diffstat (limited to 'daemon')
-rw-r--r-- | daemon/blockdev.c | 2 | ||||
-rw-r--r-- | daemon/daemon.h | 29 | ||||
-rw-r--r-- | daemon/guestfsd.c | 20 | ||||
-rw-r--r-- | daemon/proto.c | 250 | ||||
-rw-r--r-- | daemon/upload.c | 95 |
5 files changed, 353 insertions, 43 deletions
diff --git a/daemon/blockdev.c b/daemon/blockdev.c index 094ad5e6..2c075ef8 100644 --- a/daemon/blockdev.c +++ b/daemon/blockdev.c @@ -68,7 +68,7 @@ call_blockdev (const char *device, const char *switc, int extraarg, int prints) if (prints) { if (sscanf (out, "%" SCNi64, &rv) != 1) { - reply_with_error ("%s: expected output, but got nothing"); + reply_with_error ("%s: expected output, but got nothing", argv[0]); free (out); return -1; } diff --git a/daemon/daemon.h b/daemon/daemon.h index b2b81523..bd14b121 100644 --- a/daemon/daemon.h +++ b/daemon/daemon.h @@ -44,6 +44,8 @@ extern int command (char **stdoutput, char **stderror, const char *name, ...); extern int commandv (char **stdoutput, char **stderror, char * const* const argv); +extern int verbose; + /*-- in proto.c --*/ extern int proc_nr; extern int serial; @@ -61,22 +63,29 @@ extern guestfs_lvm_int_lv_list *parse_command_line_lvs (void); extern void main_loop (int sock); /* ordinary daemon functions use these to indicate errors */ -extern void reply_with_error (const char *fs, ...); -extern void reply_with_perror (const char *fs, ...); +extern void reply_with_error (const char *fs, ...) + __attribute__((format (printf,1,2))); +extern void reply_with_perror (const char *fs, ...) + __attribute__((format (printf,1,2))); /* daemon functions that receive files (FileIn) should call * receive_file for each FileIn parameter. */ -#if 0 -extern void receive_file (); -#endif +typedef int (*receive_cb) (void *opaque, const void *buf, int len); +extern int receive_file (receive_cb cb, void *opaque); + +/* daemon functions that receive files (FileIn) can call this + * to cancel incoming transfers (eg. if there is a local error), + * but they MUST then call reply_with_error or reply_with_perror. + */ +extern void cancel_receive (void); /* daemon functions that return files (FileOut) should call - * reply, then send_file for each FileOut parameter. + * reply, then send_file_* for each FileOut parameter. + * Note max write size if GUESTFS_MAX_CHUNK_SIZE. */ -#if 0 -extern void send_file (); -#endif +extern int send_file_write (const void *buf, int len); +extern void send_file_end (int cancel); /* only call this if there is a FileOut parameter */ extern void reply (xdrproc_t xdrp, char *ret); @@ -122,7 +131,7 @@ extern void reply (xdrproc_t xdrp, char *ret); if (strncmp ((path), "/dev/", 5) == 0) \ IS_DEVICE ((path),(errcode)); \ else { \ - NEED_ROOT ((path),(errcode)); \ + NEED_ROOT ((errcode)); \ ABS_PATH ((path),(errcode)); \ } \ } while (0) diff --git a/daemon/guestfsd.c b/daemon/guestfsd.c index c3f5e821..2553ea2c 100644 --- a/daemon/guestfsd.c +++ b/daemon/guestfsd.c @@ -41,6 +41,8 @@ static void usage (void); #define VMCHANNEL_PORT "6666" #define VMCHANNEL_ADDR "10.0.2.4" +int verbose = 0; + int main (int argc, char *argv[]) { @@ -108,6 +110,14 @@ main (int argc, char *argv[]) fclose (fp); buf[n] = '\0'; + /* Set the verbose flag. Not quite right because this will only + * set the flag if host and port aren't set on the command line. + * Don't worry about this for now. (XXX) + */ + verbose = strstr (buf, "guestfs_verbose=1") != NULL; + if (verbose) + printf ("verbose daemon enabled\n"); + p = strstr (buf, "guestfs="); if (p) { @@ -359,10 +369,12 @@ commandv (char **stdoutput, char **stderror, char * const* const argv) if (stdoutput) *stdoutput = NULL; if (stderror) *stderror = NULL; - printf ("%s", argv[0]); - for (i = 1; argv[i] != NULL; ++i) - printf (" %s", argv[i]); - printf ("\n"); + if (verbose) { + printf ("%s", argv[0]); + for (i = 1; argv[i] != NULL; ++i) + printf (" %s", argv[i]); + printf ("\n"); + } if (pipe (so_fd) == -1 || pipe (se_fd) == -1) { perror ("pipe"); diff --git a/daemon/proto.c b/daemon/proto.c index 76dce987..cd61f18d 100644 --- a/daemon/proto.c +++ b/daemon/proto.c @@ -32,11 +32,6 @@ #include "daemon.h" #include "../src/guestfs_protocol.h" -/* XXX We should make this configurable from /proc/cmdline so that the - * verbose setting of the guestfs_h can be inherited here. - */ -#define DEBUG 0 - /* The message currently being processed. */ int proc_nr; int serial; @@ -76,26 +71,26 @@ main_loop (int _sock) xread (sock, buf, len); -#if DEBUG - int i, j; - - for (i = 0; i < len; i += 16) { - printf ("%04x: ", i); - for (j = i; j < MIN (i+16, len); ++j) - printf ("%02x ", (unsigned char) buf[j]); - for (; j < i+16; ++j) - printf (" "); - printf ("|"); - for (j = i; j < MIN (i+16, len); ++j) - if (isprint (buf[j])) - printf ("%c", buf[j]); - else - printf ("."); - for (; j < i+16; ++j) - printf (" "); - printf ("|\n"); + if (verbose) { + int i, j; + + for (i = 0; i < len; i += 16) { + printf ("%04x: ", i); + for (j = i; j < MIN (i+16, len); ++j) + printf ("%02x ", (unsigned char) buf[j]); + for (; j < i+16; ++j) + printf (" "); + printf ("|"); + for (j = i; j < MIN (i+16, len); ++j) + if (isprint (buf[j])) + printf ("%c", buf[j]); + else + printf ("."); + for (; j < i+16; ++j) + printf (" "); + printf ("|\n"); + } } -#endif /* Decode the message header. */ xdrmem_create (&xdr, buf, len, XDR_DECODE); @@ -250,3 +245,210 @@ reply (xdrproc_t xdrp, char *ret) (void) xwrite (sock, lenbuf, 4); (void) xwrite (sock, buf, len); } + +/* Receive file chunks, repeatedly calling 'cb'. */ +int +receive_file (receive_cb cb, void *opaque) +{ + guestfs_chunk chunk; + char lenbuf[4]; + char *buf; + XDR xdr; + int r; + uint32_t len; + + for (;;) { + /* Read the length word. */ + xread (sock, lenbuf, 4); + xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE); + xdr_uint32_t (&xdr, &len); + xdr_destroy (&xdr); + + if (len == GUESTFS_CANCEL_FLAG) + continue; /* Just ignore it. */ + + if (len > GUESTFS_MESSAGE_MAX) { + fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n", + len); + exit (1); + } + + buf = malloc (len); + if (!buf) { + perror ("malloc"); + return -1; + } + + xread (sock, buf, len); + + xdrmem_create (&xdr, buf, len, XDR_DECODE); + memset (&chunk, 0, sizeof chunk); + if (!xdr_guestfs_chunk (&xdr, &chunk)) { + xdr_destroy (&xdr); + free (buf); + return -1; + } + xdr_destroy (&xdr); + free (buf); + + if (verbose) + printf ("receive_file: got chunk: cancel = %d, len = %d, buf = %p\n", + chunk.cancel, chunk.data.data_len, chunk.data.data_val); + + if (chunk.cancel) { + fprintf (stderr, "receive_file: received cancellation from library\n"); + xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk); + return -2; + } + if (chunk.data.data_len == 0) { + xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk); + return 0; /* end of file */ + } + + if (cb) + r = cb (opaque, chunk.data.data_val, chunk.data.data_len); + else + r = 0; + + xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk); + if (r == -1) /* write error */ + return -1; + } +} + +/* Send a cancellation flag back to the library. */ +void +cancel_receive (void) +{ + XDR xdr; + char fbuf[4]; + uint32_t flag = GUESTFS_CANCEL_FLAG; + + xdrmem_create (&xdr, fbuf, sizeof fbuf, XDR_ENCODE); + xdr_uint32_t (&xdr, &flag); + xdr_destroy (&xdr); + + if (xwrite (sock, fbuf, sizeof fbuf) == -1) { + perror ("write to socket"); + return; + } + + /* Keep receiving chunks and discarding, until library sees cancel. */ + (void) receive_file (NULL, NULL); +} + +static int check_for_library_cancellation (void); +static int send_chunk (const guestfs_chunk *); + +/* Also check if the library sends us a cancellation message. */ +int +send_file_write (const void *buf, int len) +{ + guestfs_chunk chunk; + int cancel; + + if (len > GUESTFS_MAX_CHUNK_SIZE) { + fprintf (stderr, "send_file_write: len (%d) > GUESTFS_MAX_CHUNK_SIZE (%d)\n", + len, GUESTFS_MAX_CHUNK_SIZE); + return -1; + } + + cancel = check_for_library_cancellation (); + + if (cancel) { + chunk.cancel = 1; + chunk.data.data_len = 0; + chunk.data.data_val = NULL; + } else { + chunk.cancel = 0; + chunk.data.data_len = len; + chunk.data.data_val = (char *) buf; + } + + if (send_chunk (&chunk) == -1) + return -1; + + if (cancel) return -2; + return 0; +} + +static int +check_for_library_cancellation (void) +{ + fd_set rset; + struct timeval tv; + int r; + char buf[4]; + uint32_t flag; + XDR xdr; + + FD_ZERO (&rset); + FD_SET (sock, &rset); + tv.tv_sec = 0; + tv.tv_usec = 0; + r = select (sock+1, &rset, NULL, NULL, &tv); + if (r == -1) { + perror ("select"); + return 0; + } + if (r == 0) + return 0; + + /* Read the message from the daemon. */ + r = xread (sock, buf, sizeof buf); + if (r == -1) { + perror ("read"); + return 0; + } + + xdrmem_create (&xdr, buf, sizeof buf, XDR_DECODE); + xdr_uint32_t (&xdr, &flag); + xdr_destroy (&xdr); + + if (flag != GUESTFS_CANCEL_FLAG) { + fprintf (stderr, "check_for_library_cancellation: read 0x%x from library, expected 0x%x\n", + flag, GUESTFS_CANCEL_FLAG); + return 0; + } + + return 1; +} + +void +send_file_end (int cancel) +{ + guestfs_chunk chunk; + + chunk.cancel = cancel; + chunk.data.data_len = 0; + chunk.data.data_val = NULL; + send_chunk (&chunk); +} + +static int +send_chunk (const guestfs_chunk *chunk) +{ + char buf[GUESTFS_MAX_CHUNK_SIZE + 48]; + char lenbuf[4]; + XDR xdr; + uint32_t len; + + xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE); + if (!xdr_guestfs_chunk (&xdr, (guestfs_chunk *) chunk)) { + fprintf (stderr, "send_chunk: failed to encode chunk\n"); + xdr_destroy (&xdr); + return -1; + } + + len = xdr_getpos (&xdr); + xdr_destroy (&xdr); + + xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE); + xdr_uint32_t (&xdr, &len); + xdr_destroy (&xdr); + + (void) xwrite (sock, lenbuf, 4); + (void) xwrite (sock, buf, len); + + return 0; +} diff --git a/daemon/upload.c b/daemon/upload.c index d2feb028..b4576950 100644 --- a/daemon/upload.c +++ b/daemon/upload.c @@ -27,14 +27,101 @@ #include "daemon.h" #include "actions.h" +static int +write_cb (void *fd_ptr, const void *buf, int len) +{ + int fd = *(int *)fd_ptr; + return xwrite (fd, buf, len); +} + +/* Has one FileIn parameter. */ int -do_upload (const char *remote_filename) +do_upload (const char *filename) { - XXX_NOT_IMPL (-1); + int err, fd, r, is_dev; + + NEED_ROOT_OR_IS_DEVICE (filename, -1); + + is_dev = strncmp (filename, "/dev/", 5) == 0; + + if (!is_dev) CHROOT_IN; + fd = open (filename, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, 0666); + if (!is_dev) CHROOT_OUT; + if (fd == -1) { + err = errno; + cancel_receive (); + errno = err; + reply_with_perror ("%s", filename); + return -1; + } + + r = receive_file (write_cb, &fd); + if (r == -1) { /* write error */ + err = errno; + cancel_receive (); + errno = err; + reply_with_perror ("write: %s", filename); + return -1; + } + if (r == -2) { /* cancellation from library */ + close (fd); + /* Do NOT send any error. */ + return -1; + } + + if (close (fd) == -1) { + err = errno; + cancel_receive (); + errno = err; + reply_with_perror ("close: %s", filename); + return -1; + } + + return 0; } +/* Has one FileOut parameter. */ int -do_download (const char *remote_filename) +do_download (const char *filename) { - XXX_NOT_IMPL (-1); + int fd, r, is_dev; + char buf[GUESTFS_MAX_CHUNK_SIZE]; + + NEED_ROOT_OR_IS_DEVICE (filename, -1); + + is_dev = strncmp (filename, "/dev/", 5) == 0; + + if (!is_dev) CHROOT_IN; + fd = open (filename, O_RDONLY); + if (!is_dev) CHROOT_OUT; + if (fd == -1) { + reply_with_perror ("%s", filename); + return -1; + } + + /* Now we must send the reply message, before the file contents. After + * this there is no opportunity in the protocol to send any error + * message back. Instead we can only cancel the transfer. + */ + reply (NULL, NULL); + + while ((r = read (fd, buf, sizeof buf)) > 0) { + if (send_file_write (buf, r) < 0) + return -1; + } + + if (r == -1) { + perror (filename); + send_file_end (1); /* Cancel. */ + return -1; + } + + if (close (fd) == -1) { + perror (filename); + send_file_end (1); /* Cancel. */ + return -1; + } + + send_file_end (0); /* Normal end of file. */ + return 0; } |