service_manager.c revision 9b738bb4110926b85da65d36b2e6f1a50199ec4c
1/* 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 ALOGI(x...) fprintf(stderr, "svcmgr: " x)
15#define ALOGE(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    uid_t uid;
28    const char *name;
29} allowed[] = {
30    { AID_MEDIA, "media.audio_flinger" },
31    { AID_MEDIA, "media.log" },
32    { AID_MEDIA, "media.player" },
33    { AID_MEDIA, "media.camera" },
34    { AID_MEDIA, "media.audio_policy" },
35    { AID_DRM,   "drm.drmManager" },
36    { AID_NFC,   "nfc" },
37    { AID_BLUETOOTH, "bluetooth" },
38    { AID_RADIO, "radio.phone" },
39    { AID_RADIO, "radio.sms" },
40    { AID_RADIO, "radio.phonesubinfo" },
41    { AID_RADIO, "radio.simphonebook" },
42/* TODO: remove after phone services are updated: */
43    { AID_RADIO, "phone" },
44    { AID_RADIO, "sip" },
45    { AID_RADIO, "isms" },
46    { AID_RADIO, "iphonesubinfo" },
47    { AID_RADIO, "simphonebook" },
48    { AID_MEDIA, "common_time.clock" },
49    { AID_MEDIA, "common_time.config" },
50    { AID_KEYSTORE, "android.security.keystore" },
51};
52
53void *svcmgr_handle;
54
55const char *str8(const uint16_t *x)
56{
57    static char buf[128];
58    unsigned max = 127;
59    char *p = buf;
60
61    if (x) {
62        while (*x && max--) {
63            *p++ = *x++;
64        }
65    }
66    *p++ = 0;
67    return buf;
68}
69
70int str16eq(const uint16_t *a, const char *b)
71{
72    while (*a && *b)
73        if (*a++ != *b++) return 0;
74    if (*a || *b)
75        return 0;
76    return 1;
77}
78
79int svc_can_register(uid_t uid, const uint16_t *name)
80{
81    size_t n;
82
83    if ((uid == 0) || (uid == AID_SYSTEM))
84        return 1;
85
86    for (n = 0; n < sizeof(allowed) / sizeof(allowed[0]); n++)
87        if ((uid == allowed[n].uid) && str16eq(name, allowed[n].name))
88            return 1;
89
90    return 0;
91}
92
93struct svcinfo
94{
95    struct svcinfo *next;
96    void *ptr;
97    struct binder_death death;
98    int allow_isolated;
99    size_t len;
100    uint16_t name[0];
101};
102
103struct svcinfo *svclist = NULL;
104
105struct svcinfo *find_svc(const uint16_t *s16, size_t len)
106{
107    struct svcinfo *si;
108
109    for (si = svclist; si; si = si->next) {
110        if ((len == si->len) &&
111            !memcmp(s16, si->name, len * sizeof(uint16_t))) {
112            return si;
113        }
114    }
115    return NULL;
116}
117
118void svcinfo_death(struct binder_state *bs, void *ptr)
119{
120    struct svcinfo *si = (struct svcinfo* ) ptr;
121
122    ALOGI("service '%s' died\n", str8(si->name));
123    if (si->ptr) {
124        binder_release(bs, si->ptr);
125        si->ptr = 0;
126    }
127}
128
129uint16_t svcmgr_id[] = {
130    'a','n','d','r','o','i','d','.','o','s','.',
131    'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r'
132};
133
134
135void *do_find_service(struct binder_state *bs, const uint16_t *s, size_t len, uid_t uid)
136{
137    struct svcinfo *si;
138    si = find_svc(s, len);
139
140//    ALOGI("check_service('%s') ptr = %p\n", str8(s), si ? si->ptr : 0);
141    if (si && si->ptr) {
142        if (!si->allow_isolated) {
143            // If this service doesn't allow access from isolated processes,
144            // then check the uid to see if it is isolated.
145            uid_t appid = uid % AID_USER;
146            if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) {
147                return 0;
148            }
149        }
150        return si->ptr;
151    } else {
152        return 0;
153    }
154}
155
156int do_add_service(struct binder_state *bs,
157                   const uint16_t *s, size_t len,
158                   void *ptr, uid_t uid, int allow_isolated)
159{
160    struct svcinfo *si;
161    //ALOGI("add_service('%s',%p,%s) uid=%d\n", str8(s), ptr,
162    //        allow_isolated ? "allow_isolated" : "!allow_isolated", uid);
163
164    if (!ptr || (len == 0) || (len > 127))
165        return -1;
166
167    if (!svc_can_register(uid, s)) {
168        ALOGE("add_service('%s',%p) uid=%d - PERMISSION DENIED\n",
169             str8(s), ptr, uid);
170        return -1;
171    }
172
173    si = find_svc(s, len);
174    if (si) {
175        if (si->ptr) {
176            ALOGE("add_service('%s',%p) uid=%d - ALREADY REGISTERED, OVERRIDE\n",
177                 str8(s), ptr, uid);
178            svcinfo_death(bs, si);
179        }
180        si->ptr = ptr;
181    } else {
182        si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
183        if (!si) {
184            ALOGE("add_service('%s',%p) uid=%d - OUT OF MEMORY\n",
185                 str8(s), ptr, uid);
186            return -1;
187        }
188        si->ptr = ptr;
189        si->len = len;
190        memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
191        si->name[len] = '\0';
192        si->death.func = (void*) svcinfo_death;
193        si->death.ptr = si;
194        si->allow_isolated = allow_isolated;
195        si->next = svclist;
196        svclist = si;
197    }
198
199    binder_acquire(bs, ptr);
200    binder_link_to_death(bs, ptr, &si->death);
201    return 0;
202}
203
204int svcmgr_handler(struct binder_state *bs,
205                   struct binder_transaction_data *txn,
206                   struct binder_io *msg,
207                   struct binder_io *reply)
208{
209    struct svcinfo *si;
210    uint16_t *s;
211    size_t len;
212    void *ptr;
213    uint32_t strict_policy;
214    int allow_isolated;
215
216//    ALOGI("target=%p code=%d pid=%d uid=%d\n",
217//         txn->target, txn->code, txn->sender_pid, txn->sender_euid);
218
219    if (txn->target.handle != svcmgr_handle)
220        return -1;
221
222    // Equivalent to Parcel::enforceInterface(), reading the RPC
223    // header with the strict mode policy mask and the interface name.
224    // Note that we ignore the strict_policy and don't propagate it
225    // further (since we do no outbound RPCs anyway).
226    strict_policy = bio_get_uint32(msg);
227    s = bio_get_string16(msg, &len);
228    if ((len != (sizeof(svcmgr_id) / 2)) ||
229        memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
230        fprintf(stderr,"invalid id %s\n", str8(s));
231        return -1;
232    }
233
234    switch(txn->code) {
235    case SVC_MGR_GET_SERVICE:
236    case SVC_MGR_CHECK_SERVICE:
237        s = bio_get_string16(msg, &len);
238        ptr = do_find_service(bs, s, len, txn->sender_euid);
239        if (!ptr)
240            break;
241        bio_put_ref(reply, ptr);
242        return 0;
243
244    case SVC_MGR_ADD_SERVICE:
245        s = bio_get_string16(msg, &len);
246        ptr = bio_get_ref(msg);
247        allow_isolated = bio_get_uint32(msg) ? 1 : 0;
248        if (do_add_service(bs, s, len, ptr, txn->sender_euid, allow_isolated))
249            return -1;
250        break;
251
252    case SVC_MGR_LIST_SERVICES: {
253        unsigned n = bio_get_uint32(msg);
254
255        si = svclist;
256        while ((n-- > 0) && si)
257            si = si->next;
258        if (si) {
259            bio_put_string16(reply, si->name);
260            return 0;
261        }
262        return -1;
263    }
264    default:
265        ALOGE("unknown code %d\n", txn->code);
266        return -1;
267    }
268
269    bio_put_uint32(reply, 0);
270    return 0;
271}
272
273int main(int argc, char **argv)
274{
275    struct binder_state *bs;
276
277    bs = binder_open(128*1024);
278    if (!bs) {
279        ALOGE("failed to open binder driver\n");
280        return -1;
281    }
282
283    if (binder_become_context_manager(bs)) {
284        ALOGE("cannot become context manager (%s)\n", strerror(errno));
285        return -1;
286    }
287
288    svcmgr_handle = BINDER_SERVICE_MANAGER;
289    binder_loop(bs, svcmgr_handler);
290
291    return 0;
292}
293