Skip to content

cmd/snap-gpio-helper: add gpio-chardev export/unexport commands #15212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
9422df2
many: refactor gpio chardev virtual device path
ZeyadYasser Mar 13, 2025
c99c29e
cmd/snap-gpio-helper: add gpio-chardev export command
ZeyadYasser Mar 13, 2025
33c2047
cmd/snap-gpio-helper: add gpio-chardev unexport command
ZeyadYasser Mar 15, 2025
046a348
cmd/snap-gpio-helper: require experimental.gpio-chardev-interface fla…
ZeyadYasser Mar 17, 2025
d9818b9
fixup! cmd/snap-gpio-helper: address review comments
ZeyadYasser Mar 19, 2025
ee46a1c
fixup! many: move gpio chip helpers into gadget/device package
ZeyadYasser Mar 27, 2025
c53bd22
fixup! cmd/snap-gpio-helper: fix unit test race
ZeyadYasser Mar 27, 2025
774d8a5
fixup! many: refactor gpio helpers into separate sandbox/gpio package
ZeyadYasser Apr 4, 2025
ff050fd
fixup! many: address review comments
ZeyadYasser Apr 4, 2025
8cca557
fixup! many: fix mac os build tests
ZeyadYasser Apr 7, 2025
81c16cd
fixup! sandbox/gpio: fix typos
ZeyadYasser Apr 7, 2025
406a0e4
osutil: add device major/minor number helpers
ZeyadYasser Apr 9, 2025
561bbfd
fixup! sandbox/gpio: address review comments
ZeyadYasser Apr 9, 2025
71e9ff6
fixup! many: address review comments
ZeyadYasser Apr 10, 2025
422970e
fixup! osutil: fix failing mac os unit tests
ZeyadYasser Apr 10, 2025
0f0c4d1
fixup! osutil: remove dependency on CGO for device major/minor helpers
ZeyadYasser Apr 11, 2025
172d333
fixup! sandbox/gpio: fix mixing fs.FileMode and raw syscall file
ZeyadYasser Apr 11, 2025
b3752c1
fixup! sandbox/gpio: fix failing mac os tests
ZeyadYasser Apr 11, 2025
168593e
fixup! many: address review comments
ZeyadYasser Apr 15, 2025
200b180
fixup! sandbox/gpio: add stat nil check
ZeyadYasser Apr 15, 2025
0d11203
fixup! many: address review comments
ZeyadYasser Apr 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions cmd/snap-gpio-helper/cmd_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@
package main

import (
"errors"
"context"
"fmt"
"os"
"os/signal"
"strings"

"github.com/snapcore/snapd/sandbox/gpio"
"github.com/snapcore/snapd/strutil"
)

type cmdExportChardev struct {
Expand All @@ -32,6 +39,17 @@ type cmdExportChardev struct {
} `positional-args:"yes" required:"true"`
}

var gpioExportGadgetChardevChip = gpio.ExportGadgetChardevChip

func (c *cmdExportChardev) Execute(args []string) error {
return errors.New("not implemented")
chipLabels := strings.Split(c.Args.ChipLabels, ",")
lines, err := strutil.ParseRange(c.Args.Lines)
if err != nil {
return fmt.Errorf("invalid lines argument: %w", err)
}

ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()

return gpioExportGadgetChardevChip(ctx, chipLabels, lines, c.Args.Gadget, c.Args.Slot)
}
75 changes: 75 additions & 0 deletions cmd/snap-gpio-helper/cmd_export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyright (C) 2025 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

package main_test

import (
"context"

. "gopkg.in/check.v1"

main "github.com/snapcore/snapd/cmd/snap-gpio-helper"
"github.com/snapcore/snapd/strutil"
)

func (s *snapGpioHelperSuite) TestExportGpioChardevBadLine(c *C) {
called := 0
restore := main.MockGpioExportGadgetChardevChip(func(ctx context.Context, chipLabels []string, lines strutil.Range, gadgetName, slotName string) error {
called++
return nil
})
defer restore()

for lines, expectedErr := range map[string]string{
"0-2,1": `invalid lines argument: overlapping range span found "1"`,
"1-0": `invalid lines argument: invalid range span "1-0": ends before it starts`,
"0-": `invalid lines argument: .*: invalid syntax`,
"a": `invalid lines argument: .*: invalid syntax`,
} {
err := main.Run([]string{
"export-chardev", "label-0", lines, "gadget-name", "slot-name",
})
c.Check(err, ErrorMatches, expectedErr)
}

c.Assert(called, Equals, 0)
}

