mbox series

[V4,0/8] libgpiod: Add Rust bindings

Message ID cover.1657279685.git.viresh.kumar@linaro.org
Headers show
Series libgpiod: Add Rust bindings | expand

Message

Viresh Kumar July 8, 2022, 11:34 a.m. UTC
Hello,

Here is another version of rust bindings for libgpiod v2.0, based of the
next/libgpiod-2.0.

Pushed here:

https://github.com/vireshk/libgpiod master

V3->V4:
- Rebased on top of new changes, and made changes accordingly.
- Added rust integration tests with gpiosim.
- Found a kernel bug with tests, sent a patch for that to LKML.

V2->V3:
- Remove naming redundancy, users just need to do this now
  use libgpiod:{Chip, Direction, LineConfig} now (Bartosz);
- Fix lifetime issues between event-buffer and edge-event modules, the event
  buffer is released after the last edge-event reference is dropped (Bartosz).
- Allow edge-event to be copied, and freed later (Bartosz).
- Add two separate rust crates, sys and wrapper (Gerard).
- Null-terminate the strings passed to libgpiod (Wedson).
- Drop unnecessary checks to validate string returned from chip:name/label/path.
- Fix SAFETY comments (Wedson).
- Drop unnecessary clone() instances (Bartosz).

V1->V2:
- Added examples (I tested everything except gpiomon.rs, didn't have right
  hardware/mock device to test).
- Build rust bindings as part of Make, update documentation.

Thanks.

--
Viresh

Viresh Kumar (8):
  libgpiod: Add libgpiod-sys rust crate
  libgpiod: Add pre generated rust bindings
  libgpiod-sys: Add support to generate gpiosim bindings
  libgpiod: Add rust wrapper crate
  libgpiod: Add rust examples
  libgpiod: Derive debug traits for few definitions
  libgpiod: Add rust tests
  libgpiod: Integrate building of rust bindings with make

 .gitignore                                   |    5 +
 README                                       |    8 +-
 TODO                                         |    8 -
 bindings/Makefile.am                         |    6 +
 bindings/rust/Cargo.toml                     |   15 +
 bindings/rust/Makefile.am                    |   18 +
 bindings/rust/examples/gpiodetect.rs         |   37 +
 bindings/rust/examples/gpiofind.rs           |   42 +
 bindings/rust/examples/gpioget.rs            |   42 +
 bindings/rust/examples/gpioinfo.rs           |   89 +
 bindings/rust/examples/gpiomon.rs            |   68 +
 bindings/rust/examples/gpioset.rs            |   52 +
 bindings/rust/libgpiod-sys/Cargo.toml        |   16 +
 bindings/rust/libgpiod-sys/README.md         |   10 +
 bindings/rust/libgpiod-sys/build.rs          |   84 +
 bindings/rust/libgpiod-sys/gpiosim_wrapper.h |    1 +
 bindings/rust/libgpiod-sys/src/bindings.rs   | 1920 ++++++++++++++++++
 bindings/rust/libgpiod-sys/src/lib.rs        |   20 +
 bindings/rust/libgpiod-sys/wrapper.h         |    2 +
 bindings/rust/src/chip.rs                    |  184 ++
 bindings/rust/src/chip_info.rs               |   70 +
 bindings/rust/src/edge_event.rs              |  105 +
 bindings/rust/src/event_buffer.rs            |   88 +
 bindings/rust/src/info_event.rs              |   68 +
 bindings/rust/src/lib.rs                     |  323 +++
 bindings/rust/src/line_config.rs             |  493 +++++
 bindings/rust/src/line_info.rs               |  190 ++
 bindings/rust/src/line_request.rs            |  249 +++
 bindings/rust/src/request_config.rs          |  122 ++
 bindings/rust/tests/chip.rs                  |   96 +
 bindings/rust/tests/common/config.rs         |  117 ++
 bindings/rust/tests/common/mod.rs            |   16 +
 bindings/rust/tests/common/sim.rs            |  306 +++
 bindings/rust/tests/edge_event.rs            |  389 ++++
 bindings/rust/tests/info_event.rs            |  126 ++
 bindings/rust/tests/line_config.rs           |  187 ++
 bindings/rust/tests/line_info.rs             |   90 +
 bindings/rust/tests/line_request.rs          |  234 +++
 bindings/rust/tests/request_config.rs        |   42 +
 configure.ac                                 |   16 +
 40 files changed, 5943 insertions(+), 11 deletions(-)
 create mode 100644 bindings/rust/Cargo.toml
 create mode 100644 bindings/rust/Makefile.am
 create mode 100644 bindings/rust/examples/gpiodetect.rs
 create mode 100644 bindings/rust/examples/gpiofind.rs
 create mode 100644 bindings/rust/examples/gpioget.rs
 create mode 100644 bindings/rust/examples/gpioinfo.rs
 create mode 100644 bindings/rust/examples/gpiomon.rs
 create mode 100644 bindings/rust/examples/gpioset.rs
 create mode 100644 bindings/rust/libgpiod-sys/Cargo.toml
 create mode 100644 bindings/rust/libgpiod-sys/README.md
 create mode 100644 bindings/rust/libgpiod-sys/build.rs
 create mode 100644 bindings/rust/libgpiod-sys/gpiosim_wrapper.h
 create mode 100644 bindings/rust/libgpiod-sys/src/bindings.rs
 create mode 100644 bindings/rust/libgpiod-sys/src/lib.rs
 create mode 100644 bindings/rust/libgpiod-sys/wrapper.h
 create mode 100644 bindings/rust/src/chip.rs
 create mode 100644 bindings/rust/src/chip_info.rs
 create mode 100644 bindings/rust/src/edge_event.rs
 create mode 100644 bindings/rust/src/event_buffer.rs
 create mode 100644 bindings/rust/src/info_event.rs
 create mode 100644 bindings/rust/src/lib.rs
 create mode 100644 bindings/rust/src/line_config.rs
 create mode 100644 bindings/rust/src/line_info.rs
 create mode 100644 bindings/rust/src/line_request.rs
 create mode 100644 bindings/rust/src/request_config.rs
 create mode 100644 bindings/rust/tests/chip.rs
 create mode 100644 bindings/rust/tests/common/config.rs
 create mode 100644 bindings/rust/tests/common/mod.rs
 create mode 100644 bindings/rust/tests/common/sim.rs
 create mode 100644 bindings/rust/tests/edge_event.rs
 create mode 100644 bindings/rust/tests/info_event.rs
 create mode 100644 bindings/rust/tests/line_config.rs
 create mode 100644 bindings/rust/tests/line_info.rs
 create mode 100644 bindings/rust/tests/line_request.rs
 create mode 100644 bindings/rust/tests/request_config.rs

