1 /* Copyright 2008 The Android Open Source Project
9 #include <private/android_filesystem_config.h>
14 #define ALOGI(x...) fprintf(stderr, "svcmgr: " x)
15 #define ALOGE(x...) fprintf(stderr, "svcmgr: " x)
17 #define LOG_TAG "ServiceManager"
18 #include <cutils/log.h>
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)
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" },
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" },
44 { AID_RADIO, "isms" },
45 { AID_RADIO, "iphonesubinfo" },
46 { AID_RADIO, "simphonebook" },
47 { AID_MEDIA, "common_time.clock" },
48 { AID_MEDIA, "common_time.config" },
53 const char *str8(uint16_t *x)
68 int str16eq(uint16_t *a, const char *b)
71 if (*a++ != *b++) return 0;
77 int svc_can_register(unsigned uid, uint16_t *name)
81 if ((uid == 0) || (uid == AID_SYSTEM))
84 for (n = 0; n < sizeof(allowed) / sizeof(allowed[0]); n++)
85 if ((uid == allowed[n].uid) && str16eq(name, allowed[n].name))
95 struct binder_death death;
101 struct svcinfo *svclist = 0;
103 struct svcinfo *find_svc(uint16_t *s16, unsigned len)
107 for (si = svclist; si; si = si->next) {
108 if ((len == si->len) &&
109 !memcmp(s16, si->name, len * sizeof(uint16_t))) {
116 void svcinfo_death(struct binder_state *bs, void *ptr)
118 struct svcinfo *si = ptr;
119 ALOGI("service '%s' died\n", str8(si->name));
121 binder_release(bs, si->ptr);
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'
132 void *do_find_service(struct binder_state *bs, uint16_t *s, unsigned len, unsigned uid)
135 si = find_svc(s, len);
137 // ALOGI("check_service('%s') ptr = %p\n", str8(s), si ? si->ptr : 0);
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) {
153 int do_add_service(struct binder_state *bs,
154 uint16_t *s, unsigned len,
155 void *ptr, unsigned uid, int allow_isolated)
158 //ALOGI("add_service('%s',%p,%s) uid=%d\n", str8(s), ptr,
159 // allow_isolated ? "allow_isolated" : "!allow_isolated", uid);
161 if (!ptr || (len == 0) || (len > 127))
164 if (!svc_can_register(uid, s)) {
165 ALOGE("add_service('%s',%p) uid=%d - PERMISSION DENIED\n",
170 si = find_svc(s, len);
173 ALOGE("add_service('%s',%p) uid=%d - ALREADY REGISTERED, OVERRIDE\n",
175 svcinfo_death(bs, si);
179 si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
181 ALOGE("add_service('%s',%p) uid=%d - OUT OF MEMORY\n",
187 memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
188 si->name[len] = '\0';
189 si->death.func = svcinfo_death;
191 si->allow_isolated = allow_isolated;
196 binder_acquire(bs, ptr);
197 binder_link_to_death(bs, ptr, &si->death);
201 int svcmgr_handler(struct binder_state *bs,
202 struct binder_txn *txn,
203 struct binder_io *msg,
204 struct binder_io *reply)
210 uint32_t strict_policy;
213 // ALOGI("target=%p code=%d pid=%d uid=%d\n",
214 // txn->target, txn->code, txn->sender_pid, txn->sender_euid);
216 if (txn->target != svcmgr_handle)
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));
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);
238 bio_put_ref(reply, ptr);
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))
249 case SVC_MGR_LIST_SERVICES: {
250 unsigned n = bio_get_uint32(msg);
253 while ((n-- > 0) && si)
256 bio_put_string16(reply, si->name);
262 ALOGE("unknown code %d\n", txn->code);
266 bio_put_uint32(reply, 0);
270 int main(int argc, char **argv)
272 struct binder_state *bs;
273 void *svcmgr = BINDER_SERVICE_MANAGER;
275 bs = binder_open(128*1024);
277 if (binder_become_context_manager(bs)) {
278 ALOGE("cannot become context manager (%s)\n", strerror(errno));
282 svcmgr_handle = svcmgr;
283 binder_loop(bs, svcmgr_handler);