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