From 4ba967cc98b83e07286e3249a3fe03a33ae6a645 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 6 Jan 2023 18:18:04 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E6=A1=8C=E9=9D=A2=E7=AB=AF=E5=86=85?= =?UTF-8?q?=E6=A0=B8=E8=BF=9B=E7=A8=8B=E6=A0=B9=E6=8D=AE=20Electron=20?= =?UTF-8?q?=E4=B8=BB=E8=BF=9B=E7=A8=8B=E5=88=A4=E6=96=AD=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E9=80=80=E5=87=BA=20Fix=20https://github.com?= =?UTF-8?q?/siyuan-note/siyuan/issues/7002?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/main.go | 1 + kernel/model/process.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/kernel/main.go b/kernel/main.go index 69e3d6c9b..14f92c102 100644 --- a/kernel/main.go +++ b/kernel/main.go @@ -51,6 +51,7 @@ func main() { go sql.AutoFlushTreeQueue() go treenode.AutoFlushBlockTree() go cache.LoadAssets() + go model.HookDesktopUIProc() model.WatchAssets() model.HandleSignal() } diff --git a/kernel/model/process.go b/kernel/model/process.go index 7ce0f82e8..9ec6da9bb 100644 --- a/kernel/model/process.go +++ b/kernel/model/process.go @@ -19,9 +19,14 @@ package model import ( "os" "os/signal" + "strconv" + "strings" "syscall" + "time" + goPS "github.com/mitchellh/go-ps" "github.com/siyuan-note/logging" + "github.com/siyuan-note/siyuan/kernel/util" ) func HandleSignal() { @@ -31,3 +36,39 @@ func HandleSignal() { logging.LogInfof("received os signal [%s], exit kernel process now", s) Close(false, 1) } + +func HookDesktopUIProc() { + if util.ContainerStd != util.Container { + return + } + + time.Sleep(30 * time.Second) + existUIProc := false + for range time.Tick(7 * time.Second) { + util.UIProcessIDs.Range(func(uiProcIDArg, _ interface{}) bool { + uiProcID, err := strconv.Atoi(uiProcIDArg.(string)) + if nil != err { + logging.LogErrorf("invalid UI proc ID [%s]: %s", uiProcIDArg, err) + return true + } + + proc, err := goPS.FindProcess(uiProcID) + if nil != err { + logging.LogErrorf("find UI proc [%d] failed: %s", uiProcID, err) + return true + } + + if strings.Contains(strings.ToLower(proc.Executable()), "siyuan") { + existUIProc = true + return false + } + + return true + }) + + if !existUIProc { + logging.LogInfof("no active UI proc, exit kernel process now") + Close(false, 1) + } + } +}