blob: d3ac26bd119dc011f586495d06bf625fb48fe055 [file] [log] [blame]
Colin Crosscf8d1c22014-06-03 13:24:21 -07001/*
2 * Copyright 2014 Google, Inc
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "libprocessgroup"
19
20#include <assert.h>
21#include <dirent.h>
Elliott Hughes0badbd62014-12-29 12:24:25 -080022#include <errno.h>
Colin Crosscf8d1c22014-06-03 13:24:21 -070023#include <fcntl.h>
Chih-Hung Hsiehfcc81152014-11-20 17:05:15 -080024#include <inttypes.h>
Tom Cherry574a0812018-02-23 13:04:40 -080025#include <signal.h>
Colin Crosscf8d1c22014-06-03 13:24:21 -070026#include <stdio.h>
27#include <stdlib.h>
Colin Crosscf8d1c22014-06-03 13:24:21 -070028#include <sys/stat.h>
29#include <sys/types.h>
Tom Cherry70a5ed42017-06-05 19:20:17 -070030#include <unistd.h>
Collin Mullinerf7e79b92016-06-01 21:03:55 +000031
32#include <chrono>
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080033#include <map>
James Hawkins588a2ca2016-02-18 14:52:46 -080034#include <memory>
Elliott Hughes8d532e42016-06-06 21:19:55 -070035#include <mutex>
Tom Cherry70a5ed42017-06-05 19:20:17 -070036#include <set>
Tom Cherry574a0812018-02-23 13:04:40 -080037#include <string>
Elliott Hughes290a2282016-11-14 17:08:47 -080038#include <thread>
Colin Crosscf8d1c22014-06-03 13:24:21 -070039
Robert Benead4852262017-07-16 19:38:11 -070040#include <android-base/file.h>
Elliott Hughes171df0a2016-08-02 13:30:30 -070041#include <android-base/logging.h>
Suren Baghdasaryanbc131c32018-05-23 12:30:44 -070042#include <android-base/properties.h>
Tom Cherry574a0812018-02-23 13:04:40 -080043#include <android-base/stringprintf.h>
44#include <android-base/strings.h>
Suren Baghdasaryan7bf43812019-01-25 05:29:55 +000045#include <cutils/android_filesystem_config.h>
Colin Crosscf8d1c22014-06-03 13:24:21 -070046#include <processgroup/processgroup.h>
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080047#include <task_profiles.h>
Martijn Coenenb82bab62016-01-20 16:39:16 -080048
Suren Baghdasaryanbc131c32018-05-23 12:30:44 -070049using android::base::GetBoolProperty;
Tom Cherry574a0812018-02-23 13:04:40 -080050using android::base::StartsWith;
51using android::base::StringPrintf;
Robert Benead4852262017-07-16 19:38:11 -070052using android::base::WriteStringToFile;
53
Elliott Hughes290a2282016-11-14 17:08:47 -080054using namespace std::chrono_literals;
55
Martijn Coenenb82bab62016-01-20 16:39:16 -080056#define PROCESSGROUP_CGROUP_PROCS_FILE "/cgroup.procs"
Martijn Coenenb82bab62016-01-20 16:39:16 -080057
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080058bool CgroupGetControllerPath(const std::string& cgroup_name, std::string* path) {
Yifan Hong53e0deb2019-03-22 17:01:08 -070059 auto controller = CgroupMap::GetInstance().FindController(cgroup_name);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080060
Yifan Hong53e0deb2019-03-22 17:01:08 -070061 if (!controller.HasValue()) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080062 return false;
63 }
64
65 if (path) {
Yifan Hong53e0deb2019-03-22 17:01:08 -070066 *path = controller.path();
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080067 }
68
69 return true;
70}
71
72bool CgroupGetAttributePath(const std::string& attr_name, std::string* path) {
73 const TaskProfiles& tp = TaskProfiles::GetInstance();
74 const ProfileAttribute* attr = tp.GetAttribute(attr_name);
75
76 if (attr == nullptr) {
77 return false;
78 }
79
80 if (path) {
81 *path = StringPrintf("%s/%s", attr->controller()->path(), attr->file_name().c_str());
82 }
83
84 return true;
85}
86
87bool CgroupGetAttributePathForTask(const std::string& attr_name, int tid, std::string* path) {
88 const TaskProfiles& tp = TaskProfiles::GetInstance();
89 const ProfileAttribute* attr = tp.GetAttribute(attr_name);
90
91 if (attr == nullptr) {
92 return false;
93 }
94
95 if (!attr->GetPathForTask(tid, path)) {
96 PLOG(ERROR) << "Failed to find cgroup for tid " << tid;
97 return false;
98 }
99
100 return true;
101}
102
103bool UsePerAppMemcg() {
104 bool low_ram_device = GetBoolProperty("ro.config.low_ram", false);
105 return GetBoolProperty("ro.config.per_app_memcg", low_ram_device);
106}
107
Peter Collingbourned7157c22018-10-30 15:49:33 -0700108static bool isMemoryCgroupSupported() {
Suren Baghdasaryanfa7a05f2019-05-08 17:59:55 -0700109 static bool memcg_supported = CgroupMap::GetInstance().FindController("memory").IsUsable();
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800110
Peter Collingbourned7157c22018-10-30 15:49:33 -0700111 return memcg_supported;
Martijn Coenenb82bab62016-01-20 16:39:16 -0800112}
113
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800114bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles,
115 bool use_fd_cache) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800116 const TaskProfiles& tp = TaskProfiles::GetInstance();
117
118 for (const auto& name : profiles) {
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800119 TaskProfile* profile = tp.GetProfile(name);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800120 if (profile != nullptr) {
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800121 if (use_fd_cache) {
122 profile->EnableResourceCaching();
123 }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800124 if (!profile->ExecuteForProcess(uid, pid)) {
125 PLOG(WARNING) << "Failed to apply " << name << " process profile";
126 }
127 } else {
128 PLOG(WARNING) << "Failed to find " << name << "process profile";
129 }
130 }
131
132 return true;
133}
134
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800135bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles, bool use_fd_cache) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800136 const TaskProfiles& tp = TaskProfiles::GetInstance();
137
138 for (const auto& name : profiles) {
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800139 TaskProfile* profile = tp.GetProfile(name);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800140 if (profile != nullptr) {
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800141 if (use_fd_cache) {
142 profile->EnableResourceCaching();
143 }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800144 if (!profile->ExecuteForTask(tid)) {
145 PLOG(WARNING) << "Failed to apply " << name << " task profile";
146 }
147 } else {
148 PLOG(WARNING) << "Failed to find " << name << "task profile";
149 }
150 }
151
152 return true;
153}
154
Peter Collingbourned7157c22018-10-30 15:49:33 -0700155static std::string ConvertUidToPath(const char* cgroup, uid_t uid) {
156 return StringPrintf("%s/uid_%d", cgroup, uid);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700157}
158
Peter Collingbourned7157c22018-10-30 15:49:33 -0700159static std::string ConvertUidPidToPath(const char* cgroup, uid_t uid, int pid) {
160 return StringPrintf("%s/uid_%d/pid_%d", cgroup, uid, pid);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700161}
162
Peter Collingbourned7157c22018-10-30 15:49:33 -0700163static int RemoveProcessGroup(const char* cgroup, uid_t uid, int pid) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700164 int ret;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700165
Peter Collingbourned7157c22018-10-30 15:49:33 -0700166 auto uid_pid_path = ConvertUidPidToPath(cgroup, uid, pid);
Tom Cherry574a0812018-02-23 13:04:40 -0800167 ret = rmdir(uid_pid_path.c_str());
Colin Crosscf8d1c22014-06-03 13:24:21 -0700168
Peter Collingbourned7157c22018-10-30 15:49:33 -0700169 auto uid_path = ConvertUidToPath(cgroup, uid);
Tom Cherry574a0812018-02-23 13:04:40 -0800170 rmdir(uid_path.c_str());
Colin Crosscf8d1c22014-06-03 13:24:21 -0700171
172 return ret;
173}
174
Wei Wang858f3e52019-02-21 11:25:16 -0800175static bool RemoveUidProcessGroups(const std::string& uid_path) {
Tom Cherry574a0812018-02-23 13:04:40 -0800176 std::unique_ptr<DIR, decltype(&closedir)> uid(opendir(uid_path.c_str()), closedir);
Wei Wang858f3e52019-02-21 11:25:16 -0800177 bool empty = true;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700178 if (uid != NULL) {
Elliott Hughes9f206932016-09-28 13:29:54 -0700179 dirent* dir;
180 while ((dir = readdir(uid.get())) != nullptr) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700181 if (dir->d_type != DT_DIR) {
182 continue;
183 }
184
Tom Cherry574a0812018-02-23 13:04:40 -0800185 if (!StartsWith(dir->d_name, "pid_")) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700186 continue;
187 }
188
Tom Cherry574a0812018-02-23 13:04:40 -0800189 auto path = StringPrintf("%s/%s", uid_path.c_str(), dir->d_name);
Tom Cherry20514c42017-04-21 13:48:49 -0700190 LOG(VERBOSE) << "Removing " << path;
Wei Wang858f3e52019-02-21 11:25:16 -0800191 if (rmdir(path.c_str()) == -1) {
192 if (errno != EBUSY) {
193 PLOG(WARNING) << "Failed to remove " << path;
194 }
195 empty = false;
196 }
Colin Crosscf8d1c22014-06-03 13:24:21 -0700197 }
198 }
Wei Wang858f3e52019-02-21 11:25:16 -0800199 return empty;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700200}
201
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800202void removeAllProcessGroups() {
Elliott Hughes171df0a2016-08-02 13:30:30 -0700203 LOG(VERBOSE) << "removeAllProcessGroups()";
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800204
205 std::vector<std::string> cgroups;
206 std::string path;
207
208 if (CgroupGetControllerPath("cpuacct", &path)) {
209 cgroups.push_back(path);
210 }
211 if (CgroupGetControllerPath("memory", &path)) {
Vic Yangab8d6ab2019-02-19 14:09:00 -0800212 cgroups.push_back(path + "/apps");
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800213 }
214
215 for (std::string cgroup_root_path : cgroups) {
216 std::unique_ptr<DIR, decltype(&closedir)> root(opendir(cgroup_root_path.c_str()), closedir);
Peter Collingbourned7157c22018-10-30 15:49:33 -0700217 if (root == NULL) {
218 PLOG(ERROR) << "Failed to open " << cgroup_root_path;
219 } else {
220 dirent* dir;
221 while ((dir = readdir(root.get())) != nullptr) {
222 if (dir->d_type != DT_DIR) {
223 continue;
224 }
Tom Cherry574a0812018-02-23 13:04:40 -0800225
Peter Collingbourned7157c22018-10-30 15:49:33 -0700226 if (!StartsWith(dir->d_name, "uid_")) {
227 continue;
228 }
Colin Crosscf8d1c22014-06-03 13:24:21 -0700229
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800230 auto path = StringPrintf("%s/%s", cgroup_root_path.c_str(), dir->d_name);
Wei Wang858f3e52019-02-21 11:25:16 -0800231 if (!RemoveUidProcessGroups(path)) {
232 LOG(VERBOSE) << "Skip removing " << path;
233 continue;
234 }
Peter Collingbourned7157c22018-10-30 15:49:33 -0700235 LOG(VERBOSE) << "Removing " << path;
Wei Wang858f3e52019-02-21 11:25:16 -0800236 if (rmdir(path.c_str()) == -1 && errno != EBUSY) {
237 PLOG(WARNING) << "Failed to remove " << path;
238 }
Peter Collingbourned7157c22018-10-30 15:49:33 -0700239 }
Colin Crosscf8d1c22014-06-03 13:24:21 -0700240 }
Colin Crosscf8d1c22014-06-03 13:24:21 -0700241 }
242}
243
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800244static bool MkdirAndChown(const std::string& path, mode_t mode, uid_t uid, gid_t gid) {
245 if (mkdir(path.c_str(), mode) == -1 && errno != EEXIST) {
246 return false;
247 }
248
249 if (chown(path.c_str(), uid, gid) == -1) {
250 int saved_errno = errno;
251 rmdir(path.c_str());
252 errno = saved_errno;
253 return false;
254 }
255
256 return true;
257}
258
Tom Cherry20514c42017-04-21 13:48:49 -0700259// Returns number of processes killed on success
260// Returns 0 if there are no processes in the process cgroup left to kill
Tom Cherry574a0812018-02-23 13:04:40 -0800261// Returns -1 on error
Peter Collingbourned7157c22018-10-30 15:49:33 -0700262static int DoKillProcessGroupOnce(const char* cgroup, uid_t uid, int initialPid, int signal) {
263 auto path = ConvertUidPidToPath(cgroup, uid, initialPid) + PROCESSGROUP_CGROUP_PROCS_FILE;
Tom Cherry574a0812018-02-23 13:04:40 -0800264 std::unique_ptr<FILE, decltype(&fclose)> fd(fopen(path.c_str(), "re"), fclose);
265 if (!fd) {
Wei Wang858f3e52019-02-21 11:25:16 -0800266 if (errno == ENOENT) {
267 // This happens when process is already dead
268 return 0;
269 }
Tom Cherry20514c42017-04-21 13:48:49 -0700270 PLOG(WARNING) << "Failed to open process cgroup uid " << uid << " pid " << initialPid;
Tom Cherry574a0812018-02-23 13:04:40 -0800271 return -1;
Tom Cherry20514c42017-04-21 13:48:49 -0700272 }
273
Tom Cherry70a5ed42017-06-05 19:20:17 -0700274 // We separate all of the pids in the cgroup into those pids that are also the leaders of
275 // process groups (stored in the pgids set) and those that are not (stored in the pids set).
276 std::set<pid_t> pgids;
277 pgids.emplace(initialPid);
278 std::set<pid_t> pids;
279
Colin Crosscf8d1c22014-06-03 13:24:21 -0700280 pid_t pid;
Tom Cherry20514c42017-04-21 13:48:49 -0700281 int processes = 0;
Tom Cherry574a0812018-02-23 13:04:40 -0800282 while (fscanf(fd.get(), "%d\n", &pid) == 1 && pid >= 0) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700283 processes++;
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700284 if (pid == 0) {
285 // Should never happen... but if it does, trying to kill this
286 // will boomerang right back and kill us! Let's not let that happen.
Elliott Hughes171df0a2016-08-02 13:30:30 -0700287 LOG(WARNING) << "Yikes, we've been told to kill pid 0! How about we don't do that?";
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700288 continue;
289 }
Tom Cherry70a5ed42017-06-05 19:20:17 -0700290 pid_t pgid = getpgid(pid);
291 if (pgid == -1) PLOG(ERROR) << "getpgid(" << pid << ") failed";
292 if (pgid == pid) {
293 pgids.emplace(pid);
294 } else {
295 pids.emplace(pid);
296 }
297 }
298
299 // Erase all pids that will be killed when we kill the process groups.
300 for (auto it = pids.begin(); it != pids.end();) {
fengshaobo950f6f32018-09-13 14:57:07 +0800301 pid_t pgid = getpgid(*it);
Tom Cherry70a5ed42017-06-05 19:20:17 -0700302 if (pgids.count(pgid) == 1) {
303 it = pids.erase(it);
304 } else {
305 ++it;
306 }
307 }
308
309 // Kill all process groups.
310 for (const auto pgid : pgids) {
311 LOG(VERBOSE) << "Killing process group " << -pgid << " in uid " << uid
312 << " as part of process cgroup " << initialPid;
313
Wei Wang858f3e52019-02-21 11:25:16 -0800314 if (kill(-pgid, signal) == -1 && errno != ESRCH) {
Tom Cherry70a5ed42017-06-05 19:20:17 -0700315 PLOG(WARNING) << "kill(" << -pgid << ", " << signal << ") failed";
316 }
317 }
318
319 // Kill remaining pids.
320 for (const auto pid : pids) {
Tom Cherry20514c42017-04-21 13:48:49 -0700321 LOG(VERBOSE) << "Killing pid " << pid << " in uid " << uid << " as part of process cgroup "
322 << initialPid;
Tom Cherry70a5ed42017-06-05 19:20:17 -0700323
Wei Wang858f3e52019-02-21 11:25:16 -0800324 if (kill(pid, signal) == -1 && errno != ESRCH) {
Elliott Hughes171df0a2016-08-02 13:30:30 -0700325 PLOG(WARNING) << "kill(" << pid << ", " << signal << ") failed";
Colin Crosscf8d1c22014-06-03 13:24:21 -0700326 }
327 }
328
Tom Cherry574a0812018-02-23 13:04:40 -0800329 return feof(fd.get()) ? processes : -1;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700330}
331
Tom Cherry574a0812018-02-23 13:04:40 -0800332static int KillProcessGroup(uid_t uid, int initialPid, int signal, int retries) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800333 std::string cpuacct_path;
334 std::string memory_path;
335
336 CgroupGetControllerPath("cpuacct", &cpuacct_path);
337 CgroupGetControllerPath("memory", &memory_path);
Vic Yangab8d6ab2019-02-19 14:09:00 -0800338 memory_path += "/apps";
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800339
Peter Collingbourned7157c22018-10-30 15:49:33 -0700340 const char* cgroup =
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800341 (!access(ConvertUidPidToPath(cpuacct_path.c_str(), uid, initialPid).c_str(), F_OK))
342 ? cpuacct_path.c_str()
343 : memory_path.c_str();
Peter Collingbourned7157c22018-10-30 15:49:33 -0700344
Collin Mullinerf7e79b92016-06-01 21:03:55 +0000345 std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
Colin Crosscf8d1c22014-06-03 13:24:21 -0700346
Tom Cherry20514c42017-04-21 13:48:49 -0700347 int retry = retries;
Collin Mullinerf7e79b92016-06-01 21:03:55 +0000348 int processes;
Peter Collingbourned7157c22018-10-30 15:49:33 -0700349 while ((processes = DoKillProcessGroupOnce(cgroup, uid, initialPid, signal)) > 0) {
Tom Cherry20514c42017-04-21 13:48:49 -0700350 LOG(VERBOSE) << "Killed " << processes << " processes for processgroup " << initialPid;
Yusuke Satod5039302015-06-16 13:51:14 -0700351 if (retry > 0) {
Elliott Hughes290a2282016-11-14 17:08:47 -0800352 std::this_thread::sleep_for(5ms);
Yusuke Satod5039302015-06-16 13:51:14 -0700353 --retry;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700354 } else {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700355 break;
356 }
357 }
358
Tom Cherry20514c42017-04-21 13:48:49 -0700359 if (processes < 0) {
360 PLOG(ERROR) << "Error encountered killing process cgroup uid " << uid << " pid "
361 << initialPid;
362 return -1;
363 }
Collin Mullinerf7e79b92016-06-01 21:03:55 +0000364
Tom Cherry20514c42017-04-21 13:48:49 -0700365 std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
Elliott Hughes171df0a2016-08-02 13:30:30 -0700366 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
Tom Cherry20514c42017-04-21 13:48:49 -0700367
368 // We only calculate the number of 'processes' when killing the processes.
369 // In the retries == 0 case, we only kill the processes once and therefore
370 // will not have waited then recalculated how many processes are remaining
371 // after the first signals have been sent.
372 // Logging anything regarding the number of 'processes' here does not make sense.
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700373
Colin Crosscf8d1c22014-06-03 13:24:21 -0700374 if (processes == 0) {
Tom Cherry20514c42017-04-21 13:48:49 -0700375 if (retries > 0) {
376 LOG(INFO) << "Successfully killed process cgroup uid " << uid << " pid " << initialPid
377 << " in " << static_cast<int>(ms) << "ms";
378 }
Peter Collingbourned7157c22018-10-30 15:49:33 -0700379 return RemoveProcessGroup(cgroup, uid, initialPid);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700380 } else {
Tom Cherry20514c42017-04-21 13:48:49 -0700381 if (retries > 0) {
382 LOG(ERROR) << "Failed to kill process cgroup uid " << uid << " pid " << initialPid
383 << " in " << static_cast<int>(ms) << "ms, " << processes
384 << " processes remain";
385 }
Colin Crosscf8d1c22014-06-03 13:24:21 -0700386 return -1;
387 }
388}
389
Keun-young Parkfac4b632017-03-28 17:25:24 -0700390int killProcessGroup(uid_t uid, int initialPid, int signal) {
Tom Cherry574a0812018-02-23 13:04:40 -0800391 return KillProcessGroup(uid, initialPid, signal, 40 /*retries*/);
Keun-young Parkfac4b632017-03-28 17:25:24 -0700392}
393
394int killProcessGroupOnce(uid_t uid, int initialPid, int signal) {
Tom Cherry574a0812018-02-23 13:04:40 -0800395 return KillProcessGroup(uid, initialPid, signal, 0 /*retries*/);
Keun-young Parkfac4b632017-03-28 17:25:24 -0700396}
397
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800398int createProcessGroup(uid_t uid, int initialPid, bool memControl) {
399 std::string cgroup;
400 if (isMemoryCgroupSupported() && (memControl || UsePerAppMemcg())) {
401 CgroupGetControllerPath("memory", &cgroup);
Vic Yangab8d6ab2019-02-19 14:09:00 -0800402 cgroup += "/apps";
Peter Collingbourned7157c22018-10-30 15:49:33 -0700403 } else {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800404 CgroupGetControllerPath("cpuacct", &cgroup);
Peter Collingbourned7157c22018-10-30 15:49:33 -0700405 }
406
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800407 auto uid_path = ConvertUidToPath(cgroup.c_str(), uid);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700408
Tom Cherry574a0812018-02-23 13:04:40 -0800409 if (!MkdirAndChown(uid_path, 0750, AID_SYSTEM, AID_SYSTEM)) {
410 PLOG(ERROR) << "Failed to make and chown " << uid_path;
Elliott Hughes171df0a2016-08-02 13:30:30 -0700411 return -errno;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700412 }
413
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800414 auto uid_pid_path = ConvertUidPidToPath(cgroup.c_str(), uid, initialPid);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700415
Tom Cherry574a0812018-02-23 13:04:40 -0800416 if (!MkdirAndChown(uid_pid_path, 0750, AID_SYSTEM, AID_SYSTEM)) {
417 PLOG(ERROR) << "Failed to make and chown " << uid_pid_path;
Elliott Hughes171df0a2016-08-02 13:30:30 -0700418 return -errno;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700419 }
420
Tom Cherry574a0812018-02-23 13:04:40 -0800421 auto uid_pid_procs_file = uid_pid_path + PROCESSGROUP_CGROUP_PROCS_FILE;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700422
Elliott Hughes171df0a2016-08-02 13:30:30 -0700423 int ret = 0;
Tom Cherry574a0812018-02-23 13:04:40 -0800424 if (!WriteStringToFile(std::to_string(initialPid), uid_pid_procs_file)) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700425 ret = -errno;
Tom Cherry574a0812018-02-23 13:04:40 -0800426 PLOG(ERROR) << "Failed to write '" << initialPid << "' to " << uid_pid_procs_file;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700427 }
428
Colin Crosscf8d1c22014-06-03 13:24:21 -0700429 return ret;
430}
Robert Benead4852262017-07-16 19:38:11 -0700431
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800432static bool SetProcessGroupValue(int tid, const std::string& attr_name, int64_t value) {
Peter Collingbourned7157c22018-10-30 15:49:33 -0700433 if (!isMemoryCgroupSupported()) {
Tom Cherry574a0812018-02-23 13:04:40 -0800434 PLOG(ERROR) << "Memcg is not mounted.";
Robert Benead4852262017-07-16 19:38:11 -0700435 return false;
436 }
437
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800438 std::string path;
439 if (!CgroupGetAttributePathForTask(attr_name, tid, &path)) {
440 PLOG(ERROR) << "Failed to find attribute '" << attr_name << "'";
441 return false;
442 }
Robert Benead4852262017-07-16 19:38:11 -0700443
444 if (!WriteStringToFile(std::to_string(value), path)) {
445 PLOG(ERROR) << "Failed to write '" << value << "' to " << path;
446 return false;
447 }
448 return true;
449}
450
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800451bool setProcessGroupSwappiness(uid_t, int pid, int swappiness) {
452 return SetProcessGroupValue(pid, "MemSwappiness", swappiness);
Robert Benead4852262017-07-16 19:38:11 -0700453}
454
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800455bool setProcessGroupSoftLimit(uid_t, int pid, int64_t soft_limit_in_bytes) {
456 return SetProcessGroupValue(pid, "MemSoftLimit", soft_limit_in_bytes);
Robert Benead4852262017-07-16 19:38:11 -0700457}
458
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800459bool setProcessGroupLimit(uid_t, int pid, int64_t limit_in_bytes) {
460 return SetProcessGroupValue(pid, "MemLimit", limit_in_bytes);
Robert Benead4852262017-07-16 19:38:11 -0700461}