func (s *snapGpioHelperSuite) TestExportGpioChardev(c *C) {
called := 0
restore := main.MockGpioExportGadgetChardevChip(func(ctx context.Context, chipLabels []string, lines strutil.Range, gadgetName, slotName string) error {
called++
c.Check(chipLabels, DeepEquals, []string{"label-0", "label-1"})
c.Check(lines, DeepEquals, strutil.Range{
{Start: 0, End: 6},
{Start: 7, End: 7},
{Start: 8, End: 100},
})
c.Check(gadgetName, Equals, "gadget-name")
c.Check(slotName, Equals, "slot-name")
return nil
})
defer restore()

err := main.Run([]string{
"export-chardev", "label-0,label-1", "7,0-6,8-100", "gadget-name", "slot-name",
})
c.Check(err, IsNil)
c.Assert(called, Equals, 1)
}
6 changes: 4 additions & 2 deletions cmd/snap-gpio-helper/cmd_unexport.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

package main

import "errors"
import "github.com/snapcore/snapd/sandbox/gpio"

type cmdUnexportChardev struct {
Args struct {
Expand All @@ -30,6 +30,8 @@ type cmdUnexportChardev struct {
} `positional-args:"yes" required:"true"`
}

var gpioUnexportGadgetChardevChip = gpio.UnexportGadgetChardevChip

func (c *cmdUnexportChardev) Execute(args []string) error {
return errors.New("not implemented")
return gpioUnexportGadgetChardevChip(c.Args.Gadget, c.Args.Slot)
}
43 changes: 43 additions & 0 deletions cmd/snap-gpio-helper/cmd_unexport_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyright (C) 2025 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

package main_test

import (
. "gopkg.in/check.v1"

main "github.com/snapcore/snapd/cmd/snap-gpio-helper"
)

func (s *snapGpioHelperSuite) TestUnexportGpioChardev(c *C) {
called := 0
restore := main.MockGpioUnxportGadgetChardevChip(func(gadgetName, slotName string) error {
called++
c.Check(gadgetName, Equals, "gadget-name")
c.Check(slotName, Equals, "slot-name")
return nil
})
defer restore()

err := main.Run([]string{
"unexport-chardev", "label-0,label-1", "7,0-6,8-100", "gadget-name", "slot-name",
})
c.Check(err, IsNil)
c.Assert(called, Equals, 1)
}
38 changes: 38 additions & 0 deletions cmd/snap-gpio-helper/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyright (C) 2025 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package main

import (
"context"

"github.com/snapcore/snapd/strutil"
"github.com/snapcore/snapd/testutil"
)

var (
Run = run
)

func MockGpioExportGadgetChardevChip(f func(ctx context.Context, chipLabels []string, lines strutil.Range, gadgetName string, slotName string) error) (restore func()) {
return testutil.Mock(&gpioExportGadgetChardevChip, f)
}

func MockGpioUnxportGadgetChardevChip(f func(gadgetName string, slotName string) error) (restore func()) {
return testutil.Mock(&gpioUnexportGadgetChardevChip, f)
}
10 changes: 8 additions & 2 deletions cmd/snap-gpio-helper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/jessevdk/go-flags"

"github.com/snapcore/snapd/features"
"github.com/snapcore/snapd/snapdtool"
)

Expand All @@ -33,11 +34,16 @@ type options struct {
CmdUnexportChardev cmdUnexportChardev `command:"unexport-chardev"`
}

