blob: 5dd38116e623d158d8ed2d79d81c616de8431898 [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 Lockwood94afecf2012-10-24 10:45:23 -070036 { AID_DRM, "drm.drmManager" },
37 { AID_NFC, "nfc" },
38 { AID_BLUETOOTH, "bluetooth" },
39 { AID_RADIO, "radio.phone" },
40 { AID_RADIO, "radio.sms" },
41 { AID_RADIO, "radio.phonesubinfo" },
42 { AID_RADIO, "radio.simphonebook" },
43/* TODO: remove after phone services are updated: */
44 { AID_RADIO, "phone" },
45 { AID_RADIO, "sip" },
46 { AID_RADIO, "isms" },
47 { AID_RADIO, "iphonesubinfo" },
48 { AID_RADIO, "simphonebook" },
49 { AID_MEDIA, "common_time.clock" },
50 { AID_MEDIA, "common_time.config" },
Kenny Root24440872012-11-14 15:42:38 -080051 { AID_KEYSTORE, "android.security.keystore" },
Mike Lockwood94afecf2012-10-24 10:45:23 -070052};
53
54void *svcmgr_handle;
55
56const char *str8(uint16_t *x)
57{
58 static char buf[128];
59 unsigned max = 127;
60 char *p = buf;
61
62 if (x) {
63 while (*x && max--) {
64 *p++ = *x++;
65 }
66 }
67 *p++ = 0;
68 return buf;
69}
70
71int str16eq(uint16_t *a, const char *b)
72{
73 while (*a && *b)
74 if (*a++ != *b++) return 0;
75 if (*a || *b)
76 return 0;
77 return 1;
78}
79
80int svc_can_register(unsigned uid, uint16_t *name)
81{
82 unsigned n;
83
84 if ((uid == 0) || (uid == AID_SYSTEM))
85 return 1;
86
87 for (n = 0; n < sizeof(allowed) / sizeof(allowed[0]); n++)
88 if ((uid == allowed[n].uid) && str16eq(name, allowed[n].name))
89 return 1;
90
91 return 0;
92}
93
94struct svcinfo
95{
96 struct svcinfo *next;
97 void *ptr;
98 struct binder_death death;
99 int allow_isolated;
100 unsigned len;
101 uint16_t name[0];
102};
103
104struct svcinfo *svclist = 0;
105
106struct svcinfo *find_svc(uint16_t *s16, unsigned len)
107{
108 struct svcinfo *si;
109
110 for (si = svclist; si; si = si->next) {
111 if ((len == si->len) &&
112 !memcmp(s16, si->name, len * sizeof(uint16_t))) {
113 return si;
114 }
115 }
116 return 0;
117}
118
119void svcinfo_death(struct binder_state *bs, void *ptr)
120{
121 struct svcinfo *si = ptr;
122 ALOGI("service '%s' died\n", str8(si->name));
123 if (si->ptr) {
124 binder_release(bs, si->ptr);
125 si->ptr = 0;
126 }
127}
128
129uint16_t svcmgr_id[] = {
130 'a','n','d','r','o','i','d','.','o','s','.',
131 'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r'
132};
133
134
135void *do_find_service(struct binder_state *bs, uint16_t *s, unsigned len, unsigned uid)
136{
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.
145 unsigned appid = uid % AID_USER;
146 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,
157 uint16_t *s, unsigned len,
158 void *ptr, unsigned uid, int allow_isolated)
159{
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';
192 si->death.func = svcinfo_death;
193 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,
205 struct binder_txn *txn,
206 struct binder_io *msg,
207 struct binder_io *reply)
208{
209 struct svcinfo *si;
210 uint16_t *s;
211 unsigned len;
212 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
219 if (txn->target != svcmgr_handle)
220 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;
276 void *svcmgr = BINDER_SERVICE_MANAGER;
277
278 bs = binder_open(128*1024);
279
280 if (binder_become_context_manager(bs)) {
281 ALOGE("cannot become context manager (%s)\n", strerror(errno));
282 return -1;
283 }
284
285 svcmgr_handle = svcmgr;
286 binder_loop(bs, svcmgr_handler);
287 return 0;
288}