ipc_process_skeleton.c 13.1 KB
Newer Older
L
liubb_0516 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ipc_process_skeleton.h"
#include "ipc_skeleton.h"
#include "ipc_thread_pool.h"
#include "iremote_invoker.h"
#include "rpc_errno.h"
#include "rpc_log.h"
L
liubb_0516 已提交
22
#include "rpc_os_adapter.h"
Y
yangguangzhao 已提交
23
#include "rpc_process_skeleton.h"
L
liubb_0516 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include "rpc_types.h"
#include "securec.h"
#include "utils_list.h"

static IpcSkeleton *g_ipcSkeleton = NULL;
static pthread_mutex_t g_ipcSkeletonMutex = PTHREAD_MUTEX_INITIALIZER;
static SvcIdentity g_samgrSvc = {.handle = 0, .token = 0, .cookie = 0};

static void DeleteIpcSkeleton(IpcSkeleton *temp)
{
    if (temp == NULL) {
        return;
    }
    DeinitThreadPool(temp->threadPool);
    free(temp);
}

static IpcSkeleton* IpcProcessSkeleton()
{
    IpcSkeleton *temp = (IpcSkeleton *)calloc(1, sizeof(IpcSkeleton));
    if (temp == NULL) {
        RPC_LOG_ERROR("create ipc skeleton failed.");
        return NULL;
    }
    temp->threadPool = InitThreadPool(SET_MAX_THREADS_DEFAULT);
    if (temp->threadPool == NULL) {
        free(temp);
        RPC_LOG_ERROR("init thread pool failed.");
        return NULL;
    }
L
liubb_0516 已提交
54
    RemoteInvoker *invoker = GetRemoteInvoker();
L
liubb_0516 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    if (invoker == NULL) {
        DeleteIpcSkeleton(temp);
        RPC_LOG_ERROR("get remote invoker failed.");
        return NULL;
    }
    if ((invoker->SetMaxWorkThread)(SET_MAX_THREADS_DEFAULT) != ERR_NONE) {
        DeleteIpcSkeleton(temp);
        RPC_LOG_ERROR("init thread context failed.");
        return NULL;
    }
    UtilsListInit(&temp->objects);
    pthread_mutex_init(&temp->lock, NULL);
    return temp;
}

IpcSkeleton *GetCurrentSkeleton(void)
{
    if (g_ipcSkeleton == NULL) {
        if (pthread_mutex_lock(&g_ipcSkeletonMutex) != 0) {
            RPC_LOG_ERROR("init ipc skeleton lock failed.");
            return NULL;
        }
        if (g_ipcSkeleton == NULL) {
            IpcSkeleton *temp = IpcProcessSkeleton();
            if (temp == NULL) {
                pthread_mutex_unlock(&g_ipcSkeletonMutex);
                RPC_LOG_ERROR("create binder connector failed.");
                return NULL;
            }
            g_ipcSkeleton = temp;
Y
yangguangzhao 已提交
85 86 87 88
            int32_t ret = RpcProcessSkeleton();
            if (ret != ERR_NONE) {
                RPC_LOG_ERROR("rpc process skeleton init failed");
            }
L
liubb_0516 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
        }
        pthread_mutex_unlock(&g_ipcSkeletonMutex);
    }
    return g_ipcSkeleton;
}

int32_t SpawnThread(int32_t policy, int32_t proto)
{
    if (g_ipcSkeleton == NULL || g_ipcSkeleton->threadPool == NULL) {
        RPC_LOG_ERROR("ipc skeleton not init");
        return ERR_IPC_SKELETON_NOT_INIT;
    }
    return SpawnNewThread(g_ipcSkeleton->threadPool, policy, proto);
}

int32_t SetMaxWorkThread(int32_t maxThreadNum)
{
    if (g_ipcSkeleton == NULL || g_ipcSkeleton->threadPool == NULL) {
        RPC_LOG_ERROR("ipc skeleton not init");
        return ERR_IPC_SKELETON_NOT_INIT;
    }
    UpdateMaxThreadNum(g_ipcSkeleton->threadPool, maxThreadNum);

L
liubb_0516 已提交
112
    RemoteInvoker *invoker = GetRemoteInvoker();
L
liubb_0516 已提交
113 114 115 116 117 118 119
    if (invoker != NULL) {
        return (invoker->SetMaxWorkThread)(maxThreadNum);
    }
    RPC_LOG_ERROR("current thread context not init");
    return ERR_THREAD_INVOKER_NOT_INIT;
}

