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