-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
120 lines (106 loc) · 3.2 KB
/
main.go
File metadata and controls
120 lines (106 loc) · 3.2 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
// Introspect methods on binder services.
//
// Lists all registered services and pings each to check liveness.
// For well-known services, prints their interface descriptor and
// demonstrates method resolution using ResolveCode.
//
// Build:
//
// GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o build/aidl_explorer ./examples/aidl_explorer/
// adb push build/aidl_explorer /data/local/tmp/ && adb shell /data/local/tmp/aidl_explorer
package main
import (
"context"
"fmt"
"os"
"github.com/AndroidGoLab/binder/binder"
"github.com/AndroidGoLab/binder/binder/versionaware"
"github.com/AndroidGoLab/binder/kernelbinder"
"github.com/AndroidGoLab/binder/servicemanager"
)
// knownInterfaces maps service names to their AIDL descriptors and
// methods for exploration.
var knownInterfaces = []struct {
service string
descriptor string
methods []string
}{
{
service: "SurfaceFlingerAIDL",
descriptor: "android.gui.ISurfaceComposer",
methods: []string{"getPhysicalDisplayIds", "getBootDisplayModeSupport", "getStaticDisplayInfo"},
},
{
service: "activity",
descriptor: "android.app.IActivityManager",
methods: []string{"getProcessLimit", "isUserAMonkey", "checkPermission", "isAppFreezerSupported"},
},
{
service: "power",
descriptor: "android.os.IPowerManager",
methods: []string{"isInteractive", "isPowerSaveMode", "isDeviceIdleMode"},
},
}
func main() {
ctx := context.Background()
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)
transport, err := versionaware.NewTransport(ctx, driver, 0)
if err != nil {
fmt.Fprintf(os.Stderr, "version-aware transport: %v\n", err)
os.Exit(1)
}
sm := servicemanager.New(transport)
// List all services.
services, err := sm.ListServices(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "ListServices: %v\n", err)
os.Exit(1)
}
fmt.Printf("=== AIDL Explorer: %d services registered ===\n\n", len(services))
// Show first 15 services with ping status.
limit := 15
if len(services) < limit {
limit = len(services)
}
for i := 0; i < limit; i++ {
svc, err := sm.CheckService(ctx, services[i])
status := "unreachable"
if err == nil && svc != nil {
if svc.IsAlive(ctx) {
status = fmt.Sprintf("alive (handle=%d)", svc.Handle())
} else {
status = "dead"
}
}
fmt.Printf(" %-45s %s\n", services[i], status)
}
if len(services) > limit {
fmt.Printf(" ... and %d more\n", len(services)-limit)
}
fmt.Println()
// Explore known interfaces: resolve method names to transaction codes.
fmt.Println("=== Method Resolution ===")
fmt.Println()
for _, iface := range knownInterfaces {
svc, err := sm.GetService(ctx, servicemanager.ServiceName(iface.service))
if err != nil {
fmt.Printf(" %s: unavailable (%v)\n", iface.service, err)
continue
}
fmt.Printf(" %s (%s):\n", iface.service, iface.descriptor)
for _, method := range iface.methods {
code, err := svc.ResolveCode(ctx, iface.descriptor, method)
if err != nil {
fmt.Printf(" %-40s -> ERROR: %v\n", method, err)
} else {
fmt.Printf(" %-40s -> code %d (0x%x)\n", method, code, code)
}
}
fmt.Println()
}
}