Commit 53118557 authored by Hartmut Knaack's avatar Hartmut Knaack Committed by Jonathan Cameron

tools:iio:iio_utils: add error handling

Add error handling to calls which can indicate a major problem by
returning an error code.
This also sets ret to -ENOENT in iioutils_get_type() and
iioutils_get_param_float() to indicate if no matching directory entry was
found.
Signed-off-by: default avatarHartmut Knaack <knaack.h@gmx.de>
Signed-off-by: default avatarJonathan Cameron <jic23@kernel.org>
parent 963f54ce
...@@ -50,6 +50,10 @@ int iioutils_break_up_name(const char *full_name, ...@@ -50,6 +50,10 @@ int iioutils_break_up_name(const char *full_name,
return -ENOMEM; return -ENOMEM;
working = strtok(current, "_\0"); working = strtok(current, "_\0");
if (!working) {
free(current);
return -EINVAL;
}
w = working; w = working;
r = working; r = working;
...@@ -117,6 +121,7 @@ int iioutils_get_type(unsigned *is_signed, ...@@ -117,6 +121,7 @@ int iioutils_get_type(unsigned *is_signed,
ret = -errno; ret = -errno;
goto error_free_builtname_generic; goto error_free_builtname_generic;
} }
ret = -ENOENT;
while (ent = readdir(dp), ent != NULL) while (ent = readdir(dp), ent != NULL)
/* /*
* Do we allow devices to override a generic name with * Do we allow devices to override a generic name with
...@@ -162,7 +167,12 @@ int iioutils_get_type(unsigned *is_signed, ...@@ -162,7 +167,12 @@ int iioutils_get_type(unsigned *is_signed,
*is_signed = 1; *is_signed = 1;
else else
*is_signed = 0; *is_signed = 0;
fclose(sysfsfp); if (fclose(sysfsfp)) {
ret = -errno;
printf("Failed to close %s\n", filename);
goto error_free_filename;
}
free(filename); free(filename);
filename = 0; filename = 0;
...@@ -170,12 +180,16 @@ int iioutils_get_type(unsigned *is_signed, ...@@ -170,12 +180,16 @@ int iioutils_get_type(unsigned *is_signed,
} }
error_close_sysfsfp: error_close_sysfsfp:
if (sysfsfp) if (sysfsfp)
fclose(sysfsfp); if (fclose(sysfsfp))
perror("iioutils_get_type(): Failed to close file");
error_free_filename: error_free_filename:
if (filename) if (filename)
free(filename); free(filename);
error_closedir: error_closedir:
closedir(dp); if (closedir(dp) == -1)
perror("iioutils_get_type(): Failed to close directory");
error_free_builtname_generic: error_free_builtname_generic:
free(builtname_generic); free(builtname_generic);
error_free_builtname: error_free_builtname:
...@@ -215,6 +229,7 @@ int iioutils_get_param_float(float *output, ...@@ -215,6 +229,7 @@ int iioutils_get_param_float(float *output,
ret = -errno; ret = -errno;
goto error_free_builtname_generic; goto error_free_builtname_generic;
} }
ret = -ENOENT;
while (ent = readdir(dp), ent != NULL) while (ent = readdir(dp), ent != NULL)
if ((strcmp(builtname, ent->d_name) == 0) || if ((strcmp(builtname, ent->d_name) == 0) ||
(strcmp(builtname_generic, ent->d_name) == 0)) { (strcmp(builtname_generic, ent->d_name) == 0)) {
...@@ -229,14 +244,19 @@ int iioutils_get_param_float(float *output, ...@@ -229,14 +244,19 @@ int iioutils_get_param_float(float *output,
ret = -errno; ret = -errno;
goto error_free_filename; goto error_free_filename;
} }
fscanf(sysfsfp, "%f", output); errno = 0;
if (fscanf(sysfsfp, "%f", output) != 1)
ret = errno ? -errno : -ENODATA;
break; break;
} }
error_free_filename: error_free_filename:
if (filename) if (filename)
free(filename); free(filename);
error_closedir: error_closedir:
closedir(dp); if (closedir(dp) == -1)
perror("iioutils_get_param_float(): Failed to close directory");
error_free_builtname_generic: error_free_builtname_generic:
free(builtname_generic); free(builtname_generic);
error_free_builtname: error_free_builtname:
...@@ -310,10 +330,24 @@ int build_channel_array(const char *device_dir, ...@@ -310,10 +330,24 @@ int build_channel_array(const char *device_dir,
free(filename); free(filename);
goto error_close_dir; goto error_close_dir;
} }
fscanf(sysfsfp, "%i", &ret); errno = 0;
if (fscanf(sysfsfp, "%i", &ret) != 1) {
ret = errno ? -errno : -ENODATA;
if (fclose(sysfsfp))
perror("build_channel_array(): Failed to close file");
free(filename);
goto error_close_dir;
}
if (ret == 1) if (ret == 1)
(*counter)++; (*counter)++;
fclose(sysfsfp); if (fclose(sysfsfp)) {
ret = -errno;
free(filename);
goto error_close_dir;
}
free(filename); free(filename);
} }
*ci_array = malloc(sizeof(**ci_array) * (*counter)); *ci_array = malloc(sizeof(**ci_array) * (*counter));
...@@ -344,8 +378,20 @@ int build_channel_array(const char *device_dir, ...@@ -344,8 +378,20 @@ int build_channel_array(const char *device_dir,
count--; count--;
goto error_cleanup_array; goto error_cleanup_array;
} }
fscanf(sysfsfp, "%i", &current_enabled); errno = 0;
fclose(sysfsfp); if (fscanf(sysfsfp, "%i", &current_enabled) != 1) {
ret = errno ? -errno : -ENODATA;
free(filename);
count--;
goto error_cleanup_array;
}
if (fclose(sysfsfp)) {
ret = -errno;
free(filename);
count--;
goto error_cleanup_array;
}
if (!current_enabled) { if (!current_enabled) {
free(filename); free(filename);
...@@ -383,8 +429,29 @@ int build_channel_array(const char *device_dir, ...@@ -383,8 +429,29 @@ int build_channel_array(const char *device_dir,
goto error_cleanup_array; goto error_cleanup_array;
} }
sysfsfp = fopen(filename, "r"); sysfsfp = fopen(filename, "r");
fscanf(sysfsfp, "%u", &current->index); if (sysfsfp == NULL) {
fclose(sysfsfp); ret = -errno;
printf("failed to open %s\n", filename);
free(filename);
goto error_cleanup_array;
}
errno = 0;
if (fscanf(sysfsfp, "%u", &current->index) != 1) {
ret = errno ? -errno : -ENODATA;
if (fclose(sysfsfp))
perror("build_channel_array(): Failed to close file");
free(filename);
goto error_cleanup_array;
}
if (fclose(sysfsfp)) {
ret = -errno;
free(filename);
goto error_cleanup_array;
}
free(filename); free(filename);
/* Find the scale */ /* Find the scale */
ret = iioutils_get_param_float(&current->scale, ret = iioutils_get_param_float(&current->scale,
...@@ -410,10 +477,16 @@ int build_channel_array(const char *device_dir, ...@@ -410,10 +477,16 @@ int build_channel_array(const char *device_dir,
device_dir, device_dir,
current->name, current->name,
current->generic_name); current->generic_name);
if (ret < 0)
goto error_cleanup_array;
} }
} }
closedir(dp); if (closedir(dp) == -1) {
ret = -errno;
goto error_cleanup_array;
}
free(scan_el_dir); free(scan_el_dir);
/* reorder so that the array is in index order */ /* reorder so that the array is in index order */
bsort_channel_array_by_index(ci_array, *counter); bsort_channel_array_by_index(ci_array, *counter);
...@@ -427,7 +500,10 @@ int build_channel_array(const char *device_dir, ...@@ -427,7 +500,10 @@ int build_channel_array(const char *device_dir,
} }
free(*ci_array); free(*ci_array);
error_close_dir: error_close_dir:
closedir(dp); if (dp)
if (closedir(dp) == -1)
perror("build_channel_array(): Failed to close dir");
error_free_name: error_free_name:
free(scan_el_dir); free(scan_el_dir);
error_ret: error_ret:
...@@ -496,29 +572,45 @@ int find_type_by_name(const char *name, const char *type) ...@@ -496,29 +572,45 @@ int find_type_by_name(const char *name, const char *type)
+ numstrlen + numstrlen
+ 6); + 6);
if (filename == NULL) { if (filename == NULL) {
closedir(dp); ret = -ENOMEM;
return -ENOMEM; goto error_close_dir;
} }
sprintf(filename, "%s%s%d/name",
iio_dir, ret = sprintf(filename, "%s%s%d/name", iio_dir,
type, type, number);
number); if (ret < 0) {
free(filename);
goto error_close_dir;
}
nameFile = fopen(filename, "r"); nameFile = fopen(filename, "r");
if (!nameFile) { if (!nameFile) {
free(filename); free(filename);
continue; continue;
} }
free(filename); free(filename);
fscanf(nameFile, "%s", thisname); errno = 0;
fclose(nameFile); if (fscanf(nameFile, "%s", thisname) != 1) {
ret = errno ? -errno : -ENODATA;
goto error_close_dir;
}
if (fclose(nameFile)) {
ret = -errno;
goto error_close_dir;
}
if (strcmp(name, thisname) == 0) { if (strcmp(name, thisname) == 0) {
closedir(dp); if (closedir(dp) == -1)
return -errno;
return number; return number;
} }
} }
} }
} }
closedir(dp); if (closedir(dp) == -1)
return -errno;
return -ENODEV; return -ENODEV;
error_close_dir: error_close_dir:
...@@ -536,15 +628,29 @@ static int _write_sysfs_int(char *filename, char *basedir, int val, int verify) ...@@ -536,15 +628,29 @@ static int _write_sysfs_int(char *filename, char *basedir, int val, int verify)
if (temp == NULL) if (temp == NULL)
return -ENOMEM; return -ENOMEM;
sprintf(temp, "%s/%s", basedir, filename); ret = sprintf(temp, "%s/%s", basedir, filename);
if (ret < 0)
goto error_free;
sysfsfp = fopen(temp, "w"); sysfsfp = fopen(temp, "w");
if (sysfsfp == NULL) { if (sysfsfp == NULL) {
ret = -errno; ret = -errno;
printf("failed to open %s\n", temp); printf("failed to open %s\n", temp);
goto error_free; goto error_free;
} }
fprintf(sysfsfp, "%d", val); ret = fprintf(sysfsfp, "%d", val);
fclose(sysfsfp); if (ret < 0) {
if (fclose(sysfsfp))
perror("_write_sysfs_int(): Failed to close dir");
goto error_free;
}
if (fclose(sysfsfp)) {
ret = -errno;
goto error_free;
}
if (verify) { if (verify) {
sysfsfp = fopen(temp, "r"); sysfsfp = fopen(temp, "r");
if (sysfsfp == NULL) { if (sysfsfp == NULL) {
...@@ -552,8 +658,19 @@ static int _write_sysfs_int(char *filename, char *basedir, int val, int verify) ...@@ -552,8 +658,19 @@ static int _write_sysfs_int(char *filename, char *basedir, int val, int verify)
printf("failed to open %s\n", temp); printf("failed to open %s\n", temp);
goto error_free; goto error_free;
} }
fscanf(sysfsfp, "%d", &test); if (fscanf(sysfsfp, "%d", &test) != 1) {
fclose(sysfsfp); ret = errno ? -errno : -ENODATA;
if (fclose(sysfsfp))
perror("_write_sysfs_int(): Failed to close dir");
goto error_free;
}
if (fclose(sysfsfp)) {
ret = -errno;
goto error_free;
}
if (test != val) { if (test != val) {
printf("Possible failure in int write %d to %s%s\n", printf("Possible failure in int write %d to %s%s\n",
val, val,
...@@ -588,15 +705,29 @@ static int _write_sysfs_string(char *filename, char *basedir, char *val, ...@@ -588,15 +705,29 @@ static int _write_sysfs_string(char *filename, char *basedir, char *val,
printf("Memory allocation failed\n"); printf("Memory allocation failed\n");
return -ENOMEM; return -ENOMEM;
} }
sprintf(temp, "%s/%s", basedir, filename); ret = sprintf(temp, "%s/%s", basedir, filename);
if (ret < 0)
goto error_free;
sysfsfp = fopen(temp, "w"); sysfsfp = fopen(temp, "w");
if (sysfsfp == NULL) { if (sysfsfp == NULL) {
ret = -errno; ret = -errno;
printf("Could not open %s\n", temp); printf("Could not open %s\n", temp);
goto error_free; goto error_free;
} }
fprintf(sysfsfp, "%s", val); ret = fprintf(sysfsfp, "%s", val);
fclose(sysfsfp); if (ret < 0) {
if (fclose(sysfsfp))
perror("_write_sysfs_string(): Failed to close dir");
goto error_free;
}
if (fclose(sysfsfp)) {
ret = -errno;
goto error_free;
}
if (verify) { if (verify) {
sysfsfp = fopen(temp, "r"); sysfsfp = fopen(temp, "r");
if (sysfsfp == NULL) { if (sysfsfp == NULL) {
...@@ -604,8 +735,19 @@ static int _write_sysfs_string(char *filename, char *basedir, char *val, ...@@ -604,8 +735,19 @@ static int _write_sysfs_string(char *filename, char *basedir, char *val,
printf("could not open file to verify\n"); printf("could not open file to verify\n");
goto error_free; goto error_free;
} }
fscanf(sysfsfp, "%s", temp); if (fscanf(sysfsfp, "%s", temp) != 1) {
fclose(sysfsfp); ret = errno ? -errno : -ENODATA;
if (fclose(sysfsfp))
perror("_write_sysfs_string(): Failed to close dir");
goto error_free;
}
if (fclose(sysfsfp)) {
ret = -errno;
goto error_free;
}
if (strcmp(temp, val) != 0) { if (strcmp(temp, val) != 0) {
printf("Possible failure in string write of %s " printf("Possible failure in string write of %s "
"Should be %s " "Should be %s "
...@@ -649,14 +791,27 @@ int read_sysfs_posint(char *filename, char *basedir) ...@@ -649,14 +791,27 @@ int read_sysfs_posint(char *filename, char *basedir)
printf("Memory allocation failed"); printf("Memory allocation failed");
return -ENOMEM; return -ENOMEM;
} }
sprintf(temp, "%s/%s", basedir, filename); ret = sprintf(temp, "%s/%s", basedir, filename);
if (ret < 0)
goto error_free;
sysfsfp = fopen(temp, "r"); sysfsfp = fopen(temp, "r");
if (sysfsfp == NULL) { if (sysfsfp == NULL) {
ret = -errno; ret = -errno;
goto error_free; goto error_free;
} }
fscanf(sysfsfp, "%d\n", &ret); errno = 0;
fclose(sysfsfp); if (fscanf(sysfsfp, "%d\n", &ret) != 1) {
ret = errno ? -errno : -ENODATA;
if (fclose(sysfsfp))
perror("read_sysfs_posint(): Failed to close dir");
goto error_free;
}
if (fclose(sysfsfp))
ret = -errno;
error_free: error_free:
free(temp); free(temp);
return ret; return ret;
...@@ -672,14 +827,27 @@ int read_sysfs_float(char *filename, char *basedir, float *val) ...@@ -672,14 +827,27 @@ int read_sysfs_float(char *filename, char *basedir, float *val)
printf("Memory allocation failed"); printf("Memory allocation failed");
return -ENOMEM; return -ENOMEM;
} }
sprintf(temp, "%s/%s", basedir, filename); ret = sprintf(temp, "%s/%s", basedir, filename);
if (ret < 0)
goto error_free;
sysfsfp = fopen(temp, "r"); sysfsfp = fopen(temp, "r");
if (sysfsfp == NULL) { if (sysfsfp == NULL) {
ret = -errno; ret = -errno;
goto error_free; goto error_free;
} }
fscanf(sysfsfp, "%f\n", val); errno = 0;
fclose(sysfsfp); if (fscanf(sysfsfp, "%f\n", val) != 1) {
ret = errno ? -errno : -ENODATA;
if (fclose(sysfsfp))
perror("read_sysfs_float(): Failed to close dir");
goto error_free;
}
if (fclose(sysfsfp))
ret = -errno;
error_free: error_free:
free(temp); free(temp);
return ret; return ret;
...@@ -695,14 +863,27 @@ int read_sysfs_string(const char *filename, const char *basedir, char *str) ...@@ -695,14 +863,27 @@ int read_sysfs_string(const char *filename, const char *basedir, char *str)
printf("Memory allocation failed"); printf("Memory allocation failed");
return -ENOMEM; return -ENOMEM;
} }
sprintf(temp, "%s/%s", basedir, filename); ret = sprintf(temp, "%s/%s", basedir, filename);
if (ret < 0)
goto error_free;
sysfsfp = fopen(temp, "r"); sysfsfp = fopen(temp, "r");
if (sysfsfp == NULL) { if (sysfsfp == NULL) {
ret = -errno; ret = -errno;
goto error_free; goto error_free;
} }
fscanf(sysfsfp, "%s\n", str); errno = 0;
fclose(sysfsfp); if (fscanf(sysfsfp, "%s\n", str) != 1) {
ret = errno ? -errno : -ENODATA;
if (fclose(sysfsfp))
perror("read_sysfs_string(): Failed to close dir");
goto error_free;
}
if (fclose(sysfsfp))
ret = -errno;
error_free: error_free:
free(temp); free(temp);
return ret; return ret;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment