-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlock.go
More file actions
55 lines (50 loc) · 1.18 KB
/
Copy pathlock.go
File metadata and controls
55 lines (50 loc) · 1.18 KB
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
package daemonit
import (
"fmt"
"io/ioutil"
"os"
"os/user"
"strconv"
"strings"
"syscall"
)
func lock(arg0 string) error {
filename := lockfile(arg0)
if err := checkFileExists(filename); err != nil {
return err
}
pid := fmt.Sprintf("%d", os.Getpid())
return ioutil.WriteFile(filename, []byte(pid), 0644)
}
func lockfile(arg0 string) string {
var currentUser *user.User
var err error
processPath := strings.Split(arg0, "/")
if currentUser, err = user.Current(); err != nil {
currentUser = &user.User{
Uid: "0",
Gid: "0",
Username: "",
Name: "",
HomeDir: "/tmp",
}
}
return fmt.Sprintf("/tmp/%s.%s.lock", processPath[len(processPath)-1], currentUser.Username)
}
func checkFileExists(filename string) error {
if dat, err := ioutil.ReadFile(filename); err == nil {
if pid, err := strconv.ParseInt(string(dat), 10, 32); err == nil {
if process, err := os.FindProcess(int(pid)); err == nil {
if err := process.Signal(syscall.Signal(0)); err == nil {
return fmt.Errorf("process %d is running", pid)
}
}
}
} else if !os.IsNotExist(err) {
return err
}
return nil
}
func cleanupLock(arg0 string) {
os.Remove(lockfile(arg0))
}