Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | 0badbd6 | 2014-12-29 12:24:25 -0800 | [diff] [blame] | 22 | #include <errno.h> |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 23 | #include <fcntl.h> |
Chih-Hung Hsieh | fcc8115 | 2014-11-20 17:05:15 -0800 | [diff] [blame] | 24 | #include <inttypes.h> |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <sys/types.h> |
Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 30 | |
| 31 | #include <chrono> |
James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 32 | #include <memory> |
Elliott Hughes | 8d532e4 | 2016-06-06 21:19:55 -0700 | [diff] [blame] | 33 | #include <mutex> |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 34 | |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 35 | #include <android-base/logging.h> |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 36 | #include <private/android_filesystem_config.h> |
| 37 | |
| 38 | #include <processgroup/processgroup.h> |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 39 | |
Martijn Coenen | 5bb91ab | 2016-03-18 15:28:31 +0100 | [diff] [blame] | 40 | // Uncomment line below use memory cgroups for keeping track of (forked) PIDs |
| 41 | // #define USE_MEMCG 1 |
| 42 | |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 43 | #define MEM_CGROUP_PATH "/dev/memcg/apps" |
Martijn Coenen | 623b56a | 2016-02-08 11:42:25 +0100 | [diff] [blame] | 44 | #define MEM_CGROUP_TASKS "/dev/memcg/apps/tasks" |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 45 | #define ACCT_CGROUP_PATH "/acct" |
| 46 | |
| 47 | #define PROCESSGROUP_UID_PREFIX "uid_" |
| 48 | #define PROCESSGROUP_PID_PREFIX "pid_" |
| 49 | #define PROCESSGROUP_CGROUP_PROCS_FILE "/cgroup.procs" |
| 50 | #define PROCESSGROUP_MAX_UID_LEN 11 |
| 51 | #define PROCESSGROUP_MAX_PID_LEN 11 |
| 52 | #define PROCESSGROUP_MAX_PATH_LEN \ |
| 53 | ((sizeof(MEM_CGROUP_PATH) > sizeof(ACCT_CGROUP_PATH) ? \ |
| 54 | sizeof(MEM_CGROUP_PATH) : sizeof(ACCT_CGROUP_PATH)) + \ |
| 55 | sizeof(PROCESSGROUP_UID_PREFIX) + 1 + \ |
| 56 | PROCESSGROUP_MAX_UID_LEN + \ |
| 57 | sizeof(PROCESSGROUP_PID_PREFIX) + 1 + \ |
| 58 | PROCESSGROUP_MAX_PID_LEN + \ |
| 59 | sizeof(PROCESSGROUP_CGROUP_PROCS_FILE) + \ |
| 60 | 1) |
| 61 | |
| 62 | std::once_flag init_path_flag; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 63 | |
| 64 | struct ctx { |
| 65 | bool initialized; |
| 66 | int fd; |
| 67 | char buf[128]; |
| 68 | char *buf_ptr; |
| 69 | size_t buf_len; |
| 70 | }; |
| 71 | |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 72 | static const char* getCgroupRootPath() { |
Martijn Coenen | 5bb91ab | 2016-03-18 15:28:31 +0100 | [diff] [blame] | 73 | #ifdef USE_MEMCG |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 74 | static const char* cgroup_root_path = NULL; |
| 75 | std::call_once(init_path_flag, [&]() { |
Martijn Coenen | 623b56a | 2016-02-08 11:42:25 +0100 | [diff] [blame] | 76 | // Check if mem cgroup is mounted, only then check for write-access to avoid |
| 77 | // SELinux denials |
| 78 | cgroup_root_path = access(MEM_CGROUP_TASKS, F_OK) || access(MEM_CGROUP_PATH, W_OK) ? |
| 79 | ACCT_CGROUP_PATH : MEM_CGROUP_PATH; |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 80 | }); |
| 81 | return cgroup_root_path; |
Martijn Coenen | 5bb91ab | 2016-03-18 15:28:31 +0100 | [diff] [blame] | 82 | #else |
| 83 | return ACCT_CGROUP_PATH; |
| 84 | #endif |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 87 | static int convertUidToPath(char *path, size_t size, uid_t uid) |
| 88 | { |
| 89 | return snprintf(path, size, "%s/%s%d", |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 90 | getCgroupRootPath(), |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 91 | PROCESSGROUP_UID_PREFIX, |
| 92 | uid); |
| 93 | } |
| 94 | |
| 95 | static int convertUidPidToPath(char *path, size_t size, uid_t uid, int pid) |
| 96 | { |
| 97 | return snprintf(path, size, "%s/%s%d/%s%d", |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 98 | getCgroupRootPath(), |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 99 | PROCESSGROUP_UID_PREFIX, |
| 100 | uid, |
| 101 | PROCESSGROUP_PID_PREFIX, |
| 102 | pid); |
| 103 | } |
| 104 | |
| 105 | static int initCtx(uid_t uid, int pid, struct ctx *ctx) |
| 106 | { |
| 107 | int ret; |
| 108 | char path[PROCESSGROUP_MAX_PATH_LEN] = {0}; |
| 109 | convertUidPidToPath(path, sizeof(path), uid, pid); |
| 110 | strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path)); |
| 111 | |
| 112 | int fd = open(path, O_RDONLY); |
| 113 | if (fd < 0) { |
| 114 | ret = -errno; |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 115 | PLOG(WARNING) << "failed to open " << path; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 116 | return ret; |
| 117 | } |
| 118 | |
| 119 | ctx->fd = fd; |
| 120 | ctx->buf_ptr = ctx->buf; |
| 121 | ctx->buf_len = 0; |
| 122 | ctx->initialized = true; |
| 123 | |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 124 | LOG(VERBOSE) << "Initialized context for " << path; |
Dianne Hackborn | 2c5e7e1 | 2014-10-13 17:45:22 -0700 | [diff] [blame] | 125 | |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static int refillBuffer(struct ctx *ctx) |
| 130 | { |
| 131 | memmove(ctx->buf, ctx->buf_ptr, ctx->buf_len); |
| 132 | ctx->buf_ptr = ctx->buf; |
| 133 | |
| 134 | ssize_t ret = read(ctx->fd, ctx->buf_ptr + ctx->buf_len, |
Dianne Hackborn | 2c5e7e1 | 2014-10-13 17:45:22 -0700 | [diff] [blame] | 135 | sizeof(ctx->buf) - ctx->buf_len - 1); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 136 | if (ret < 0) { |
| 137 | return -errno; |
| 138 | } else if (ret == 0) { |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | ctx->buf_len += ret; |
Dianne Hackborn | 67f46cb | 2014-10-15 11:36:28 -0700 | [diff] [blame] | 143 | ctx->buf[ctx->buf_len] = 0; |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 144 | LOG(VERBOSE) << "Read " << ret << " to buffer: " << ctx->buf; |
Dianne Hackborn | 2c5e7e1 | 2014-10-13 17:45:22 -0700 | [diff] [blame] | 145 | |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 146 | assert(ctx->buf_len <= sizeof(ctx->buf)); |
| 147 | |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | static pid_t getOneAppProcess(uid_t uid, int appProcessPid, struct ctx *ctx) |
| 152 | { |
| 153 | if (!ctx->initialized) { |
| 154 | int ret = initCtx(uid, appProcessPid, ctx); |
| 155 | if (ret < 0) { |
| 156 | return ret; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | char *eptr; |
| 161 | while ((eptr = (char *)memchr(ctx->buf_ptr, '\n', ctx->buf_len)) == NULL) { |
| 162 | int ret = refillBuffer(ctx); |
| 163 | if (ret == 0) { |
| 164 | return -ERANGE; |
| 165 | } |
| 166 | if (ret < 0) { |
| 167 | return ret; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | *eptr = '\0'; |
| 172 | char *pid_eptr = NULL; |
| 173 | errno = 0; |
| 174 | long pid = strtol(ctx->buf_ptr, &pid_eptr, 10); |
| 175 | if (errno != 0) { |
| 176 | return -errno; |
| 177 | } |
| 178 | if (pid_eptr != eptr) { |
| 179 | return -EINVAL; |
| 180 | } |
| 181 | |
Dianne Hackborn | 2c5e7e1 | 2014-10-13 17:45:22 -0700 | [diff] [blame] | 182 | ctx->buf_len -= (eptr - ctx->buf_ptr) + 1; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 183 | ctx->buf_ptr = eptr + 1; |
| 184 | |
| 185 | return (pid_t)pid; |
| 186 | } |
| 187 | |
| 188 | static int removeProcessGroup(uid_t uid, int pid) |
| 189 | { |
| 190 | int ret; |
| 191 | char path[PROCESSGROUP_MAX_PATH_LEN] = {0}; |
| 192 | |
| 193 | convertUidPidToPath(path, sizeof(path), uid, pid); |
| 194 | ret = rmdir(path); |
| 195 | |
| 196 | convertUidToPath(path, sizeof(path), uid); |
| 197 | rmdir(path); |
| 198 | |
| 199 | return ret; |
| 200 | } |
| 201 | |
| 202 | static void removeUidProcessGroups(const char *uid_path) |
| 203 | { |
James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 204 | std::unique_ptr<DIR, decltype(&closedir)> uid(opendir(uid_path), closedir); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 205 | if (uid != NULL) { |
Elliott Hughes | 9f20693 | 2016-09-28 13:29:54 -0700 | [diff] [blame] | 206 | dirent* dir; |
| 207 | while ((dir = readdir(uid.get())) != nullptr) { |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 208 | char path[PROCESSGROUP_MAX_PATH_LEN]; |
| 209 | |
| 210 | if (dir->d_type != DT_DIR) { |
| 211 | continue; |
| 212 | } |
| 213 | |
| 214 | if (strncmp(dir->d_name, PROCESSGROUP_PID_PREFIX, strlen(PROCESSGROUP_PID_PREFIX))) { |
| 215 | continue; |
| 216 | } |
| 217 | |
| 218 | snprintf(path, sizeof(path), "%s/%s", uid_path, dir->d_name); |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 219 | LOG(VERBOSE) << "removing " << path; |
| 220 | if (rmdir(path) == -1) PLOG(WARNING) << "failed to remove " << path; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | void removeAllProcessGroups() |
| 226 | { |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 227 | LOG(VERBOSE) << "removeAllProcessGroups()"; |
Elliott Hughes | b6e1d15 | 2016-08-03 13:29:04 -0700 | [diff] [blame] | 228 | const char* cgroup_root_path = getCgroupRootPath(); |
James Hawkins | 22b6f7a | 2016-02-19 11:10:30 -0800 | [diff] [blame] | 229 | std::unique_ptr<DIR, decltype(&closedir)> root(opendir(cgroup_root_path), closedir); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 230 | if (root == NULL) { |
Elliott Hughes | b6e1d15 | 2016-08-03 13:29:04 -0700 | [diff] [blame] | 231 | PLOG(ERROR) << "failed to open " << cgroup_root_path; |
Colin Cross | c15dd04 | 2014-08-20 14:11:13 -0700 | [diff] [blame] | 232 | } else { |
Elliott Hughes | 9f20693 | 2016-09-28 13:29:54 -0700 | [diff] [blame] | 233 | dirent* dir; |
| 234 | while ((dir = readdir(root.get())) != nullptr) { |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 235 | char path[PROCESSGROUP_MAX_PATH_LEN]; |
| 236 | |
| 237 | if (dir->d_type != DT_DIR) { |
| 238 | continue; |
| 239 | } |
| 240 | if (strncmp(dir->d_name, PROCESSGROUP_UID_PREFIX, strlen(PROCESSGROUP_UID_PREFIX))) { |
| 241 | continue; |
| 242 | } |
| 243 | |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 244 | snprintf(path, sizeof(path), "%s/%s", cgroup_root_path, dir->d_name); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 245 | removeUidProcessGroups(path); |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 246 | LOG(VERBOSE) << "removing " << path; |
| 247 | if (rmdir(path) == -1) PLOG(WARNING) << "failed to remove " << path; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 248 | } |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
| 252 | static int killProcessGroupOnce(uid_t uid, int initialPid, int signal) |
| 253 | { |
| 254 | int processes = 0; |
| 255 | struct ctx ctx; |
| 256 | pid_t pid; |
| 257 | |
| 258 | ctx.initialized = false; |
| 259 | |
| 260 | while ((pid = getOneAppProcess(uid, initialPid, &ctx)) >= 0) { |
| 261 | processes++; |
Dianne Hackborn | 2c5e7e1 | 2014-10-13 17:45:22 -0700 | [diff] [blame] | 262 | if (pid == 0) { |
| 263 | // Should never happen... but if it does, trying to kill this |
| 264 | // will boomerang right back and kill us! Let's not let that happen. |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 265 | LOG(WARNING) << "Yikes, we've been told to kill pid 0! How about we don't do that?"; |
Dianne Hackborn | 2c5e7e1 | 2014-10-13 17:45:22 -0700 | [diff] [blame] | 266 | continue; |
| 267 | } |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 268 | LOG(VERBOSE) << "Killing pid " << pid << " in uid " << uid |
| 269 | << " as part of process group " << initialPid; |
| 270 | if (kill(pid, signal) == -1) { |
| 271 | PLOG(WARNING) << "kill(" << pid << ", " << signal << ") failed"; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
| 275 | if (ctx.initialized) { |
| 276 | close(ctx.fd); |
| 277 | } |
| 278 | |
| 279 | return processes; |
| 280 | } |
| 281 | |
| 282 | int killProcessGroup(uid_t uid, int initialPid, int signal) |
| 283 | { |
Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 284 | std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 285 | |
Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 286 | int retry = 40; |
| 287 | int processes; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 288 | while ((processes = killProcessGroupOnce(uid, initialPid, signal)) > 0) { |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 289 | LOG(VERBOSE) << "killed " << processes << " processes for processgroup " << initialPid; |
Yusuke Sato | d503930 | 2015-06-16 13:51:14 -0700 | [diff] [blame] | 290 | if (retry > 0) { |
Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 291 | usleep(5 * 1000); // 5ms |
Yusuke Sato | d503930 | 2015-06-16 13:51:14 -0700 | [diff] [blame] | 292 | --retry; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 293 | } else { |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 294 | LOG(ERROR) << "failed to kill " << processes << " processes for processgroup " |
| 295 | << initialPid; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 296 | break; |
| 297 | } |
| 298 | } |
| 299 | |
Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 300 | std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); |
| 301 | |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 302 | auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); |
| 303 | LOG(VERBOSE) << "Killed process group uid " << uid << " pid " << initialPid << " in " |
| 304 | << static_cast<int>(ms) << "ms, " << processes << " procs remain"; |
Dianne Hackborn | 2c5e7e1 | 2014-10-13 17:45:22 -0700 | [diff] [blame] | 305 | |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 306 | if (processes == 0) { |
| 307 | return removeProcessGroup(uid, initialPid); |
| 308 | } else { |
| 309 | return -1; |
| 310 | } |
| 311 | } |
| 312 | |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 313 | static bool mkdirAndChown(const char *path, mode_t mode, uid_t uid, gid_t gid) |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 314 | { |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 315 | if (mkdir(path, mode) == -1 && errno != EEXIST) { |
| 316 | return false; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 319 | if (chown(path, uid, gid) == -1) { |
| 320 | int saved_errno = errno; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 321 | rmdir(path); |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 322 | errno = saved_errno; |
| 323 | return false; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 326 | return true; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | int createProcessGroup(uid_t uid, int initialPid) |
| 330 | { |
| 331 | char path[PROCESSGROUP_MAX_PATH_LEN] = {0}; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 332 | |
| 333 | convertUidToPath(path, sizeof(path), uid); |
| 334 | |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 335 | if (!mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM)) { |
| 336 | PLOG(ERROR) << "failed to make and chown " << path; |
| 337 | return -errno; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | convertUidPidToPath(path, sizeof(path), uid, initialPid); |
| 341 | |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 342 | if (!mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM)) { |
| 343 | PLOG(ERROR) << "failed to make and chown " << path; |
| 344 | return -errno; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path)); |
| 348 | |
| 349 | int fd = open(path, O_WRONLY); |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 350 | if (fd == -1) { |
| 351 | int ret = -errno; |
| 352 | PLOG(ERROR) << "failed to open " << path; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 353 | return ret; |
| 354 | } |
| 355 | |
| 356 | char pid[PROCESSGROUP_MAX_PID_LEN + 1] = {0}; |
| 357 | int len = snprintf(pid, sizeof(pid), "%d", initialPid); |
| 358 | |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 359 | int ret = 0; |
| 360 | if (write(fd, pid, len) < 0) { |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 361 | ret = -errno; |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 362 | PLOG(ERROR) << "failed to write '" << pid << "' to " << path; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | close(fd); |
| 366 | return ret; |
| 367 | } |