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