blob: 8526b3a29ca3906647d1ca26ff04dac13731b4bc [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>
Colin Crosscf8d1c22014-06-03 13:24:21 -070025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#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>
James Hawkins588a2ca2016-02-18 14:52:46 -080033#include <memory>
Elliott Hughes8d532e42016-06-06 21:19:55 -070034#include <mutex>
Tom Cherry70a5ed42017-06-05 19:20:17 -070035#include <set>
Elliott Hughes290a2282016-11-14 17:08:47 -080036#include <thread>
Colin Crosscf8d1c22014-06-03 13:24:21 -070037
Robert Benead4852262017-07-16 19:38:11 -070038#include <android-base/file.h>
Elliott Hughes171df0a2016-08-02 13:30:30 -070039#include <android-base/logging.h>
Tom Cherry20514c42017-04-21 13:48:49 -070040#include <android-base/unique_fd.h>
Colin Crosscf8d1c22014-06-03 13:24:21 -070041#include <private/android_filesystem_config.h>
42
43#include <processgroup/processgroup.h>
Martijn Coenenb82bab62016-01-20 16:39:16 -080044
Robert Benead4852262017-07-16 19:38:11 -070045using android::base::WriteStringToFile;
46
Elliott Hughes290a2282016-11-14 17:08:47 -080047using namespace std::chrono_literals;
48
Martijn Coenenb82bab62016-01-20 16:39:16 -080049#define MEM_CGROUP_PATH "/dev/memcg/apps"
Martijn Coenen623b56a2016-02-08 11:42:25 +010050#define MEM_CGROUP_TASKS "/dev/memcg/apps/tasks"
Martijn Coenenb82bab62016-01-20 16:39:16 -080051#define ACCT_CGROUP_PATH "/acct"
52
53#define PROCESSGROUP_UID_PREFIX "uid_"
54#define PROCESSGROUP_PID_PREFIX "pid_"
55#define PROCESSGROUP_CGROUP_PROCS_FILE "/cgroup.procs"
56#define PROCESSGROUP_MAX_UID_LEN 11
57#define PROCESSGROUP_MAX_PID_LEN 11
58#define PROCESSGROUP_MAX_PATH_LEN \
59 ((sizeof(MEM_CGROUP_PATH) > sizeof(ACCT_CGROUP_PATH) ? \
60 sizeof(MEM_CGROUP_PATH) : sizeof(ACCT_CGROUP_PATH)) + \
61 sizeof(PROCESSGROUP_UID_PREFIX) + 1 + \
62 PROCESSGROUP_MAX_UID_LEN + \
63 sizeof(PROCESSGROUP_PID_PREFIX) + 1 + \
64 PROCESSGROUP_MAX_PID_LEN + \
65 sizeof(PROCESSGROUP_CGROUP_PROCS_FILE) + \
66 1)
67
68std::once_flag init_path_flag;
Colin Crosscf8d1c22014-06-03 13:24:21 -070069
Tom Cherry20514c42017-04-21 13:48:49 -070070class ProcessGroup {
71 public:
72 ProcessGroup() : buf_ptr_(buf_), buf_len_(0) {}
73
74 bool Open(uid_t uid, int pid);
75
76 // Return positive number and sets *pid = next pid in process cgroup on success
77 // Returns 0 if there are no pids left in the process cgroup
78 // Returns -errno if an error was encountered
79 int GetOneAppProcess(pid_t* pid);
80
81 private:
82 // Returns positive number of bytes filled on success
83 // Returns 0 if there was nothing to read
84 // Returns -errno if an error was encountered
85 int RefillBuffer();
86
87 android::base::unique_fd fd_;
88 char buf_[128];
89 char* buf_ptr_;
90 size_t buf_len_;
Colin Crosscf8d1c22014-06-03 13:24:21 -070091};
92
Martijn Coenenb82bab62016-01-20 16:39:16 -080093static const char* getCgroupRootPath() {
94 static const char* cgroup_root_path = NULL;
95 std::call_once(init_path_flag, [&]() {
Martijn Coenen623b56a2016-02-08 11:42:25 +010096 // Check if mem cgroup is mounted, only then check for write-access to avoid
97 // SELinux denials
98 cgroup_root_path = access(MEM_CGROUP_TASKS, F_OK) || access(MEM_CGROUP_PATH, W_OK) ?
99 ACCT_CGROUP_PATH : MEM_CGROUP_PATH;
Martijn Coenenb82bab62016-01-20 16:39:16 -0800100 });
101 return cgroup_root_path;
102}
103
Colin Crosscf8d1c22014-06-03 13:24:21 -0700104static int convertUidToPath(char *path, size_t size, uid_t uid)
105{
106 return snprintf(path, size, "%s/%s%d",
Martijn Coenenb82bab62016-01-20 16:39:16 -0800107 getCgroupRootPath(),
Colin Crosscf8d1c22014-06-03 13:24:21 -0700108 PROCESSGROUP_UID_PREFIX,
109 uid);
110}
111
112static int convertUidPidToPath(char *path, size_t size, uid_t uid, int pid)
113{
114 return snprintf(path, size, "%s/%s%d/%s%d",
Martijn Coenenb82bab62016-01-20 16:39:16 -0800115 getCgroupRootPath(),
Colin Crosscf8d1c22014-06-03 13:24:21 -0700116 PROCESSGROUP_UID_PREFIX,
117 uid,
118 PROCESSGROUP_PID_PREFIX,
119 pid);
120}
121
Tom Cherry20514c42017-04-21 13:48:49 -0700122bool ProcessGroup::Open(uid_t uid, int pid) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700123 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
124 convertUidPidToPath(path, sizeof(path), uid, pid);
125 strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path));
126
127 int fd = open(path, O_RDONLY);
Tom Cherry20514c42017-04-21 13:48:49 -0700128 if (fd < 0) return false;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700129
Tom Cherry20514c42017-04-21 13:48:49 -0700130 fd_.reset(fd);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700131
Elliott Hughes171df0a2016-08-02 13:30:30 -0700132 LOG(VERBOSE) << "Initialized context for " << path;
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700133
Tom Cherry20514c42017-04-21 13:48:49 -0700134 return true;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700135}
136
Tom Cherry20514c42017-04-21 13:48:49 -0700137int ProcessGroup::RefillBuffer() {
138 memmove(buf_, buf_ptr_, buf_len_);
139 buf_ptr_ = buf_;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700140
Tom Cherry20514c42017-04-21 13:48:49 -0700141 ssize_t ret = read(fd_, buf_ptr_ + buf_len_, sizeof(buf_) - buf_len_ - 1);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700142 if (ret < 0) {
143 return -errno;
144 } else if (ret == 0) {
145 return 0;
146 }
147
Tom Cherry20514c42017-04-21 13:48:49 -0700148 buf_len_ += ret;
149 buf_[buf_len_] = 0;
150 LOG(VERBOSE) << "Read " << ret << " to buffer: " << buf_;
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700151
Tom Cherry20514c42017-04-21 13:48:49 -0700152 assert(buf_len_ <= sizeof(buf_));
Colin Crosscf8d1c22014-06-03 13:24:21 -0700153
154 return ret;
155}
156
Tom Cherry20514c42017-04-21 13:48:49 -0700157int ProcessGroup::GetOneAppProcess(pid_t* out_pid) {
158 *out_pid = 0;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700159
Tom Cherry20514c42017-04-21 13:48:49 -0700160 char* eptr;
161 while ((eptr = static_cast<char*>(memchr(buf_ptr_, '\n', buf_len_))) == nullptr) {
162 int ret = RefillBuffer();
163 if (ret <= 0) return ret;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700164 }
165
166 *eptr = '\0';
Tom Cherry20514c42017-04-21 13:48:49 -0700167 char* pid_eptr = nullptr;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700168 errno = 0;
Tom Cherry20514c42017-04-21 13:48:49 -0700169 long pid = strtol(buf_ptr_, &pid_eptr, 10);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700170 if (errno != 0) {
171 return -errno;
172 }
173 if (pid_eptr != eptr) {
Tom Cherry20514c42017-04-21 13:48:49 -0700174 errno = EINVAL;
175 return -errno;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700176 }
177
Tom Cherry20514c42017-04-21 13:48:49 -0700178 buf_len_ -= (eptr - buf_ptr_) + 1;
179 buf_ptr_ = eptr + 1;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700180
Tom Cherry20514c42017-04-21 13:48:49 -0700181 *out_pid = static_cast<pid_t>(pid);
182 return 1;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700183}
184
185static int removeProcessGroup(uid_t uid, int pid)
186{
187 int ret;
188 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
189
190 convertUidPidToPath(path, sizeof(path), uid, pid);
191 ret = rmdir(path);
192
193 convertUidToPath(path, sizeof(path), uid);
194 rmdir(path);
195
196 return ret;
197}
198
199static void removeUidProcessGroups(const char *uid_path)
200{
James Hawkins588a2ca2016-02-18 14:52:46 -0800201 std::unique_ptr<DIR, decltype(&closedir)> uid(opendir(uid_path), closedir);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700202 if (uid != NULL) {
Elliott Hughes9f206932016-09-28 13:29:54 -0700203 dirent* dir;
204 while ((dir = readdir(uid.get())) != nullptr) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700205 char path[PROCESSGROUP_MAX_PATH_LEN];
206
207 if (dir->d_type != DT_DIR) {
208 continue;
209 }
210
211 if (strncmp(dir->d_name, PROCESSGROUP_PID_PREFIX, strlen(PROCESSGROUP_PID_PREFIX))) {
212 continue;
213 }
214
215 snprintf(path, sizeof(path), "%s/%s", uid_path, dir->d_name);
Tom Cherry20514c42017-04-21 13:48:49 -0700216 LOG(VERBOSE) << "Removing " << path;
217 if (rmdir(path) == -1) PLOG(WARNING) << "Failed to remove " << path;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700218 }
219 }
220}
221
222void removeAllProcessGroups()
223{
Elliott Hughes171df0a2016-08-02 13:30:30 -0700224 LOG(VERBOSE) << "removeAllProcessGroups()";
Elliott Hughesb6e1d152016-08-03 13:29:04 -0700225 const char* cgroup_root_path = getCgroupRootPath();
James Hawkins22b6f7a2016-02-19 11:10:30 -0800226 std::unique_ptr<DIR, decltype(&closedir)> root(opendir(cgroup_root_path), closedir);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700227 if (root == NULL) {
Tom Cherry20514c42017-04-21 13:48:49 -0700228 PLOG(ERROR) << "Failed to open " << cgroup_root_path;
Colin Crossc15dd042014-08-20 14:11:13 -0700229 } else {
Elliott Hughes9f206932016-09-28 13:29:54 -0700230 dirent* dir;
231 while ((dir = readdir(root.get())) != nullptr) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700232 char path[PROCESSGROUP_MAX_PATH_LEN];
233
234 if (dir->d_type != DT_DIR) {
235 continue;
236 }
237 if (strncmp(dir->d_name, PROCESSGROUP_UID_PREFIX, strlen(PROCESSGROUP_UID_PREFIX))) {
238 continue;
239 }
240
Martijn Coenenb82bab62016-01-20 16:39:16 -0800241 snprintf(path, sizeof(path), "%s/%s", cgroup_root_path, dir->d_name);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700242 removeUidProcessGroups(path);
Tom Cherry20514c42017-04-21 13:48:49 -0700243 LOG(VERBOSE) << "Removing " << path;
244 if (rmdir(path) == -1) PLOG(WARNING) << "Failed to remove " << path;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700245 }
Colin Crosscf8d1c22014-06-03 13:24:21 -0700246 }
247}
248
Tom Cherry20514c42017-04-21 13:48:49 -0700249// Returns number of processes killed on success
250// Returns 0 if there are no processes in the process cgroup left to kill
251// Returns -errno on error
Keun-young Parkfac4b632017-03-28 17:25:24 -0700252static int doKillProcessGroupOnce(uid_t uid, int initialPid, int signal) {
Tom Cherry20514c42017-04-21 13:48:49 -0700253 ProcessGroup process_group;
254 if (!process_group.Open(uid, initialPid)) {
255 PLOG(WARNING) << "Failed to open process cgroup uid " << uid << " pid " << initialPid;
256 return -errno;
257 }
258
Tom Cherry70a5ed42017-06-05 19:20:17 -0700259 // We separate all of the pids in the cgroup into those pids that are also the leaders of
260 // process groups (stored in the pgids set) and those that are not (stored in the pids set).
261 std::set<pid_t> pgids;
262 pgids.emplace(initialPid);
263 std::set<pid_t> pids;
264
Tom Cherry20514c42017-04-21 13:48:49 -0700265 int ret;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700266 pid_t pid;
Tom Cherry20514c42017-04-21 13:48:49 -0700267 int processes = 0;
268 while ((ret = process_group.GetOneAppProcess(&pid)) > 0 && pid >= 0) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700269 processes++;
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700270 if (pid == 0) {
271 // Should never happen... but if it does, trying to kill this
272 // will boomerang right back and kill us! Let's not let that happen.
Elliott Hughes171df0a2016-08-02 13:30:30 -0700273 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 -0700274 continue;
275 }
Tom Cherry70a5ed42017-06-05 19:20:17 -0700276 pid_t pgid = getpgid(pid);
277 if (pgid == -1) PLOG(ERROR) << "getpgid(" << pid << ") failed";
278 if (pgid == pid) {
279 pgids.emplace(pid);
280 } else {
281 pids.emplace(pid);
282 }
283 }
284
285 // Erase all pids that will be killed when we kill the process groups.
286 for (auto it = pids.begin(); it != pids.end();) {
287 pid_t pgid = getpgid(pid);
288 if (pgids.count(pgid) == 1) {
289 it = pids.erase(it);
290 } else {
291 ++it;
292 }
293 }
294
295 // Kill all process groups.
296 for (const auto pgid : pgids) {
297 LOG(VERBOSE) << "Killing process group " << -pgid << " in uid " << uid
298 << " as part of process cgroup " << initialPid;
299
300 if (kill(-pgid, signal) == -1) {
301 PLOG(WARNING) << "kill(" << -pgid << ", " << signal << ") failed";
302 }
303 }
304
305 // Kill remaining pids.
306 for (const auto pid : pids) {
Tom Cherry20514c42017-04-21 13:48:49 -0700307 LOG(VERBOSE) << "Killing pid " << pid << " in uid " << uid << " as part of process cgroup "
308 << initialPid;
Tom Cherry70a5ed42017-06-05 19:20:17 -0700309
Elliott Hughes171df0a2016-08-02 13:30:30 -0700310 if (kill(pid, signal) == -1) {
311 PLOG(WARNING) << "kill(" << pid << ", " << signal << ") failed";
Colin Crosscf8d1c22014-06-03 13:24:21 -0700312 }
313 }
314
Tom Cherry20514c42017-04-21 13:48:49 -0700315 return ret >= 0 ? processes : ret;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700316}
317
Tom Cherry20514c42017-04-21 13:48:49 -0700318static int killProcessGroup(uid_t uid, int initialPid, int signal, int retries) {
Collin Mullinerf7e79b92016-06-01 21:03:55 +0000319 std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
Colin Crosscf8d1c22014-06-03 13:24:21 -0700320
Tom Cherry20514c42017-04-21 13:48:49 -0700321 int retry = retries;
Collin Mullinerf7e79b92016-06-01 21:03:55 +0000322 int processes;
Keun-young Parkfac4b632017-03-28 17:25:24 -0700323 while ((processes = doKillProcessGroupOnce(uid, initialPid, signal)) > 0) {
Tom Cherry20514c42017-04-21 13:48:49 -0700324 LOG(VERBOSE) << "Killed " << processes << " processes for processgroup " << initialPid;
Yusuke Satod5039302015-06-16 13:51:14 -0700325 if (retry > 0) {
Elliott Hughes290a2282016-11-14 17:08:47 -0800326 std::this_thread::sleep_for(5ms);
Yusuke Satod5039302015-06-16 13:51:14 -0700327 --retry;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700328 } else {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700329 break;
330 }
331 }
332
Tom Cherry20514c42017-04-21 13:48:49 -0700333 if (processes < 0) {
334 PLOG(ERROR) << "Error encountered killing process cgroup uid " << uid << " pid "
335 << initialPid;
336 return -1;
337 }
Collin Mullinerf7e79b92016-06-01 21:03:55 +0000338
Tom Cherry20514c42017-04-21 13:48:49 -0700339 std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
Elliott Hughes171df0a2016-08-02 13:30:30 -0700340 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
Tom Cherry20514c42017-04-21 13:48:49 -0700341
342 // We only calculate the number of 'processes' when killing the processes.
343 // In the retries == 0 case, we only kill the processes once and therefore
344 // will not have waited then recalculated how many processes are remaining
345 // after the first signals have been sent.
346 // Logging anything regarding the number of 'processes' here does not make sense.
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700347
Colin Crosscf8d1c22014-06-03 13:24:21 -0700348 if (processes == 0) {
Tom Cherry20514c42017-04-21 13:48:49 -0700349 if (retries > 0) {
350 LOG(INFO) << "Successfully killed process cgroup uid " << uid << " pid " << initialPid
351 << " in " << static_cast<int>(ms) << "ms";
352 }
Colin Crosscf8d1c22014-06-03 13:24:21 -0700353 return removeProcessGroup(uid, initialPid);
354 } else {
Tom Cherry20514c42017-04-21 13:48:49 -0700355 if (retries > 0) {
356 LOG(ERROR) << "Failed to kill process cgroup uid " << uid << " pid " << initialPid
357 << " in " << static_cast<int>(ms) << "ms, " << processes
358 << " processes remain";
359 }
Colin Crosscf8d1c22014-06-03 13:24:21 -0700360 return -1;
361 }
362}
363
Keun-young Parkfac4b632017-03-28 17:25:24 -0700364int killProcessGroup(uid_t uid, int initialPid, int signal) {
Tom Cherry20514c42017-04-21 13:48:49 -0700365 return killProcessGroup(uid, initialPid, signal, 40 /*retries*/);
Keun-young Parkfac4b632017-03-28 17:25:24 -0700366}
367
368int killProcessGroupOnce(uid_t uid, int initialPid, int signal) {
Tom Cherry20514c42017-04-21 13:48:49 -0700369 return killProcessGroup(uid, initialPid, signal, 0 /*retries*/);
Keun-young Parkfac4b632017-03-28 17:25:24 -0700370}
371
Elliott Hughes171df0a2016-08-02 13:30:30 -0700372static bool mkdirAndChown(const char *path, mode_t mode, uid_t uid, gid_t gid)
Colin Crosscf8d1c22014-06-03 13:24:21 -0700373{
Elliott Hughes171df0a2016-08-02 13:30:30 -0700374 if (mkdir(path, mode) == -1 && errno != EEXIST) {
375 return false;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700376 }
377
Elliott Hughes171df0a2016-08-02 13:30:30 -0700378 if (chown(path, uid, gid) == -1) {
379 int saved_errno = errno;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700380 rmdir(path);
Elliott Hughes171df0a2016-08-02 13:30:30 -0700381 errno = saved_errno;
382 return false;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700383 }
384
Elliott Hughes171df0a2016-08-02 13:30:30 -0700385 return true;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700386}
387
388int createProcessGroup(uid_t uid, int initialPid)
389{
390 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
Colin Crosscf8d1c22014-06-03 13:24:21 -0700391
392 convertUidToPath(path, sizeof(path), uid);
393
Elliott Hughes171df0a2016-08-02 13:30:30 -0700394 if (!mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM)) {
Tom Cherry20514c42017-04-21 13:48:49 -0700395 PLOG(ERROR) << "Failed to make and chown " << path;
Elliott Hughes171df0a2016-08-02 13:30:30 -0700396 return -errno;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700397 }
398
399 convertUidPidToPath(path, sizeof(path), uid, initialPid);
400
Elliott Hughes171df0a2016-08-02 13:30:30 -0700401 if (!mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM)) {
Tom Cherry20514c42017-04-21 13:48:49 -0700402 PLOG(ERROR) << "Failed to make and chown " << path;
Elliott Hughes171df0a2016-08-02 13:30:30 -0700403 return -errno;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700404 }
405
406 strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path));
407
Elliott Hughes171df0a2016-08-02 13:30:30 -0700408 int ret = 0;
Robert Benead4852262017-07-16 19:38:11 -0700409 if (!WriteStringToFile(std::to_string(initialPid), path)) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700410 ret = -errno;
Robert Benead4852262017-07-16 19:38:11 -0700411 PLOG(ERROR) << "Failed to write '" << initialPid << "' to " << path;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700412 }
413
Colin Crosscf8d1c22014-06-03 13:24:21 -0700414 return ret;
415}
Robert Benead4852262017-07-16 19:38:11 -0700416
417static bool setProcessGroupValue(uid_t uid, int pid, const char* fileName, int64_t value) {
418 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
419 if (strcmp(getCgroupRootPath(), MEM_CGROUP_PATH)) {
420 PLOG(ERROR) << "Memcg is not mounted." << path;
421 return false;
422 }
423
424 convertUidPidToPath(path, sizeof(path), uid, pid);
425 strlcat(path, fileName, sizeof(path));
426
427 if (!WriteStringToFile(std::to_string(value), path)) {
428 PLOG(ERROR) << "Failed to write '" << value << "' to " << path;
429 return false;
430 }
431 return true;
432}
433
434bool setProcessGroupSwappiness(uid_t uid, int pid, int swappiness) {
435 return setProcessGroupValue(uid, pid, "/memory.swappiness", swappiness);
436}
437
438bool setProcessGroupSoftLimit(uid_t uid, int pid, int64_t soft_limit_in_bytes) {
439 return setProcessGroupValue(uid, pid, "/memory.soft_limit_in_bytes", soft_limit_in_bytes);
440}
441
442bool setProcessGroupLimit(uid_t uid, int pid, int64_t limit_in_bytes) {
443 return setProcessGroupValue(uid, pid, "/memory.limit_in_bytes", limit_in_bytes);
444}