Repository navigation
Expand file tree
/
Copy pathipc.go
More file actions
106 lines (96 loc) · 2.34 KB
/
Copy pathipc.go
File metadata and controls
106 lines (96 loc) · 2.34 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
package main
import (
"encoding/json"
"errors"
"net"
"os"
"path/filepath"
"strings"
"time"
)
// sockPath returns the unix socket path used for single-instance IPC.
func sockPath() string {
user := os.Getenv("USER")
if user == "" {
user = "puremark"
}
return filepath.Join(os.TempDir(), "puremark-"+user+".sock")
}
// filteredArgs returns os.Args[1:] turned into absolute file paths,
// ignoring flags and non-existent entries.
func filteredArgs() []string {
out := []string{}
for _, arg := range os.Args[1:] {
if strings.HasPrefix(arg, "-") {
continue
}
abs, err := filepath.Abs(arg)
if err != nil {
continue
}
info, err := os.Stat(abs)
if err != nil || info.IsDir() {
continue
}
out = append(out, abs)
}
return out
}
// trySendToPrimary attempts to deliver the current CLI args to an already
// running PureMark instance. Returns true if delivery succeeded, in which
// case this process should exit.
func trySendToPrimary() bool {
sp := sockPath()
conn, err := net.DialTimeout("unix", sp, 300*time.Millisecond)
if err != nil {
// Stale socket file (no listener) → remove so we can take primary role.
if errors.Is(err, os.ErrNotExist) {
return false
}
// Connection refused / no listener: drop the orphan socket.
_ = os.Remove(sp)
return false
}
defer conn.Close()
_ = conn.SetWriteDeadline(time.Now().Add(500 * time.Millisecond))
if err := json.NewEncoder(conn).Encode(filteredArgs()); err != nil {
return false
}
return true
}
// startIPCServer creates the unix socket and dispatches incoming path lists
// to the provided handler. Removes the socket on process exit signals.
func startIPCServer(onPaths func([]string)) error {
sp := sockPath()
_ = os.Remove(sp) // clean any stale file we own
ln, err := net.Listen("unix", sp)
if err != nil {
return err
}
_ = os.Chmod(sp, 0600)
go func() {
for {
conn, err := ln.Accept()
if err != nil {
return
}
go handleIPCConn(conn, onPaths)
}
}()
return nil
}
func handleIPCConn(conn net.Conn, onPaths func([]string)) {
defer conn.Close()
_ = conn.SetReadDeadline(time.Now().Add(2 * time.Second))
var paths []string
if err := json.NewDecoder(conn).Decode(&paths); err != nil {
return
}
if len(paths) > 0 {
onPaths(paths)
}
}
// cleanupIPC removes the socket file (called on shutdown).
func cleanupIPC() {
_ = os.Remove(sockPath())
}