Skip to contentSkip to conversation

tools: iio: iio_generic_buffer: fix exit status

PATCH In review Marco Chen 3 participants 6 messages Nov 27 – Nov 29 1 message not in the archive IIO

lore
Share & tools
lore

Marco Chen

author
#0 lore raw

cover tools: iio: iio_generic_buffer: fix exit status

This series fixes a wrong exit status in two cases for iio_generic_buffer. These issues were found during the process of using iio_generic_buffer in a bash shell script while testing buffered reads with the max30102 pulse oximeter.

All patches were tested on a Raspberry Pi 4 running the IIO subsystem tree testing branch with the max30102.

For patch 1, a normal buffered capture exited with 1 before and 0 after, with and without the -e flag.

For patch 2, running iio_generic_buffer as a non-root user exited with 0 before and 254 (-ENOENT) after.

Marco Chen (2):
tools: iio: iio_generic_buffer: return 0 on success
tools: iio: iio_generic_buffer: return error if no channels can be
enabled

 tools/iio/iio_generic_buffer.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

base-commit: 39508411b82d4dc29dd245bb471ec9ecc1111c9d

Signature · 1 line
2.56.1
author
#1 lore raw

PATCH 1/2 tools: iio: iio_generic_buffer: return 0 on success

A successful buffered capture exits with status 1. As a result, shell scripts treat this as a failure. This happens because ret still holds the return value of poll(), which is the number of ready file descriptors, or in the case with the -e flag set, the return value of write_sysfs_int().

Set ret to 0 after the capture loop and before the error label. The read() error path currently breaks out of the loop and would fall through to that line, so make it set ret to -errno and jump to the error label instead, just like the poll() error path already does.

Fixes 6bb7cac8551e ("tools:iio:generic_buffer: add error handling")

---
The read() error path was found by inspection, but I did not actually run or test this error path explicitly.
Thank you.

 tools/iio/iio_generic_buffer.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/iio/iio_generic_buffer.c +3 -1
index fd955cf444ad..122fa54bb74a 100644
@@ -807,7 +807,8 @@ int main(int argc, char **argv)
 				fprintf(stderr, "nothing available\n");
 				continue;
 			} else {
-				break;
+				ret = -errno;
+				goto error;
 			}
 		}
 		for (i = 0; i < read_size / scan_size; i++)
@@ -815,6 +816,7 @@ int main(int argc, char **argv)
 				     num_channels);
 	}
 
+	ret = 0;
 error:
 	cleanup();
 
Signature · 1 line
2.56.1
author
#2 lore raw

PATCH 2/2 tools: iio: iio_generic_buffer: return error if no channels can be enabled

In reply to Marco Chen

If no channels end up being enabled when using the -a flag, iio_generic_buffer prints "Still no channels after auto-enabling, giving up". However, it exits with status 0 because ret still holds 0 from the preceding build_channel_array() call, so callers see success even though no data was captured. This happens, for example, when iio_generic_buffer runs without permission to write to the scan element enable files.

Set ret=-ENOENT before jumping to the error label. This fix matches the "No channels are enabled" error directly below, which already returns -ENOENT.

Fixes 5f991a921a08 ("iio: tools: generic_buffer: auto-enable channels")

---
I used -ENOENT to match the existing "No channels are enabled" path, but I am not 100% sure if this is the right error code to return here. I would be glad to hear your input on this.
Thank you.

 tools/iio/iio_generic_buffer.c | 1 +
 1 file changed, 1 insertion(+)
tools/iio/iio_generic_buffer.c +1 -0
index 122fa54bb74a..974548150eb1 100644
@@ -643,6 +643,7 @@ int main(int argc, char **argv)
 		if (!num_channels) {
 			fprintf(stderr, "Still no channels after "
 				"auto-enabling, giving up\n");
+			ret = -ENOENT;
 			goto error;
 		}
 	}
Signature · 1 line
2.56.1
Reviewed-by on the v1 series
#3 lore raw
In reply to Marco Chen
Quoted 2 lines · Marco Chen

This series fixes a wrong exit status in two cases for iio_generic_buffer.

