-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdaemon_unix.go
More file actions
133 lines (116 loc) · 2.78 KB
/
Copy pathdaemon_unix.go
File metadata and controls
133 lines (116 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//go:build !windows
package main
import (
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
"time"
)
func findRunningPIDs() []int {
myPID := os.Getpid()
out, err := exec.Command("pgrep", "-f", "my[aA]i[rR]outer").Output()
var pids []int
if err == nil {
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
if p, e := strconv.Atoi(line); e == nil && p != myPID {
pids = append(pids, p)
}
}
}
return pids
}
func showStatus() {
pids := findRunningPIDs()
if len(pids) == 0 {
fmt.Println("myairouter: NOT RUNNING")
return
}
if len(pids) == 1 {
fmt.Printf("myairouter: RUNNING (PID %d)\n", pids[0])
showPIDInfo(pids[0])
return
}
fmt.Printf("⚠️ WARNING: Multiple (%d) myairouter processes detected!\n", len(pids))
for i, pid := range pids {
fmt.Printf(" [%d] PID %d\n", i+1, pid)
showPIDInfo(pid)
}
fmt.Println("\nRun 'myairouter stop' to terminate all duplicate instances.")
}
func showPIDInfo(pid int) {
out, err := exec.Command("lsof", "-p", strconv.Itoa(pid), "-iTCP", "-sTCP:LISTEN").Output()
if err == nil && len(out) > 0 {
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
if len(lines) > 1 {
fields := strings.Fields(lines[1])
if len(fields) >= 9 {
fmt.Printf(" Listening on: %s\n", fields[len(fields)-1])
}
}
}
}
func stopExistingDuplicates() {
pids := findRunningPIDs()
if len(pids) > 0 {
fmt.Printf("Stopping %d existing myairouter process(es)...\n", len(pids))
stopProcessInternal(pids)
time.Sleep(500 * time.Millisecond)
}
}
func stopProcessInternal(pids []int) {
if len(pids) == 0 {
return
}
for _, pid := range pids {
p, err := os.FindProcess(pid)
if err == nil {
_ = p.Signal(syscall.SIGTERM)
}
}
time.Sleep(500 * time.Millisecond)
remaining := findRunningPIDs()
for _, pid := range remaining {
if p, err := os.FindProcess(pid); err == nil {
_ = p.Kill()
}
}
os.Remove(pidFile)
}
func stopProcess() {
pids := findRunningPIDs()
if len(pids) == 0 {
os.Remove(pidFile)
fmt.Println("myairouter not running")
return
}
count := len(pids)
stopProcessInternal(pids)
if count == 1 {
fmt.Printf("myairouter stopped (PID %d)\n", pids[0])
} else {
fmt.Printf("myairouter stopped (%d processes terminated: %v)\n", count, pids)
}
}
func startBackground() {
stopExistingDuplicates()
cmd := exec.Command(os.Args[0])
cmd.Stdout = nil
cmd.Stderr = nil
cmd.Env = os.Environ()
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
if err := cmd.Start(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
os.WriteFile(pidFile, []byte(strconv.Itoa(cmd.Process.Pid)), 0644)
fmt.Printf("myairouter started (PID %d)\n", cmd.Process.Pid)
os.Exit(0)
}