blob: 30bc1f7507f7a33318c7966dbec20eea89d77763 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
|