blob: 0320b0263ad883cc547da4a2d099a92f5025d9a9 [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
Suren Baghdasaryan9e3ace52021-06-16 17:01:19 -070072bool CgroupGetControllerFromPath(const std::string& path, std::string* cgroup_name) {
73 auto controller = CgroupMap::GetInstance().FindControllerByPath(path);
74
75 if (!controller.HasValue()) {
76 return false;
77 }
78
79 if (cgroup_name) {
80 *cgroup_name = controller.name();
81 }
82
83 return true;
84}
85
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080086bool CgroupGetAttributePath(const std::string& attr_name, std::string* path) {
87 const TaskProfiles& tp = TaskProfiles::GetInstance();
88 const ProfileAttribute* attr = tp.GetAttribute(attr_name);
89
90 if (attr == nullptr) {
91 return false;
92 }
93
94 if (path) {
95 *path = StringPrintf("%s/%s", attr->controller()->path(), attr->file_name().c_str());
96 }
97
98 return true;
99}
100
101bool CgroupGetAttributePathForTask(const std::string& attr_name, int tid, std::string* path) {
102 const TaskProfiles& tp = TaskProfiles::GetInstance();
103 const ProfileAttribute* attr = tp.GetAttribute(attr_name);
104
105 if (attr == nullptr) {
106 return false;
107 }
108
109 if (!attr->GetPathForTask(tid, path)) {
110 PLOG(ERROR) << "Failed to find cgroup for tid " << tid;
111 return false;
112 }
113
114 return true;
115}
116
117bool UsePerAppMemcg() {
118 bool low_ram_device = GetBoolProperty("ro.config.low_ram", false);
119 return GetBoolProperty("ro.config.per_app_memcg", low_ram_device);
120}
121
Peter Collingbourned7157c22018-10-30 15:49:33 -0700122static bool isMemoryCgroupSupported() {
Suren Baghdasaryanfa7a05f2019-05-08 17:59:55 -0700123 static bool memcg_supported = CgroupMap::GetInstance().FindController("memory").IsUsable();
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800124
Peter Collingbourned7157c22018-10-30 15:49:33 -0700125 return memcg_supported;
Martijn Coenenb82bab62016-01-20 16:39:16 -0800126}
127
Riddle Hsua6abd822019-06-18 15:53:53 -0600128void DropTaskProfilesResourceCaching() {
129 TaskProfiles::GetInstance().DropResourceCaching();
130}
131
Suren Baghdasaryan911109c2020-02-13 17:28:00 -0800132bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles) {
133 return TaskProfiles::GetInstance().SetProcessProfiles(uid, pid, profiles);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800134}
135
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800136bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles, bool use_fd_cache) {
Rick Yiu0b211fa2019-09-16 19:07:17 +0800137 return TaskProfiles::GetInstance().SetTaskProfiles(tid, profiles, use_fd_cache);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800138}
139
Peter Collingbourned7157c22018-10-30 15:49:33 -0700140static std::string ConvertUidToPath(const char* cgroup, uid_t uid) {
141 return StringPrintf("%s/uid_%d", cgroup, uid);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700142}
143
Peter Collingbourned7157c22018-10-30 15:49:33 -0700144static std::string ConvertUidPidToPath(const char* cgroup, uid_t uid, int pid) {
145 return StringPrintf("%s/uid_%d/pid_%d", cgroup, uid, pid);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700146}
147
Marco Ballesio4dac8162021-02-10 16:35:44 -0800148static int RemoveProcessGroup(const char* cgroup, uid_t uid, int pid, unsigned int retries) {
149 int ret = 0;
Peter Collingbourned7157c22018-10-30 15:49:33 -0700150 auto uid_pid_path = ConvertUidPidToPath(cgroup, uid, pid);
Peter Collingbourned7157c22018-10-30 15:49:33 -0700151 auto uid_path = ConvertUidToPath(cgroup, uid);
Marco Ballesio4dac8162021-02-10 16:35:44 -0800152
153 if (retries == 0) {
154 retries = 1;
155 }
156
157 while (retries--) {
158 ret = rmdir(uid_pid_path.c_str());
159 if (!ret || errno != EBUSY) break;
160 std::this_thread::sleep_for(5ms);
161 }
162
Colin Crosscf8d1c22014-06-03 13:24:21 -0700163 return ret;
164}
165
Wei Wang858f3e52019-02-21 11:25:16 -0800166static bool RemoveUidProcessGroups(const std::string& uid_path) {
Tom Cherry574a0812018-02-23 13:04:40 -0800167 std::unique_ptr<DIR, decltype(&closedir)> uid(opendir(uid_path.c_str()), closedir);
Wei Wang858f3e52019-02-21 11:25:16 -0800168 bool empty = true;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700169 if (uid != NULL) {
Elliott Hughes9f206932016-09-28 13:29:54 -0700170 dirent* dir;
171 while ((dir = readdir(uid.get())) != nullptr) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700172 if (dir->d_type != DT_DIR) {
173 continue;
174 }
175
Tom Cherry574a0812018-02-23 13:04:40 -0800176 if (!StartsWith(dir->d_name, "pid_")) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700177 continue;
178 }
179
Tom Cherry574a0812018-02-23 13:04:40 -0800180 auto path = StringPrintf("%s/%s", uid_path.c_str(), dir->d_name);
Tom Cherry20514c42017-04-21 13:48:49 -0700181 LOG(VERBOSE) << "Removing " << path;
Wei Wang858f3e52019-02-21 11:25:16 -0800182 if (rmdir(path.c_str()) == -1) {
183 if (errno != EBUSY) {
184 PLOG(WARNING) << "Failed to remove " << path;
185 }
186 empty = false;
187 }
Colin Crosscf8d1c22014-06-03 13:24:21 -0700188 }
189 }
Wei Wang858f3e52019-02-21 11:25:16 -0800190 return empty;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700191}
192
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800193void removeAllProcessGroups() {
Elliott Hughes171df0a2016-08-02 13:30:30 -0700194 LOG(VERBOSE) << "removeAllProcessGroups()";
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800195
196 std::vector<std::string> cgroups;
197 std::string path;
198
Marco Ballesio4dac8162021-02-10 16:35:44 -0800199 if (CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &path)) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800200 cgroups.push_back(path);
201 }
202 if (CgroupGetControllerPath("memory", &path)) {
Vic Yangab8d6ab2019-02-19 14:09:00 -0800203 cgroups.push_back(path + "/apps");
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800204 }
205
206 for (std::string cgroup_root_path : cgroups) {
207 std::unique_ptr<DIR, decltype(&closedir)> root(opendir(cgroup_root_path.c_str()), closedir);
Peter Collingbourned7157c22018-10-30 15:49:33 -0700208 if (root == NULL) {
209 PLOG(ERROR) << "Failed to open " << cgroup_root_path;
210 } else {
211 dirent* dir;
212 while ((dir = readdir(root.get())) != nullptr) {
213 if (dir->d_type != DT_DIR) {
214 continue;
215 }
Tom Cherry574a0812018-02-23 13:04:40 -0800216
Peter Collingbourned7157c22018-10-30 15:49:33 -0700217 if (!StartsWith(dir->d_name, "uid_")) {
218 continue;
219 }
Colin Crosscf8d1c22014-06-03 13:24:21 -0700220
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800221 auto path = StringPrintf("%s/%s", cgroup_root_path.c_str(), dir->d_name);
Wei Wang858f3e52019-02-21 11:25:16 -0800222 if (!RemoveUidProcessGroups(path)) {
223 LOG(VERBOSE) << "Skip removing " << path;
224 continue;
225 }
Peter Collingbourned7157c22018-10-30 15:49:33 -0700226 LOG(VERBOSE) << "Removing " << path;
Wei Wang858f3e52019-02-21 11:25:16 -0800227 if (rmdir(path.c_str()) == -1 && errno != EBUSY) {
228 PLOG(WARNING) << "Failed to remove " << path;
229 }
Peter Collingbourned7157c22018-10-30 15:49:33 -0700230 }
Colin Crosscf8d1c22014-06-03 13:24:21 -0700231 }
Colin Crosscf8d1c22014-06-03 13:24:21 -0700232 }
233}
234
Marco Ballesio4dac8162021-02-10 16:35:44 -0800235/**
236 * Process groups are primarily created by the Zygote, meaning that uid/pid groups are created by
237 * the user root. Ownership for the newly created cgroup and all of its files must thus be
238 * transferred for the user/group passed as uid/gid before system_server can properly access them.
239 */
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800240static bool MkdirAndChown(const std::string& path, mode_t mode, uid_t uid, gid_t gid) {
Suren Baghdasaryan29c9e262021-07-07 10:59:59 -0700241 if (mkdir(path.c_str(), mode) == -1) {
242 if (errno == EEXIST) {
243 // Directory already exists and permissions have been set at the time it was created
244 return true;
245 }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800246 return false;
247 }
248
Marco Ballesio4dac8162021-02-10 16:35:44 -0800249 auto dir = std::unique_ptr<DIR, decltype(&closedir)>(opendir(path.c_str()), closedir);
250
251 if (dir == NULL) {
252 PLOG(ERROR) << "opendir failed for " << path;
253 goto err;
254 }
255
256 struct dirent* dir_entry;
257 while ((dir_entry = readdir(dir.get()))) {
258 if (!strcmp("..", dir_entry->d_name)) {
259 continue;
260 }
261
262 std::string file_path = path + "/" + dir_entry->d_name;
263
264 if (lchown(file_path.c_str(), uid, gid) < 0) {
265 PLOG(ERROR) << "lchown failed for " << file_path;
266 goto err;
267 }
268
269 if (fchmodat(AT_FDCWD, file_path.c_str(), mode, AT_SYMLINK_NOFOLLOW) != 0) {
270 PLOG(ERROR) << "fchmodat failed for " << file_path;
271 goto err;
272 }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800273 }
274
275 return true;
Marco Ballesio4dac8162021-02-10 16:35:44 -0800276err:
277 int saved_errno = errno;
278 rmdir(path.c_str());
279 errno = saved_errno;
280
281 return false;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800282}
283
Tom Cherry20514c42017-04-21 13:48:49 -0700284// Returns number of processes killed on success
285// Returns 0 if there are no processes in the process cgroup left to kill
Tom Cherry574a0812018-02-23 13:04:40 -0800286// Returns -1 on error
Peter Collingbourned7157c22018-10-30 15:49:33 -0700287static int DoKillProcessGroupOnce(const char* cgroup, uid_t uid, int initialPid, int signal) {
288 auto path = ConvertUidPidToPath(cgroup, uid, initialPid) + PROCESSGROUP_CGROUP_PROCS_FILE;
Tom Cherry574a0812018-02-23 13:04:40 -0800289 std::unique_ptr<FILE, decltype(&fclose)> fd(fopen(path.c_str(), "re"), fclose);
290 if (!fd) {
Wei Wang858f3e52019-02-21 11:25:16 -0800291 if (errno == ENOENT) {
292 // This happens when process is already dead
293 return 0;
294 }
Tom Cherry20514c42017-04-21 13:48:49 -0700295 PLOG(WARNING) << "Failed to open process cgroup uid " << uid << " pid " << initialPid;
Tom Cherry574a0812018-02-23 13:04:40 -0800296 return -1;
Tom Cherry20514c42017-04-21 13:48:49 -0700297 }
298
Tom Cherry70a5ed42017-06-05 19:20:17 -0700299 // We separate all of the pids in the cgroup into those pids that are also the leaders of
300 // process groups (stored in the pgids set) and those that are not (stored in the pids set).
301 std::set<pid_t> pgids;
302 pgids.emplace(initialPid);
303 std::set<pid_t> pids;
304
Colin Crosscf8d1c22014-06-03 13:24:21 -0700305 pid_t pid;
Tom Cherry20514c42017-04-21 13:48:49 -0700306 int processes = 0;
Tom Cherry574a0812018-02-23 13:04:40 -0800307 while (fscanf(fd.get(), "%d\n", &pid) == 1 && pid >= 0) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700308 processes++;
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700309 if (pid == 0) {
310 // Should never happen... but if it does, trying to kill this
311 // will boomerang right back and kill us! Let's not let that happen.
Elliott Hughes171df0a2016-08-02 13:30:30 -0700312 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 -0700313 continue;
314 }
Tom Cherry70a5ed42017-06-05 19:20:17 -0700315 pid_t pgid = getpgid(pid);
316 if (pgid == -1) PLOG(ERROR) << "getpgid(" << pid << ") failed";
317 if (pgid == pid) {
318 pgids.emplace(pid);
319 } else {
320 pids.emplace(pid);
321 }
322 }
323
324 // Erase all pids that will be killed when we kill the process groups.
325 for (auto it = pids.begin(); it != pids.end();) {
fengshaobo950f6f32018-09-13 14:57:07 +0800326 pid_t pgid = getpgid(*it);
Tom Cherry70a5ed42017-06-05 19:20:17 -0700327 if (pgids.count(pgid) == 1) {
328 it = pids.erase(it);
329 } else {
330 ++it;
331 }
332 }
333
334 // Kill all process groups.
335 for (const auto pgid : pgids) {
336 LOG(VERBOSE) << "Killing process group " << -pgid << " in uid " << uid
337 << " as part of process cgroup " << initialPid;
338
Wei Wang858f3e52019-02-21 11:25:16 -0800339 if (kill(-pgid, signal) == -1 && errno != ESRCH) {
Tom Cherry70a5ed42017-06-05 19:20:17 -0700340 PLOG(WARNING) << "kill(" << -pgid << ", " << signal << ") failed";
341 }
342 }
343
344 // Kill remaining pids.
345 for (const auto pid : pids) {
Tom Cherry20514c42017-04-21 13:48:49 -0700346 LOG(VERBOSE) << "Killing pid " << pid << " in uid " << uid << " as part of process cgroup "
347 << initialPid;
Tom Cherry70a5ed42017-06-05 19:20:17 -0700348
Wei Wang858f3e52019-02-21 11:25:16 -0800349 if (kill(pid, signal) == -1 && errno != ESRCH) {
Elliott Hughes171df0a2016-08-02 13:30:30 -0700350 PLOG(WARNING) << "kill(" << pid << ", " << signal << ") failed";
Colin Crosscf8d1c22014-06-03 13:24:21 -0700351 }
352 }
353
Tom Cherry574a0812018-02-23 13:04:40 -0800354 return feof(fd.get()) ? processes : -1;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700355}
356
Tom Cherryd89ed132019-11-19 14:19:40 -0800357static int KillProcessGroup(uid_t uid, int initialPid, int signal, int retries,
358 int* max_processes) {
Marco Ballesio4dac8162021-02-10 16:35:44 -0800359 std::string hierarchy_root_path;
360 CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &hierarchy_root_path);
361 const char* cgroup = hierarchy_root_path.c_str();
Peter Collingbourned7157c22018-10-30 15:49:33 -0700362
Collin Mullinerf7e79b92016-06-01 21:03:55 +0000363 std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
Colin Crosscf8d1c22014-06-03 13:24:21 -0700364
Tom Cherryd89ed132019-11-19 14:19:40 -0800365 if (max_processes != nullptr) {
366 *max_processes = 0;
367 }
368
Tom Cherry20514c42017-04-21 13:48:49 -0700369 int retry = retries;
Collin Mullinerf7e79b92016-06-01 21:03:55 +0000370 int processes;
Peter Collingbourned7157c22018-10-30 15:49:33 -0700371 while ((processes = DoKillProcessGroupOnce(cgroup, uid, initialPid, signal)) > 0) {
Tom Cherryd89ed132019-11-19 14:19:40 -0800372 if (max_processes != nullptr && processes > *max_processes) {
373 *max_processes = processes;
374 }
Tom Cherry20514c42017-04-21 13:48:49 -0700375 LOG(VERBOSE) << "Killed " << processes << " processes for processgroup " << initialPid;
Yusuke Satod5039302015-06-16 13:51:14 -0700376 if (retry > 0) {
Elliott Hughes290a2282016-11-14 17:08:47 -0800377 std::this_thread::sleep_for(5ms);
Yusuke Satod5039302015-06-16 13:51:14 -0700378 --retry;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700379 } else {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700380 break;
381 }
382 }
383
Tom Cherry20514c42017-04-21 13:48:49 -0700384 if (processes < 0) {
385 PLOG(ERROR) << "Error encountered killing process cgroup uid " << uid << " pid "
386 << initialPid;
387 return -1;
388 }
Collin Mullinerf7e79b92016-06-01 21:03:55 +0000389
Tom Cherry20514c42017-04-21 13:48:49 -0700390 std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
Elliott Hughes171df0a2016-08-02 13:30:30 -0700391 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
Tom Cherry20514c42017-04-21 13:48:49 -0700392
393 // We only calculate the number of 'processes' when killing the processes.
394 // In the retries == 0 case, we only kill the processes once and therefore
395 // will not have waited then recalculated how many processes are remaining
396 // after the first signals have been sent.
397 // Logging anything regarding the number of 'processes' here does not make sense.
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700398
Colin Crosscf8d1c22014-06-03 13:24:21 -0700399 if (processes == 0) {
Tom Cherry20514c42017-04-21 13:48:49 -0700400 if (retries > 0) {
401 LOG(INFO) << "Successfully killed process cgroup uid " << uid << " pid " << initialPid
402 << " in " << static_cast<int>(ms) << "ms";
403 }
Marco Ballesio4dac8162021-02-10 16:35:44 -0800404
405 int err = RemoveProcessGroup(cgroup, uid, initialPid, retries);
406
407 if (isMemoryCgroupSupported() && UsePerAppMemcg()) {
408 std::string memory_path;
409 CgroupGetControllerPath("memory", &memory_path);
410 memory_path += "/apps";
411 if (RemoveProcessGroup(memory_path.c_str(), uid, initialPid, retries)) return -1;
412 }
413
414 return err;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700415 } else {
Tom Cherry20514c42017-04-21 13:48:49 -0700416 if (retries > 0) {
417 LOG(ERROR) << "Failed to kill process cgroup uid " << uid << " pid " << initialPid
418 << " in " << static_cast<int>(ms) << "ms, " << processes
419 << " processes remain";
420 }
Colin Crosscf8d1c22014-06-03 13:24:21 -0700421 return -1;
422 }
423}
424
Tom Cherryd89ed132019-11-19 14:19:40 -0800425int killProcessGroup(uid_t uid, int initialPid, int signal, int* max_processes) {
426 return KillProcessGroup(uid, initialPid, signal, 40 /*retries*/, max_processes);
Keun-young Parkfac4b632017-03-28 17:25:24 -0700427}
428
Tom Cherryd89ed132019-11-19 14:19:40 -0800429int killProcessGroupOnce(uid_t uid, int initialPid, int signal, int* max_processes) {
430 return KillProcessGroup(uid, initialPid, signal, 0 /*retries*/, max_processes);
Keun-young Parkfac4b632017-03-28 17:25:24 -0700431}
432
Suren Baghdasaryan25ad3f92021-07-30 13:42:39 -0700433static int createProcessGroupInternal(uid_t uid, int initialPid, std::string cgroup,
434 bool activate_controllers) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800435 auto uid_path = ConvertUidToPath(cgroup.c_str(), uid);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700436
Marco Ballesio9e628a62021-02-11 14:44:53 -0800437 struct stat cgroup_stat;
438 mode_t cgroup_mode = 0750;
439 gid_t cgroup_uid = AID_SYSTEM;
440 uid_t cgroup_gid = AID_SYSTEM;
Suren Baghdasaryan25ad3f92021-07-30 13:42:39 -0700441 int ret = 0;
Marco Ballesio9e628a62021-02-11 14:44:53 -0800442
443 if (stat(cgroup.c_str(), &cgroup_stat) == 1) {
444 PLOG(ERROR) << "Failed to get stats for " << cgroup;
445 } else {
446 cgroup_mode = cgroup_stat.st_mode;
447 cgroup_uid = cgroup_stat.st_uid;
448 cgroup_gid = cgroup_stat.st_gid;
449 }
450
451 if (!MkdirAndChown(uid_path, cgroup_mode, cgroup_uid, cgroup_gid)) {
Tom Cherry574a0812018-02-23 13:04:40 -0800452 PLOG(ERROR) << "Failed to make and chown " << uid_path;
Elliott Hughes171df0a2016-08-02 13:30:30 -0700453 return -errno;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700454 }
Suren Baghdasaryan25ad3f92021-07-30 13:42:39 -0700455 if (activate_controllers) {
456 ret = CgroupMap::GetInstance().ActivateControllers(uid_path);
457 if (ret) {
458 LOG(ERROR) << "Failed to activate controllers in " << uid_path;
459 return ret;
460 }
461 }
Colin Crosscf8d1c22014-06-03 13:24:21 -0700462
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800463 auto uid_pid_path = ConvertUidPidToPath(cgroup.c_str(), uid, initialPid);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700464
Marco Ballesio9e628a62021-02-11 14:44:53 -0800465 if (!MkdirAndChown(uid_pid_path, cgroup_mode, cgroup_uid, cgroup_gid)) {
Tom Cherry574a0812018-02-23 13:04:40 -0800466 PLOG(ERROR) << "Failed to make and chown " << uid_pid_path;
Elliott Hughes171df0a2016-08-02 13:30:30 -0700467 return -errno;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700468 }
469
Tom Cherry574a0812018-02-23 13:04:40 -0800470 auto uid_pid_procs_file = uid_pid_path + PROCESSGROUP_CGROUP_PROCS_FILE;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700471
Tom Cherry574a0812018-02-23 13:04:40 -0800472 if (!WriteStringToFile(std::to_string(initialPid), uid_pid_procs_file)) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700473 ret = -errno;
Tom Cherry574a0812018-02-23 13:04:40 -0800474 PLOG(ERROR) << "Failed to write '" << initialPid << "' to " << uid_pid_procs_file;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700475 }
476
Colin Crosscf8d1c22014-06-03 13:24:21 -0700477 return ret;
478}
Robert Benead4852262017-07-16 19:38:11 -0700479
Marco Ballesio4dac8162021-02-10 16:35:44 -0800480int createProcessGroup(uid_t uid, int initialPid, bool memControl) {
481 std::string cgroup;
482
483 if (memControl && !UsePerAppMemcg()) {
484 PLOG(ERROR) << "service memory controls are used without per-process memory cgroup support";
485 return -EINVAL;
486 }
487
488 if (isMemoryCgroupSupported() && UsePerAppMemcg()) {
489 CgroupGetControllerPath("memory", &cgroup);
490 cgroup += "/apps";
Suren Baghdasaryan25ad3f92021-07-30 13:42:39 -0700491 int ret = createProcessGroupInternal(uid, initialPid, cgroup, false);
Marco Ballesio4dac8162021-02-10 16:35:44 -0800492 if (ret != 0) {
493 return ret;
494 }
495 }
496
497 CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &cgroup);
Suren Baghdasaryan25ad3f92021-07-30 13:42:39 -0700498 return createProcessGroupInternal(uid, initialPid, cgroup, true);
Marco Ballesio4dac8162021-02-10 16:35:44 -0800499}
500
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800501static bool SetProcessGroupValue(int tid, const std::string& attr_name, int64_t value) {
Peter Collingbourned7157c22018-10-30 15:49:33 -0700502 if (!isMemoryCgroupSupported()) {
Tom Cherry574a0812018-02-23 13:04:40 -0800503 PLOG(ERROR) << "Memcg is not mounted.";
Robert Benead4852262017-07-16 19:38:11 -0700504 return false;
505 }
506
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800507 std::string path;
508 if (!CgroupGetAttributePathForTask(attr_name, tid, &path)) {
509 PLOG(ERROR) << "Failed to find attribute '" << attr_name << "'";
510 return false;
511 }
Robert Benead4852262017-07-16 19:38:11 -0700512
513 if (!WriteStringToFile(std::to_string(value), path)) {
514 PLOG(ERROR) << "Failed to write '" << value << "' to " << path;
515 return false;
516 }
517 return true;
518}
519
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800520bool setProcessGroupSwappiness(uid_t, int pid, int swappiness) {
521 return SetProcessGroupValue(pid, "MemSwappiness", swappiness);
Robert Benead4852262017-07-16 19:38:11 -0700522}
523
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800524bool setProcessGroupSoftLimit(uid_t, int pid, int64_t soft_limit_in_bytes) {
525 return SetProcessGroupValue(pid, "MemSoftLimit", soft_limit_in_bytes);
Robert Benead4852262017-07-16 19:38:11 -0700526}
527
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800528bool setProcessGroupLimit(uid_t, int pid, int64_t limit_in_bytes) {
529 return SetProcessGroupValue(pid, "MemLimit", limit_in_bytes);
Robert Benead4852262017-07-16 19:38:11 -0700530}
Marco Ballesio4e644c42021-02-24 16:30:50 -0800531
532bool getAttributePathForTask(const std::string& attr_name, int tid, std::string* path) {
533 return CgroupGetAttributePathForTask(attr_name, tid, path);
534}