2
0
mirror of https://github.com/openvswitch/ovs synced 2025-08-30 13:58:14 +00:00

socket-util: Make get_max_fds() and get_null_fd() thread-safe.

Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: Andy Zhou <azhou@nicira.com>
This commit is contained in:
Ben Pfaff
2013-04-23 16:19:21 -07:00
parent 8c45d00f56
commit 1d4fd3a5e7

View File

@@ -132,8 +132,10 @@ rlim_is_finite(rlim_t limit)
int int
get_max_fds(void) get_max_fds(void)
{ {
static int max_fds = -1; static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
if (max_fds < 0) { static int max_fds;
if (ovsthread_once_start(&once)) {
struct rlimit r; struct rlimit r;
if (!getrlimit(RLIMIT_NOFILE, &r) && rlim_is_finite(r.rlim_cur)) { if (!getrlimit(RLIMIT_NOFILE, &r) && rlim_is_finite(r.rlim_cur)) {
max_fds = r.rlim_cur; max_fds = r.rlim_cur;
@@ -141,7 +143,9 @@ get_max_fds(void)
VLOG_WARN("failed to obtain fd limit, defaulting to 1024"); VLOG_WARN("failed to obtain fd limit, defaulting to 1024");
max_fds = 1024; max_fds = 1024;
} }
ovsthread_once_done(&once);
} }
return max_fds; return max_fds;
} }
@@ -802,15 +806,19 @@ error:
int int
get_null_fd(void) get_null_fd(void)
{ {
static int null_fd = -1; static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
if (null_fd < 0) { static int null_fd;
if (ovsthread_once_start(&once)) {
null_fd = open("/dev/null", O_RDWR); null_fd = open("/dev/null", O_RDWR);
if (null_fd < 0) { if (null_fd < 0) {
int error = errno; int error = errno;
VLOG_ERR("could not open /dev/null: %s", ovs_strerror(error)); VLOG_ERR("could not open /dev/null: %s", ovs_strerror(error));
return -error; null_fd = -error;
} }
ovsthread_once_done(&once);
} }
return null_fd; return null_fd;
} }