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

[libvirt] [PATCH] LXC: fix order in virProcessGetNamespaces



virProcessGetNamespaces() opens files in /proc/XXX/ns/ which will
later be passed to setns().
We have to make sure that the file descriptors in the array are in the correct
order. Otherwise setns() may fail.
The order has been taken from util-linux's sys-utils/nsenter.c

Signed-off-by: Richard Weinberger <richard nod at>
---
 src/util/virprocess.c | 33 ++++++++++-----------------------
 1 file changed, 10 insertions(+), 23 deletions(-)

diff --git a/src/util/virprocess.c b/src/util/virprocess.c
index bc028d7..fce0d46 100644
--- a/src/util/virprocess.c
+++ b/src/util/virprocess.c
@@ -513,11 +513,11 @@ int virProcessGetNamespaces(pid_t pid,
                             int **fdlist)
 {
     int ret = -1;
-    DIR *dh = NULL;
     struct dirent *de;
     char *nsdir = NULL;
     char *nsfile = NULL;
-    size_t i;
+    char *ns_files[] = { "user", "ipc", "uts", "net", "pid", "mnt", NULL };
+    size_t i = 0;
 
     *nfdlist = 0;
     *fdlist = NULL;
@@ -528,45 +528,32 @@ int virProcessGetNamespaces(pid_t pid,
         goto cleanup;
     }
 
-    if (!(dh = opendir(nsdir))) {
-        virReportSystemError(errno,
-                             _("Cannot read directory %s"),
-                             nsdir);
-        goto cleanup;
-    }
-
-    while ((de = readdir(dh))) {
+    while (ns_files[i]) {
         int fd;
-        if (de->d_name[0] == '.')
-            continue;
-
-        if (VIR_EXPAND_N(*fdlist, *nfdlist, 1) < 0) {
+        if (virAsprintf(&nsfile, "%s/%s", nsdir, ns_files[i]) < 0) {
             virReportOOMError();
             goto cleanup;
         }
 
-        if (virAsprintf(&nsfile, "%s/%s", nsdir, de->d_name) < 0) {
-            virReportOOMError();
-            goto cleanup;
+        if ((fd = open(nsfile, O_RDWR)) < 0) {
+            goto next;
         }
 
-        if ((fd = open(nsfile, O_RDWR)) < 0) {
-            virReportSystemError(errno,
-                                 _("Unable to open %s"),
-                                 nsfile);
+        if (VIR_EXPAND_N(*fdlist, *nfdlist, 1) < 0) {
+            virReportOOMError();
             goto cleanup;
         }
 
         (*fdlist)[(*nfdlist)-1] = fd;
 
+next:
         VIR_FREE(nsfile);
+        i++;
     }
 
     ret = 0;
 
 cleanup:
-    if (dh)
-        closedir(dh);
     VIR_FREE(nsdir);
     VIR_FREE(nsfile);
     if (ret < 0) {
-- 
1.8.3


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