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