GNU Linux-libre 4.9.315-gnu1
[releases.git] / tools / lib / api / fs / fs.c
1 #include <ctype.h>
2 #include <errno.h>
3 #include <limits.h>
4 #include <stdbool.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/vfs.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <sys/mount.h>
14
15 #include "fs.h"
16 #include "debug-internal.h"
17
18 #define _STR(x) #x
19 #define STR(x) _STR(x)
20
21 #ifndef SYSFS_MAGIC
22 #define SYSFS_MAGIC            0x62656572
23 #endif
24
25 #ifndef PROC_SUPER_MAGIC
26 #define PROC_SUPER_MAGIC       0x9fa0
27 #endif
28
29 #ifndef DEBUGFS_MAGIC
30 #define DEBUGFS_MAGIC          0x64626720
31 #endif
32
33 #ifndef TRACEFS_MAGIC
34 #define TRACEFS_MAGIC          0x74726163
35 #endif
36
37 #ifndef HUGETLBFS_MAGIC
38 #define HUGETLBFS_MAGIC        0x958458f6
39 #endif
40
41 static const char * const sysfs__fs_known_mountpoints[] = {
42         "/sys",
43         0,
44 };
45
46 static const char * const procfs__known_mountpoints[] = {
47         "/proc",
48         0,
49 };
50
51 #ifndef DEBUGFS_DEFAULT_PATH
52 #define DEBUGFS_DEFAULT_PATH "/sys/kernel/debug"
53 #endif
54
55 static const char * const debugfs__known_mountpoints[] = {
56         DEBUGFS_DEFAULT_PATH,
57         "/debug",
58         0,
59 };
60
61
62 #ifndef TRACEFS_DEFAULT_PATH
63 #define TRACEFS_DEFAULT_PATH "/sys/kernel/tracing"
64 #endif
65
66 static const char * const tracefs__known_mountpoints[] = {
67         TRACEFS_DEFAULT_PATH,
68         "/sys/kernel/debug/tracing",
69         "/tracing",
70         "/trace",
71         0,
72 };
73
74 static const char * const hugetlbfs__known_mountpoints[] = {
75         0,
76 };
77
78 struct fs {
79         const char              *name;
80         const char * const      *mounts;
81         char                     path[PATH_MAX];
82         bool                     found;
83         long                     magic;
84 };
85
86 enum {
87         FS__SYSFS   = 0,
88         FS__PROCFS  = 1,
89         FS__DEBUGFS = 2,
90         FS__TRACEFS = 3,
91         FS__HUGETLBFS = 4,
92 };
93
94 #ifndef TRACEFS_MAGIC
95 #define TRACEFS_MAGIC 0x74726163
96 #endif
97
98 static struct fs fs__entries[] = {
99         [FS__SYSFS] = {
100                 .name   = "sysfs",
101                 .mounts = sysfs__fs_known_mountpoints,
102                 .magic  = SYSFS_MAGIC,
103         },
104         [FS__PROCFS] = {
105                 .name   = "proc",
106                 .mounts = procfs__known_mountpoints,
107                 .magic  = PROC_SUPER_MAGIC,
108         },
109         [FS__DEBUGFS] = {
110                 .name   = "debugfs",
111                 .mounts = debugfs__known_mountpoints,
112                 .magic  = DEBUGFS_MAGIC,
113         },
114         [FS__TRACEFS] = {
115                 .name   = "tracefs",
116                 .mounts = tracefs__known_mountpoints,
117                 .magic  = TRACEFS_MAGIC,
118         },
119         [FS__HUGETLBFS] = {
120                 .name   = "hugetlbfs",
121                 .mounts = hugetlbfs__known_mountpoints,
122                 .magic  = HUGETLBFS_MAGIC,
123         },
124 };
125
126 static bool fs__read_mounts(struct fs *fs)
127 {
128         bool found = false;
129         char type[100];
130         FILE *fp;
131
132         fp = fopen("/proc/mounts", "r");
133         if (fp == NULL)
134                 return NULL;
135
136         while (!found &&
137                fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
138                       fs->path, type) == 2) {
139
140                 if (strcmp(type, fs->name) == 0)
141                         found = true;
142         }
143
144         fclose(fp);
145         return fs->found = found;
146 }
147
148 static int fs__valid_mount(const char *fs, long magic)
149 {
150         struct statfs st_fs;
151
152         if (statfs(fs, &st_fs) < 0)
153                 return -ENOENT;
154         else if ((long)st_fs.f_type != magic)
155                 return -ENOENT;
156
157         return 0;
158 }
159
160 static bool fs__check_mounts(struct fs *fs)
161 {
162         const char * const *ptr;
163
164         ptr = fs->mounts;
165         while (*ptr) {
166                 if (fs__valid_mount(*ptr, fs->magic) == 0) {
167                         fs->found = true;
168                         strcpy(fs->path, *ptr);
169                         return true;
170                 }
171                 ptr++;
172         }
173
174         return false;
175 }
176
177 static void mem_toupper(char *f, size_t len)
178 {
179         while (len) {
180                 *f = toupper(*f);
181                 f++;
182                 len--;
183         }
184 }
185
186 /*
187  * Check for "NAME_PATH" environment variable to override fs location (for
188  * testing). This matches the recommendation in Documentation/sysfs-rules.txt
189  * for SYSFS_PATH.
190  */
191 static bool fs__env_override(struct fs *fs)
192 {
193         char *override_path;
194         size_t name_len = strlen(fs->name);
195         /* name + "_PATH" + '\0' */
196         char upper_name[name_len + 5 + 1];
197
198         memcpy(upper_name, fs->name, name_len);
199         mem_toupper(upper_name, name_len);
200         strcpy(&upper_name[name_len], "_PATH");
201
202         override_path = getenv(upper_name);
203         if (!override_path)
204                 return false;
205
206         fs->found = true;
207         strncpy(fs->path, override_path, sizeof(fs->path) - 1);
208         fs->path[sizeof(fs->path) - 1] = '\0';
209         return true;
210 }
211
212 static const char *fs__get_mountpoint(struct fs *fs)
213 {
214         if (fs__env_override(fs))
215                 return fs->path;
216
217         if (fs__check_mounts(fs))
218                 return fs->path;
219
220         if (fs__read_mounts(fs))
221                 return fs->path;
222
223         return NULL;
224 }
225
226 static const char *fs__mountpoint(int idx)
227 {
228         struct fs *fs = &fs__entries[idx];
229
230         if (fs->found)
231                 return (const char *)fs->path;
232
233         return fs__get_mountpoint(fs);
234 }
235
236 static const char *mount_overload(struct fs *fs)
237 {
238         size_t name_len = strlen(fs->name);
239         /* "PERF_" + name + "_ENVIRONMENT" + '\0' */
240         char upper_name[5 + name_len + 12 + 1];
241
242         snprintf(upper_name, name_len, "PERF_%s_ENVIRONMENT", fs->name);
243         mem_toupper(upper_name, name_len);
244
245         return getenv(upper_name) ?: *fs->mounts;
246 }
247
248 static const char *fs__mount(int idx)
249 {
250         struct fs *fs = &fs__entries[idx];
251         const char *mountpoint;
252
253         if (fs__mountpoint(idx))
254                 return (const char *)fs->path;
255
256         mountpoint = mount_overload(fs);
257
258         if (mount(NULL, mountpoint, fs->name, 0, NULL) < 0)
259                 return NULL;
260
261         return fs__check_mounts(fs) ? fs->path : NULL;
262 }
263
264 #define FS(name, idx)                           \
265 const char *name##__mountpoint(void)            \
266 {                                               \
267         return fs__mountpoint(idx);             \
268 }                                               \
269                                                 \
270 const char *name##__mount(void)                 \
271 {                                               \
272         return fs__mount(idx);                  \
273 }                                               \
274                                                 \
275 bool name##__configured(void)                   \
276 {                                               \
277         return name##__mountpoint() != NULL;    \
278 }
279
280 FS(sysfs,   FS__SYSFS);
281 FS(procfs,  FS__PROCFS);
282 FS(debugfs, FS__DEBUGFS);
283 FS(tracefs, FS__TRACEFS);
284 FS(hugetlbfs, FS__HUGETLBFS);
285
286 int filename__read_int(const char *filename, int *value)
287 {
288         char line[64];
289         int fd = open(filename, O_RDONLY), err = -1;
290
291         if (fd < 0)
292                 return -1;
293
294         if (read(fd, line, sizeof(line)) > 0) {
295                 *value = atoi(line);
296                 err = 0;
297         }
298
299         close(fd);
300         return err;
301 }
302
303 /*
304  * Parses @value out of @filename with strtoull.
305  * By using 0 for base, the strtoull detects the
306  * base automatically (see man strtoull).
307  */
308 int filename__read_ull(const char *filename, unsigned long long *value)
309 {
310         char line[64];
311         int fd = open(filename, O_RDONLY), err = -1;
312
313         if (fd < 0)
314                 return -1;
315
316         if (read(fd, line, sizeof(line)) > 0) {
317                 *value = strtoull(line, NULL, 0);
318                 if (*value != ULLONG_MAX)
319                         err = 0;
320         }
321
322         close(fd);
323         return err;
324 }
325
326 #define STRERR_BUFSIZE  128     /* For the buffer size of strerror_r */
327
328 int filename__read_str(const char *filename, char **buf, size_t *sizep)
329 {
330         size_t size = 0, alloc_size = 0;
331         void *bf = NULL, *nbf;
332         int fd, n, err = 0;
333         char sbuf[STRERR_BUFSIZE];
334
335         fd = open(filename, O_RDONLY);
336         if (fd < 0)
337                 return -errno;
338
339         do {
340                 if (size == alloc_size) {
341                         alloc_size += BUFSIZ;
342                         nbf = realloc(bf, alloc_size);
343                         if (!nbf) {
344                                 err = -ENOMEM;
345                                 break;
346                         }
347
348                         bf = nbf;
349                 }
350
351                 n = read(fd, bf + size, alloc_size - size);
352                 if (n < 0) {
353                         if (size) {
354                                 pr_warning("read failed %d: %s\n", errno,
355                                          strerror_r(errno, sbuf, sizeof(sbuf)));
356                                 err = 0;
357                         } else
358                                 err = -errno;
359
360                         break;
361                 }
362
363                 size += n;
364         } while (n > 0);
365
366         if (!err) {
367                 *sizep = size;
368                 *buf   = bf;
369         } else
370                 free(bf);
371
372         close(fd);
373         return err;
374 }
375
376 int procfs__read_str(const char *entry, char **buf, size_t *sizep)
377 {
378         char path[PATH_MAX];
379         const char *procfs = procfs__mountpoint();
380
381         if (!procfs)
382                 return -1;
383
384         snprintf(path, sizeof(path), "%s/%s", procfs, entry);
385
386         return filename__read_str(path, buf, sizep);
387 }
388
389 int sysfs__read_ull(const char *entry, unsigned long long *value)
390 {
391         char path[PATH_MAX];
392         const char *sysfs = sysfs__mountpoint();
393
394         if (!sysfs)
395                 return -1;
396
397         snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
398
399         return filename__read_ull(path, value);
400 }
401
402 int sysfs__read_int(const char *entry, int *value)
403 {
404         char path[PATH_MAX];
405         const char *sysfs = sysfs__mountpoint();
406
407         if (!sysfs)
408                 return -1;
409
410         snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
411
412         return filename__read_int(path, value);
413 }
414
415 int sysfs__read_str(const char *entry, char **buf, size_t *sizep)
416 {
417         char path[PATH_MAX];
418         const char *sysfs = sysfs__mountpoint();
419
420         if (!sysfs)
421                 return -1;
422
423         snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
424
425         return filename__read_str(path, buf, sizep);
426 }
427
428 int sysctl__read_int(const char *sysctl, int *value)
429 {
430         char path[PATH_MAX];
431         const char *procfs = procfs__mountpoint();
432
433         if (!procfs)
434                 return -1;
435
436         snprintf(path, sizeof(path), "%s/sys/%s", procfs, sysctl);
437
438         return filename__read_int(path, value);
439 }