Vous êtes sur la page 1sur 4

Join GitHub today

GitHub is home to over 50 million developers working together to host and review code,
manage projects, and build software together.
Sign up
 master 
 44 branches 237 tags
Go to file Code 
Latest commit
mvo5 Merge pull request #9290 from mvo5/release-2.46.1

eaa9fb916 hours ago
Git stats
  45,007 commits

Files
snapdtool: helper to check whether the current binary is reexeced
fro…
…m a snap

Add a helper for checking whether the current binary is running from a snap.

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>


 

 master

 (#8898)

 
 

 2.46.1

 2.46

bboozzoo committed on Jun 22 


1 parent c8d5479 commit 3dc1ca5ad3f634a80b338df642ef351752b00649

Unified Split
Showing 3 changed files with 54 additions and 1 deletion.

 12  snapdtool/tool_linux.go 

@@ -205,6 +205,18 @@ func ExecInSnapdOrCoreSnap() {

panic(syscallExec(full, os.Args, os.Environ()))


}

// IsReexecd returns true when the current process binary is running from a snap.
func IsReexecd() (bool, error) {
exe, err := osReadlink(selfExe)
if err != nil {
return false, err
}
if strings.HasPrefix(exe, dirs.SnapMountDir) {
return true, nil
}
return false, nil
}

// MockOsReadlink is for use in tests


func MockOsReadlink(f func(string) (string, error)) func() {
realOsReadlink := osReadlink

 11  snapdtool/tool_other.go 

@@ -24,6 +24,8 @@ import (

"errors"
)

var errUnsupported = errors.New("unsupported on non-Linux systems")

// ExecInSnapdOrCoreSnap makes sure you're executing the binary that ships in


// the snapd/core snap.
// On this OS this is a stub.

@@ -36,5 +38,12 @@ func ExecInSnapdOrCoreSnap() {

//
// On this OS this is a stub and always returns an error.
func InternalToolPath(tool string) (string, error) {
return "", errors.New("unsupported on non-Linux systems")
return "", errUnsupported
}

// IsReexecd returns true when the current process binary is running from a snap.
//
// On this OS this is a stub and always returns an error.
func IsReexecd() (bool, error) {
return false, errUnsupported
}

 32  snapdtool/tool_test.go 

@@ -417,3 +417,35 @@ func (s *toolSuite) TestExecInSnapdOrCoreSnapDisabled(c *C) {

snapdtool.ExecInSnapdOrCoreSnap()
c.Check(s.execCalled, Equals, 0)
}

func (s *toolSuite) TestIsReexecd(c *C) {


mockedSelfExe := filepath.Join(s.fakeroot, "proc/self/exe")
restore := snapdtool.MockSelfExe(mockedSelfExe)
defer restore()

// pretend the binary reexecd from snap mount location


err := os.Symlink(filepath.Join(s.snapdPath, "usr/lib/snapd/snapd"), mockedSelfExe
c.Assert(err, IsNil)

is, err := snapdtool.IsReexecd()


c.Assert(err, IsNil)
c.Assert(is, Equals, true)

err = os.Remove(mockedSelfExe)
c.Assert(err, IsNil)
// now it's not
err = os.Symlink(filepath.Join(dirs.DistroLibExecDir, "snapd"), mockedSelfExe)
c.Assert(err, IsNil)

is, err = snapdtool.IsReexecd()


c.Assert(err, IsNil)
c.Assert(is, Equals, false)
// trouble reading the symlink
err = os.Remove(mockedSelfExe)
c.Assert(err, IsNil)

is, err = snapdtool.IsReexecd()


c.Assert(err, ErrorMatches, ".*/proc/self/exe: no such fi

Vous aimerez peut-être aussi