diff options
author | Hartmut Knaack <knaack.h@gmx.de> | 2015-05-31 08:40:14 -0400 |
---|---|---|
committer | Jonathan Cameron <jic23@kernel.org> | 2015-05-31 14:43:50 -0400 |
commit | 53118557b6a9c263e4a80825da367b2116529541 (patch) | |
tree | 4339029fde36a15974c2271d948c219f5967b2fb /tools/iio | |
parent | 963f54cef23b7e8c91bbe60b978b5f4a3e990f2c (diff) |
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: Hartmut Knaack <knaack.h@gmx.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>
Diffstat (limited to 'tools/iio')
-rw-r--r-- | tools/iio/iio_utils.c | 265 |
1 files changed, 223 insertions, 42 deletions
diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c index f0896f46847f..e1828d0d8a38 100644 --- a/tools/iio/iio_utils.c +++ b/tools/iio/iio_utils.c | |||
@@ -50,6 +50,10 @@ int iioutils_break_up_name(const char *full_name, | |||
50 | return -ENOMEM; | 50 | return -ENOMEM; |
51 | 51 | ||
52 | working = strtok(current, "_\0"); | 52 | working = strtok(current, "_\0"); |
53 | if (!working) { | ||
54 | free(current); | ||
55 | return -EINVAL; | ||
56 | } | ||
53 | 57 | ||
54 | w = working; | 58 | w = working; |
55 | r = working; | 59 | r = working; |
@@ -117,6 +121,7 @@ int iioutils_get_type(unsigned *is_signed, | |||
117 | ret = -errno; | 121 | ret = -errno; |
118 | goto error_free_builtname_generic; | 122 | goto error_free_builtname_generic; |
119 | } | 123 | } |
124 | ret = -ENOENT; | ||
120 | while (ent = readdir(dp), ent != NULL) | 125 | while (ent = readdir(dp), ent != NULL) |
121 | /* | 126 | /* |
122 | * Do we allow devices to override a generic name with | 127 | * Do we allow devices to override a generic name with |
@@ -162,7 +167,12 @@ int iioutils_get_type(unsigned *is_signed, | |||
162 | *is_signed = 1; | 167 | *is_signed = 1; |
163 | else | 168 | else |
164 | *is_signed = 0; | 169 | *is_signed = 0; |
165 | fclose(sysfsfp); | 170 | if (fclose(sysfsfp)) { |
171 | ret = -errno; | ||
172 | printf("Failed to close %s\n", filename); | ||
173 | goto error_free_filename; | ||
174 | } | ||
175 | |||
166 | free(filename); | 176 | free(filename); |
167 | 177 | ||
168 | filename = 0; | 178 | filename = 0; |
@@ -170,12 +180,16 @@ int iioutils_get_type(unsigned *is_signed, | |||
170 | } | 180 | } |
171 | error_close_sysfsfp: | 181 | error_close_sysfsfp: |
172 | if (sysfsfp) | 182 | if (sysfsfp) |
173 | fclose(sysfsfp); | 183 | if (fclose(sysfsfp)) |
184 | perror("iioutils_get_type(): Failed to close file"); | ||
185 | |||
174 | error_free_filename: | 186 | error_free_filename: |
175 | if (filename) | 187 | if (filename) |
176 | free(filename); | 188 | free(filename); |
177 | error_closedir: | 189 | error_closedir: |
178 | closedir(dp); | 190 | if (closedir(dp) == -1) |
191 | perror("iioutils_get_type(): Failed to close directory"); | ||
192 | |||
179 | error_free_builtname_generic: | 193 | error_free_builtname_generic: |
180 | free(builtname_generic); | 194 | free(builtname_generic); |
181 | error_free_builtname: | 195 | error_free_builtname: |
@@ -215,6 +229,7 @@ int iioutils_get_param_float(float *output, | |||
215 | ret = -errno; | 229 | ret = -errno; |
216 | goto error_free_builtname_generic; | 230 | goto error_free_builtname_generic; |
217 | } | 231 | } |
232 | ret = -ENOENT; | ||
218 | while (ent = readdir(dp), ent != NULL) | 233 | while (ent = readdir(dp), ent != NULL) |
219 | if ((strcmp(builtname, ent->d_name) == 0) || | 234 | if ((strcmp(builtname, ent->d_name) == 0) || |
220 | (strcmp(builtname_generic, ent->d_name) == 0)) { | 235 | (strcmp(builtname_generic, ent->d_name) == 0)) { |
@@ -229,14 +244,19 @@ int iioutils_get_param_float(float *output, | |||
229 | ret = -errno; | 244 | ret = -errno; |
230 | goto error_free_filename; | 245 | goto error_free_filename; |
231 | } | 246 | } |
232 | fscanf(sysfsfp, "%f", output); | 247 | errno = 0; |
248 | if (fscanf(sysfsfp, "%f", output) != 1) | ||
249 | ret = errno ? -errno : -ENODATA; | ||
250 | |||
233 | break; | 251 | break; |
234 | } | 252 | } |
235 | error_free_filename: | 253 | error_free_filename: |
236 | if (filename) | 254 | if (filename) |
237 | free(filename); | 255 | free(filename); |
238 | error_closedir: | 256 | error_closedir: |
239 | closedir(dp); | 257 | if (closedir(dp) == -1) |
258 | perror("iioutils_get_param_float(): Failed to close directory"); | ||
259 | |||
240 | error_free_builtname_generic: | 260 | error_free_builtname_generic: |
241 | free(builtname_generic); | 261 | free(builtname_generic); |
242 | error_free_builtname: | 262 | error_free_builtname: |
@@ -310,10 +330,24 @@ int build_channel_array(const char *device_dir, | |||
310 | free(filename); | 330 | free(filename); |
311 | goto error_close_dir; | 331 | goto error_close_dir; |
312 | } | 332 | } |
313 | fscanf(sysfsfp, "%i", &ret); | 333 | errno = 0; |
334 | if (fscanf(sysfsfp, "%i", &ret) != 1) { | ||
335 | ret = errno ? -errno : -ENODATA; | ||
336 | if (fclose(sysfsfp)) | ||
337 | perror("build_channel_array(): Failed to close file"); | ||
338 | |||
339 | free(filename); | ||
340 | goto error_close_dir; | ||
341 | } | ||
342 | |||
314 | if (ret == 1) | 343 | if (ret == 1) |
315 | (*counter)++; | 344 | (*counter)++; |
316 | fclose(sysfsfp); | 345 | if (fclose(sysfsfp)) { |
346 | ret = -errno; | ||
347 | free(filename); | ||
348 | goto error_close_dir; | ||
349 | } | ||
350 | |||
317 | free(filename); | 351 | free(filename); |
318 | } | 352 | } |
319 | *ci_array = malloc(sizeof(**ci_array) * (*counter)); | 353 | *ci_array = malloc(sizeof(**ci_array) * (*counter)); |
@@ -344,8 +378,20 @@ int build_channel_array(const char *device_dir, | |||
344 | count--; | 378 | count--; |
345 | goto error_cleanup_array; | 379 | goto error_cleanup_array; |
346 | } | 380 | } |
347 | fscanf(sysfsfp, "%i", ¤t_enabled); | 381 | errno = 0; |
348 | fclose(sysfsfp); | 382 | if (fscanf(sysfsfp, "%i", ¤t_enabled) != 1) { |
383 | ret = errno ? -errno : -ENODATA; | ||
384 | free(filename); | ||
385 | count--; | ||
386 | goto error_cleanup_array; | ||
387 | } | ||
388 | |||
389 | if (fclose(sysfsfp)) { | ||
390 | ret = -errno; | ||
391 | free(filename); | ||
392 | count--; | ||
393 | goto error_cleanup_array; | ||
394 | } | ||
349 | 395 | ||
350 | if (!current_enabled) { | 396 | if (!current_enabled) { |
351 | free(filename); | 397 | free(filename); |
@@ -383,8 +429,29 @@ int build_channel_array(const char *device_dir, | |||
383 | goto error_cleanup_array; | 429 | goto error_cleanup_array; |
384 | } | 430 | } |
385 | sysfsfp = fopen(filename, "r"); | 431 | sysfsfp = fopen(filename, "r"); |
386 | fscanf(sysfsfp, "%u", ¤t->index); | 432 | if (sysfsfp == NULL) { |
387 | fclose(sysfsfp); | 433 | ret = -errno; |
434 | printf("failed to open %s\n", filename); | ||
435 | free(filename); | ||
436 | goto error_cleanup_array; | ||
437 | } | ||
438 | |||
439 | errno = 0; | ||
440 | if (fscanf(sysfsfp, "%u", ¤t->index) != 1) { | ||
441 | ret = errno ? -errno : -ENODATA; | ||
442 | if (fclose(sysfsfp)) | ||
443 | perror("build_channel_array(): Failed to close file"); | ||
444 | |||
445 | free(filename); | ||
446 | goto error_cleanup_array; | ||
447 | } | ||
448 | |||
449 | if (fclose(sysfsfp)) { | ||
450 | ret = -errno; | ||
451 | free(filename); | ||
452 | goto error_cleanup_array; | ||
453 | } | ||
454 | |||
388 | free(filename); | 455 | free(filename); |
389 | /* Find the scale */ | 456 | /* Find the scale */ |
390 | ret = iioutils_get_param_float(¤t->scale, | 457 | ret = iioutils_get_param_float(¤t->scale, |
@@ -410,10 +477,16 @@ int build_channel_array(const char *device_dir, | |||
410 | device_dir, | 477 | device_dir, |
411 | current->name, | 478 | current->name, |
412 | current->generic_name); | 479 | current->generic_name); |
480 | if (ret < 0) | ||
481 | goto error_cleanup_array; | ||
413 | } | 482 | } |
414 | } | 483 | } |
415 | 484 | ||
416 | closedir(dp); | 485 | if (closedir(dp) == -1) { |
486 | ret = -errno; | ||
487 | goto error_cleanup_array; | ||
488 | } | ||
489 | |||
417 | free(scan_el_dir); | 490 | free(scan_el_dir); |
418 | /* reorder so that the array is in index order */ | 491 | /* reorder so that the array is in index order */ |
419 | bsort_channel_array_by_index(ci_array, *counter); | 492 | bsort_channel_array_by_index(ci_array, *counter); |
@@ -427,7 +500,10 @@ error_cleanup_array: | |||
427 | } | 500 | } |
428 | free(*ci_array); | 501 | free(*ci_array); |
429 | error_close_dir: | 502 | error_close_dir: |
430 | closedir(dp); | 503 | if (dp) |
504 | if (closedir(dp) == -1) | ||
505 | perror("build_channel_array(): Failed to close dir"); | ||
506 | |||
431 | error_free_name: | 507 | error_free_name: |
432 | free(scan_el_dir); | 508 | free(scan_el_dir); |
433 | error_ret: | 509 | error_ret: |
@@ -496,29 +572,45 @@ int find_type_by_name(const char *name, const char *type) | |||
496 | + numstrlen | 572 | + numstrlen |
497 | + 6); | 573 | + 6); |
498 | if (filename == NULL) { | 574 | if (filename == NULL) { |
499 | closedir(dp); | 575 | ret = -ENOMEM; |
500 | return -ENOMEM; | 576 | goto error_close_dir; |
577 | } | ||
578 | |||
579 | ret = sprintf(filename, "%s%s%d/name", iio_dir, | ||
580 | type, number); | ||
581 | if (ret < 0) { | ||
582 | free(filename); | ||
583 | goto error_close_dir; | ||
501 | } | 584 | } |
502 | sprintf(filename, "%s%s%d/name", | 585 | |
503 | iio_dir, | ||
504 | type, | ||
505 | number); | ||
506 | nameFile = fopen(filename, "r"); | 586 | nameFile = fopen(filename, "r"); |
507 | if (!nameFile) { | 587 | if (!nameFile) { |
508 | free(filename); | 588 | free(filename); |
509 | continue; | 589 | continue; |
510 | } | 590 | } |
511 | free(filename); | 591 | free(filename); |
512 | fscanf(nameFile, "%s", thisname); | 592 | errno = 0; |
513 | fclose(nameFile); | 593 | if (fscanf(nameFile, "%s", thisname) != 1) { |
594 | ret = errno ? -errno : -ENODATA; | ||
595 | goto error_close_dir; | ||
596 | } | ||
597 | |||
598 | if (fclose(nameFile)) { | ||
599 | ret = -errno; | ||
600 | goto error_close_dir; | ||
601 | } | ||
602 | |||
514 | if (strcmp(name, thisname) == 0) { | 603 | if (strcmp(name, thisname) == 0) { |
515 | closedir(dp); | 604 | if (closedir(dp) == -1) |
605 | return -errno; | ||
516 | return number; | 606 | return number; |
517 | } | 607 | } |
518 | } | 608 | } |
519 | } | 609 | } |
520 | } | 610 | } |
521 | closedir(dp); | 611 | if (closedir(dp) == -1) |
612 | return -errno; | ||
613 | |||
522 | return -ENODEV; | 614 | return -ENODEV; |
523 | 615 | ||
524 | error_close_dir: | 616 | error_close_dir: |
@@ -536,15 +628,29 @@ static int _write_sysfs_int(char *filename, char *basedir, int val, int verify) | |||
536 | 628 | ||
537 | if (temp == NULL) | 629 | if (temp == NULL) |
538 | return -ENOMEM; | 630 | return -ENOMEM; |
539 | sprintf(temp, "%s/%s", basedir, filename); | 631 | ret = sprintf(temp, "%s/%s", basedir, filename); |
632 | if (ret < 0) | ||
633 | goto error_free; | ||
634 | |||
540 | sysfsfp = fopen(temp, "w"); | 635 | sysfsfp = fopen(temp, "w"); |
541 | if (sysfsfp == NULL) { | 636 | if (sysfsfp == NULL) { |
542 | ret = -errno; | 637 | ret = -errno; |
543 | printf("failed to open %s\n", temp); | 638 | printf("failed to open %s\n", temp); |
544 | goto error_free; | 639 | goto error_free; |
545 | } | 640 | } |
546 | fprintf(sysfsfp, "%d", val); | 641 | ret = fprintf(sysfsfp, "%d", val); |
547 | fclose(sysfsfp); | 642 | if (ret < 0) { |
643 | if (fclose(sysfsfp)) | ||
644 | perror("_write_sysfs_int(): Failed to close dir"); | ||
645 | |||
646 | goto error_free; | ||
647 | } | ||
648 | |||
649 | if (fclose(sysfsfp)) { | ||
650 | ret = -errno; | ||
651 | goto error_free; | ||
652 | } | ||
653 | |||
548 | if (verify) { | 654 | if (verify) { |
549 | sysfsfp = fopen(temp, "r"); | 655 | sysfsfp = fopen(temp, "r"); |
550 | if (sysfsfp == NULL) { | 656 | if (sysfsfp == NULL) { |
@@ -552,8 +658,19 @@ static int _write_sysfs_int(char *filename, char *basedir, int val, int verify) | |||
552 | printf("failed to open %s\n", temp); | 658 | printf("failed to open %s\n", temp); |
553 | goto error_free; | 659 | goto error_free; |
554 | } | 660 | } |
555 | fscanf(sysfsfp, "%d", &test); | 661 | if (fscanf(sysfsfp, "%d", &test) != 1) { |
556 | fclose(sysfsfp); | 662 | ret = errno ? -errno : -ENODATA; |
663 | if (fclose(sysfsfp)) | ||
664 | perror("_write_sysfs_int(): Failed to close dir"); | ||
665 | |||
666 | goto error_free; | ||
667 | } | ||
668 | |||
669 | if (fclose(sysfsfp)) { | ||
670 | ret = -errno; | ||
671 | goto error_free; | ||
672 | } | ||
673 | |||
557 | if (test != val) { | 674 | if (test != val) { |
558 | printf("Possible failure in int write %d to %s%s\n", | 675 | printf("Possible failure in int write %d to %s%s\n", |
559 | val, | 676 | val, |
@@ -588,15 +705,29 @@ static int _write_sysfs_string(char *filename, char *basedir, char *val, | |||
588 | printf("Memory allocation failed\n"); | 705 | printf("Memory allocation failed\n"); |
589 | return -ENOMEM; | 706 | return -ENOMEM; |
590 | } | 707 | } |
591 | sprintf(temp, "%s/%s", basedir, filename); | 708 | ret = sprintf(temp, "%s/%s", basedir, filename); |
709 | if (ret < 0) | ||
710 | goto error_free; | ||
711 | |||
592 | sysfsfp = fopen(temp, "w"); | 712 | sysfsfp = fopen(temp, "w"); |
593 | if (sysfsfp == NULL) { | 713 | if (sysfsfp == NULL) { |
594 | ret = -errno; | 714 | ret = -errno; |
595 | printf("Could not open %s\n", temp); | 715 | printf("Could not open %s\n", temp); |
596 | goto error_free; | 716 | goto error_free; |
597 | } | 717 | } |
598 | fprintf(sysfsfp, "%s", val); | 718 | ret = fprintf(sysfsfp, "%s", val); |
599 | fclose(sysfsfp); | 719 | if (ret < 0) { |
720 | if (fclose(sysfsfp)) | ||
721 | perror("_write_sysfs_string(): Failed to close dir"); | ||
722 | |||
723 | goto error_free; | ||
724 | } | ||
725 | |||
726 | if (fclose(sysfsfp)) { | ||
727 | ret = -errno; | ||
728 | goto error_free; | ||
729 | } | ||
730 | |||
600 | if (verify) { | 731 | if (verify) { |
601 | sysfsfp = fopen(temp, "r"); | 732 | sysfsfp = fopen(temp, "r"); |
602 | if (sysfsfp == NULL) { | 733 | if (sysfsfp == NULL) { |
@@ -604,8 +735,19 @@ static int _write_sysfs_string(char *filename, char *basedir, char *val, | |||
604 | printf("could not open file to verify\n"); | 735 | printf("could not open file to verify\n"); |
605 | goto error_free; | 736 | goto error_free; |
606 | } | 737 | } |
607 | fscanf(sysfsfp, "%s", temp); | 738 | if (fscanf(sysfsfp, "%s", temp) != 1) { |
608 | fclose(sysfsfp); | 739 | ret = errno ? -errno : -ENODATA; |
740 | if (fclose(sysfsfp)) | ||
741 | perror("_write_sysfs_string(): Failed to close dir"); | ||
742 | |||
743 | goto error_free; | ||
744 | } | ||
745 | |||
746 | if (fclose(sysfsfp)) { | ||
747 | ret = -errno; | ||
748 | goto error_free; | ||
749 | } | ||
750 | |||
609 | if (strcmp(temp, val) != 0) { | 751 | if (strcmp(temp, val) != 0) { |
610 | printf("Possible failure in string write of %s " | 752 | printf("Possible failure in string write of %s " |
611 | "Should be %s " | 753 | "Should be %s " |
@@ -649,14 +791,27 @@ int read_sysfs_posint(char *filename, char *basedir) | |||
649 | printf("Memory allocation failed"); | 791 | printf("Memory allocation failed"); |
650 | return -ENOMEM; | 792 | return -ENOMEM; |
651 | } | 793 | } |
652 | sprintf(temp, "%s/%s", basedir, filename); | 794 | ret = sprintf(temp, "%s/%s", basedir, filename); |
795 | if (ret < 0) | ||
796 | goto error_free; | ||
797 | |||
653 | sysfsfp = fopen(temp, "r"); | 798 | sysfsfp = fopen(temp, "r"); |
654 | if (sysfsfp == NULL) { | 799 | if (sysfsfp == NULL) { |
655 | ret = -errno; | 800 | ret = -errno; |
656 | goto error_free; | 801 | goto error_free; |
657 | } | 802 | } |
658 | fscanf(sysfsfp, "%d\n", &ret); | 803 | errno = 0; |
659 | fclose(sysfsfp); | 804 | if (fscanf(sysfsfp, "%d\n", &ret) != 1) { |
805 | ret = errno ? -errno : -ENODATA; | ||
806 | if (fclose(sysfsfp)) | ||
807 | perror("read_sysfs_posint(): Failed to close dir"); | ||
808 | |||
809 | goto error_free; | ||
810 | } | ||
811 | |||
812 | if (fclose(sysfsfp)) | ||
813 | ret = -errno; | ||
814 | |||
660 | error_free: | 815 | error_free: |
661 | free(temp); | 816 | free(temp); |
662 | return ret; | 817 | return ret; |
@@ -672,14 +827,27 @@ int read_sysfs_float(char *filename, char *basedir, float *val) | |||
672 | printf("Memory allocation failed"); | 827 | printf("Memory allocation failed"); |
673 | return -ENOMEM; | 828 | return -ENOMEM; |
674 | } | 829 | } |
675 | sprintf(temp, "%s/%s", basedir, filename); | 830 | ret = sprintf(temp, "%s/%s", basedir, filename); |
831 | if (ret < 0) | ||
832 | goto error_free; | ||
833 | |||
676 | sysfsfp = fopen(temp, "r"); | 834 | sysfsfp = fopen(temp, "r"); |
677 | if (sysfsfp == NULL) { | 835 | if (sysfsfp == NULL) { |
678 | ret = -errno; | 836 | ret = -errno; |
679 | goto error_free; | 837 | goto error_free; |
680 | } | 838 | } |
681 | fscanf(sysfsfp, "%f\n", val); | 839 | errno = 0; |
682 | fclose(sysfsfp); | 840 | if (fscanf(sysfsfp, "%f\n", val) != 1) { |
841 | ret = errno ? -errno : -ENODATA; | ||
842 | if (fclose(sysfsfp)) | ||
843 | perror("read_sysfs_float(): Failed to close dir"); | ||
844 | |||
845 | goto error_free; | ||
846 | } | ||
847 | |||
848 | if (fclose(sysfsfp)) | ||
849 | ret = -errno; | ||
850 | |||
683 | error_free: | 851 | error_free: |
684 | free(temp); | 852 | free(temp); |
685 | return ret; | 853 | return ret; |
@@ -695,14 +863,27 @@ int read_sysfs_string(const char *filename, const char *basedir, char *str) | |||
695 | printf("Memory allocation failed"); | 863 | printf("Memory allocation failed"); |
696 | return -ENOMEM; | 864 | return -ENOMEM; |
697 | } | 865 | } |
698 | sprintf(temp, "%s/%s", basedir, filename); | 866 | ret = sprintf(temp, "%s/%s", basedir, filename); |
867 | if (ret < 0) | ||
868 | goto error_free; | ||
869 | |||
699 | sysfsfp = fopen(temp, "r"); | 870 | sysfsfp = fopen(temp, "r"); |
700 | if (sysfsfp == NULL) { | 871 | if (sysfsfp == NULL) { |
701 | ret = -errno; | 872 | ret = -errno; |
702 | goto error_free; | 873 | goto error_free; |
703 | } | 874 | } |
704 | fscanf(sysfsfp, "%s\n", str); | 875 | errno = 0; |
705 | fclose(sysfsfp); | 876 | if (fscanf(sysfsfp, "%s\n", str) != 1) { |
877 | ret = errno ? -errno : -ENODATA; | ||
878 | if (fclose(sysfsfp)) | ||
879 | perror("read_sysfs_string(): Failed to close dir"); | ||
880 | |||
881 | goto error_free; | ||
882 | } | ||
883 | |||
884 | if (fclose(sysfsfp)) | ||
885 | ret = -errno; | ||
886 | |||
706 | error_free: | 887 | error_free: |
707 | free(temp); | 888 | free(temp); |
708 | return ret; | 889 | return ret; |