]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
util-lib: split out resource limits related calls into rlimit-util.[ch]
authorLennart Poettering <lennart@poettering.net>
Mon, 26 Oct 2015 18:40:43 +0000 (19:40 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 27 Oct 2015 12:25:56 +0000 (13:25 +0100)
12 files changed:
Makefile.am
src/basic/rlimit-util.c [new file with mode: 0644]
src/basic/rlimit-util.h [new file with mode: 0644]
src/basic/util.c
src/basic/util.h
src/core/dbus-execute.c
src/core/execute.c
src/core/main.c
src/journal/journalctl.c
src/shared/bus-util.c
src/systemctl/systemctl.c
src/test/test-tables.c

index 275b85619146c2de800fbfb1a67396c513a0017b..efd1329adefa3f61839aa259895b94d5f1b6042d 100644 (file)
@@ -791,6 +791,8 @@ libbasic_la_SOURCES = \
        src/basic/parse-util.h \
        src/basic/user-util.c \
        src/basic/user-util.h \
+       src/basic/rlimit-util.c \
+       src/basic/rlimit-util.h \
        src/basic/mount-util.c \
        src/basic/mount-util.h \
        src/basic/hexdecoct.c \
diff --git a/src/basic/rlimit-util.c b/src/basic/rlimit-util.c
new file mode 100644 (file)
index 0000000..7f9d632
--- /dev/null
@@ -0,0 +1,69 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright 2010 Lennart Poettering
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include "missing.h"
+#include "rlimit-util.h"
+#include "util.h"
+
+int setrlimit_closest(int resource, const struct rlimit *rlim) {
+        struct rlimit highest, fixed;
+
+        assert(rlim);
+
+        if (setrlimit(resource, rlim) >= 0)
+                return 0;
+
+        if (errno != EPERM)
+                return -errno;
+
+        /* So we failed to set the desired setrlimit, then let's try
+         * to get as close as we can */
+        assert_se(getrlimit(resource, &highest) == 0);
+
+        fixed.rlim_cur = MIN(rlim->rlim_cur, highest.rlim_max);
+        fixed.rlim_max = MIN(rlim->rlim_max, highest.rlim_max);
+
+        if (setrlimit(resource, &fixed) < 0)
+                return -errno;
+
+        return 0;
+}
+
+static const char* const rlimit_table[_RLIMIT_MAX] = {
+        [RLIMIT_CPU] = "LimitCPU",
+        [RLIMIT_FSIZE] = "LimitFSIZE",
+        [RLIMIT_DATA] = "LimitDATA",
+        [RLIMIT_STACK] = "LimitSTACK",
+        [RLIMIT_CORE] = "LimitCORE",
+        [RLIMIT_RSS] = "LimitRSS",
+        [RLIMIT_NOFILE] = "LimitNOFILE",
+        [RLIMIT_AS] = "LimitAS",
+        [RLIMIT_NPROC] = "LimitNPROC",
+        [RLIMIT_MEMLOCK] = "LimitMEMLOCK",
+        [RLIMIT_LOCKS] = "LimitLOCKS",
+        [RLIMIT_SIGPENDING] = "LimitSIGPENDING",
+        [RLIMIT_MSGQUEUE] = "LimitMSGQUEUE",
+        [RLIMIT_NICE] = "LimitNICE",
+        [RLIMIT_RTPRIO] = "LimitRTPRIO",
+        [RLIMIT_RTTIME] = "LimitRTTIME"
+};
+
+DEFINE_STRING_TABLE_LOOKUP(rlimit, int);
diff --git a/src/basic/rlimit-util.h b/src/basic/rlimit-util.h
new file mode 100644 (file)
index 0000000..262f86d
--- /dev/null
@@ -0,0 +1,33 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#pragma once
+
+/***
+  This file is part of systemd.
+
+  Copyright 2010 Lennart Poettering
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <sys/resource.h>
+
+#include "macro.h"
+
+const char *rlimit_to_string(int i) _const_;
+int rlimit_from_string(const char *s) _pure_;
+
+int setrlimit_closest(int resource, const struct rlimit *rlim);
+
+#define RLIMIT_MAKE_CONST(lim) ((struct rlimit) { lim, lim })
index 2ee5de9cd03041cf65c89535e5a220debcc23ca1..121ca3376e8056e851c39736e25603b0420a7619 100644 (file)
@@ -46,7 +46,6 @@
 #include <sys/mount.h>
 #include <sys/personality.h>
 #include <sys/prctl.h>
-#include <sys/resource.h>
 #include <sys/stat.h>
 #include <sys/statvfs.h>
 #include <sys/time.h>
@@ -1173,27 +1172,6 @@ static const char* const sched_policy_table[] = {
 
 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(sched_policy, int, INT_MAX);
 
-static const char* const rlimit_table[_RLIMIT_MAX] = {
-        [RLIMIT_CPU] = "LimitCPU",
-        [RLIMIT_FSIZE] = "LimitFSIZE",
-        [RLIMIT_DATA] = "LimitDATA",
-        [RLIMIT_STACK] = "LimitSTACK",
-        [RLIMIT_CORE] = "LimitCORE",
-        [RLIMIT_RSS] = "LimitRSS",
-        [RLIMIT_NOFILE] = "LimitNOFILE",
-        [RLIMIT_AS] = "LimitAS",
-        [RLIMIT_NPROC] = "LimitNPROC",
-        [RLIMIT_MEMLOCK] = "LimitMEMLOCK",
-        [RLIMIT_LOCKS] = "LimitLOCKS",
-        [RLIMIT_SIGPENDING] = "LimitSIGPENDING",
-        [RLIMIT_MSGQUEUE] = "LimitMSGQUEUE",
-        [RLIMIT_NICE] = "LimitNICE",
-        [RLIMIT_RTPRIO] = "LimitRTPRIO",
-        [RLIMIT_RTTIME] = "LimitRTTIME"
-};
-
-DEFINE_STRING_TABLE_LOOKUP(rlimit, int);
-
 bool kexec_loaded(void) {
        bool loaded = false;
        char *s;
@@ -1339,30 +1317,6 @@ int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *pa
         _exit(EXIT_FAILURE);
 }
 
-int setrlimit_closest(int resource, const struct rlimit *rlim) {
-        struct rlimit highest, fixed;
-
-        assert(rlim);
-
-        if (setrlimit(resource, rlim) >= 0)
-                return 0;
-
-        if (errno != EPERM)
-                return -errno;
-
-        /* So we failed to set the desired setrlimit, then let's try
-         * to get as close as we can */
-        assert_se(getrlimit(resource, &highest) == 0);
-
-        fixed.rlim_cur = MIN(rlim->rlim_cur, highest.rlim_max);
-        fixed.rlim_max = MIN(rlim->rlim_max, highest.rlim_max);
-
-        if (setrlimit(resource, &fixed) < 0)
-                return -errno;
-
-        return 0;
-}
-
 bool http_etag_is_valid(const char *etag) {
         if (isempty(etag))
                 return false;
index 76d0784d3627a2becfbaf5956b2d6ce3f667c724..6e5df014501b397d8af77879311ef56cf6cc7a0f 100644 (file)
@@ -247,9 +247,6 @@ bool log_level_is_valid(int level);
 int sched_policy_to_string_alloc(int i, char **s);
 int sched_policy_from_string(const char *s);
 
-const char *rlimit_to_string(int i) _const_;
-int rlimit_from_string(const char *s) _pure_;
-
 extern int saved_argc;
 extern char **saved_argv;
 
@@ -261,8 +258,6 @@ void* memdup(const void *p, size_t l) _alloc_(2);
 
 int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...);
 