L
liubb_0516 已提交
120
void JoinMainWorkThread(void)
L
liubb_0516 已提交
121
{
L
liubb_0516 已提交
122
    RemoteInvoker *invoker = GetRemoteInvoker();
L
liubb_0516 已提交
123 124 125 126 127
    if (invoker != NULL) {
        (invoker->JoinThread)(true);
    }
}

L
liubb_0516 已提交
128
pid_t ProcessGetCallingPid(void)
L
liubb_0516 已提交
129 130 131 132 133
{
    ThreadContext *currentContext = GetCurrentThreadContext();
    if (currentContext != NULL) {
        return currentContext->callerPid;
    }
L
liubb_0516 已提交
134
    return RpcGetPid();
L
liubb_0516 已提交
135 136
}

L
liubb_0516 已提交
137
pid_t ProcessGetCallingUid(void)
L
liubb_0516 已提交
138 139 140 141 142
{
    ThreadContext *currentContext = GetCurrentThreadContext();
    if (currentContext != NULL) {
        return currentContext->callerUid;
    }
L
liubb_0516 已提交
143
    return RpcGetUid();
L
liubb_0516 已提交
144 145 146 147 148 149 150 151 152 153
}

const SvcIdentity *GetRegistryObject(void)
{
    return &g_samgrSvc;
}

int32_t SetRegistryObject(SvcIdentity target)
{
    int32_t ret = ERR_THREAD_INVOKER_NOT_INIT;
L
liubb_0516 已提交
154
    RemoteInvoker *invoker = GetRemoteInvoker();
L
liubb_0516 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    if (invoker != NULL) {
        ret = (invoker->SetRegistryObject)();
    }
    if (ret == ERR_NONE) {
        g_samgrSvc.handle = -1;
        g_samgrSvc.cookie = target.cookie;
    }
    return ret;
}

static void DeleteDeadHandle(int32_t handle)
{
    if (pthread_mutex_lock(&g_ipcSkeleton->lock) != 0) {
        RPC_LOG_ERROR("Get ipc skeleton mutex failed.");
        return;
    }
    DeathCallback *node = NULL;
    DeathCallback *next = NULL;
    bool isValidHandle = false;
    UTILS_DL_LIST_FOR_EACH_ENTRY_SAFE(node, next, &g_ipcSkeleton->objects, DeathCallback, list)
    {
        if (node->handle == handle) {
            isValidHandle = true;
            UtilsListDelete(&node->list);
            free(node);
            break;
        }
    }
    pthread_mutex_unlock(&g_ipcSkeleton->lock);
    if (isValidHandle) {
L
liubb_0516 已提交
185
        RemoteInvoker *invoker = GetRemoteInvoker();
L
liubb_0516 已提交
186 187 188 189 190 191
        if (invoker != NULL) {
            (invoker->ReleaseHandle)(handle);
        }
    }
}

L
liubb_0516 已提交
192 193
int32_t ProcessSendRequest(SvcIdentity target, uint32_t code, IpcIo *data, IpcIo *reply,
    MessageOption option, uintptr_t *buffer)
L
liubb_0516 已提交
194 195
{
    int32_t ret = ERR_THREAD_INVOKER_NOT_INIT;
L
liubb_0516 已提交
196
    RemoteInvoker *invoker = GetRemoteInvoker();
L
liubb_0516 已提交
197 198 199 200 201 202 203 204 205 206 207 208
    if (invoker != NULL) {
        ret = (invoker->SendRequest)(target, code, data, reply, option, buffer);
    }
    if (ret == ERR_DEAD_OBJECT) {
        RPC_LOG_ERROR("dead binder = %d now delete it", target.handle);
        DeleteDeadHandle(target.handle);
    }
    return ret;
}

int32_t ProcessFreeBuffer(void *ptr)
{
L
liubb_0516 已提交
209
    RemoteInvoker *invoker = GetRemoteInvoker();
L
liubb_0516 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    if (invoker != NULL) {
        return (invoker->FreeBuffer)(ptr);
    }
    return ERR_THREAD_INVOKER_NOT_INIT;
}

