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