~mterry/snappy/selftest-reboot-notice

« back to all changes in this revision

Viewing changes to priv/filelock.go

  • Committer: Snappy Tarmac
  • Author(s): Michael Vogt
  • Date: 2015-06-16 10:32:05 UTC
  • mfrom: (510.1.3 snappy-with-decorator)
  • Revision ID: snappy_tarmac-20150616103205-czpa1fhc2cyr2c9p
Add priv.WithMutex() decorator to avoid duplicating code in cmd/snappy/cmd_*.go by mvo approved by chipaca

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- Mode: Go; indent-tabs-mode: t -*-
 
2
 
 
3
/*
 
4
 * Copyright (C) 2014-2015 Canonical Ltd
 
5
 *
 
6
 * This program is free software: you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License version 3 as
 
8
 * published by the Free Software Foundation.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
 *
 
18
 */
 
19
 
 
20
package priv
 
21
 
 
22
import (
 
23
        "errors"
 
24
        "os"
 
25
        "syscall"
 
26
)
 
27
 
 
28
// ErrNotLocked is returned when an attempts is made to unlock an
 
29
// unlocked FileLock.
 
30
var ErrNotLocked = errors.New("not locked")
 
31
 
 
32
// FileLock is a Lock file object used to serialise access for
 
33
// privileged operations.
 
34
type FileLock struct {
 
35
        Filename string
 
36
        realFile *os.File
 
37
}
 
38
 
 
39
// NewFileLock creates a new lock object (but does not lock it).
 
40
func NewFileLock(path string) *FileLock {
 
41
        return &FileLock{Filename: path}
 
42
}
 
43
 
 
44
// Lock the FileLock object.
 
45
// Returns ErrAlreadyLocked if an existing lock is in place.
 
46
func (l *FileLock) Lock(blocking bool) error {
 
47
        var err error
 
48
 
 
49
        // XXX: don't try to create exclusively - we care if the file failed to
 
50
        // be created, but we don't care if it already existed as the lock
 
51
        // _on_ the file is the most important thing.
 
52
        flags := (os.O_CREATE | os.O_WRONLY)
 
53
 
 
54
        f, err := os.OpenFile(l.Filename, flags, 0600)
 
55
        if err != nil {
 
56
                return err
 
57
        }
 
58
        l.realFile = f
 
59
 
 
60
        // Note: we don't want to block if the lock is already held.
 
61
        how := syscall.LOCK_EX
 
62
        if !blocking {
 
63
                how |= syscall.LOCK_NB
 
64
        }
 
65
 
 
66
        if err = syscall.Flock(int(l.realFile.Fd()), how); err != nil {
 
67
                return ErrAlreadyLocked
 
68
        }
 
69
 
 
70
        return nil
 
71
}
 
72
 
 
73
// Unlock the FileLock object.
 
74
// Returns ErrNotLocked if no existing lock is in place.
 
75
func (l *FileLock) Unlock() error {
 
76
        if err := syscall.Flock(int(l.realFile.Fd()), syscall.LOCK_UN); err != nil {
 
77
                return ErrNotLocked
 
78
        }
 
79
 
 
80
        if err := l.realFile.Close(); err != nil {
 
81
                return err
 
82
        }
 
83
 
 
84
        filename := l.Filename
 
85
 
 
86
        // Invalidate
 
87
        l.realFile = nil
 
88
        l.Filename = ""
 
89
 
 
90
        return os.Remove(filename)
 
91
}