#define CHMEM_EXIT_SOMEOK 64
#define _PATH_SYS_MEMORY "/sys/devices/system/memory"
-#define _PATH_SYS_MEMORY_BLOCK_SIZE _PATH_SYS_MEMORY "/block_size_bytes"
struct chmem_desc {
+ struct path_cxt *sysmem; /* _PATH_SYS_MEMORY handler */
struct dirent **dirs;
int ndirs;
uint64_t block_size;
for (; i >= 0 && i < desc->ndirs && size; i += enable ? 1 : -1) {
name = desc->dirs[i]->d_name;
index = strtou64_or_err(name + 6, _("Failed to parse index"));
- path_read_str(line, sizeof(line), _PATH_SYS_MEMORY "/%s/state", name);
- if (strncmp(onoff, line, 6) == 0)
+
+ if (ul_path_readf_buffer(desc->sysmem, line, sizeof(line), "%s/state", name) > 0
+ && strncmp(onoff, line, 6) == 0)
continue;
if (desc->have_zones) {
- path_read_str(line, sizeof(line),
- _PATH_SYS_MEMORY "/%s/valid_zones", name);
+ ul_path_readf_buffer(desc->sysmem, line, sizeof(line), "%s/valid_zones", name);
if (zone_id >= 0) {
zn = zone_names[zone_id];
if (enable && !strcasestr(line, zn))
}
idxtostr(desc, index, str, sizeof(str));
- rc = path_write_str(onoff, _PATH_SYS_MEMORY"/%s/state", name);
- if (rc == -1 && desc->verbose) {
+ rc = ul_path_writef_string(desc->sysmem, onoff, "%s/state", name);
+ if (rc != 0 && desc->verbose) {
if (enable)
fprintf(stdout, _("%s enable failed\n"), str);
else
if (index > desc->end)
break;
idxtostr(desc, index, str, sizeof(str));
- path_read_str(line, sizeof(line), _PATH_SYS_MEMORY "/%s/state", name);
- if (strncmp(onoff, line, 6) == 0) {
+ if (ul_path_readf_buffer(desc->sysmem, line, sizeof(line), "%s/state", name) > 0
+ && strncmp(onoff, line, 6) == 0) {
if (desc->verbose && enable)
fprintf(stdout, _("%s already enabled\n"), str);
else if (desc->verbose && !enable)
}
if (desc->have_zones) {
- path_read_str(line, sizeof(line),
- _PATH_SYS_MEMORY "/%s/valid_zones", name);
+ ul_path_readf_buffer(desc->sysmem, line, sizeof(line), "%s/valid_zones", name);
if (zone_id >= 0) {
zn = zone_names[zone_id];
if (enable && !strcasestr(line, zn)) {
}
}
- rc = path_write_str(onoff, _PATH_SYS_MEMORY"/%s/state", name);
- if (rc == -1) {
+ rc = ul_path_writef_string(desc->sysmem, onoff, "%s/state", name);
+ if (rc != 0) {
if (enable)
warn(_("%s enable failed"), str);
else
static void read_info(struct chmem_desc *desc)
{
- char line[BUFSIZ];
+ char line[128];
desc->ndirs = scandir(_PATH_SYS_MEMORY, &desc->dirs, filter, versionsort);
if (desc->ndirs <= 0)
err(EXIT_FAILURE, _("Failed to read %s"), _PATH_SYS_MEMORY);
- path_read_str(line, sizeof(line), _PATH_SYS_MEMORY_BLOCK_SIZE);
+ ul_path_read_buffer(desc->sysmem, line, sizeof(line), "block_size_bytes");
desc->block_size = strtoumax(line, NULL, 16);
}
textdomain(PACKAGE);
atexit(close_stdout);
+ ul_path_init_debug();
+ desc->sysmem = ul_new_path(_PATH_SYS_MEMORY);
+ if (!desc->sysmem)
+ err(EXIT_FAILURE, _("failed to initialize %s handler"), _PATH_SYS_MEMORY);
+
read_info(desc);
while ((c = getopt_long(argc, argv, "bdehvVz:", longopts, NULL)) != -1) {
parse_parameter(desc, argv[optind]);
+
/* The valid_zones sysfs attribute was introduced with kernel 3.18 */
- if (path_exist(_PATH_SYS_MEMORY "/memory0/valid_zones"))
+ if (ul_path_access(desc->sysmem, F_OK, "memory0/valid_zones"))
desc->have_zones = 1;
else if (zone)
warnx(_("zone ignored, no valid_zones sysfs attribute present"));
else
rc = chmem_range(desc, cmd == CMD_MEMORY_ENABLE ? 1 : 0, zone_id);
+ ul_unref_path(desc->sysmem);
+
return rc == 0 ? EXIT_SUCCESS :
rc < 0 ? EXIT_FAILURE : CHMEM_EXIT_SOMEOK;
}