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.log" },
32 { AID_MEDIA, "media.player" },
33 { AID_MEDIA, "media.camera" },
34 { AID_MEDIA, "media.audio_policy" },
35 { AID_AUDIO, "audio" },
36 { AID_INPUT, "input" },
37 { AID_DRM, "drm.drmManager" },
39 { AID_BLUETOOTH, "bluetooth" },
40 { AID_RADIO, "radio.phone" },
41 { AID_RADIO, "radio.sms" },
42 { AID_RADIO, "radio.phonesubinfo" },
43 { AID_RADIO, "radio.simphonebook" },
44 /* TODO: remove after phone services are updated: */
45 { AID_RADIO, "phone" },
47 { AID_RADIO, "isms" },
48 { AID_RADIO, "iphonesubinfo" },
49 { AID_RADIO, "simphonebook" },
50 { AID_MEDIA, "common_time.clock" },
51 { AID_MEDIA, "common_time.config" },
52 { AID_KEYSTORE, "android.security.keystore" },
55 uint32_t svcmgr_handle;
57 const char *str8(const uint16_t *x)
72 int str16eq(const uint16_t *a, const char *b)
75 if (*a++ != *b++) return 0;
81 int svc_can_register(uid_t uid, const uint16_t *name)
85 if ((uid == 0) || (uid == AID_SYSTEM))
88 for (n = 0; n < sizeof(allowed) / sizeof(allowed[0]); n++)
89 if ((uid == allowed[n].uid) && str16eq(name, allowed[n].name))
99 struct binder_death death;
105 struct svcinfo *svclist = NULL;
107 struct svcinfo *find_svc(const uint16_t *s16, size_t len)
111 for (si = svclist; si; si = si->next) {
112 if ((len == si->len) &&
113 !memcmp(s16, si->name, len * sizeof(uint16_t))) {
120 void svcinfo_death(struct binder_state *bs, void *ptr)
122 struct svcinfo *si = (struct svcinfo* ) ptr;
124 ALOGI("service '%s' died\n", str8(si->name));
126 binder_release(bs, si->handle);
131 uint16_t svcmgr_id[] = {
132 'a','n','d','r','o','i','d','.','o','s','.',
133 'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r'
137 uint32_t do_find_service(struct binder_state *bs, const uint16_t *s, size_t len, uid_t uid)
141 si = find_svc(s, len);
142 //ALOGI("check_service('%s') handle = %x\n", str8(s), si ? si->handle : 0);
143 if (si && si->handle) {
144 if (!si->allow_isolated) {
145 // If this service doesn't allow access from isolated processes,
146 // then check the uid to see if it is isolated.
147 uid_t appid = uid % AID_USER;
148 if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) {
158 int do_add_service(struct binder_state *bs,
159 const uint16_t *s, size_t len,
160 uint32_t handle, uid_t uid, int allow_isolated)
164 //ALOGI("add_service('%s',%x,%s) uid=%d\n", str8(s), handle,
165 // allow_isolated ? "allow_isolated" : "!allow_isolated", uid);
167 if (!handle || (len == 0) || (len > 127))
170 if (!svc_can_register(uid, s)) {
171 ALOGE("add_service('%s',%x) uid=%d - PERMISSION DENIED\n",
172 str8(s), handle, uid);
176 si = find_svc(s, len);
179 ALOGE("add_service('%s',%x) uid=%d - ALREADY REGISTERED, OVERRIDE\n",
180 str8(s), handle, uid);
181 svcinfo_death(bs, si);
185 si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
187 ALOGE("add_service('%s',%x) uid=%d - OUT OF MEMORY\n",
188 str8(s), handle, uid);
193 memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
194 si->name[len] = '\0';
195 si->death.func = (void*) svcinfo_death;
197 si->allow_isolated = allow_isolated;
202 binder_acquire(bs, handle);
203 binder_link_to_death(bs, handle, &si->death);
207 int svcmgr_handler(struct binder_state *bs,
208 struct binder_transaction_data *txn,
209 struct binder_io *msg,
210 struct binder_io *reply)
216 uint32_t strict_policy;
219 //ALOGI("target=%x code=%d pid=%d uid=%d\n",
220 // txn->target.handle, txn->code, txn->sender_pid, txn->sender_euid);
222 if (txn->target.handle != svcmgr_handle)
225 if (txn->code == PING_TRANSACTION)
228 // Equivalent to Parcel::enforceInterface(), reading the RPC
229 // header with the strict mode policy mask and the interface name.
230 // Note that we ignore the strict_policy and don't propagate it
231 // further (since we do no outbound RPCs anyway).
232 strict_policy = bio_get_uint32(msg);
233 s = bio_get_string16(msg, &len);
234 if ((len != (sizeof(svcmgr_id) / 2)) ||
235 memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
236 fprintf(stderr,"invalid id %s\n", str8(s));
241 case SVC_MGR_GET_SERVICE:
242 case SVC_MGR_CHECK_SERVICE:
243 s = bio_get_string16(msg, &len);
244 handle = do_find_service(bs, s, len, txn->sender_euid);
247 bio_put_ref(reply, handle);
250 case SVC_MGR_ADD_SERVICE:
251 s = bio_get_string16(msg, &len);
252 handle = bio_get_ref(msg);
253 allow_isolated = bio_get_uint32(msg) ? 1 : 0;
254 if (do_add_service(bs, s, len, handle, txn->sender_euid, allow_isolated))
258 case SVC_MGR_LIST_SERVICES: {
259 uint32_t n = bio_get_uint32(msg);
262 while ((n-- > 0) && si)
265 bio_put_string16(reply, si->name);
271 ALOGE("unknown code %d\n", txn->code);
275 bio_put_uint32(reply, 0);
279 int main(int argc, char **argv)
281 struct binder_state *bs;
283 bs = binder_open(128*1024);
285 ALOGE("failed to open binder driver\n");
289 if (binder_become_context_manager(bs)) {
290 ALOGE("cannot become context manager (%s)\n", strerror(errno));
294 svcmgr_handle = BINDER_SERVICE_MANAGER;
295 binder_loop(bs, svcmgr_handler);