1 /* Copyright 2008 The Android Open Source Project 2 */ 3 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <string.h> 7 #include <errno.h> 8 9 #include "binder.h" 10 11 uint32_t svcmgr_lookup(struct binder_state *bs, uint32_t target, const char *name) 12 { 13 uint32_t handle; 14 unsigned iodata[512/4]; 15 struct binder_io msg, reply; 16 17 bio_init(&msg, iodata, sizeof(iodata), 4); 18 bio_put_uint32(&msg, 0); // strict mode header 19 bio_put_string16_x(&msg, SVC_MGR_NAME); 20 bio_put_string16_x(&msg, name); 21 22 if (binder_call(bs, &msg, &reply, target, SVC_MGR_CHECK_SERVICE)) 23 return 0; 24 25 handle = bio_get_ref(&reply); 26 27 if (handle) 28 binder_acquire(bs, handle); 29 30 binder_done(bs, &msg, &reply); 31 32 return handle; 33 } 34 35 int svcmgr_publish(struct binder_state *bs, uint32_t target, const char *name, void *ptr) 36 { 37 int status; 38 unsigned iodata[512/4]; 39 struct binder_io msg, reply; 40 41 bio_init(&msg, iodata, sizeof(iodata), 4); 42 bio_put_uint32(&msg, 0); // strict mode header 43 bio_put_string16_x(&msg, SVC_MGR_NAME); 44 bio_put_string16_x(&msg, name); 45 bio_put_obj(&msg, ptr); 46 47 if (binder_call(bs, &msg, &reply, target, SVC_MGR_ADD_SERVICE)) 48 return -1; 49 50 status = bio_get_uint32(&reply); 51 52 binder_done(bs, &msg, &reply); 53 54 return status; 55 } 56 57 unsigned token; 58 59 int main(int argc, char **argv) 60 { 61 struct binder_state *bs; 62 uint32_t svcmgr = BINDER_SERVICE_MANAGER; 63 uint32_t handle; 64 65 bs = binder_open(128*1024); 66 if (!bs) { 67 fprintf(stderr, "failed to open binder driver\n"); 68 return -1; 69 } 70 71 argc--; 72 argv++; 73 while (argc > 0) { 74 if (!strcmp(argv[0],"alt")) { 75 handle = svcmgr_lookup(bs, svcmgr, "alt_svc_mgr"); 76 if (!handle) { 77 fprintf(stderr,"cannot find alt_svc_mgr\n"); 78 return -1; 79 } 80 svcmgr = handle; 81 fprintf(stderr,"svcmgr is via %x\n", handle); 82 } else if (!strcmp(argv[0],"lookup")) { 83 if (argc < 2) { 84 fprintf(stderr,"argument required\n"); 85 return -1; 86 } 87 handle = svcmgr_lookup(bs, svcmgr, argv[1]); 88 fprintf(stderr,"lookup(%s) = %x\n", argv[1], handle); 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 } 108