service_manager.c revision 9387f4f80081128601da936fe5e6006809ff479c
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 */ 26static struct { 27 unsigned uid; 28 const char *name; 29} allowed[] = { 30#ifdef LVMX 31 { AID_MEDIA, "com.lifevibes.mx.ipc" }, 32#endif 33 { AID_MEDIA, "media.audio_flinger" }, 34 { AID_MEDIA, "media.player" }, 35 { AID_MEDIA, "media.camera" }, 36 { AID_MEDIA, "media.audio_policy" }, 37 { AID_DRM, "drm.drmManager" }, 38 { AID_NFC, "nfc" }, 39 { AID_RADIO, "radio.phone" }, 40 { AID_RADIO, "radio.sms" }, 41 { AID_RADIO, "radio.phonesubinfo" }, 42 { AID_RADIO, "radio.simphonebook" }, 43/* TODO: remove after phone services are updated: */ 44 { AID_RADIO, "phone" }, 45 { AID_RADIO, "sip" }, 46 { AID_RADIO, "isms" }, 47 { AID_RADIO, "iphonesubinfo" }, 48 { AID_RADIO, "simphonebook" }, 49 { AID_MEDIA, "common_time.clock" }, 50 { AID_MEDIA, "common_time.config" }, 51}; 52 53void *svcmgr_handle; 54 55const char *str8(uint16_t *x) 56{ 57 static char buf[128]; 58 unsigned max = 127; 59 char *p = buf; 60 61 if (x) { 62 while (*x && max--) { 63 *p++ = *x++; 64 } 65 } 66 *p++ = 0; 67 return buf; 68} 69 70int str16eq(uint16_t *a, const char *b) 71{ 72 while (*a && *b) 73 if (*a++ != *b++) return 0; 74 if (*a || *b) 75 return 0; 76 return 1; 77} 78 79int svc_can_register(unsigned uid, uint16_t *name) 80{ 81 unsigned n; 82 83 if ((uid == 0) || (uid == AID_SYSTEM)) 84 return 1; 85 86 for (n = 0; n < sizeof(allowed) / sizeof(allowed[0]); n++) 87 if ((uid == allowed[n].uid) && str16eq(name, allowed[n].name)) 88 return 1; 89 90 return 0; 91} 92 93struct svcinfo 94{ 95 struct svcinfo *next; 96 void *ptr; 97 struct binder_death death; 98 unsigned len; 99 uint16_t name[0]; 100}; 101 102struct svcinfo *svclist = 0; 103 104struct svcinfo *find_svc(uint16_t *s16, unsigned len) 105{ 106 struct svcinfo *si; 107 108 for (si = svclist; si; si = si->next) { 109 if ((len == si->len) && 110 !memcmp(s16, si->name, len * sizeof(uint16_t))) { 111 return si; 112 } 113 } 114 return 0; 115} 116 117void svcinfo_death(struct binder_state *bs, void *ptr) 118{ 119 struct svcinfo *si = ptr; 120 LOGI("service '%s' died\n", str8(si->name)); 121 if (si->ptr) { 122 binder_release(bs, si->ptr); 123 si->ptr = 0; 124 } 125} 126 127uint16_t svcmgr_id[] = { 128 'a','n','d','r','o','i','d','.','o','s','.', 129 'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r' 130}; 131 132 133void *do_find_service(struct binder_state *bs, uint16_t *s, unsigned len) 134{ 135 struct svcinfo *si; 136 si = find_svc(s, len); 137 138// LOGI("check_service('%s') ptr = %p\n", str8(s), si ? si->ptr : 0); 139 if (si && si->ptr) { 140 return si->ptr; 141 } else { 142 return 0; 143 } 144} 145 146int do_add_service(struct binder_state *bs, 147 uint16_t *s, unsigned len, 148 void *ptr, unsigned uid) 149{ 150 struct svcinfo *si; 151// LOGI("add_service('%s',%p) uid=%d\n", str8(s), ptr, uid); 152 153 if (!ptr || (len == 0) || (len > 127)) 154 return -1; 155 156 if (!svc_can_register(uid, s)) { 157 LOGE("add_service('%s',%p) uid=%d - PERMISSION DENIED\n", 158 str8(s), ptr, uid); 159 return -1; 160 } 161 162 si = find_svc(s, len); 163 if (si) { 164 if (si->ptr) { 165 LOGE("add_service('%s',%p) uid=%d - ALREADY REGISTERED, OVERRIDE\n", 166 str8(s), ptr, uid); 167 svcinfo_death(bs, si); 168 } 169 si->ptr = ptr; 170 } else { 171 si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t)); 172 if (!si) { 173 LOGE("add_service('%s',%p) uid=%d - OUT OF MEMORY\n", 174 str8(s), ptr, uid); 175 return -1; 176 } 177 si->ptr = ptr; 178 si->len = len; 179 memcpy(si->name, s, (len + 1) * sizeof(uint16_t)); 180 si->name[len] = '\0'; 181 si->death.func = svcinfo_death; 182 si->death.ptr = si; 183 si->next = svclist; 184 svclist = si; 185 } 186 187 binder_acquire(bs, ptr); 188 binder_link_to_death(bs, ptr, &si->death); 189 return 0; 190} 191 192int svcmgr_handler(struct binder_state *bs, 193 struct binder_txn *txn, 194 struct binder_io *msg, 195 struct binder_io *reply) 196{ 197 struct svcinfo *si; 198 uint16_t *s; 199 unsigned len; 200 void *ptr; 201 uint32_t strict_policy; 202 203// LOGI("target=%p code=%d pid=%d uid=%d\n", 204// txn->target, txn->code, txn->sender_pid, txn->sender_euid); 205 206 if (txn->target != svcmgr_handle) 207 return -1; 208 209 // Equivalent to Parcel::enforceInterface(), reading the RPC 210 // header with the strict mode policy mask and the interface name. 211 // Note that we ignore the strict_policy and don't propagate it 212 // further (since we do no outbound RPCs anyway). 213 strict_policy = bio_get_uint32(msg); 214 s = bio_get_string16(msg, &len); 215 if ((len != (sizeof(svcmgr_id) / 2)) || 216 memcmp(svcmgr_id, s, sizeof(svcmgr_id))) { 217 fprintf(stderr,"invalid id %s\n", str8(s)); 218 return -1; 219 } 220 221 switch(txn->code) { 222 case SVC_MGR_GET_SERVICE: 223 case SVC_MGR_CHECK_SERVICE: 224 s = bio_get_string16(msg, &len); 225 ptr = do_find_service(bs, s, len); 226 if (!ptr) 227 break; 228 bio_put_ref(reply, ptr); 229 return 0; 230 231 case SVC_MGR_ADD_SERVICE: 232 s = bio_get_string16(msg, &len); 233 ptr = bio_get_ref(msg); 234 if (do_add_service(bs, s, len, ptr, txn->sender_euid)) 235 return -1; 236 break; 237 238 case SVC_MGR_LIST_SERVICES: { 239 unsigned n = bio_get_uint32(msg); 240 241 si = svclist; 242 while ((n-- > 0) && si) 243 si = si->next; 244 if (si) { 245 bio_put_string16(reply, si->name); 246 return 0; 247 } 248 return -1; 249 } 250 default: 251 LOGE("unknown code %d\n", txn->code); 252 return -1; 253 } 254 255 bio_put_uint32(reply, 0); 256 return 0; 257} 258 259int main(int argc, char **argv) 260{ 261 struct binder_state *bs; 262 void *svcmgr = BINDER_SERVICE_MANAGER; 263 264 bs = binder_open(128*1024); 265 266 if (binder_become_context_manager(bs)) { 267 LOGE("cannot become context manager (%s)\n", strerror(errno)); 268 return -1; 269 } 270 271 svcmgr_handle = svcmgr; 272 binder_loop(bs, svcmgr_handler); 273 return 0; 274} 275