block: decouple REQ_OP_SECURE_ERASE from REQ_OP_DISCARD
[linux-2.6-microblaze.git] / tools / perf / util / util.c
index fb4f661..f8571a6 100644 (file)
@@ -431,3 +431,34 @@ void perf_debuginfod_setup(struct perf_debuginfod *di)
 
        pr_debug("DEBUGINFOD_URLS=%s\n", getenv("DEBUGINFOD_URLS"));
 }
+
+/*
+ * Return a new filename prepended with task's root directory if it's in
+ * a chroot.  Callers should free the returned string.
+ */
+char *filename_with_chroot(int pid, const char *filename)
+{
+       char buf[PATH_MAX];
+       char proc_root[32];
+       char *new_name = NULL;
+       int ret;
+
+       scnprintf(proc_root, sizeof(proc_root), "/proc/%d/root", pid);
+       ret = readlink(proc_root, buf, sizeof(buf) - 1);
+       if (ret <= 0)
+               return NULL;
+
+       /* readlink(2) does not append a null byte to buf */
+       buf[ret] = '\0';
+
+       if (!strcmp(buf, "/"))
+               return NULL;
+
+       if (strstr(buf, "(deleted)"))
+               return NULL;
+
+       if (asprintf(&new_name, "%s/%s", buf, filename) < 0)
+               return NULL;
+
+       return new_name;
+}