port.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // SiYuan - Refactor your thinking
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package server
  17. import (
  18. "fmt"
  19. "os"
  20. "os/exec"
  21. "path/filepath"
  22. "strconv"
  23. "strings"
  24. "time"
  25. "github.com/88250/gulu"
  26. goPS "github.com/mitchellh/go-ps"
  27. "github.com/siyuan-note/logging"
  28. "github.com/siyuan-note/siyuan/kernel/util"
  29. )
  30. func killRunningKernel() {
  31. defer logging.Recover()
  32. now := time.Now()
  33. defer logging.LogInfof("check running kernel elapsed [%dms]", time.Since(now).Milliseconds())
  34. processes, err := goPS.Processes()
  35. if nil != err {
  36. logging.LogErrorf("get processes failed: %s", err)
  37. killByPort(util.FixedPort)
  38. return
  39. }
  40. currentPid := os.Getpid()
  41. killed := false
  42. for _, process := range processes {
  43. if process.Pid() == currentPid {
  44. continue
  45. }
  46. procName := strings.ToLower(process.Executable())
  47. if strings.Contains(procName, "siyuan-kernel") {
  48. kill(fmt.Sprintf("%d", process.Pid()))
  49. killed = true
  50. }
  51. }
  52. if killed {
  53. portJSON := filepath.Join(util.HomeDir, ".config", "siyuan", "port.json")
  54. os.RemoveAll(portJSON)
  55. }
  56. }
  57. func killByPort(port string) {
  58. if !util.IsPortOpen(port) {
  59. return
  60. }
  61. portJSON := filepath.Join(util.HomeDir, ".config", "siyuan", "port.json")
  62. os.RemoveAll(portJSON)
  63. pid := pidByPort(port)
  64. if "" == pid {
  65. return
  66. }
  67. pidInt, _ := strconv.Atoi(pid)
  68. proc, _ := goPS.FindProcess(pidInt)
  69. var name string
  70. if nil != proc {
  71. name = proc.Executable()
  72. }
  73. kill(pid)
  74. logging.LogInfof("killed process [name=%s, pid=%s]", name, pid)
  75. }
  76. func kill(pid string) {
  77. var killCmd *exec.Cmd
  78. if gulu.OS.IsWindows() {
  79. killCmd = exec.Command("cmd", "/c", "TASKKILL /F /PID "+pid)
  80. } else {
  81. killCmd = exec.Command("kill", "-9", pid)
  82. }
  83. gulu.CmdAttr(killCmd)
  84. killCmd.CombinedOutput()
  85. }
  86. func pidByPort(port string) (ret string) {
  87. if gulu.OS.IsWindows() {
  88. cmd := exec.Command("cmd", "/c", "netstat -ano | findstr "+port)
  89. gulu.CmdAttr(cmd)
  90. data, err := cmd.CombinedOutput()
  91. if nil != err {
  92. logging.LogErrorf("netstat failed: %s", err)
  93. return
  94. }
  95. output := string(data)
  96. lines := strings.Split(output, "\n")
  97. for _, l := range lines {
  98. if strings.Contains(l, "LISTENING") {
  99. l = l[strings.Index(l, "LISTENING")+len("LISTENING"):]
  100. l = strings.TrimSpace(l)
  101. ret = l
  102. return
  103. }
  104. }
  105. return
  106. }
  107. cmd := exec.Command("lsof", "-Fp", "-i", ":"+port)
  108. gulu.CmdAttr(cmd)
  109. data, err := cmd.CombinedOutput()
  110. if nil != err {
  111. logging.LogErrorf("lsof failed: %s", err)
  112. return
  113. }
  114. output := string(data)
  115. lines := strings.Split(output, "\n")
  116. for _, l := range lines {
  117. if strings.HasPrefix(l, "p") {
  118. l = l[1:]
  119. ret = l
  120. return
  121. }
  122. }
  123. return
  124. }