|
1 | 1 | // Register a Go service with the ServiceManager and call it back. |
2 | 2 | // |
| 3 | +// Demonstrates implementing binder.TransactionReceiver — the server-side |
| 4 | +// binder interface — and registering it via AddService. When running in |
| 5 | +// a restricted SELinux context (e.g. shell), AddService will be denied; |
| 6 | +// in that case the example falls back to an in-process self-test that |
| 7 | +// exercises the same OnTransaction code path. |
| 8 | +// |
3 | 9 | // Build: |
4 | 10 | // |
5 | | -// GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o build/server_service ./examples/server_service/ |
6 | | -// adb push server_service /data/local/tmp/ && adb shell /data/local/tmp/server_service |
| 11 | +// GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o build/server_service ./examples/server_service/ |
| 12 | +// adb push build/server_service /data/local/tmp/ && adb shell /data/local/tmp/server_service |
7 | 13 | package main |
8 | 14 |
|
9 | 15 | import ( |
10 | 16 | "context" |
| 17 | + "errors" |
11 | 18 | "fmt" |
12 | 19 | "os" |
13 | 20 |
|
14 | 21 | "github.com/AndroidGoLab/binder/binder" |
15 | 22 | "github.com/AndroidGoLab/binder/binder/versionaware" |
| 23 | + aidlerrors "github.com/AndroidGoLab/binder/errors" |
16 | 24 | "github.com/AndroidGoLab/binder/kernelbinder" |
17 | 25 | "github.com/AndroidGoLab/binder/parcel" |
18 | 26 | "github.com/AndroidGoLab/binder/servicemanager" |
@@ -88,71 +96,148 @@ func main() { |
88 | 96 |
|
89 | 97 | // Register our ping service with the service manager. |
90 | 98 | svc := &pingService{} |
91 | | - if err := sm.AddService(ctx, servicemanager.ServiceName(pingServiceName), svc, false, 0); err != nil { |
92 | | - fmt.Fprintf(os.Stderr, "add service: %v\n", err) |
93 | | - os.Exit(1) |
| 99 | + err = sm.AddService(ctx, servicemanager.ServiceName(pingServiceName), svc, false, 0) |
| 100 | + |
| 101 | + switch { |
| 102 | + case err == nil: |
| 103 | + fmt.Printf("Registered service %q\n", pingServiceName) |
| 104 | + remoteTest(ctx, sm) |
| 105 | + |
| 106 | + default: |
| 107 | + // AddService typically fails from the shell SELinux context. |
| 108 | + // Show the error and fall back to an in-process self-test. |
| 109 | + var se *aidlerrors.StatusError |
| 110 | + if errors.As(err, &se) && se.Exception == aidlerrors.ExceptionSecurity { |
| 111 | + fmt.Printf("AddService denied by SELinux (expected from shell context).\n") |
| 112 | + fmt.Printf("Falling back to in-process self-test.\n\n") |
| 113 | + } else { |
| 114 | + fmt.Fprintf(os.Stderr, "add service: %v\n", err) |
| 115 | + fmt.Fprintf(os.Stderr, "Falling back to in-process self-test.\n\n") |
| 116 | + } |
| 117 | + inProcessTest(ctx, svc) |
94 | 118 | } |
95 | | - fmt.Printf("Registered service %q\n", pingServiceName) |
| 119 | +} |
96 | 120 |
|
97 | | - // Self-test: look up the service we just registered and call it. |
| 121 | +// remoteTest looks up the service via the ServiceManager and calls it |
| 122 | +// through the binder driver, exercising the full IPC path. |
| 123 | +func remoteTest(ctx context.Context, sm *servicemanager.ServiceManager) { |
98 | 124 | remote, err := sm.GetService(ctx, servicemanager.ServiceName(pingServiceName)) |
99 | 125 | if err != nil { |
100 | 126 | fmt.Fprintf(os.Stderr, "get service: %v\n", err) |
101 | 127 | os.Exit(1) |
102 | 128 | } |
103 | 129 |
|
104 | | - // Call Ping (codePing). |
| 130 | + callPing(ctx, remote) |
| 131 | + callEcho(ctx, remote, "hello from Go") |
| 132 | + fmt.Println("All remote self-tests passed.") |
| 133 | +} |
| 134 | + |
| 135 | +// inProcessTest calls OnTransaction directly, verifying the handler |
| 136 | +// logic without requiring ServiceManager registration. |
| 137 | +func inProcessTest(ctx context.Context, svc *pingService) { |
| 138 | + // Ping |
105 | 139 | { |
106 | 140 | data := parcel.New() |
107 | 141 | defer data.Recycle() |
108 | 142 | data.WriteInterfaceToken(pingServiceDescriptor) |
109 | 143 |
|
110 | | - reply, err := remote.Transact(ctx, codePing, 0, data) |
| 144 | + reply, err := svc.OnTransaction(ctx, codePing, data) |
111 | 145 | if err != nil { |
112 | | - fmt.Fprintf(os.Stderr, "ping transact: %v\n", err) |
| 146 | + fmt.Fprintf(os.Stderr, "in-process ping: %v\n", err) |
113 | 147 | os.Exit(1) |
114 | 148 | } |
115 | 149 | defer reply.Recycle() |
116 | 150 |
|
117 | 151 | if err := binder.ReadStatus(reply); err != nil { |
118 | | - fmt.Fprintf(os.Stderr, "ping status: %v\n", err) |
| 152 | + fmt.Fprintf(os.Stderr, "in-process ping status: %v\n", err) |
119 | 153 | os.Exit(1) |
120 | 154 | } |
121 | 155 |
|
122 | 156 | result, err := reply.ReadString16() |
123 | 157 | if err != nil { |
124 | | - fmt.Fprintf(os.Stderr, "ping read result: %v\n", err) |
| 158 | + fmt.Fprintf(os.Stderr, "in-process ping read: %v\n", err) |
125 | 159 | os.Exit(1) |
126 | 160 | } |
127 | 161 | fmt.Printf("Ping -> %q\n", result) |
128 | 162 | } |
129 | 163 |
|
130 | | - // Call Echo (codeEcho). |
| 164 | + // Echo |
131 | 165 | { |
132 | 166 | data := parcel.New() |
133 | 167 | defer data.Recycle() |
134 | 168 | data.WriteInterfaceToken(pingServiceDescriptor) |
135 | 169 | data.WriteString16("hello from Go") |
136 | 170 |
|
137 | | - reply, err := remote.Transact(ctx, codeEcho, 0, data) |
| 171 | + reply, err := svc.OnTransaction(ctx, codeEcho, data) |
138 | 172 | if err != nil { |
139 | | - fmt.Fprintf(os.Stderr, "echo transact: %v\n", err) |
| 173 | + fmt.Fprintf(os.Stderr, "in-process echo: %v\n", err) |
140 | 174 | os.Exit(1) |
141 | 175 | } |
142 | 176 | defer reply.Recycle() |
143 | 177 |
|
144 | 178 | if err := binder.ReadStatus(reply); err != nil { |
145 | | - fmt.Fprintf(os.Stderr, "echo status: %v\n", err) |
| 179 | + fmt.Fprintf(os.Stderr, "in-process echo status: %v\n", err) |
146 | 180 | os.Exit(1) |
147 | 181 | } |
148 | 182 |
|
149 | 183 | result, err := reply.ReadString16() |
150 | 184 | if err != nil { |
151 | | - fmt.Fprintf(os.Stderr, "echo read result: %v\n", err) |
| 185 | + fmt.Fprintf(os.Stderr, "in-process echo read: %v\n", err) |
152 | 186 | os.Exit(1) |
153 | 187 | } |
154 | 188 | fmt.Printf("Echo -> %q\n", result) |
155 | 189 | } |
156 | 190 |
|
157 | | - fmt.Println("All self-tests passed.") |
| 191 | + fmt.Println("All in-process self-tests passed.") |
| 192 | +} |
| 193 | + |
| 194 | +func callPing(ctx context.Context, remote binder.IBinder) { |
| 195 | + data := parcel.New() |
| 196 | + defer data.Recycle() |
| 197 | + data.WriteInterfaceToken(pingServiceDescriptor) |
| 198 | + |
| 199 | + reply, err := remote.Transact(ctx, codePing, 0, data) |
| 200 | + if err != nil { |
| 201 | + fmt.Fprintf(os.Stderr, "ping transact: %v\n", err) |
| 202 | + os.Exit(1) |
| 203 | + } |
| 204 | + defer reply.Recycle() |
| 205 | + |
| 206 | + if err := binder.ReadStatus(reply); err != nil { |
| 207 | + fmt.Fprintf(os.Stderr, "ping status: %v\n", err) |
| 208 | + os.Exit(1) |
| 209 | + } |
| 210 | + |
| 211 | + result, err := reply.ReadString16() |
| 212 | + if err != nil { |
| 213 | + fmt.Fprintf(os.Stderr, "ping read result: %v\n", err) |
| 214 | + os.Exit(1) |
| 215 | + } |
| 216 | + fmt.Printf("Ping -> %q\n", result) |
| 217 | +} |
| 218 | + |
| 219 | +func callEcho(ctx context.Context, remote binder.IBinder, msg string) { |
| 220 | + data := parcel.New() |
| 221 | + defer data.Recycle() |
| 222 | + data.WriteInterfaceToken(pingServiceDescriptor) |
| 223 | + data.WriteString16(msg) |
| 224 | + |
| 225 | + reply, err := remote.Transact(ctx, codeEcho, 0, data) |
| 226 | + if err != nil { |
| 227 | + fmt.Fprintf(os.Stderr, "echo transact: %v\n", err) |
| 228 | + os.Exit(1) |
| 229 | + } |
| 230 | + defer reply.Recycle() |
| 231 | + |
| 232 | + if err := binder.ReadStatus(reply); err != nil { |
| 233 | + fmt.Fprintf(os.Stderr, "echo status: %v\n", err) |
| 234 | + os.Exit(1) |
| 235 | + } |
| 236 | + |
| 237 | + result, err := reply.ReadString16() |
| 238 | + if err != nil { |
| 239 | + fmt.Fprintf(os.Stderr, "echo read result: %v\n", err) |
| 240 | + os.Exit(1) |
| 241 | + } |
| 242 | + fmt.Printf("Echo -> %q\n", result) |
158 | 243 | } |
0 commit comments