summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLee Duncan <lduncan@suse.com>2018-09-24 16:37:19 -0700
committerLee Duncan <lduncan@suse.com>2018-09-26 10:53:53 -0700
commit09d7031cb462889392090e71991a89c522d387bc (patch)
tree3148347cb680c351673a472703027957a5cf4cfd
parent7df5edbb427c79337bdd651c2bcdda8eb05f077b (diff)
downloadopen-iscsi-09d7031cb462889392090e71991a89c522d387bc.tar.gz
Fix reading of sysfs signed integers when negative.
The code for reading all sysfs integer types (of all sizes) did not work when reading signed integers and the return value was negative. So when the default was -1 and the value was not present, the code tried to return -1. But the logic for checking against "max value" was incorrect, causing INT_MAX to be returned instead of -1.
-rw-r--r--libopeniscsiusr/sysfs.c32
1 files changed, 27 insertions, 5 deletions
diff --git a/libopeniscsiusr/sysfs.c b/libopeniscsiusr/sysfs.c
index 08f71b3..c4f89a3 100644
--- a/libopeniscsiusr/sysfs.c
+++ b/libopeniscsiusr/sysfs.c
@@ -47,7 +47,7 @@
#define _SYS_NULL_STR "(null)"
-#define _sysfs_prop_get_int_func_gen(func_name, out_type, type_max_value) \
+#define _sysfs_prop_get_uint_func_gen(func_name, out_type, type_max_value) \
int func_name(struct iscsi_context *ctx, const char *dir_path, \
const char *prop_name, out_type *val, \
out_type default_value, bool ignore_error) \
@@ -63,6 +63,28 @@
return rc; \
}
+#define _sysfs_prop_get_int_func_gen(func_name, out_type, type_min_value, type_max_value) \
+ int func_name(struct iscsi_context *ctx, const char *dir_path, \
+ const char *prop_name, out_type *val, \
+ out_type default_value, bool ignore_error) \
+ { \
+ long long int tmp_val = 0; \
+ int rc = LIBISCSI_OK; \
+ long long int dv = default_value; \
+ rc = iscsi_sysfs_prop_get_ll(ctx, dir_path, prop_name, \
+ &tmp_val, (long long int) dv, \
+ ignore_error); \
+ if (rc == LIBISCSI_OK) { \
+ if (tmp_val > type_max_value) \
+ *val = type_max_value; \
+ else if (tmp_val < type_min_value) \
+ *val = type_min_value; \
+ else \
+ *val = tmp_val; \
+ } \
+ return rc; \
+ }
+
enum _sysfs_dev_class {
_SYSFS_DEV_CLASS_ISCSI_SESSION,
@@ -82,10 +104,10 @@ static int iscsi_sysfs_prop_get_ll(struct iscsi_context *ctx,
static int sysfs_get_dev_path(struct iscsi_context *ctx, const char *path,
enum _sysfs_dev_class class, char **dev_path);
-_sysfs_prop_get_int_func_gen(_sysfs_prop_get_u8, uint8_t, UINT8_MAX);
-_sysfs_prop_get_int_func_gen(_sysfs_prop_get_u16, uint16_t, UINT16_MAX);
-_sysfs_prop_get_int_func_gen(_sysfs_prop_get_i32, int32_t, INT32_MAX);
-_sysfs_prop_get_int_func_gen(_sysfs_prop_get_u32, uint32_t, UINT32_MAX);
+_sysfs_prop_get_uint_func_gen(_sysfs_prop_get_u8, uint8_t, UINT8_MAX);
+_sysfs_prop_get_uint_func_gen(_sysfs_prop_get_u16, uint16_t, UINT16_MAX);
+_sysfs_prop_get_int_func_gen(_sysfs_prop_get_i32, int32_t, INT32_MIN, INT32_MAX);
+_sysfs_prop_get_uint_func_gen(_sysfs_prop_get_u32, uint32_t, UINT32_MAX);
static int sysfs_read_file(const char *path, uint8_t *buff, size_t buff_size)
{