free(namespaces);
 }
 
+static int nsinfo__get_nspid(struct nsinfo *nsi, const char *path)
+{
+       FILE *f = NULL;
+       char *statln = NULL;
+       size_t linesz = 0;
+       char *nspid;
+
+       f = fopen(path, "r");
+       if (f == NULL)
+               return -1;
+
+       while (getline(&statln, &linesz, f) != -1) {
+               /* Use tgid if CONFIG_PID_NS is not defined. */
+               if (strstr(statln, "Tgid:") != NULL) {
+                       nsi->tgid = (pid_t)strtol(strrchr(statln, '\t'),
+                                                    NULL, 10);
+                       nsi->nstgid = nsi->tgid;
+               }
+
+               if (strstr(statln, "NStgid:") != NULL) {
+                       nspid = strrchr(statln, '\t');
+                       nsi->nstgid = (pid_t)strtol(nspid, NULL, 10);
+                       /*
+                        * If innermost tgid is not the first, process is in a different
+                        * PID namespace.
+                        */
+                       nsi->in_pidns = (statln + sizeof("NStgid:") - 1) != nspid;
+                       break;
+               }
+       }
+
+       fclose(f);
+       free(statln);
+       return 0;
+}
+
 int nsinfo__init(struct nsinfo *nsi)
 {
        char oldns[PATH_MAX];
        char spath[PATH_MAX];
        char *newns = NULL;
-       char *statln = NULL;
-       char *nspid;
        struct stat old_stat;
        struct stat new_stat;
-       FILE *f = NULL;
-       size_t linesz = 0;
        int rv = -1;
 
        if (snprintf(oldns, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
        if (snprintf(spath, PATH_MAX, "/proc/%d/status", nsi->pid) >= PATH_MAX)
                goto out;
 
-       f = fopen(spath, "r");
-       if (f == NULL)
-               goto out;
-
-       while (getline(&statln, &linesz, f) != -1) {
-               /* Use tgid if CONFIG_PID_NS is not defined. */
-               if (strstr(statln, "Tgid:") != NULL) {
-                       nsi->tgid = (pid_t)strtol(strrchr(statln, '\t'),
-                                                    NULL, 10);
-                       nsi->nstgid = nsi->tgid;
-               }
-
-               if (strstr(statln, "NStgid:") != NULL) {
-                       nspid = strrchr(statln, '\t');
-                       nsi->nstgid = (pid_t)strtol(nspid, NULL, 10);
-                       /* If innermost tgid is not the first, process is in a different
-                        * PID namespace.
-                        */
-                       nsi->in_pidns = (statln + sizeof("NStgid:") - 1) != nspid;
-                       break;
-               }
-       }
-       rv = 0;
+       rv = nsinfo__get_nspid(nsi, spath);
 
 out:
-       if (f != NULL)
-               (void) fclose(f);
-       free(statln);
        free(newns);
        return rv;
 }
 
        return ret;
 }
+
+bool nsinfo__is_in_root_namespace(void)
+{
+       struct nsinfo nsi;
+
+       memset(&nsi, 0x0, sizeof(nsi));
+       nsinfo__get_nspid(&nsi, "/proc/self/status");
+       return !nsi.in_pidns;
+}