diff options
Diffstat (limited to 'tools/lib/api')
| -rw-r--r-- | tools/lib/api/Makefile | 44 | ||||
| -rw-r--r-- | tools/lib/api/fs/debugfs.c | 100 | ||||
| -rw-r--r-- | tools/lib/api/fs/debugfs.h | 29 | ||||
| -rw-r--r-- | tools/lib/api/fs/fs.c | 165 | ||||
| -rw-r--r-- | tools/lib/api/fs/fs.h | 14 | 
5 files changed, 352 insertions, 0 deletions
diff --git a/tools/lib/api/Makefile b/tools/lib/api/Makefile new file mode 100644 index 00000000000..ce00f7ee645 --- /dev/null +++ b/tools/lib/api/Makefile @@ -0,0 +1,44 @@ +include ../../scripts/Makefile.include +include ../../perf/config/utilities.mak		# QUIET_CLEAN + +CC = $(CROSS_COMPILE)gcc +AR = $(CROSS_COMPILE)ar + +# guard against environment variables +LIB_H= +LIB_OBJS= + +LIB_H += fs/debugfs.h +LIB_H += fs/fs.h + +LIB_OBJS += $(OUTPUT)fs/debugfs.o +LIB_OBJS += $(OUTPUT)fs/fs.o + +LIBFILE = libapikfs.a + +CFLAGS = -ggdb3 -Wall -Wextra -std=gnu99 -Werror -O6 -D_FORTIFY_SOURCE=2 $(EXTRA_WARNINGS) $(EXTRA_CFLAGS) -fPIC +EXTLIBS = -lelf -lpthread -lrt -lm +ALL_CFLAGS = $(CFLAGS) $(BASIC_CFLAGS) -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 +ALL_LDFLAGS = $(LDFLAGS) + +RM = rm -f + +$(LIBFILE): $(LIB_OBJS) +	$(QUIET_AR)$(RM) $@ && $(AR) rcs $(OUTPUT)$@ $(LIB_OBJS) + +$(LIB_OBJS): $(LIB_H) + +libapi_dirs: +	$(QUIET_MKDIR)mkdir -p $(OUTPUT)fs/ + +$(OUTPUT)%.o: %.c libapi_dirs +	$(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) $< +$(OUTPUT)%.s: %.c libapi_dirs +	$(QUIET_CC)$(CC) -S $(ALL_CFLAGS) $< +$(OUTPUT)%.o: %.S libapi_dirs +	$(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) $< + +clean: +	$(call QUIET_CLEAN, libapi) $(RM) $(LIB_OBJS) $(LIBFILE) + +.PHONY: clean diff --git a/tools/lib/api/fs/debugfs.c b/tools/lib/api/fs/debugfs.c new file mode 100644 index 00000000000..a74fba6d774 --- /dev/null +++ b/tools/lib/api/fs/debugfs.c @@ -0,0 +1,100 @@ +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdbool.h> +#include <sys/vfs.h> +#include <sys/mount.h> +#include <linux/kernel.h> + +#include "debugfs.h" + +char debugfs_mountpoint[PATH_MAX + 1] = "/sys/kernel/debug"; + +static const char * const debugfs_known_mountpoints[] = { +	"/sys/kernel/debug", +	"/debug", +	0, +}; + +static bool debugfs_found; + +/* find the path to the mounted debugfs */ +const char *debugfs_find_mountpoint(void) +{ +	const char * const *ptr; +	char type[100]; +	FILE *fp; + +	if (debugfs_found) +		return (const char *)debugfs_mountpoint; + +	ptr = debugfs_known_mountpoints; +	while (*ptr) { +		if (debugfs_valid_mountpoint(*ptr) == 0) { +			debugfs_found = true; +			strcpy(debugfs_mountpoint, *ptr); +			return debugfs_mountpoint; +		} +		ptr++; +	} + +	/* give up and parse /proc/mounts */ +	fp = fopen("/proc/mounts", "r"); +	if (fp == NULL) +		return NULL; + +	while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n", +		      debugfs_mountpoint, type) == 2) { +		if (strcmp(type, "debugfs") == 0) +			break; +	} +	fclose(fp); + +	if (strcmp(type, "debugfs") != 0) +		return NULL; + +	debugfs_found = true; + +	return debugfs_mountpoint; +} + +/* verify that a mountpoint is actually a debugfs instance */ + +int debugfs_valid_mountpoint(const char *debugfs) +{ +	struct statfs st_fs; + +	if (statfs(debugfs, &st_fs) < 0) +		return -ENOENT; +	else if (st_fs.f_type != (long) DEBUGFS_MAGIC) +		return -ENOENT; + +	return 0; +} + +/* mount the debugfs somewhere if it's not mounted */ +char *debugfs_mount(const char *mountpoint) +{ +	/* see if it's already mounted */ +	if (debugfs_find_mountpoint()) +		goto out; + +	/* if not mounted and no argument */ +	if (mountpoint == NULL) { +		/* see if environment variable set */ +		mountpoint = getenv(PERF_DEBUGFS_ENVIRONMENT); +		/* if no environment variable, use default */ +		if (mountpoint == NULL) +			mountpoint = "/sys/kernel/debug"; +	} + +	if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0) +		return NULL; + +	/* save the mountpoint */ +	debugfs_found = true; +	strncpy(debugfs_mountpoint, mountpoint, sizeof(debugfs_mountpoint)); +out: +	return debugfs_mountpoint; +} diff --git a/tools/lib/api/fs/debugfs.h b/tools/lib/api/fs/debugfs.h new file mode 100644 index 00000000000..f19d3df9609 --- /dev/null +++ b/tools/lib/api/fs/debugfs.h @@ -0,0 +1,29 @@ +#ifndef __API_DEBUGFS_H__ +#define __API_DEBUGFS_H__ + +#define _STR(x) #x +#define STR(x) _STR(x) + +/* + * On most systems <limits.h> would have given us this, but  not on some systems + * (e.g. GNU/Hurd). + */ +#ifndef PATH_MAX +#define PATH_MAX 4096 +#endif + +#ifndef DEBUGFS_MAGIC +#define DEBUGFS_MAGIC          0x64626720 +#endif + +#ifndef PERF_DEBUGFS_ENVIRONMENT +#define PERF_DEBUGFS_ENVIRONMENT "PERF_DEBUGFS_DIR" +#endif + +const char *debugfs_find_mountpoint(void); +int debugfs_valid_mountpoint(const char *debugfs); +char *debugfs_mount(const char *mountpoint); + +extern char debugfs_mountpoint[]; + +#endif /* __API_DEBUGFS_H__ */ diff --git a/tools/lib/api/fs/fs.c b/tools/lib/api/fs/fs.c new file mode 100644 index 00000000000..c1b49c36a95 --- /dev/null +++ b/tools/lib/api/fs/fs.c @@ -0,0 +1,165 @@ +/* TODO merge/factor in debugfs.c here */ + +#include <ctype.h> +#include <errno.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/vfs.h> + +#include "debugfs.h" +#include "fs.h" + +static const char * const sysfs__fs_known_mountpoints[] = { +	"/sys", +	0, +}; + +static const char * const procfs__known_mountpoints[] = { +	"/proc", +	0, +}; + +struct fs { +	const char		*name; +	const char * const	*mounts; +	char			 path[PATH_MAX + 1]; +	bool			 found; +	long			 magic; +}; + +enum { +	FS__SYSFS  = 0, +	FS__PROCFS = 1, +}; + +static struct fs fs__entries[] = { +	[FS__SYSFS] = { +		.name	= "sysfs", +		.mounts	= sysfs__fs_known_mountpoints, +		.magic	= SYSFS_MAGIC, +	}, +	[FS__PROCFS] = { +		.name	= "proc", +		.mounts	= procfs__known_mountpoints, +		.magic	= PROC_SUPER_MAGIC, +	}, +}; + +static bool fs__read_mounts(struct fs *fs) +{ +	bool found = false; +	char type[100]; +	FILE *fp; + +	fp = fopen("/proc/mounts", "r"); +	if (fp == NULL) +		return NULL; + +	while (!found && +	       fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n", +		      fs->path, type) == 2) { + +		if (strcmp(type, fs->name) == 0) +			found = true; +	} + +	fclose(fp); +	return fs->found = found; +} + +static int fs__valid_mount(const char *fs, long magic) +{ +	struct statfs st_fs; + +	if (statfs(fs, &st_fs) < 0) +		return -ENOENT; +	else if (st_fs.f_type != magic) +		return -ENOENT; + +	return 0; +} + +static bool fs__check_mounts(struct fs *fs) +{ +	const char * const *ptr; + +	ptr = fs->mounts; +	while (*ptr) { +		if (fs__valid_mount(*ptr, fs->magic) == 0) { +			fs->found = true; +			strcpy(fs->path, *ptr); +			return true; +		} +		ptr++; +	} + +	return false; +} + +static void mem_toupper(char *f, size_t len) +{ +	while (len) { +		*f = toupper(*f); +		f++; +		len--; +	} +} + +/* + * Check for "NAME_PATH" environment variable to override fs location (for + * testing). This matches the recommendation in Documentation/sysfs-rules.txt + * for SYSFS_PATH. + */ +static bool fs__env_override(struct fs *fs) +{ +	char *override_path; +	size_t name_len = strlen(fs->name); +	/* name + "_PATH" + '\0' */ +	char upper_name[name_len + 5 + 1]; +	memcpy(upper_name, fs->name, name_len); +	mem_toupper(upper_name, name_len); +	strcpy(&upper_name[name_len], "_PATH"); + +	override_path = getenv(upper_name); +	if (!override_path) +		return false; + +	fs->found = true; +	strncpy(fs->path, override_path, sizeof(fs->path)); +	return true; +} + +static const char *fs__get_mountpoint(struct fs *fs) +{ +	if (fs__env_override(fs)) +		return fs->path; + +	if (fs__check_mounts(fs)) +		return fs->path; + +	if (fs__read_mounts(fs)) +		return fs->path; + +	return NULL; +} + +static const char *fs__mountpoint(int idx) +{ +	struct fs *fs = &fs__entries[idx]; + +	if (fs->found) +		return (const char *)fs->path; + +	return fs__get_mountpoint(fs); +} + +#define FS__MOUNTPOINT(name, idx)	\ +const char *name##__mountpoint(void)	\ +{					\ +	return fs__mountpoint(idx);	\ +} + +FS__MOUNTPOINT(sysfs,  FS__SYSFS); +FS__MOUNTPOINT(procfs, FS__PROCFS); diff --git a/tools/lib/api/fs/fs.h b/tools/lib/api/fs/fs.h new file mode 100644 index 00000000000..cb7049551f3 --- /dev/null +++ b/tools/lib/api/fs/fs.h @@ -0,0 +1,14 @@ +#ifndef __API_FS__ +#define __API_FS__ + +#ifndef SYSFS_MAGIC +#define SYSFS_MAGIC            0x62656572 +#endif + +#ifndef PROC_SUPER_MAGIC +#define PROC_SUPER_MAGIC       0x9fa0 +#endif + +const char *sysfs__mountpoint(void); +const char *procfs__mountpoint(void); +#endif /* __API_FS__ */  | 
