-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
228 lines (206 loc) · 7.12 KB
/
main.go
File metadata and controls
228 lines (206 loc) · 7.12 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Binary getservice_vs_checkservice compares GetService vs CheckService
// for HAL binder services to investigate the discrepancy where CheckService
// returns NOT FOUND for services that GetService can retrieve.
//
// All method calls are READ-ONLY and non-destructive.
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/facebookincubator/go-belt"
"github.com/facebookincubator/go-belt/tool/logger"
"github.com/facebookincubator/go-belt/tool/logger/implementation/logrus"
genApp "github.com/AndroidGoLab/binder/android/app"
genBoot "github.com/AndroidGoLab/binder/android/hardware/boot"
genKeymint "github.com/AndroidGoLab/binder/android/hardware/security/keymint"
genUsb "github.com/AndroidGoLab/binder/android/hardware/usb"
"github.com/AndroidGoLab/binder/binder"
"github.com/AndroidGoLab/binder/binder/versionaware"
"github.com/AndroidGoLab/binder/kernelbinder"
"github.com/AndroidGoLab/binder/servicemanager"
)
type serviceTest struct {
Name string
// CallMethod is called with the binder handle to invoke a read-only
// method via the generated proxy. Returns a summary string.
CallMethod func(ctx context.Context, svc binder.IBinder) string
}
var services = []serviceTest{
{
Name: "android.hardware.security.keymint.IKeyMintDevice/default",
CallMethod: func(ctx context.Context, svc binder.IBinder) string {
proxy := genKeymint.NewKeyMintDeviceProxy(svc)
info, err := proxy.GetHardwareInfo(ctx)
if err != nil {
return fmt.Sprintf("ERROR: %v", err)
}
return fmt.Sprintf("version=%d secLevel=%d name=%q author=%q",
info.VersionNumber, info.SecurityLevel, info.KeyMintName, info.KeyMintAuthorName)
},
},
{
Name: "android.hardware.usb.IUsb/default",
CallMethod: func(ctx context.Context, svc binder.IBinder) string {
proxy := genUsb.NewUsbProxy(svc)
err := proxy.QueryPortStatus(ctx, 0)
if err != nil {
return fmt.Sprintf("ERROR: %v", err)
}
return "SUCCESS"
},
},
{
Name: "android.hardware.boot.IBootControl/default",
CallMethod: func(ctx context.Context, svc binder.IBinder) string {
proxy := genBoot.NewBootControlProxy(svc)
slot, err := proxy.GetCurrentSlot(ctx)
if err != nil {
return fmt.Sprintf("ERROR: %v", err)
}
return fmt.Sprintf("currentSlot=%d", slot)
},
},
{
Name: "SurfaceFlinger",
CallMethod: nil, // legacy interface, skip method call
},
{
Name: "activity",
CallMethod: func(ctx context.Context, svc binder.IBinder) string {
proxy := genApp.NewActivityManagerProxy(svc)
isMonkey, err := proxy.IsUserAMonkey(ctx)
if err != nil {
return fmt.Sprintf("ERROR: %v", err)
}
return fmt.Sprintf("isUserAMonkey=%v", isMonkey)
},
},
}
func main() {
ctx := context.Background()
l := logrus.Default().WithLevel(logger.LevelDebug)
ctx = belt.CtxWithBelt(ctx, belt.New())
ctx = logger.CtxWithLogger(ctx, l)
fmt.Println("========================================")
fmt.Println("GetService vs CheckService Comparison")
fmt.Println("========================================")
fmt.Printf("PID: %d UID: %d GID: %d\n", os.Getpid(), os.Getuid(), os.Getgid())
fmt.Println()
// Read SELinux context
seCtx, err := os.ReadFile("/proc/self/attr/current")
if err != nil {
fmt.Printf("SELinux context: ERROR (%v)\n", err)
} else {
fmt.Printf("SELinux context: %s\n", string(seCtx))
}
fmt.Println()
// Open binder driver
driver, err := kernelbinder.Open(ctx, binder.WithMapSize(128*1024))
if err != nil {
fmt.Fprintf(os.Stderr, "open binder: %v\n", err)
os.Exit(1)
}
defer driver.Close(ctx)
fmt.Println("/dev/binder: OK")
transport, err := versionaware.NewTransport(ctx, driver, 0)
if err != nil {
fmt.Fprintf(os.Stderr, "version-aware transport: %v\n", err)
os.Exit(1)
}
defer transport.Close(ctx)
fmt.Println("VersionAwareTransport: OK")
fmt.Println()
sm := servicemanager.New(transport)
// First: list services to see what's visible
fmt.Println("--- ListServices ---")
svcList, err := sm.ListServices(ctx)
if err != nil {
fmt.Printf("ListServices: FAILED (%v)\n", err)
} else {
fmt.Printf("ListServices: %d services visible\n", len(svcList))
// Check which target services are listed
listed := make(map[string]bool)
for _, s := range svcList {
listed[string(s)] = true
}
for _, st := range services {
if listed[st.Name] {
fmt.Printf(" %s: LISTED\n", st.Name)
} else {
fmt.Printf(" %s: NOT LISTED\n", st.Name)
}
}
}
fmt.Println()
// Test IsDeclared for each service
fmt.Println("--- IsDeclared ---")
for _, st := range services {
declared, err := sm.IsDeclared(ctx, servicemanager.ServiceName(st.Name))
if err != nil {
fmt.Printf(" %s: ERROR (%v)\n", st.Name, err)
} else {
fmt.Printf(" %s: declared=%v\n", st.Name, declared)
}
}
fmt.Println()
// Now test CheckService vs GetService for each
fmt.Println("--- CheckService vs GetService ---")
for _, st := range services {
fmt.Printf("\n[%s]\n", st.Name)
// 1) CheckService (non-blocking)
checkCtx, checkCancel := context.WithTimeout(ctx, 5*time.Second)
checkBinder, checkErr := sm.CheckService(checkCtx, servicemanager.ServiceName(st.Name))
checkCancel()
if checkErr != nil {
fmt.Printf(" CheckService: ERROR (%v)\n", checkErr)
} else if checkBinder == nil {
fmt.Printf(" CheckService: NOT FOUND (nil binder)\n")
} else {
fmt.Printf(" CheckService: SUCCESS (handle=%d)\n", checkBinder.Handle())
}
// 2) GetService (blocking, with timeout)
getCtx, getCancel := context.WithTimeout(ctx, 10*time.Second)
getBinder, getErr := sm.GetService(getCtx, servicemanager.ServiceName(st.Name))
getCancel()
if getErr != nil {
fmt.Printf(" GetService: ERROR (%v)\n", getErr)
} else if getBinder == nil {
fmt.Printf(" GetService: NOT FOUND (nil binder)\n")
} else {
fmt.Printf(" GetService: SUCCESS (handle=%d)\n", getBinder.Handle())
}
// 3) If either succeeded, try the read-only method via generated proxy
var activeBinder binder.IBinder
var source string
if getBinder != nil {
activeBinder = getBinder
source = "GetService"
} else if checkBinder != nil {
activeBinder = checkBinder
source = "CheckService"
}
if activeBinder != nil && st.CallMethod != nil {
fmt.Printf(" Calling method (via %s handle=%d)...\n", source, activeBinder.Handle())
callCtx, callCancel := context.WithTimeout(ctx, 5*time.Second)
result := st.CallMethod(callCtx, activeBinder)
callCancel()
fmt.Printf(" %s\n", result)
}
// 4) Compare
if checkBinder == nil && getBinder != nil {
fmt.Printf(" *** DISCREPANCY: CheckService=nil but GetService=handle %d ***\n", getBinder.Handle())
fmt.Printf(" This means GetService's startIfNotFound=true triggered lazy service start!\n")
} else if checkBinder != nil && getBinder == nil {
fmt.Printf(" *** DISCREPANCY: CheckService=handle %d but GetService=nil ***\n", checkBinder.Handle())
} else if checkBinder == nil && getBinder == nil {
fmt.Printf(" Both returned nil/error - service truly unavailable\n")
} else {
fmt.Printf(" Both succeeded (check handle=%d, get handle=%d)\n", checkBinder.Handle(), getBinder.Handle())
}
}
fmt.Println()
fmt.Println("========================================")
fmt.Println("Done.")
}