The interactive 'sleep' command stored the result of parse_period() in
an int. parse_period() returns a long long number of microseconds, so a
period above INT_MAX microseconds (~36 minutes, e.g. "sleep 40m")
overflowed: it could wrap to a negative value and be wrongly rejected as
an invalid period, or to an incorrect positive value producing the wrong
sleep duration.
Declare period_us as long long to match parse_period()'s return type.
The error check (period_us < 0) and the sleep_us() call, which takes an
unsigned long long, both remain correct. The non-interactive hold and
toggle period paths already use unsigned long long and were unaffected.
Add a test that issues "sleep 40m" at the interactive prompt and verifies
the tool starts sleeping rather than emitting an "invalid period" error
Fixes: c34a572c53 ("tools: add minutes as a new supported time unit")
Assisted-by: Claude Opus 4.8
Link: https://patch.msgid.link/20260608-more-pre-2-3-fixes-v1-5-577a5ba426a5@oss.qualcomm.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
parse_period() consumed an optional unit suffix ('us', 'ms', 's', 'm')
but never verified that the suffix was the end of the string. As a
result inputs with trailing garbage such as "100msx", "100usx" or
"100sx" were silently accepted and parsed as if the garbage were not
there, while inconsistently "100mx" was already rejected.
Require that the whole string be consumed by checking *end == '\0'
after the unit suffix has been processed, mirroring what parse_uint()
already does. All valid forms still leave end pointing at the
terminating NUL at that point, so only malformed input is rejected.
Add a test that passes an invalid suffix and verifies it's rejected.
Fixes: 6702eed1a5 ("tools: fix an integer overflow bug in parse_period()")
Assisted-by: Claude Opus 4.8
Link: https://patch.msgid.link/20260608-more-pre-2-3-fixes-v1-4-577a5ba426a5@oss.qualcomm.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Commit 0de77e2abf ("tools: gpionotify: don't leak info returned by
gpiod_chip_watch_line_info()") moved the call to
gpiod_chip_watch_line_info() out of the condition that guarded it. The
original code relied on the short-circuit evaluation of the logical AND
to only request a watch when the line actually belonged to the chip
currently being processed:
if ((resolver->lines[j].chip_num == i) &&
!gpiod_chip_watch_line_info(chip, resolver->lines[j].offset))
die_perror(...);
After the refactor the watch is requested unconditionally for every
resolved line on every chip, using the line's offset as-is. When lines
are spread across multiple chips this installs watches at foreign
offsets on the wrong chip. Such a watch typically succeeds (the offset
usually exists on the other chip too), persists - freeing the returned
gpiod_line_info does not remove it - and makes gpionotify report
spurious info events for lines the user never asked to watch.
Restore the guard so that each line is only ever watched on the chip it
belongs to, while keeping the gpiod_line_info_free() leak fix. As a side
effect a watch failure on the correct chip is now always treated as
fatal instead of only when it coincided with a matching chip_num.
Add a test-case that requests two lines with distinct offsets on two
different chips and then toggles one line's offset on the other chip. No
event must be reported for that, as the offset is not watched there.
Fixes: 0de77e2abf ("tools: gpionotify: don't leak info returned by gpiod_chip_watch_line_info()")
Assisted-by: Claude Opus 4.8
Link: https://patch.msgid.link/20260608-more-pre-2-3-fixes-v1-3-577a5ba426a5@oss.qualcomm.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Every other tool (gpioinfo, gpiomon, gpioset …) uses the value returned
by parse_config() to advance argc/argv past the parsed options:
i = parse_config(argc, argv, &cfg);
argc -= i;
argv += i;
gpionotify was directly using the global optind variable instead of i,
which is equivalent today because parse_config() returns optind, but is
inconsistent and would silently break if the function were ever changed
to return something else.
Assisted-by: Claude Sonnet 4.6
Acked-by: Vincent Fazio <vfazio@xes-inc.com>
Reviewed-by: Vincent Fazio <vfazio@xes-inc.com>
Tested-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://patch.msgid.link/20260604-pre-2-3-fixes-v2-4-781bcaf47475@oss.qualcomm.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Reuse as much of the functionality provided by the new libgpiotools
shared library in tools-common while retaining the functions used to
validate command-line arguments, print help text or bail-out on errors.
We keep the existing interfaces to gpio-tools and use them to wrap the
API exposed by libgpiotools as we want to keep bailing out on errors
without requiring tools to do a lot of repeated error checking.
Acked-by: Kent Gibson <warthog618@gmail.com>
Link: https://patch.msgid.link/20260513-tools-common-shared-lib-v1-2-58ac28a9f1d5@oss.qualcomm.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Instead of repeating the 'die("out of memory");' statement, provide a
variant of die() that emits this message and use it on OOM failures in
all gpio-tools.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Port the project from autotools to meson. All build options are
preserved with the same semantics.
The goal of this change is to produce the same set of files as the
current autotools approach: libraries with correct SONAME/version,
headers, pkg-config files, GObject introspection data, Python extension
modules, D-Bus interface and configuration files. This is done to enable
a one-liner test command running all available test suites.
The only thing we drop is the `make docs` target as it's rarely used and
docs can be generated easily with sphinx directly. They are also not
required for testing.
The Rust workspace is built via a custom target that invokes cargo and
uses SYSTEM_DEPS_* environment variables to point the libgpiod-sys and
gpiosim-sys crates at the locally built C libraries.
In addition to porting the build files to meson, update the docs, remove
unneded items from .gitignore files (or remove them entirely if they're
no longer needed) and update the shunit2 test scripts to no longer care
about source directories.
Tested-by: Erik Wierich <erik@riscstar.com>
Tested-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://patch.msgid.link/20260428-meson-build-v3-1-8da6976fb0a2@oss.qualcomm.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
In coreutils v9.10 (specifically with commit 8c2461933411 ("timeout:
honor ignored signal dispositions")) the behavior of timeout changed and
it will no longer propagate SIGINT or SIGQUIT in shell background jobs.
This breaks the test cases checking the behavior of tools after SIGINT.
We have to assume that if exit after SIGTERM works, then so does it
after SIGINT and remove the failing tests.
Link: https://patch.msgid.link/20260318131413.56575-1-bartosz.golaszewski@oss.qualcomm.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Consider the following output of `gpioinfo`:
gpiochip1 - 2 lines:
line 0: "foo" output
line 1: unnamed output
Now let's run:
gpioset --chip=gpiochip1 0=active 1=active
The output of `gpioinfo --unquoted` is correct:
gpiochip1 - 2 lines:
line 0: foo output consumer=gpioset
line 1: unnamed output consumer=gpioset
However, without the `unquoted` switch, it's inconsistent:
gpiochip1 - 2 lines:
line 0: "foo" output consumer="gpioset"
line 1: unnamed output consumer=gpioset
This is because gpioinfo drops the quotes for all subsequent attribute
strings when it encounters an unnamed line. We should instead keep a
separate instance of the `unquoted` flag just for the line name and pass
the original value of `unquoted_strings` down to
print_line_attributes().
Fixes: 8ffb648928 ("tools: line name focussed rework")
Reviewed-by: Kent Gibson <warthog618@gmail.com>
Link: https://lore.kernel.org/r/20250321-gpioinfo-unnamed-quotes-v1-1-a806e9027adf@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Since kernel commit 840a97e2fbaf ("gpio: sim: delimit the fwnode name
with a ":" when generating labels") the gpio-sim automatic labels are
generated by delimiting the device name and the fwnode name with ':'
instead of '-' for better readability. This will break the tests once
linux v6.9 is out. Act in advance and accept both forms.
Reviewed-by: Kent Gibson <warthog618@gmail.com>
Link: https://lore.kernel.org/r/20240506162506.176935-1-brgl@bgdev.pl
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
We currently store time as microseconds in 32-bit integers and allow
seconds as the longest time unit when parsing command-line arguments
limiting the time period possible to specify when passing arguments such
as --hold-period to 35 minutes. Let's use 64-bit integers to vastly
increase that.
Use nanosleep() instead of usleep() to extend the possible sleep time
range.
Reported-by: Gunnar Thörnqvist <gunnar@igl.se>
Link: https://lore.kernel.org/r/20240423100452.32958-4-brgl@bgdev.pl
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
The note regarding the state of a line after gpioset exits is confusing
and unhelpful to the average reader, if not outright incorrect.
A common mis-interpretation is that this behaviour is arbitrarily chosen
by spiteful implementors. The note also specifies that the line reverts
to default, but that is not always the case, or is at least out of the
control of gpioset or libgpiod.
Reword the note to constrain the scope to that relevant to the likely
reader, and to emphasize that the behaviour is inherent in the kernel
GPIO interface, not in the gpioset implementation.
Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Apparently bash scripts need to be listed in the dist_noinst_SCRIPTS in
order to be picked up into the dist package, noinst_SCRIPTS is not
enough.
Fixes: 555f5e4cfe ("tools: tests: port tests to shunit2")
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Add shunit assert messages when testing the result of regex matches,
otherwise we're left with a not very useful: "expected:<0> but was:<1>".
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
For any given target (let's say foobar), automake defaults to looking for
foobar.c if foobar_SOURCES are not specified. Remove redundant assignments
as we've seen multiple hidden typos in makefiles already.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Reviewed-by: Kent Gibson <warthog618@gmail.com>
BATS has been confirmed to run much more slowly that shunit2. This is
most likely caused by the way BATS evaluates each test file n+1 times
where n is the number of tests[1].
Port tests to using shunit2 which executes as a regular shell script
which, in addition to higher speed, allows for easier debugging as
standard shell flags like -x now work.
[1] https://bats-core.readthedocs.io/en/stable/writing-tests.html
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Reviewed-by: Kent Gibson <warthog618@gmail.com>
Toggling the gpio-sim pull too fast can lead to losing events by gpiomon.
Add short sleeps between the calls to gpiosim_set_pull.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
bats has an implicit depencency on ncurses, as I found when trying to
run the tests on a minimal install that lacked ncurses. Rather than make
the dependency explicit, force the output formatting to use the TAP
format which does not require ncurses.
Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
The continuous toggle test was recently changed to poll the line to
check for toggles, and so increase test reliability. Tighten up the
test timings so the test can now also run in a significantly shorter
time.
And, as it is now faster, add an extra edge just to be sure.
Note that the test does not need to catch every edge, it only has to
check that the line is in fact toggling.
Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>