Generated code for lvremove, vgremove, pvremove.
[libguestfs.git] / daemon / debug.c
index 22baa9b..e4a7f05 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 #include <unistd.h>
+#include <dirent.h>
 
 #include "../src/guestfs_protocol.h"
 #include "daemon.h"
@@ -42,21 +45,13 @@ struct cmd {
 };
 
 static char *debug_help (const char *subcmd, int argc, char *const *const argv);
-#if 0
 static char *debug_fds (const char *subcmd, int argc, char *const *const argv);
-static char *debug_free (const char *subcmd, int argc, char *const *const argv);
-static char *debug_mem (const char *subcmd, int argc, char *const *const argv);
-static char *debug_ps (const char *subcmd, int argc, char *const *const argv);
-#endif
+static char *debug_sh (const char *subcmd, int argc, char *const *const argv);
 
 static struct cmd cmds[] = {
   { "help", debug_help },
-#if 0
   { "fds", debug_fds },
-  { "free", debug_free },
-  { "mem", debug_mem },
-  { "ps", debug_free },
-#endif
+  { "sh", debug_sh },
   { NULL, NULL }
 };
 #endif
@@ -114,25 +109,92 @@ debug_help (const char *subcmd, int argc, char *const *const argv)
   return r;
 }
 
-#if 0
+/* Show open FDs. */
 static char *
 debug_fds (const char *subcmd, int argc, char *const *const argv)
 {
-}
+  int r;
+  char *out;
+  size_t size;
+  FILE *fp;
+  DIR *dir;
+  struct dirent *d;
+  char fname[256], link[256];
+  struct stat statbuf;
+
+  fp = open_memstream (&out, &size);
+  if (!fp) {
+    reply_with_perror ("open_memstream");
+    return NULL;
+  }
 
-static char *
-debug_free (const char *subcmd, int argc, char *const *const argv)
-{
-}
+  dir = opendir ("/proc/self/fd");
+  if (!dir) {
+    reply_with_perror ("opendir: /proc/self/fd");
+    fclose (fp);
+    return NULL;
+  }
 
-static char *
-debug_mem (const char *subcmd, int argc, char *const *const argv)
-{
+  while ((d = readdir (dir)) != NULL) {
+    if (strcmp (d->d_name, ".") == 0 || strcmp (d->d_name, "..") == 0)
+      continue;
+
+    snprintf (fname, sizeof fname, "/proc/self/fd/%s", d->d_name);
+
+    r = lstat (fname, &statbuf);
+    if (r == -1) {
+      reply_with_perror ("stat: %s", fname);
+      fclose (fp);
+      free (out);
+      closedir (dir);
+      return NULL;
+    }
+
+    if (S_ISLNK (statbuf.st_mode)) {
+      r = readlink (fname, link, sizeof link - 1);
+      if (r == -1) {
+       reply_with_perror ("readline: %s", fname);
+       fclose (fp);
+       free (out);
+       closedir (dir);
+       return NULL;
+      }
+      link[r] = '\0';
+
+      fprintf (fp, "%2s %s\n", d->d_name, link);
+    } else
+      fprintf (fp, "%2s 0%o\n", d->d_name, statbuf.st_mode);
+  }
+
+  fclose (fp);
+
+  if (closedir (dir) == -1) {
+    reply_with_perror ("closedir");
+    free (out);
+    return NULL;
+  }
+
+  return out;
 }
 
+/* Run an arbitrary shell command. */
 static char *
-debug_ps (const char *subcmd, int argc, char *const *const argv)
+debug_sh (const char *subcmd, int argc, char *const *const argv)
 {
+  int r;
+  char *out, *err;
+
+  r = commandv (&out, &err, argv);
+  if (r == -1) {
+    reply_with_error ("ps: %s", err);
+    free (out);
+    free (err);
+    return NULL;
+  }
+
+  free (err);
+
+  return out;
 }
-#endif
+
 #endif /* ENABLE_DEBUG_COMMAND */