mirror of
https://github.com/anyproto/anytype-heart.git
synced 2025-06-10 18:10:49 +09:00
GO-866 Protect process with a lockfile
This commit is contained in:
parent
875dc61da5
commit
e42420f62d
5 changed files with 641 additions and 20 deletions
|
@ -43,6 +43,7 @@ import (
|
|||
"github.com/anytypeio/go-anytype-middleware/pkg/lib/pb/model"
|
||||
"github.com/anytypeio/go-anytype-middleware/space"
|
||||
"github.com/anytypeio/go-anytype-middleware/util/files"
|
||||
"github.com/anytypeio/go-anytype-middleware/util/osprocess"
|
||||
"github.com/anytypeio/go-anytype-middleware/util/pbtypes"
|
||||
)
|
||||
|
||||
|
@ -354,6 +355,10 @@ func (mw *Middleware) AccountRecover(cctx context.Context, _ *pb.RpcAccountRecov
|
|||
}
|
||||
|
||||
func (mw *Middleware) AccountSelect(cctx context.Context, req *pb.RpcAccountSelectRequest) *pb.RpcAccountSelectResponse {
|
||||
err := osprocess.Lock()
|
||||
if err != nil {
|
||||
log.Warnf("accountselect lock failed: %v", err)
|
||||
}
|
||||
response := func(account *model.Account, code pb.RpcAccountSelectResponseErrorCode, err error) *pb.RpcAccountSelectResponse {
|
||||
var clientConfig *pb.RpcAccountConfig
|
||||
if account != nil {
|
||||
|
|
|
@ -5,12 +5,14 @@ import (
|
|||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"github.com/anytypeio/any-sync/util/crypto"
|
||||
"os"
|
||||
|
||||
"github.com/anytypeio/any-sync/util/crypto"
|
||||
|
||||
"github.com/anytypeio/go-anytype-middleware/core/session"
|
||||
"github.com/anytypeio/go-anytype-middleware/pb"
|
||||
"github.com/anytypeio/go-anytype-middleware/pkg/lib/core"
|
||||
"github.com/anytypeio/go-anytype-middleware/util/osprocess"
|
||||
)
|
||||
|
||||
const wordCount int = 12
|
||||
|
@ -59,6 +61,10 @@ func (mw *Middleware) setMnemonic(mnemonic string) error {
|
|||
}
|
||||
|
||||
func (mw *Middleware) WalletRecover(cctx context.Context, req *pb.RpcWalletRecoverRequest) *pb.RpcWalletRecoverResponse {
|
||||
err := osprocess.Lock()
|
||||
if err != nil {
|
||||
log.Warnf("walletrecover lock failed: %v", err)
|
||||
}
|
||||
response := func(code pb.RpcWalletRecoverResponseErrorCode, err error) *pb.RpcWalletRecoverResponse {
|
||||
m := &pb.RpcWalletRecoverResponse{Error: &pb.RpcWalletRecoverResponseError{Code: code}}
|
||||
if err != nil {
|
||||
|
|
12
go.mod
12
go.mod
|
@ -69,6 +69,7 @@ require (
|
|||
github.com/multiformats/go-multiaddr-dns v0.3.1
|
||||
github.com/multiformats/go-multibase v0.2.0
|
||||
github.com/multiformats/go-multihash v0.2.1
|
||||
github.com/nightlyone/lockfile v1.0.0
|
||||
github.com/opentracing/opentracing-go v1.2.0
|
||||
github.com/otiai10/copy v1.9.0
|
||||
github.com/otiai10/opengraph/v2 v2.1.0
|
||||
|
@ -78,6 +79,7 @@ require (
|
|||
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd
|
||||
github.com/samber/lo v1.36.0
|
||||
github.com/sasha-s/go-deadlock v0.3.1
|
||||
github.com/shirou/gopsutil/v3 v3.23.3
|
||||
github.com/spf13/cobra v1.4.0
|
||||
github.com/stretchr/testify v1.8.2
|
||||
github.com/textileio/go-ds-badger3 v0.1.0
|
||||
|
@ -107,6 +109,16 @@ require (
|
|||
github.com/tyler-smith/go-bip39 v1.1.0 // indirect
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/go-ole/go-ole v1.2.6 // indirect
|
||||
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
|
||||
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
|
||||
github.com/shoenig/go-m1cpu v0.1.4 // indirect
|
||||
github.com/tklauser/go-sysconf v0.3.11 // indirect
|
||||
github.com/tklauser/numcpus v0.6.0 // indirect
|
||||
github.com/yusufpapurcu/wmi v1.2.2 // indirect
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/AndreasBriese/bbloom v0.0.0-20190825152654-46b345b51c96 // indirect
|
||||
github.com/HdrHistogram/hdrhistogram-go v1.1.0 // indirect
|
||||
|
|
69
util/osprocess/process.go
Normal file
69
util/osprocess/process.go
Normal file
|
@ -0,0 +1,69 @@
|
|||
package osprocess
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/nightlyone/lockfile"
|
||||
"github.com/samber/lo"
|
||||
"github.com/shirou/gopsutil/v3/process"
|
||||
)
|
||||
|
||||
func Lock() error {
|
||||
exePath, err := os.Executable()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cwd := filepath.Dir(exePath)
|
||||
lock, err := lockfile.New(filepath.Join(cwd, "lock.lck"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
killOldProcess(lock, exePath)
|
||||
|
||||
return lock.TryLock()
|
||||
}
|
||||
|
||||
func ProcessByPid(pid int) (*process.Process, error) {
|
||||
processes, err := process.Processes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
item, found := lo.Find(
|
||||
processes,
|
||||
func(item *process.Process) bool { return int(item.Pid) == pid },
|
||||
)
|
||||
|
||||
if found {
|
||||
return item, nil
|
||||
}
|
||||
return nil, fmt.Errorf("process not found")
|
||||
}
|
||||
|
||||
func isMyProcess(exePath string, process *process.Process) bool {
|
||||
processPath, err := process.Exe()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return processPath == exePath
|
||||
}
|
||||
|
||||
func killOldProcess(lock lockfile.Lockfile, exePath string) {
|
||||
oldProcess, _ := lock.GetOwner() //nolint:errcheck
|
||||
if oldProcess != nil {
|
||||
proc, err := ProcessByPid(oldProcess.Pid)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
isNotCurrentRun := os.Getpid() != oldProcess.Pid
|
||||
|
||||
if isNotCurrentRun && isMyProcess(exePath, proc) {
|
||||
_ = proc.Kill() //nolint:errcheck
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue