88f6ad18de
* commit 'c93865de80a16d3638936890fb42eb175284044b': ServiceManager: Implement PING_TRANSACTION ServiceManager: Use 32/64 bit types from new binder header Binder: Use 64 bit pointers in 32 processes if selected by the target Add BINDER_IPC_32BIT to CFLAGS unless TARGET_USES_64_BIT_BINDER is true Binder: Make binder portable ServiceManager: Fix the binder interface ServiceManager: Store handles in uint32_t instead of void * ServiceManager: Generic Fixes ServiceManager: Add extra error handling ServiceManager: Fix Android.mk ServiceManager: Make use of kernel exported structures
299 lines
7.5 KiB
C
299 lines
7.5 KiB
C
/* Copyright 2008 The Android Open Source Project
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
|
|
#include <private/android_filesystem_config.h>
|
|
|
|
#include "binder.h"
|
|
|
|
#if 0
|
|
#define ALOGI(x...) fprintf(stderr, "svcmgr: " x)
|
|
#define ALOGE(x...) fprintf(stderr, "svcmgr: " x)
|
|
#else
|
|
#define LOG_TAG "ServiceManager"
|
|
#include <cutils/log.h>
|
|
#endif
|
|
|
|
/* TODO:
|
|
* These should come from a config file or perhaps be
|
|
* based on some namespace rules of some sort (media
|
|
* uid can register media.*, etc)
|
|
*/
|
|
static struct {
|
|
uid_t uid;
|
|
const char *name;
|
|
} allowed[] = {
|
|
{ AID_MEDIA, "media.audio_flinger" },
|
|
{ AID_MEDIA, "media.log" },
|
|
{ AID_MEDIA, "media.player" },
|
|
{ AID_MEDIA, "media.camera" },
|
|
{ AID_MEDIA, "media.audio_policy" },
|
|
{ AID_AUDIO, "audio" },
|
|
{ AID_INPUT, "input" },
|
|
{ AID_DRM, "drm.drmManager" },
|
|
{ AID_NFC, "nfc" },
|
|
{ AID_BLUETOOTH, "bluetooth" },
|
|
{ AID_RADIO, "radio.phone" },
|
|
{ AID_RADIO, "radio.sms" },
|
|
{ AID_RADIO, "radio.phonesubinfo" },
|
|
{ AID_RADIO, "radio.simphonebook" },
|
|
/* TODO: remove after phone services are updated: */
|
|
{ AID_RADIO, "phone" },
|
|
{ AID_RADIO, "sip" },
|
|
{ AID_RADIO, "isms" },
|
|
{ AID_RADIO, "iphonesubinfo" },
|
|
{ AID_RADIO, "simphonebook" },
|
|
{ AID_MEDIA, "common_time.clock" },
|
|
{ AID_MEDIA, "common_time.config" },
|
|
{ AID_KEYSTORE, "android.security.keystore" },
|
|
};
|
|
|
|
uint32_t svcmgr_handle;
|
|
|
|
const char *str8(const uint16_t *x)
|
|
{
|
|
static char buf[128];
|
|
unsigned max = 127;
|
|
char *p = buf;
|
|
|
|
if (x) {
|
|
while (*x && max--) {
|
|
*p++ = *x++;
|
|
}
|
|
}
|
|
*p++ = 0;
|
|
return buf;
|
|
}
|
|
|
|
int str16eq(const uint16_t *a, const char *b)
|
|
{
|
|
while (*a && *b)
|
|
if (*a++ != *b++) return 0;
|
|
if (*a || *b)
|
|
return 0;
|
|
return 1;
|
|
}
|
|
|
|
int svc_can_register(uid_t uid, const uint16_t *name)
|
|
{
|
|
size_t n;
|
|
|
|
if ((uid == 0) || (uid == AID_SYSTEM))
|
|
return 1;
|
|
|
|
for (n = 0; n < sizeof(allowed) / sizeof(allowed[0]); n++)
|
|
if ((uid == allowed[n].uid) && str16eq(name, allowed[n].name))
|
|
return 1;
|
|
|
|
return 0;
|
|
}
|
|
|
|
struct svcinfo
|
|
{
|
|
struct svcinfo *next;
|
|
uint32_t handle;
|
|
struct binder_death death;
|
|
int allow_isolated;
|
|
size_t len;
|
|
uint16_t name[0];
|
|
};
|
|
|
|
struct svcinfo *svclist = NULL;
|
|
|
|
struct svcinfo *find_svc(const uint16_t *s16, size_t len)
|
|
{
|
|
struct svcinfo *si;
|
|
|
|
for (si = svclist; si; si = si->next) {
|
|
if ((len == si->len) &&
|
|
!memcmp(s16, si->name, len * sizeof(uint16_t))) {
|
|
return si;
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
void svcinfo_death(struct binder_state *bs, void *ptr)
|
|
{
|
|
struct svcinfo *si = (struct svcinfo* ) ptr;
|
|
|
|
ALOGI("service '%s' died\n", str8(si->name));
|
|
if (si->handle) {
|
|
binder_release(bs, si->handle);
|
|
si->handle = 0;
|
|
}
|
|
}
|
|
|
|
uint16_t svcmgr_id[] = {
|
|
'a','n','d','r','o','i','d','.','o','s','.',
|
|
'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r'
|
|
};
|
|
|
|
|
|
uint32_t do_find_service(struct binder_state *bs, const uint16_t *s, size_t len, uid_t uid)
|
|
{
|
|
struct svcinfo *si;
|
|
|
|
si = find_svc(s, len);
|
|
//ALOGI("check_service('%s') handle = %x\n", str8(s), si ? si->handle : 0);
|
|
if (si && si->handle) {
|
|
if (!si->allow_isolated) {
|
|
// If this service doesn't allow access from isolated processes,
|
|
// then check the uid to see if it is isolated.
|
|
uid_t appid = uid % AID_USER;
|
|
if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) {
|
|
return 0;
|
|
}
|
|
}
|
|
return si->handle;
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
int do_add_service(struct binder_state *bs,
|
|
const uint16_t *s, size_t len,
|
|
uint32_t handle, uid_t uid, int allow_isolated)
|
|
{
|
|
struct svcinfo *si;
|
|
|
|
//ALOGI("add_service('%s',%x,%s) uid=%d\n", str8(s), handle,
|
|
// allow_isolated ? "allow_isolated" : "!allow_isolated", uid);
|
|
|
|
if (!handle || (len == 0) || (len > 127))
|
|
return -1;
|
|
|
|
if (!svc_can_register(uid, s)) {
|
|
ALOGE("add_service('%s',%x) uid=%d - PERMISSION DENIED\n",
|
|
str8(s), handle, uid);
|
|
return -1;
|
|
}
|
|
|
|
si = find_svc(s, len);
|
|
if (si) {
|
|
if (si->handle) {
|
|
ALOGE("add_service('%s',%x) uid=%d - ALREADY REGISTERED, OVERRIDE\n",
|
|
str8(s), handle, uid);
|
|
svcinfo_death(bs, si);
|
|
}
|
|
si->handle = handle;
|
|
} else {
|
|
si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
|
|
if (!si) {
|
|
ALOGE("add_service('%s',%x) uid=%d - OUT OF MEMORY\n",
|
|
str8(s), handle, uid);
|
|
return -1;
|
|
}
|
|
si->handle = handle;
|
|
si->len = len;
|
|
memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
|
|
si->name[len] = '\0';
|
|
si->death.func = (void*) svcinfo_death;
|
|
si->death.ptr = si;
|
|
si->allow_isolated = allow_isolated;
|
|
si->next = svclist;
|
|
svclist = si;
|
|
}
|
|
|
|
binder_acquire(bs, handle);
|
|
binder_link_to_death(bs, handle, &si->death);
|
|
return 0;
|
|
}
|
|
|
|
int svcmgr_handler(struct binder_state *bs,
|
|
struct binder_transaction_data *txn,
|
|
struct binder_io *msg,
|
|
struct binder_io *reply)
|
|
{
|
|
struct svcinfo *si;
|
|
uint16_t *s;
|
|
size_t len;
|
|
uint32_t handle;
|
|
uint32_t strict_policy;
|
|
int allow_isolated;
|
|
|
|
//ALOGI("target=%x code=%d pid=%d uid=%d\n",
|
|
// txn->target.handle, txn->code, txn->sender_pid, txn->sender_euid);
|
|
|
|
if (txn->target.handle != svcmgr_handle)
|
|
return -1;
|
|
|
|
if (txn->code == PING_TRANSACTION)
|
|
return 0;
|
|
|
|
// Equivalent to Parcel::enforceInterface(), reading the RPC
|
|
// header with the strict mode policy mask and the interface name.
|
|
// Note that we ignore the strict_policy and don't propagate it
|
|
// further (since we do no outbound RPCs anyway).
|
|
strict_policy = bio_get_uint32(msg);
|
|
s = bio_get_string16(msg, &len);
|
|
if ((len != (sizeof(svcmgr_id) / 2)) ||
|
|
memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
|
|
fprintf(stderr,"invalid id %s\n", str8(s));
|
|
return -1;
|
|
}
|
|
|
|
switch(txn->code) {
|
|
case SVC_MGR_GET_SERVICE:
|
|
case SVC_MGR_CHECK_SERVICE:
|
|
s = bio_get_string16(msg, &len);
|
|
handle = do_find_service(bs, s, len, txn->sender_euid);
|
|
if (!handle)
|
|
break;
|
|
bio_put_ref(reply, handle);
|
|
return 0;
|
|
|
|
case SVC_MGR_ADD_SERVICE:
|
|
s = bio_get_string16(msg, &len);
|
|
handle = bio_get_ref(msg);
|
|
allow_isolated = bio_get_uint32(msg) ? 1 : 0;
|
|
if (do_add_service(bs, s, len, handle, txn->sender_euid, allow_isolated))
|
|
return -1;
|
|
break;
|
|
|
|
case SVC_MGR_LIST_SERVICES: {
|
|
uint32_t n = bio_get_uint32(msg);
|
|
|
|
si = svclist;
|
|
while ((n-- > 0) && si)
|
|
si = si->next;
|
|
if (si) {
|
|
bio_put_string16(reply, si->name);
|
|
return 0;
|
|
}
|
|
return -1;
|
|
}
|
|
default:
|
|
ALOGE("unknown code %d\n", txn->code);
|
|
return -1;
|
|
}
|
|
|
|
bio_put_uint32(reply, 0);
|
|
return 0;
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
struct binder_state *bs;
|
|
|
|
bs = binder_open(128*1024);
|
|
if (!bs) {
|
|
ALOGE("failed to open binder driver\n");
|
|
return -1;
|
|
}
|
|
|
|
if (binder_become_context_manager(bs)) {
|
|
ALOGE("cannot become context manager (%s)\n", strerror(errno));
|
|
return -1;
|
|
}
|
|
|
|
svcmgr_handle = BINDER_SERVICE_MANAGER;
|
|
binder_loop(bs, svcmgr_handler);
|
|
|
|
return 0;
|
|
}
|