service_manager.c revision e5245cbf5d4e830cf605ef07f5d284d7c5d2867e
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
53uint32_t 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    uint32_t handle;
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->handle) {
124        binder_release(bs, si->handle);
125        si->handle = 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
135uint32_t do_find_service(struct binder_state *bs, const uint16_t *s, size_t len, uid_t uid)
136{
137    struct svcinfo *si;
138
139    si = find_svc(s, len);
140    //ALOGI("check_service('%s') handle = %x\n", str8(s), si ? si->handle : 0);
141    if (si && si->handle) {
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->handle;
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                   uint32_t handle, uid_t uid, int allow_isolated)
159{
160    struct svcinfo *si;
161
162    //ALOGI("add_service('%s',%x,%s) uid=%d\n", str8(s), handle,
163    //        allow_isolated ? "allow_isolated" : "!allow_isolated", uid);
164
165    if (!handle || (len == 0) || (len > 127))
166        return -1;
167
168    if (!svc_can_register(uid, s)) {
169        ALOGE("add_service('%s',%x) uid=%d - PERMISSION DENIED\n",
170             str8(s), handle, uid);
171        return -1;
172    }
173
174    si = find_svc(s, len);
175    if (si) {
176        if (si->handle) {
177            ALOGE("add_service('%s',%x) uid=%d - ALREADY REGISTERED, OVERRIDE\n",
178                 str8(s), handle, uid);
179            svcinfo_death(bs, si);
180        }
181        si->handle = handle;
182    } else {
183        si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
184        if (!si) {
185            ALOGE("add_service('%s',%x) uid=%d - OUT OF MEMORY\n",
186                 str8(s), handle, uid);
187            return -1;
188        }
189        si->handle = handle;
190        si->len = len;
191        memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
192        si->name[len] = '\0';
193        si->death.func = (void*) svcinfo_death;
194        si->death.ptr = si;
195        si->allow_isolated = allow_isolated;
196        si->next = svclist;
197        svclist = si;
198    }
199
200    binder_acquire(bs, handle);
201    binder_link_to_death(bs, handle, &si->death);
202    return 0;
203}
204
205int svcmgr_handler(struct binder_state *bs,
206                   struct binder_transaction_data *txn,
207                   struct binder_io *msg,
208                   struct binder_io *reply)
209{
210    struct svcinfo *si;
211    uint16_t *s;
212    size_t len;
213    uint32_t handle;
214    uint32_t strict_policy;
215    int allow_isolated;
216
217    //ALOGI("target=%x code=%d pid=%d uid=%d\n",
218    //  txn->target.handle, txn->code, txn->sender_pid, txn->sender_euid);
219
220    if (txn->target.handle != svcmgr_handle)
221        return -1;
222
223    if (txn->code == PING_TRANSACTION)
224        return 0;
225
226    // Equivalent to Parcel::enforceInterface(), reading the RPC
227    // header with the strict mode policy mask and the interface name.
228    // Note that we ignore the strict_policy and don't propagate it
229    // further (since we do no outbound RPCs anyway).
230    strict_policy = bio_get_uint32(msg);
231    s = bio_get_string16(msg, &len);
232    if ((len != (sizeof(svcmgr_id) / 2)) ||
233        memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
234        fprintf(stderr,"invalid id %s\n", str8(s));
235        return -1;
236    }
237
238    switch(txn->code) {
239    case SVC_MGR_GET_SERVICE:
240    case SVC_MGR_CHECK_SERVICE:
241        s = bio_get_string16(msg, &len);
242        handle = do_find_service(bs, s, len, txn->sender_euid);
243        if (!handle)
244            break;
245        bio_put_ref(reply, handle);
246        return 0;
247
248    case SVC_MGR_ADD_SERVICE:
249        s = bio_get_string16(msg, &len);
250        handle = bio_get_ref(msg);
251        allow_isolated = bio_get_uint32(msg) ? 1 : 0;
252        if (do_add_service(bs, s, len, handle, txn->sender_euid, allow_isolated))
253            return -1;
254        break;
255
256    case SVC_MGR_LIST_SERVICES: {
257        uint32_t n = bio_get_uint32(msg);
258
259        si = svclist;
260        while ((n-- > 0) && si)
261            si = si->next;
262        if (si) {
263            bio_put_string16(reply, si->name);
264            return 0;
265        }
266        return -1;
267    }
268    default:
269        ALOGE("unknown code %d\n", txn->code);
270        return -1;
271    }
272
273    bio_put_uint32(reply, 0);
274    return 0;
275}
276
277int main(int argc, char **argv)
278{
279    struct binder_state *bs;
280
281    bs = binder_open(128*1024);
282    if (!bs) {
283        ALOGE("failed to open binder driver\n");
284        return -1;
285    }
286
287    if (binder_become_context_manager(bs)) {
288        ALOGE("cannot become context manager (%s)\n", strerror(errno));
289        return -1;
290    }
291
292    svcmgr_handle = BINDER_SERVICE_MANAGER;
293    binder_loop(bs, svcmgr_handler);
294
295    return 0;
296}
297