blob: ee49679fd96bd9861643b242d0fab910e1295c73 [file] [log] [blame]
Mike Lockwood94afecf2012-10-24 10:45:23 -07001/* Copyright 2008 The Android Open Source Project
2 */
3
4#include <stdio.h>
5#include <stdlib.h>
6#include <errno.h>
7
8#include "binder.h"
9
10void *svcmgr_lookup(struct binder_state *bs, void *target, const char *name)
11{
12 void *ptr;
13 unsigned iodata[512/4];
14 struct binder_io msg, reply;
15
16 bio_init(&msg, iodata, sizeof(iodata), 4);
17 bio_put_uint32(&msg, 0); // strict mode header
18 bio_put_string16_x(&msg, SVC_MGR_NAME);
19 bio_put_string16_x(&msg, name);
20
21 if (binder_call(bs, &msg, &reply, target, SVC_MGR_CHECK_SERVICE))
22 return 0;
23
24 ptr = bio_get_ref(&reply);
25
26 if (ptr)
27 binder_acquire(bs, ptr);
28
29 binder_done(bs, &msg, &reply);
30
31 return ptr;
32}
33
34int svcmgr_publish(struct binder_state *bs, void *target, const char *name, void *ptr)
35{
36 unsigned status;
37 unsigned iodata[512/4];
38 struct binder_io msg, reply;
39
40 bio_init(&msg, iodata, sizeof(iodata), 4);
41 bio_put_uint32(&msg, 0); // strict mode header
42 bio_put_string16_x(&msg, SVC_MGR_NAME);
43 bio_put_string16_x(&msg, name);
44 bio_put_obj(&msg, ptr);
45
46 if (binder_call(bs, &msg, &reply, target, SVC_MGR_ADD_SERVICE))
47 return -1;
48
49 status = bio_get_uint32(&reply);
50
51 binder_done(bs, &msg, &reply);
52
53 return status;
54}
55
56unsigned token;
57
58int main(int argc, char **argv)
59{
60 int fd;
61 struct binder_state *bs;
62 void *svcmgr = BINDER_SERVICE_MANAGER;
63
64 bs = binder_open(128*1024);
Serban Constantinescua44542c2014-01-30 15:16:45 +000065 if (!bs) {
66 fprintf(stderr, "failed to open binder driver\n");
67 return -1;
68 }
Mike Lockwood94afecf2012-10-24 10:45:23 -070069
70 argc--;
71 argv++;
72 while (argc > 0) {
73 if (!strcmp(argv[0],"alt")) {
74 void *ptr = svcmgr_lookup(bs, svcmgr, "alt_svc_mgr");
75 if (!ptr) {
76 fprintf(stderr,"cannot find alt_svc_mgr\n");
77 return -1;
78 }
79 svcmgr = ptr;
80 fprintf(stderr,"svcmgr is via %p\n", ptr);
81 } else if (!strcmp(argv[0],"lookup")) {
82 void *ptr;
83 if (argc < 2) {
84 fprintf(stderr,"argument required\n");
85 return -1;
86 }
87 ptr = svcmgr_lookup(bs, svcmgr, argv[1]);
88 fprintf(stderr,"lookup(%s) = %p\n", argv[1], ptr);
89 argc--;
90 argv++;
91 } else if (!strcmp(argv[0],"publish")) {
92 if (argc < 2) {
93 fprintf(stderr,"argument required\n");
94 return -1;
95 }
96 svcmgr_publish(bs, svcmgr, argv[1], &token);
97 argc--;
98 argv++;
99 } else {
100 fprintf(stderr,"unknown command %s\n", argv[0]);
101 return -1;
102 }
103 argc--;
104 argv++;
105 }
106 return 0;
107}