1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
/* SPDX-License-Identifier: LGPL-2.1 */
/* Copyright(c) 2014-2025 Intel Corporation. All rights reserved. */
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <util/log.h>
#include <util/sysfs.h>
int __sysfs_read_attr(struct log_ctx *ctx, const char *path, char *buf)
{
int fd = open(path, O_RDONLY|O_CLOEXEC);
int n;
if (fd < 0) {
return -errno;
}
n = read(fd, buf, SYSFS_ATTR_SIZE);
close(fd);
if (n < 0 || n >= SYSFS_ATTR_SIZE) {
buf[0] = 0;
log_dbg(ctx, "failed to read %s: %s\n", path, strerror(errno));
return -errno;
}
buf[n] = 0;
if (n && buf[n-1] == '\n'){
buf[n-1] = 0;
}
return 0;
}
static int write_attr(struct log_ctx *ctx, const char *path,
const char *buf, int quiet)
{
int fd = open(path, O_WRONLY|O_CLOEXEC);
int n, len = strlen(buf), rc;
if (fd < 0) {
rc = -errno;
log_dbg(ctx, "failed to open %s: %s\n", path, strerror(errno));
return rc;
}
n = write(fd, buf, len);
rc = -errno;
close(fd);
if (n < len) {
if (!quiet)
log_dbg(ctx, "failed to write %s to %s: %s\n", buf, path,
strerror(errno));
return rc;
}
return 0;
}
int __sysfs_write_attr(struct log_ctx *ctx, const char *path,
const char *buf)
{
return write_attr(ctx, path, buf, 0);
}
int __sysfs_write_attr_quiet(struct log_ctx *ctx, const char *path,
const char *buf)
{
return write_attr(ctx, path, buf, 1);
}
int __sysfs_device_parse(struct log_ctx *ctx, const char *base_path,
char *dev_prefix, char *bus_type,
int (*filter)(const struct dirent *),
void *parent, add_dev_fn add_dev)
{
int add_errors = 0;
struct dirent **d, *de;
int i, n;
n = scandir(base_path, &d, filter, alphasort);
if (n == -1)
return -ENODEV;
for (i = 0; i < n; i++) {
char *dev_path;
void *dev;
int id;
de = d[i];
if (sscanf(de->d_name, "%*[a-z]%d", &id) < 0) {
while (n--)
free(d[n]);
free(d);
return -EINVAL;
}
if (strchr(de->d_name, '!'))
continue;
if (asprintf(&dev_path, "%s/%s", base_path, de->d_name) < 0) {
log_err(ctx, "%s%d: path allocation failure\n",
de->d_name, id);
continue;
}
dev = add_dev(parent, id, dev_path, dev_prefix, bus_type);
free(dev_path);
if (!dev) {
add_errors++;
log_err(ctx, "%d: add_dev() failed\n", id);
} else
log_dbg(ctx, "%d: processed\n", id);
}
while (n--)
free(d[n]);
free(d);
return add_errors;
}
|