OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/process/process.h" | 5 #include "base/process/process.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <sys/resource.h> | 8 #include <sys/resource.h> |
9 | 9 |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 background_file = | 53 background_file = |
54 base::FilePath(base::StringPrintf(kControlPath, kBackground)); | 54 base::FilePath(base::StringPrintf(kControlPath, kBackground)); |
55 base::FileSystemType foreground_type; | 55 base::FileSystemType foreground_type; |
56 base::FileSystemType background_type; | 56 base::FileSystemType background_type; |
57 enabled = | 57 enabled = |
58 base::GetFileSystemType(foreground_file, &foreground_type) && | 58 base::GetFileSystemType(foreground_file, &foreground_type) && |
59 base::GetFileSystemType(background_file, &background_type) && | 59 base::GetFileSystemType(background_file, &background_type) && |
60 foreground_type == FILE_SYSTEM_CGROUP && | 60 foreground_type == FILE_SYSTEM_CGROUP && |
61 background_type == FILE_SYSTEM_CGROUP; | 61 background_type == FILE_SYSTEM_CGROUP; |
62 } | 62 } |
| 63 |
| 64 static CGroups& Get() { |
| 65 static auto& groups = *new CGroups; |
| 66 return groups; |
| 67 } |
63 }; | 68 }; |
64 | |
65 base::LazyInstance<CGroups>::DestructorAtExit g_cgroups = | |
66 LAZY_INSTANCE_INITIALIZER; | |
67 #else | 69 #else |
68 const int kBackgroundPriority = 5; | 70 const int kBackgroundPriority = 5; |
69 #endif // defined(OS_CHROMEOS) | 71 #endif // defined(OS_CHROMEOS) |
70 | 72 |
71 struct CheckForNicePermission { | 73 struct CheckForNicePermission { |
72 CheckForNicePermission() : can_reraise_priority(false) { | 74 CheckForNicePermission() : can_reraise_priority(false) { |
73 // We won't be able to raise the priority if we don't have the right rlimit. | 75 // We won't be able to raise the priority if we don't have the right rlimit. |
74 // The limit may be adjusted in /etc/security/limits.conf for PAM systems. | 76 // The limit may be adjusted in /etc/security/limits.conf for PAM systems. |
75 struct rlimit rlim; | 77 struct rlimit rlim; |
76 if ((getrlimit(RLIMIT_NICE, &rlim) == 0) && | 78 if ((getrlimit(RLIMIT_NICE, &rlim) == 0) && |
77 (20 - kForegroundPriority) <= static_cast<int>(rlim.rlim_cur)) { | 79 (20 - kForegroundPriority) <= static_cast<int>(rlim.rlim_cur)) { |
78 can_reraise_priority = true; | 80 can_reraise_priority = true; |
79 } | 81 } |
80 }; | 82 }; |
81 | 83 |
82 bool can_reraise_priority; | 84 bool can_reraise_priority; |
83 }; | 85 }; |
84 | 86 |
85 } // namespace | 87 } // namespace |
86 | 88 |
87 // static | 89 // static |
88 bool Process::CanBackgroundProcesses() { | 90 bool Process::CanBackgroundProcesses() { |
89 #if defined(OS_CHROMEOS) | 91 #if defined(OS_CHROMEOS) |
90 if (g_cgroups.Get().enabled) | 92 if (CGroups::Get().enabled) |
91 return true; | 93 return true; |
92 #endif // defined(OS_CHROMEOS) | 94 #endif // defined(OS_CHROMEOS) |
93 | 95 |
94 static LazyInstance<CheckForNicePermission>::DestructorAtExit | 96 static LazyInstance<CheckForNicePermission>::DestructorAtExit |
95 check_for_nice_permission = LAZY_INSTANCE_INITIALIZER; | 97 check_for_nice_permission = LAZY_INSTANCE_INITIALIZER; |
96 return check_for_nice_permission.Get().can_reraise_priority; | 98 return check_for_nice_permission.Get().can_reraise_priority; |
97 } | 99 } |
98 | 100 |
99 bool Process::IsProcessBackgrounded() const { | 101 bool Process::IsProcessBackgrounded() const { |
100 DCHECK(IsValid()); | 102 DCHECK(IsValid()); |
101 | 103 |
102 #if defined(OS_CHROMEOS) | 104 #if defined(OS_CHROMEOS) |
103 if (g_cgroups.Get().enabled) { | 105 if (CGroups::Get().enabled) { |
104 // Used to allow reading the process priority from proc on thread launch. | 106 // Used to allow reading the process priority from proc on thread launch. |
105 base::ThreadRestrictions::ScopedAllowIO allow_io; | 107 base::ThreadRestrictions::ScopedAllowIO allow_io; |
106 std::string proc; | 108 std::string proc; |
107 if (base::ReadFileToString( | 109 if (base::ReadFileToString( |
108 base::FilePath(StringPrintf(kProcPath, process_)), &proc)) { | 110 base::FilePath(StringPrintf(kProcPath, process_)), &proc)) { |
109 return IsProcessBackgroundedCGroup(proc); | 111 return IsProcessBackgroundedCGroup(proc); |
110 } | 112 } |
111 return false; | 113 return false; |
112 } | 114 } |
113 #endif // defined(OS_CHROMEOS) | 115 #endif // defined(OS_CHROMEOS) |
114 | 116 |
115 return GetPriority() == kBackgroundPriority; | 117 return GetPriority() == kBackgroundPriority; |
116 } | 118 } |
117 | 119 |
118 bool Process::SetProcessBackgrounded(bool background) { | 120 bool Process::SetProcessBackgrounded(bool background) { |
119 DCHECK(IsValid()); | 121 DCHECK(IsValid()); |
120 | 122 |
121 #if defined(OS_CHROMEOS) | 123 #if defined(OS_CHROMEOS) |
122 if (g_cgroups.Get().enabled) { | 124 if (CGroups::Get().enabled) { |
123 std::string pid = IntToString(process_); | 125 std::string pid = IntToString(process_); |
124 const base::FilePath file = | 126 const base::FilePath file = background ? CGroups::Get().background_file |
125 background ? | 127 : CGroups::Get().foreground_file; |
126 g_cgroups.Get().background_file : g_cgroups.Get().foreground_file; | |
127 return base::WriteFile(file, pid.c_str(), pid.size()) > 0; | 128 return base::WriteFile(file, pid.c_str(), pid.size()) > 0; |
128 } | 129 } |
129 #endif // defined(OS_CHROMEOS) | 130 #endif // defined(OS_CHROMEOS) |
130 | 131 |
131 if (!CanBackgroundProcesses()) | 132 if (!CanBackgroundProcesses()) |
132 return false; | 133 return false; |
133 | 134 |
134 int priority = background ? kBackgroundPriority : kForegroundPriority; | 135 int priority = background ? kBackgroundPriority : kForegroundPriority; |
135 int result = setpriority(PRIO_PROCESS, process_, priority); | 136 int result = setpriority(PRIO_PROCESS, process_, priority); |
136 DPCHECK(result == 0); | 137 DPCHECK(result == 0); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 return kNullProcessId; | 195 return kNullProcessId; |
195 } | 196 } |
196 return value; | 197 return value; |
197 } | 198 } |
198 } | 199 } |
199 return kNullProcessId; | 200 return kNullProcessId; |
200 } | 201 } |
201 #endif // defined(OS_CHROMEOS) | 202 #endif // defined(OS_CHROMEOS) |
202 | 203 |
203 } // namespace base | 204 } // namespace base |
OLD | NEW |