~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/lib/api/fs/debugfs.c

Version: ~ [ linux-5.16 ] ~ [ linux-5.15.13 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.90 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.170 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.224 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.261 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.296 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.298 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 #include <errno.h>
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include <string.h>
  5 #include <stdbool.h>
  6 #include <sys/vfs.h>
  7 #include <sys/mount.h>
  8 #include <linux/kernel.h>
  9 
 10 #include "debugfs.h"
 11 
 12 char debugfs_mountpoint[PATH_MAX + 1] = "/sys/kernel/debug";
 13 
 14 static const char * const debugfs_known_mountpoints[] = {
 15         "/sys/kernel/debug",
 16         "/debug",
 17         0,
 18 };
 19 
 20 static bool debugfs_found;
 21 
 22 /* find the path to the mounted debugfs */
 23 const char *debugfs_find_mountpoint(void)
 24 {
 25         const char * const *ptr;
 26         char type[100];
 27         FILE *fp;
 28 
 29         if (debugfs_found)
 30                 return (const char *)debugfs_mountpoint;
 31 
 32         ptr = debugfs_known_mountpoints;
 33         while (*ptr) {
 34                 if (debugfs_valid_mountpoint(*ptr) == 0) {
 35                         debugfs_found = true;
 36                         strcpy(debugfs_mountpoint, *ptr);
 37                         return debugfs_mountpoint;
 38                 }
 39                 ptr++;
 40         }
 41 
 42         /* give up and parse /proc/mounts */
 43         fp = fopen("/proc/mounts", "r");
 44         if (fp == NULL)
 45                 return NULL;
 46 
 47         while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
 48                       debugfs_mountpoint, type) == 2) {
 49                 if (strcmp(type, "debugfs") == 0)
 50                         break;
 51         }
 52         fclose(fp);
 53 
 54         if (strcmp(type, "debugfs") != 0)
 55                 return NULL;
 56 
 57         debugfs_found = true;
 58 
 59         return debugfs_mountpoint;
 60 }
 61 
 62 /* verify that a mountpoint is actually a debugfs instance */
 63 
 64 int debugfs_valid_mountpoint(const char *debugfs)
 65 {
 66         struct statfs st_fs;
 67 
 68         if (statfs(debugfs, &st_fs) < 0)
 69                 return -ENOENT;
 70         else if ((long)st_fs.f_type != (long)DEBUGFS_MAGIC)
 71                 return -ENOENT;
 72 
 73         return 0;
 74 }
 75 
 76 /* mount the debugfs somewhere if it's not mounted */
 77 char *debugfs_mount(const char *mountpoint)
 78 {
 79         /* see if it's already mounted */
 80         if (debugfs_find_mountpoint())
 81                 goto out;
 82 
 83         /* if not mounted and no argument */
 84         if (mountpoint == NULL) {
 85                 /* see if environment variable set */
 86                 mountpoint = getenv(PERF_DEBUGFS_ENVIRONMENT);
 87                 /* if no environment variable, use default */
 88                 if (mountpoint == NULL)
 89                         mountpoint = "/sys/kernel/debug";
 90         }
 91 
 92         if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0)
 93                 return NULL;
 94 
 95         /* save the mountpoint */
 96         debugfs_found = true;
 97         strncpy(debugfs_mountpoint, mountpoint, sizeof(debugfs_mountpoint));
 98 out:
 99         return debugfs_mountpoint;
100 }
101 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp