1 /*
2  * Copyright (c) 2024 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 "child_process_args.h"
17 
18 #include "hilog_tag_wrapper.h"
19 #include "message_parcel.h"
20 #include "parcel_macro_base.h"
21 #include "string_ex.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
ReadFromParcel(Parcel & parcel)25 bool ChildProcessArgs::ReadFromParcel(Parcel &parcel)
26 {
27     entryParams = Str16ToStr8(parcel.ReadString16());
28 
29     int32_t fdsSize = parcel.ReadInt32();
30     if (fdsSize > CHILD_PROCESS_ARGS_FDS_MAX_COUNT) {
31         TAG_LOGE(AAFwkTag::PROCESSMGR, "fds count must <= %{public}d.", CHILD_PROCESS_ARGS_FDS_MAX_COUNT);
32         return false;
33     }
34     auto messageParcel = static_cast<MessageParcel*>(&parcel);
35     if (messageParcel == nullptr) {
36         TAG_LOGE(AAFwkTag::APPMGR, "static cast messageParcel failed");
37         return false;
38     }
39     for (int32_t i = 0; i < fdsSize; i++) {
40         std::string key = Str16ToStr8(parcel.ReadString16());
41         if (!CheckFdKeyLength(key)) {
42             return false;
43         }
44         int32_t fd = messageParcel->ReadFileDescriptor();
45         fds.emplace(key, fd);
46     }
47     return true;
48 }
49 
Unmarshalling(Parcel & parcel)50 ChildProcessArgs *ChildProcessArgs::Unmarshalling(Parcel &parcel)
51 {
52     ChildProcessArgs *obj = new (std::nothrow) ChildProcessArgs();
53     if (obj && !obj->ReadFromParcel(parcel)) {
54         TAG_LOGW(AAFwkTag::APPMGR, "read from parcel failed");
55         delete obj;
56         obj = nullptr;
57     }
58     return obj;
59 }
60 
Marshalling(Parcel & parcel) const61 bool ChildProcessArgs::Marshalling(Parcel &parcel) const
62 {
63     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(entryParams));
64     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, fds.size());
65     auto messageParcel = static_cast<MessageParcel*>(&parcel);
66     if (messageParcel == nullptr) {
67         TAG_LOGW(AAFwkTag::APPMGR, "static cast messageParcel failed");
68         return false;
69     }
70     if (!CheckFdsSize()) {
71         return false;
72     }
73     for (auto &item : fds) {
74         if (!CheckFdKeyLength(item.first)) {
75             return false;
76         }
77         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(item.first));
78         if (!messageParcel->WriteFileDescriptor(item.second)) {
79             TAG_LOGE(AAFwkTag::APPMGR, "WriteFileDescriptor failed, fd:%{private}d", item.second);
80             return false;
81         }
82     }
83     return true;
84 }
85 
CheckFdsSize() const86 bool ChildProcessArgs::CheckFdsSize() const
87 {
88     TAG_LOGD(AAFwkTag::APPMGR, "CheckFdsSize: %{public}zu", fds.size());
89     if (fds.size() > CHILD_PROCESS_ARGS_FDS_MAX_COUNT) {
90         TAG_LOGE(AAFwkTag::PROCESSMGR, "fds count must <= %{public}d.", CHILD_PROCESS_ARGS_FDS_MAX_COUNT);
91         return false;
92     }
93     return true;
94 }
95 
CheckFdsKeyLength() const96 bool ChildProcessArgs::CheckFdsKeyLength() const
97 {
98     for (auto iter = fds.begin(); iter != fds.end(); iter++) {
99         if (!CheckFdKeyLength(iter->first)) {
100             return false;
101         }
102     }
103     return true;
104 }
105 
CheckFdKeyLength(const std::string & key)106 bool ChildProcessArgs::CheckFdKeyLength(const std::string &key)
107 {
108     if (key.length() > CHILD_PROCESS_ARGS_FD_KEY_MAX_LENGTH) {
109         TAG_LOGE(AAFwkTag::PROCESSMGR, "fd key length must <= %{public}d, key:%{public}s",
110             CHILD_PROCESS_ARGS_FD_KEY_MAX_LENGTH, key.c_str());
111         return false;
112     }
113     return true;
114 }
115 }  // namespace AppExecFwk
116 }  // namespace OHOS
117