blob: 3314dc3acc5952e1de9e7e28cff8c987e1142f45 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/* 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
Steve Block6215d3f2012-01-04 20:05:49 +000014#define ALOGI(x...) fprintf(stderr, "svcmgr: " x)
Steve Block3762c312012-01-06 19:20:56 +000015#define ALOGE(x...) fprintf(stderr, "svcmgr: " x)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016#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" },
31 { AID_MEDIA, "media.player" },
32 { AID_MEDIA, "media.camera" },
Eric Laurenta553c252009-07-17 12:17:14 -070033 { AID_MEDIA, "media.audio_policy" },
aimitakeshid074e302010-07-29 10:12:27 +090034 { AID_DRM, "drm.drmManager" },
Nick Pellycd0e8392010-10-13 17:25:24 -070035 { AID_NFC, "nfc" },
Jaikumar Ganesh1abb1cb2012-01-25 16:14:50 -080036 { AID_BLUETOOTH, "bluetooth" },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037 { 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" },
Hung-ying Tyan7e54ef72010-09-25 22:49:59 +080043 { AID_RADIO, "sip" },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044 { AID_RADIO, "isms" },
45 { AID_RADIO, "iphonesubinfo" },
46 { AID_RADIO, "simphonebook" },
Mike J. Chen6c929512011-08-15 11:59:47 -070047 { AID_MEDIA, "common_time.clock" },
48 { AID_MEDIA, "common_time.config" },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049};
50
51void *svcmgr_handle;
52
53const char *str8(uint16_t *x)
54{
55 static char buf[128];
56 unsigned max = 127;
57 char *p = buf;
58
59 if (x) {
60 while (*x && max--) {
61 *p++ = *x++;
62 }
63 }
64 *p++ = 0;
65 return buf;
66}
67
68int str16eq(uint16_t *a, const char *b)
69{
70 while (*a && *b)
71 if (*a++ != *b++) return 0;
72 if (*a || *b)
73 return 0;
74 return 1;
75}
76
77int svc_can_register(unsigned uid, uint16_t *name)
78{
79 unsigned n;
80
81 if ((uid == 0) || (uid == AID_SYSTEM))
82 return 1;
83
84 for (n = 0; n < sizeof(allowed) / sizeof(allowed[0]); n++)
85 if ((uid == allowed[n].uid) && str16eq(name, allowed[n].name))
86 return 1;
87
88 return 0;
89}
90
91struct svcinfo
92{
93 struct svcinfo *next;
94 void *ptr;
95 struct binder_death death;
Dianne Hackborna573f6a2012-02-09 16:12:18 -080096 int allow_isolated;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 unsigned len;
98 uint16_t name[0];
99};
100
101struct svcinfo *svclist = 0;
102
103struct svcinfo *find_svc(uint16_t *s16, unsigned len)
104{
105 struct svcinfo *si;
106
107 for (si = svclist; si; si = si->next) {
108 if ((len == si->len) &&
109 !memcmp(s16, si->name, len * sizeof(uint16_t))) {
110 return si;
111 }
112 }
113 return 0;
114}
115
116void svcinfo_death(struct binder_state *bs, void *ptr)
117{
118 struct svcinfo *si = ptr;
Steve Block6215d3f2012-01-04 20:05:49 +0000119 ALOGI("service '%s' died\n", str8(si->name));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 if (si->ptr) {
121 binder_release(bs, si->ptr);
122 si->ptr = 0;
123 }
124}
125
126uint16_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'
129};
130
131
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800132void *do_find_service(struct binder_state *bs, uint16_t *s, unsigned len, unsigned uid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133{
134 struct svcinfo *si;
135 si = find_svc(s, len);
136
Steve Block6215d3f2012-01-04 20:05:49 +0000137// ALOGI("check_service('%s') ptr = %p\n", str8(s), si ? si->ptr : 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 if (si && si->ptr) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800139 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) {
144 return 0;
145 }
146 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 return si->ptr;
148 } else {
149 return 0;
150 }
151}
152
153int do_add_service(struct binder_state *bs,
154 uint16_t *s, unsigned len,
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800155 void *ptr, unsigned uid, int allow_isolated)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156{
157 struct svcinfo *si;
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800158 //ALOGI("add_service('%s',%p,%s) uid=%d\n", str8(s), ptr,
159 // allow_isolated ? "allow_isolated" : "!allow_isolated", uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160
161 if (!ptr || (len == 0) || (len > 127))
162 return -1;
163
164 if (!svc_can_register(uid, s)) {
Steve Block3762c312012-01-06 19:20:56 +0000165 ALOGE("add_service('%s',%p) uid=%d - PERMISSION DENIED\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 str8(s), ptr, uid);
167 return -1;
168 }
169
170 si = find_svc(s, len);
171 if (si) {
172 if (si->ptr) {
Steve Block3762c312012-01-06 19:20:56 +0000173 ALOGE("add_service('%s',%p) uid=%d - ALREADY REGISTERED, OVERRIDE\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 str8(s), ptr, uid);
Iliyan Malchev1d884382010-12-08 11:21:24 -0800175 svcinfo_death(bs, si);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 }
177 si->ptr = ptr;
178 } else {
179 si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
180 if (!si) {
Steve Block3762c312012-01-06 19:20:56 +0000181 ALOGE("add_service('%s',%p) uid=%d - OUT OF MEMORY\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 str8(s), ptr, uid);
183 return -1;
184 }
185 si->ptr = ptr;
186 si->len = len;
187 memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
188 si->name[len] = '\0';
189 si->death.func = svcinfo_death;
190 si->death.ptr = si;
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800191 si->allow_isolated = allow_isolated;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 si->next = svclist;
193 svclist = si;
194 }
195
196 binder_acquire(bs, ptr);
197 binder_link_to_death(bs, ptr, &si->death);
198 return 0;
199}
200
201int svcmgr_handler(struct binder_state *bs,
202 struct binder_txn *txn,
203 struct binder_io *msg,
204 struct binder_io *reply)
205{
206 struct svcinfo *si;
207 uint16_t *s;
208 unsigned len;
209 void *ptr;
Brad Fitzpatrick27b3a7a2010-06-18 13:07:53 -0700210 uint32_t strict_policy;
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800211 int allow_isolated;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212
Steve Block6215d3f2012-01-04 20:05:49 +0000213// ALOGI("target=%p code=%d pid=%d uid=%d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214// txn->target, txn->code, txn->sender_pid, txn->sender_euid);
215
216 if (txn->target != svcmgr_handle)
217 return -1;
218
Brad Fitzpatrick27b3a7a2010-06-18 13:07:53 -0700219 // 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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 s = bio_get_string16(msg, &len);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 if ((len != (sizeof(svcmgr_id) / 2)) ||
226 memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
227 fprintf(stderr,"invalid id %s\n", str8(s));
228 return -1;
229 }
230
231 switch(txn->code) {
232 case SVC_MGR_GET_SERVICE:
233 case SVC_MGR_CHECK_SERVICE:
234 s = bio_get_string16(msg, &len);
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800235 ptr = do_find_service(bs, s, len, txn->sender_euid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 if (!ptr)
237 break;
238 bio_put_ref(reply, ptr);
239 return 0;
240
241 case SVC_MGR_ADD_SERVICE:
242 s = bio_get_string16(msg, &len);
243 ptr = bio_get_ref(msg);
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800244 allow_isolated = bio_get_uint32(msg) ? 1 : 0;
245 if (do_add_service(bs, s, len, ptr, txn->sender_euid, allow_isolated))
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 return -1;
247 break;
248
249 case SVC_MGR_LIST_SERVICES: {
250 unsigned n = bio_get_uint32(msg);
251
252 si = svclist;
253 while ((n-- > 0) && si)
254 si = si->next;
255 if (si) {
256 bio_put_string16(reply, si->name);
257 return 0;
258 }
259 return -1;
260 }
261 default:
Steve Block3762c312012-01-06 19:20:56 +0000262 ALOGE("unknown code %d\n", txn->code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 return -1;
264 }
265
266 bio_put_uint32(reply, 0);
267 return 0;
268}
269
270int main(int argc, char **argv)
271{
272 struct binder_state *bs;
273 void *svcmgr = BINDER_SERVICE_MANAGER;
274
275 bs = binder_open(128*1024);
276
277 if (binder_become_context_manager(bs)) {
Steve Block3762c312012-01-06 19:20:56 +0000278 ALOGE("cannot become context manager (%s)\n", strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 return -1;
280 }
281
282 svcmgr_handle = svcmgr;
283 binder_loop(bs, svcmgr_handler);
284 return 0;
285}