The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* Copyright 2008 The Android Open Source Project |
| 2 | */ |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <errno.h> |
| 7 | #include <fcntl.h> |
| 8 | |
| 9 | #include <private/android_filesystem_config.h> |
| 10 | |
| 11 | #include "binder.h" |
| 12 | |
| 13 | #if 0 |
| 14 | #define LOGI(x...) fprintf(stderr, "svcmgr: " x) |
| 15 | #define LOGE(x...) fprintf(stderr, "svcmgr: " x) |
| 16 | #else |
| 17 | #define LOG_TAG "ServiceManager" |
| 18 | #include <cutils/log.h> |
| 19 | #endif |
| 20 | |
| 21 | /* TODO: |
| 22 | * These should come from a config file or perhaps be |
| 23 | * based on some namespace rules of some sort (media |
| 24 | * uid can register media.*, etc) |
| 25 | */ |
| 26 | static struct { |
| 27 | unsigned uid; |
| 28 | const char *name; |
| 29 | } allowed[] = { |
| 30 | { AID_MEDIA, "media.audio_flinger" }, |
| 31 | { AID_MEDIA, "media.player" }, |
| 32 | { AID_MEDIA, "media.camera" }, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 33 | { AID_MEDIA, "media.audio_policy" }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | { AID_RADIO, "radio.phone" }, |
| 35 | { AID_RADIO, "radio.sms" }, |
| 36 | { AID_RADIO, "radio.phonesubinfo" }, |
| 37 | { AID_RADIO, "radio.simphonebook" }, |
| 38 | /* TODO: remove after phone services are updated: */ |
| 39 | { AID_RADIO, "phone" }, |
| 40 | { AID_RADIO, "isms" }, |
| 41 | { AID_RADIO, "iphonesubinfo" }, |
| 42 | { AID_RADIO, "simphonebook" }, |
| 43 | }; |
| 44 | |
| 45 | void *svcmgr_handle; |
| 46 | |
| 47 | const char *str8(uint16_t *x) |
| 48 | { |
| 49 | static char buf[128]; |
| 50 | unsigned max = 127; |
| 51 | char *p = buf; |
| 52 | |
| 53 | if (x) { |
| 54 | while (*x && max--) { |
| 55 | *p++ = *x++; |
| 56 | } |
| 57 | } |
| 58 | *p++ = 0; |
| 59 | return buf; |
| 60 | } |
| 61 | |
| 62 | int str16eq(uint16_t *a, const char *b) |
| 63 | { |
| 64 | while (*a && *b) |
| 65 | if (*a++ != *b++) return 0; |
| 66 | if (*a || *b) |
| 67 | return 0; |
| 68 | return 1; |
| 69 | } |
| 70 | |
| 71 | int svc_can_register(unsigned uid, uint16_t *name) |
| 72 | { |
| 73 | unsigned n; |
| 74 | |
| 75 | if ((uid == 0) || (uid == AID_SYSTEM)) |
| 76 | return 1; |
| 77 | |
| 78 | for (n = 0; n < sizeof(allowed) / sizeof(allowed[0]); n++) |
| 79 | if ((uid == allowed[n].uid) && str16eq(name, allowed[n].name)) |
| 80 | return 1; |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | struct svcinfo |
| 86 | { |
| 87 | struct svcinfo *next; |
| 88 | void *ptr; |
| 89 | struct binder_death death; |
| 90 | unsigned len; |
| 91 | uint16_t name[0]; |
| 92 | }; |
| 93 | |
| 94 | struct svcinfo *svclist = 0; |
| 95 | |
| 96 | struct svcinfo *find_svc(uint16_t *s16, unsigned len) |
| 97 | { |
| 98 | struct svcinfo *si; |
| 99 | |
| 100 | for (si = svclist; si; si = si->next) { |
| 101 | if ((len == si->len) && |
| 102 | !memcmp(s16, si->name, len * sizeof(uint16_t))) { |
| 103 | return si; |
| 104 | } |
| 105 | } |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | void svcinfo_death(struct binder_state *bs, void *ptr) |
| 110 | { |
| 111 | struct svcinfo *si = ptr; |
| 112 | LOGI("service '%s' died\n", str8(si->name)); |
| 113 | if (si->ptr) { |
| 114 | binder_release(bs, si->ptr); |
| 115 | si->ptr = 0; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | uint16_t svcmgr_id[] = { |
| 120 | 'a','n','d','r','o','i','d','.','o','s','.', |
| 121 | 'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r' |
| 122 | }; |
| 123 | |
| 124 | |
| 125 | void *do_find_service(struct binder_state *bs, uint16_t *s, unsigned len) |
| 126 | { |
| 127 | struct svcinfo *si; |
| 128 | si = find_svc(s, len); |
| 129 | |
| 130 | // LOGI("check_service('%s') ptr = %p\n", str8(s), si ? si->ptr : 0); |
| 131 | if (si && si->ptr) { |
| 132 | return si->ptr; |
| 133 | } else { |
| 134 | return 0; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | int do_add_service(struct binder_state *bs, |
| 139 | uint16_t *s, unsigned len, |
| 140 | void *ptr, unsigned uid) |
| 141 | { |
| 142 | struct svcinfo *si; |
| 143 | // LOGI("add_service('%s',%p) uid=%d\n", str8(s), ptr, uid); |
| 144 | |
| 145 | if (!ptr || (len == 0) || (len > 127)) |
| 146 | return -1; |
| 147 | |
| 148 | if (!svc_can_register(uid, s)) { |
| 149 | LOGE("add_service('%s',%p) uid=%d - PERMISSION DENIED\n", |
| 150 | str8(s), ptr, uid); |
| 151 | return -1; |
| 152 | } |
| 153 | |
| 154 | si = find_svc(s, len); |
| 155 | if (si) { |
| 156 | if (si->ptr) { |
| 157 | LOGE("add_service('%s',%p) uid=%d - ALREADY REGISTERED\n", |
| 158 | str8(s), ptr, uid); |
| 159 | return -1; |
| 160 | } |
| 161 | si->ptr = ptr; |
| 162 | } else { |
| 163 | si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t)); |
| 164 | if (!si) { |
| 165 | LOGE("add_service('%s',%p) uid=%d - OUT OF MEMORY\n", |
| 166 | str8(s), ptr, uid); |
| 167 | return -1; |
| 168 | } |
| 169 | si->ptr = ptr; |
| 170 | si->len = len; |
| 171 | memcpy(si->name, s, (len + 1) * sizeof(uint16_t)); |
| 172 | si->name[len] = '\0'; |
| 173 | si->death.func = svcinfo_death; |
| 174 | si->death.ptr = si; |
| 175 | si->next = svclist; |
| 176 | svclist = si; |
| 177 | } |
| 178 | |
| 179 | binder_acquire(bs, ptr); |
| 180 | binder_link_to_death(bs, ptr, &si->death); |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | int svcmgr_handler(struct binder_state *bs, |
| 185 | struct binder_txn *txn, |
| 186 | struct binder_io *msg, |
| 187 | struct binder_io *reply) |
| 188 | { |
| 189 | struct svcinfo *si; |
| 190 | uint16_t *s; |
| 191 | unsigned len; |
| 192 | void *ptr; |
| 193 | |
| 194 | // LOGI("target=%p code=%d pid=%d uid=%d\n", |
| 195 | // txn->target, txn->code, txn->sender_pid, txn->sender_euid); |
| 196 | |
| 197 | if (txn->target != svcmgr_handle) |
| 198 | return -1; |
| 199 | |
| 200 | s = bio_get_string16(msg, &len); |
| 201 | |
| 202 | if ((len != (sizeof(svcmgr_id) / 2)) || |
| 203 | memcmp(svcmgr_id, s, sizeof(svcmgr_id))) { |
| 204 | fprintf(stderr,"invalid id %s\n", str8(s)); |
| 205 | return -1; |
| 206 | } |
| 207 | |
| 208 | switch(txn->code) { |
| 209 | case SVC_MGR_GET_SERVICE: |
| 210 | case SVC_MGR_CHECK_SERVICE: |
| 211 | s = bio_get_string16(msg, &len); |
| 212 | ptr = do_find_service(bs, s, len); |
| 213 | if (!ptr) |
| 214 | break; |
| 215 | bio_put_ref(reply, ptr); |
| 216 | return 0; |
| 217 | |
| 218 | case SVC_MGR_ADD_SERVICE: |
| 219 | s = bio_get_string16(msg, &len); |
| 220 | ptr = bio_get_ref(msg); |
| 221 | if (do_add_service(bs, s, len, ptr, txn->sender_euid)) |
| 222 | return -1; |
| 223 | break; |
| 224 | |
| 225 | case SVC_MGR_LIST_SERVICES: { |
| 226 | unsigned n = bio_get_uint32(msg); |
| 227 | |
| 228 | si = svclist; |
| 229 | while ((n-- > 0) && si) |
| 230 | si = si->next; |
| 231 | if (si) { |
| 232 | bio_put_string16(reply, si->name); |
| 233 | return 0; |
| 234 | } |
| 235 | return -1; |
| 236 | } |
| 237 | default: |
| 238 | LOGE("unknown code %d\n", txn->code); |
| 239 | return -1; |
| 240 | } |
| 241 | |
| 242 | bio_put_uint32(reply, 0); |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | int main(int argc, char **argv) |
| 247 | { |
| 248 | struct binder_state *bs; |
| 249 | void *svcmgr = BINDER_SERVICE_MANAGER; |
| 250 | |
| 251 | bs = binder_open(128*1024); |
| 252 | |
| 253 | if (binder_become_context_manager(bs)) { |
| 254 | LOGE("cannot become context manager (%s)\n", strerror(errno)); |
| 255 | return -1; |
| 256 | } |
| 257 | |
| 258 | svcmgr_handle = svcmgr; |
| 259 | binder_loop(bs, svcmgr_handler); |
| 260 | return 0; |
| 261 | } |