+ if (test_mounted)
+ goto skip_mounting;
+ test_mounted = true;
+
+ if (mount("none", "/sys/kernel/debug", "debugfs", 0, "") == -1) {
+ /* Already mounted or don't have permission to mount is okay */
+ if (errno != EPERM && errno != EBUSY)
+ fprintf(stderr, "Warning: failed to mount debug: %s\n",
+ strerror(errno));
+ } else {
+ struct stat mtab;
+
+ /* This is all for RHEL6 which is old school. Can be removed
+ * later when RHEL6 client support is dropped. */
+ rc = lstat(_PATH_MOUNTED, &mtab);
+ if (!rc && !S_ISLNK(mtab.st_mode)) {
+ FILE *fp = setmntent(_PATH_MOUNTED, "r+");
+
+ if (fp != NULL) {
+ const struct mntent fs = {
+ .mnt_fsname = "debugfs",
+ .mnt_dir = "/sys/kernel/debug",
+ .mnt_type = "debugfs",
+ .mnt_opts = "rw,relatime",
+ };
+
+ rc = addmntent(fp, &fs);
+ if (rc) {
+ fprintf(stderr,
+ "failed to add debugfs to %s: %s\n",
+ _PATH_MOUNTED, strerror(errno));
+ }
+ endmntent(fp);
+ } else {
+ fprintf(stderr, "could not open %s: %s\n",
+ _PATH_MOUNTED, strerror(errno));
+ }
+ }
+ }
+skip_mounting: