cmd/ipfs: Obtain lock when starting daemon
Matt Bell committed
Oct 27, 2014 at 16:20 UTC
e011d950bb71c4b0e32248058c31af43e32338a9
1 file changed
+28
-1
cmd/ipfs/daemon.go
+28
-1
@@ -1,12 +1,19 @@
1
package main
2
3
import (
4
+ "fmt"
5
"net/http"
6
7
+ "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/camlistore/lock"
8
+
9
cmds "github.com/jbenet/go-ipfs/commands"
10
cmdsHttp "github.com/jbenet/go-ipfs/commands/http"
11
+ "github.com/jbenet/go-ipfs/config"
12
)
13
14
+// DaemonLockFile is the filename of the daemon lock, relative to config dir
15
+const DaemonLockFile = "daemon.lock"
16
+
17
var Daemon = &cmds.Command{
18
Options: []cmds.Option{},
19
Help: "TODO",
@@ -15,13 +22,33 @@ var Daemon = &cmds.Command{
22
}
23
24
func daemonFunc(req cmds.Request, res cmds.Response) {
25
+ configPath, err := getConfigPath(req)
26
+ if err != nil {
27
+ res.SetError(err, cmds.ErrNormal)
28
+ return
29
+ }
30
+
31
+ lockPath, err := config.Path(configPath, DaemonLockFile)
32
+ if err != nil {
33
+ res.SetError(err, cmds.ErrNormal)
34
+ return
35
+ }
36
+
37
+ lk, err := lock.Lock(lockPath)
38
+ if err != nil {
39
+ res.SetError(fmt.Errorf("Couldn't obtain lock. Is another daemon already running?"), cmds.ErrNormal)
40
+ return
41
+ }
42
+ defer lk.Close()
43
+
44
handler := cmdsHttp.Handler{}
45
http.Handle(cmdsHttp.ApiPath+"/", handler)
46
// TODO: load listen address/port from config/options
21
- err := http.ListenAndServe(":8080", nil)
47
+ err = http.ListenAndServe(":8080", nil)
48
if err != nil {
49
res.SetError(err, cmds.ErrNormal)
50
return
51
}
52
// TODO: log to indicate that we are now listening
53
+
54
}