-int setrlimit_closest(int resource, const struct rlimit *rlim);
-
 bool http_url_is_valid(const char *url) _pure_;
 bool documentation_url_is_valid(const char *url) _pure_;
 
@@ -550,8 +545,6 @@ int chattr_path(const char *p, unsigned value, unsigned mask);
 int read_attr_fd(int fd, unsigned *ret);
 int read_attr_path(const char *p, unsigned *ret);
 
-#define RLIMIT_MAKE_CONST(lim) ((struct rlimit) { lim, lim })
-
 int syslog_parse_priority(const char **p, int *priority, bool with_facility);
 
 int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
index 238fc8efdd27cb433d816c1e4fde10e7c4e34700..950e463bc5aa49da563edd20f09fd9ff3eb6df1f 100644 (file)
@@ -38,6 +38,7 @@
 #include "namespace.h"
 #include "parse-util.h"
 #include "path-util.h"
+#include "rlimit-util.h"
 #ifdef HAVE_SECCOMP
 #include "seccomp-util.h"
 #endif
index 24a8e646ad6005d88dba807e21eb03931858886f..ad0e6be4b7db529fd63f865680376255cfe62fb4 100644 (file)
@@ -79,6 +79,7 @@
 #include "parse-util.h"
 #include "path-util.h"
 #include "process-util.h"