Both look right to me. For 1/2, the exit status of 1 with -e comes from write_sysfs_int("enable", ..., 1): without verify it returns the fprintf() count, which is 1 for "1". Without -e it is poll()'s return value, so resetting ret before the error label covers both, and the read() error path now matches the poll() one.

For 2/2, -ENOENT is fine, it matches the "No channels are enabled" path right below.

The reason you get that far as non-root, though, is that enable_disable_all_channels() ignores failed writes: it stores the write_sysfs_int() result in ret, only prints a message, and always returns 0 (the ret = -ENOENT at the top is never used either). So the "Failed to enable all channels" check in main() can't fire for a write failure. Returning the error from there, as a separate patch, would make the non-root case fail earlier with -EACCES. Your 2/2 is still useful as a safety net.

Both apply to iio testing, build cleanly and are checkpatch --strict clean.

For the series:

A message that is not in the archive (sent off-list or not yet received): CAAZOf24WuvdmyvpAhZsxafTiscSnjvW8Rt0fZjK9QHkfK8PN9Q@mail.gmail.com

author
#5 lore raw
Quoted 12 lines · David Kahurani
Quoted 9 lines · On Fri, Nov 27, 2026, 14:39 Arnav Kapoor <[email protected]> wrote:

The reason you get that far as non-root, though, is that enable_disable_all_channels() ignores failed writes: it stores the write_sysfs_int() result in ret, only prints a message, and always returns 0 (the ret = -ENOENT at the top is never used either). So the "Failed to enable all channels" check in main() can't fire for a write failure. Returning the error from there, as a separate patch, would make the non-root case fail earlier with -EACCES. Your 2/2 is still useful as a safety net.

Quoted 3 lines

Not sure about this... But, in a write and possibly read attempt a return code of zero means no operation was performed. That end of file(EOF), zero bytes written, zero bytes read etc

Thank you Arnav for the review, and thank you David for bringing up your point on the return code of zero.

For this series though, I did not change how write_sysfs_int() reports its return value. The value this series changes is only the process's exit status, where 0 indicates success. That is a different convention from the number of bytes written from the return code of a read or write attempt.

Returning the write error from enable_disable_all_channels(), as Arnav suggested, would be a good solution to make the non-root case fail earlier with -EACCES. But, as Arnav said, I think that would belong in a separate patch.

Thank you both, again.

Marco

maintainer
#6 lore raw
Quoted 26 lines · On Sat, 28 Nov 2026 13:44:37 -0500 Marco Chen <[email protected]> wrote:
Quoted 12 lines · David Kahurani
Quoted 9 lines · On Fri, Nov 27, 2026, 14:39 Arnav Kapoor <[email protected]> wrote:

The reason you get that far as non-root, though, is that enable_disable_all_channels() ignores failed writes: it stores the write_sysfs_int() result in ret, only prints a message, and always returns 0 (the ret = -ENOENT at the top is never used either). So the "Failed to enable all channels" check in main() can't fire for a write failure. Returning the error from there, as a separate patch, would make the non-root case fail earlier with -EACCES. Your 2/2 is still useful as a safety net.

Quoted 3 lines

Not sure about this... But, in a write and possibly read attempt a return code of zero means no operation was performed. That end of file(EOF), zero bytes written, zero bytes read etc

Thank you Arnav for the review, and thank you David for bringing up your point on the return code of zero.

For this series though, I did not change how write_sysfs_int() reports its return value. The value this series changes is only the process's exit status, where 0 indicates success. That is a different convention from the number of bytes written from the return code of a read or write attempt.

Quoted 7 lines

Returning the write error from enable_disable_all_channels(), as Arnav suggested, would be a good solution to make the non-root case fail earlier with -EACCES. But, as Arnav said, I think that would belong in a separate patch.

Thank you both, again.

Given this is the testing / reference tool and we are fairly late in the cycle I'm not going to rush this in. It has also only been out for a few days but given the deluge of patches at the moment I'm keen to resolve as many as possible quickly.

As such applied to the testing branch of iio.git.

Thanks,

Jonathan

Quoted 2 lines

Marco