blob: a2006c177bfdbf69a349036d89770d32784f1291 [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" },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037 { AID_RADIO, "radio.phone" },
38 { AID_RADIO, "radio.sms" },
39 { AID_RADIO, "radio.phonesubinfo" },
40 { AID_RADIO, "radio.simphonebook" },
41/* TODO: remove after phone services are updated: */
42 { AID_RADIO, "phone" },
43 { 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;
93 unsigned len;
94 uint16_t name[0];
95};
96
97struct svcinfo *svclist = 0;
98
99struct svcinfo *find_svc(uint16_t *s16, unsigned len)
100{
101 struct svcinfo *si;
102
103 for (si = svclist; si; si = si->next) {
104 if ((len == si->len) &&
105 !memcmp(s16, si->name, len * sizeof(uint16_t))) {
106 return si;
107 }
108 }
109 return 0;
110}
111
112void svcinfo_death(struct binder_state *bs, void *ptr)
113{
114 struct svcinfo *si = ptr;
115 LOGI("service '%s' died\n", str8(si->name));
116 if (si->ptr) {
117 binder_release(bs, si->ptr);
118 si->ptr = 0;
119 }
120}
121
122uint16_t svcmgr_id[] = {
123 'a','n','d','r','o','i','d','.','o','s','.',
124 'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r'
125};
126
127
128void *do_find_service(struct binder_state *bs, uint16_t *s, unsigned len)
129{
130 struct svcinfo *si;
131 si = find_svc(s, len);
132
133// LOGI("check_service('%s') ptr = %p\n", str8(s), si ? si->ptr : 0);
134 if (si && si->ptr) {
135 return si->ptr;
136 } else {
137 return 0;
138 }
139}
140
141int do_add_service(struct binder_state *bs,
142 uint16_t *s, unsigned len,
143 void *ptr, unsigned uid)
144{
145 struct svcinfo *si;
146// LOGI("add_service('%s',%p) uid=%d\n", str8(s), ptr, uid);
147
148 if (!ptr || (len == 0) || (len > 127))
149 return -1;
150
151 if (!svc_can_register(uid, s)) {
152 LOGE("add_service('%s',%p) uid=%d - PERMISSION DENIED\n",
153 str8(s), ptr, uid);
154 return -1;
155 }
156
157 si = find_svc(s, len);
158 if (si) {
159 if (si->ptr) {
160 LOGE("add_service('%s',%p) uid=%d - ALREADY REGISTERED\n",
161 str8(s), ptr, uid);
162 return -1;
163 }
164 si->ptr = ptr;
165 } else {
166 si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
167 if (!si) {
168 LOGE("add_service('%s',%p) uid=%d - OUT OF MEMORY\n",
169 str8(s), ptr, uid);
170 return -1;
171 }
172 si->ptr = ptr;
173 si->len = len;
174 memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
175 si->name[len] = '\0';
176 si->death.func = svcinfo_death;
177 si->death.ptr = si;
178 si->next = svclist;
179 svclist = si;
180 }
181
182 binder_acquire(bs, ptr);
183 binder_link_to_death(bs, ptr, &si->death);
184 return 0;
185}
186
187int svcmgr_handler(struct binder_state *bs,
188 struct binder_txn *txn,
189 struct binder_io *msg,
190 struct binder_io *reply)
191{
192 struct svcinfo *si;
193 uint16_t *s;
194 unsigned len;
195 void *ptr;
196
197// LOGI("target=%p code=%d pid=%d uid=%d\n",
198// txn->target, txn->code, txn->sender_pid, txn->sender_euid);
199
200 if (txn->target != svcmgr_handle)
201 return -1;
202
203 s = bio_get_string16(msg, &len);
204
205 if ((len != (sizeof(svcmgr_id) / 2)) ||
206 memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
207 fprintf(stderr,"invalid id %s\n", str8(s));
208 return -1;
209 }
210
211 switch(txn->code) {
212 case SVC_MGR_GET_SERVICE:
213 case SVC_MGR_CHECK_SERVICE:
214 s = bio_get_string16(msg, &len);
215 ptr = do_find_service(bs, s, len);
216 if (!ptr)
217 break;
218 bio_put_ref(reply, ptr);
219 return 0;
220
221 case SVC_MGR_ADD_SERVICE:
222 s = bio_get_string16(msg, &len);
223 ptr = bio_get_ref(msg);
224 if (do_add_service(bs, s, len, ptr, txn->sender_euid))
225 return -1;
226 break;
227
228 case SVC_MGR_LIST_SERVICES: {
229 unsigned n = bio_get_uint32(msg);
230
231 si = svclist;
232 while ((n-- > 0) && si)
233 si = si->next;
234 if (si) {
235 bio_put_string16(reply, si->name);
236 return 0;
237 }
238 return -1;
239 }
240 default:
241 LOGE("unknown code %d\n", txn->code);
242 return -1;
243 }
244
245 bio_put_uint32(reply, 0);
246 return 0;
247}
248
249int main(int argc, char **argv)
250{
251 struct binder_state *bs;
252 void *svcmgr = BINDER_SERVICE_MANAGER;
253
254 bs = binder_open(128*1024);
255
256 if (binder_become_context_manager(bs)) {
257 LOGE("cannot become context manager (%s)\n", strerror(errno));
258 return -1;
259 }
260
261 svcmgr_handle = svcmgr;
262 binder_loop(bs, svcmgr_handler);
263 return 0;
264}