From ee0fa947ce0b65edd961678663bb3b03d29b5d31 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Fri, 7 Jul 2023 16:53:12 -0400 Subject: [PATCH] MT#56374 move resources() to lib Useful helper function for other components Change-Id: Idb8ab38e3ac27461b71e28029e7abc0bfc0529c2 --- daemon/main.c | 17 ----------------- include/helpers.h | 10 ---------- lib/auxlib.c | 18 ++++++++++++++++++ lib/auxlib.h | 15 ++++++++++++++- 4 files changed, 32 insertions(+), 28 deletions(-) diff --git a/daemon/main.c b/daemon/main.c index 03f0b0d9c..8760f6046 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -3,7 +3,6 @@ #include #include #include -#include #include #include #include @@ -165,22 +164,6 @@ static void signals(void) { pthread_sigmask(SIG_SETMASK, &ss, NULL); } -static void resources(void) { - struct rlimit rl; - int tryv; - - rlim(RLIMIT_CORE, RLIM_INFINITY); - - if (getrlimit(RLIMIT_NOFILE, &rl)) - rl.rlim_cur = 0; - for (tryv = ((1<<20) - 1); tryv && tryv > rl.rlim_cur && rlim(RLIMIT_NOFILE, tryv) == -1; tryv >>= 1) - ; - - rlim(RLIMIT_DATA, RLIM_INFINITY); - rlim(RLIMIT_RSS, RLIM_INFINITY); - rlim(RLIMIT_AS, RLIM_INFINITY); -} - static void __find_if_name(char *s, struct ifaddrs *ifas, GQueue *addrs) { diff --git a/include/helpers.h b/include/helpers.h index 64497326c..e86ce2bc9 100644 --- a/include/helpers.h +++ b/include/helpers.h @@ -13,7 +13,6 @@ #include #include #include -#include #include #include #include @@ -251,15 +250,6 @@ INLINE void swap_ptrs(void *a, void *b) { *bb = t; } -INLINE int rlim(int res, rlim_t val) { - struct rlimit rlim; - - ZERO(rlim); - rlim.rlim_cur = rlim.rlim_max = val; - return setrlimit(res, &rlim); -} - - /*** INET ADDRESS HELPERS ***/ diff --git a/lib/auxlib.c b/lib/auxlib.c index 108cc93b2..ac63c455c 100644 --- a/lib/auxlib.c +++ b/lib/auxlib.c @@ -12,6 +12,7 @@ #endif #include #include +#include #include "log.h" #include "loglib.h" @@ -69,6 +70,23 @@ void service_notify(const char *message) { } +void resources(void) { + struct rlimit rl; + int tryv; + + rlim(RLIMIT_CORE, RLIM_INFINITY); + + if (getrlimit(RLIMIT_NOFILE, &rl)) + rl.rlim_cur = 0; + for (tryv = ((1<<20) - 1); tryv && tryv > rl.rlim_cur && rlim(RLIMIT_NOFILE, tryv) == -1; tryv >>= 1) + ; + + rlim(RLIMIT_DATA, RLIM_INFINITY); + rlim(RLIMIT_RSS, RLIM_INFINITY); + rlim(RLIMIT_AS, RLIM_INFINITY); +} + + static unsigned int options_length(const GOptionEntry *arr) { unsigned int len = 0; for (const GOptionEntry *p = arr; p->long_name; p++) diff --git a/lib/auxlib.h b/lib/auxlib.h index cbe504d7b..296262acb 100644 --- a/lib/auxlib.h +++ b/lib/auxlib.h @@ -8,6 +8,7 @@ #include #include #include +#include #define THREAD_BUF_SIZE 64 @@ -412,6 +413,19 @@ INLINE long unsigned int ssl_random(void) { } +INLINE int rlim(int res, rlim_t val) { + struct rlimit rlim; + + ZERO(rlim); + rlim.rlim_cur = rlim.rlim_max = val; + return setrlimit(res, &rlim); +} + + +void resources(void); + + + /*** TAINT FUNCTIONS ***/ #if __has_attribute(__error__) @@ -431,5 +445,4 @@ taint_func(random, "use ssl_random() instead"); taint_func(srandom, "use rtpe_ssl_init() instead"); - #endif