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
difs,interfaces/mount: add support for locking namespaces #3311
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2017 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 mount | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"syscall" | ||
|
||
"github.com/snapcore/snapd/dirs" | ||
) | ||
|
||
// lockFileName returns the name of the lock file for the given snap. | ||
func lockFileName(snapName string) string { | ||
return filepath.Join(dirs.SnapRunLockDir, fmt.Sprintf("%s.lock", snapName)) | ||
} | ||
|
||
// NSLock describes a lock on a mount namespace of a particular snap. | ||
type NSLock struct { | ||
file *os.File | ||
fname string | ||
} | ||
|
||
// OpenLock creates and opens a lock file associated with a particular snap. | ||
func OpenLock(snapName string) (*NSLock, error) { | ||
if err := os.MkdirAll(dirs.SnapRunLockDir, 0700); err != nil { | ||
return nil, fmt.Errorf("cannot create lock directory: %s", err) | ||
} | ||
fname := lockFileName(snapName) | ||
mode := syscall.O_RDWR | syscall.O_CREAT | syscall.O_NOFOLLOW | syscall.O_CLOEXEC | ||
file, err := os.OpenFile(fname, mode, os.FileMode(0600)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
l := &NSLock{fname: fname, file: file} | ||
return l, nil | ||
} | ||
|
||
// Path returns the path of the lock file. | ||
func (l *NSLock) Path() string { | ||
return l.fname | ||
} | ||
|
||
// Close closes the lock, unlocking it automatically if needed. | ||
func (l *NSLock) Close() error { | ||
return l.file.Close() | ||
} | ||
|
||
// Lock acquires an exclusive lock on the mount namespace. | ||
func (l *NSLock) Lock() error { | ||
return syscall.Flock(int(l.file.Fd()), syscall.LOCK_EX) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check l.file for not being nil |
||
} | ||
|
||
// Unlock releases an acquired lock. | ||
func (l *NSLock) Unlock() error { | ||
return syscall.Flock(int(l.file.Fd()), syscall.LOCK_UN) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check l.file for not being nil |
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2017 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 mount_test | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
|
||
. "gopkg.in/check.v1" | ||
|
||
"github.com/snapcore/snapd/dirs" | ||
"github.com/snapcore/snapd/interfaces/mount" | ||
) | ||
|
||
type lockSuite struct{} | ||
|
||
var _ = Suite(&lockSuite{}) | ||
|
||
func (s *lockSuite) SetUpTest(c *C) { | ||
dirs.SetRootDir(c.MkDir()) | ||
} | ||
|
||
func (s *lockSuite) TearDownTest(c *C) { | ||
dirs.SetRootDir("") | ||
} | ||
|
||
// Test that opening and closing a lock works as expected. | ||
func (s *lockSuite) TestOpenLock(c *C) { | ||
lock, err := mount.OpenLock("name") | ||
c.Assert(err, IsNil) | ||
defer lock.Close() | ||
|
||
_, err = os.Stat(lock.Path()) | ||
c.Assert(err, IsNil) | ||
} | ||
|
||
// Test that Lock and Unlock work as expected. | ||
func (s *lockSuite) TestLockUnlockWorks(c *C) { | ||
lock, err := mount.OpenLock("name") | ||
c.Assert(err, IsNil) | ||
defer lock.Close() | ||
|
||
// Run a flock command in another process, it should succeed because it can | ||
// lock the lock as we didn't do it yet. | ||
cmd := exec.Command("flock", "--exclusive", "--nonblock", lock.Path(), "true") | ||
c.Assert(cmd.Run(), IsNil) | ||
|
||
// Lock the lock. | ||
c.Assert(lock.Lock(), IsNil) | ||
|
||
// Run a flock command in another process, it should fail with the distinct | ||
// error code because we hold the lock already and we asked it not to block. | ||
cmd = exec.Command("flock", "--exclusive", "--nonblock", | ||
"--conflict-exit-code", "2", lock.Path(), "true") | ||
c.Assert(cmd.Run(), ErrorMatches, "exit status 2") | ||
|
||
// Unlock the lock. | ||
c.Assert(lock.Unlock(), IsNil) | ||
|
||
// Run a flock command in another process, it should succeed because it can | ||
// grab the lock again now. | ||
cmd = exec.Command("flock", "--exclusive", "--nonblock", lock.Path(), "true") | ||
c.Assert(cmd.Run(), IsNil) | ||
} | ||
|
||
// Test that locking a locked lock does nothing. | ||
func (s *lockSuite) TestLockLocked(c *C) { | ||
lock, err := mount.OpenLock("name") | ||
c.Assert(err, IsNil) | ||
defer lock.Close() | ||
|
||
// NOTE: technically this replaces the lock type but we only use LOCK_EX. | ||
c.Assert(lock.Lock(), IsNil) | ||
c.Assert(lock.Lock(), IsNil) | ||
} | ||
|
||
// Test that unlocking an unlocked lock does nothing. | ||
func (s *lockSuite) TestUnlockUnlocked(c *C) { | ||
lock, err := mount.OpenLock("name") | ||
c.Assert(err, IsNil) | ||
defer lock.Close() | ||
|
||
c.Assert(lock.Unlock(), IsNil) | ||
} | ||
|
||
// Test that locking or unlocking a closed lock fails. | ||
func (s *lockSuite) TestUsingClosedLock(c *C) { | ||
lock, err := mount.OpenLock("name") | ||
c.Assert(err, IsNil) | ||
lock.Close() | ||
|
||
c.Assert(lock.Lock(), ErrorMatches, "bad file descriptor") | ||
c.Assert(lock.Unlock(), ErrorMatches, "bad file descriptor") | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth to check if the file is really open and return an error if it is not? If l.file is nil then this will result in a SEGV.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
l.file
can never benil
because onlyOpenFile
can create an instance ofl
and it checks for errors already.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very much depends on the OpenFile implementation. Feel better when we check l.file as well. But fine if you don't want that. Didn't blocked because of this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't do it because we don't have a habit of checking object fields for being
nil
unless they can reasonably benil
as a normal case.