blob: 14536bd816bc91c898116bedd1ebc11b051a01ef [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_DRMIO, "drm.drmIOService" },
38 { AID_DRM, "drm.drmManager" },
Nick Pellycd0e8392010-10-13 17:25:24 -070039 { AID_NFC, "nfc" },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 { AID_RADIO, "radio.phone" },
41 { AID_RADIO, "radio.sms" },
42 { AID_RADIO, "radio.phonesubinfo" },
43 { AID_RADIO, "radio.simphonebook" },
44/* TODO: remove after phone services are updated: */
45 { AID_RADIO, "phone" },
Hung-ying Tyan7e54ef72010-09-25 22:49:59 +080046 { AID_RADIO, "sip" },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 { AID_RADIO, "isms" },
48 { AID_RADIO, "iphonesubinfo" },
49 { AID_RADIO, "simphonebook" },
50};
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;
97 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;
119 LOGI("service '%s' died\n", str8(si->name));
120 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
132void *do_find_service(struct binder_state *bs, uint16_t *s, unsigned len)
133{
134 struct svcinfo *si;
135 si = find_svc(s, len);
136
137// LOGI("check_service('%s') ptr = %p\n", str8(s), si ? si->ptr : 0);
138 if (si && si->ptr) {
139 return si->ptr;
140 } else {
141 return 0;
142 }
143}
144
145int do_add_service(struct binder_state *bs,
146 uint16_t *s, unsigned len,
147 void *ptr, unsigned uid)
148{
149 struct svcinfo *si;
150// LOGI("add_service('%s',%p) uid=%d\n", str8(s), ptr, uid);
151
152 if (!ptr || (len == 0) || (len > 127))
153 return -1;
154
155 if (!svc_can_register(uid, s)) {
156 LOGE("add_service('%s',%p) uid=%d - PERMISSION DENIED\n",
157 str8(s), ptr, uid);
158 return -1;
159 }
160
161 si = find_svc(s, len);
162 if (si) {
163 if (si->ptr) {
164 LOGE("add_service('%s',%p) uid=%d - ALREADY REGISTERED\n",
165 str8(s), ptr, uid);
166 return -1;
167 }
168 si->ptr = ptr;
169 } else {
170 si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
171 if (!si) {
172 LOGE("add_service('%s',%p) uid=%d - OUT OF MEMORY\n",
173 str8(s), ptr, uid);
174 return -1;
175 }
176 si->ptr = ptr;
177 si->len = len;
178 memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
179 si->name[len] = '\0';
180 si->death.func = svcinfo_death;
181 si->death.ptr = si;
182 si->next = svclist;
183 svclist = si;
184 }
185
186 binder_acquire(bs, ptr);
187 binder_link_to_death(bs, ptr, &si->death);
188 return 0;
189}
190
191int svcmgr_handler(struct binder_state *bs,
192 struct binder_txn *txn,
193 struct binder_io *msg,
194 struct binder_io *reply)
195{
196 struct svcinfo *si;
197 uint16_t *s;
198 unsigned len;
199 void *ptr;
Brad Fitzpatrick27b3a7a2010-06-18 13:07:53 -0700200 uint32_t strict_policy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201
202// LOGI("target=%p code=%d pid=%d uid=%d\n",
203// txn->target, txn->code, txn->sender_pid, txn->sender_euid);
204
205 if (txn->target != svcmgr_handle)
206 return -1;
207
Brad Fitzpatrick27b3a7a2010-06-18 13:07:53 -0700208 // Equivalent to Parcel::enforceInterface(), reading the RPC
209 // header with the strict mode policy mask and the interface name.
210 // Note that we ignore the strict_policy and don't propagate it
211 // further (since we do no outbound RPCs anyway).
212 strict_policy = bio_get_uint32(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 s = bio_get_string16(msg, &len);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 if ((len != (sizeof(svcmgr_id) / 2)) ||
215 memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
216 fprintf(stderr,"invalid id %s\n", str8(s));
217 return -1;
218 }
219
220 switch(txn->code) {
221 case SVC_MGR_GET_SERVICE:
222 case SVC_MGR_CHECK_SERVICE:
223 s = bio_get_string16(msg, &len);
224 ptr = do_find_service(bs, s, len);
225 if (!ptr)
226 break;
227 bio_put_ref(reply, ptr);
228 return 0;
229
230 case SVC_MGR_ADD_SERVICE:
231 s = bio_get_string16(msg, &len);
232 ptr = bio_get_ref(msg);
233 if (do_add_service(bs, s, len, ptr, txn->sender_euid))
234 return -1;
235 break;
236
237 case SVC_MGR_LIST_SERVICES: {
238 unsigned n = bio_get_uint32(msg);
239
240 si = svclist;
241 while ((n-- > 0) && si)
242 si = si->next;
243 if (si) {
244 bio_put_string16(reply, si->name);
245 return 0;
246 }
247 return -1;
248 }
249 default:
250 LOGE("unknown code %d\n", txn->code);
251 return -1;
252 }
253
254 bio_put_uint32(reply, 0);
255 return 0;
256}
257
258int main(int argc, char **argv)
259{
260 struct binder_state *bs;
261 void *svcmgr = BINDER_SERVICE_MANAGER;
262
263 bs = binder_open(128*1024);
264
265 if (binder_become_context_manager(bs)) {
266 LOGE("cannot become context manager (%s)\n", strerror(errno));
267 return -1;
268 }
269
270 svcmgr_handle = svcmgr;
271 binder_loop(bs, svcmgr_handler);
272 return 0;
273}