Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1 | /* Copyright 2008 The Android Open Source Project |
| 2 | */ |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
Elliott Hughes | 824e30e | 2015-01-29 22:32:32 -0800 | [diff] [blame^] | 6 | #include <string.h> |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 7 | #include <errno.h> |
| 8 | |
| 9 | #include "binder.h" |
| 10 | |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 11 | uint32_t svcmgr_lookup(struct binder_state *bs, uint32_t target, const char *name) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 12 | { |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 13 | uint32_t handle; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 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 | |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 25 | handle = bio_get_ref(&reply); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 26 | |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 27 | if (handle) |
| 28 | binder_acquire(bs, handle); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 29 | |
| 30 | binder_done(bs, &msg, &reply); |
| 31 | |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 32 | return handle; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 35 | int svcmgr_publish(struct binder_state *bs, uint32_t target, const char *name, void *ptr) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 36 | { |
Serban Constantinescu | 3a345f0 | 2013-12-19 09:11:41 +0000 | [diff] [blame] | 37 | int status; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 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 | int fd; |
| 62 | struct binder_state *bs; |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 63 | uint32_t svcmgr = BINDER_SERVICE_MANAGER; |
| 64 | uint32_t handle; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 65 | |
| 66 | bs = binder_open(128*1024); |
Serban Constantinescu | a44542c | 2014-01-30 15:16:45 +0000 | [diff] [blame] | 67 | if (!bs) { |
| 68 | fprintf(stderr, "failed to open binder driver\n"); |
| 69 | return -1; |
| 70 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 71 | |
| 72 | argc--; |
| 73 | argv++; |
| 74 | while (argc > 0) { |
| 75 | if (!strcmp(argv[0],"alt")) { |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 76 | handle = svcmgr_lookup(bs, svcmgr, "alt_svc_mgr"); |
| 77 | if (!handle) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 78 | fprintf(stderr,"cannot find alt_svc_mgr\n"); |
| 79 | return -1; |
| 80 | } |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 81 | svcmgr = handle; |
| 82 | fprintf(stderr,"svcmgr is via %x\n", handle); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 83 | } else if (!strcmp(argv[0],"lookup")) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 84 | if (argc < 2) { |
| 85 | fprintf(stderr,"argument required\n"); |
| 86 | return -1; |
| 87 | } |
Serban Constantinescu | 5fb1b88 | 2014-01-30 14:07:34 +0000 | [diff] [blame] | 88 | handle = svcmgr_lookup(bs, svcmgr, argv[1]); |
| 89 | fprintf(stderr,"lookup(%s) = %x\n", argv[1], handle); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 90 | argc--; |
| 91 | argv++; |
| 92 | } else if (!strcmp(argv[0],"publish")) { |
| 93 | if (argc < 2) { |
| 94 | fprintf(stderr,"argument required\n"); |
| 95 | return -1; |
| 96 | } |
| 97 | svcmgr_publish(bs, svcmgr, argv[1], &token); |
| 98 | argc--; |
| 99 | argv++; |
| 100 | } else { |
| 101 | fprintf(stderr,"unknown command %s\n", argv[0]); |
| 102 | return -1; |
| 103 | } |
| 104 | argc--; |
| 105 | argv++; |
| 106 | } |
| 107 | return 0; |
| 108 | } |