• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <inttypes.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31 
32 #include <chrono>
33 #include <map>
34 #include <memory>
35 #include <mutex>
36 #include <set>
37 #include <string>
38 #include <thread>
39 
40 #include <android-base/file.h>
41 #include <android-base/logging.h>
42 #include <android-base/properties.h>
43 #include <android-base/stringprintf.h>
44 #include <android-base/strings.h>
45 #include <cutils/android_filesystem_config.h>
46 #include <processgroup/processgroup.h>
47 #include <task_profiles.h>
48 
49 using android::base::GetBoolProperty;
50 using android::base::StartsWith;
51 using android::base::StringPrintf;
52 using android::base::WriteStringToFile;
53 
54 using namespace std::chrono_literals;
55 
56 #define PROCESSGROUP_CGROUP_PROCS_FILE "/cgroup.procs"
57 
CgroupGetControllerPath(const std::string & cgroup_name,std::string * path)58 bool CgroupGetControllerPath(const std::string& cgroup_name, std::string* path) {
59     auto controller = CgroupMap::GetInstance().FindController(cgroup_name);
60 
61     if (!controller.HasValue()) {
62         return false;
63     }
64 
65     if (path) {
66         *path = controller.path();
67     }
68 
69     return true;
70 }
71 
CgroupGetAttributePath(const std::string & attr_name,std::string * path)72 bool CgroupGetAttributePath(const std::string& attr_name, std::string* path) {
73     const TaskProfiles& tp = TaskProfiles::GetInstance();
74     const ProfileAttribute* attr = tp.GetAttribute(attr_name);
75 
76     if (attr == nullptr) {
77         return false;
78     }
79 
80     if (path) {
81         *path = StringPrintf("%s/%s", attr->controller()->path(), attr->file_name().c_str());
82     }
83 
84     return true;
85 }
86 
CgroupGetAttributePathForTask(const std::string & attr_name,int tid,std::string * path)87 bool CgroupGetAttributePathForTask(const std::string& attr_name, int tid, std::string* path) {
88     const TaskProfiles& tp = TaskProfiles::GetInstance();
89     const ProfileAttribute* attr = tp.GetAttribute(attr_name);
90 
91     if (attr == nullptr) {
92         return false;
93     }
94 
95     if (!attr->GetPathForTask(tid, path)) {
96         PLOG(ERROR) << "Failed to find cgroup for tid " << tid;
97         return false;
98     }
99 
100     return true;
101 }
102 
UsePerAppMemcg()103 bool UsePerAppMemcg() {
104     bool low_ram_device = GetBoolProperty("ro.config.low_ram", false);
105     return GetBoolProperty("ro.config.per_app_memcg", low_ram_device);
106 }
107 
isMemoryCgroupSupported()108 static bool isMemoryCgroupSupported() {
109     static bool memcg_supported = CgroupMap::GetInstance().FindController("memory").IsUsable();
110 
111     return memcg_supported;
112 }
113 
DropTaskProfilesResourceCaching()114 void DropTaskProfilesResourceCaching() {
115     TaskProfiles::GetInstance().DropResourceCaching();
116 }
117 
SetProcessProfiles(uid_t uid,pid_t pid,const std::vector<std::string> & profiles)118 bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles) {
119     return TaskProfiles::GetInstance().SetProcessProfiles(uid, pid, profiles);
120 }
121 
SetTaskProfiles(int tid,const std::vector<std::string> & profiles,bool use_fd_cache)122 bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles, bool use_fd_cache) {
123     return TaskProfiles::GetInstance().SetTaskProfiles(tid, profiles, use_fd_cache);
124 }
125 
ConvertUidToPath(const char * cgroup,uid_t uid)126 static std::string ConvertUidToPath(const char* cgroup, uid_t uid) {
127     return StringPrintf("%s/uid_%d", cgroup, uid);
128 }
129 
ConvertUidPidToPath(const char * cgroup,uid_t uid,int pid)130 static std::string ConvertUidPidToPath(const char* cgroup, uid_t uid, int pid) {
131     return StringPrintf("%s/uid_%d/pid_%d", cgroup, uid, pid);
132 }
133 
RemoveProcessGroup(const char * cgroup,uid_t uid,int pid,unsigned int retries)134 static int RemoveProcessGroup(const char* cgroup, uid_t uid, int pid, unsigned int retries) {
135     int ret = 0;
136     auto uid_pid_path = ConvertUidPidToPath(cgroup, uid, pid);
137     auto uid_path = ConvertUidToPath(cgroup, uid);
138 
139     if (retries == 0) {
140         retries = 1;
141     }
142 
143     while (retries--) {
144         ret = rmdir(uid_pid_path.c_str());
145         if (!ret || errno != EBUSY) break;
146         std::this_thread::sleep_for(5ms);
147     }
148 
149     return ret;
150 }
151 
RemoveUidProcessGroups(const std::string & uid_path)152 static bool RemoveUidProcessGroups(const std::string& uid_path) {
153     std::unique_ptr<DIR, decltype(&closedir)> uid(opendir(uid_path.c_str()), closedir);
154     bool empty = true;
155     if (uid != NULL) {
156         dirent* dir;
157         while ((dir = readdir(uid.get())) != nullptr) {
158             if (dir->d_type != DT_DIR) {
159                 continue;
160             }
161 
162             if (!StartsWith(dir->d_name, "pid_")) {
163                 continue;
164             }
165 
166             auto path = StringPrintf("%s/%s", uid_path.c_str(), dir->d_name);
167             LOG(VERBOSE) << "Removing " << path;
168             if (rmdir(path.c_str()) == -1) {
169                 if (errno != EBUSY) {
170                     PLOG(WARNING) << "Failed to remove " << path;
171                 }
172                 empty = false;
173             }
174         }
175     }
176     return empty;
177 }
178 
removeAllProcessGroups()179 void removeAllProcessGroups() {
180     LOG(VERBOSE) << "removeAllProcessGroups()";
181 
182     std::vector<std::string> cgroups;
183     std::string path;
184 
185     if (CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &path)) {
186         cgroups.push_back(path);
187     }
188     if (CgroupGetControllerPath("memory", &path)) {
189         cgroups.push_back(path + "/apps");
190     }
191 
192     for (std::string cgroup_root_path : cgroups) {
193         std::unique_ptr<DIR, decltype(&closedir)> root(opendir(cgroup_root_path.c_str()), closedir);
194         if (root == NULL) {
195             PLOG(ERROR) << "Failed to open " << cgroup_root_path;
196         } else {
197             dirent* dir;
198             while ((dir = readdir(root.get())) != nullptr) {
199                 if (dir->d_type != DT_DIR) {
200                     continue;
201                 }
202 
203                 if (!StartsWith(dir->d_name, "uid_")) {
204                     continue;
205                 }
206 
207                 auto path = StringPrintf("%s/%s", cgroup_root_path.c_str(), dir->d_name);
208                 if (!RemoveUidProcessGroups(path)) {
209                     LOG(VERBOSE) << "Skip removing " << path;
210                     continue;
211                 }
212                 LOG(VERBOSE) << "Removing " << path;
213                 if (rmdir(path.c_str()) == -1 && errno != EBUSY) {
214                     PLOG(WARNING) << "Failed to remove " << path;
215                 }
216             }
217         }
218     }
219 }
220 
221 /**
222  * Process groups are primarily created by the Zygote, meaning that uid/pid groups are created by
223  * the user root. Ownership for the newly created cgroup and all of its files must thus be
224  * transferred for the user/group passed as uid/gid before system_server can properly access them.
225  */
MkdirAndChown(const std::string & path,mode_t mode,uid_t uid,gid_t gid)226 static bool MkdirAndChown(const std::string& path, mode_t mode, uid_t uid, gid_t gid) {
227     if (mkdir(path.c_str(), mode) == -1) {
228         if (errno == EEXIST) {
229             // Directory already exists and permissions have been set at the time it was created
230             return true;
231         }
232         return false;
233     }
234 
235     auto dir = std::unique_ptr<DIR, decltype(&closedir)>(opendir(path.c_str()), closedir);
236 
237     if (dir == NULL) {
238         PLOG(ERROR) << "opendir failed for " << path;
239         goto err;
240     }
241 
242     struct dirent* dir_entry;
243     while ((dir_entry = readdir(dir.get()))) {
244         if (!strcmp("..", dir_entry->d_name)) {
245             continue;
246         }
247 
248         std::string file_path = path + "/" + dir_entry->d_name;
249 
250         if (lchown(file_path.c_str(), uid, gid) < 0) {
251             PLOG(ERROR) << "lchown failed for " << file_path;
252             goto err;
253         }
254 
255         if (fchmodat(AT_FDCWD, file_path.c_str(), mode, AT_SYMLINK_NOFOLLOW) != 0) {
256             PLOG(ERROR) << "fchmodat failed for " << file_path;
257             goto err;
258         }
259     }
260 
261     return true;
262 err:
263     int saved_errno = errno;
264     rmdir(path.c_str());
265     errno = saved_errno;
266 
267     return false;
268 }
269 
270 // Returns number of processes killed on success
271 // Returns 0 if there are no processes in the process cgroup left to kill
272 // Returns -1 on error
DoKillProcessGroupOnce(const char * cgroup,uid_t uid,int initialPid,int signal)273 static int DoKillProcessGroupOnce(const char* cgroup, uid_t uid, int initialPid, int signal) {
274     auto path = ConvertUidPidToPath(cgroup, uid, initialPid) + PROCESSGROUP_CGROUP_PROCS_FILE;
275     std::unique_ptr<FILE, decltype(&fclose)> fd(fopen(path.c_str(), "re"), fclose);
276     if (!fd) {
277         if (errno == ENOENT) {
278             // This happens when process is already dead
279             return 0;
280         }
281         PLOG(WARNING) << "Failed to open process cgroup uid " << uid << " pid " << initialPid;
282         return -1;
283     }
284 
285     // We separate all of the pids in the cgroup into those pids that are also the leaders of
286     // process groups (stored in the pgids set) and those that are not (stored in the pids set).
287     std::set<pid_t> pgids;
288     pgids.emplace(initialPid);
289     std::set<pid_t> pids;
290 
291     pid_t pid;
292     int processes = 0;
293     while (fscanf(fd.get(), "%d\n", &pid) == 1 && pid >= 0) {
294         processes++;
295         if (pid == 0) {
296             // Should never happen...  but if it does, trying to kill this
297             // will boomerang right back and kill us!  Let's not let that happen.
298             LOG(WARNING) << "Yikes, we've been told to kill pid 0!  How about we don't do that?";
299             continue;
300         }
301         pid_t pgid = getpgid(pid);
302         if (pgid == -1) PLOG(ERROR) << "getpgid(" << pid << ") failed";
303         if (pgid == pid) {
304             pgids.emplace(pid);
305         } else {
306             pids.emplace(pid);
307         }
308     }
309 
310     // Erase all pids that will be killed when we kill the process groups.
311     for (auto it = pids.begin(); it != pids.end();) {
312         pid_t pgid = getpgid(*it);
313         if (pgids.count(pgid) == 1) {
314             it = pids.erase(it);
315         } else {
316             ++it;
317         }
318     }
319 
320     // Kill all process groups.
321     for (const auto pgid : pgids) {
322         LOG(VERBOSE) << "Killing process group " << -pgid << " in uid " << uid
323                      << " as part of process cgroup " << initialPid;
324 
325         if (kill(-pgid, signal) == -1 && errno != ESRCH) {
326             PLOG(WARNING) << "kill(" << -pgid << ", " << signal << ") failed";
327         }
328     }
329 
330     // Kill remaining pids.
331     for (const auto pid : pids) {
332         LOG(VERBOSE) << "Killing pid " << pid << " in uid " << uid << " as part of process cgroup "
333                      << initialPid;
334 
335         if (kill(pid, signal) == -1 && errno != ESRCH) {
336             PLOG(WARNING) << "kill(" << pid << ", " << signal << ") failed";
337         }
338     }
339 
340     return feof(fd.get()) ? processes : -1;
341 }
342 
KillProcessGroup(uid_t uid,int initialPid,int signal,int retries,int * max_processes)343 static int KillProcessGroup(uid_t uid, int initialPid, int signal, int retries,
344                             int* max_processes) {
345     std::string hierarchy_root_path;
346     CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &hierarchy_root_path);
347     const char* cgroup = hierarchy_root_path.c_str();
348 
349     std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
350 
351     if (max_processes != nullptr) {
352         *max_processes = 0;
353     }
354 
355     int retry = retries;
356     int processes;
357     while ((processes = DoKillProcessGroupOnce(cgroup, uid, initialPid, signal)) > 0) {
358         if (max_processes != nullptr && processes > *max_processes) {
359             *max_processes = processes;
360         }
361         LOG(VERBOSE) << "Killed " << processes << " processes for processgroup " << initialPid;
362         if (retry > 0) {
363             std::this_thread::sleep_for(5ms);
364             --retry;
365         } else {
366             break;
367         }
368     }
369 
370     if (processes < 0) {
371         PLOG(ERROR) << "Error encountered killing process cgroup uid " << uid << " pid "
372                     << initialPid;
373         return -1;
374     }
375 
376     std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
377     auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
378 
379     // We only calculate the number of 'processes' when killing the processes.
380     // In the retries == 0 case, we only kill the processes once and therefore
381     // will not have waited then recalculated how many processes are remaining
382     // after the first signals have been sent.
383     // Logging anything regarding the number of 'processes' here does not make sense.
384 
385     if (processes == 0) {
386         if (retries > 0) {
387             LOG(INFO) << "Successfully killed process cgroup uid " << uid << " pid " << initialPid
388                       << " in " << static_cast<int>(ms) << "ms";
389         }
390 
391         int err = RemoveProcessGroup(cgroup, uid, initialPid, retries);
392 
393         if (isMemoryCgroupSupported() && UsePerAppMemcg()) {
394             std::string memory_path;
395             CgroupGetControllerPath("memory", &memory_path);
396             memory_path += "/apps";
397             if (RemoveProcessGroup(memory_path.c_str(), uid, initialPid, retries)) return -1;
398         }
399 
400         return err;
401     } else {
402         if (retries > 0) {
403             LOG(ERROR) << "Failed to kill process cgroup uid " << uid << " pid " << initialPid
404                        << " in " << static_cast<int>(ms) << "ms, " << processes
405                        << " processes remain";
406         }
407         return -1;
408     }
409 }
410 
killProcessGroup(uid_t uid,int initialPid,int signal,int * max_processes)411 int killProcessGroup(uid_t uid, int initialPid, int signal, int* max_processes) {
412     return KillProcessGroup(uid, initialPid, signal, 40 /*retries*/, max_processes);
413 }
414 
killProcessGroupOnce(uid_t uid,int initialPid,int signal,int * max_processes)415 int killProcessGroupOnce(uid_t uid, int initialPid, int signal, int* max_processes) {
416     return KillProcessGroup(uid, initialPid, signal, 0 /*retries*/, max_processes);
417 }
418 
createProcessGroupInternal(uid_t uid,int initialPid,std::string cgroup)419 static int createProcessGroupInternal(uid_t uid, int initialPid, std::string cgroup) {
420     auto uid_path = ConvertUidToPath(cgroup.c_str(), uid);
421 
422     struct stat cgroup_stat;
423     mode_t cgroup_mode = 0750;
424     gid_t cgroup_uid = AID_SYSTEM;
425     uid_t cgroup_gid = AID_SYSTEM;
426 
427     if (stat(cgroup.c_str(), &cgroup_stat) == 1) {
428         PLOG(ERROR) << "Failed to get stats for " << cgroup;
429     } else {
430         cgroup_mode = cgroup_stat.st_mode;
431         cgroup_uid = cgroup_stat.st_uid;
432         cgroup_gid = cgroup_stat.st_gid;
433     }
434 
435     if (!MkdirAndChown(uid_path, cgroup_mode, cgroup_uid, cgroup_gid)) {
436         PLOG(ERROR) << "Failed to make and chown " << uid_path;
437         return -errno;
438     }
439 
440     auto uid_pid_path = ConvertUidPidToPath(cgroup.c_str(), uid, initialPid);
441 
442     if (!MkdirAndChown(uid_pid_path, cgroup_mode, cgroup_uid, cgroup_gid)) {
443         PLOG(ERROR) << "Failed to make and chown " << uid_pid_path;
444         return -errno;
445     }
446 
447     auto uid_pid_procs_file = uid_pid_path + PROCESSGROUP_CGROUP_PROCS_FILE;
448 
449     int ret = 0;
450     if (!WriteStringToFile(std::to_string(initialPid), uid_pid_procs_file)) {
451         ret = -errno;
452         PLOG(ERROR) << "Failed to write '" << initialPid << "' to " << uid_pid_procs_file;
453     }
454 
455     return ret;
456 }
457 
createProcessGroup(uid_t uid,int initialPid,bool memControl)458 int createProcessGroup(uid_t uid, int initialPid, bool memControl) {
459     std::string cgroup;
460 
461     if (memControl && !UsePerAppMemcg()) {
462         PLOG(ERROR) << "service memory controls are used without per-process memory cgroup support";
463         return -EINVAL;
464     }
465 
466     if (isMemoryCgroupSupported() && UsePerAppMemcg()) {
467         CgroupGetControllerPath("memory", &cgroup);
468         cgroup += "/apps";
469         int ret = createProcessGroupInternal(uid, initialPid, cgroup);
470         if (ret != 0) {
471             return ret;
472         }
473     }
474 
475     CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &cgroup);
476     return createProcessGroupInternal(uid, initialPid, cgroup);
477 }
478 
SetProcessGroupValue(int tid,const std::string & attr_name,int64_t value)479 static bool SetProcessGroupValue(int tid, const std::string& attr_name, int64_t value) {
480     if (!isMemoryCgroupSupported()) {
481         PLOG(ERROR) << "Memcg is not mounted.";
482         return false;
483     }
484 
485     std::string path;
486     if (!CgroupGetAttributePathForTask(attr_name, tid, &path)) {
487         PLOG(ERROR) << "Failed to find attribute '" << attr_name << "'";
488         return false;
489     }
490 
491     if (!WriteStringToFile(std::to_string(value), path)) {
492         PLOG(ERROR) << "Failed to write '" << value << "' to " << path;
493         return false;
494     }
495     return true;
496 }
497 
setProcessGroupSwappiness(uid_t,int pid,int swappiness)498 bool setProcessGroupSwappiness(uid_t, int pid, int swappiness) {
499     return SetProcessGroupValue(pid, "MemSwappiness", swappiness);
500 }
501 
setProcessGroupSoftLimit(uid_t,int pid,int64_t soft_limit_in_bytes)502 bool setProcessGroupSoftLimit(uid_t, int pid, int64_t soft_limit_in_bytes) {
503     return SetProcessGroupValue(pid, "MemSoftLimit", soft_limit_in_bytes);
504 }
505 
setProcessGroupLimit(uid_t,int pid,int64_t limit_in_bytes)506 bool setProcessGroupLimit(uid_t, int pid, int64_t limit_in_bytes) {
507     return SetProcessGroupValue(pid, "MemLimit", limit_in_bytes);
508 }
509 
getAttributePathForTask(const std::string & attr_name,int tid,std::string * path)510 bool getAttributePathForTask(const std::string& attr_name, int tid, std::string* path) {
511     return CgroupGetAttributePathForTask(attr_name, tid, path);
512 }
513