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