blob: f5d4e1c9e4f4fe9fe9ae82f9e44c16c4ecf2150c [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
Elliott Hughes171df0a2016-08-02 13:30:30 -070038#include <android-base/logging.h>
Tom Cherry20514c42017-04-21 13:48:49 -070039#include <android-base/unique_fd.h>
Colin Crosscf8d1c22014-06-03 13:24:21 -070040#include <private/android_filesystem_config.h>
41
42#include <processgroup/processgroup.h>
Martijn Coenenb82bab62016-01-20 16:39:16 -080043
Elliott Hughes290a2282016-11-14 17:08:47 -080044using namespace std::chrono_literals;
45
Martijn Coenen5bb91ab2016-03-18 15:28:31 +010046// Uncomment line below use memory cgroups for keeping track of (forked) PIDs
47// #define USE_MEMCG 1
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() {
Martijn Coenen5bb91ab2016-03-18 15:28:31 +010094#ifdef USE_MEMCG
Martijn Coenenb82bab62016-01-20 16:39:16 -080095 static const char* cgroup_root_path = NULL;
96 std::call_once(init_path_flag, [&]() {
Martijn Coenen623b56a2016-02-08 11:42:25 +010097 // Check if mem cgroup is mounted, only then check for write-access to avoid
98 // SELinux denials
99 cgroup_root_path = access(MEM_CGROUP_TASKS, F_OK) || access(MEM_CGROUP_PATH, W_OK) ?
100 ACCT_CGROUP_PATH : MEM_CGROUP_PATH;
Martijn Coenenb82bab62016-01-20 16:39:16 -0800101 });
102 return cgroup_root_path;
Martijn Coenen5bb91ab2016-03-18 15:28:31 +0100103#else
104 return ACCT_CGROUP_PATH;
105#endif
Martijn Coenenb82bab62016-01-20 16:39:16 -0800106}
107
Colin Crosscf8d1c22014-06-03 13:24:21 -0700108static int convertUidToPath(char *path, size_t size, uid_t uid)
109{
110 return snprintf(path, size, "%s/%s%d",
Martijn Coenenb82bab62016-01-20 16:39:16 -0800111 getCgroupRootPath(),
Colin Crosscf8d1c22014-06-03 13:24:21 -0700112 PROCESSGROUP_UID_PREFIX,
113 uid);
114}
115
116static int convertUidPidToPath(char *path, size_t size, uid_t uid, int pid)
117{
118 return snprintf(path, size, "%s/%s%d/%s%d",
Martijn Coenenb82bab62016-01-20 16:39:16 -0800119 getCgroupRootPath(),
Colin Crosscf8d1c22014-06-03 13:24:21 -0700120 PROCESSGROUP_UID_PREFIX,
121 uid,
122 PROCESSGROUP_PID_PREFIX,
123 pid);
124}
125
Tom Cherry20514c42017-04-21 13:48:49 -0700126bool ProcessGroup::Open(uid_t uid, int pid) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700127 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
128 convertUidPidToPath(path, sizeof(path), uid, pid);
129 strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path));
130
131 int fd = open(path, O_RDONLY);
Tom Cherry20514c42017-04-21 13:48:49 -0700132 if (fd < 0) return false;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700133
Tom Cherry20514c42017-04-21 13:48:49 -0700134 fd_.reset(fd);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700135
Elliott Hughes171df0a2016-08-02 13:30:30 -0700136 LOG(VERBOSE) << "Initialized context for " << path;
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700137
Tom Cherry20514c42017-04-21 13:48:49 -0700138 return true;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700139}
140
Tom Cherry20514c42017-04-21 13:48:49 -0700141int ProcessGroup::RefillBuffer() {
142 memmove(buf_, buf_ptr_, buf_len_);
143 buf_ptr_ = buf_;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700144
Tom Cherry20514c42017-04-21 13:48:49 -0700145 ssize_t ret = read(fd_, buf_ptr_ + buf_len_, sizeof(buf_) - buf_len_ - 1);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700146 if (ret < 0) {
147 return -errno;
148 } else if (ret == 0) {
149 return 0;
150 }
151
Tom Cherry20514c42017-04-21 13:48:49 -0700152 buf_len_ += ret;
153 buf_[buf_len_] = 0;
154 LOG(VERBOSE) << "Read " << ret << " to buffer: " << buf_;
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700155
Tom Cherry20514c42017-04-21 13:48:49 -0700156 assert(buf_len_ <= sizeof(buf_));
Colin Crosscf8d1c22014-06-03 13:24:21 -0700157
158 return ret;
159}
160
Tom Cherry20514c42017-04-21 13:48:49 -0700161int ProcessGroup::GetOneAppProcess(pid_t* out_pid) {
162 *out_pid = 0;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700163
Tom Cherry20514c42017-04-21 13:48:49 -0700164 char* eptr;
165 while ((eptr = static_cast<char*>(memchr(buf_ptr_, '\n', buf_len_))) == nullptr) {
166 int ret = RefillBuffer();
167 if (ret <= 0) return ret;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700168 }
169
170 *eptr = '\0';
Tom Cherry20514c42017-04-21 13:48:49 -0700171 char* pid_eptr = nullptr;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700172 errno = 0;
Tom Cherry20514c42017-04-21 13:48:49 -0700173 long pid = strtol(buf_ptr_, &pid_eptr, 10);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700174 if (errno != 0) {
175 return -errno;
176 }
177 if (pid_eptr != eptr) {
Tom Cherry20514c42017-04-21 13:48:49 -0700178 errno = EINVAL;
179 return -errno;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700180 }
181
Tom Cherry20514c42017-04-21 13:48:49 -0700182 buf_len_ -= (eptr - buf_ptr_) + 1;
183 buf_ptr_ = eptr + 1;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700184
Tom Cherry20514c42017-04-21 13:48:49 -0700185 *out_pid = static_cast<pid_t>(pid);
186 return 1;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700187}
188
189static int removeProcessGroup(uid_t uid, int pid)
190{
191 int ret;
192 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
193
194 convertUidPidToPath(path, sizeof(path), uid, pid);
195 ret = rmdir(path);
196
197 convertUidToPath(path, sizeof(path), uid);
198 rmdir(path);
199
200 return ret;
201}
202
203static void removeUidProcessGroups(const char *uid_path)
204{
James Hawkins588a2ca2016-02-18 14:52:46 -0800205 std::unique_ptr<DIR, decltype(&closedir)> uid(opendir(uid_path), closedir);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700206 if (uid != NULL) {
Elliott Hughes9f206932016-09-28 13:29:54 -0700207 dirent* dir;
208 while ((dir = readdir(uid.get())) != nullptr) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700209 char path[PROCESSGROUP_MAX_PATH_LEN];
210
211 if (dir->d_type != DT_DIR) {
212 continue;
213 }
214
215 if (strncmp(dir->d_name, PROCESSGROUP_PID_PREFIX, strlen(PROCESSGROUP_PID_PREFIX))) {
216 continue;
217 }
218
219 snprintf(path, sizeof(path), "%s/%s", uid_path, dir->d_name);
Tom Cherry20514c42017-04-21 13:48:49 -0700220 LOG(VERBOSE) << "Removing " << path;
221 if (rmdir(path) == -1) PLOG(WARNING) << "Failed to remove " << path;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700222 }
223 }
224}
225
226void removeAllProcessGroups()
227{
Elliott Hughes171df0a2016-08-02 13:30:30 -0700228 LOG(VERBOSE) << "removeAllProcessGroups()";
Elliott Hughesb6e1d152016-08-03 13:29:04 -0700229 const char* cgroup_root_path = getCgroupRootPath();
James Hawkins22b6f7a2016-02-19 11:10:30 -0800230 std::unique_ptr<DIR, decltype(&closedir)> root(opendir(cgroup_root_path), closedir);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700231 if (root == NULL) {
Tom Cherry20514c42017-04-21 13:48:49 -0700232 PLOG(ERROR) << "Failed to open " << cgroup_root_path;
Colin Crossc15dd042014-08-20 14:11:13 -0700233 } else {
Elliott Hughes9f206932016-09-28 13:29:54 -0700234 dirent* dir;
235 while ((dir = readdir(root.get())) != nullptr) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700236 char path[PROCESSGROUP_MAX_PATH_LEN];
237
238 if (dir->d_type != DT_DIR) {
239 continue;
240 }
241 if (strncmp(dir->d_name, PROCESSGROUP_UID_PREFIX, strlen(PROCESSGROUP_UID_PREFIX))) {
242 continue;
243 }
244
Martijn Coenenb82bab62016-01-20 16:39:16 -0800245 snprintf(path, sizeof(path), "%s/%s", cgroup_root_path, dir->d_name);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700246 removeUidProcessGroups(path);
Tom Cherry20514c42017-04-21 13:48:49 -0700247 LOG(VERBOSE) << "Removing " << path;
248 if (rmdir(path) == -1) PLOG(WARNING) << "Failed to remove " << path;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700249 }
Colin Crosscf8d1c22014-06-03 13:24:21 -0700250 }
251}
252
Tom Cherry20514c42017-04-21 13:48:49 -0700253// Returns number of processes killed on success
254// Returns 0 if there are no processes in the process cgroup left to kill
255// Returns -errno on error
Keun-young Parkfac4b632017-03-28 17:25:24 -0700256static int doKillProcessGroupOnce(uid_t uid, int initialPid, int signal) {
Tom Cherry20514c42017-04-21 13:48:49 -0700257 ProcessGroup process_group;
258 if (!process_group.Open(uid, initialPid)) {
259 PLOG(WARNING) << "Failed to open process cgroup uid " << uid << " pid " << initialPid;
260 return -errno;
261 }
262
Tom Cherry70a5ed42017-06-05 19:20:17 -0700263 // We separate all of the pids in the cgroup into those pids that are also the leaders of
264 // process groups (stored in the pgids set) and those that are not (stored in the pids set).
265 std::set<pid_t> pgids;
266 pgids.emplace(initialPid);
267 std::set<pid_t> pids;
268
Tom Cherry20514c42017-04-21 13:48:49 -0700269 int ret;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700270 pid_t pid;
Tom Cherry20514c42017-04-21 13:48:49 -0700271 int processes = 0;
272 while ((ret = process_group.GetOneAppProcess(&pid)) > 0 && pid >= 0) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700273 processes++;
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700274 if (pid == 0) {
275 // Should never happen... but if it does, trying to kill this
276 // will boomerang right back and kill us! Let's not let that happen.
Elliott Hughes171df0a2016-08-02 13:30:30 -0700277 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 -0700278 continue;
279 }
Tom Cherry70a5ed42017-06-05 19:20:17 -0700280 pid_t pgid = getpgid(pid);
281 if (pgid == -1) PLOG(ERROR) << "getpgid(" << pid << ") failed";
282 if (pgid == pid) {
283 pgids.emplace(pid);
284 } else {
285 pids.emplace(pid);
286 }
287 }
288
289 // Erase all pids that will be killed when we kill the process groups.
290 for (auto it = pids.begin(); it != pids.end();) {
291 pid_t pgid = getpgid(pid);
292 if (pgids.count(pgid) == 1) {
293 it = pids.erase(it);
294 } else {
295 ++it;
296 }
297 }
298
299 // Kill all process groups.
300 for (const auto pgid : pgids) {
301 LOG(VERBOSE) << "Killing process group " << -pgid << " in uid " << uid
302 << " as part of process cgroup " << initialPid;
303
304 if (kill(-pgid, signal) == -1) {
305 PLOG(WARNING) << "kill(" << -pgid << ", " << signal << ") failed";
306 }
307 }
308
309 // Kill remaining pids.
310 for (const auto pid : pids) {
Tom Cherry20514c42017-04-21 13:48:49 -0700311 LOG(VERBOSE) << "Killing pid " << pid << " in uid " << uid << " as part of process cgroup "
312 << initialPid;
Tom Cherry70a5ed42017-06-05 19:20:17 -0700313
Elliott Hughes171df0a2016-08-02 13:30:30 -0700314 if (kill(pid, signal) == -1) {
315 PLOG(WARNING) << "kill(" << pid << ", " << signal << ") failed";
Colin Crosscf8d1c22014-06-03 13:24:21 -0700316 }
317 }
318
Tom Cherry20514c42017-04-21 13:48:49 -0700319 return ret >= 0 ? processes : ret;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700320}
321
Tom Cherry20514c42017-04-21 13:48:49 -0700322static int killProcessGroup(uid_t uid, int initialPid, int signal, int retries) {
Collin Mullinerf7e79b92016-06-01 21:03:55 +0000323 std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
Colin Crosscf8d1c22014-06-03 13:24:21 -0700324
Tom Cherry20514c42017-04-21 13:48:49 -0700325 int retry = retries;
Collin Mullinerf7e79b92016-06-01 21:03:55 +0000326 int processes;
Keun-young Parkfac4b632017-03-28 17:25:24 -0700327 while ((processes = doKillProcessGroupOnce(uid, initialPid, signal)) > 0) {
Tom Cherry20514c42017-04-21 13:48:49 -0700328 LOG(VERBOSE) << "Killed " << processes << " processes for processgroup " << initialPid;
Yusuke Satod5039302015-06-16 13:51:14 -0700329 if (retry > 0) {
Elliott Hughes290a2282016-11-14 17:08:47 -0800330 std::this_thread::sleep_for(5ms);
Yusuke Satod5039302015-06-16 13:51:14 -0700331 --retry;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700332 } else {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700333 break;
334 }
335 }
336
Tom Cherry20514c42017-04-21 13:48:49 -0700337 if (processes < 0) {
338 PLOG(ERROR) << "Error encountered killing process cgroup uid " << uid << " pid "
339 << initialPid;
340 return -1;
341 }
Collin Mullinerf7e79b92016-06-01 21:03:55 +0000342
Tom Cherry20514c42017-04-21 13:48:49 -0700343 std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
Elliott Hughes171df0a2016-08-02 13:30:30 -0700344 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
Tom Cherry20514c42017-04-21 13:48:49 -0700345
346 // We only calculate the number of 'processes' when killing the processes.
347 // In the retries == 0 case, we only kill the processes once and therefore
348 // will not have waited then recalculated how many processes are remaining
349 // after the first signals have been sent.
350 // Logging anything regarding the number of 'processes' here does not make sense.
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700351
Colin Crosscf8d1c22014-06-03 13:24:21 -0700352 if (processes == 0) {
Tom Cherry20514c42017-04-21 13:48:49 -0700353 if (retries > 0) {
354 LOG(INFO) << "Successfully killed process cgroup uid " << uid << " pid " << initialPid
355 << " in " << static_cast<int>(ms) << "ms";
356 }
Colin Crosscf8d1c22014-06-03 13:24:21 -0700357 return removeProcessGroup(uid, initialPid);
358 } else {
Tom Cherry20514c42017-04-21 13:48:49 -0700359 if (retries > 0) {
360 LOG(ERROR) << "Failed to kill process cgroup uid " << uid << " pid " << initialPid
361 << " in " << static_cast<int>(ms) << "ms, " << processes
362 << " processes remain";
363 }
Colin Crosscf8d1c22014-06-03 13:24:21 -0700364 return -1;
365 }
366}
367
Keun-young Parkfac4b632017-03-28 17:25:24 -0700368int killProcessGroup(uid_t uid, int initialPid, int signal) {
Tom Cherry20514c42017-04-21 13:48:49 -0700369 return killProcessGroup(uid, initialPid, signal, 40 /*retries*/);
Keun-young Parkfac4b632017-03-28 17:25:24 -0700370}
371
372int killProcessGroupOnce(uid_t uid, int initialPid, int signal) {
Tom Cherry20514c42017-04-21 13:48:49 -0700373 return killProcessGroup(uid, initialPid, signal, 0 /*retries*/);
Keun-young Parkfac4b632017-03-28 17:25:24 -0700374}
375
Elliott Hughes171df0a2016-08-02 13:30:30 -0700376static bool mkdirAndChown(const char *path, mode_t mode, uid_t uid, gid_t gid)
Colin Crosscf8d1c22014-06-03 13:24:21 -0700377{
Elliott Hughes171df0a2016-08-02 13:30:30 -0700378 if (mkdir(path, mode) == -1 && errno != EEXIST) {
379 return false;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700380 }
381
Elliott Hughes171df0a2016-08-02 13:30:30 -0700382 if (chown(path, uid, gid) == -1) {
383 int saved_errno = errno;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700384 rmdir(path);
Elliott Hughes171df0a2016-08-02 13:30:30 -0700385 errno = saved_errno;
386 return false;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700387 }
388
Elliott Hughes171df0a2016-08-02 13:30:30 -0700389 return true;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700390}
391
392int createProcessGroup(uid_t uid, int initialPid)
393{
394 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
Colin Crosscf8d1c22014-06-03 13:24:21 -0700395
396 convertUidToPath(path, sizeof(path), uid);
397
Elliott Hughes171df0a2016-08-02 13:30:30 -0700398 if (!mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM)) {
Tom Cherry20514c42017-04-21 13:48:49 -0700399 PLOG(ERROR) << "Failed to make and chown " << path;
Elliott Hughes171df0a2016-08-02 13:30:30 -0700400 return -errno;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700401 }
402
403 convertUidPidToPath(path, sizeof(path), uid, initialPid);
404
Elliott Hughes171df0a2016-08-02 13:30:30 -0700405 if (!mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM)) {
Tom Cherry20514c42017-04-21 13:48:49 -0700406 PLOG(ERROR) << "Failed to make and chown " << path;
Elliott Hughes171df0a2016-08-02 13:30:30 -0700407 return -errno;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700408 }
409
410 strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path));
411
412 int fd = open(path, O_WRONLY);
Elliott Hughes171df0a2016-08-02 13:30:30 -0700413 if (fd == -1) {
414 int ret = -errno;
Tom Cherry20514c42017-04-21 13:48:49 -0700415 PLOG(ERROR) << "Failed to open " << path;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700416 return ret;
417 }
418
419 char pid[PROCESSGROUP_MAX_PID_LEN + 1] = {0};
420 int len = snprintf(pid, sizeof(pid), "%d", initialPid);
421
Elliott Hughes171df0a2016-08-02 13:30:30 -0700422 int ret = 0;
423 if (write(fd, pid, len) < 0) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700424 ret = -errno;
Tom Cherry20514c42017-04-21 13:48:49 -0700425 PLOG(ERROR) << "Failed to write '" << pid << "' to " << path;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700426 }
427
428 close(fd);
429 return ret;
430}