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