OSDN Git Service

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