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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Nov 02, 2011 at 03:39:54PM -0600, Eric Blake wrote:
> On 11/02/2011 03:31 PM, Guido Günther wrote:
> >to avoid exceeding UNIX_PATH_MAX
> >---
> >  tests/virnetsockettest.c |   60 ++++++++++++++++++++++++++++++---------------
> >  1 files changed, 40 insertions(+), 20 deletions(-)
> 
> I like this better than Stefan's proposal (a temporary directory is
> better than unlink() on an open fd).
> 
> ACK with one nit:
> 
> >+    char *template;
> >+    char *tmpdir = NULL;
> >+
> >+    template = strdup("/tmp/libvirt_XXXXXX");
> 
> No need to malloc() the template.  Just do:
> 
> char template[] = "/tmp/libvirt_XXXXXX";

This was actually the first version I had but it didn't seem to match
libvirt's style (in the non-const form and I didn't find it in HACKING)
so I went for the explicit strdup. New version attached.
Cheers,
 -- Guido

>From 6ddd359787b7bb1ce6c2d8fe08ebd825896dcf52 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Guido=20G=C3=BCnther?= <agx@xxxxxxxxxxx>
Date: Wed, 2 Nov 2011 22:06:44 +0100
Subject: [PATCH] virnetsockettest: Use a temporary directory in /tmp

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

diff --git a/tests/virnetsockettest.c b/tests/virnetsockettest.c
index 6320ce0..a76e5cc 100644
--- a/tests/virnetsockettest.c
+++ b/tests/virnetsockettest.c
@@ -202,16 +202,18 @@ 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 *tmpdir;
+    char template[] = "/tmp/libvirt_XXXXXX";
+
+    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 +241,8 @@ cleanup:
     VIR_FREE(path);
     virNetSocketFree(lsock);
     virNetSocketFree(ssock);
+    if (tmpdir)
+        rmdir(tmpdir);
     return ret;
 }
 
@@ -251,16 +255,18 @@ 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 *tmpdir;
+    char template[] = "/tmp/libvirt_XXXXXX";
+
+    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 +323,8 @@ cleanup:
     virNetSocketFree(lsock);
     virNetSocketFree(ssock);
     virNetSocketFree(csock);
+    if (tmpdir)
+        rmdir(tmpdir);
     return ret;
 }
 
-- 
1.7.7

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]