1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2017-2020 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 snapdtool_test
import (
"fmt"
"os"
"path/filepath"
"testing"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/dirs/dirstest"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/snapdtool"
"github.com/snapcore/snapd/testutil"
)
func Test(t *testing.T) { TestingT(t) }
type toolSuite struct {
testutil.BaseTest
execCalled int
lastExecArgv0 string
lastExecArgv []string
lastExecEnvv []string
fakeroot string
snapdPath string
corePath string
}
var _ = Suite(&toolSuite{})
func (s *toolSuite) SetUpTest(c *C) {
s.BaseTest.SetUpTest(c)
s.AddCleanup(snapdtool.MockSyscallExec(s.syscallExec))
_, restore := logger.MockLogger()
s.AddCleanup(restore)
s.AddCleanup(release.MockReleaseInfo(&release.OS{ID: "ubuntu"}))
s.execCalled = 0
s.lastExecArgv0 = ""
s.lastExecArgv = nil
s.lastExecEnvv = nil
s.fakeroot = c.MkDir()
dirs.SetRootDir(s.fakeroot)
s.AddCleanup(func() { dirs.SetRootDir("/") })
s.snapdPath = filepath.Join(dirs.SnapMountDir, "/snapd/42")
s.corePath = filepath.Join(dirs.SnapMountDir, "/core/21")
c.Assert(os.MkdirAll(filepath.Join(s.fakeroot, "proc/self"), 0755), IsNil)
}
func (s *toolSuite) syscallExec(argv0 string, argv []string, envv []string) (err error) {
s.execCalled++
s.lastExecArgv0 = argv0
s.lastExecArgv = argv
s.lastExecEnvv = envv
return fmt.Errorf(">exec of %q in tests<", argv0)
}
func (s *toolSuite) fakeCoreVersion(c *C, coreDir, version string) {
p := filepath.Join(coreDir, "/usr/lib/snapd")
c.Assert(os.MkdirAll(p, 0755), IsNil)
c.Assert(os.WriteFile(filepath.Join(p, "info"), []byte("VERSION="+version), 0644), IsNil)
}
func makeFakeExe(c *C, path string) {
err := os.MkdirAll(filepath.Dir(path), 0755)
c.Assert(err, IsNil)
err = os.WriteFile(path, nil, 0755)
c.Assert(err, IsNil)
}
func (s *toolSuite) fakeInternalTool(c *C, coreDir, toolName string) string {
s.fakeCoreVersion(c, coreDir, "42")
p := filepath.Join(coreDir, "/usr/lib/snapd", toolName)
makeFakeExe(c, p)
return p
}
func (s *toolSuite) mockReExecingEnv() func() {
restore := []func(){
release.MockOnClassic(true),
snapdtool.MockCoreSnapdPaths(s.corePath, s.snapdPath),
snapdtool.MockVersion("2"),
}
return func() {
for i := len(restore) - 1; i >= 0; i-- {
restore[i]()
}
}
}
func (s *toolSuite) mockReExecFor(c *C, coreDir, toolName, libexecDir string) func() {
selfExe := filepath.Join(s.fakeroot, "proc/self/exe")
restore := []func(){
s.mockReExecingEnv(),
snapdtool.MockSelfExe(selfExe),
}
s.fakeInternalTool(c, coreDir, toolName)
c.Assert(os.Symlink(filepath.Join(s.fakeroot, libexecDir, toolName), selfExe), IsNil)
return func() {
for i := len(restore) - 1; i >= 0; i-- {
restore[i]()
}
}
}
func (s *toolSuite) TestDistroSupportsReExec(c *C) {
restore := release.MockOnClassic(true)
defer restore()
// Some distributions don't support re-execution yet.
for _, id := range []string{"fedora", "centos", "rhel", "opensuse", "suse", "poky"} {
restore = release.MockReleaseInfo(&release.OS{ID: id})
defer restore()
c.Check(snapdtool.DistroSupportsReExec(), Equals, false, Commentf("ID: %q", id))
}
// While others do.
for _, id := range []string{"debian", "ubuntu"} {
restore = release.MockReleaseInfo(&release.OS{ID: id})
defer restore()
c.Check(snapdtool.DistroSupportsReExec(), Equals, true, Commentf("ID: %q", id))
}
}
func (s *toolSuite) TestNonClassicDistroNoSupportsReExec(c *C) {
restore := release.MockOnClassic(false)
defer restore()
// no distro supports re-exec when not on classic :-)
for _, id := range []string{
"fedora", "centos", "rhel", "opensuse", "suse", "poky",
"debian", "ubuntu", "arch", "archlinux",
} {
restore = release.MockReleaseInfo(&release.OS{ID: id})
defer restore()
c.Check(snapdtool.DistroSupportsReExec(), Equals, false, Commentf("ID: %q", id))
}
}
func (s *toolSuite) TestSystemSnapSupportsReExecNoInfo(c *C) {
// there's no snapd/info in a just-created tmpdir :-p
c.Check(snapdtool.SystemSnapSupportsReExec(c.MkDir()), Equals, false)
}
func (s *toolSuite) TestSystemSnapSupportsReExecBadInfo(c *C) {
// can't read snapd/info if it's a directory
p := s.snapdPath + "/usr/lib/snapd/info"
c.Assert(os.MkdirAll(p, 0755), IsNil)
c.Check(snapdtool.SystemSnapSupportsReExec(s.snapdPath), Equals, false)
}
func (s *toolSuite) TestSystemSnapSupportsReExecBadInfoContent(c *C) {
// can't understand snapd/info if all it holds are potatoes
p := s.snapdPath + "/usr/lib/snapd"
c.Assert(os.MkdirAll(p, 0755), IsNil)
c.Assert(os.WriteFile(p+"/info", []byte("potatoes"), 0644), IsNil)
c.Check(snapdtool.SystemSnapSupportsReExec(s.snapdPath), Equals, false)
}
func (s *toolSuite) TestSystemSnapSupportsReExecBadVersion(c *C) {
// can't understand snapd/info if all its version is gibberish
s.fakeCoreVersion(c, s.snapdPath, "0:")
c.Check(snapdtool.SystemSnapSupportsReExec(s.snapdPath), Equals, false)
}
func (s *toolSuite) TestSystemSnapSupportsReExecOldVersion(c *C) {
// can't re-exec if core version is too old
defer snapdtool.MockVersion("2")()
s.fakeCoreVersion(c, s.snapdPath, "0")
c.Check(snapdtool.SystemSnapSupportsReExec(s.snapdPath), Equals, false)
}
func (s *toolSuite) TestSystemSnapSupportsReExec(c *C) {
defer snapdtool.MockVersion("2")()
s.fakeCoreVersion(c, s.snapdPath, "9999")
c.Check(snapdtool.SystemSnapSupportsReExec(s.snapdPath), Equals, true)
}
func (s *toolSuite) TestInternalToolPathNoReexec(c *C) {
restore := snapdtool.MockOsReadlink(func(string) (string, error) {
return filepath.Join(dirs.DistroLibExecDir, "snapd"), nil
})
defer restore()
path, err := snapdtool.InternalToolPath("potato")
c.Check(err, IsNil)
c.Check(path, Equals, filepath.Join(dirs.DistroLibExecDir, "potato"))
}
func (s *toolSuite) TestInternalToolPathWithReexec(c *C) {
s.fakeInternalTool(c, s.snapdPath, "potato")
restore := snapdtool.MockOsReadlink(func(string) (string, error) {
return filepath.Join(s.snapdPath, "/usr/lib/snapd/snapd"), nil
})
defer restore()
path, err := snapdtool.InternalToolPath("potato")
c.Check(err, IsNil)
c.Check(path, Equals, filepath.Join(dirs.SnapMountDir, "snapd/42/usr/lib/snapd/potato"))
}
func (s *toolSuite) TestInternalToolPathWithOtherLocation(c *C) {
tmpdir := c.MkDir()
restore := snapdtool.MockOsReadlink(func(string) (string, error) {
return filepath.Join(tmpdir, "/tmp/tmp.foo_1234/usr/lib/snapd/snapd"), nil
})
defer restore()
devTool := filepath.Join(tmpdir, "/tmp/tmp.foo_1234/usr/lib/snapd/potato")
makeFakeExe(c, devTool)
path, err := snapdtool.InternalToolPath("potato")
c.Check(err, IsNil)
c.Check(path, Equals, tmpdir+"/tmp/tmp.foo_1234/usr/lib/snapd/potato")
}
func (s *toolSuite) TestInternalToolSnapPathWithOtherLocation(c *C) {
tmpdir := c.MkDir()
restore := snapdtool.MockOsReadlink(func(string) (string, error) {
return filepath.Join(tmpdir, "/tmp/tmp.foo_1234/usr/bin/snap"), nil
})
defer restore()
devTool := filepath.Join(tmpdir, "/tmp/tmp.foo_1234/usr/lib/snapd/potato")
makeFakeExe(c, devTool)
path, err := snapdtool.InternalToolPath("potato")
c.Check(err, IsNil)
c.Check(path, Equals, tmpdir+"/tmp/tmp.foo_1234/usr/lib/snapd/potato")
}
func (s *toolSuite) TestInternalToolPathWithOtherCrazyLocation(c *C) {
tmpdir := c.MkDir()
s.fakeInternalTool(c, filepath.Join(tmpdir, "/usr/foo/usr/tmp/tmp.foo_1234"), "potato")
restore := snapdtool.MockOsReadlink(func(string) (string, error) {
return filepath.Join(tmpdir, "/usr/foo/usr/tmp/tmp.foo_1234/usr/bin/snap"), nil
})
defer restore()
path, err := snapdtool.InternalToolPath("potato")
c.Check(err, IsNil)
c.Check(path, Equals, tmpdir+"/usr/foo/usr/tmp/tmp.foo_1234/usr/lib/snapd/potato")
}
func (s *toolSuite) TestInternalToolPathWithDevLocationFallback(c *C) {
restore := snapdtool.MockOsReadlink(func(string) (string, error) {
return filepath.Join("/home/dev/snapd/snapd"), nil
})
defer restore()
path, err := snapdtool.InternalToolPath("potato")
c.Check(err, IsNil)
c.Check(path, Equals, filepath.Join(dirs.DistroLibExecDir, "potato"))
}
func (s *toolSuite) TestInternalToolPathWithOtherDevLocationWhenExecutableFallback(c *C) {
restore := snapdtool.MockOsReadlink(func(string) (string, error) {
return filepath.Join(dirs.GlobalRootDir, "/tmp/snapd"), nil
})
defer restore()
devTool := filepath.Join(dirs.GlobalRootDir, "/tmp/potato")
err := os.MkdirAll(filepath.Dir(devTool), 0755)
c.Assert(err, IsNil)
err = os.WriteFile(devTool, []byte(""), 0755)
c.Assert(err, IsNil)
path, err := snapdtool.InternalToolPath("potato")
c.Check(err, IsNil)
c.Check(path, Equals, filepath.Join(dirs.DistroLibExecDir, "potato"))
}
func (s *toolSuite) TestInternalToolPathWithOtherDevLocationNonExecutable(c *C) {
restore := snapdtool.MockOsReadlink(func(string) (string, error) {
return filepath.Join(dirs.GlobalRootDir, "/tmp/snapd"), nil
})
defer restore()
devTool := filepath.Join(dirs.GlobalRootDir, "/tmp/potato")
makeFakeExe(c, devTool)
path, err := snapdtool.InternalToolPath("non-executable-potato")
c.Check(err, IsNil)
c.Check(path, Equals, filepath.Join(dirs.DistroLibExecDir, "non-executable-potato"))
}
func (s *toolSuite) TestInternalToolPathSnapdPathReexec(c *C) {
s.fakeInternalTool(c, filepath.Join(dirs.SnapMountDir, "core/111"), "snapd")
restore := snapdtool.MockOsReadlink(func(string) (string, error) {
return filepath.Join(dirs.SnapMountDir, "core/111/usr/bin/snap"), nil
})
defer restore()
p, err := snapdtool.InternalToolPath("snapd")
c.Assert(err, IsNil)
c.Check(p, Equals, filepath.Join(dirs.SnapMountDir, "/core/111/usr/lib/snapd/snapd"))
}
func (s *toolSuite) TestInternalToolPathSnapdSnap(c *C) {
s.fakeInternalTool(c, filepath.Join(dirs.SnapMountDir, "snapd/22"), "snapd")
restore := snapdtool.MockOsReadlink(func(string) (string, error) {
return filepath.Join(dirs.SnapMountDir, "snapd/22/usr/bin/snap"), nil
})
defer restore()
p, err := snapdtool.InternalToolPath("snapd")
c.Assert(err, IsNil)
c.Check(p, Equals, filepath.Join(dirs.SnapMountDir, "/snapd/22/usr/lib/snapd/snapd"))
}
func (s *toolSuite) TestInternalToolPathSnapdSnapNotExecutable(c *C) {
snapdMountDir := filepath.Join(dirs.SnapMountDir, "snapd/22")
snapdSnapInternalToolPath := filepath.Join(snapdMountDir, "/usr/lib/snapd/snapd")
s.fakeInternalTool(c, snapdMountDir, "snapd")
restore := snapdtool.MockOsReadlink(func(string) (string, error) {
return snapdSnapInternalToolPath, nil
})
defer restore()
// make snapd *not* executable
c.Assert(os.Chmod(snapdSnapInternalToolPath, 0644), IsNil)
// Now the internal tool path falls back to the global snapd because
// the internal one is not executable
p, err := snapdtool.InternalToolPath("snapd")
c.Assert(err, IsNil)
c.Check(p, Equals, filepath.Join(dirs.DistroLibExecDir, "snapd"))
}
func (s *toolSuite) TestInternalToolPathWithLibexecdirLocation(c *C) {
dirstest.MustMockAltLibExecDir(s.fakeroot)
dirs.SetRootDir(s.fakeroot)
restore := snapdtool.MockOsReadlink(func(string) (string, error) {
return filepath.Join("/usr/bin/snap"), nil
})
defer restore()
path, err := snapdtool.InternalToolPath("potato")
c.Check(err, IsNil)
c.Check(dirs.StripRootDir(path), Equals, filepath.Join("/usr/libexec/snapd/potato"))
}
func (s *toolSuite) TestExecInSnapdOrCoreSnap(c *C) {
defer s.mockReExecFor(c, s.snapdPath, "potato", dirs.DefaultDistroLibexecDir)()
c.Check(snapdtool.ExecInSnapdOrCoreSnap, PanicMatches, `>exec of "[^"]+/potato" in tests<`)
c.Check(s.execCalled, Equals, 1)
c.Check(s.lastExecArgv0, Equals, filepath.Join(s.snapdPath, "/usr/lib/snapd/potato"))
c.Check(s.lastExecArgv, DeepEquals, os.Args)
}
func (s *toolSuite) TestExecInSnapdOrCoreSnapFixArg0(c *C) {
defer s.mockReExecFor(c, s.snapdPath, "snapdtool.test", dirs.DefaultDistroLibexecDir)()
c.Check(snapdtool.ExecInSnapdOrCoreSnap, PanicMatches, `>exec of "[^"]+/snapdtool.test" in tests<`)
c.Check(s.execCalled, Equals, 1)
c.Check(s.lastExecArgv0, Equals, filepath.Join(s.snapdPath, "/usr/lib/snapd/snapdtool.test"))
c.Check(s.lastExecArgv[0], Equals, filepath.Join(s.snapdPath, "/usr/lib/snapd/snapdtool.test"))
c.Check(s.lastExecArgv[1:], DeepEquals, os.Args[1:])
}
func (s *toolSuite) TestExecInOldCoreSnap(c *C) {
defer s.mockReExecFor(c, s.corePath, "potato", dirs.DefaultDistroLibexecDir)()
c.Check(snapdtool.ExecInSnapdOrCoreSnap, PanicMatches, `>exec of "[^"]+/potato" in tests<`)
c.Check(s.execCalled, Equals, 1)
c.Check(s.lastExecArgv0, Equals, filepath.Join(s.corePath, "/usr/lib/snapd/potato"))
c.Check(s.lastExecArgv, DeepEquals, os.Args)
}
func (s *toolSuite) TestExecInSnapdOrCoreSnapBailsNoCoreSupport(c *C) {
defer s.mockReExecFor(c, s.snapdPath, "potato", dirs.DefaultDistroLibexecDir)()
// no "info" -> no core support:
c.Assert(os.Remove(filepath.Join(s.snapdPath, "/usr/lib/snapd/info")), IsNil)
snapdtool.ExecInSnapdOrCoreSnap()
c.Check(s.execCalled, Equals, 0)
}
func (s *toolSuite) TestExecInSnapdOrCoreSnapMissingExe(c *C) {
defer s.mockReExecFor(c, s.snapdPath, "potato", dirs.DefaultDistroLibexecDir)()
// missing exe:
c.Assert(os.Remove(filepath.Join(s.snapdPath, "/usr/lib/snapd/potato")), IsNil)
snapdtool.ExecInSnapdOrCoreSnap()
c.Check(s.execCalled, Equals, 0)
}
func (s *toolSuite) TestExecInSnapdOrCoreSnapBadSelfExe(c *C) {
defer s.mockReExecFor(c, s.snapdPath, "potato", dirs.DefaultDistroLibexecDir)()
// missing self/exe:
c.Assert(os.Remove(filepath.Join(s.fakeroot, "proc/self/exe")), IsNil)
snapdtool.ExecInSnapdOrCoreSnap()
c.Check(s.execCalled, Equals, 0)
}
func (s *toolSuite) TestExecInSnapdOrCoreSnapBailsNoDistroSupport(c *C) {
snapReexec := os.Getenv("SNAP_REEXEC")
defer os.Setenv("SNAP_REEXEC", snapReexec)
err := os.Unsetenv("SNAP_REEXEC")
c.Assert(err, IsNil)
defer s.mockReExecFor(c, s.snapdPath, "potato", dirs.DefaultDistroLibexecDir)()
// no distro support:
defer release.MockOnClassic(false)()
snapdtool.ExecInSnapdOrCoreSnap()
c.Check(s.execCalled, Equals, 0)
}
func (s *toolSuite) TestExecInSnapdOrCoreSnapNoDouble(c *C) {
selfExe := filepath.Join(s.fakeroot, "proc/self/exe")
err := os.Symlink(filepath.Join(s.fakeroot, "/snap/core/42/usr/lib/snapd"), selfExe)
c.Assert(err, IsNil)
snapdtool.MockSelfExe(selfExe)
snapdtool.ExecInSnapdOrCoreSnap()
c.Check(s.execCalled, Equals, 0)
}
func (s *toolSuite) TestExecInSnapdOrCoreSnapDisabled(c *C) {
defer s.mockReExecFor(c, s.snapdPath, "potato", dirs.DefaultDistroLibexecDir)()
os.Setenv("SNAP_REEXEC", "0")
defer os.Unsetenv("SNAP_REEXEC")
snapdtool.ExecInSnapdOrCoreSnap()
c.Check(s.execCalled, Equals, 0)
}
func (s *toolSuite) testExecInSnapdOrCoreSnapOnUnsupportedDistro(c *C, libexecDir string) {
// distro which does not support reexec
defer release.MockReleaseInfo(&release.OS{ID: "arch"})()
dirs.SetRootDir(s.fakeroot)
s.snapdPath = filepath.Join(dirs.SnapMountDir, "/snapd/42")
s.corePath = filepath.Join(dirs.SnapMountDir, "/core/21")
defer snapdtool.MockCoreSnapdPaths(s.corePath, s.snapdPath)()
// set up desired libexecdir
defer s.mockReExecFor(c, s.snapdPath, "potato", libexecDir)()
// reexec does not happen
snapdtool.ExecInSnapdOrCoreSnap()
c.Check(s.execCalled, Equals, 0)
// unless explicitly requested through the environment
os.Setenv("SNAP_REEXEC", "1")
defer os.Unsetenv("SNAP_REEXEC")
// in which case we do reexec
c.Check(snapdtool.ExecInSnapdOrCoreSnap, PanicMatches, `>exec of "[^"]+/potato" in tests<`)
c.Check(s.execCalled, Equals, 1)
// and reexec uses the correct mount path
c.Check(s.lastExecArgv0, Equals, filepath.Join(s.fakeroot, "/var/lib/snapd/snap/snapd/42/usr/lib/snapd/potato"))
c.Check(s.lastExecArgv, DeepEquals, os.Args)
}
func (s *toolSuite) TestExecInSnapdOrCoreSnapOnUnsupportedDistro(c *C) {
s.testExecInSnapdOrCoreSnapOnUnsupportedDistro(c, dirs.DefaultDistroLibexecDir)
}
func (s *toolSuite) TestExecInSnapdOrCoreSnapOnUnsupportedDistroAltLibexecdir(c *C) {
s.testExecInSnapdOrCoreSnapOnUnsupportedDistro(c, dirs.AltDistroLibexecDir)
}
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 file or directory")
c.Assert(is, Equals, false)
}
func (s *toolSuite) TestInReexecEnabled(c *C) {
defer os.Unsetenv("SNAP_REEXEC")
// explicitly disabled
os.Setenv("SNAP_REEXEC", "0")
c.Assert(snapdtool.IsReexecEnabled(), Equals, false)
// default to true
os.Unsetenv("SNAP_REEXEC")
c.Assert(snapdtool.IsReexecEnabled(), Equals, true)
// explicitly enabled
os.Setenv("SNAP_REEXEC", "1")
c.Assert(snapdtool.IsReexecEnabled(), Equals, true)
}
func (s *toolSuite) TestExeAndRoot(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)
root, exe, err := snapdtool.ExeAndRoot()
c.Assert(err, IsNil)
c.Assert(root, Equals, s.snapdPath)
c.Assert(exe, Equals, "usr/lib/snapd/snapd")
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)
root, exe, err = snapdtool.ExeAndRoot()
c.Assert(err, IsNil)
c.Assert(root, Equals, dirs.GlobalRootDir)
// distro libexecdir without the root part
noRootPrefix, err := filepath.Rel(dirs.GlobalRootDir, dirs.DistroLibExecDir)
c.Assert(err, IsNil)
c.Assert(exe, Equals, filepath.Join(noRootPrefix, "snapd"))
// trouble reading the symlink
err = os.Remove(mockedSelfExe)
c.Assert(err, IsNil)
root, exe, err = snapdtool.ExeAndRoot()
c.Assert(err, ErrorMatches, ".*/proc/self/exe: no such file or directory")
c.Check(root, Equals, "")
c.Check(exe, Equals, "")
err = os.Symlink(filepath.Join(dirs.SnapMountDir, "abc/def"), mockedSelfExe)
c.Assert(err, IsNil)
root, exe, err = snapdtool.ExeAndRoot()
c.Check(err, ErrorMatches, `cannot parse snap tool path ".*/snap/abc/def"`)
c.Check(root, Equals, "")
c.Check(exe, Equals, "")
}
|