static bool FirstAddObject(int32_t handle)
{
    if (pthread_mutex_lock(&g_ipcSkeleton->lock) != 0) {
        RPC_LOG_ERROR("Get ipc skeleton mutex failed.");
        return false;
    }
    DeathCallback *node = NULL;
    DeathCallback *next = NULL;
    UTILS_DL_LIST_FOR_EACH_ENTRY_SAFE(node, next, &g_ipcSkeleton->objects, DeathCallback, list)
    {
        if (node->handle == handle) {
            RPC_LOG_INFO("current handle already exist");
            pthread_mutex_unlock(&g_ipcSkeleton->lock);
            return false;
        }
    }
    node = (DeathCallback*)calloc(1, sizeof(DeathCallback));
    if (node == NULL) {
        pthread_mutex_unlock(&g_ipcSkeleton->lock);
        return false;
    }

    node->handle = handle;
    node->deathNum = 0;
    node->isRemoteDead = false;
    node->isNewHandler = true;
    pthread_mutex_init(&node->lock, NULL);
    UtilsListAdd(&g_ipcSkeleton->objects, &node->list);
    pthread_mutex_unlock(&g_ipcSkeleton->lock);
    return true;
}

L
liubb_0516 已提交
248
void OnFirstStrongRef(int32_t handle)
L
liubb_0516 已提交
249 250 251 252 253 254
{
    if (handle <= 0) {
        RPC_LOG_ERROR("invalid handle.");
        return;
    }
    if (FirstAddObject(handle)) {
L
liubb_0516 已提交
255
        RemoteInvoker *invoker = GetRemoteInvoker();
L
liubb_0516 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
        if (invoker != NULL) {
            (invoker->AcquireHandle)(handle);
        }
    }
}

static uint32_t SetDeathHandlerPair(DeathCallback *node, uint32_t index, OnRemoteDead func, void* args)
{
    node->handler[index].usedFlag = true;
    node->handler[index].func = func;
    node->handler[index].args = args;
    node->deathNum++;
    return index;
}

int32_t ProcessAddDeathRecipient(int32_t handle, OnRemoteDead deathFunc, void *args, uint32_t *cbId)
{
Y
yangguangzhao 已提交
273
    int32_t ret = ERR_NONE;
L
liubb_0516 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
    if (g_ipcSkeleton == NULL) {
        return ERR_IPC_SKELETON_NOT_INIT;
    }
    if (deathFunc == NULL || cbId == NULL) {
        return ERR_INVALID_PARAM;
    }
    if (pthread_mutex_lock(&g_ipcSkeleton->lock) != 0) {
        return ERR_FAILED;
    }
    DeathCallback *node = NULL;
    DeathCallback *next = NULL;
    bool firstDeathNode = false;
    UTILS_DL_LIST_FOR_EACH_ENTRY_SAFE(node, next, &g_ipcSkeleton->objects, DeathCallback, list)
    {
        if (node->handle != handle) {
            continue;
        }
        if (node->isRemoteDead) {
            pthread_mutex_unlock(&g_ipcSkeleton->lock);
            return ERR_DEAD_OBJECT;
        }
        if (node->deathNum == MAX_DEATH_CALLBACK_NUM) {
            pthread_mutex_unlock(&g_ipcSkeleton->lock);
            return ERR_INVALID_PARAM;
        }
        (void)pthread_mutex_lock(&node->lock);
        for (int i = 0; i < MAX_DEATH_CALLBACK_NUM; i++) {
            if (!node->handler[i].usedFlag) {
                *cbId = SetDeathHandlerPair(node, i, deathFunc, args);
                ret = ERR_NONE;
                break;
            }
        }
        pthread_mutex_unlock(&node->lock);
        if (node->deathNum == 1 && node->isNewHandler) {
            firstDeathNode = true;
            node->isNewHandler = false;
        }
        break;
    }
    pthread_mutex_unlock(&g_ipcSkeleton->lock);
    if (firstDeathNode) {
        RPC_LOG_ERROR("first add death callback for handle = %d.", handle);
L
liubb_0516 已提交
317
        RemoteInvoker *invoker = GetRemoteInvoker();
L
liubb_0516 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
        if (invoker != NULL) {
            ret = (invoker->AddDeathRecipient)(handle, node);
        }
    }
    return ret;
}

