1 /*
2 * Copyright (c) 2025 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 "utils/hmsf_utils.h"
17
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <sys/ioctl.h>
21
22 namespace OHOS {
23 namespace AbilityRuntime {
24 #define HMFS_MONITOR_FL 0x00000002
25 #define HMF_IOCTL_HW_GET_FLAGS _IOR(0xf5, 70, unsigned int)
26 #define HMF_IOCTL_HW_SET_FLAGS _IOR(0xf5, 71, unsigned int)
AddDeleteDfx(const std::string & path)27 void HmfsUtils::AddDeleteDfx(const std::string &path)
28 {
29 int32_t fd = open(path.c_str(), O_RDONLY);
30 if (fd < 0) {
31 TAG_LOGD(AAFwkTag::ABILITYMGR, "open dfx path %{public}s failed", path.c_str());
32 return;
33 }
34 unsigned int flags = 0;
35 int32_t ret = ioctl(fd, HMF_IOCTL_HW_GET_FLAGS, &flags);
36 if (ret < 0) {
37 TAG_LOGD(AAFwkTag::ABILITYMGR, "check dfx flag path %{public}s failed errno:%{public}d", path.c_str(), errno);
38 close(fd);
39 return;
40 }
41 if (flags & HMFS_MONITOR_FL) {
42 TAG_LOGD(AAFwkTag::ABILITYMGR, "Delete Control flag is already set");
43 close(fd);
44 return;
45 }
46 flags |= HMFS_MONITOR_FL;
47 ret = ioctl(fd, HMF_IOCTL_HW_SET_FLAGS, &flags);
48 if (ret < 0) {
49 TAG_LOGW(AAFwkTag::ABILITYMGR, "Add dfx flag failed errno:%{public}d path %{public}s", errno, path.c_str());
50 close(fd);
51 return;
52 }
53 TAG_LOGI(AAFwkTag::ABILITYMGR, "Delete Control flag of %{public}s is set succeed", path.c_str());
54 close(fd);
55 return;
56 }
57
58 } // namespace AbilityRuntime
59 } // namespace OHOS
60