1
0
mirror of https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git synced 2026-10-24 00:48:53 +03:00
Commit Graph

19 Commits

Author SHA1 Message Date
Bartosz Golaszewski
37b3ac4bb5 build: remove redundant SOURCES assignments
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>
2023-06-30 14:51:34 +02:00
Erik Schilling
370bf6825d gpiosim: fix data race that corrupts heap
Hit this while seeing some heap corruptions when running cargo test on
the Rust bindings.

Took a bit to track down since I first used address sanitizers, but with
the thread sanitizer it becomes obvious immediately (output simplified):

    ==================
    WARNING: ThreadSanitizer: data race (pid=288119)
      Write of size 8 at 0x0000018f1e78 by thread T6:
        #0 id_free /libgpiod/tests/gpiosim/gpiosim.c:141:17
        #1 dev_release /libgpiod/tests/gpiosim/gpiosim.c:600:2
        #2 refcount_dec /libgpiod/tests/gpiosim/gpiosim.c:176:3
        #3 gpiosim_dev_unref /libgpiod/tests/gpiosim/gpiosim.c:671:2
        #4 bank_release /libgpiod/tests/gpiosim/gpiosim.c:873:2
        #5 refcount_dec /libgpiod/tests/gpiosim/gpiosim.c:176:3
        #6 gpiosim_bank_unref /libgpiod/tests/gpiosim/gpiosim.c:941:2
        [...]

      Previous write of size 8 at 0x0000018f1e78 by thread T1:
        #0 id_free /libgpiod/tests/gpiosim/gpiosim.c:141:17
        #1 bank_release /libgpiod/tests/gpiosim/gpiosim.c:878:2
        #2 refcount_dec /libgpiod/tests/gpiosim/gpiosim.c:176:3
        #3 gpiosim_bank_unref /libgpiod/tests/gpiosim/gpiosim.c:941:2
        [...]

      Location is global 'id_del_ctx' of size 16 at 0x0000018f1e70

      Thread T6 'chip::verify::f' (tid=288126, running) created by main thread at:
        #7 test::run_tests::hd53a07a011bd771f /.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/test/src/lib.rs:407:21
        [...]

      Thread T1 'chip::open::gpi' (tid=288121, finished) created by main thread at:
        #7 test::run_tests::hd53a07a011bd771f /.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/test/src/lib.rs:407:21
        [...]

    SUMMARY: ThreadSanitizer: data race /libgpiod/tests/gpiosim/gpiosim.c:141:17 in id_free
    ==================

This eventually can either lead to leaks or double free's that corrupt
the heap and lead to crashes.

The issue got introduced when a previously local variable that did not
require protection was turned into a global variable.

Fixes: 5e111df2fc ("gpiosim: use twalk() instead of twalk_r()")
Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-06-26 16:31:39 +02:00
Bartosz Golaszewski
968c7c8752 gpiosim: remove unused define
ARRAY_SIZE() is defined but unused so remove it.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-02-17 14:05:30 +01:00
Bartosz Golaszewski
5e111df2fc gpiosim: use twalk() instead of twalk_r()
twalk_r() is a GNU extension for the binary search tree API. Musl libc
doesn't provide it and after inquiring with the maintainer it turned out
it won't provide it anytime soon as musl strives to only implement well
standardized functions.

In order to not limit building libgpiosim and tests to glibc only, let's
replace twalk_r() with twalk() and use global variables for the state.
It's not a big deal as we're already using a global root node and a mutex
to protect it.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-02-14 09:13:59 +01:00
Bartosz Golaszewski
936785d36e gpiosim: fix error reporting in gpiosim_bank_hog_line()
We don't return errno numbers from public functions - we set errno and
indicate an error by returning -1 or NULL depending on the function
signature. Make gpiosim_bank_hog_line() consistent with that behavior.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-01-02 15:31:58 +01:00
Bartosz Golaszewski
43fed2c87c gpiosim: add missing fcntl.h include
The fcntl.h header is needed for openat() and mkdirat() that the gpiosim
code uses. It seems to be pulled in indirectly on most toolchains but for
the sake of correctness, include it explicitly.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2022-12-09 11:09:07 +01:00
Bartosz Golaszewski
3006d924e4 gpiosim: get the process name using prctl()
program_invocation_short_name is set once at the program's start. If we
change the process name using prctl(), we need to retrieve it using the
same system call as program_invocation_short_name will not be updated.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2022-12-09 10:06:53 +01:00
Bartosz Golaszewski
ba0fa90b8b gpiosim: fix a resource leak
If we set the number of lines to x, set line names or hogs for lines
from 0 through x, then set the number of lines to (x - y), we will not
unlink all the line directories as we only iterate up to num_lines in
bank_release().

