forked from sevlyar/go-daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock_file.go
109 lines (94 loc) · 2.58 KB
/
lock_file.go
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
package daemon
import (
"errors"
"fmt"
"os"
)
var (
// ErrWouldBlock indicates on locking pid-file by another process.
ErrWouldBlock = errors.New("daemon: Resource temporarily unavailable")
)
// LockFile wraps *os.File and provide functions for locking of files.
type LockFile struct {
*os.File
}
// NewLockFile returns a new LockFile with the given File.
func NewLockFile(file *os.File) *LockFile {
return &LockFile{file}
}
// CreatePidFile opens the named file, applies exclusive lock and writes
// current process id to file.
func CreatePidFile(name string, perm os.FileMode) (lock *LockFile, err error) {
if lock, err = OpenLockFile(name, perm); err != nil {
return
}
if err = lock.Lock(); err != nil {
lock.Remove()
return
}
if err = lock.WritePid(); err != nil {
lock.Remove()
}
return
}
// OpenLockFile opens the named file with flags os.O_RDWR|os.O_CREATE and specified perm.
// If successful, function returns LockFile for opened file.
func OpenLockFile(name string, perm os.FileMode) (lock *LockFile, err error) {
var file *os.File
if file, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE, perm); err == nil {
lock = &LockFile{file}
}
return
}
// Lock apply exclusive lock on an open file. If file already locked, returns error.
func (file *LockFile) Lock() error {
return lockFile(file.Fd())
}
// Unlock remove exclusive lock on an open file.
func (file *LockFile) Unlock() error {
return unlockFile(file.Fd())
}
// ReadPidFile reads process id from file with give name and returns pid.
// If unable read from a file, returns error.
func ReadPidFile(name string) (pid int, err error) {
var file *os.File
if file, err = os.OpenFile(name, os.O_RDONLY, 0640); err != nil {
return
}
defer file.Close()
lock := &LockFile{file}
pid, err = lock.ReadPid()
return
}
// WritePid writes current process id to an open file.
func (file *LockFile) WritePid() (err error) {
if _, err = file.Seek(0, os.SEEK_SET); err != nil {
return
}
var fileLen int
if fileLen, err = fmt.Fprint(file, os.Getpid()); err != nil {
return
}
if err = file.Truncate(int64(fileLen)); err != nil {
return
}
err = file.Sync()
return
}
// ReadPid reads process id from file and returns pid.
// If unable read from a file, returns error.
func (file *LockFile) ReadPid() (pid int, err error) {
if _, err = file.Seek(0, os.SEEK_SET); err != nil {
return
}
_, err = fmt.Fscan(file, &pid)
return
}
// Remove removes lock, closes and removes an open file.
func (file *LockFile) Remove() error {
defer file.Close()
if err := file.Unlock(); err != nil {
return err
}
return os.Remove(file.Name())
}