• 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 <poll.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <unistd.h>
32 
33 #include <chrono>
34 #include <cstring>
35 #include <map>
36 #include <memory>
37 #include <mutex>
38 #include <set>
39 #include <string>
40 #include <string_view>
41 #include <thread>
42 
43 #include <android-base/file.h>
44 #include <android-base/logging.h>
45 #include <android-base/properties.h>
46 #include <android-base/stringprintf.h>
47 #include <cutils/android_filesystem_config.h>
48 #include <processgroup/processgroup.h>
49 #include <task_profiles.h>
50 
51 using android::base::GetBoolProperty;
52 using android::base::StringPrintf;
53 using android::base::WriteStringToFile;
54 
55 using namespace std::chrono_literals;
56 
57 #define PROCESSGROUP_CGROUP_PROCS_FILE "cgroup.procs"
58 #define PROCESSGROUP_CGROUP_KILL_FILE "cgroup.kill"
59 #define PROCESSGROUP_CGROUP_EVENTS_FILE "cgroup.events"
60 
CgroupsAvailable()61 bool CgroupsAvailable() {
62     static bool cgroups_available = access("/proc/cgroups", F_OK) == 0;
63     return cgroups_available;
64 }
65 
CgroupGetControllerPath(const std::string & cgroup_name,std::string * path)66 bool CgroupGetControllerPath(const std::string& cgroup_name, std::string* path) {
67     auto controller = CgroupMap::GetInstance().FindController(cgroup_name);
68 
69     if (!controller.HasValue()) {
70         return false;
71     }
72 
73     if (path) {
74         *path = controller.path();
75     }
76 
77     return true;
78 }
79 
CgroupKillAvailable()80 static bool CgroupKillAvailable() {
81     static std::once_flag f;
82     static bool cgroup_kill_available = false;
83     std::call_once(f, []() {
84         std::string cg_kill;
85         CgroupGetControllerPath(CGROUPV2_HIERARCHY_NAME, &cg_kill);
86         // cgroup.kill is not on the root cgroup, so check a non-root cgroup that should always
87         // exist
88         cg_kill = ConvertUidToPath(cg_kill.c_str(), AID_ROOT, true) + '/' +
89             PROCESSGROUP_CGROUP_KILL_FILE;
90         cgroup_kill_available = access(cg_kill.c_str(), F_OK) == 0;
91     });
92 
93     return cgroup_kill_available;
94 }
95 
CgroupGetMemcgAppsPath(std::string * path)96 static bool CgroupGetMemcgAppsPath(std::string* path) {
97     CgroupControllerWrapper controller = CgroupMap::GetInstance().FindController("memory");
98 
99     if (!controller.HasValue()) {
100         return false;
101     }
102 
103     if (path) {
104         *path = controller.path();
105         if (controller.version() == 1) {
106             *path += "/apps";
107         }
108     }
109 
110     return true;
111 }
112 
CgroupGetControllerFromPath(const std::string & path,std::string * cgroup_name)113 bool CgroupGetControllerFromPath(const std::string& path, std::string* cgroup_name) {
114     auto controller = CgroupMap::GetInstance().FindControllerByPath(path);
115 
116     if (!controller.HasValue()) {
117         return false;
118     }
119 
120     if (cgroup_name) {
121         *cgroup_name = controller.name();
122     }
123 
124     return true;
125 }
126 
CgroupGetAttributePath(const std::string & attr_name,std::string * path)127 bool CgroupGetAttributePath(const std::string& attr_name, std::string* path) {
128     const TaskProfiles& tp = TaskProfiles::GetInstance();
129     const IProfileAttribute* attr = tp.GetAttribute(attr_name);
130 
131     if (attr == nullptr) {
132         return false;
133     }
134 
135     if (path) {
136         *path = StringPrintf("%s/%s", attr->controller()->path(), attr->file_name().c_str());
137     }
138 
139     return true;
140 }
141 
CgroupGetAttributePathForTask(const std::string & attr_name,pid_t tid,std::string * path)142 bool CgroupGetAttributePathForTask(const std::string& attr_name, pid_t tid, std::string* path) {
143     const TaskProfiles& tp = TaskProfiles::GetInstance();
144     const IProfileAttribute* attr = tp.GetAttribute(attr_name);
145 
146     if (attr == nullptr) {
147         return false;
148     }
149 
150     if (!attr->GetPathForTask(tid, path)) {
151         LOG(ERROR) << "Failed to find cgroup for tid " << tid;
152         return false;
153     }
154 
155     return true;
156 }
157 
CgroupGetAttributePathForProcess(std::string_view attr_name,uid_t uid,pid_t pid,std::string & path)158 bool CgroupGetAttributePathForProcess(std::string_view attr_name, uid_t uid, pid_t pid,
159                                       std::string &path) {
160     const TaskProfiles& tp = TaskProfiles::GetInstance();
161     const IProfileAttribute* attr = tp.GetAttribute(attr_name);
162 
163     if (attr == nullptr) {
164         return false;
165     }
166 
167     if (!attr->GetPathForProcess(uid, pid, &path)) {
168         LOG(ERROR) << "Failed to find cgroup for uid " << uid << " pid " << pid;
169         return false;
170     }
171 
172     return true;
173 }
174 
UsePerAppMemcg()175 bool UsePerAppMemcg() {
176     bool low_ram_device = GetBoolProperty("ro.config.low_ram", false);
177     return GetBoolProperty("ro.config.per_app_memcg", low_ram_device);
178 }
179 
isMemoryCgroupSupported()180 static bool isMemoryCgroupSupported() {
181     static bool memcg_supported = CgroupMap::GetInstance().FindController("memory").IsUsable();
182 
183     return memcg_supported;
184 }
185 
DropTaskProfilesResourceCaching()186 void DropTaskProfilesResourceCaching() {
187     TaskProfiles::GetInstance().DropResourceCaching(ProfileAction::RCT_TASK);
188     TaskProfiles::GetInstance().DropResourceCaching(ProfileAction::RCT_PROCESS);
189 }
190 
SetProcessProfiles(uid_t uid,pid_t pid,const std::vector<std::string> & profiles)191 bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles) {
192     return TaskProfiles::GetInstance().SetProcessProfiles(
193             uid, pid, std::span<const std::string>(profiles), false);
194 }
195 
SetProcessProfiles(uid_t uid,pid_t pid,std::initializer_list<std::string_view> profiles)196 bool SetProcessProfiles(uid_t uid, pid_t pid, std::initializer_list<std::string_view> profiles) {
197     return TaskProfiles::GetInstance().SetProcessProfiles(
198             uid, pid, std::span<const std::string_view>(profiles), false);
199 }
200 
SetProcessProfiles(uid_t uid,pid_t pid,std::span<const std::string_view> profiles)201 bool SetProcessProfiles(uid_t uid, pid_t pid, std::span<const std::string_view> profiles) {
202     return TaskProfiles::GetInstance().SetProcessProfiles(uid, pid, profiles, false);
203 }
204 
SetProcessProfilesCached(uid_t uid,pid_t pid,const std::vector<std::string> & profiles)205 bool SetProcessProfilesCached(uid_t uid, pid_t pid, const std::vector<std::string>& profiles) {
206     return TaskProfiles::GetInstance().SetProcessProfiles(
207             uid, pid, std::span<const std::string>(profiles), true);
208 }
209 
SetTaskProfiles(pid_t tid,const std::vector<std::string> & profiles,bool use_fd_cache)210 bool SetTaskProfiles(pid_t tid, const std::vector<std::string>& profiles, bool use_fd_cache) {
211     return TaskProfiles::GetInstance().SetTaskProfiles(tid, std::span<const std::string>(profiles),
212                                                        use_fd_cache);
213 }
214 
SetTaskProfiles(pid_t tid,std::initializer_list<std::string_view> profiles,bool use_fd_cache)215 bool SetTaskProfiles(pid_t tid, std::initializer_list<std::string_view> profiles,
216                      bool use_fd_cache) {
217     return TaskProfiles::GetInstance().SetTaskProfiles(
218             tid, std::span<const std::string_view>(profiles), use_fd_cache);
219 }
220 
SetTaskProfiles(pid_t tid,std::span<const std::string_view> profiles,bool use_fd_cache)221 bool SetTaskProfiles(pid_t tid, std::span<const std::string_view> profiles, bool use_fd_cache) {
222     return TaskProfiles::GetInstance().SetTaskProfiles(tid, profiles, use_fd_cache);
223 }
224 
225 // C wrapper for SetProcessProfiles.
226 // No need to have this in the header file because this function is specifically for crosvm. Crosvm
227 // which is written in Rust has its own declaration of this foreign function and doesn't rely on the
228 // header. See
229 // https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3574427/5/src/linux/android.rs#12
android_set_process_profiles(uid_t uid,pid_t pid,size_t num_profiles,const char * profiles[])230 extern "C" bool android_set_process_profiles(uid_t uid, pid_t pid, size_t num_profiles,
231                                              const char* profiles[]) {
232     std::vector<std::string_view> profiles_;
233     profiles_.reserve(num_profiles);
234     for (size_t i = 0; i < num_profiles; i++) {
235         profiles_.emplace_back(profiles[i]);
236     }
237     return SetProcessProfiles(uid, pid, std::span<const std::string_view>(profiles_));
238 }
239 
SetUserProfiles(uid_t uid,const std::vector<std::string> & profiles)240 bool SetUserProfiles(uid_t uid, const std::vector<std::string>& profiles) {
241     return TaskProfiles::GetInstance().SetUserProfiles(uid, std::span<const std::string>(profiles),
242                                                        false);
243 }
244 
RemoveCgroup(const char * cgroup,uid_t uid,pid_t pid,bool v2_path)245 static int RemoveCgroup(const char* cgroup, uid_t uid, pid_t pid, bool v2_path) {
246     auto path = ConvertUidPidToPath(cgroup, uid, pid, v2_path);
247     int ret = TEMP_FAILURE_RETRY(rmdir(path.c_str()));
248 
249     if (!ret && uid >= AID_ISOLATED_START && uid <= AID_ISOLATED_END) {
250         // Isolated UIDs are unlikely to be reused soon after removal,
251         // so free up the kernel resources for the UID level cgroup.
252         path = ConvertUidToPath(cgroup, uid, v2_path);
253         ret = TEMP_FAILURE_RETRY(rmdir(path.c_str()));
254     }
255 
256     if (ret < 0 && errno == ENOENT) {
257         // This function is idempoetent, but still warn here.
258         LOG(WARNING) << "RemoveCgroup: " << path << " does not exist.";
259         ret = 0;
260     }
261 
262     return ret;
263 }
264 
RemoveEmptyUidCgroups(const std::string & uid_path)265 static bool RemoveEmptyUidCgroups(const std::string& uid_path) {
266     std::unique_ptr<DIR, decltype(&closedir)> uid(opendir(uid_path.c_str()), closedir);
267     bool empty = true;
268     if (uid != NULL) {
269         dirent* dir;
270         while ((dir = readdir(uid.get())) != nullptr) {
271             if (dir->d_type != DT_DIR) {
272                 continue;
273             }
274 
275             if (!std::string_view(dir->d_name).starts_with("pid_")) {
276                 continue;
277             }
278 
279             auto path = StringPrintf("%s/%s", uid_path.c_str(), dir->d_name);
280             LOG(VERBOSE) << "Removing " << path;
281             if (rmdir(path.c_str()) == -1) {
282                 if (errno != EBUSY) {
283                     PLOG(WARNING) << "Failed to remove " << path;
284                 }
285                 empty = false;
286             }
287         }
288     }
289     return empty;
290 }
291 
removeAllEmptyProcessGroups()292 void removeAllEmptyProcessGroups() {
293     LOG(VERBOSE) << "removeAllEmptyProcessGroups()";
294 
295     std::vector<std::string> cgroups;
296     std::string path, memcg_apps_path;
297 
298     if (CgroupGetControllerPath(CGROUPV2_HIERARCHY_NAME, &path)) {
299         cgroups.push_back(path);
300     }
301     if (CgroupGetMemcgAppsPath(&memcg_apps_path) && memcg_apps_path != path) {
302         cgroups.push_back(memcg_apps_path);
303     }
304 
305     for (std::string cgroup_root_path : cgroups) {
306         std::unique_ptr<DIR, decltype(&closedir)> root(opendir(cgroup_root_path.c_str()), closedir);
307         if (root == NULL) {
308             PLOG(ERROR) << __func__ << " failed to open " << cgroup_root_path;
309         } else {
310             dirent* dir;
311             while ((dir = readdir(root.get())) != nullptr) {
312                 if (dir->d_type != DT_DIR) {
313                     continue;
314                 }
315 
316                 if (!std::string_view(dir->d_name).starts_with("uid_")) {
317                     continue;
318                 }
319 
320                 auto path = StringPrintf("%s/%s", cgroup_root_path.c_str(), dir->d_name);
321                 if (!RemoveEmptyUidCgroups(path)) {
322                     LOG(VERBOSE) << "Skip removing " << path;
323                     continue;
324                 }
325                 LOG(VERBOSE) << "Removing " << path;
326                 if (rmdir(path.c_str()) == -1 && errno != EBUSY) {
327                     PLOG(WARNING) << "Failed to remove " << path;
328                 }
329             }
330         }
331     }
332 }
333 
334 /**
335  * Process groups are primarily created by the Zygote, meaning that uid/pid groups are created by
336  * the user root. Ownership for the newly created cgroup and all of its files must thus be
337  * transferred for the user/group passed as uid/gid before system_server can properly access them.
338  */
MkdirAndChown(const std::string & path,mode_t mode,uid_t uid,gid_t gid)339 static bool MkdirAndChown(const std::string& path, mode_t mode, uid_t uid, gid_t gid) {
340     if (mkdir(path.c_str(), mode) == -1) {
341         if (errno == EEXIST) {
342             // Directory already exists and permissions have been set at the time it was created
343             return true;
344         }
345         return false;
346     }
347 
348     auto dir = std::unique_ptr<DIR, decltype(&closedir)>(opendir(path.c_str()), closedir);
349 
350     if (dir == NULL) {
351         PLOG(ERROR) << "opendir failed for " << path;
352         goto err;
353     }
354 
355     struct dirent* dir_entry;
356     while ((dir_entry = readdir(dir.get()))) {
357         if (!strcmp("..", dir_entry->d_name)) {
358             continue;
359         }
360 
361         std::string file_path = path + "/" + dir_entry->d_name;
362 
363         if (lchown(file_path.c_str(), uid, gid) < 0) {
364             PLOG(ERROR) << "lchown failed for " << file_path;
365             goto err;
366         }
367 
368         if (fchmodat(AT_FDCWD, file_path.c_str(), mode, AT_SYMLINK_NOFOLLOW) != 0) {
369             PLOG(ERROR) << "fchmodat failed for " << file_path;
370             goto err;
371         }
372     }
373 
374     return true;
375 err:
376     int saved_errno = errno;
377     rmdir(path.c_str());
378     errno = saved_errno;
379 
380     return false;
381 }
382 
sendSignalToProcessGroup(uid_t uid,pid_t initialPid,int signal)383 bool sendSignalToProcessGroup(uid_t uid, pid_t initialPid, int signal) {
384     std::set<pid_t> pgids, pids;
385 
386     if (CgroupsAvailable()) {
387         std::string hierarchy_root_path, cgroup_v2_path;
388         CgroupGetControllerPath(CGROUPV2_HIERARCHY_NAME, &hierarchy_root_path);
389         cgroup_v2_path = ConvertUidPidToPath(hierarchy_root_path.c_str(), uid, initialPid, true);
390 
391         if (signal == SIGKILL && CgroupKillAvailable()) {
392             LOG(VERBOSE) << "Using " << PROCESSGROUP_CGROUP_KILL_FILE << " to SIGKILL "
393                          << cgroup_v2_path;
394 
395             // We need to kill the process group in addition to the cgroup. For normal apps they
396             // should completely overlap, but system_server kills depend on process group kills to
397             // take down apps which are in their own cgroups and not individually targeted.
398             if (kill(-initialPid, signal) == -1 && errno != ESRCH) {
399                 PLOG(WARNING) << "kill(" << -initialPid << ", " << signal << ") failed";
400             }
401 
402             const std::string killfilepath = cgroup_v2_path + '/' + PROCESSGROUP_CGROUP_KILL_FILE;
403             if (WriteStringToFile("1", killfilepath)) {
404                 return true;
405             } else {
406                 PLOG(ERROR) << "Failed to write 1 to " << killfilepath;
407                 // Fallback to cgroup.procs below
408             }
409         }
410 
411         // Since cgroup.kill only sends SIGKILLs, we read cgroup.procs to find each process to
412         // signal individually. This is more costly than using cgroup.kill for SIGKILLs.
413         LOG(VERBOSE) << "Using " << PROCESSGROUP_CGROUP_PROCS_FILE << " to signal (" << signal
414                      << ") " << cgroup_v2_path;
415 
416         // We separate all of the pids in the cgroup into those pids that are also the leaders of
417         // process groups (stored in the pgids set) and those that are not (stored in the pids set).
418         const auto procsfilepath = cgroup_v2_path + '/' + PROCESSGROUP_CGROUP_PROCS_FILE;
419         std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(procsfilepath.c_str(), "re"), fclose);
420         if (!fp) {
421             // This should only happen if the cgroup has already been removed with a successful call
422             // to killProcessGroup. Callers should only retry sendSignalToProcessGroup or
423             // killProcessGroup calls if they fail without ENOENT.
424             PLOG(ERROR) << "Failed to open " << procsfilepath;
425             kill(-initialPid, signal);
426             return false;
427         }
428 
429         pid_t pid;
430         bool file_is_empty = true;
431         while (fscanf(fp.get(), "%d\n", &pid) == 1 && pid >= 0) {
432             file_is_empty = false;
433             if (pid == 0) {
434                 // Should never happen...  but if it does, trying to kill this
435                 // will boomerang right back and kill us!  Let's not let that happen.
436                 LOG(WARNING)
437                         << "Yikes, we've been told to kill pid 0!  How about we don't do that?";
438                 continue;
439             }
440             pid_t pgid = getpgid(pid);
441             if (pgid == -1) PLOG(ERROR) << "getpgid(" << pid << ") failed";
442             if (pgid == pid) {
443                 pgids.emplace(pid);
444             } else {
445                 pids.emplace(pid);
446             }
447         }
448         if (!file_is_empty) {
449             // Erase all pids that will be killed when we kill the process groups.
450             for (auto it = pids.begin(); it != pids.end();) {
451                 pid_t pgid = getpgid(*it);
452                 if (pgids.count(pgid) == 1) {
453                     it = pids.erase(it);
454                 } else {
455                     ++it;
456                 }
457             }
458         }
459     }
460 
461     pgids.emplace(initialPid);
462 
463     // Kill all process groups.
464     for (const auto pgid : pgids) {
465         LOG(VERBOSE) << "Killing process group " << -pgid << " in uid " << uid
466                      << " as part of process cgroup " << initialPid;
467 
468         if (kill(-pgid, signal) == -1 && errno != ESRCH) {
469             PLOG(WARNING) << "kill(" << -pgid << ", " << signal << ") failed";
470         }
471     }
472 
473     // Kill remaining pids.
474     for (const auto pid : pids) {
475         LOG(VERBOSE) << "Killing pid " << pid << " in uid " << uid << " as part of process cgroup "
476                      << initialPid;
477 
478         if (kill(pid, signal) == -1 && errno != ESRCH) {
479             PLOG(WARNING) << "kill(" << pid << ", " << signal << ") failed";
480         }
481     }
482 
483     return true;
484 }
485 
486 template <typename T>
toMillisec(T && duration)487 static std::chrono::milliseconds toMillisec(T&& duration) {
488     return std::chrono::duration_cast<std::chrono::milliseconds>(duration);
489 }
490 
491 enum class populated_status
492 {
493     populated,
494     not_populated,
495     error
496 };
497 
cgroupIsPopulated(int events_fd)498 static populated_status cgroupIsPopulated(int events_fd) {
499     const std::string POPULATED_KEY("populated ");
500     const std::string::size_type MAX_EVENTS_FILE_SIZE = 32;
501 
502     std::string buf;
503     buf.resize(MAX_EVENTS_FILE_SIZE);
504     ssize_t len = TEMP_FAILURE_RETRY(pread(events_fd, buf.data(), buf.size(), 0));
505     if (len == -1) {
506         PLOG(ERROR) << "Could not read cgroup.events: ";
507         // Potentially ENODEV if the cgroup has been removed since we opened this file, but that
508         // shouldn't have happened yet.
509         return populated_status::error;
510     }
511 
512     if (len == 0) {
513         LOG(ERROR) << "cgroup.events EOF";
514         return populated_status::error;
515     }
516 
517     buf.resize(len);
518 
519     const std::string::size_type pos = buf.find(POPULATED_KEY);
520     if (pos == std::string::npos) {
521         LOG(ERROR) << "Could not find populated key in cgroup.events";
522         return populated_status::error;
523     }
524 
525     if (pos + POPULATED_KEY.size() + 1 > len) {
526         LOG(ERROR) << "Partial read of cgroup.events";
527         return populated_status::error;
528     }
529 
530     return buf[pos + POPULATED_KEY.size()] == '1' ?
531         populated_status::populated : populated_status::not_populated;
532 }
533 
534 // The default timeout of 2200ms comes from the default number of retries in a previous
535 // implementation of this function. The default retry value was 40 for killing and 400 for cgroup
536 // removal with 5ms sleeps between each retry.
KillProcessGroup(uid_t uid,pid_t initialPid,int signal,bool once=false,std::chrono::steady_clock::time_point until=std::chrono::steady_clock::now ()+2200ms)537 static int KillProcessGroup(
538         uid_t uid, pid_t initialPid, int signal, bool once = false,
539         std::chrono::steady_clock::time_point until = std::chrono::steady_clock::now() + 2200ms) {
540     if (uid < 0) {
541         LOG(ERROR) << __func__ << ": invalid UID " << uid;
542         return -1;
543     }
544     if (initialPid <= 0) {
545         LOG(ERROR) << __func__ << ": invalid PID " << initialPid;
546         return -1;
547     }
548 
549     // Always attempt to send a kill signal to at least the initialPid, at least once, regardless of
550     // whether its cgroup exists or not. This should only be necessary if a bug results in the
551     // migration of the targeted process out of its cgroup, which we will also attempt to kill.
552     const bool signal_ret = sendSignalToProcessGroup(uid, initialPid, signal);
553 
554     if (!CgroupsAvailable() || !signal_ret) return signal_ret ? 0 : -1;
555 
556     std::string hierarchy_root_path;
557     CgroupGetControllerPath(CGROUPV2_HIERARCHY_NAME, &hierarchy_root_path);
558 
559     const std::string cgroup_v2_path =
560             ConvertUidPidToPath(hierarchy_root_path.c_str(), uid, initialPid, true);
561 
562     const std::string eventsfile = cgroup_v2_path + '/' + PROCESSGROUP_CGROUP_EVENTS_FILE;
563     android::base::unique_fd events_fd(open(eventsfile.c_str(), O_RDONLY));
564     if (events_fd.get() == -1) {
565         PLOG(WARNING) << "Error opening " << eventsfile << " for KillProcessGroup";
566         return -1;
567     }
568 
569     struct pollfd fds = {
570         .fd = events_fd,
571         .events = POLLPRI,
572     };
573 
574     const std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
575 
576     // The primary reason to loop here is to capture any new forks or migrations that could occur
577     // after we send signals to the original set of processes, but before all of those processes
578     // exit and the cgroup becomes unpopulated, or before we remove the cgroup. We try hard to
579     // ensure this completes successfully to avoid permanent memory leaks, but we still place a
580     // large default upper bound on the amount of time we spend in this loop. The amount of CPU
581     // contention, and the amount of work that needs to be done in do_exit for each process
582     // determines how long this will take.
583     int ret;
584     do {
585         populated_status populated;
586         while ((populated = cgroupIsPopulated(events_fd.get())) == populated_status::populated &&
587                std::chrono::steady_clock::now() < until) {
588 
589             sendSignalToProcessGroup(uid, initialPid, signal);
590             if (once) {
591                 populated = cgroupIsPopulated(events_fd.get());
592                 break;
593             }
594 
595             const std::chrono::steady_clock::time_point poll_start =
596                     std::chrono::steady_clock::now();
597 
598             if (poll_start < until)
599                 ret = TEMP_FAILURE_RETRY(poll(&fds, 1, toMillisec(until - poll_start).count()));
600 
601             if (ret == -1) {
602                 // Fallback to 5ms sleeps if poll fails
603                 PLOG(ERROR) << "Poll on " << eventsfile << "failed";
604                 const std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
605                 if (now < until)
606                     std::this_thread::sleep_for(std::min(5ms, toMillisec(until - now)));
607             }
608 
609             LOG(VERBOSE) << "Waited "
610                          << toMillisec(std::chrono::steady_clock::now() - poll_start).count()
611                          << " ms for " << eventsfile << " poll";
612         }
613 
614         const std::chrono::milliseconds kill_duration =
615                 toMillisec(std::chrono::steady_clock::now() - start);
616 
617         if (populated == populated_status::populated) {
618             LOG(WARNING) << "Still waiting on process(es) to exit for cgroup " << cgroup_v2_path
619                          << " after " << kill_duration.count() << " ms";
620             // We'll still try the cgroup removal below which we expect to log an error.
621         } else if (populated == populated_status::not_populated) {
622             LOG(VERBOSE) << "Killed all processes under cgroup " << cgroup_v2_path
623                          << " after " << kill_duration.count() << " ms";
624         }
625 
626         ret = RemoveCgroup(hierarchy_root_path.c_str(), uid, initialPid, true);
627         if (ret)
628             PLOG(ERROR) << "Unable to remove cgroup " << cgroup_v2_path;
629         else
630             LOG(INFO) << "Removed cgroup " << cgroup_v2_path;
631 
632         if (isMemoryCgroupSupported() && UsePerAppMemcg()) {
633             // This per-application memcg v1 case should eventually be removed after migration to
634             // memcg v2.
635             std::string memcg_apps_path;
636             if (CgroupGetMemcgAppsPath(&memcg_apps_path) &&
637                 (ret = RemoveCgroup(memcg_apps_path.c_str(), uid, initialPid, false)) < 0) {
638                 const auto memcg_v1_cgroup_path =
639                         ConvertUidPidToPath(memcg_apps_path.c_str(), uid, initialPid, false);
640                 PLOG(ERROR) << "Unable to remove memcg v1 cgroup " << memcg_v1_cgroup_path;
641             }
642         }
643 
644         if (once) break;
645         if (std::chrono::steady_clock::now() >= until) break;
646     } while (ret && errno == EBUSY);
647 
648     return ret;
649 }
650 
killProcessGroup(uid_t uid,pid_t initialPid,int signal)651 int killProcessGroup(uid_t uid, pid_t initialPid, int signal) {
652     return KillProcessGroup(uid, initialPid, signal);
653 }
654 
killProcessGroupOnce(uid_t uid,pid_t initialPid,int signal)655 int killProcessGroupOnce(uid_t uid, pid_t initialPid, int signal) {
656     return KillProcessGroup(uid, initialPid, signal, true);
657 }
658 
createProcessGroupInternal(uid_t uid,pid_t initialPid,std::string cgroup,bool activate_controllers)659 static int createProcessGroupInternal(uid_t uid, pid_t initialPid, std::string cgroup,
660                                       bool activate_controllers) {
661     auto uid_path = ConvertUidToPath(cgroup.c_str(), uid, activate_controllers);
662 
663     struct stat cgroup_stat;
664     mode_t cgroup_mode = 0750;
665     uid_t cgroup_uid = AID_SYSTEM;
666     gid_t cgroup_gid = AID_SYSTEM;
667     int ret = 0;
668 
669     if (stat(cgroup.c_str(), &cgroup_stat) < 0) {
670         PLOG(ERROR) << "Failed to get stats for " << cgroup;
671     } else {
672         cgroup_mode = cgroup_stat.st_mode;
673         cgroup_uid = cgroup_stat.st_uid;
674         cgroup_gid = cgroup_stat.st_gid;
675     }
676 
677     if (!MkdirAndChown(uid_path, cgroup_mode, cgroup_uid, cgroup_gid)) {
678         PLOG(ERROR) << "Failed to make and chown " << uid_path;
679         return -errno;
680     }
681     if (activate_controllers) {
682         if (!CgroupMap::GetInstance().ActivateControllers(uid_path)) {
683             PLOG(ERROR) << "Failed to activate controllers in " << uid_path;
684             return -errno;
685         }
686     }
687 
688     auto uid_pid_path = ConvertUidPidToPath(cgroup.c_str(), uid, initialPid, activate_controllers);
689 
690     if (!MkdirAndChown(uid_pid_path, cgroup_mode, cgroup_uid, cgroup_gid)) {
691         PLOG(ERROR) << "Failed to make and chown " << uid_pid_path;
692         return -errno;
693     }
694 
695     auto uid_pid_procs_file = uid_pid_path + '/' + PROCESSGROUP_CGROUP_PROCS_FILE;
696 
697     if (!WriteStringToFile(std::to_string(initialPid), uid_pid_procs_file)) {
698         ret = -errno;
699         PLOG(ERROR) << "Failed to write '" << initialPid << "' to " << uid_pid_procs_file;
700     }
701 
702     return ret;
703 }
704 
createProcessGroup(uid_t uid,pid_t initialPid,bool memControl)705 int createProcessGroup(uid_t uid, pid_t initialPid, bool memControl) {
706     if (uid < 0) {
707         LOG(ERROR) << __func__ << ": invalid UID " << uid;
708         return -1;
709     }
710     if (initialPid <= 0) {
711         LOG(ERROR) << __func__ << ": invalid PID " << initialPid;
712         return -1;
713     }
714 
715     if (memControl && !UsePerAppMemcg()) {
716         LOG(ERROR) << "service memory controls are used without per-process memory cgroup support";
717         return -EINVAL;
718     }
719 
720     if (std::string memcg_apps_path;
721         isMemoryCgroupSupported() && UsePerAppMemcg() && CgroupGetMemcgAppsPath(&memcg_apps_path)) {
722         // Note by bvanassche: passing 'false' as fourth argument below implies that the v1
723         // hierarchy is used. It is not clear to me whether the above conditions guarantee that the
724         // v1 hierarchy is used.
725         int ret = createProcessGroupInternal(uid, initialPid, memcg_apps_path, false);
726         if (ret != 0) {
727             return ret;
728         }
729     }
730 
731     std::string cgroup;
732     CgroupGetControllerPath(CGROUPV2_HIERARCHY_NAME, &cgroup);
733     return createProcessGroupInternal(uid, initialPid, cgroup, true);
734 }
735 
SetProcessGroupValue(pid_t tid,const std::string & attr_name,int64_t value)736 static bool SetProcessGroupValue(pid_t tid, const std::string& attr_name, int64_t value) {
737     if (!isMemoryCgroupSupported()) {
738         LOG(ERROR) << "Memcg is not mounted.";
739         return false;
740     }
741 
742     std::string path;
743     if (!CgroupGetAttributePathForTask(attr_name, tid, &path)) {
744         LOG(ERROR) << "Failed to find attribute '" << attr_name << "'";
745         return false;
746     }
747 
748     if (!WriteStringToFile(std::to_string(value), path)) {
749         PLOG(ERROR) << "Failed to write '" << value << "' to " << path;
750         return false;
751     }
752     return true;
753 }
754 
setProcessGroupSwappiness(uid_t,pid_t pid,int swappiness)755 bool setProcessGroupSwappiness(uid_t, pid_t pid, int swappiness) {
756     return SetProcessGroupValue(pid, "MemSwappiness", swappiness);
757 }
758 
setProcessGroupSoftLimit(uid_t,pid_t pid,int64_t soft_limit_in_bytes)759 bool setProcessGroupSoftLimit(uid_t, pid_t pid, int64_t soft_limit_in_bytes) {
760     return SetProcessGroupValue(pid, "MemSoftLimit", soft_limit_in_bytes);
761 }
762 
setProcessGroupLimit(uid_t,pid_t pid,int64_t limit_in_bytes)763 bool setProcessGroupLimit(uid_t, pid_t pid, int64_t limit_in_bytes) {
764     return SetProcessGroupValue(pid, "MemLimit", limit_in_bytes);
765 }
766 
isProfileValidForProcess(const std::string & profile_name,uid_t uid,pid_t pid)767 bool isProfileValidForProcess(const std::string& profile_name, uid_t uid, pid_t pid) {
768     const TaskProfile* tp = TaskProfiles::GetInstance().GetProfile(profile_name);
769 
770     if (tp == nullptr) {
771         return false;
772     }
773 
774     return tp->IsValidForProcess(uid, pid);
775 }
776