blob: 2df450f34defca80cc5d5ba4d141841ea10a8bce [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
14#define LOGI(x...) fprintf(stderr, "svcmgr: " x)
15#define LOGE(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[] = {
Glenn Kasten871c16c2010-03-05 12:18:01 -080030#ifdef LVMX
31 { AID_MEDIA, "com.lifevibes.mx.ipc" },
32#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033 { AID_MEDIA, "media.audio_flinger" },
34 { AID_MEDIA, "media.player" },
35 { AID_MEDIA, "media.camera" },
Eric Laurenta553c252009-07-17 12:17:14 -070036 { AID_MEDIA, "media.audio_policy" },
aimitakeshid074e302010-07-29 10:12:27 +090037 { AID_DRM, "drm.drmManager" },
Nick Pellycd0e8392010-10-13 17:25:24 -070038 { AID_NFC, "nfc" },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039 { 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" },
Hung-ying Tyan7e54ef72010-09-25 22:49:59 +080045 { AID_RADIO, "sip" },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 { AID_RADIO, "isms" },
47 { AID_RADIO, "iphonesubinfo" },
48 { AID_RADIO, "simphonebook" },
49};
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;
96 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;
118 LOGI("service '%s' died\n", str8(si->name));
119 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
131void *do_find_service(struct binder_state *bs, uint16_t *s, unsigned len)
132{
133 struct svcinfo *si;
134 si = find_svc(s, len);
135
136// LOGI("check_service('%s') ptr = %p\n", str8(s), si ? si->ptr : 0);
137 if (si && si->ptr) {
138 return si->ptr;
139 } else {
140 return 0;
141 }
142}
143
144int do_add_service(struct binder_state *bs,
145 uint16_t *s, unsigned len,
146 void *ptr, unsigned uid)
147{
148 struct svcinfo *si;
149// LOGI("add_service('%s',%p) uid=%d\n", str8(s), ptr, uid);
150
151 if (!ptr || (len == 0) || (len > 127))
152 return -1;
153
154 if (!svc_can_register(uid, s)) {
155 LOGE("add_service('%s',%p) uid=%d - PERMISSION DENIED\n",
156 str8(s), ptr, uid);
157 return -1;
158 }
159
160 si = find_svc(s, len);
161 if (si) {
162 if (si->ptr) {
Iliyan Malchev1d884382010-12-08 11:21:24 -0800163 LOGE("add_service('%s',%p) uid=%d - ALREADY REGISTERED, OVERRIDE\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 str8(s), ptr, uid);
Iliyan Malchev1d884382010-12-08 11:21:24 -0800165 svcinfo_death(bs, si);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 }
167 si->ptr = ptr;
168 } else {
169 si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
170 if (!si) {
171 LOGE("add_service('%s',%p) uid=%d - OUT OF MEMORY\n",
172 str8(s), ptr, uid);
173 return -1;
174 }
175 si->ptr = ptr;
176 si->len = len;
177 memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
178 si->name[len] = '\0';
179 si->death.func = svcinfo_death;
180 si->death.ptr = si;
181 si->next = svclist;
182 svclist = si;
183 }
184
185 binder_acquire(bs, ptr);
186 binder_link_to_death(bs, ptr, &si->death);
187 return 0;
188}
189
190int svcmgr_handler(struct binder_state *bs,
191 struct binder_txn *txn,
192 struct binder_io *msg,
193 struct binder_io *reply)
194{
195 struct svcinfo *si;
196 uint16_t *s;
197 unsigned len;
198 void *ptr;
Brad Fitzpatrick27b3a7a2010-06-18 13:07:53 -0700199 uint32_t strict_policy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200
201// LOGI("target=%p code=%d pid=%d uid=%d\n",
202// txn->target, txn->code, txn->sender_pid, txn->sender_euid);
203
204 if (txn->target != svcmgr_handle)
205 return -1;
206
Brad Fitzpatrick27b3a7a2010-06-18 13:07:53 -0700207 // Equivalent to Parcel::enforceInterface(), reading the RPC
208 // header with the strict mode policy mask and the interface name.
209 // Note that we ignore the strict_policy and don't propagate it
210 // further (since we do no outbound RPCs anyway).
211 strict_policy = bio_get_uint32(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 s = bio_get_string16(msg, &len);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 if ((len != (sizeof(svcmgr_id) / 2)) ||
214 memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
215 fprintf(stderr,"invalid id %s\n", str8(s));
216 return -1;
217 }
218
219 switch(txn->code) {
220 case SVC_MGR_GET_SERVICE:
221 case SVC_MGR_CHECK_SERVICE:
222 s = bio_get_string16(msg, &len);
223 ptr = do_find_service(bs, s, len);
224 if (!ptr)
225 break;
226 bio_put_ref(reply, ptr);
227 return 0;
228
229 case SVC_MGR_ADD_SERVICE:
230 s = bio_get_string16(msg, &len);
231 ptr = bio_get_ref(msg);
232 if (do_add_service(bs, s, len, ptr, txn->sender_euid))
233 return -1;
234 break;
235
236 case SVC_MGR_LIST_SERVICES: {
237 unsigned n = bio_get_uint32(msg);
238
239 si = svclist;
240 while ((n-- > 0) && si)
241 si = si->next;
242 if (si) {
243 bio_put_string16(reply, si->name);
244 return 0;
245 }
246 return -1;
247 }
248 default:
249 LOGE("unknown code %d\n", txn->code);
250 return -1;
251 }
252
253 bio_put_uint32(reply, 0);
254 return 0;
255}
256
257int main(int argc, char **argv)
258{
259 struct binder_state *bs;
260 void *svcmgr = BINDER_SERVICE_MANAGER;
261
262 bs = binder_open(128*1024);
263
264 if (binder_become_context_manager(bs)) {
265 LOGE("cannot become context manager (%s)\n", strerror(errno));
266 return -1;
267 }
268
269 svcmgr_handle = svcmgr;
270 binder_loop(bs, svcmgr_handler);
271 return 0;
272}