service_manager.c revision de57586c90f8aeea489e35e634672803e1dd9757
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    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_MEDIA, "media.audio_policy" },
34    { AID_DRM,   "drm.drmManager" },
35    { AID_NFC,   "nfc" },
36    { AID_RADIO, "radio.phone" },
37    { AID_RADIO, "radio.sms" },
38    { AID_RADIO, "radio.phonesubinfo" },
39    { AID_RADIO, "radio.simphonebook" },
40/* TODO: remove after phone services are updated: */
41    { AID_RADIO, "phone" },
42    { AID_RADIO, "sip" },
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    ALOGI("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//    ALOGI("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//    ALOGI("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        ALOGE("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            ALOGE("add_service('%s',%p) uid=%d - ALREADY REGISTERED, OVERRIDE\n",
161                 str8(s), ptr, uid);
162            svcinfo_death(bs, si);
163        }
164        si->ptr = ptr;
165    } else {
166        si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
167        if (!si) {
168            ALOGE("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    uint32_t strict_policy;
197
198//    ALOGI("target=%p code=%d pid=%d uid=%d\n",
199//         txn->target, txn->code, txn->sender_pid, txn->sender_euid);
200
201    if (txn->target != svcmgr_handle)
202        return -1;
203
204    // Equivalent to Parcel::enforceInterface(), reading the RPC
205    // header with the strict mode policy mask and the interface name.
206    // Note that we ignore the strict_policy and don't propagate it
207    // further (since we do no outbound RPCs anyway).
208    strict_policy = bio_get_uint32(msg);
209    s = bio_get_string16(msg, &len);
210    if ((len != (sizeof(svcmgr_id) / 2)) ||
211        memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
212        fprintf(stderr,"invalid id %s\n", str8(s));
213        return -1;
214    }
215
216    switch(txn->code) {
217    case SVC_MGR_GET_SERVICE:
218    case SVC_MGR_CHECK_SERVICE:
219        s = bio_get_string16(msg, &len);
220        ptr = do_find_service(bs, s, len);
221        if (!ptr)
222            break;
223        bio_put_ref(reply, ptr);
224        return 0;
225
226    case SVC_MGR_ADD_SERVICE:
227        s = bio_get_string16(msg, &len);
228        ptr = bio_get_ref(msg);
229        if (do_add_service(bs, s, len, ptr, txn->sender_euid))
230            return -1;
231        break;
232
233    case SVC_MGR_LIST_SERVICES: {
234        unsigned n = bio_get_uint32(msg);
235
236        si = svclist;
237        while ((n-- > 0) && si)
238            si = si->next;
239        if (si) {
240            bio_put_string16(reply, si->name);
241            return 0;
242        }
243        return -1;
244    }
245    default:
246        ALOGE("unknown code %d\n", txn->code);
247        return -1;
248    }
249
250    bio_put_uint32(reply, 0);
251    return 0;
252}
253
254int main(int argc, char **argv)
255{
256    struct binder_state *bs;
257    void *svcmgr = BINDER_SERVICE_MANAGER;
258
259    bs = binder_open(128*1024);
260
261    if (binder_become_context_manager(bs)) {
262        ALOGE("cannot become context manager (%s)\n", strerror(errno));
263        return -1;
264    }
265
266    svcmgr_handle = svcmgr;
267    binder_loop(bs, svcmgr_handler);
268    return 0;
269}
270