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