OSDN Git Service

add dir lock
[bytom/bytom.git] / node / node.go
index 4bef9bc..74952d1 100644 (file)
@@ -2,6 +2,7 @@ package node
 
 import (
        "context"
+       "errors"
        "net/http"
        _ "net/http/pprof"
        "os"
@@ -9,6 +10,7 @@ import (
        "path/filepath"
        "time"
 
+       "github.com/prometheus/prometheus/util/flock"
        log "github.com/sirupsen/logrus"
        cmn "github.com/tendermint/tmlibs/common"
        dbm "github.com/tendermint/tmlibs/db"
@@ -61,6 +63,9 @@ type Node struct {
 
 func NewNode(config *cfg.Config) *Node {
        ctx := context.Background()
+       if err := lockDataDirectory(config); err != nil {
+               cmn.Exit("Error: " + err.Error())
+       }
        initLogFile(config)
        initActiveNetParams(config)
        // Get store
@@ -145,6 +150,15 @@ func NewNode(config *cfg.Config) *Node {
        return node
 }
 
+// Lock data directory after daemonization
+func lockDataDirectory(config *cfg.Config) error {
+       _, _, err := flock.New(filepath.Join(config.RootDir, "LOCK"))
+       if err != nil {
+               return errors.New("datadir already used by another process")
+       }
+       return nil
+}
+
 func initActiveNetParams(config *cfg.Config) {
        var exist bool
        consensus.ActiveNetParams, exist = consensus.NetParams[config.ChainID]