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