1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "cm_response.h"
17 
18 #include <dlfcn.h>
19 #include <sys/time.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22 
23 #include "ipc_skeleton.h"
24 
25 #include "cm_log.h"
26 #include "cm_mem.h"
27 #include "os_account_manager.h"
28 
29 #define MAX_SIZE_CAPACITY 409600
30 
31 using namespace OHOS;
32 
CmSendResponse(const struct CmContext * context,int32_t result,const struct CmBlob * response)33 void CmSendResponse(const struct CmContext *context, int32_t result, const struct CmBlob *response)
34 {
35     if (context == nullptr) {
36         CM_LOG_E("SendResponse NULL Pointer");
37         return;
38     }
39     MessageParcel *reply = reinterpret_cast<MessageParcel *>(const_cast<CmContext *>(context));
40     reply->WriteInt32(result);
41     if (result != CM_SUCCESS) {
42         CM_LOG_E("SendResponse result is %d.", result);
43         return;
44     }
45     if (response == nullptr) {
46         reply->WriteUint32(0);
47     } else {
48         reply->SetMaxCapacity(MAX_SIZE_CAPACITY);
49         reply->WriteUint32(response->size);
50         reply->WriteBuffer(response->data, static_cast<size_t>(response->size));
51     }
52 }
53 
CmGetProcessInfoForIPC(struct CmContext * cmContext)54 int32_t CmGetProcessInfoForIPC(struct CmContext *cmContext)
55 {
56     if (cmContext == nullptr) {
57         CM_LOG_D("CmGetProcessInfoForIPC Paramset is Invalid");
58         return CM_FAILURE;
59     }
60 
61     int userId = 0;
62     auto callingUid = IPCSkeleton::GetCallingUid();
63 
64     OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(callingUid, userId);
65 
66     cmContext->uid = (uint32_t)callingUid;
67     cmContext->userId = static_cast<uint32_t>(userId);
68 
69     return CM_SUCCESS;
70 }
71