New API: set-smp, get-smp
[libguestfs.git] / src / guestfs.c
index 9c4f22f..f7ad967 100644 (file)
@@ -1,5 +1,5 @@
 /* libguestfs
- * Copyright (C) 2009-2010 Red Hat Inc.
+ * Copyright (C) 2009-2011 Red Hat Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -72,6 +72,7 @@
 #include "guestfs_protocol.h"
 
 static void default_error_cb (guestfs_h *g, void *data, const char *msg);
+static void remove_tmpdir (guestfs_h *g);
 static void close_handles (void);
 
 gl_lock_define_initialized (static, handles_lock);
@@ -143,6 +144,9 @@ guestfs_create (void)
    */
   g->msg_next_serial = 0x00123400;
 
+  /* Default is uniprocessor appliance. */
+  g->smp = 1;
+
   /* Link the handles onto a global list. */
   gl_lock_lock (handles_lock);
   g->next = handles;
@@ -168,16 +172,19 @@ guestfs_create (void)
 void
 guestfs_close (guestfs_h *g)
 {
-  int i;
-  char filename[256];
-  guestfs_h *gg;
-
   if (g->state == NO_HANDLE) {
     /* Not safe to call ANY callbacks here, so ... */
     fprintf (stderr, _("guestfs_close: called twice on the same handle\n"));
     return;
   }
 
+  if (g->trace) {
+    const char trace_msg[] = "close";
+
+    guestfs___call_callbacks_message (g, GUESTFS_EVENT_TRACE,
+                                      trace_msg, strlen (trace_msg));
+  }
+
   debug (g, "closing guestfs handle %p (state %d)", g, g->state);
 
   /* Try to sync if autosync flag is set. */
@@ -215,17 +222,12 @@ guestfs_close (guestfs_h *g)
   if (g->pid > 0) waitpid (g->pid, NULL, 0);
   if (g->recoverypid > 0) waitpid (g->recoverypid, NULL, 0);
 
-  /* Remove tmpfiles. */
-  if (g->tmpdir) {
-    snprintf (filename, sizeof filename, "%s/guestfsd.sock", g->tmpdir);
-    unlink (filename);
-
-    rmdir (g->tmpdir);
-
-    free (g->tmpdir);
-  }
+  /* Remove whole temporary directory. */
+  remove_tmpdir (g);
 
   if (g->cmdline) {
+    size_t i;
+
     for (i = 0; i < g->cmdline_size; ++i)
       free (g->cmdline[i]);
     free (g->cmdline);
@@ -238,6 +240,8 @@ guestfs_close (guestfs_h *g)
   if (handles == g)
     handles = g->next;
   else {
+    guestfs_h *gg;
+
     for (gg = handles; gg->next != g; gg = gg->next)
       ;
     gg->next = g->next;
@@ -255,6 +259,43 @@ guestfs_close (guestfs_h *g)
   free (g);
 }
 
+/* g->tmpdir can contain any files (but not subdirectories).  Remove
+ * those and the directory itself.  Note that errors in this function
+ * aren't really that important: if we end up not deleting temporary
+ * files it's only annoying.
+ */
+static void
+remove_tmpdir (guestfs_h *g)
+{
+  DIR *dir;
+  struct dirent *d;
+
+  if (!g->tmpdir)
+    return;
+
+  dir = opendir (g->tmpdir);
+  if (dir == NULL) {
+    perror (g->tmpdir);
+    return;
+  }
+
+  while ((d = readdir (dir)) != NULL) {
+    if (STRNEQ (d->d_name, ".") && STRNEQ (d->d_name, "..")) {
+      if (unlinkat (dirfd (dir), d->d_name, 0) == -1)
+        perror (d->d_name);
+    }
+  }
+
+  if (closedir (dir) == -1)
+    perror (g->tmpdir);
+
+  if (rmdir (g->tmpdir) == -1)
+    perror (g->tmpdir);
+
+  free (g->tmpdir);
+  g->tmpdir = NULL;
+}
+
 /* Close all open handles (called from atexit(3)). */
 static void
 close_handles (void)
@@ -395,14 +436,8 @@ guestfs_perrorf (guestfs_h *g, const char *fs, ...)
 
   if (err < 0) return;
 
-#if !defined(_GNU_SOURCE) || defined(__APPLE__)
   char buf[256];
   strerror_r (errnum, buf, sizeof buf);
-#else
-  char _buf[256];
-  char *buf;
-  buf = strerror_r (errnum, _buf, sizeof _buf);
-#endif
 
   msg = safe_realloc (g, msg, strlen (msg) + 2 + strlen (buf) + 1);
   strcat (msg, ": ");
@@ -540,6 +575,12 @@ guestfs_get_error_handler (guestfs_h *g, void **data_rtn)
   return g->error_cb;
 }
 
+void
+guestfs_user_cancel (guestfs_h *g)
+{
+  g->user_cancel = 1;
+}
+
 int
 guestfs__set_verbose (guestfs_h *g, int v)
 {
@@ -763,6 +804,37 @@ guestfs__get_attach_method (guestfs_h *g)
   return ret;
 }
 
+int
+guestfs__set_pgroup (guestfs_h *g, int v)
+{
+  g->pgroup = !!v;
+  return 0;
+}
+
+int
+guestfs__get_pgroup (guestfs_h *g)
+{
+  return g->pgroup;
+}
+
+int
+guestfs__set_smp (guestfs_h *g, int v)
+{
+  if (v >= 1) {
+    g->smp = v;
+    return 0;
+  } else {
+    error (g, "invalid smp parameter: %d", v);
+    return -1;
+  }
+}
+
+int
+guestfs__get_smp (guestfs_h *g)
+{
+  return g->smp;
+}
+
 /* Note the private data area is allocated lazily, since the vast
  * majority of callers will never use it.  This means g->pda is
  * likely to be NULL.