Skip to content

Commit

Permalink
cmd/snap-failure: do not attempt to revert snapd when reexec isn't su…
Browse files Browse the repository at this point in the history
…pported by the systemd

The snap-failure helper is used as a general failure handler for failures of the
snapd.service unit. Its purpose is to trigger a revert of snapd snap to the
previous revision whenever a failure occurs. The logic asserting whether a snapd
refresh was in progress is privy to the actual snapd binary, however any attempt
to execute snapd binary from the snapd snap makes no sense if the system does
not support running snapd directly from the snap in the first place.

Signed-off-by: Maciej Borzecki <[email protected]>
  • Loading branch information
bboozzoo authored and Meulengracht committed May 24, 2024
1 parent fbaa0e5 commit c20b188
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
12 changes: 12 additions & 0 deletions cmd/snap-failure/cmd_snapd.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/snapdtool"
)

func init() {
Expand Down Expand Up @@ -112,6 +114,16 @@ var (
)

func (c *cmdSnapd) Execute(args []string) error {
if release.OnClassic {
// snap failure was invoked in a classic system, while there are
// scenarios in which it may make sense, they are limited to a
// case when snapd is being reexec'd from the snapd snap
if !snapdtool.DistroSupportsReExec() || !snapdtool.IsReexecEnabled() {
logger.Noticef("re-exec unsupported or disabled")
return nil
}
}

var snapdPath string
// find previous the snapd snap
prevRev, err := prevRevision("snapd")
Expand Down
36 changes: 36 additions & 0 deletions cmd/snap-failure/cmd_snapd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
failure "github.com/snapcore/snapd/cmd/snap-failure"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/testutil"
)
Expand Down Expand Up @@ -438,3 +439,38 @@ func (r *failureSuite) TestStickySnapdSocket(c *C) {
// make sure the socket file was deleted
c.Assert(osutil.FileExists(dirs.SnapdSocket), Equals, false)
}

func (r *failureSuite) testNoReexec(c *C) {
origArgs := os.Args
defer func() { os.Args = origArgs }()

writeSeqFile(c, "snapd", snap.R(100), []*snap.SideInfo{
{Revision: snap.R(99)},
{Revision: snap.R(100)},
})

// mock snapd command from 'previous' revision
snapdCmd := testutil.MockCommand(c, filepath.Join(dirs.SnapMountDir, "snapd", "99", "/usr/lib/snapd/snapd"), "exit 1")
defer snapdCmd.Restore()

os.Args = []string{"snap-failure", "snapd"}
err := failure.Run()
c.Check(err, IsNil)

c.Check(snapdCmd.Calls(), HasLen, 0)
c.Check(r.systemctlCmd.Calls(), HasLen, 0)
c.Check(r.log.String(), testutil.Contains, "re-exec unsupported or disabled")
}

func (r *failureSuite) TestReexecDisabled(c *C) {
os.Setenv("SNAP_REEXEC", "0")
defer os.Unsetenv("SNAP_REEXEC")
r.testNoReexec(c)

}

func (r *failureSuite) TestReexecUnsupported(c *C) {
r.AddCleanup(release.MockReleaseInfo(&release.OS{ID: "fedora"}))
dirs.SetRootDir(r.rootdir)
r.testNoReexec(c)
}
3 changes: 3 additions & 0 deletions cmd/snap-failure/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
failure "github.com/snapcore/snapd/cmd/snap-failure"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/testutil"
)

Expand Down Expand Up @@ -59,6 +60,8 @@ func (r *failureSuite) SetUpTest(c *C) {
failure.Stderr = r.stderr
failure.Stdout = r.stdout

r.AddCleanup(release.MockReleaseInfo(&release.OS{ID: "ubuntu"}))

r.rootdir = c.MkDir()
dirs.SetRootDir(r.rootdir)
r.AddCleanup(func() { dirs.SetRootDir("/") })
Expand Down

0 comments on commit c20b188

Please sign in to comment.