ipc_process_skeleton.c 12.8 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"
L
liubb_0516 已提交
23 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
#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 已提交
53
    RemoteInvoker *invoker = GetRemoteInvoker();
L
liubb_0516 已提交
54 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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    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;
        }
        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 已提交
107
    RemoteInvoker *invoker = GetRemoteInvoker();
L
liubb_0516 已提交
108 109 110 111 112 113 114
    if (invoker != NULL) {
        return (invoker->SetMaxWorkThread)(maxThreadNum);
    }
    RPC_LOG_ERROR("current thread context not init");
    return ERR_THREAD_INVOKER_NOT_INIT;
}

L
liubb_0516 已提交
115
void JoinMainWorkThread(void)
L
liubb_0516 已提交
116
{
L
liubb_0516 已提交
117
    RemoteInvoker *invoker = GetRemoteInvoker();
L
liubb_0516 已提交
118 119 120 121 122
    if (invoker != NULL) {
        (invoker->JoinThread)(true);
    }
}

L
liubb_0516 已提交
123
pid_t ProcessGetCallingPid(void)
L
liubb_0516 已提交
124 125 126 127 128
{
    ThreadContext *currentContext = GetCurrentThreadContext();
    if (currentContext != NULL) {
        return currentContext->callerPid;
    }
L
liubb_0516 已提交
129
    return RpcGetPid();
L
liubb_0516 已提交
130 131
}

L
liubb_0516 已提交
132
pid_t ProcessGetCallingUid(void)
L
liubb_0516 已提交
133 134 135 136 137
{
    ThreadContext *currentContext = GetCurrentThreadContext();
    if (currentContext != NULL) {
        return currentContext->callerUid;
    }
L
liubb_0516 已提交
138
    return RpcGetUid();
L
liubb_0516 已提交
139 140 141 142 143 144 145 146 147 148
}

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

int32_t SetRegistryObject(SvcIdentity target)
{
    int32_t ret = ERR_THREAD_INVOKER_NOT_INIT;
L
liubb_0516 已提交
149
    RemoteInvoker *invoker = GetRemoteInvoker();
L
liubb_0516 已提交
150 151 152 153 154 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
    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 已提交
180
        RemoteInvoker *invoker = GetRemoteInvoker();
L
liubb_0516 已提交
181 182 183 184 185 186
        if (invoker != NULL) {
            (invoker->ReleaseHandle)(handle);
        }
    }
}

L
liubb_0516 已提交
187 188
int32_t ProcessSendRequest(SvcIdentity target, uint32_t code, IpcIo *data, IpcIo *reply,
    MessageOption option, uintptr_t *buffer)
L
liubb_0516 已提交
189 190
{
    int32_t ret = ERR_THREAD_INVOKER_NOT_INIT;
L
liubb_0516 已提交
191
    RemoteInvoker *invoker = GetRemoteInvoker();
L
liubb_0516 已提交
192 193 194 195 196 197 198 199 200 201 202 203
    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 已提交
204
    RemoteInvoker *invoker = GetRemoteInvoker();
L
liubb_0516 已提交
205 206 207 208 209 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
    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 已提交
243
void OnFirstStrongRef(int32_t handle)
L
liubb_0516 已提交
244 245 246 247 248 249
{
    if (handle <= 0) {
        RPC_LOG_ERROR("invalid handle.");
        return;
    }
    if (FirstAddObject(handle)) {
L
liubb_0516 已提交
250
        RemoteInvoker *invoker = GetRemoteInvoker();
L
liubb_0516 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 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
        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)
{
    int32_t ret = ERR_INVALID_PARAM;
    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 已提交
312
        RemoteInvoker *invoker = GetRemoteInvoker();
L
liubb_0516 已提交
313 314 315 316 317 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 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
        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;
    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 已提交
382
        RemoteInvoker *invoker = GetRemoteInvoker();
L
liubb_0516 已提交
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
        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 已提交
407
    RemoteInvoker *invoker = GetRemoteInvoker();
L
liubb_0516 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420
    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);
}