blob: e4aa8b51cf2a7a1587acc1d3e6a637b9e2ef19fb [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[] = {
30 { AID_MEDIA, "media.audio_flinger" },
31 { AID_MEDIA, "media.player" },
32 { AID_MEDIA, "media.camera" },
33 { AID_RADIO, "radio.phone" },
34 { AID_RADIO, "radio.sms" },
35 { AID_RADIO, "radio.phonesubinfo" },
36 { AID_RADIO, "radio.simphonebook" },
37/* TODO: remove after phone services are updated: */
38 { AID_RADIO, "phone" },
39 { AID_RADIO, "isms" },
40 { AID_RADIO, "iphonesubinfo" },
41 { AID_RADIO, "simphonebook" },
42};
43
44void *svcmgr_handle;
45
46const char *str8(uint16_t *x)
47{
48 static char buf[128];
49 unsigned max = 127;
50 char *p = buf;
51
52 if (x) {
53 while (*x && max--) {
54 *p++ = *x++;
55 }
56 }
57 *p++ = 0;
58 return buf;
59}
60
61int str16eq(uint16_t *a, const char *b)
62{
63 while (*a && *b)
64 if (*a++ != *b++) return 0;
65 if (*a || *b)
66 return 0;
67 return 1;
68}
69
70int svc_can_register(unsigned uid, uint16_t *name)
71{
72 unsigned n;
73
74 if ((uid == 0) || (uid == AID_SYSTEM))
75 return 1;
76
77 for (n = 0; n < sizeof(allowed) / sizeof(allowed[0]); n++)
78 if ((uid == allowed[n].uid) && str16eq(name, allowed[n].name))
79 return 1;
80
81 return 0;
82}
83
84struct svcinfo
85{
86 struct svcinfo *next;
87 void *ptr;
88 struct binder_death death;
89 unsigned len;
90 uint16_t name[0];
91};
92
93struct svcinfo *svclist = 0;
94
95struct svcinfo *find_svc(uint16_t *s16, unsigned len)
96{
97 struct svcinfo *si;
98
99 for (si = svclist; si; si = si->next) {
100 if ((len == si->len) &&
101 !memcmp(s16, si->name, len * sizeof(uint16_t))) {
102 return si;
103 }
104 }
105 return 0;
106}
107
108void svcinfo_death(struct binder_state *bs, void *ptr)
109{
110 struct svcinfo *si = ptr;
111 LOGI("service '%s' died\n", str8(si->name));
112 if (si->ptr) {
113 binder_release(bs, si->ptr);
114 si->ptr = 0;
115 }
116}
117
118uint16_t svcmgr_id[] = {
119 'a','n','d','r','o','i','d','.','o','s','.',
120 'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r'
121};
122
123
124void *do_find_service(struct binder_state *bs, uint16_t *s, unsigned len)
125{
126 struct svcinfo *si;
127 si = find_svc(s, len);
128
129// LOGI("check_service('%s') ptr = %p\n", str8(s), si ? si->ptr : 0);
130 if (si && si->ptr) {
131 return si->ptr;
132 } else {
133 return 0;
134 }
135}
136
137int do_add_service(struct binder_state *bs,
138 uint16_t *s, unsigned len,
139 void *ptr, unsigned uid)
140{
141 struct svcinfo *si;
142// LOGI("add_service('%s',%p) uid=%d\n", str8(s), ptr, uid);
143
144 if (!ptr || (len == 0) || (len > 127))
145 return -1;
146
147 if (!svc_can_register(uid, s)) {
148 LOGE("add_service('%s',%p) uid=%d - PERMISSION DENIED\n",
149 str8(s), ptr, uid);
150 return -1;
151 }
152
153 si = find_svc(s, len);
154 if (si) {
155 if (si->ptr) {
156 LOGE("add_service('%s',%p) uid=%d - ALREADY REGISTERED\n",
157 str8(s), ptr, uid);
158 return -1;
159 }
160 si->ptr = ptr;
161 } else {
162 si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
163 if (!si) {
164 LOGE("add_service('%s',%p) uid=%d - OUT OF MEMORY\n",
165 str8(s), ptr, uid);
166 return -1;
167 }
168 si->ptr = ptr;
169 si->len = len;
170 memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
171 si->name[len] = '\0';
172 si->death.func = svcinfo_death;
173 si->death.ptr = si;
174 si->next = svclist;
175 svclist = si;
176 }
177
178 binder_acquire(bs, ptr);
179 binder_link_to_death(bs, ptr, &si->death);
180 return 0;
181}
182
183int svcmgr_handler(struct binder_state *bs,
184 struct binder_txn *txn,
185 struct binder_io *msg,
186 struct binder_io *reply)
187{
188 struct svcinfo *si;
189 uint16_t *s;
190 unsigned len;
191 void *ptr;
192
193// LOGI("target=%p code=%d pid=%d uid=%d\n",
194// txn->target, txn->code, txn->sender_pid, txn->sender_euid);
195
196 if (txn->target != svcmgr_handle)
197 return -1;
198
199 s = bio_get_string16(msg, &len);
200
201 if ((len != (sizeof(svcmgr_id) / 2)) ||
202 memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
203 fprintf(stderr,"invalid id %s\n", str8(s));
204 return -1;
205 }
206
207 switch(txn->code) {
208 case SVC_MGR_GET_SERVICE:
209 case SVC_MGR_CHECK_SERVICE:
210 s = bio_get_string16(msg, &len);
211 ptr = do_find_service(bs, s, len);
212 if (!ptr)
213 break;
214 bio_put_ref(reply, ptr);
215 return 0;
216
217 case SVC_MGR_ADD_SERVICE:
218 s = bio_get_string16(msg, &len);
219 ptr = bio_get_ref(msg);
220 if (do_add_service(bs, s, len, ptr, txn->sender_euid))
221 return -1;
222 break;
223
224 case SVC_MGR_LIST_SERVICES: {
225 unsigned n = bio_get_uint32(msg);
226
227 si = svclist;
228 while ((n-- > 0) && si)
229 si = si->next;
230 if (si) {
231 bio_put_string16(reply, si->name);
232 return 0;
233 }
234 return -1;
235 }
236 default:
237 LOGE("unknown code %d\n", txn->code);
238 return -1;
239 }
240
241 bio_put_uint32(reply, 0);
242 return 0;
243}
244
245int main(int argc, char **argv)
246{
247 struct binder_state *bs;
248 void *svcmgr = BINDER_SERVICE_MANAGER;
249
250 bs = binder_open(128*1024);
251
252 if (binder_become_context_manager(bs)) {
253 LOGE("cannot become context manager (%s)\n", strerror(errno));
254 return -1;
255 }
256
257 svcmgr_handle = svcmgr;
258 binder_loop(bs, svcmgr_handler);
259 return 0;
260}