func run(osArgs1 []string) error {
func run(args []string) error {
if !features.GPIOChardevInterface.IsEnabled() {
_, flag := features.GPIOChardevInterface.ConfigOption()
return fmt.Errorf("gpio-chardev interface requires the %q flag to be set", flag)
}

var opts options
p := flags.NewParser(&opts, flags.HelpFlag|flags.PassDoubleDash)

if _, err := p.ParseArgs(osArgs1); err != nil {
if _, err := p.ParseArgs(args); err != nil {
return err
}
return nil
Expand Down
64 changes: 64 additions & 0 deletions cmd/snap-gpio-helper/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyright (C) 2025 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

package main_test

import (
"os"
"testing"

. "gopkg.in/check.v1"

main "github.com/snapcore/snapd/cmd/snap-gpio-helper"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/features"
"github.com/snapcore/snapd/testutil"
)

// Hook up check.v1 into the "go test" runner
func Test(t *testing.T) { TestingT(t) }

type snapGpioHelperSuite struct {
testutil.BaseTest
}

var _ = Suite(&snapGpioHelperSuite{})

func (s *snapGpioHelperSuite) SetUpTest(c *C) {
dirs.SetRootDir(c.MkDir())
s.AddCleanup(func() { dirs.SetRootDir("") })

// Mock experimental.gpio-chardev-interface
c.Assert(os.MkdirAll(dirs.FeaturesDir, 0755), IsNil)
c.Assert(os.WriteFile(features.GPIOChardevInterface.ControlFile(), []byte(nil), 0644), IsNil)
}

func (s *snapGpioHelperSuite) TestGpioChardevExperimentlFlagUnset(c *C) {
c.Assert(os.RemoveAll(features.GPIOChardevInterface.ControlFile()), IsNil)

err := main.Run([]string{
"export-chardev", "label-0", "0,2", "gadget-name", "slot-name",
})
c.Check(err, ErrorMatches, `gpio-chardev interface requires the "experimental.gpio-chardev-interface" flag to be set`)

err = main.Run([]string{
"unexport-chardev", "label-0", "0,2", "gadget-name", "slot-name",
})
c.Check(err, ErrorMatches, `gpio-chardev interface requires the "experimental.gpio-chardev-interface" flag to be set`)
}
7 changes: 7 additions & 0 deletions dirs/dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ var (
SnapDeviceSaveDir string
SnapDataSaveDir string

SnapGpioChardevDir string

CloudMetaDataFile string
CloudInstanceDataFile string

Expand All @@ -148,6 +150,8 @@ var (

SysfsDir string

DevDir string

FeaturesDir string

// WritableMountPath is a path where writable root data is
Expand Down Expand Up @@ -642,6 +646,9 @@ func SetRootDir(rootdir string) {
SnapKModModulesDir = filepath.Join(rootdir, "/etc/modules-load.d/")
SnapKModModprobeDir = filepath.Join(rootdir, "/etc/modprobe.d/")

DevDir = filepath.Join(rootdir, "/dev")
SnapGpioChardevDir = filepath.Join(DevDir, "/snap/gpio-chardev")

LocaleDir = filepath.Join(rootdir, "/usr/share/locale")
ClassicDir = filepath.Join(rootdir, "/writable/classic")

Expand Down
5 changes: 3 additions & 2 deletions interfaces/builtin/gpio_chardev.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/snapcore/snapd/interfaces/apparmor"
"github.com/snapcore/snapd/interfaces/systemd"
"github.com/snapcore/snapd/interfaces/udev"
"github.com/snapcore/snapd/sandbox/gpio"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/strutil"
)
Expand Down Expand Up @@ -165,8 +166,8 @@ func (iface *gpioChardevInterface) SystemdConnectedPlug(spec *systemd.Specificat
plugName := plug.Name()
plugSnapName := plug.Snap().InstanceName()

target := filepath.Join("/dev/snap/gpio-chardev", slotSnapName, slotName)
symlink := filepath.Join("/dev/snap/gpio-chardev", plugSnapName, plugName)
target := gpio.SnapChardevPath(slotSnapName, slotName)
symlink := gpio.SnapChardevPath(plugSnapName, plugName)

// Create symlink pointing to exported virtual slot device.
serviceSuffix := fmt.Sprintf("gpio-chardev-%s", plugName)
Expand Down
4 changes: 2 additions & 2 deletions interfaces/builtin/gpio_chardev_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ func (s *GpioChardevInterfaceSuite) TestSystemdConnectedPlug(c *C) {
err := spec.AddConnectedPlug(s.iface, s.plug, s.slot)
c.Assert(err, IsNil)

target := "/dev/snap/gpio-chardev/my-device/gpio-chardev-good-slot"
symlink := "/dev/snap/gpio-chardev/consumer/gpio-chardev-good-plug"
target := fmt.Sprintf("%s/my-device/gpio-chardev-good-slot", dirs.SnapGpioChardevDir)
symlink := fmt.Sprintf("%s/consumer/gpio-chardev-good-plug", dirs.SnapGpioChardevDir)

expectedExecStart := fmt.Sprintf("/bin/sh -c 'mkdir -p %q && ln -s %q %q'", filepath.Dir(symlink), target, symlink)
expectedExecStop := fmt.Sprintf("/bin/rm -f %q", symlink)
Expand Down
2 changes: 2 additions & 0 deletions osutil/inotify/inotify_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ var errNotSupported = fmt.Errorf("watch not supported on %s", runtime.GOOS)
const (
InDelete uint32 = 0
InDeleteSelf uint32 = 0
InCreate uint32 = 0
InCloseWrite uint32 = 0
)

// NewWatcher creates and returns a new inotify instance using inotify_init(2)
Expand Down
2 changes: 1 addition & 1 deletion osutil/stat.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyright (C) 2014-2015 Canonical Ltd
* Copyright (C) 2014-2025 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
Expand Down
2 changes: 1 addition & 1 deletion osutil/stat_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyright (C) 2014-2015 Canonical Ltd
* Copyright (C) 2014-2025 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
Expand Down
Loading
Loading