2022-05-26 15:18:53 +08:00
|
|
|
// SiYuan - Build Your Eternal Digital Garden
|
|
|
|
|
// Copyright (c) 2020-present, b3log.org
|
|
|
|
|
//
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
|
|
|
|
"os/signal"
|
2023-01-06 18:18:04 +08:00
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2022-05-26 15:18:53 +08:00
|
|
|
"syscall"
|
2023-01-06 18:18:04 +08:00
|
|
|
"time"
|
2022-05-26 15:18:53 +08:00
|
|
|
|
2023-01-06 18:18:04 +08:00
|
|
|
goPS "github.com/mitchellh/go-ps"
|
2022-07-17 12:22:32 +08:00
|
|
|
"github.com/siyuan-note/logging"
|
2023-01-06 18:18:04 +08:00
|
|
|
"github.com/siyuan-note/siyuan/kernel/util"
|
2022-05-26 15:18:53 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func HandleSignal() {
|
|
|
|
|
c := make(chan os.Signal)
|
|
|
|
|
signal.Notify(c, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
|
|
|
|
|
s := <-c
|
2022-07-17 12:22:32 +08:00
|
|
|
logging.LogInfof("received os signal [%s], exit kernel process now", s)
|
2022-09-09 17:45:10 +08:00
|
|
|
Close(false, 1)
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
2023-01-06 18:18:04 +08:00
|
|
|
|
2023-01-26 23:30:29 +08:00
|
|
|
var firstRunHookDesktopUIProcJob = true
|
|
|
|
|
|
|
|
|
|
func HookDesktopUIProcJob() {
|
2023-01-06 18:37:33 +08:00
|
|
|
if util.ContainerStd != util.Container || "dev" == util.Mode {
|
2023-01-06 18:18:04 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-26 23:30:29 +08:00
|
|
|
if firstRunHookDesktopUIProcJob {
|
|
|
|
|
// 等待启动结束再
|
|
|
|
|
time.Sleep(30 * time.Second)
|
|
|
|
|
firstRunHookDesktopUIProcJob = false
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-27 15:35:30 +08:00
|
|
|
if 0 < util.CountSessions() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-06 18:27:27 +08:00
|
|
|
uiProcNames := []string{"siyuan", "electron"}
|
2023-01-06 18:18:04 +08:00
|
|
|
existUIProc := false
|
2023-01-26 23:30:29 +08:00
|
|
|
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
|
|
|
|
|
}
|
2023-01-06 18:18:04 +08:00
|
|
|
|
2023-01-26 23:30:29 +08:00
|
|
|
proc, err := goPS.FindProcess(uiProcID)
|
|
|
|
|
if nil != err {
|
|
|
|
|
logging.LogErrorf("find UI proc [%d] failed: %s", uiProcID, err)
|
|
|
|
|
return true
|
|
|
|
|
}
|
2023-01-06 18:40:30 +08:00
|
|
|
|
2023-01-26 23:30:29 +08:00
|
|
|
if nil == proc {
|
2023-01-06 18:18:04 +08:00
|
|
|
return true
|
2023-01-26 23:30:29 +08:00
|
|
|
}
|
2023-01-06 18:18:04 +08:00
|
|
|
|
2023-01-26 23:30:29 +08:00
|
|
|
procName := strings.ToLower(proc.Executable())
|
|
|
|
|
for _, name := range uiProcNames {
|
|
|
|
|
if strings.Contains(procName, name) {
|
|
|
|
|
existUIProc = true
|
|
|
|
|
return false
|
|
|
|
|
}
|
2023-01-06 18:18:04 +08:00
|
|
|
}
|
2023-01-26 23:30:29 +08:00
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if !existUIProc {
|
|
|
|
|
logging.LogInfof("no active UI proc, exit kernel process now")
|
|
|
|
|
Close(false, 1)
|
2023-01-06 18:18:04 +08:00
|
|
|
}
|
|
|
|
|
}
|