Comments

Bartosz Golaszewski July 15, 2022, 7:07 p.m. UTC | #1
On Fri, Jul 8, 2022 at 1:35 PM Viresh Kumar <viresh.kumar@linaro.org> wrote:
>
> Hello,
>
> Here is another version of rust bindings for libgpiod v2.0, based of the
> next/libgpiod-2.0.
>
> Pushed here:
>
> https://github.com/vireshk/libgpiod master
>
> V3->V4:
> - Rebased on top of new changes, and made changes accordingly.
> - Added rust integration tests with gpiosim.
> - Found a kernel bug with tests, sent a patch for that to LKML.
>
> V2->V3:
> - Remove naming redundancy, users just need to do this now
>   use libgpiod:{Chip, Direction, LineConfig} now (Bartosz);
> - Fix lifetime issues between event-buffer and edge-event modules, the event
>   buffer is released after the last edge-event reference is dropped (Bartosz).
> - Allow edge-event to be copied, and freed later (Bartosz).
> - Add two separate rust crates, sys and wrapper (Gerard).
> - Null-terminate the strings passed to libgpiod (Wedson).
> - Drop unnecessary checks to validate string returned from chip:name/label/path.
> - Fix SAFETY comments (Wedson).
> - Drop unnecessary clone() instances (Bartosz).
>
> V1->V2:
> - Added examples (I tested everything except gpiomon.rs, didn't have right
>   hardware/mock device to test).
> - Build rust bindings as part of Make, update documentation.
>
> Thanks.
>
> --
> Viresh
>
> Viresh Kumar (8):
>   libgpiod: Add libgpiod-sys rust crate
>   libgpiod: Add pre generated rust bindings
>   libgpiod-sys: Add support to generate gpiosim bindings
>   libgpiod: Add rust wrapper crate
>   libgpiod: Add rust examples
>   libgpiod: Derive debug traits for few definitions
>   libgpiod: Add rust tests
>   libgpiod: Integrate building of rust bindings with make
>

Hey Viresh, Kent and Miguel!

Rust noob here: I have my cargo installed using rustup for my local
user but I can't run it via sudo or as root (to run tests) because I'm
seeing this:

    error: no override and no default toolchain set

What is the right way to use cargo as root? I'm hesitant to just curl
a random script and pipe it to shell as root honestly.

Bart
Miguel Ojeda July 15, 2022, 7:17 p.m. UTC | #2
Hi Bartosz,

On Fri, Jul 15, 2022 at 9:08 PM Bartosz Golaszewski <brgl@bgdev.pl> wrote:
>
> Rust noob here: I have my cargo installed using rustup for my local
> user but I can't run it via sudo or as root (to run tests) because I'm
> seeing this:
>
>     error: no override and no default toolchain set
>
> What is the right way to use cargo as root? I'm hesitant to just curl
> a random script and pipe it to shell as root honestly.

If by "random script" you mean the rustup installer, you can always
download rustup on its own, or even directly a Rust "standalone
installer", which are signed and do not require rustup, from:

    https://forge.rust-lang.org/infra/other-installation-methods.html#standalone

However, since it appears that you have rustup installed, maybe you
don't mean that. In any case, if rustup is installed, maybe you can
try something like:

    rustup default stable

or similar to install a toolchain.

