blob: a44994cd2f960575810922af19d6d60a6f849ddd [file] [log] [blame]
Dan Willemsena14704c2017-10-28 22:57:22 -07001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package paths
16
17import (
18 "context"
19 "encoding/gob"
20 "net"
21 "os"
22 "time"
23)
24
25type LogProcess struct {
26 Pid int
27 Command string
28}
29
30type LogEntry struct {
31 Basename string
32 Args []string
33 Parents []LogProcess
34}
35
36const timeoutDuration = time.Duration(250) * time.Millisecond
37
38func SendLog(logSocket string, entry *LogEntry, done chan interface{}) {
39 defer close(done)
40
41 dialer := &net.Dialer{}
42 conn, err := dialer.Dial("unix", logSocket)
43 if err != nil {
44 return
45 }
46 defer conn.Close()
47
48 if err := conn.SetDeadline(dialer.Deadline); err != nil {
49 return
50 }
51
52 enc := gob.NewEncoder(conn)
53 enc.Encode(entry)
54}
55
56func LogListener(ctx context.Context, logSocket string) (chan *LogEntry, error) {
57 ret := make(chan *LogEntry, 5)
58
59 if err := os.Remove(logSocket); err != nil && !os.IsNotExist(err) {
60 return nil, err
61 }
62
63 ln, err := net.Listen("unix", logSocket)
64 if err != nil {
65 return nil, err
66 }
67
68 go func() {
69 for {
70 select {
71 case <-ctx.Done():
72 ln.Close()
73 }
74 }
75 }()
76
77 go func() {
78 defer close(ret)
79
80 for {
81 conn, err := ln.Accept()
82 if err != nil {
83 ln.Close()
84 break
85 }
86 conn.SetDeadline(time.Now().Add(timeoutDuration))
87
88 go func() {
89 defer conn.Close()
90
91 dec := gob.NewDecoder(conn)
92 entry := &LogEntry{}
93 if err := dec.Decode(entry); err != nil {
94 return
95 }
96 ret <- entry
97 }()
98 }
99 }()
100 return ret, nil
101}