blob: 71c5622ec382c0ef969580c2109bc871325d79da [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" },
Mike J. Chen6c929512011-08-15 11:59:47 -070046 { AID_MEDIA, "common_time.clock" },
47 { AID_MEDIA, "common_time.config" },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048};
49
50void *svcmgr_handle;
51
52const char *str8(uint16_t *x)
53{
54 static char buf[128];
55 unsigned max = 127;
56 char *p = buf;
57
58 if (x) {
59 while (*x && max--) {
60 *p++ = *x++;
61 }
62 }
63 *p++ = 0;
64 return buf;
65}
66
67int str16eq(uint16_t *a, const char *b)
68{
69 while (*a && *b)
70 if (*a++ != *b++) return 0;
71 if (*a || *b)
72 return 0;
73 return 1;
74}
75
76int svc_can_register(unsigned uid, uint16_t *name)
77{
78 unsigned n;
79
80 if ((uid == 0) || (uid == AID_SYSTEM))
81 return 1;
82
83 for (n = 0; n < sizeof(allowed) / sizeof(allowed[0]); n++)
84 if ((uid == allowed[n].uid) && str16eq(name, allowed[n].name))
85 return 1;
86
87 return 0;
88}
89
90struct svcinfo
91{
92 struct svcinfo *next;
93 void *ptr;
94 struct binder_death death;
Dianne Hackborna573f6a2012-02-09 16:12:18 -080095 int allow_isolated;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 unsigned len;
97 uint16_t name[0];
98};
99
100struct svcinfo *svclist = 0;
101
102struct svcinfo *find_svc(uint16_t *s16, unsigned len)
103{
104 struct svcinfo *si;
105
106 for (si = svclist; si; si = si->next) {
107 if ((len == si->len) &&
108 !memcmp(s16, si->name, len * sizeof(uint16_t))) {
109 return si;
110 }
111 }
112 return 0;
113}
114
115void svcinfo_death(struct binder_state *bs, void *ptr)
116{
117 struct svcinfo *si = ptr;
Steve Block6215d3f2012-01-04 20:05:49 +0000118 ALOGI("service '%s' died\n", str8(si->name));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 if (si->ptr) {
120 binder_release(bs, si->ptr);
121 si->ptr = 0;
122 }
123}
124
125uint16_t svcmgr_id[] = {
126 'a','n','d','r','o','i','d','.','o','s','.',
127 'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r'
128};
129
130
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800131void *do_find_service(struct binder_state *bs, uint16_t *s, unsigned len, unsigned uid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132{
133 struct svcinfo *si;
134 si = find_svc(s, len);
135
Steve Block6215d3f2012-01-04 20:05:49 +0000136// ALOGI("check_service('%s') ptr = %p\n", str8(s), si ? si->ptr : 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 if (si && si->ptr) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800138 if (!si->allow_isolated) {
139 // If this service doesn't allow access from isolated processes,
140 // then check the uid to see if it is isolated.
141 unsigned appid = uid % AID_USER;
142 if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) {
143 return 0;
144 }
145 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 return si->ptr;
147 } else {
148 return 0;
149 }
150}
151
152int do_add_service(struct binder_state *bs,
153 uint16_t *s, unsigned len,
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800154 void *ptr, unsigned uid, int allow_isolated)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155{
156 struct svcinfo *si;
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800157 //ALOGI("add_service('%s',%p,%s) uid=%d\n", str8(s), ptr,
158 // allow_isolated ? "allow_isolated" : "!allow_isolated", uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159
160 if (!ptr || (len == 0) || (len > 127))
161 return -1;
162
163 if (!svc_can_register(uid, s)) {
Steve Block3762c312012-01-06 19:20:56 +0000164 ALOGE("add_service('%s',%p) uid=%d - PERMISSION DENIED\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 str8(s), ptr, uid);
166 return -1;
167 }
168
169 si = find_svc(s, len);
170 if (si) {
171 if (si->ptr) {
Steve Block3762c312012-01-06 19:20:56 +0000172 ALOGE("add_service('%s',%p) uid=%d - ALREADY REGISTERED, OVERRIDE\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 str8(s), ptr, uid);
Iliyan Malchev1d884382010-12-08 11:21:24 -0800174 svcinfo_death(bs, si);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 }
176 si->ptr = ptr;
177 } else {
178 si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
179 if (!si) {
Steve Block3762c312012-01-06 19:20:56 +0000180 ALOGE("add_service('%s',%p) uid=%d - OUT OF MEMORY\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 str8(s), ptr, uid);
182 return -1;
183 }
184 si->ptr = ptr;
185 si->len = len;
186 memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
187 si->name[len] = '\0';
188 si->death.func = svcinfo_death;
189 si->death.ptr = si;
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800190 si->allow_isolated = allow_isolated;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 si->next = svclist;
192 svclist = si;
193 }
194
195 binder_acquire(bs, ptr);
196 binder_link_to_death(bs, ptr, &si->death);
197 return 0;
198}
199
200int svcmgr_handler(struct binder_state *bs,
201 struct binder_txn *txn,
202 struct binder_io *msg,
203 struct binder_io *reply)
204{
205 struct svcinfo *si;
206 uint16_t *s;
207 unsigned len;
208 void *ptr;
Brad Fitzpatrick27b3a7a2010-06-18 13:07:53 -0700209 uint32_t strict_policy;
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800210 int allow_isolated;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211
Steve Block6215d3f2012-01-04 20:05:49 +0000212// ALOGI("target=%p code=%d pid=%d uid=%d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213// txn->target, txn->code, txn->sender_pid, txn->sender_euid);
214
215 if (txn->target != svcmgr_handle)
216 return -1;
217
Brad Fitzpatrick27b3a7a2010-06-18 13:07:53 -0700218 // Equivalent to Parcel::enforceInterface(), reading the RPC
219 // header with the strict mode policy mask and the interface name.
220 // Note that we ignore the strict_policy and don't propagate it
221 // further (since we do no outbound RPCs anyway).
222 strict_policy = bio_get_uint32(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 s = bio_get_string16(msg, &len);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 if ((len != (sizeof(svcmgr_id) / 2)) ||
225 memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
226 fprintf(stderr,"invalid id %s\n", str8(s));
227 return -1;
228 }
229
230 switch(txn->code) {
231 case SVC_MGR_GET_SERVICE:
232 case SVC_MGR_CHECK_SERVICE:
233 s = bio_get_string16(msg, &len);
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800234 ptr = do_find_service(bs, s, len, txn->sender_euid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 if (!ptr)
236 break;
237 bio_put_ref(reply, ptr);
238 return 0;
239
240 case SVC_MGR_ADD_SERVICE:
241 s = bio_get_string16(msg, &len);
242 ptr = bio_get_ref(msg);
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800243 allow_isolated = bio_get_uint32(msg) ? 1 : 0;
244 if (do_add_service(bs, s, len, ptr, txn->sender_euid, allow_isolated))
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 return -1;
246 break;
247
248 case SVC_MGR_LIST_SERVICES: {
249 unsigned n = bio_get_uint32(msg);
250
251 si = svclist;
252 while ((n-- > 0) && si)
253 si = si->next;
254 if (si) {
255 bio_put_string16(reply, si->name);
256 return 0;
257 }
258 return -1;
259 }
260 default:
Steve Block3762c312012-01-06 19:20:56 +0000261 ALOGE("unknown code %d\n", txn->code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 return -1;
263 }
264
265 bio_put_uint32(reply, 0);
266 return 0;
267}
268
269int main(int argc, char **argv)
270{
271 struct binder_state *bs;
272 void *svcmgr = BINDER_SERVICE_MANAGER;
273
274 bs = binder_open(128*1024);
275
276 if (binder_become_context_manager(bs)) {
Steve Block3762c312012-01-06 19:20:56 +0000277 ALOGE("cannot become context manager (%s)\n", strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 return -1;
279 }
280
281 svcmgr_handle = svcmgr;
282 binder_loop(bs, svcmgr_handler);
283 return 0;
284}