Cheers,
Miguel
Miguel Ojeda July 15, 2022, 7:27 p.m. UTC | #3
On Fri, Jul 15, 2022 at 9:17 PM Miguel Ojeda
<miguel.ojeda.sandonis@gmail.com> wrote:
>
> If by "random script" you mean the rustup installer, you can always
> download rustup on its own, or even directly a Rust "standalone
> installer", which are signed and do not require rustup, from:
>
>     https://forge.rust-lang.org/infra/other-installation-methods.html#standalone
>
> However, since it appears that you have rustup installed, maybe you
> don't mean that. In any case, if rustup is installed, maybe you can
> try something like:
>
>     rustup default stable
>
> or similar to install a toolchain.

Having said that, if building as a normal user is OK (should be), then
you maybe can simply run the test binary as root (building it with
something like `cargo --no-run` as a normal user).

Cheers,
Miguel
Miguel Ojeda July 16, 2022, 9:43 a.m. UTC | #4
On Fri, Jul 15, 2022 at 9:27 PM Miguel Ojeda
<miguel.ojeda.sandonis@gmail.com> wrote:
>
> something like `cargo --no-run` as a normal user).

Sorry, that was intended to be `cargo test --no-run` (and the test
binary will be in `target/debug/deps`). Also note that is only for the
unit tests, though, not doctests. It can get more involved for those
if you want to avoid to run Cargo/rustdoc as root.

Hope that helps!

Cheers,
Miguel
Bartosz Golaszewski July 16, 2022, 10:43 a.m. UTC | #5
On Sat, Jul 16, 2022 at 11:43 AM Miguel Ojeda
<miguel.ojeda.sandonis@gmail.com> wrote:
>
> On Fri, Jul 15, 2022 at 9:27 PM Miguel Ojeda
> <miguel.ojeda.sandonis@gmail.com> wrote:
> >
> > something like `cargo --no-run` as a normal user).
>
> Sorry, that was intended to be `cargo test --no-run` (and the test
> binary will be in `target/debug/deps`). Also note that is only for the
> unit tests, though, not doctests. It can get more involved for those
> if you want to avoid to run Cargo/rustdoc as root.
>
> Hope that helps!
>
> Cheers,
> Miguel

This does seem to build the test-suite but there's no single
executable in target/debug/deps. Instead it seems that every test
section gets its own executable. Does 'cargo test' run them
separately? Or is there a single binary including all of them
somewhere else?

Bart
Kent Gibson July 16, 2022, 12:23 p.m. UTC | #6
On Sat, Jul 16, 2022 at 12:43:58PM +0200, Bartosz Golaszewski wrote:
> On Sat, Jul 16, 2022 at 11:43 AM Miguel Ojeda
> <miguel.ojeda.sandonis@gmail.com> wrote:
> >
> > On Fri, Jul 15, 2022 at 9:27 PM Miguel Ojeda
> > <miguel.ojeda.sandonis@gmail.com> wrote:
> > >
> > > something like `cargo --no-run` as a normal user).
> >
> > Sorry, that was intended to be `cargo test --no-run` (and the test
> > binary will be in `target/debug/deps`). Also note that is only for the
> > unit tests, though, not doctests. It can get more involved for those
> > if you want to avoid to run Cargo/rustdoc as root.
> >
> > Hope that helps!
> >
> > Cheers,
> > Miguel
> 
> This does seem to build the test-suite but there's no single
> executable in target/debug/deps.

As does providing the --enable-tests to configure, btw.

> Instead it seems that every test
> section gets its own executable. Does 'cargo test' run them
> separately? Or is there a single binary including all of them
> somewhere else?
> 

AIUI 'cargo test' builds and runs those tests individually.

As we need to run the integration tests as root, and cargo doesn't play
nice unless you install it as root, and the executables include
md5(?) hashes in their names, it is a major PITA to run the full suite
as root.  Unless you do everything as root.

Anyway to get those names from cargo?

Assuming the gpio-sim module is already loaded, is it at all possible to
run tests as a low privilege user?  e.g. udev rules can be added to chown
the /dev/gpiochips into a gpio group.  Is it possible to do something
similar for the configfs and sysfs for the gpio-sim?
Or do we need a gpiosimd that will allow a user to launch and control
sims without being root ;-).

Cheers,
Kent.
Miguel Ojeda July 16, 2022, 1:46 p.m. UTC | #7
On Sat, Jul 16, 2022 at 12:44 PM Bartosz Golaszewski <brgl@bgdev.pl> wrote:
>
> This does seem to build the test-suite but there's no single
> executable in target/debug/deps. Instead it seems that every test
> section gets its own executable. Does 'cargo test' run them
> separately? Or is there a single binary including all of them
> somewhere else?

Yeah, for `tests/` tests it seems to create several binaries. For
`#[test]`s, they are put in a single binary.

Another solution for both of those (from
https://github.com/rust-lang/cargo/issues/5999) is `.cargo/config` (in
the project folder):

    [target.x86_64-unknown-linux-gnu]
    runner = 'sudo -E'

Which indeed seems to work for both `tests/` and `#[test]`s.

However, I don't think that one works for doctests, though (at least I
don't see it executing `rustdoc` with the runner)... But maybe you
don't need privileges for those.

Cheers,
Miguel