From 2b6a6e67af6f8f644a48f75efc1f44544c0d74f6 Mon Sep 17 00:00:00 2001 From: Hartmut Knaack Date: Sun, 31 May 2015 14:39:48 +0200 Subject: tools:iio: save errno first The man-page of errno states, that errno should be saved before doing any library call, as that call may have changed the value of errno. So, when encountering any error, save errno first. This patch affects generic_buffer.c, iio_event_monitor.c and iio_utils.c. Signed-off-by: Hartmut Knaack Signed-off-by: Jonathan Cameron --- tools/iio/iio_event_monitor.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'tools/iio/iio_event_monitor.c') diff --git a/tools/iio/iio_event_monitor.c b/tools/iio/iio_event_monitor.c index 427c271ac0d6..f6fdab48d340 100644 --- a/tools/iio/iio_event_monitor.c +++ b/tools/iio/iio_event_monitor.c @@ -269,8 +269,8 @@ int main(int argc, char **argv) fd = open(chrdev_name, 0); if (fd == -1) { - fprintf(stdout, "Failed to open %s\n", chrdev_name); ret = -errno; + fprintf(stdout, "Failed to open %s\n", chrdev_name); goto error_free_chrdev_name; } @@ -279,8 +279,8 @@ int main(int argc, char **argv) close(fd); if (ret == -1 || event_fd == -1) { - fprintf(stdout, "Failed to retrieve event fd\n"); ret = -errno; + fprintf(stdout, "Failed to retrieve event fd\n"); goto error_free_chrdev_name; } @@ -291,8 +291,8 @@ int main(int argc, char **argv) printf("nothing available\n"); continue; } else { - perror("Failed to read event from device"); ret = -errno; + perror("Failed to read event from device"); break; } } -- cgit v1.2.3 From 5fdb8c6127c3c594360fb5f2959a724809c0032e Mon Sep 17 00:00:00 2001 From: Hartmut Knaack Date: Sun, 31 May 2015 14:39:49 +0200 Subject: tools:iio:iio_event_monitor: save right errno Move up error handling code to preserve the errno coming from ioctl(), before it may be changed by close(). Signed-off-by: Hartmut Knaack Signed-off-by: Jonathan Cameron --- tools/iio/iio_event_monitor.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'tools/iio/iio_event_monitor.c') diff --git a/tools/iio/iio_event_monitor.c b/tools/iio/iio_event_monitor.c index f6fdab48d340..578390db1bec 100644 --- a/tools/iio/iio_event_monitor.c +++ b/tools/iio/iio_event_monitor.c @@ -275,15 +275,17 @@ int main(int argc, char **argv) } ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd); - - close(fd); - if (ret == -1 || event_fd == -1) { ret = -errno; fprintf(stdout, "Failed to retrieve event fd\n"); + if (close(fd) == -1) + perror("Failed to close character device file"); + goto error_free_chrdev_name; } + close(fd); + while (true) { ret = read(event_fd, &event, sizeof(event)); if (ret == -1) { -- cgit v1.2.3 From e9e45b43b8f06273d9b78f187042dff0bf5be0a5 Mon Sep 17 00:00:00 2001 From: Hartmut Knaack Date: Sun, 31 May 2015 14:40:02 +0200 Subject: tools:iio: catch errors in string allocation This patch catches errors in string allocation in generic_buffer.c, iio_event_monitor.c, iio_utils.c and lsiio.c. Signed-off-by: Hartmut Knaack Signed-off-by: Jonathan Cameron --- tools/iio/iio_event_monitor.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'tools/iio/iio_event_monitor.c') diff --git a/tools/iio/iio_event_monitor.c b/tools/iio/iio_event_monitor.c index 578390db1bec..1374374208ee 100644 --- a/tools/iio/iio_event_monitor.c +++ b/tools/iio/iio_event_monitor.c @@ -265,6 +265,8 @@ int main(int argc, char **argv) /* If we can't find a IIO device by name assume device_name is a IIO chrdev */ chrdev_name = strdup(device_name); + if (!chrdev_name) + return -ENOMEM; } fd = open(chrdev_name, 0); -- cgit v1.2.3 From 963f54cef23b7e8c91bbe60b978b5f4a3e990f2c Mon Sep 17 00:00:00 2001 From: Hartmut Knaack Date: Sun, 31 May 2015 14:40:05 +0200 Subject: tools:iio:iio_event_monitor: add error handling Add error handling to calls which can indicate a major problem by returning an error code. Signed-off-by: Hartmut Knaack Signed-off-by: Jonathan Cameron --- tools/iio/iio_event_monitor.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'tools/iio/iio_event_monitor.c') diff --git a/tools/iio/iio_event_monitor.c b/tools/iio/iio_event_monitor.c index 1374374208ee..1316527f3c3c 100644 --- a/tools/iio/iio_event_monitor.c +++ b/tools/iio/iio_event_monitor.c @@ -286,7 +286,10 @@ int main(int argc, char **argv) goto error_free_chrdev_name; } - close(fd); + if (close(fd) == -1) { + ret = -errno; + goto error_free_chrdev_name; + } while (true) { ret = read(event_fd, &event, sizeof(event)); @@ -304,7 +307,9 @@ int main(int argc, char **argv) print_event(&event); } - close(event_fd); + if (close(event_fd) == -1) + perror("Failed to close event file"); + error_free_chrdev_name: free(chrdev_name); error_ret: -- cgit v1.2.3 From 0e799878175aa7d08f5882b6a391de4724c52e9e Mon Sep 17 00:00:00 2001 From: Hartmut Knaack Date: Sun, 31 May 2015 14:40:17 +0200 Subject: tools:iio: return values directly Return directly, if no common cleanup is required. Signed-off-by: Hartmut Knaack Signed-off-by: Jonathan Cameron --- tools/iio/iio_event_monitor.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'tools/iio/iio_event_monitor.c') diff --git a/tools/iio/iio_event_monitor.c b/tools/iio/iio_event_monitor.c index 1316527f3c3c..7f56238b1d66 100644 --- a/tools/iio/iio_event_monitor.c +++ b/tools/iio/iio_event_monitor.c @@ -258,8 +258,7 @@ int main(int argc, char **argv) device_name, dev_num); ret = asprintf(&chrdev_name, "/dev/iio:device%d", dev_num); if (ret < 0) { - ret = -ENOMEM; - goto error_ret; + return -ENOMEM; } } else { /* If we can't find a IIO device by name assume device_name is a @@ -312,6 +311,6 @@ int main(int argc, char **argv) error_free_chrdev_name: free(chrdev_name); -error_ret: + return ret; } -- cgit v1.2.3 From 916e89e4b7272e1eda9eba057cf25197129bcf79 Mon Sep 17 00:00:00 2001 From: Hartmut Knaack Date: Sun, 31 May 2015 14:40:18 +0200 Subject: tools:iio:iio_event_monitor: refactor events output Refactor the code in print_event() to reduce code duplication and better reflect that the type is output unconditionally, as well as cascade the dependency of the diff-channel. Saves a few lines of code, as well. Signed-off-by: Hartmut Knaack Signed-off-by: Jonathan Cameron --- tools/iio/iio_event_monitor.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) (limited to 'tools/iio/iio_event_monitor.c') diff --git a/tools/iio/iio_event_monitor.c b/tools/iio/iio_event_monitor.c index 7f56238b1d66..016760e769c0 100644 --- a/tools/iio/iio_event_monitor.c +++ b/tools/iio/iio_event_monitor.c @@ -213,23 +213,19 @@ static void print_event(struct iio_event_data *event) return; } - printf("Event: time: %lld, ", event->timestamp); + printf("Event: time: %lld, type: %s", event->timestamp, + iio_chan_type_name_spec[type]); - if (mod != IIO_NO_MOD) { - printf("type: %s(%s), ", - iio_chan_type_name_spec[type], - iio_modifier_names[mod]); - } else { - printf("type: %s, ", - iio_chan_type_name_spec[type]); - } + if (mod != IIO_NO_MOD) + printf("(%s)", iio_modifier_names[mod]); - if (diff && chan >= 0 && chan2 >= 0) - printf("channel: %d-%d, ", chan, chan2); - else if (chan >= 0) - printf("channel: %d, ", chan); + if (chan >= 0) { + printf(", channel: %d", chan); + if (diff && chan2 >= 0) + printf("-%d", chan2); + } - printf("evtype: %s", iio_ev_type_text[ev_type]); + printf(", evtype: %s", iio_ev_type_text[ev_type]); if (dir != IIO_EV_DIR_NONE) printf(", direction: %s", iio_ev_dir_text[dir]); -- cgit v1.2.3