[libvirt] [PATCH 2/2] virnetsockettest: Use a temporary directory in /tmp

Guido Günther agx at sigxcpu.org
Wed Nov 2 21:31:13 UTC 2011


to avoid exceeding UNIX_PATH_MAX
---
 tests/virnetsockettest.c |   60 ++++++++++++++++++++++++++++++---------------
 1 files changed, 40 insertions(+), 20 deletions(-)

diff --git a/tests/virnetsockettest.c b/tests/virnetsockettest.c
index 6320ce0..aeb4f3f 100644
--- a/tests/virnetsockettest.c
+++ b/tests/virnetsockettest.c
@@ -202,16 +202,23 @@ static int testSocketUNIXAccept(const void *data ATTRIBUTE_UNUSED)
     int ret = -1;
 
     char *path;
-    if (progname[0] == '/') {
-        if (virAsprintf(&path, "%s-test.sock", progname) < 0) {
-            virReportOOMError();
-            goto cleanup;
-        }
-    } else {
-        if (virAsprintf(&path, "%s/%s-test.sock", abs_builddir, progname) < 0) {
-            virReportOOMError();
-            goto cleanup;
-        }
+    char *template;
+    char *tmpdir = NULL;
+
+    template = strdup("/tmp/libvirt_XXXXXX");
+    if (template == NULL) {
+        virReportOOMError();
+        goto cleanup;
+    }
+    tmpdir = mkdtemp(template);
+    if (tmpdir == NULL) {
+        virReportSystemError(errno, "%s",
+                             _("Failed to create temporary directory"));
+        goto cleanup;
+    }
+    if (virAsprintf(&path, "%s/test.sock", tmpdir) < 0) {
+        virReportOOMError();
+        goto cleanup;
     }
 
     if (virNetSocketNewListenUNIX(path, 0700, -1, getgid(), &lsock) < 0)
@@ -239,6 +246,9 @@ cleanup:
     VIR_FREE(path);
     virNetSocketFree(lsock);
     virNetSocketFree(ssock);
+    if (tmpdir)
+        rmdir(tmpdir);
+    VIR_FREE(template);
     return ret;
 }
 
@@ -251,16 +261,23 @@ static int testSocketUNIXAddrs(const void *data ATTRIBUTE_UNUSED)
     int ret = -1;
 
     char *path;
-    if (progname[0] == '/') {
-        if (virAsprintf(&path, "%s-test.sock", progname) < 0) {
-            virReportOOMError();
-            goto cleanup;
-        }
-    } else {
-        if (virAsprintf(&path, "%s/%s-test.sock", abs_builddir, progname) < 0) {
-            virReportOOMError();
-            goto cleanup;
-        }
+    char *template;
+    char *tmpdir = NULL;
+
+    template = strdup("/tmp/libvirt_XXXXXX");
+    if (template == NULL) {
+        virReportOOMError();
+        goto cleanup;
+    }
+    tmpdir = mkdtemp(template);
+    if (tmpdir == NULL) {
+        virReportSystemError(errno, "%s",
+                             _("Failed to create temporary directory"));
+        goto cleanup;
+    }
+    if (virAsprintf(&path, "%s/test.sock", tmpdir) < 0) {
+        virReportOOMError();
+        goto cleanup;
     }
 
     if (virNetSocketNewListenUNIX(path, 0700, -1, getgid(), &lsock) < 0)
@@ -317,6 +334,9 @@ cleanup:
     virNetSocketFree(lsock);
     virNetSocketFree(ssock);
     virNetSocketFree(csock);
+    if (tmpdir)
+        rmdir(tmpdir);
+    VIR_FREE(template);
     return ret;
 }
 
-- 
1.7.7




More information about the libvir-list mailing list