From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Benjamin Marzinski Date: Mon, 8 Oct 2018 11:41:03 -0500 Subject: [PATCH] libmultipath: remove max_fds code duplication Instead of multipath, multipathd, and mpathpersist all having code to set the max number of open file descriptors, just use a util function to do it. Signed-off-by: Benjamin Marzinski --- libmpathpersist/mpath_persist.c | 11 +---------- libmultipath/util.c | 31 +++++++++++++++++++++++++++++++ libmultipath/util.h | 1 + multipath/main.c | 12 +----------- multipathd/main.c | 31 +++---------------------------- 5 files changed, 37 insertions(+), 49 deletions(-) diff --git a/libmpathpersist/mpath_persist.c b/libmpathpersist/mpath_persist.c index 4229a94..29e7fb4 100644 --- a/libmpathpersist/mpath_persist.c +++ b/libmpathpersist/mpath_persist.c @@ -31,7 +31,6 @@ #include #include #include -#include #define __STDC_FORMAT_MACROS 1 @@ -48,15 +47,7 @@ mpath_lib_init (void) return NULL; } - if (conf->max_fds) { - struct rlimit fd_limit; - - fd_limit.rlim_cur = conf->max_fds; - fd_limit.rlim_max = conf->max_fds; - if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0) - condlog(0, "can't set open fds limit to %d : %s", - conf->max_fds, strerror(errno)); - } + set_max_fds(conf->max_fds); return conf; } diff --git a/libmultipath/util.c b/libmultipath/util.c index 347af5b..d08112d 100644 --- a/libmultipath/util.c +++ b/libmultipath/util.c @@ -7,6 +7,8 @@ #include #include #include +#include +#include #include #include #include @@ -465,3 +467,32 @@ int safe_write(int fd, const void *buf, size_t count) } return 0; } + +void set_max_fds(int max_fds) +{ + struct rlimit fd_limit; + + if (!max_fds) + return; + + if (getrlimit(RLIMIT_NOFILE, &fd_limit) < 0) { + condlog(0, "can't get open fds limit: %s", + strerror(errno)); + fd_limit.rlim_cur = 0; + fd_limit.rlim_max = 0; + } + if (fd_limit.rlim_cur < max_fds) { + fd_limit.rlim_cur = max_fds; + if (fd_limit.rlim_max < max_fds) + fd_limit.rlim_max = max_fds; + if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0) { + condlog(0, "can't set open fds limit to " + "%lu/%lu : %s", + fd_limit.rlim_cur, fd_limit.rlim_max, + strerror(errno)); + } else { + condlog(3, "set open fds limit to %lu/%lu", + fd_limit.rlim_cur, fd_limit.rlim_max); + } + } +} diff --git a/libmultipath/util.h b/libmultipath/util.h index 56cec76..c246295 100644 --- a/libmultipath/util.h +++ b/libmultipath/util.h @@ -21,6 +21,7 @@ int get_linux_version_code(void); int parse_prkey(char *ptr, uint64_t *prkey); int parse_prkey_flags(char *ptr, uint64_t *prkey, uint8_t *flags); int safe_write(int fd, const void *buf, size_t count); +void set_max_fds(int max_fds); #define KERNEL_VERSION(maj, min, ptc) ((((maj) * 256) + (min)) * 256 + (ptc)) diff --git a/multipath/main.c b/multipath/main.c index d5aad95..05b7bf0 100644 --- a/multipath/main.c +++ b/multipath/main.c @@ -56,8 +56,6 @@ #include "pgpolicies.h" #include "version.h" #include -#include -#include #include "wwids.h" #include "uxsock.h" #include "mpath_cmd.h" @@ -1002,15 +1000,7 @@ main (int argc, char *argv[]) logsink = 1; } - if (conf->max_fds) { - struct rlimit fd_limit; - - fd_limit.rlim_cur = conf->max_fds; - fd_limit.rlim_max = conf->max_fds; - if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0) - condlog(0, "can't set open fds limit to %d : %s", - conf->max_fds, strerror(errno)); - } + set_max_fds(conf->max_fds); libmp_udev_set_sync_support(1); diff --git a/multipathd/main.c b/multipathd/main.c index 5f0193b..d3f7719 100644 --- a/multipathd/main.c +++ b/multipathd/main.c @@ -12,8 +12,6 @@ #include #include #include -#include -#include #include #include #include @@ -2663,33 +2661,10 @@ child (void * param) envp = getenv("LimitNOFILE"); - if (envp) { + if (envp) condlog(2,"Using systemd provided open fds limit of %s", envp); - } else if (conf->max_fds) { - struct rlimit fd_limit; - - if (getrlimit(RLIMIT_NOFILE, &fd_limit) < 0) { - condlog(0, "can't get open fds limit: %s", - strerror(errno)); - fd_limit.rlim_cur = 0; - fd_limit.rlim_max = 0; - } - if (fd_limit.rlim_cur < conf->max_fds) { - fd_limit.rlim_cur = conf->max_fds; - if (fd_limit.rlim_max < conf->max_fds) - fd_limit.rlim_max = conf->max_fds; - if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0) { - condlog(0, "can't set open fds limit to " - "%lu/%lu : %s", - fd_limit.rlim_cur, fd_limit.rlim_max, - strerror(errno)); - } else { - condlog(3, "set open fds limit to %lu/%lu", - fd_limit.rlim_cur, fd_limit.rlim_max); - } - } - - } + else + set_max_fds(conf->max_fds); vecs = gvecs = init_vecs(); if (!vecs) -- 2.7.4