summaryrefslogtreecommitdiffstats
path: root/base/process_linux.cc
diff options
context:
space:
mode:
authordavemoore@chromium.org <davemoore@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-29 17:43:44 +0000
committerdavemoore@chromium.org <davemoore@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-29 17:43:44 +0000
commit276aa6a60396329c7fb4a988bd0be7f7d0895651 (patch)
tree62ed233e5faf34a14cf99fcbe49416b64e506d23 /base/process_linux.cc
parent57f9caa083ef107001b12ec6e455e23a5f94e420 (diff)
downloadchromium_src-276aa6a60396329c7fb4a988bd0be7f7d0895651.zip
chromium_src-276aa6a60396329c7fb4a988bd0be7f7d0895651.tar.gz
chromium_src-276aa6a60396329c7fb4a988bd0be7f7d0895651.tar.bz2
Lower priorities of background tabs on linux.
BUG=none TEST=none Review URL: http://codereview.chromium.org/345009 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@30470 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/process_linux.cc')
-rw-r--r--base/process_linux.cc63
1 files changed, 63 insertions, 0 deletions
diff --git a/base/process_linux.cc b/base/process_linux.cc
new file mode 100644
index 0000000..30bc1f7
--- /dev/null
+++ b/base/process_linux.cc
@@ -0,0 +1,63 @@
+// Copyright (c) 2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/process.h"
+
+#include <errno.h>
+#include <sys/resource.h>
+
+#include "base/logging.h"
+
+namespace base {
+
+bool Process::IsProcessBackgrounded() const {
+ DCHECK(process_);
+ return saved_priority_ == kUnsetProcessPriority;
+}
+
+bool Process::SetProcessBackgrounded(bool background) {
+ DCHECK(process_);
+
+ if (background) {
+ // We won't be able to raise the priority if we don't have the right rlimit.
+ // The limit may be adjusted in /etc/security/limits.conf for PAM systems.
+ struct rlimit rlim;
+ if (getrlimit(RLIMIT_NICE, &rlim) != 0) {
+ // Call to getrlimit failed, don't background.
+ return false;
+ }
+ errno = 0;
+ int current_priority = GetPriority();
+ if (errno) {
+ // Couldn't get priority.
+ return false;
+ }
+ // {set,get}priority values are in the range -20 to 19, where -1 is higher
+ // priority than 0. But rlimit's are in the range from 0 to 39 where
+ // 1 is higher than 0.
+ if ((20 - current_priority) > static_cast<int>(rlim.rlim_cur)) {
+ // User is not allowed to raise the priority back to where it is now.
+ return false;
+ }
+ int result = setpriority(PRIO_PROCESS, process_, current_priority + 1);
+ if (result == -1) {
+ // Failed to lower priority.
+ return false;
+ }
+ saved_priority_ = current_priority;
+ return true;
+ } else {
+ if (saved_priority_ == kUnsetProcessPriority) {
+ // Can't restore if we were never backgrounded.
+ return false;
+ }
+ int result = setpriority(PRIO_PROCESS, process_, saved_priority_);
+ // If we can't restore something has gone terribly wrong.
+ DPCHECK(result == 0);
+ saved_priority_ = kUnsetProcessPriority;
+ return true;
+ }
+}
+
+} // namespace base