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 "process_group_util.h"
17 #include <sstream>
18 #include <string>
19 #include <fstream>
20 #include <fcntl.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23 #include <linux/limits.h>
24 #include "nlohmann/json.hpp"
25 #include "securec.h"
26 #include "process_group_log.h"
27 #include "res_exe_type.h"
28 #include "res_sched_exe_client.h"
29 
30 namespace OHOS {
31 namespace ResourceSchedule {
32 namespace CgroupSetting {
33 namespace {
34     static constexpr int FMT_STR_BUFF_LEN = 256;
35 }
36 
FormatString(const char * fmt,va_list vararg)37 std::string FormatString(const char* fmt, va_list vararg)
38 {
39     std::string strResult;
40     if (fmt) {
41         char buffer[FMT_STR_BUFF_LEN] = { 0 };
42         int nWritten = vsnprintf_s(buffer, FMT_STR_BUFF_LEN, FMT_STR_BUFF_LEN - 1, fmt, vararg);
43         if (nWritten > 0) {
44             strResult.append(buffer, 0, nWritten);
45         }
46     }
47     return strResult;
48 }
49 
StringPrintf(const char * fmt,...)50 std::string StringPrintf(const char* fmt, ...)
51 {
52     va_list vararg;
53     va_start(vararg, fmt);
54     std::string result = FormatString(fmt, vararg);
55     va_end(vararg);
56     return result;
57 }
58 
GetRealPath(const std::string & path,std::string & realPath)59 bool GetRealPath(const std::string& path, std::string& realPath)
60 {
61     char resolvedPath[PATH_MAX] = { 0 };
62     if (path.size() > PATH_MAX || !realpath(path.c_str(), resolvedPath)) {
63         PGCGS_LOGE("%{public}s realpath for %s failed", __func__, path.c_str());
64         return false;
65     }
66     realPath = std::string(resolvedPath);
67     return true;
68 }
69 
ReadFileToString(const std::string & filePath,std::string & content)70 bool ReadFileToString(const std::string& filePath, std::string& content)
71 {
72     std::string realPath;
73     if (!GetRealPath(filePath, realPath)) {
74         return false;
75     }
76     int fd = open(realPath.c_str(), O_RDONLY | O_CLOEXEC);
77     if (fd < 0) {
78         return false;
79     }
80     struct stat sb {};
81     if (fstat(fd, &sb) != -1 && sb.st_size > 0) {
82         content.resize(sb.st_size);
83     }
84 
85     ssize_t remaining = sb.st_size;
86     bool readStatus = true;
87     char* p = const_cast<char*>(content.data());
88 
89     while (remaining > 0) {
90         ssize_t n = read(fd, p, remaining);
91         if (n < 0) {
92             readStatus = false;
93             break;
94         }
95         p += n;
96         remaining -= n;
97     }
98     close(fd);
99     return readStatus;
100 }
101 
ReadFileToStringForVFSFromExecutor(int tid,std::string & content)102 bool ReadFileToStringForVFSFromExecutor(int tid, std::string& content)
103 {
104     nlohmann::json payload;
105     payload["pid"] = tid;
106     nlohmann::json reply;
107     ResourceSchedule::ResSchedExeClient::GetInstance().SendRequestSync(
108         ResExeType::RES_TYPE_CGROUP_SYNC_EVENT, 0, payload, reply);
109     std::string resStr{"res"};
110     if (!reply.contains(resStr) || !reply[resStr].is_string()) {
111         return false;
112     }
113     content = reply[resStr].get<std::string>();
114     return true;
115 }
116 
ReadFileToStringForVFS(const std::string & filePath,std::string & content)117 bool ReadFileToStringForVFS(const std::string& filePath, std::string& content)
118 {
119     std::string realPath;
120     if (!GetRealPath(filePath, realPath)) {
121         return false;
122     }
123     std::ifstream fin(realPath.c_str(), std::ios::in);
124     if (!fin) {
125         return false;
126     }
127     std::stringstream ss;
128     ss << fin.rdbuf();
129     content = ss.str();
130     fin.close();
131     return true;
132 }
133 
134 
WriteStringToFile(int fd,const std::string & content)135 bool WriteStringToFile(int fd, const std::string& content)
136 {
137     const char *p = content.data();
138     size_t remaining = content.size();
139     while (remaining > 0) {
140         ssize_t n = write(fd, p, remaining);
141         if (n == -1) {
142             return false;
143         }
144         p += n;
145         remaining -= n;
146     }
147     return true;
148 }
149 
WriteStringToFile(const std::string & content,const std::string & filePath)150 bool WriteStringToFile(const std::string& content, const std::string& filePath)
151 {
152     std::string realPath;
153     if (!GetRealPath(filePath, realPath)) {
154         return false;
155     }
156     if (access(realPath.c_str(), W_OK)) {
157         return false;
158     }
159     int fd = open(realPath.c_str(), O_WRONLY | O_CLOEXEC);
160     if (fd < 0) {
161         PGCGS_LOGE("%{public}s failed. file: %s, fd = %{public}d", __func__, realPath.c_str(), fd);
162         return false;
163     }
164     bool result = WriteStringToFile(fd, content);
165     close(fd);
166     return result;
167 }
168 } // namespace CgroupSetting
169 } // namespace ResourceSchedule
170 } // namespace OHOS
171