blob: 9b8248e88c5706165c7dc3ea50778018cbbcb53d [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 Coenenb82bab62016-01-20 16:39:16 -080046#define MEM_CGROUP_PATH "/dev/memcg/apps"
Martijn Coenen623b56a2016-02-08 11:42:25 +010047#define MEM_CGROUP_TASKS "/dev/memcg/apps/tasks"
Martijn Coenenb82bab62016-01-20 16:39:16 -080048#define ACCT_CGROUP_PATH "/acct"
49
50#define PROCESSGROUP_UID_PREFIX "uid_"
51#define PROCESSGROUP_PID_PREFIX "pid_"
52#define PROCESSGROUP_CGROUP_PROCS_FILE "/cgroup.procs"
53#define PROCESSGROUP_MAX_UID_LEN 11
54#define PROCESSGROUP_MAX_PID_LEN 11
55#define PROCESSGROUP_MAX_PATH_LEN \
56 ((sizeof(MEM_CGROUP_PATH) > sizeof(ACCT_CGROUP_PATH) ? \
57 sizeof(MEM_CGROUP_PATH) : sizeof(ACCT_CGROUP_PATH)) + \
58 sizeof(PROCESSGROUP_UID_PREFIX) + 1 + \
59 PROCESSGROUP_MAX_UID_LEN + \
60 sizeof(PROCESSGROUP_PID_PREFIX) + 1 + \
61 PROCESSGROUP_MAX_PID_LEN + \
62 sizeof(PROCESSGROUP_CGROUP_PROCS_FILE) + \
63 1)
64
65std::once_flag init_path_flag;
Colin Crosscf8d1c22014-06-03 13:24:21 -070066
Tom Cherry20514c42017-04-21 13:48:49 -070067class ProcessGroup {
68 public:
69 ProcessGroup() : buf_ptr_(buf_), buf_len_(0) {}
70
71 bool Open(uid_t uid, int pid);
72
73 // Return positive number and sets *pid = next pid in process cgroup on success
74 // Returns 0 if there are no pids left in the process cgroup
75 // Returns -errno if an error was encountered
76 int GetOneAppProcess(pid_t* pid);
77
78 private:
79 // Returns positive number of bytes filled on success
80 // Returns 0 if there was nothing to read
81 // Returns -errno if an error was encountered
82 int RefillBuffer();
83
84 android::base::unique_fd fd_;
85 char buf_[128];
86 char* buf_ptr_;
87 size_t buf_len_;
Colin Crosscf8d1c22014-06-03 13:24:21 -070088};
89
Martijn Coenenb82bab62016-01-20 16:39:16 -080090static const char* getCgroupRootPath() {
91 static const char* cgroup_root_path = NULL;
92 std::call_once(init_path_flag, [&]() {
Martijn Coenen623b56a2016-02-08 11:42:25 +010093 // Check if mem cgroup is mounted, only then check for write-access to avoid
94 // SELinux denials
95 cgroup_root_path = access(MEM_CGROUP_TASKS, F_OK) || access(MEM_CGROUP_PATH, W_OK) ?
96 ACCT_CGROUP_PATH : MEM_CGROUP_PATH;
Martijn Coenenb82bab62016-01-20 16:39:16 -080097 });
98 return cgroup_root_path;
99}
100
Colin Crosscf8d1c22014-06-03 13:24:21 -0700101static int convertUidToPath(char *path, size_t size, uid_t uid)
102{
103 return snprintf(path, size, "%s/%s%d",
Martijn Coenenb82bab62016-01-20 16:39:16 -0800104 getCgroupRootPath(),
Colin Crosscf8d1c22014-06-03 13:24:21 -0700105 PROCESSGROUP_UID_PREFIX,
106 uid);
107}
108
109static int convertUidPidToPath(char *path, size_t size, uid_t uid, int pid)
110{
111 return snprintf(path, size, "%s/%s%d/%s%d",
Martijn Coenenb82bab62016-01-20 16:39:16 -0800112 getCgroupRootPath(),
Colin Crosscf8d1c22014-06-03 13:24:21 -0700113 PROCESSGROUP_UID_PREFIX,
114 uid,
115 PROCESSGROUP_PID_PREFIX,
116 pid);
117}
118
Tom Cherry20514c42017-04-21 13:48:49 -0700119bool ProcessGroup::Open(uid_t uid, int pid) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700120 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
121 convertUidPidToPath(path, sizeof(path), uid, pid);
122 strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path));
123
124 int fd = open(path, O_RDONLY);
Tom Cherry20514c42017-04-21 13:48:49 -0700125 if (fd < 0) return false;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700126
Tom Cherry20514c42017-04-21 13:48:49 -0700127 fd_.reset(fd);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700128
Elliott Hughes171df0a2016-08-02 13:30:30 -0700129 LOG(VERBOSE) << "Initialized context for " << path;
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700130
Tom Cherry20514c42017-04-21 13:48:49 -0700131 return true;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700132}
133
Tom Cherry20514c42017-04-21 13:48:49 -0700134int ProcessGroup::RefillBuffer() {
135 memmove(buf_, buf_ptr_, buf_len_);
136 buf_ptr_ = buf_;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700137
Tom Cherry20514c42017-04-21 13:48:49 -0700138 ssize_t ret = read(fd_, buf_ptr_ + buf_len_, sizeof(buf_) - buf_len_ - 1);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700139 if (ret < 0) {
140 return -errno;
141 } else if (ret == 0) {
142 return 0;
143 }
144
Tom Cherry20514c42017-04-21 13:48:49 -0700145 buf_len_ += ret;
146 buf_[buf_len_] = 0;
147 LOG(VERBOSE) << "Read " << ret << " to buffer: " << buf_;
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700148
Tom Cherry20514c42017-04-21 13:48:49 -0700149 assert(buf_len_ <= sizeof(buf_));
Colin Crosscf8d1c22014-06-03 13:24:21 -0700150
151 return ret;
152}
153
Tom Cherry20514c42017-04-21 13:48:49 -0700154int ProcessGroup::GetOneAppProcess(pid_t* out_pid) {
155 *out_pid = 0;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700156
Tom Cherry20514c42017-04-21 13:48:49 -0700157 char* eptr;
158 while ((eptr = static_cast<char*>(memchr(buf_ptr_, '\n', buf_len_))) == nullptr) {
159 int ret = RefillBuffer();
160 if (ret <= 0) return ret;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700161 }
162
163 *eptr = '\0';
Tom Cherry20514c42017-04-21 13:48:49 -0700164 char* pid_eptr = nullptr;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700165 errno = 0;
Tom Cherry20514c42017-04-21 13:48:49 -0700166 long pid = strtol(buf_ptr_, &pid_eptr, 10);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700167 if (errno != 0) {
168 return -errno;
169 }
170 if (pid_eptr != eptr) {
Tom Cherry20514c42017-04-21 13:48:49 -0700171 errno = EINVAL;
172 return -errno;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700173 }
174
Tom Cherry20514c42017-04-21 13:48:49 -0700175 buf_len_ -= (eptr - buf_ptr_) + 1;
176 buf_ptr_ = eptr + 1;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700177
Tom Cherry20514c42017-04-21 13:48:49 -0700178 *out_pid = static_cast<pid_t>(pid);
179 return 1;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700180}
181
182static int removeProcessGroup(uid_t uid, int pid)
183{
184 int ret;
185 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
186
187 convertUidPidToPath(path, sizeof(path), uid, pid);
188 ret = rmdir(path);
189
190 convertUidToPath(path, sizeof(path), uid);
191 rmdir(path);
192
193 return ret;
194}
195
196static void removeUidProcessGroups(const char *uid_path)
197{
James Hawkins588a2ca2016-02-18 14:52:46 -0800198 std::unique_ptr<DIR, decltype(&closedir)> uid(opendir(uid_path), closedir);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700199 if (uid != NULL) {
Elliott Hughes9f206932016-09-28 13:29:54 -0700200 dirent* dir;
201 while ((dir = readdir(uid.get())) != nullptr) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700202 char path[PROCESSGROUP_MAX_PATH_LEN];
203
204 if (dir->d_type != DT_DIR) {
205 continue;
206 }
207
208 if (strncmp(dir->d_name, PROCESSGROUP_PID_PREFIX, strlen(PROCESSGROUP_PID_PREFIX))) {
209 continue;
210 }
211
212 snprintf(path, sizeof(path), "%s/%s", uid_path, dir->d_name);
Tom Cherry20514c42017-04-21 13:48:49 -0700213 LOG(VERBOSE) << "Removing " << path;
214 if (rmdir(path) == -1) PLOG(WARNING) << "Failed to remove " << path;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700215 }
216 }
217}
218
219void removeAllProcessGroups()
220{
Elliott Hughes171df0a2016-08-02 13:30:30 -0700221 LOG(VERBOSE) << "removeAllProcessGroups()";
Elliott Hughesb6e1d152016-08-03 13:29:04 -0700222 const char* cgroup_root_path = getCgroupRootPath();
James Hawkins22b6f7a2016-02-19 11:10:30 -0800223 std::unique_ptr<DIR, decltype(&closedir)> root(opendir(cgroup_root_path), closedir);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700224 if (root == NULL) {
Tom Cherry20514c42017-04-21 13:48:49 -0700225 PLOG(ERROR) << "Failed to open " << cgroup_root_path;
Colin Crossc15dd042014-08-20 14:11:13 -0700226 } else {
Elliott Hughes9f206932016-09-28 13:29:54 -0700227 dirent* dir;
228 while ((dir = readdir(root.get())) != nullptr) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700229 char path[PROCESSGROUP_MAX_PATH_LEN];
230
231 if (dir->d_type != DT_DIR) {
232 continue;
233 }
234 if (strncmp(dir->d_name, PROCESSGROUP_UID_PREFIX, strlen(PROCESSGROUP_UID_PREFIX))) {
235 continue;
236 }
237
Martijn Coenenb82bab62016-01-20 16:39:16 -0800238 snprintf(path, sizeof(path), "%s/%s", cgroup_root_path, dir->d_name);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700239 removeUidProcessGroups(path);
Tom Cherry20514c42017-04-21 13:48:49 -0700240 LOG(VERBOSE) << "Removing " << path;
241 if (rmdir(path) == -1) PLOG(WARNING) << "Failed to remove " << path;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700242 }
Colin Crosscf8d1c22014-06-03 13:24:21 -0700243 }
244}
245
Tom Cherry20514c42017-04-21 13:48:49 -0700246// Returns number of processes killed on success
247// Returns 0 if there are no processes in the process cgroup left to kill
248// Returns -errno on error
Keun-young Parkfac4b632017-03-28 17:25:24 -0700249static int doKillProcessGroupOnce(uid_t uid, int initialPid, int signal) {
Tom Cherry20514c42017-04-21 13:48:49 -0700250 ProcessGroup process_group;
251 if (!process_group.Open(uid, initialPid)) {
252 PLOG(WARNING) << "Failed to open process cgroup uid " << uid << " pid " << initialPid;
253 return -errno;
254 }
255
Tom Cherry70a5ed42017-06-05 19:20:17 -0700256 // We separate all of the pids in the cgroup into those pids that are also the leaders of
257 // process groups (stored in the pgids set) and those that are not (stored in the pids set).
258 std::set<pid_t> pgids;
259 pgids.emplace(initialPid);
260 std::set<pid_t> pids;
261
Tom Cherry20514c42017-04-21 13:48:49 -0700262 int ret;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700263 pid_t pid;
Tom Cherry20514c42017-04-21 13:48:49 -0700264 int processes = 0;
265 while ((ret = process_group.GetOneAppProcess(&pid)) > 0 && pid >= 0) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700266 processes++;
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700267 if (pid == 0) {
268 // Should never happen... but if it does, trying to kill this
269 // will boomerang right back and kill us! Let's not let that happen.
Elliott Hughes171df0a2016-08-02 13:30:30 -0700270 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 -0700271 continue;
272 }
Tom Cherry70a5ed42017-06-05 19:20:17 -0700273 pid_t pgid = getpgid(pid);
274 if (pgid == -1) PLOG(ERROR) << "getpgid(" << pid << ") failed";
275 if (pgid == pid) {
276 pgids.emplace(pid);
277 } else {
278 pids.emplace(pid);
279 }
280 }
281
282 // Erase all pids that will be killed when we kill the process groups.
283 for (auto it = pids.begin(); it != pids.end();) {
284 pid_t pgid = getpgid(pid);
285 if (pgids.count(pgid) == 1) {
286 it = pids.erase(it);
287 } else {
288 ++it;
289 }
290 }
291
292 // Kill all process groups.
293 for (const auto pgid : pgids) {
294 LOG(VERBOSE) << "Killing process group " << -pgid << " in uid " << uid
295 << " as part of process cgroup " << initialPid;
296
297 if (kill(-pgid, signal) == -1) {
298 PLOG(WARNING) << "kill(" << -pgid << ", " << signal << ") failed";
299 }
300 }
301
302 // Kill remaining pids.
303 for (const auto pid : pids) {
Tom Cherry20514c42017-04-21 13:48:49 -0700304 LOG(VERBOSE) << "Killing pid " << pid << " in uid " << uid << " as part of process cgroup "
305 << initialPid;
Tom Cherry70a5ed42017-06-05 19:20:17 -0700306
Elliott Hughes171df0a2016-08-02 13:30:30 -0700307 if (kill(pid, signal) == -1) {
308 PLOG(WARNING) << "kill(" << pid << ", " << signal << ") failed";
Colin Crosscf8d1c22014-06-03 13:24:21 -0700309 }
310 }
311
Tom Cherry20514c42017-04-21 13:48:49 -0700312 return ret >= 0 ? processes : ret;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700313}
314
Tom Cherry20514c42017-04-21 13:48:49 -0700315static int killProcessGroup(uid_t uid, int initialPid, int signal, int retries) {
Collin Mullinerf7e79b92016-06-01 21:03:55 +0000316 std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
Colin Crosscf8d1c22014-06-03 13:24:21 -0700317
Tom Cherry20514c42017-04-21 13:48:49 -0700318 int retry = retries;
Collin Mullinerf7e79b92016-06-01 21:03:55 +0000319 int processes;
Keun-young Parkfac4b632017-03-28 17:25:24 -0700320 while ((processes = doKillProcessGroupOnce(uid, initialPid, signal)) > 0) {
Tom Cherry20514c42017-04-21 13:48:49 -0700321 LOG(VERBOSE) << "Killed " << processes << " processes for processgroup " << initialPid;
Yusuke Satod5039302015-06-16 13:51:14 -0700322 if (retry > 0) {
Elliott Hughes290a2282016-11-14 17:08:47 -0800323 std::this_thread::sleep_for(5ms);
Yusuke Satod5039302015-06-16 13:51:14 -0700324 --retry;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700325 } else {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700326 break;
327 }
328 }
329
Tom Cherry20514c42017-04-21 13:48:49 -0700330 if (processes < 0) {
331 PLOG(ERROR) << "Error encountered killing process cgroup uid " << uid << " pid "
332 << initialPid;
333 return -1;
334 }
Collin Mullinerf7e79b92016-06-01 21:03:55 +0000335
Tom Cherry20514c42017-04-21 13:48:49 -0700336 std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
Elliott Hughes171df0a2016-08-02 13:30:30 -0700337 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
Tom Cherry20514c42017-04-21 13:48:49 -0700338
339 // We only calculate the number of 'processes' when killing the processes.
340 // In the retries == 0 case, we only kill the processes once and therefore
341 // will not have waited then recalculated how many processes are remaining
342 // after the first signals have been sent.
343 // Logging anything regarding the number of 'processes' here does not make sense.
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700344
Colin Crosscf8d1c22014-06-03 13:24:21 -0700345 if (processes == 0) {
Tom Cherry20514c42017-04-21 13:48:49 -0700346 if (retries > 0) {
347 LOG(INFO) << "Successfully killed process cgroup uid " << uid << " pid " << initialPid
348 << " in " << static_cast<int>(ms) << "ms";
349 }
Colin Crosscf8d1c22014-06-03 13:24:21 -0700350 return removeProcessGroup(uid, initialPid);
351 } else {
Tom Cherry20514c42017-04-21 13:48:49 -0700352 if (retries > 0) {
353 LOG(ERROR) << "Failed to kill process cgroup uid " << uid << " pid " << initialPid
354 << " in " << static_cast<int>(ms) << "ms, " << processes
355 << " processes remain";
356 }
Colin Crosscf8d1c22014-06-03 13:24:21 -0700357 return -1;
358 }
359}
360
Keun-young Parkfac4b632017-03-28 17:25:24 -0700361int killProcessGroup(uid_t uid, int initialPid, int signal) {
Tom Cherry20514c42017-04-21 13:48:49 -0700362 return killProcessGroup(uid, initialPid, signal, 40 /*retries*/);
Keun-young Parkfac4b632017-03-28 17:25:24 -0700363}
364
365int killProcessGroupOnce(uid_t uid, int initialPid, int signal) {
Tom Cherry20514c42017-04-21 13:48:49 -0700366 return killProcessGroup(uid, initialPid, signal, 0 /*retries*/);
Keun-young Parkfac4b632017-03-28 17:25:24 -0700367}
368
Elliott Hughes171df0a2016-08-02 13:30:30 -0700369static bool mkdirAndChown(const char *path, mode_t mode, uid_t uid, gid_t gid)
Colin Crosscf8d1c22014-06-03 13:24:21 -0700370{
Elliott Hughes171df0a2016-08-02 13:30:30 -0700371 if (mkdir(path, mode) == -1 && errno != EEXIST) {
372 return false;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700373 }
374
Elliott Hughes171df0a2016-08-02 13:30:30 -0700375 if (chown(path, uid, gid) == -1) {
376 int saved_errno = errno;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700377 rmdir(path);
Elliott Hughes171df0a2016-08-02 13:30:30 -0700378 errno = saved_errno;
379 return false;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700380 }
381
Elliott Hughes171df0a2016-08-02 13:30:30 -0700382 return true;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700383}
384
385int createProcessGroup(uid_t uid, int initialPid)
386{
387 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
Colin Crosscf8d1c22014-06-03 13:24:21 -0700388
389 convertUidToPath(path, sizeof(path), uid);
390
Elliott Hughes171df0a2016-08-02 13:30:30 -0700391 if (!mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM)) {
Tom Cherry20514c42017-04-21 13:48:49 -0700392 PLOG(ERROR) << "Failed to make and chown " << path;
Elliott Hughes171df0a2016-08-02 13:30:30 -0700393 return -errno;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700394 }
395
396 convertUidPidToPath(path, sizeof(path), uid, initialPid);
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 strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path));
404
405 int fd = open(path, O_WRONLY);
Elliott Hughes171df0a2016-08-02 13:30:30 -0700406 if (fd == -1) {
407 int ret = -errno;
Tom Cherry20514c42017-04-21 13:48:49 -0700408 PLOG(ERROR) << "Failed to open " << path;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700409 return ret;
410 }
411
412 char pid[PROCESSGROUP_MAX_PID_LEN + 1] = {0};
413 int len = snprintf(pid, sizeof(pid), "%d", initialPid);
414
Elliott Hughes171df0a2016-08-02 13:30:30 -0700415 int ret = 0;
416 if (write(fd, pid, len) < 0) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700417 ret = -errno;
Tom Cherry20514c42017-04-21 13:48:49 -0700418 PLOG(ERROR) << "Failed to write '" << pid << "' to " << path;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700419 }
420
421 close(fd);
422 return ret;
423}