Allocate a small structure for every line we create a lineX directory
for and iterate over it in bank_release() to know exactly which ones
need freeing.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2022-12-07 10:14:48 +01:00
Bartosz Golaszewski
2064a05984 gpiosim: use wrappers around container_of()
Simplify the code a bit by providing helpers that extract the underlying
structure from struct refcount using container_of().

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2022-12-07 10:14:48 +01:00
Bartosz Golaszewski
0acc1fb29e gpiosim: rename HOG_DIR to DIRECTION
The enum itself should just define direction settings, it's the functions
that use it that should refer to hogging.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
2022-12-07 09:53:28 +01:00
Bartosz Golaszewski
3fe7de7a65 treewide: apply formatting changes with clang-format
Use linux kernel's .clang-format file to automatically improve the coding
style of libgpiod's C code base. We don't import the file into the
repository as it's not perfect and certain converted fragments were
rolled back because they looked better before the conversion.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2022-12-07 09:45:06 +01:00
Bartosz Golaszewski
e065ef962a treewide: use C enum types explicitly
Use enum types explicitly across the entire C API. Modern compilers can
catch some bugs with enums and it also helps IDEs and general readability.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
[Viresh: make rust bindings work with negative enum values]
Co-authored-by: Viresh Kumar <viresh.kumar@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
2022-12-07 09:42:02 +01:00
Bartosz Golaszewski
b7ba732e6a treewide: libgpiod v2 implementation
This commit is the result of a squash of all the commits created during
the development of libgpiod v2. This is done to preserve bisectability
of the git tree. It contains significant re-writes of the core C library
as well as C++ and python bindings. The tools have been adjusted to work
with the new API but have not undergone significant changes yet.

This commit contains the bulk of the work on libgpiod v2 but the
development continues with more changes planned.

Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
[Ben: fix format specifier for uint64_t]
Signed-off-by: Ben Hutchings <ben.hutchings@mind.be>
[Viresh: Fix ioctl number for gpiod_line_request_reconfigure_lines()]
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
[Kent: bug fixes, improvements, better pointer contracts, type strictness]
Signed-off-by: Kent Gibson <warthog618@gmail.com>
2022-10-31 11:15:14 +01:00
Bartosz Golaszewski
d5ea8c9ed1 gpiosim: initialize the bank's num_lines field to 1
If we create a simulated chip and never set the num_lines property, the
num_lines field remains set to 0 and the potential lineX and hog
directories within the bank's directory will not get removed so the
removal of the bank's and device's directories will fail too leaving
a dangling chip in place.

Initialize bank->num_lines to 1 to avoid this issue.

Fixes: a2f6e8cd35 ("libgpiosim: new library for controlling the gpio-sim module")
Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
2022-04-25 14:41:55 +02:00
Bartosz Golaszewski
38f57b1084 gpiosim: fix file descriptor leak
If a bank gets released before the device is disabled, the sysfs file
descriptor of that bank is leaked. close() it when releasing the bank.

While at it: improve two other details - unlink the configfs directory
after closing the file descriptor associated with it and remove an
unnecessary check when closing the sysfs descriptor when disabling the
device.

Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
2022-03-18 15:31:24 +01:00
Bartosz Golaszewski
54957b0572 gpiosim: add an enum representing line active state
In order to avoid confusion and stress the fact that "value" represents
the logical state of a GPIO line, add a two-value enum with the names
"ACTIVE" and "INACTIVE" to the libgpiosim API.

Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
2022-03-09 10:14:33 +01:00
Bartosz Golaszewski
d7d0aae7b9 gpiosim: use size_t in the public interface where applicable
Use the standard integer type for representing sizes in the API of
libgpiod wherever it makes sense.

Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
2022-03-09 10:09:24 +01:00
Bartosz Golaszewski
ffb006dd4a gpiosim: fix a memory leak
If a bank gets dropped when the device is still enabled and before the
parent device, we leak the device path and chip name strings. This makes
sure we always free them.

Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
2022-02-23 22:14:17 +01:00
Bartosz Golaszewski
a2f6e8cd35 libgpiosim: new library for controlling the gpio-sim module
Add a C library for controlling the gpio-sim kernel module from various
libgpiod test suites. This aims at replacing the old gpio-mockup module
and its user-space library - libgpio-mockup - in the project's tree.

Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
2022-02-10 14:42:44 +01:00