summaryrefslogtreecommitdiffstats
path: root/daemon
diff options
context:
space:
mode:
authorRichard Jones <rjones@redhat.com>2009-04-06 11:44:48 +0100
committerRichard Jones <rjones@redhat.com>2009-04-06 11:44:48 +0100
commit1cf85b1e60e85c4940869c6291d75ac44a5bd190 (patch)
tree1ee9a499264042801ffd73f4c999a48a5535973b /daemon
parentbf17bf81fef275892d24458ce5f1e5290b426742 (diff)
downloadlibguestfs-1cf85b1e60e85c4940869c6291d75ac44a5bd190.tar.gz
libguestfs-1cf85b1e60e85c4940869c6291d75ac44a5bd190.tar.xz
libguestfs-1cf85b1e60e85c4940869c6291d75ac44a5bd190.zip
Implementations of 'cat', 'ls', and some cleanups.
Diffstat (limited to 'daemon')
-rw-r--r--daemon/daemon.h15
-rw-r--r--daemon/file.c78
-rw-r--r--daemon/guestfsd.c32
-rw-r--r--daemon/ls.c57
4 files changed, 165 insertions, 17 deletions
diff --git a/daemon/daemon.h b/daemon/daemon.h
index 42d77276..3740595c 100644
--- a/daemon/daemon.h
+++ b/daemon/daemon.h
@@ -26,8 +26,11 @@
/* in guestfsd.c */
extern void xwrite (int sock, const void *buf, size_t len);
extern void xread (int sock, void *buf, size_t len);
+
+extern int add_string (char ***argv, int *size, int *alloc, const char *str);
extern int count_strings (char **argv);
extern void free_strings (char **argv);
+
extern int command (char **stdoutput, char **stderror, const char *name, ...);
/* in proto.c */
@@ -46,15 +49,23 @@ extern void reply_with_error (const char *fs, ...);
extern void reply_with_perror (const char *fs, ...);
extern void reply (xdrproc_t xdrp, char *ret);
-#define NEED_ROOT \
+#define NEED_ROOT(errcode) \
do { \
if (!root_mounted) { \
reply_with_error ("%s: you must call 'mount' first to mount the root filesystem", __func__); \
- return -1; \
+ return (errcode); \
} \
} \
while (0)
+#define ABS_PATH(path,errcode) \
+ do { \
+ if ((path)[0] != '/') { \
+ reply_with_error ("%s: path must start with a / character", __func__); \
+ return (errcode); \
+ } \
+ } while (0)
+
/* NB:
* (1) You must match CHROOT_IN and CHROOT_OUT even along error paths.
* (2) You must not change directory! cwd must always be "/", otherwise
diff --git a/daemon/file.c b/daemon/file.c
index db199182..537618f2 100644
--- a/daemon/file.c
+++ b/daemon/file.c
@@ -27,6 +27,7 @@
#include <fcntl.h>
#include <sys/stat.h>
+#include "../src/guestfs_protocol.h"
#include "daemon.h"
#include "actions.h"
@@ -35,12 +36,8 @@ do_touch (const char *path)
{
int fd;
- NEED_ROOT;
-
- if (path[0] != '/') {
- reply_with_error ("touch: path must start with a / character");
- return -1;
- }
+ NEED_ROOT (-1);
+ ABS_PATH (path, -1);
CHROOT_IN;
fd = open (path, O_WRONLY | O_CREAT | O_NOCTTY | O_NONBLOCK, 0666);
@@ -48,7 +45,6 @@ do_touch (const char *path)
if (fd == -1) {
reply_with_perror ("open: %s", path);
- close (fd);
return -1;
}
@@ -65,6 +61,70 @@ do_touch (const char *path)
char *
do_cat (const char *path)
{
- reply_with_error ("cat command is not yet implemented");
- return NULL;
+ int fd;
+ int alloc, size, r, max;
+ char *buf, *buf2;
+
+ NEED_ROOT (NULL);
+ ABS_PATH (path,NULL);
+
+ CHROOT_IN;
+ fd = open (path, O_RDONLY);
+ CHROOT_OUT;
+
+ if (fd == -1) {
+ reply_with_perror ("open: %s", path);
+ return NULL;
+ }
+
+ /* Read up to GUESTFS_MESSAGE_MAX - <overhead> bytes. If it's
+ * larger than that, we need to return an error instead (for
+ * correctness).
+ */
+ max = GUESTFS_MESSAGE_MAX - 1000;
+ buf = NULL;
+ size = alloc = 0;
+
+ for (;;) {
+ if (size >= alloc) {
+ alloc += 8192;
+ if (alloc > max) {
+ reply_with_error ("cat: %s: file is too large for message buffer",
+ path);
+ free (buf);
+ close (fd);
+ return NULL;
+ }
+ buf2 = realloc (buf, alloc);
+ if (buf2 == NULL) {
+ reply_with_perror ("realloc");
+ free (buf);
+ close (fd);
+ return NULL;
+ }
+ buf = buf2;
+ }
+
+ r = read (fd, buf + size, alloc - size);
+ if (r == -1) {
+ reply_with_perror ("read: %s", path);
+ free (buf);
+ close (fd);
+ return NULL;
+ }
+ if (r == 0) {
+ buf[size] = '\0';
+ break;
+ }
+ if (r > 0)
+ size += r;
+ }
+
+ if (close (fd) == -1) {
+ reply_with_perror ("close: %s", path);
+ free (buf);
+ return NULL;
+ }
+
+ return buf; /* caller will free */
}
diff --git a/daemon/guestfsd.c b/daemon/guestfsd.c
index 9d110d73..6730c1d2 100644
--- a/daemon/guestfsd.c
+++ b/daemon/guestfsd.c
@@ -231,6 +231,38 @@ usage (void)
}
int
+add_string (char ***argv, int *size, int *alloc, const char *str)
+{
+ char **new_argv;
+ char *new_str;
+
+ if (*size >= *alloc) {
+ *alloc += 64;
+ new_argv = realloc (*argv, *alloc * sizeof (char *));
+ if (new_argv == NULL) {
+ reply_with_perror ("realloc");
+ free_strings (*argv);
+ return -1;
+ }
+ *argv = new_argv;
+ }
+
+ if (str) {
+ new_str = strdup (str);
+ if (new_str == NULL) {
+ reply_with_perror ("strdup");
+ free_strings (*argv);
+ }
+ } else
+ new_str = NULL;
+
+ (*argv)[*size] = new_str;
+
+ (*size)++;
+ return 0;
+}
+
+int
count_strings (char **argv)
{
int argc;
diff --git a/daemon/ls.c b/daemon/ls.c
index 1bea5f14..05e2cc70 100644
--- a/daemon/ls.c
+++ b/daemon/ls.c
@@ -23,16 +23,63 @@
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
+#include <dirent.h>
#include <sys/stat.h>
#include "daemon.h"
#include "actions.h"
+static int
+compare (const void *vp1, const void *vp2)
+{
+ char * const *p1 = (char * const *) vp1;
+ char * const *p2 = (char * const *) vp2;
+ return strcmp (*p1, *p2);
+}
+
char **
do_ls (const char *path)
{
- reply_with_error ("ls command is not yet implemented");
- return NULL;
+ char **r = NULL;
+ int size = 0, alloc = 0;
+ DIR *dir;
+ struct dirent *d;
+
+ NEED_ROOT (NULL);
+ ABS_PATH (path, NULL);
+
+ CHROOT_IN;
+ dir = opendir (path);
+ CHROOT_OUT;
+
+ if (!dir) {
+ reply_with_perror ("opendir: %s", path);
+ return NULL;
+ }
+
+ while ((d = readdir (dir)) != NULL) {
+ if (strcmp (d->d_name, ".") == 0 || strcmp (d->d_name, "..") == 0)
+ continue;
+
+ if (add_string (&r, &size, &alloc, d->d_name) == -1) {
+ closedir (dir);
+ return NULL;
+ }
+ }
+
+ if (add_string (&r, &size, &alloc, NULL) == -1) {
+ closedir (dir);
+ return NULL;
+ }
+
+ if (closedir (dir) == -1) {
+ reply_with_perror ("closedir: %s", path);
+ free_strings (r);
+ return NULL;
+ }
+
+ qsort (r, size-1, sizeof (char *), compare);
+ return r;
}
char *
@@ -42,10 +89,8 @@ do_ll (const char *path)
char *out, *err;
char *spath;
- if (path[0] != '/') {
- reply_with_error ("ll: path must start with a / character");
- return NULL;
- }
+ //NEED_ROOT
+ ABS_PATH (path, NULL);
/* This exposes the /sysroot, because we can't chroot and run the ls
* command (since 'ls' won't necessarily exist in the chroot). This