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>
gpiod_edge_event_buffer_read_fd() is documented to read at most
max_events events and to return the number of events read. When called
with max_events == 0 it nonetheless issued a read() for 0 bytes, which
returns 0, and the subsequent short-read check
((unsigned int)rd < sizeof(*event_data)) then treated this as an I/O
error, setting errno to EIO and returning -1.
Requesting zero events is not an error - it is a no-op that reads
nothing. Handle it explicitly by clearing num_events and returning 0
before attempting the read.
Add a test case verifying the behavior.
Fixes: b7ba732e6a ("treewide: libgpiod v2 implementation")
Assisted-by: Claude Opus 4.8
Link: https://patch.msgid.link/20260608-more-pre-2-3-fixes-v1-2-577a5ba426a5@oss.qualcomm.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
When releasing a no-longer-referenced settings node in
gpiod_line_config_add_line_settings(), the embedded settings object was
freed with a raw free() instead of gpiod_line_settings_free(), unlike
every other release site (e.g. free_refs()).
This is currently harmless because gpiod_line_settings_free() is just a
free(), but bypassing the type's destructor is a latent leak should the
struct ever gain heap-allocated members. Use the proper destructor for
consistency.
Fixes: 2560a857ce ("core: keep memory usage of struct line_config in check")
Assisted-by: Claude Opus 4.8
Link: https://patch.msgid.link/20260608-more-pre-2-3-fixes-v1-1-577a5ba426a5@oss.qualcomm.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
The Event struct wraps a single *mut gpiod_edge_event pointer and is cast
from raw pointers in event_buffer.rs. Without #[repr(transparent)], the
layout compatibility is not guaranteed by the language specification.
Add #[repr(transparent)] to make the pointer cast sound, and replace
try_into().unwrap() with a simple 'as' cast for the sequence number
getters since c_ulong and usize are the same width on Linux.
Fixes: 91f9373c65 ("bindings: rust: Add libgpiod crate")
Assisted-by: Claude Opus 4.6
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-11-781bcaf47475@oss.qualcomm.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
chip::Info already implements PartialEq and PartialOrd (both based on
comparing chip names as UTF-8 strings), but not their total-order
counterparts Eq and Ord. The omission is inconsistent: types with a
total order should implement Ord so callers can use the ergonomic sort()
/ min() / max() APIs.
Add the missing impls. Ord::cmp() delegates to partial_cmp() and falls
back to Ordering::Equal for the unreachable case where a chip name is
not valid UTF-8. Update gpiochip_devices() to use cmp() directly now
that Ord is available.
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-10-781bcaf47475@oss.qualcomm.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
The direction() method correctly uses get_mapped_value() which converts
std::out_of_range to the project's bad_mapping exception, but bias(),
drive(), edge_detection(), and event_clock() call .at() directly. This
causes them to throw std::out_of_range instead of the expected
bad_mapping exception on unexpected values.
Use get_mapped_value() consistently for all enum mapping lookups.
Fixes: 78020eae9d ("bindings: cxx: remove duplicate mapping helper")
Assisted-by: Claude Opus 4.6
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-8-781bcaf47475@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>
Both gpiod_chip_info_from_uapi() and gpiod_line_info_from_uapi() copy
kernel UAPI name strings with strncpy() into buffers that are one byte
larger than GPIO_MAX_NAME_SIZE. If the source string is exactly
GPIO_MAX_NAME_SIZE bytes long strncpy() will not append a NUL, so the
strings are only guaranteed to be terminated because the structs are
zeroed with memset() beforehand.
Make the termination unconditional and independent of the zeroing by
writing the NUL byte explicitly after each strncpy() call.
Fixes: c040d4e15e ("core: fix 1-byte buffer over-read bugs in gpiod_chip_info_from_uapi()")
Fixes: 5533f277aa ("core: fix line-info strings termination")
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-3-781bcaf47475@oss.qualcomm.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
The local variable used to store the return value of
gpiod_line_request_get_values_subset() was declared as unsigned int
while the function returns int (-1 on error). The check for the error
condition worked by accident because assigning -1 to an unsigned type
produces UINT_MAX, which is non-zero, but using the wrong type is
misleading and technically invoking implementation-defined behaviour.
Use int throughout.
Fixes: b7ba732e6a ("treewide: libgpiod v2 implementation")
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-1-781bcaf47475@oss.qualcomm.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
We have some .txt files that contain SPDX headers to make `reuse lint`
happy. Let's move them into REUSE.toml instead for better readability of
their content.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
It's useful to know which options ended up really being selected by
meson (for instance for --auto-features) so use the dedicated meson
function to display the summary.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
gpiod_line_config_add_line_settings() checks the remaining capacity
using the raw num_offsets from the caller. If the config is already at
the 64-line limit, any call - even one that would only update settings
for already configured offsets - would fail with E2BIG making it
impossible to reconfigure a line once the config is full.
Extend gpiod_line_config_add_line_settings() to only count offsets that
would require a new config slot and use this number to check if the
line-config object still has capacity.
Add a test case allowing to verify the fix.
Fixes: b7ba732e6a ("treewide: libgpiod v2 implementation")
Link: https://patch.msgid.link/20260522-line-config-full-config-bug-v1-1-b138958bf05d@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>
The soversion argument of meson's both_libraries() accepts int and str
types. Use int as it's less error-prone and can be more easily compared.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Drop the 'sources' argument from meson's declare_dependency() for
libgpiodbus as this causes the build system to include the source files
in the source list of the targets using the dependency rather than link
against the libgpiodbus shared library.
Fixes: 97bab4cc87 ("build: replace autotools with meson & ninja")
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Use the meson can_run_host_binaries() function to determine whether we
should even descend into the man/ directory as the man pages are built
using help2man which requires us to actually run the executables.
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>
Recent versions of python tooling - like, for instance, one used by
gi-docgen - may reject PEP-440 non-compliant version strings and thus
fail to generate docs in some environments. Change the '-devel' suffix
to '+devel' to make these tools happy.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Update .readthedocs.yaml to pull in the correct packages for its docker
build.
Fixes: 97bab4cc87 ("build: replace autotools with meson & ninja")
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
When generating docs, don't enable more options than is necessary. This
could pull additional dependencies that we don't want.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Migration to meson broke the sphinx build for docs. Wire the gi-docgen
generation into the meson build and fix docs/conf.py to use the new
build system.
Fixes: 97bab4cc87 ("build: replace autotools with meson & ninja")
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Define and ue the project_url variable instead of repeating the URL in
several places.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Remove leftover mentions of Makefiles from Cargo.toml files in rust
bindings. They are no longer needed after the migration to meson.
Fixes: 97bab4cc87 ("build: replace autotools with meson & ninja")
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
The meson build already generates all pkg-config files via
pkgconfig.generate() in the respective meson.build files. The .pc.in
templates were not cleaned up during the autotools-to-meson migration
and are dead code. Drop them.
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>
Previously, tp_new and tp_init functions were defined for Request with
the tp_init function raising a NotImplementedError to prevent the object
from being initialized (__init__, the stage after __new__).
Similar results can be achieved by removing the tp_new and tp_init
functions which raise a slightly different error:
TypeError: cannot create 'gpiod._ext.Request' instances
As this is an internal only detail, there should be impact to users.
Signed-off-by: Vincent Fazio <vfazio@gmail.com>
Link: https://patch.msgid.link/20260423222125.29097-4-vfazio@gmail.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>