blob: 1e32133b1b50f030b0d5d9cdfd715546f48a6e05 [file] [log] [blame]
Mike Lockwood94afecf2012-10-24 10:45:23 -07001/* 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 {
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000027 uid_t uid;
Mike Lockwood94afecf2012-10-24 10:45:23 -070028 const char *name;
29} allowed[] = {
30 { AID_MEDIA, "media.audio_flinger" },
Glenn Kasten64c8be02013-01-16 12:06:39 -080031 { AID_MEDIA, "media.log" },
Mike Lockwood94afecf2012-10-24 10:45:23 -070032 { AID_MEDIA, "media.player" },
33 { AID_MEDIA, "media.camera" },
34 { AID_MEDIA, "media.audio_policy" },
35 { AID_DRM, "drm.drmManager" },
36 { AID_NFC, "nfc" },
37 { AID_BLUETOOTH, "bluetooth" },
38 { AID_RADIO, "radio.phone" },
39 { AID_RADIO, "radio.sms" },
40 { AID_RADIO, "radio.phonesubinfo" },
41 { AID_RADIO, "radio.simphonebook" },
42/* TODO: remove after phone services are updated: */
43 { AID_RADIO, "phone" },
44 { AID_RADIO, "sip" },
45 { AID_RADIO, "isms" },
46 { AID_RADIO, "iphonesubinfo" },
47 { AID_RADIO, "simphonebook" },
48 { AID_MEDIA, "common_time.clock" },
49 { AID_MEDIA, "common_time.config" },
Kenny Root24440872012-11-14 15:42:38 -080050 { AID_KEYSTORE, "android.security.keystore" },
Mike Lockwood94afecf2012-10-24 10:45:23 -070051};
52
53void *svcmgr_handle;
54
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000055const char *str8(const uint16_t *x)
Mike Lockwood94afecf2012-10-24 10:45:23 -070056{
57 static char buf[128];
58 unsigned max = 127;
59 char *p = buf;
60
61 if (x) {
62 while (*x && max--) {
63 *p++ = *x++;
64 }
65 }
66 *p++ = 0;
67 return buf;
68}
69
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000070int str16eq(const uint16_t *a, const char *b)
Mike Lockwood94afecf2012-10-24 10:45:23 -070071{
72 while (*a && *b)
73 if (*a++ != *b++) return 0;
74 if (*a || *b)
75 return 0;
76 return 1;
77}
78
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000079int svc_can_register(uid_t uid, const uint16_t *name)
Mike Lockwood94afecf2012-10-24 10:45:23 -070080{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000081 size_t n;
82
Mike Lockwood94afecf2012-10-24 10:45:23 -070083 if ((uid == 0) || (uid == AID_SYSTEM))
84 return 1;
85
86 for (n = 0; n < sizeof(allowed) / sizeof(allowed[0]); n++)
87 if ((uid == allowed[n].uid) && str16eq(name, allowed[n].name))
88 return 1;
89
90 return 0;
91}
92
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000093struct svcinfo
Mike Lockwood94afecf2012-10-24 10:45:23 -070094{
95 struct svcinfo *next;
96 void *ptr;
97 struct binder_death death;
98 int allow_isolated;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000099 size_t len;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700100 uint16_t name[0];
101};
102
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000103struct svcinfo *svclist = NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700104
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000105struct svcinfo *find_svc(const uint16_t *s16, size_t len)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700106{
107 struct svcinfo *si;
108
109 for (si = svclist; si; si = si->next) {
110 if ((len == si->len) &&
111 !memcmp(s16, si->name, len * sizeof(uint16_t))) {
112 return si;
113 }
114 }
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000115 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700116}
117
118void svcinfo_death(struct binder_state *bs, void *ptr)
119{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000120 struct svcinfo *si = (struct svcinfo* ) ptr;
121
Mike Lockwood94afecf2012-10-24 10:45:23 -0700122 ALOGI("service '%s' died\n", str8(si->name));
123 if (si->ptr) {
124 binder_release(bs, si->ptr);
125 si->ptr = 0;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000126 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700127}
128
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000129uint16_t svcmgr_id[] = {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700130 'a','n','d','r','o','i','d','.','o','s','.',
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000131 'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r'
Mike Lockwood94afecf2012-10-24 10:45:23 -0700132};
Mike Lockwood94afecf2012-10-24 10:45:23 -0700133
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000134
135void *do_find_service(struct binder_state *bs, const uint16_t *s, size_t len, uid_t uid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700136{
137 struct svcinfo *si;
138 si = find_svc(s, len);
139
140// ALOGI("check_service('%s') ptr = %p\n", str8(s), si ? si->ptr : 0);
141 if (si && si->ptr) {
142 if (!si->allow_isolated) {
143 // If this service doesn't allow access from isolated processes,
144 // then check the uid to see if it is isolated.
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000145 uid_t appid = uid % AID_USER;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700146 if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) {
147 return 0;
148 }
149 }
150 return si->ptr;
151 } else {
152 return 0;
153 }
154}
155
156int do_add_service(struct binder_state *bs,
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000157 const uint16_t *s, size_t len,
158 void *ptr, uid_t uid, int allow_isolated)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700159{
160 struct svcinfo *si;
161 //ALOGI("add_service('%s',%p,%s) uid=%d\n", str8(s), ptr,
162 // allow_isolated ? "allow_isolated" : "!allow_isolated", uid);
163
164 if (!ptr || (len == 0) || (len > 127))
165 return -1;
166
167 if (!svc_can_register(uid, s)) {
168 ALOGE("add_service('%s',%p) uid=%d - PERMISSION DENIED\n",
169 str8(s), ptr, uid);
170 return -1;
171 }
172
173 si = find_svc(s, len);
174 if (si) {
175 if (si->ptr) {
176 ALOGE("add_service('%s',%p) uid=%d - ALREADY REGISTERED, OVERRIDE\n",
177 str8(s), ptr, uid);
178 svcinfo_death(bs, si);
179 }
180 si->ptr = ptr;
181 } else {
182 si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
183 if (!si) {
184 ALOGE("add_service('%s',%p) uid=%d - OUT OF MEMORY\n",
185 str8(s), ptr, uid);
186 return -1;
187 }
188 si->ptr = ptr;
189 si->len = len;
190 memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
191 si->name[len] = '\0';
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000192 si->death.func = (void*) svcinfo_death;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700193 si->death.ptr = si;
194 si->allow_isolated = allow_isolated;
195 si->next = svclist;
196 svclist = si;
197 }
198
199 binder_acquire(bs, ptr);
200 binder_link_to_death(bs, ptr, &si->death);
201 return 0;
202}
203
204int svcmgr_handler(struct binder_state *bs,
Serban Constantinescubcf38882014-01-10 13:56:27 +0000205 struct binder_transaction_data *txn,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700206 struct binder_io *msg,
207 struct binder_io *reply)
208{
209 struct svcinfo *si;
210 uint16_t *s;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000211 size_t len;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700212 void *ptr;
213 uint32_t strict_policy;
214 int allow_isolated;
215
216// ALOGI("target=%p code=%d pid=%d uid=%d\n",
217// txn->target, txn->code, txn->sender_pid, txn->sender_euid);
218
Serban Constantinescubcf38882014-01-10 13:56:27 +0000219 if (txn->target.handle != svcmgr_handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700220 return -1;
221
222 // Equivalent to Parcel::enforceInterface(), reading the RPC
223 // header with the strict mode policy mask and the interface name.
224 // Note that we ignore the strict_policy and don't propagate it
225 // further (since we do no outbound RPCs anyway).
226 strict_policy = bio_get_uint32(msg);
227 s = bio_get_string16(msg, &len);
228 if ((len != (sizeof(svcmgr_id) / 2)) ||
229 memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
230 fprintf(stderr,"invalid id %s\n", str8(s));
231 return -1;
232 }
233
234 switch(txn->code) {
235 case SVC_MGR_GET_SERVICE:
236 case SVC_MGR_CHECK_SERVICE:
237 s = bio_get_string16(msg, &len);
238 ptr = do_find_service(bs, s, len, txn->sender_euid);
239 if (!ptr)
240 break;
241 bio_put_ref(reply, ptr);
242 return 0;
243
244 case SVC_MGR_ADD_SERVICE:
245 s = bio_get_string16(msg, &len);
246 ptr = bio_get_ref(msg);
247 allow_isolated = bio_get_uint32(msg) ? 1 : 0;
248 if (do_add_service(bs, s, len, ptr, txn->sender_euid, allow_isolated))
249 return -1;
250 break;
251
252 case SVC_MGR_LIST_SERVICES: {
253 unsigned n = bio_get_uint32(msg);
254
255 si = svclist;
256 while ((n-- > 0) && si)
257 si = si->next;
258 if (si) {
259 bio_put_string16(reply, si->name);
260 return 0;
261 }
262 return -1;
263 }
264 default:
265 ALOGE("unknown code %d\n", txn->code);
266 return -1;
267 }
268
269 bio_put_uint32(reply, 0);
270 return 0;
271}
272
273int main(int argc, char **argv)
274{
275 struct binder_state *bs;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700276
277 bs = binder_open(128*1024);
Serban Constantinescua44542c2014-01-30 15:16:45 +0000278 if (!bs) {
279 ALOGE("failed to open binder driver\n");
280 return -1;
281 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700282
283 if (binder_become_context_manager(bs)) {
284 ALOGE("cannot become context manager (%s)\n", strerror(errno));
285 return -1;
286 }
287
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000288 svcmgr_handle = BINDER_SERVICE_MANAGER;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700289 binder_loop(bs, svcmgr_handler);
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000290
Mike Lockwood94afecf2012-10-24 10:45:23 -0700291 return 0;
292}