/* fname is a relative path under "cpuX/cpufreq" dir */ static unsigned int sysfs_cpufreq_read_file(unsigned int cpu, const char *fname, char *buf, size_t buflen) { char path[SYSFS_PATH_MAX]; snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/cpufreq/%s", cpu, fname); return sysfs_read_file(path, buf, buflen); }
int _sysfs_prop_get_str(struct iscsi_context *ctx, const char *dir_path, const char *prop_name, char *buff, size_t buff_size, const char *default_value) { char file_path[PATH_MAX]; int rc = LIBISCSI_OK; int errno_save = 0; assert(dir_path != NULL); assert(prop_name != NULL); assert(buff != NULL); snprintf(file_path, PATH_MAX, "%s/%s", dir_path, prop_name); errno_save = sysfs_read_file(file_path, (uint8_t *) buff, buff_size); if (errno_save != 0) { if (errno_save == ENOENT) { if (default_value == NULL) { rc = LIBISCSI_ERR_SYSFS_LOOKUP; _error(ctx, "Failed to read '%s': " "file '%s' does not exists", prop_name, file_path); } else { _info(ctx, "Failed to read '%s': " "file '%s' does not exists, " "using default value %s", prop_name, file_path, default_value); memcpy(buff, (void *) default_value, strlen(default_value) + 1); } } else if (errno_save == EACCES) { rc = LIBISCSI_ERR_ACCESS; _error(ctx, "Failed to read '%s': " "permission deny when reading '%s'", prop_name, file_path); } else { rc = LIBISCSI_ERR_BUG; _error(ctx, "Failed to read '%s': " "error when reading '%s': %d", prop_name, file_path, errno_save); } } else { if ((buff[0] == '\0') && (default_value != NULL)) { memcpy(buff, (void *) default_value, strlen(default_value) + 1); _debug(ctx, "Open '%s', got NULL, using default value", file_path, default_value); } else _debug(ctx, "Open '%s', got '%s'", file_path, buff); } return rc; }
/* returns -1 on failure, 0 on success */ int sysfs_topology_read_file(unsigned int cpu, const char *fname) { unsigned long value; char linebuf[MAX_LINE_LEN]; char *endp; char path[SYSFS_PATH_MAX]; snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/topology/%s", cpu, fname); if (sysfs_read_file(path, linebuf, MAX_LINE_LEN) == 0) return -1; value = strtoul(linebuf, &endp, 0); if (endp == linebuf || errno == ERANGE) return -1; return value; }
int sysfs_get_sched(const char *smt_mc) { unsigned long value; char linebuf[MAX_LINE_LEN]; char *endp; char path[SYSFS_PATH_MAX]; if (strcmp("mc", smt_mc) && strcmp("smt", smt_mc)) return -EINVAL; snprintf(path, sizeof(path), PATH_TO_CPU "sched_%s_power_savings", smt_mc); if (sysfs_read_file(path, linebuf, MAX_LINE_LEN) == 0) return -1; value = strtoul(linebuf, &endp, 0); if (endp == linebuf || errno == ERANGE) return -1; return value; }
static int iscsi_sysfs_prop_get_ll(struct iscsi_context *ctx, const char *dir_path, const char *prop_name, long long int *val, long long int default_value, bool ignore_error) { char file_path[PATH_MAX]; int rc = LIBISCSI_OK; int errno_save = 0; uint8_t buff[_INT32_STR_MAX_LEN]; long long int tmp_val = 0; assert(dir_path != NULL); assert(prop_name != NULL); assert(val != NULL); *val = 0; snprintf(file_path, PATH_MAX, "%s/%s", dir_path, prop_name); errno_save = sysfs_read_file(file_path, buff, _INT32_STR_MAX_LEN); if (errno_save != 0) { if (errno_save == ENOENT) { if (! ignore_error) { rc = LIBISCSI_ERR_SYSFS_LOOKUP; _error(ctx, "Failed to read '%s': " "file '%s' does not exists", prop_name, file_path); return rc; } else { _info(ctx, "Failed to read '%s': " "File '%s' does not exists, using ", "default value %lld", file_path, default_value); *val = default_value; return rc; } } else if (errno_save == EACCES) { rc = LIBISCSI_ERR_ACCESS; _error(ctx, "Permission deny when reading '%s'", file_path); return rc; } else { rc = LIBISCSI_ERR_BUG; _error(ctx, "Error when reading '%s': %d", file_path, errno_save); return rc; } } errno = 0; tmp_val = strtoll((const char *) buff, NULL, 10 /* base */); errno_save = errno; if ((errno_save != 0) && (! ignore_error)) { rc = LIBISCSI_ERR_BUG; _error(ctx, "Sysfs: %s: Error when converting '%s' " "to number", file_path, (char *) buff, errno_save); return rc; } *val = tmp_val; _debug(ctx, "Open '%s', got %lld", file_path, tmp_val); return rc; }