]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
rlimit-util: don't call setrlimit() needlessly if it wouldn't change anything
authorLennart Poettering <lennart@poettering.net>
Tue, 2 Oct 2018 06:41:03 +0000 (08:41 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 16 Oct 2018 14:33:55 +0000 (16:33 +0200)
Just a tiny tweak to avoid generating an error if there's no need to.

src/basic/rlimit-util.c

index d63b85850cf8a7d50051a2661ed89098a05236a5..c133f84b7e90e24c7dd10149d09188b11beeb371 100644 (file)
@@ -34,8 +34,15 @@ int setrlimit_closest(int resource, const struct rlimit *rlim) {
         if (highest.rlim_max == RLIM_INFINITY)
                 return -EPERM;
 
-        fixed.rlim_cur = MIN(rlim->rlim_cur, highest.rlim_max);
-        fixed.rlim_max = MIN(rlim->rlim_max, highest.rlim_max);
+        fixed = (struct rlimit) {
+                .rlim_cur = MIN(rlim->rlim_cur, highest.rlim_max),
+                .rlim_max = MIN(rlim->rlim_max, highest.rlim_max),
+        };
+
+        /* Shortcut things if we wouldn't change anything. */
+        if (fixed.rlim_cur == highest.rlim_cur &&
+            fixed.rlim_max == highest.rlim_max)
+                return 0;
 
         if (setrlimit(resource, &fixed) < 0)
                 return -errno;