-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcli.go
194 lines (166 loc) · 4.26 KB
/
cli.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
"strings"
"sync"
"syscall"
"time"
"unsafe"
)
const devCli = "/dev/cli"
type lineCallback func(line string, cmd *exec.Cmd) error
func streamLog(cb lineCallback) error {
cmd := exec.Command("logcat", "-bmain", "-s", "MTK_KL")
pipe, err := cmd.StdoutPipe()
if err != nil {
return fmt.Errorf("can't get a pipe for logcat child: %v", err)
}
if err := cmd.Start(); err != nil {
return fmt.Errorf("can't start logcat: %v", err)
}
var ret error
s := bufio.NewScanner(pipe)
for s.Scan() {
line := s.Text()
if err := cb(line, cmd); err != nil {
ret = fmt.Errorf("while running streamLog callback: %v", err)
cmd.Process.Kill()
break
}
}
if err := s.Err(); err != nil {
if ret != nil {
ret = fmt.Errorf("%v (additionally, while splitting logcat input: %v)", ret, err)
} else {
ret = fmt.Errorf("can't split logcat output: %v", err)
}
}
if err := cmd.Wait(); err != nil {
if ee, ok := err.(*exec.ExitError); ok {
if ws, ok := ee.ProcessState.Sys().(syscall.WaitStatus); ok {
if ws.Signaled() && ws.Signal() == os.Kill {
// We sent SIGKILL, don’t report is an error.
err = nil
}
}
}
if err != nil {
if ret != nil {
ret = fmt.Errorf("%v (additionally, while executing logcat: %v)", ret, err)
} else {
ret = fmt.Errorf("execution of logcat failed: %v", err)
}
}
}
return ret
}
func sendString(f *os.File, cmd string) error {
bp, err := syscall.BytePtrFromString(cmd)
if err != nil {
return fmt.Errorf("can't get a byte buffer: %v", err)
}
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(f.Fd()), 1, uintptr(unsafe.Pointer(bp)))
if errno != 0 {
return fmt.Errorf("ioctl(%d=%q, INPUT_STRING, %q) failed: %v", f.Fd(), devCli, cmd, errno)
}
return nil
}
func sendChar(f *os.File, char byte) error {
bp, err := syscall.BytePtrFromString(string(char))
if err != nil {
return fmt.Errorf("can't get a byte buffer: %v", err)
}
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(f.Fd()), 0, uintptr(unsafe.Pointer(bp)))
if errno != 0 {
return fmt.Errorf("ioctl(%d=%q, INPUT_CHAR, %#v) failed: %v", f.Fd(), devCli, char, errno)
}
return nil
}
func sendStringAsChars(f *os.File, cmd string) error {
for _, c := range cmd {
if err := sendChar(f, byte(c)); err != nil {
return err
}
}
return nil
}
func streamCommand(f *os.File, command string) error {
const sentinelKey = "cl1w4sh3re"
now := time.Now().UnixNano()
sentinelValStart := fmt.Sprintf("sTaRT.%d", now)
sentinelValEnd := fmt.Sprintf("enD.%d", now)
putSentinel := func(value string) error {
for _, c := range []string{fmt.Sprintf("alias %s %s", sentinelKey, value), "alias"} {
if err := sendString(f, c); err != nil {
return err
}
}
return nil
}
hasSentinel := func(line, value string) bool {
return strings.Index(line, sentinelKey) > 0 && strings.Index(line, value) > 0
}
type stateKind int
const (
_ stateKind = iota
stateWaitStart
stateWaitEnd
stateDone
)
state := stateWaitStart
callback := func(line string, cmd *exec.Cmd) error {
if state == stateWaitStart {
if hasSentinel(line, sentinelValStart) {
state = stateWaitEnd
go func() { // Need to run asynchronously; the ioctl() blocks while the handler runs.
for _, err := range []error{sendString(f, command), putSentinel(sentinelValEnd)} {
if err != nil {
log.Fatalf("Error while sending: %v", err)
}
}
}()
}
} else if state == stateWaitEnd {
if hasSentinel(line, sentinelValEnd) {
cmd.Process.Kill()
state = stateDone
} else {
if idx := strings.Index(line, "] "); idx > 0 {
line = line[idx+2:]
}
line = strings.Replace(line, "\r", "", -1)
fmt.Println(line)
}
}
return nil
}
var wg sync.WaitGroup
wg.Add(1)
var ret error
go func() {
ret = streamLog(callback)
wg.Done()
}()
if err := putSentinel(sentinelValStart); err != nil {
return err
}
wg.Wait()
return ret
}
func main() {
if len(os.Args) < 2 {
log.Fatalf("usage: %s <cmd>", os.Args[0])
}
cmd := strings.Join(os.Args[1:], " ")
f, err := os.Open(devCli)
if err != nil {
log.Fatalf("can't open %v: %v", devCli, err)
}
if err := streamCommand(f, cmd); err != nil {
log.Fatalf("Error when streaming command: %v", err)
}
}