summaryrefslogtreecommitdiffstats
path: root/src/basic/process-util.c
diff options
context:
space:
mode:
authorMike Yuan <me@yhndnzj.com>2024-08-09 17:36:07 +0200
committerDaan De Meyer <daan.j.demeyer@gmail.com>2024-08-18 15:16:03 +0200
commitf32538e1cceb31477a7af1f6e75ddfda43f63828 (patch)
treeee75544164e38076237ee2ee118d125f9fb6a929 /src/basic/process-util.c
parentnetwork: refuse files under API VFS specified in PrivateKeyFile= and friends (diff)
downloadsystemd-f32538e1cceb31477a7af1f6e75ddfda43f63828.tar.xz
systemd-f32538e1cceb31477a7af1f6e75ddfda43f63828.zip
basic/process-util: modernize setpriority_closest()
Before this commit, the "Cannot raise nice level" branch is rather confusing, as we're actually lowering the nice. Also, it's better to log about the final nice value for both cases, no matter whether we need to set to limit or not.
Diffstat (limited to '')
-rw-r--r--src/basic/process-util.c31
1 files changed, 13 insertions, 18 deletions
diff --git a/src/basic/process-util.c b/src/basic/process-util.c
index ede5f85135..e5dbd6f72d 100644
--- a/src/basic/process-util.c
+++ b/src/basic/process-util.c
@@ -1901,17 +1901,15 @@ static int rlimit_to_nice(rlim_t limit) {
}
int setpriority_closest(int priority) {
- int current, limit, saved_errno;
struct rlimit highest;
+ int r, current, limit;
/* Try to set requested nice level */
- if (setpriority(PRIO_PROCESS, 0, priority) >= 0)
+ r = RET_NERRNO(setpriority(PRIO_PROCESS, 0, priority));
+ if (r >= 0)
return 1;
-
- /* Permission failed */
- saved_errno = -errno;
- if (!ERRNO_IS_PRIVILEGE(saved_errno))
- return saved_errno;
+ if (!ERRNO_IS_NEG_PRIVILEGE(r))
+ return r;
errno = 0;
current = getpriority(PRIO_PROCESS, 0);
@@ -1925,24 +1923,21 @@ int setpriority_closest(int priority) {
* then the whole setpriority() system call is blocked to us, hence let's propagate the error
* right-away */
if (priority > current)
- return saved_errno;
+ return r;
if (getrlimit(RLIMIT_NICE, &highest) < 0)
return -errno;
limit = rlimit_to_nice(highest.rlim_cur);
- /* We are already less nice than limit allows us */
- if (current < limit) {
- log_debug("Cannot raise nice level, permissions and the resource limit do not allow it.");
- return 0;
- }
-
- /* Push to the allowed limit */
- if (setpriority(PRIO_PROCESS, 0, limit) < 0)
- return -errno;
+ /* Push to the allowed limit if we're higher than that. Note that we could also be less nice than
+ * limit allows us, but still higher than what's requested. In that case our current value is
+ * the best choice. */
+ if (current > limit)
+ if (setpriority(PRIO_PROCESS, 0, limit) < 0)
+ return -errno;
- log_debug("Cannot set requested nice level (%i), used next best (%i).", priority, limit);
+ log_debug("Cannot set requested nice level (%i), using next best (%i).", priority, MIN(current, limit));
return 0;
}