Skip to content
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

cmd/snap-gpio-helper: add skeleton with re-exec support #15023

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion build-aux/snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,8 @@ parts:
lib/snapd/snap-seccomp
lib/snapd/snap-update-ns
lib/snapd/snapctl
lib/snapd/snapd-apparmor)
lib/snapd/snapd-apparmor
lib/snapd/snap-gpio-helper)
for cmd in "${CMDS[@]}"; do
case "${cmd}" in
# FIXME: some binaries need to be run confined in apps. But
Expand Down
35 changes: 35 additions & 0 deletions cmd/snap-gpio-helper/cmd_export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// -*- 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 (
"errors"
)

type cmdExportChardev struct {
ChipLabels string `long:"chip-labels" description:"comma-separated list of source chip label(s) to match" required:"yes"`
Lines uint `long:"line" description:"comma-separated list of gpio line(s) to export" required:"yes"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Lines uint `long:"line" description:"comma-separated list of gpio line(s) to export" required:"yes"`
Lines string `long:"line" description:"comma-separated list of gpio line(s) to export" required:"yes"`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my bad, thanks!

Gadget string `long:"gadget" description:"gadget snap name" required:"yes"`
Slot string `long:"slot" description:"gpio-chardev slot name" required:"yes"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all are required so you can make them positional

}

func (c *cmdExportChardev) Execute(args []string) error {
return errors.New("not implemented")
}
31 changes: 31 additions & 0 deletions cmd/snap-gpio-helper/cmd_unexport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// -*- 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 "errors"

type cmdUnexportChardev struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the spec not mention "<chip-labels>" "<line>" "<gadget>" "<slot-name>" for this command?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for unexporting they are not really needed, but I could add them for completeness.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually the description for chip-labels and lines wouldn't make sense in the case of unexporting.

	ChipLabels string `long:"chip-labels" description:"comma-separated list of source chip label(s) to match" required:"yes"`
	Lines      uint   `long:"line" description:"comma-separated list of gpio line(s) to unexport" required:"yes"`

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

options are marked as required, so you could make them positional like for export. Also, let's add all of them, such that even if the implementation was to change we have enough context provided in the input.

Gadget string `long:"gadget" description:"gadget snap name" required:"yes"`
Slot string `long:"slot" description:"gpio-chardev slot name" required:"yes"`
}

func (c *cmdUnexportChardev) Execute(args []string) error {
return errors.New("not implemented")
}
53 changes: 53 additions & 0 deletions cmd/snap-gpio-helper/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// -*- 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 (
"fmt"
"os"

"github.com/jessevdk/go-flags"

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

type options struct {
CmdExportChardev cmdExportChardev `command:"export-chardev"`
CmdUnexportChardev cmdUnexportChardev `command:"unexport-chardev"`
}

func run(osArgs1 []string) error {
var opts options
p := flags.NewParser(&opts, flags.HelpFlag|flags.PassDoubleDash)

if _, err := p.ParseArgs(osArgs1); err != nil {
return err
}
return nil
}

func main() {
snapdtool.ExecInSnapdOrCoreSnap()

if err := run(os.Args[1:]); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
1 change: 1 addition & 0 deletions packaging/ubuntu-16.04/snapd.install.in
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ usr/bin/snap-preseed /usr/lib/snapd/
usr/bin/snap-recovery-chooser /usr/lib/snapd/
usr/bin/snap-fde-keymgr /usr/lib/snapd/
usr/bin/snapd-apparmor /usr/lib/snapd/
usr/bin/snap-gpio-helper /usr/lib/snapd/

# bash completion
data/completion/bash/snap /usr/share/bash-completion/completions
Expand Down
24 changes: 24 additions & 0 deletions tests/main/snap-gpio-helper/task.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
summary: Check that snap-helper-helper exists

details: |
The test verifies that snap-gpio-helper exists

systems: [ubuntu-*]

execute: |
libexecdir=$(os.paths libexec-dir)
if tests.info is-snapd-from-archive; then
libexecdir="/snap/snapd/current$libexecdir"
fi

if [ ! -f "$libexecdir/snapd/snap-gpio-helper" ]; then
exit 1
fi

# Verify that snap-gpio-helper skeleton re-execs
echo "Ensure snap-gpio-helper re-execs"
set +e
SNAPD_DEBUG=1 "$libexecdir/snapd/snap-gpio-helper" 2>&1 | MATCH "DEBUG: restarting into"
set -e

# TODO: add more checks
Loading