int32_t ProcessRemoveDeathRecipient(int32_t handle, uint32_t cbId)
{
    int32_t ret = ERR_INVALID_PARAM;
    if (g_ipcSkeleton == NULL) {
        return ERR_IPC_SKELETON_NOT_INIT;
    }
    if (cbId >= MAX_DEATH_CALLBACK_NUM) {
        RPC_LOG_ERROR("invalid callback id.");
        return ERR_INVALID_PARAM;
    }
    if (pthread_mutex_lock(&g_ipcSkeleton->lock) != 0) {
        return ERR_FAILED;
    }
    DeathCallback *node = NULL;
    DeathCallback *next = NULL;
    UTILS_DL_LIST_FOR_EACH_ENTRY_SAFE(node, next, &g_ipcSkeleton->objects, DeathCallback, list)
    {
        if (node->handle != handle) {
            continue;
        }
        if (node->isRemoteDead) {
            RPC_LOG_ERROR("service is dead, delete it later.");
            pthread_mutex_unlock(&g_ipcSkeleton->lock);
            return ERR_DEAD_OBJECT;
        }
        (void)pthread_mutex_lock(&node->lock);
        if (node->handler[cbId].usedFlag) {
            node->handler[cbId].usedFlag = false;
            node->handler[cbId].func = NULL;
            node->deathNum--;
            ret = ERR_NONE;
        }
        (void)pthread_mutex_unlock(&node->lock);
        break;
    }
    pthread_mutex_unlock(&g_ipcSkeleton->lock);
    return ret;
}

int32_t OnRemoteRequestInner(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption option, IpcObjectStub *objectStub)
{
    int32_t result = ERR_NOT_RPC;
Y
yangguangzhao 已提交
367
    result = RpcOnRemoteRequestInner(code, data, reply, option, objectStub);
L
liubb_0516 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
    if (result == ERR_NOT_RPC) {
        if (objectStub != NULL && objectStub->func != NULL) {
            result = (OnRemoteRequest)(objectStub->func)(code, data, reply, option);
        }
    }
    return result;
}

void SendObituary(DeathCallback *deathCallback)
{
    int32_t deathNum = deathCallback->deathNum;
    deathCallback->isRemoteDead = true;
    (void)pthread_mutex_lock(&deathCallback->lock);
    for (int i = 0; i < MAX_DEATH_CALLBACK_NUM; i++) {
        if (deathCallback->handler[i].usedFlag && deathCallback->handler[i].func != NULL) {
            (deathCallback->handler[i].func)(deathCallback->handler[i].args);
        }
    }
    pthread_mutex_unlock(&deathCallback->lock);
    if (deathNum > 0) {
L
liubb_0516 已提交
388
        RemoteInvoker *invoker = GetRemoteInvoker();
L
liubb_0516 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
        if (invoker != NULL) {
            (invoker->RemoveDeathRecipient)(deathCallback->handle, deathCallback);
        }
    }
}

void DeleteDeathCallback(DeathCallback *deathCallback)
{
    (void)pthread_mutex_lock(&g_ipcSkeleton->lock);
    DeathCallback *node = NULL;
    DeathCallback *next = NULL;
    bool isValidDead = false;
    UTILS_DL_LIST_FOR_EACH_ENTRY_SAFE(node, next, &g_ipcSkeleton->objects, DeathCallback, list)
    {
        if (node == deathCallback) {
            isValidDead = true;
            break;
        }
    }
    pthread_mutex_unlock(&g_ipcSkeleton->lock);
    if (!isValidDead) {
        RPC_LOG_ERROR("invalid death node");
        return;
    }
L
liubb_0516 已提交
413
    RemoteInvoker *invoker = GetRemoteInvoker();
L
liubb_0516 已提交
414 415 416 417 418 419 420 421 422 423 424 425
    if (invoker != NULL) {
        (invoker->ReleaseHandle)(deathCallback->handle);
    }
    UtilsListDelete(&deathCallback->list);
    pthread_mutex_destroy(&deathCallback->lock);
    free(deathCallback);
}

void WaitForProxyInit(int32_t handle)
{
    RPC_LOG_INFO("ipc skeleton wait for proxy init");
    OnFirstStrongRef(handle);
Y
yangguangzhao 已提交
426
    UpdateProtoIfNeed(handle);
L
liubb_0516 已提交
427
}