/
localmaster.go
79 lines (68 loc) · 1.66 KB
/
localmaster.go
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
package termite
import (
"fmt"
"log"
"net"
"net/rpc"
"os"
"github.com/hanwen/termite/attr"
)
// Expose functionality for the local tool to use.
type LocalMaster struct {
master *Master
listener connListener
server *rpc.Server
}
func localStart(m *Master, sock string) {
l := LocalMaster{
master: m,
}
l.server = rpc.NewServer()
l.server.Register(&l)
l.start(sock)
}
func (m *LocalMaster) Run(req *WorkRequest, rep *WorkResponse) error {
if req.RanLocally {
log.Println("Ran command locally:", req.Argv)
return nil
}
if len(req.Binary) == 0 || req.Binary[0] != '/' {
return fmt.Errorf("Path to binary is not absolute: %q", req.Binary)
}
if req.StdinId != "" {
req.StdinConn = m.listener.Pending().accept(req.StdinId)
}
return m.master.run(req, rep)
}
func (m *LocalMaster) Shutdown(req *int, rep *int) error {
m.master.quit <- 1
return nil
}
func (m *LocalMaster) RefreshAttributeCache(input *int, output *int) error {
log.Println("Refreshing attribute cache")
m.master.refreshAttributeCache()
m.master.setAnalysisDir()
log.Println("Refresh done")
return nil
}
func (m *LocalMaster) InspectFile(req *attr.AttrRequest, rep *attr.AttrResponse) error {
a := m.master.attributes.GetDir(req.Name)
rep.Attrs = append(rep.Attrs, a)
return nil
}
func (m *LocalMaster) start(sock string) {
l, err := net.Listen("unix", sock)
if err != nil {
log.Fatal("startLocalServer: ", err)
}
defer os.Remove(sock)
err = os.Chmod(sock, 0700)
if err != nil {
log.Fatal("sock chmod", err)
}
log.Println("accepting connections on", sock)
m.listener = newTCPListener(l, nil)
for c := range m.listener.Pending().rpcChan() {
go m.server.ServeConn(c)
}
}