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 <fcntl.h>
17 #include "common_utils.h"
18 #include "file_util.h"
19 
20 namespace OHOS {
21 namespace HiviewDFX {
22 static bool g_isSelinuxEnabled = false;
InitSeLinuxEnabled()23 void InitSeLinuxEnabled()
24 {
25     constexpr uint32_t BUF_SIZE_64 = 64;
26     char buffer[BUF_SIZE_64] = {'\0'};
27     FILE* fp = popen("getenforce", "r");
28     if (fp != nullptr) {
29         fgets(buffer, sizeof(buffer), fp);
30         std::string str = buffer;
31         printf("buffer is %s\n", str.c_str());
32         if (str.find("Enforcing") != str.npos) {
33             printf("Enforcing %s\n", str.c_str());
34             g_isSelinuxEnabled = true;
35         } else {
36             printf("This isn't Enforcing %s\n", str.c_str());
37         }
38         pclose(fp);
39     } else {
40         printf("fp == nullptr\n");
41     }
42     system("setenforce 0");
43 
44     constexpr mode_t defaultLogDirMode = 0770;
45     std::string path = "/data/test/log";
46     if (!FileUtil::FileExists(path)) {
47         FileUtil::ForceCreateDirectory(path);
48         FileUtil::ChangeModeDirectory(path, defaultLogDirMode);
49     }
50 }
51 
CancelSeLinuxEnabled()52 void CancelSeLinuxEnabled()
53 {
54     if (g_isSelinuxEnabled) {
55         system("setenforce 1");
56         g_isSelinuxEnabled = false;
57     }
58 }
59 } // namespace HiviewDFX
60 } // namespace OHOS
61