+#include "rlimit-util.h"
 #include "rm-rf.h"
 #ifdef HAVE_SECCOMP
 #include "seccomp-util.h"
index a8834b6f0b2db62b770aa2391520ca144b23da87..dfd17694c9b97510ec528e8dfa17f868490e4523 100644 (file)
@@ -69,6 +69,7 @@
 #include "pager.h"
 #include "parse-util.h"
 #include "process-util.h"
+#include "rlimit-util.h"
 #include "selinux-setup.h"
 #include "selinux-util.h"
 #include "signal-util.h"
index c13b674e15dcf0009c094e81da3c061014277873..5b3b6cd143f7a3fc7bf8b823e574451f43242ed2 100644 (file)
@@ -58,6 +58,7 @@
 #include "pager.h"
 #include "parse-util.h"
 #include "path-util.h"
+#include "rlimit-util.h"
 #include "set.h"
 #include "sigbus.h"
 #include "strv.h"
index 53b9752af0a749b96d25b250e06bbf97cfcf0ee1..724103fc3f85b4bf4a35f0d447a5cb1e1760fc1b 100644 (file)
@@ -39,6 +39,7 @@
 #include "missing.h"
 #include "parse-util.h"
 #include "path-util.h"
+#include "rlimit-util.h"
 #include "set.h"
 #include "signal-util.h"
 #include "string-util.h"
index 054e9eaa132bb2d2b66704c12918bb25c6fb2785..77a05ca346a230e127dbdde8223b942ebe3a1253 100644 (file)
@@ -65,6 +65,7 @@
 #include "path-lookup.h"
 #include "path-util.h"
 #include "process-util.h"
+#include "rlimit-util.h"
 #include "set.h"
 #include "signal-util.h"
 #include "socket-util.h"
index 0e5ab1645fb85a8c2badc93ce2faf6495f92eff9..ed4abdbf12e2d627ca50b0e7e1d72a070594db13 100644 (file)
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#include "architecture.h"
 #include "automount.h"
+#include "bus-xml-policy.h"
+#include "busname.h"
 #include "cgroup.h"
 #include "compress.h"
 #include "condition.h"
 #include "execute.h"
 #include "install.h"
 #include "job.h"
+#include "journald-server.h"
 #include "kill.h"
+#include "link-config.h"
+#include "locale-util.h"
 #include "log.h"
 #include "logs-show.h"
 #include "mount.h"
 #include "unit-name.h"
 #include "unit.h"
 #include "util.h"
-#include "architecture.h"
-#include "link-config.h"
-#include "bus-xml-policy.h"
-#include "busname.h"
-#include "journald-server.h"
-#include "locale-util.h"
+#include "rlimit-util.h"
 
 #include "test-tables.h"