blob: cfc2d16116285d492cab3830dc61c7781cd3941d [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" },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036 { AID_RADIO, "radio.phone" },
37 { AID_RADIO, "radio.sms" },
38 { AID_RADIO, "radio.phonesubinfo" },
39 { AID_RADIO, "radio.simphonebook" },
40/* TODO: remove after phone services are updated: */
41 { AID_RADIO, "phone" },
Hung-ying Tyan7e54ef72010-09-25 22:49:59 +080042 { AID_RADIO, "sip" },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 { AID_RADIO, "isms" },
44 { AID_RADIO, "iphonesubinfo" },
45 { AID_RADIO, "simphonebook" },
46};
47
48void *svcmgr_handle;
49
50const char *str8(uint16_t *x)
51{
52 static char buf[128];
53 unsigned max = 127;
54 char *p = buf;
55
56 if (x) {
57 while (*x && max--) {
58 *p++ = *x++;
59 }
60 }
61 *p++ = 0;
62 return buf;
63}
64
65int str16eq(uint16_t *a, const char *b)
66{
67 while (*a && *b)
68 if (*a++ != *b++) return 0;
69 if (*a || *b)
70 return 0;
71 return 1;
72}
73
74int svc_can_register(unsigned uid, uint16_t *name)
75{
76 unsigned n;
77
78 if ((uid == 0) || (uid == AID_SYSTEM))
79 return 1;
80
81 for (n = 0; n < sizeof(allowed) / sizeof(allowed[0]); n++)
82 if ((uid == allowed[n].uid) && str16eq(name, allowed[n].name))
83 return 1;
84
85 return 0;
86}
87
88struct svcinfo
89{
90 struct svcinfo *next;
91 void *ptr;
92 struct binder_death death;
Dianne Hackborna573f6a2012-02-09 16:12:18 -080093 int allow_isolated;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 unsigned len;
95 uint16_t name[0];
96};
97
98struct svcinfo *svclist = 0;
99
100struct svcinfo *find_svc(uint16_t *s16, unsigned len)
101{
102 struct svcinfo *si;
103
104 for (si = svclist; si; si = si->next) {
105 if ((len == si->len) &&
106 !memcmp(s16, si->name, len * sizeof(uint16_t))) {
107 return si;
108 }
109 }
110 return 0;
111}
112
113void svcinfo_death(struct binder_state *bs, void *ptr)
114{
115 struct svcinfo *si = ptr;
Steve Block6215d3f2012-01-04 20:05:49 +0000116 ALOGI("service '%s' died\n", str8(si->name));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 if (si->ptr) {
118 binder_release(bs, si->ptr);
119 si->ptr = 0;
120 }
121}
122
123uint16_t svcmgr_id[] = {
124 'a','n','d','r','o','i','d','.','o','s','.',
125 'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r'
126};
127
128
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800129void *do_find_service(struct binder_state *bs, uint16_t *s, unsigned len, unsigned uid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130{
131 struct svcinfo *si;
132 si = find_svc(s, len);
133
Steve Block6215d3f2012-01-04 20:05:49 +0000134// ALOGI("check_service('%s') ptr = %p\n", str8(s), si ? si->ptr : 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 if (si && si->ptr) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800136 if (!si->allow_isolated) {
137 // If this service doesn't allow access from isolated processes,
138 // then check the uid to see if it is isolated.
139 unsigned appid = uid % AID_USER;
140 if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) {
141 return 0;
142 }
143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 return si->ptr;
145 } else {
146 return 0;
147 }
148}
149
150int do_add_service(struct binder_state *bs,
151 uint16_t *s, unsigned len,
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800152 void *ptr, unsigned uid, int allow_isolated)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153{
154 struct svcinfo *si;
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800155 //ALOGI("add_service('%s',%p,%s) uid=%d\n", str8(s), ptr,
156 // allow_isolated ? "allow_isolated" : "!allow_isolated", uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157
158 if (!ptr || (len == 0) || (len > 127))
159 return -1;
160
161 if (!svc_can_register(uid, s)) {
Steve Block3762c312012-01-06 19:20:56 +0000162 ALOGE("add_service('%s',%p) uid=%d - PERMISSION DENIED\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 str8(s), ptr, uid);
164 return -1;
165 }
166
167 si = find_svc(s, len);
168 if (si) {
169 if (si->ptr) {
Steve Block3762c312012-01-06 19:20:56 +0000170 ALOGE("add_service('%s',%p) uid=%d - ALREADY REGISTERED, OVERRIDE\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 str8(s), ptr, uid);
Iliyan Malchev1d884382010-12-08 11:21:24 -0800172 svcinfo_death(bs, si);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 }
174 si->ptr = ptr;
175 } else {
176 si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
177 if (!si) {
Steve Block3762c312012-01-06 19:20:56 +0000178 ALOGE("add_service('%s',%p) uid=%d - OUT OF MEMORY\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 str8(s), ptr, uid);
180 return -1;
181 }
182 si->ptr = ptr;
183 si->len = len;
184 memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
185 si->name[len] = '\0';
186 si->death.func = svcinfo_death;
187 si->death.ptr = si;
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800188 si->allow_isolated = allow_isolated;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 si->next = svclist;
190 svclist = si;
191 }
192
193 binder_acquire(bs, ptr);
194 binder_link_to_death(bs, ptr, &si->death);
195 return 0;
196}
197
198int svcmgr_handler(struct binder_state *bs,
199 struct binder_txn *txn,
200 struct binder_io *msg,
201 struct binder_io *reply)
202{
203 struct svcinfo *si;
204 uint16_t *s;
205 unsigned len;
206 void *ptr;
Brad Fitzpatrick27b3a7a2010-06-18 13:07:53 -0700207 uint32_t strict_policy;
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800208 int allow_isolated;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209
Steve Block6215d3f2012-01-04 20:05:49 +0000210// ALOGI("target=%p code=%d pid=%d uid=%d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211// txn->target, txn->code, txn->sender_pid, txn->sender_euid);
212
213 if (txn->target != svcmgr_handle)
214 return -1;
215
Brad Fitzpatrick27b3a7a2010-06-18 13:07:53 -0700216 // Equivalent to Parcel::enforceInterface(), reading the RPC
217 // header with the strict mode policy mask and the interface name.
218 // Note that we ignore the strict_policy and don't propagate it
219 // further (since we do no outbound RPCs anyway).
220 strict_policy = bio_get_uint32(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 s = bio_get_string16(msg, &len);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 if ((len != (sizeof(svcmgr_id) / 2)) ||
223 memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
224 fprintf(stderr,"invalid id %s\n", str8(s));
225 return -1;
226 }
227
228 switch(txn->code) {
229 case SVC_MGR_GET_SERVICE:
230 case SVC_MGR_CHECK_SERVICE:
231 s = bio_get_string16(msg, &len);
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800232 ptr = do_find_service(bs, s, len, txn->sender_euid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 if (!ptr)
234 break;
235 bio_put_ref(reply, ptr);
236 return 0;
237
238 case SVC_MGR_ADD_SERVICE:
239 s = bio_get_string16(msg, &len);
240 ptr = bio_get_ref(msg);
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800241 allow_isolated = bio_get_uint32(msg) ? 1 : 0;
242 if (do_add_service(bs, s, len, ptr, txn->sender_euid, allow_isolated))
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 return -1;
244 break;
245
246 case SVC_MGR_LIST_SERVICES: {
247 unsigned n = bio_get_uint32(msg);
248
249 si = svclist;
250 while ((n-- > 0) && si)
251 si = si->next;
252 if (si) {
253 bio_put_string16(reply, si->name);
254 return 0;
255 }
256 return -1;
257 }
258 default:
Steve Block3762c312012-01-06 19:20:56 +0000259 ALOGE("unknown code %d\n", txn->code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 return -1;
261 }
262
263 bio_put_uint32(reply, 0);
264 return 0;
265}
266
267int main(int argc, char **argv)
268{
269 struct binder_state *bs;
270 void *svcmgr = BINDER_SERVICE_MANAGER;
271
272 bs = binder_open(128*1024);
273
274 if (binder_become_context_manager(bs)) {
Steve Block3762c312012-01-06 19:20:56 +0000275 ALOGE("cannot become context manager (%s)\n", strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 return -1;
277 }
278
279 svcmgr_handle = svcmgr;
280 binder_loop(bs, svcmgr_handler);
281 return 0;
282}