Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) #include <ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <limits.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <sys/vfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <sys/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include "fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include "debug-internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #define _STR(x) #x
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define STR(x) _STR(x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #ifndef SYSFS_MAGIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define SYSFS_MAGIC            0x62656572
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #ifndef PROC_SUPER_MAGIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define PROC_SUPER_MAGIC       0x9fa0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #ifndef DEBUGFS_MAGIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define DEBUGFS_MAGIC          0x64626720
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #ifndef TRACEFS_MAGIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define TRACEFS_MAGIC          0x74726163
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #ifndef HUGETLBFS_MAGIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define HUGETLBFS_MAGIC        0x958458f6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #ifndef BPF_FS_MAGIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define BPF_FS_MAGIC           0xcafe4a11
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) static const char * const sysfs__fs_known_mountpoints[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	"/sys",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) static const char * const procfs__known_mountpoints[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	"/proc",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) #ifndef DEBUGFS_DEFAULT_PATH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) #define DEBUGFS_DEFAULT_PATH "/sys/kernel/debug"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static const char * const debugfs__known_mountpoints[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	DEBUGFS_DEFAULT_PATH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	"/debug",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) #ifndef TRACEFS_DEFAULT_PATH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) #define TRACEFS_DEFAULT_PATH "/sys/kernel/tracing"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) static const char * const tracefs__known_mountpoints[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	TRACEFS_DEFAULT_PATH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	"/sys/kernel/debug/tracing",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	"/tracing",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	"/trace",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) static const char * const hugetlbfs__known_mountpoints[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) static const char * const bpf_fs__known_mountpoints[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	"/sys/fs/bpf",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) struct fs {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	const char		*name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	const char * const	*mounts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	char			 path[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	bool			 found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	bool			 checked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	long			 magic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	FS__SYSFS   = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	FS__PROCFS  = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	FS__DEBUGFS = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	FS__TRACEFS = 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	FS__HUGETLBFS = 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	FS__BPF_FS = 5,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) #ifndef TRACEFS_MAGIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) #define TRACEFS_MAGIC 0x74726163
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static struct fs fs__entries[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	[FS__SYSFS] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		.name	= "sysfs",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		.mounts	= sysfs__fs_known_mountpoints,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		.magic	= SYSFS_MAGIC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		.checked = false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	[FS__PROCFS] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		.name	= "proc",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		.mounts	= procfs__known_mountpoints,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		.magic	= PROC_SUPER_MAGIC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		.checked = false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	[FS__DEBUGFS] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		.name	= "debugfs",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		.mounts	= debugfs__known_mountpoints,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		.magic	= DEBUGFS_MAGIC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		.checked = false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	[FS__TRACEFS] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		.name	= "tracefs",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		.mounts	= tracefs__known_mountpoints,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		.magic	= TRACEFS_MAGIC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		.checked = false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	[FS__HUGETLBFS] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		.name	= "hugetlbfs",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		.mounts = hugetlbfs__known_mountpoints,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		.magic	= HUGETLBFS_MAGIC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		.checked = false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	[FS__BPF_FS] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		.name	= "bpf",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		.mounts = bpf_fs__known_mountpoints,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		.magic	= BPF_FS_MAGIC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		.checked = false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static bool fs__read_mounts(struct fs *fs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	bool found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	char type[100];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	FILE *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	fp = fopen("/proc/mounts", "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	if (fp == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	while (!found &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	       fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		      fs->path, type) == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		if (strcmp(type, fs->name) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 			found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	fclose(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	fs->checked = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	return fs->found = found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static int fs__valid_mount(const char *fs, long magic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	struct statfs st_fs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	if (statfs(fs, &st_fs) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	else if ((long)st_fs.f_type != magic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static bool fs__check_mounts(struct fs *fs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	const char * const *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	ptr = fs->mounts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	while (*ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		if (fs__valid_mount(*ptr, fs->magic) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			fs->found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 			strcpy(fs->path, *ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		ptr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static void mem_toupper(char *f, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	while (len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		*f = toupper(*f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		f++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		len--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)  * Check for "NAME_PATH" environment variable to override fs location (for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)  * testing). This matches the recommendation in Documentation/admin-guide/sysfs-rules.rst
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)  * for SYSFS_PATH.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static bool fs__env_override(struct fs *fs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	char *override_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	size_t name_len = strlen(fs->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	/* name + "_PATH" + '\0' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	char upper_name[name_len + 5 + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	memcpy(upper_name, fs->name, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	mem_toupper(upper_name, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	strcpy(&upper_name[name_len], "_PATH");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	override_path = getenv(upper_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	if (!override_path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	fs->found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	fs->checked = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	strncpy(fs->path, override_path, sizeof(fs->path) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	fs->path[sizeof(fs->path) - 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) static const char *fs__get_mountpoint(struct fs *fs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	if (fs__env_override(fs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		return fs->path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	if (fs__check_mounts(fs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		return fs->path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	if (fs__read_mounts(fs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		return fs->path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static const char *fs__mountpoint(int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	struct fs *fs = &fs__entries[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	if (fs->found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		return (const char *)fs->path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	/* the mount point was already checked for the mount point
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	 * but and did not exist, so return NULL to avoid scanning again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	 * This makes the found and not found paths cost equivalent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	 * in case of multiple calls.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	if (fs->checked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	return fs__get_mountpoint(fs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) static const char *mount_overload(struct fs *fs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	size_t name_len = strlen(fs->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	/* "PERF_" + name + "_ENVIRONMENT" + '\0' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	char upper_name[5 + name_len + 12 + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	snprintf(upper_name, name_len, "PERF_%s_ENVIRONMENT", fs->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	mem_toupper(upper_name, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	return getenv(upper_name) ?: *fs->mounts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) static const char *fs__mount(int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	struct fs *fs = &fs__entries[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	const char *mountpoint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	if (fs__mountpoint(idx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		return (const char *)fs->path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	mountpoint = mount_overload(fs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	if (mount(NULL, mountpoint, fs->name, 0, NULL) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	return fs__check_mounts(fs) ? fs->path : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) #define FS(name, idx)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) const char *name##__mountpoint(void)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) {						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	return fs__mountpoint(idx);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) const char *name##__mount(void)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) {						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	return fs__mount(idx);			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) bool name##__configured(void)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) {						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	return name##__mountpoint() != NULL;	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) FS(sysfs,   FS__SYSFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) FS(procfs,  FS__PROCFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) FS(debugfs, FS__DEBUGFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) FS(tracefs, FS__TRACEFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) FS(hugetlbfs, FS__HUGETLBFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) FS(bpf_fs, FS__BPF_FS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) int filename__read_int(const char *filename, int *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	char line[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	int fd = open(filename, O_RDONLY), err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	if (read(fd, line, sizeof(line)) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		*value = atoi(line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) static int filename__read_ull_base(const char *filename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 				   unsigned long long *value, int base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	char line[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	int fd = open(filename, O_RDONLY), err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	if (read(fd, line, sizeof(line)) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		*value = strtoull(line, NULL, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		if (*value != ULLONG_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 			err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)  * Parses @value out of @filename with strtoull.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)  * By using 16 for base to treat the number as hex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) int filename__read_xll(const char *filename, unsigned long long *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	return filename__read_ull_base(filename, value, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)  * Parses @value out of @filename with strtoull.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)  * By using 0 for base, the strtoull detects the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)  * base automatically (see man strtoull).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) int filename__read_ull(const char *filename, unsigned long long *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	return filename__read_ull_base(filename, value, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) #define STRERR_BUFSIZE  128     /* For the buffer size of strerror_r */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) int filename__read_str(const char *filename, char **buf, size_t *sizep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	size_t size = 0, alloc_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	void *bf = NULL, *nbf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	int fd, n, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	char sbuf[STRERR_BUFSIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	fd = open(filename, O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		if (size == alloc_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 			alloc_size += BUFSIZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 			nbf = realloc(bf, alloc_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 			if (!nbf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 				err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 			bf = nbf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		n = read(fd, bf + size, alloc_size - size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		if (n < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 			if (size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 				pr_warn("read failed %d: %s\n", errno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 					strerror_r(errno, sbuf, sizeof(sbuf)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 				err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 			} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 				err = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		size += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	} while (n > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		*sizep = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 		*buf   = bf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		free(bf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) int filename__write_int(const char *filename, int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	int fd = open(filename, O_WRONLY), err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	char buf[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	sprintf(buf, "%d", value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	if (write(fd, buf, sizeof(buf)) == sizeof(buf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) int procfs__read_str(const char *entry, char **buf, size_t *sizep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	char path[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	const char *procfs = procfs__mountpoint();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	if (!procfs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	snprintf(path, sizeof(path), "%s/%s", procfs, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	return filename__read_str(path, buf, sizep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) static int sysfs__read_ull_base(const char *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 				unsigned long long *value, int base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	char path[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	const char *sysfs = sysfs__mountpoint();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	if (!sysfs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	return filename__read_ull_base(path, value, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) int sysfs__read_xll(const char *entry, unsigned long long *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	return sysfs__read_ull_base(entry, value, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) int sysfs__read_ull(const char *entry, unsigned long long *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	return sysfs__read_ull_base(entry, value, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) int sysfs__read_int(const char *entry, int *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	char path[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	const char *sysfs = sysfs__mountpoint();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	if (!sysfs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	return filename__read_int(path, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) int sysfs__read_str(const char *entry, char **buf, size_t *sizep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	char path[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	const char *sysfs = sysfs__mountpoint();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	if (!sysfs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	return filename__read_str(path, buf, sizep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) int sysfs__read_bool(const char *entry, bool *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	ret = sysfs__read_str(entry, &buf, &size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	switch (buf[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	case '1':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	case 'y':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	case 'Y':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 		*value = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	case '0':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	case 'n':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	case 'N':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		*value = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 		ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	free(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) int sysctl__read_int(const char *sysctl, int *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	char path[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	const char *procfs = procfs__mountpoint();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	if (!procfs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	snprintf(path, sizeof(path), "%s/sys/%s", procfs, sysctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	return filename__read_int(path, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) int sysfs__write_int(const char *entry, int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	char path[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	const char *sysfs = sysfs__mountpoint();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	if (!sysfs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	if (snprintf(path, sizeof(path), "%s/%s", sysfs, entry) >= PATH_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	return filename__write_int(path, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) }