Files
s390-tools/libcpumf/libcpumf_pmutype.c
Thomas Richter fed79474e4 cpumf: Convert S390_CPUMF_XXX to util_path_sysfs()
Use util_path_sysfs() to form a fully qualified sysfs file name
for files S390_CPUMF_CF, S390_CPUMF_CFDIAG, S390_CPUMF_SF,
S390_SYSFS_PAI_CRYPTO and S390_SYSFS_PAI_EXT.
No functional change.

Signed-off-by: Thomas Richter <tmricht@linux.ibm.com>
Reviewed-by: Sumanth Korikkar <sumanthk@linux.ibm.com>
Reviewed-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
2025-04-16 12:12:45 +02:00

70 lines
1.5 KiB
C

/* Copyright IBM Corp. 2022, 2025
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lib/util_file.h"
#include "lib/util_libc.h"
#include "lib/util_path.h"
#include "lib/util_scandir.h"
#include "lib/libcpumf.h"
int libcpumf_pmutype(const char *dirname)
{
int ret = -1;
FILE *file;
char *fn;
fn = util_path_sysfs("%s/type", dirname);
file = fopen(fn, "r");
free(fn);
if (file) {
/* Read out a single number from that file */
if (fscanf(file, "%u", &ret) != 1)
/* Unexpected format error, set errno */
errno = -ERANGE;
fclose(file);
}
return ret;
}
int libcpumf_pmuname(unsigned int wanted_type, char **name)
{
struct dirent **de_vec;
unsigned int type;
char *dirname;
char *path;
int count;
int rc;
path = util_path_sysfs("devices");
count = util_scandir(&de_vec, alphasort, path, "(pai|cpum_).*");
free(path);
*name = NULL;
for (int i = 0; i < count; i++) {
if (de_vec[i]->d_type == DT_DIR) {
dirname = de_vec[i]->d_name;
path = util_path_sysfs("devices/%s/type", dirname);
rc = util_file_read_ui(&type, 10, path);
if (rc)
warn("Failed to open %s", path);
free(path);
if (!rc && type == wanted_type) {
*name = util_strdup(dirname);
goto out;
}
}
}
out:
util_scandir_free(de_vec, count);
return *name ? 0 : -1;
}