blob: 71e840e4fa6aa3fde97b8a4eb77fbeab9a901882 [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" },
Dake Gu710d7052012-10-01 12:35:58 -070034 { AID_MEDIA, "android.media.IAAHMetaDataService" },
aimitakeshid074e302010-07-29 10:12:27 +090035 { AID_DRM, "drm.drmManager" },
Nick Pellycd0e8392010-10-13 17:25:24 -070036 { AID_NFC, "nfc" },
Jaikumar Ganesh1abb1cb2012-01-25 16:14:50 -080037 { AID_BLUETOOTH, "bluetooth" },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 { 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" },
Hung-ying Tyan7e54ef72010-09-25 22:49:59 +080044 { AID_RADIO, "sip" },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045 { AID_RADIO, "isms" },
46 { AID_RADIO, "iphonesubinfo" },
47 { AID_RADIO, "simphonebook" },
Mike J. Chen6c929512011-08-15 11:59:47 -070048 { AID_MEDIA, "common_time.clock" },
49 { AID_MEDIA, "common_time.config" },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050};
51
52void *svcmgr_handle;
53
54const char *str8(uint16_t *x)
55{
56 static char buf[128];
57 unsigned max = 127;
58 char *p = buf;
59
60 if (x) {
61 while (*x && max--) {
62 *p++ = *x++;
63 }
64 }
65 *p++ = 0;
66 return buf;
67}
68
69int str16eq(uint16_t *a, const char *b)
70{
71 while (*a && *b)
72 if (*a++ != *b++) return 0;
73 if (*a || *b)
74 return 0;
75 return 1;
76}
77
78int svc_can_register(unsigned uid, uint16_t *name)
79{
80 unsigned n;
81
82 if ((uid == 0) || (uid == AID_SYSTEM))
83 return 1;
84
85 for (n = 0; n < sizeof(allowed) / sizeof(allowed[0]); n++)
86 if ((uid == allowed[n].uid) && str16eq(name, allowed[n].name))
87 return 1;
88
89 return 0;
90}
91
92struct svcinfo
93{
94 struct svcinfo *next;
95 void *ptr;
96 struct binder_death death;
Dianne Hackborna573f6a2012-02-09 16:12:18 -080097 int allow_isolated;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 unsigned len;
99 uint16_t name[0];
100};
101
102struct svcinfo *svclist = 0;
103
104struct svcinfo *find_svc(uint16_t *s16, unsigned len)
105{
106 struct svcinfo *si;
107
108 for (si = svclist; si; si = si->next) {
109 if ((len == si->len) &&
110 !memcmp(s16, si->name, len * sizeof(uint16_t))) {
111 return si;
112 }
113 }
114 return 0;
115}
116
117void svcinfo_death(struct binder_state *bs, void *ptr)
118{
119 struct svcinfo *si = ptr;
Steve Block6215d3f2012-01-04 20:05:49 +0000120 ALOGI("service '%s' died\n", str8(si->name));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 if (si->ptr) {
122 binder_release(bs, si->ptr);
123 si->ptr = 0;
124 }
125}
126
127uint16_t svcmgr_id[] = {
128 'a','n','d','r','o','i','d','.','o','s','.',
129 'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r'
130};
131
132
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800133void *do_find_service(struct binder_state *bs, uint16_t *s, unsigned len, unsigned uid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134{
135 struct svcinfo *si;
136 si = find_svc(s, len);
137
Steve Block6215d3f2012-01-04 20:05:49 +0000138// ALOGI("check_service('%s') ptr = %p\n", str8(s), si ? si->ptr : 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 if (si && si->ptr) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800140 if (!si->allow_isolated) {
141 // If this service doesn't allow access from isolated processes,
142 // then check the uid to see if it is isolated.
143 unsigned appid = uid % AID_USER;
144 if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) {
145 return 0;
146 }
147 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 return si->ptr;
149 } else {
150 return 0;
151 }
152}
153
154int do_add_service(struct binder_state *bs,
155 uint16_t *s, unsigned len,
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800156 void *ptr, unsigned uid, int allow_isolated)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157{
158 struct svcinfo *si;
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800159 //ALOGI("add_service('%s',%p,%s) uid=%d\n", str8(s), ptr,
160 // allow_isolated ? "allow_isolated" : "!allow_isolated", uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161
162 if (!ptr || (len == 0) || (len > 127))
163 return -1;
164
165 if (!svc_can_register(uid, s)) {
Steve Block3762c312012-01-06 19:20:56 +0000166 ALOGE("add_service('%s',%p) uid=%d - PERMISSION DENIED\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 str8(s), ptr, uid);
168 return -1;
169 }
170
171 si = find_svc(s, len);
172 if (si) {
173 if (si->ptr) {
Steve Block3762c312012-01-06 19:20:56 +0000174 ALOGE("add_service('%s',%p) uid=%d - ALREADY REGISTERED, OVERRIDE\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 str8(s), ptr, uid);
Iliyan Malchev1d884382010-12-08 11:21:24 -0800176 svcinfo_death(bs, si);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 }
178 si->ptr = ptr;
179 } else {
180 si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
181 if (!si) {
Steve Block3762c312012-01-06 19:20:56 +0000182 ALOGE("add_service('%s',%p) uid=%d - OUT OF MEMORY\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 str8(s), ptr, uid);
184 return -1;
185 }
186 si->ptr = ptr;
187 si->len = len;
188 memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
189 si->name[len] = '\0';
190 si->death.func = svcinfo_death;
191 si->death.ptr = si;
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800192 si->allow_isolated = allow_isolated;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 si->next = svclist;
194 svclist = si;
195 }
196
197 binder_acquire(bs, ptr);
198 binder_link_to_death(bs, ptr, &si->death);
199 return 0;
200}
201
202int svcmgr_handler(struct binder_state *bs,
203 struct binder_txn *txn,
204 struct binder_io *msg,
205 struct binder_io *reply)
206{
207 struct svcinfo *si;
208 uint16_t *s;
209 unsigned len;
210 void *ptr;
Brad Fitzpatrick27b3a7a2010-06-18 13:07:53 -0700211 uint32_t strict_policy;
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800212 int allow_isolated;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213
Steve Block6215d3f2012-01-04 20:05:49 +0000214// ALOGI("target=%p code=%d pid=%d uid=%d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215// txn->target, txn->code, txn->sender_pid, txn->sender_euid);
216
217 if (txn->target != svcmgr_handle)
218 return -1;
219
Brad Fitzpatrick27b3a7a2010-06-18 13:07:53 -0700220 // Equivalent to Parcel::enforceInterface(), reading the RPC
221 // header with the strict mode policy mask and the interface name.
222 // Note that we ignore the strict_policy and don't propagate it
223 // further (since we do no outbound RPCs anyway).
224 strict_policy = bio_get_uint32(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 s = bio_get_string16(msg, &len);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 if ((len != (sizeof(svcmgr_id) / 2)) ||
227 memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
228 fprintf(stderr,"invalid id %s\n", str8(s));
229 return -1;
230 }
231
232 switch(txn->code) {
233 case SVC_MGR_GET_SERVICE:
234 case SVC_MGR_CHECK_SERVICE:
235 s = bio_get_string16(msg, &len);
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800236 ptr = do_find_service(bs, s, len, txn->sender_euid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 if (!ptr)
238 break;
239 bio_put_ref(reply, ptr);
240 return 0;
241
242 case SVC_MGR_ADD_SERVICE:
243 s = bio_get_string16(msg, &len);
244 ptr = bio_get_ref(msg);
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800245 allow_isolated = bio_get_uint32(msg) ? 1 : 0;
246 if (do_add_service(bs, s, len, ptr, txn->sender_euid, allow_isolated))
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 return -1;
248 break;
249
250 case SVC_MGR_LIST_SERVICES: {
251 unsigned n = bio_get_uint32(msg);
252
253 si = svclist;
254 while ((n-- > 0) && si)
255 si = si->next;
256 if (si) {
257 bio_put_string16(reply, si->name);
258 return 0;
259 }
260 return -1;
261 }
262 default:
Steve Block3762c312012-01-06 19:20:56 +0000263 ALOGE("unknown code %d\n", txn->code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 return -1;
265 }
266
267 bio_put_uint32(reply, 0);
268 return 0;
269}
270
271int main(int argc, char **argv)
272{
273 struct binder_state *bs;
274 void *svcmgr = BINDER_SERVICE_MANAGER;
275
276 bs = binder_open(128*1024);
277
278 if (binder_become_context_manager(bs)) {
Steve Block3762c312012-01-06 19:20:56 +0000279 ALOGE("cannot become context manager (%s)\n", strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 return -1;
281 }
282
283 svcmgr_handle = svcmgr;
284 binder_loop(bs, svcmgr_handler);
285 return 0;
286}