ServiceManager: Allow system services running as secondary users to add services

This should be reverted when all system services have been cleaned up to not
do this. A process looking up a service while running in the background will
see the service registered by the active user (assuming the service is
registered on every user switch), not the service registered by the user that
the process itself belongs to.

BUG: 30795333
Change-Id: I1b74d58be38ed358f43c163692f9e704f8f31dbe
(cherry picked from commit e6bbe69ba739c8a08837134437aaccfea5f1d943)

int do_add_service(struct binder_state *bs,
 const uint16_t *s, size_t len,
 uint32_t handle, uid_t uid, int allow_isolated,
 pid_t spid)
{
 struct svcinfo *si;


 if (!handle || (len == 0) || (len > 127))
 return -1;

 if (!svc_can_register(s, len, spid, uid)) {
        ALOGE("add_service('%s',%x) uid=%d - PERMISSION DENIED\n",
             str8(s, len), 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, len), 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, len), 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 do_add_service(struct binder_state *bs,
 const uint16_t *s, size_t len,
 uint32_t handle, uid_t uid, int allow_isolated,
 pid_t spid)
{
 struct svcinfo *si;


 if (!handle || (len == 0) || (len > 127))
 return -1;

 if (!svc_can_register(s, len, spid, uid)) {
        ALOGE("add_service('%s',%x) uid=%d - PERMISSION DENIED\n",
             str8(s, len), 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, len), 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, len), 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;
}
