diff options
author | Richard Jones <rjones@redhat.com> | 2009-06-10 12:48:26 +0100 |
---|---|---|
committer | Richard Jones <rjones@redhat.com> | 2009-06-10 14:16:54 +0100 |
commit | 875dc84cc01dacb1e254e294a66e179b96fbdbc6 (patch) | |
tree | 92fc6dbdfd1a7ecdb3defd855129254d25bdbdfa /daemon | |
parent | 56bef498f46ac3dd580f4bde3c8f3ed2fe688826 (diff) | |
download | libguestfs-875dc84cc01dacb1e254e294a66e179b96fbdbc6.tar.gz libguestfs-875dc84cc01dacb1e254e294a66e179b96fbdbc6.tar.xz libguestfs-875dc84cc01dacb1e254e294a66e179b96fbdbc6.zip |
Implement device name translation. Remove device name hacks in tests.
Diffstat (limited to 'daemon')
-rw-r--r-- | daemon/daemon.h | 15 | ||||
-rw-r--r-- | daemon/guestfsd.c | 49 |
2 files changed, 57 insertions, 7 deletions
diff --git a/daemon/daemon.h b/daemon/daemon.h index 8ad7b7c7..c3b91209 100644 --- a/daemon/daemon.h +++ b/daemon/daemon.h @@ -22,8 +22,6 @@ #include <stdarg.h> #include <errno.h> #include <unistd.h> -#include <sys/types.h> -#include <sys/stat.h> #include <rpc/types.h> #include <rpc/xdr.h> @@ -51,6 +49,8 @@ extern char **split_lines (char *str); extern int shell_quote (char *out, int len, const char *in); +extern int device_name_translation (char *device, const char *func); + extern int verbose; /*-- in proto.c --*/ @@ -120,20 +120,21 @@ extern void reply (xdrproc_t xdrp, char *ret); } \ } while (0) -/* Helper for functions that need an argument ("path") that is a device. +/* All functions that need an argument that is a device or partition name + * must call this macro. It checks that the device exists and does + * device name translation (described in the guestfs(3) manpage). + * Note that the "path" argument may be modified. + * * NB. Cannot be used for FileIn functions. */ #define IS_DEVICE(path,errcode) \ do { \ - struct stat statbuf; \ if (strncmp ((path), "/dev/", 5) != 0) { \ reply_with_error ("%s: %s: expecting a device name", __func__, (path)); \ return (errcode); \ } \ - if (stat ((path), &statbuf) == -1) { \ - reply_with_perror ("%s: %s", __func__, (path)); \ + if (device_name_translation ((path), __func__) == -1) \ return (errcode); \ - } \ } while (0) /* Helper for functions which need either an absolute path in the diff --git a/daemon/guestfsd.c b/daemon/guestfsd.c index 1d740545..2e83b9fa 100644 --- a/daemon/guestfsd.c +++ b/daemon/guestfsd.c @@ -32,6 +32,8 @@ #include <sys/select.h> #include <sys/types.h> #include <sys/wait.h> +#include <sys/stat.h> +#include <fcntl.h> #include <ctype.h> #include <signal.h> @@ -685,3 +687,50 @@ shell_quote (char *out, int len, const char *in) return outlen; } + +/* Perform device name translation. Don't call this directly - + * use the IS_DEVICE macro. + * + * See guestfs(3) for the algorithm. + * + * We have to open the device and test for ENXIO, because + * the device nodes themselves will exist in the appliance. + */ +int +device_name_translation (char *device, const char *func) +{ + int fd; + + fd = open (device, O_RDONLY); + if (fd >= 0) { + close (fd); + return 0; + } + + if (errno != ENXIO) { + error: + reply_with_perror ("%s: %s", func, device); + return -1; + } + + /* If the name begins with "/dev/sd" then try the alternatives. */ + if (strncmp (device, "/dev/sd", 7) != 0) + goto error; + + device[5] = 'h'; /* /dev/hd (old IDE driver) */ + fd = open (device, O_RDONLY); + if (fd >= 0) { + close (fd); + return 0; + } + + device[5] = 'v'; /* /dev/vd (for virtio devices) */ + fd = open (device, O_RDONLY); + if (fd >= 0) { + close (fd); + return 0; + } + + device[5] = 's'; /* Restore original device name. */ + goto error; +} |