From 8d113f4aeccc18d4c473855fdd4c865297decdd9 Mon Sep 17 00:00:00 2001 From: Edward Tomasz Napierala Date: Sat, 10 Nov 2018 23:07:46 +0000 Subject: [PATCH] Don't call stat(2) on nsswitch.conf(5) every time nsdispatch(3) and dependent functions (eg getpwname(3)) get called. This can improve performance of binaries that perform a lot of name lookups, such as gssd(8). It also matches documented behaviour of Linux and Solaris. The old code is left in place, should anyone need it, guarded by #ifdef NS_REREAD_CONF. Reviewed by: imp, bcr MFC after: 2 weeks Relnotes: yes Sponsored by: DARPA, AFRL Differential Revision: https://reviews.freebsd.org/D17934 --- lib/libc/net/nsdispatch.c | 11 +++++++++++ share/man/man5/nsswitch.conf.5 | 8 +++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/libc/net/nsdispatch.c b/lib/libc/net/nsdispatch.c index b9533bd1ca4..b0f80d079b0 100644 --- a/lib/libc/net/nsdispatch.c +++ b/lib/libc/net/nsdispatch.c @@ -335,6 +335,7 @@ static int nss_configure(void) { static time_t confmod; + static int already_initialized = 0; struct stat statbuf; int result, isthreaded; const char *path; @@ -352,6 +353,16 @@ nss_configure(void) if (path == NULL) #endif path = _PATH_NS_CONF; +#ifndef NS_REREAD_CONF + /* + * Define NS_REREAD_CONF to have nsswitch notice changes + * to nsswitch.conf(5) during runtime. This involves calling + * stat(2) every time, which can result in performance hit. + */ + if (already_initialized) + return (0); + already_initialized = 1; +#endif /* NS_REREAD_CONF */ if (stat(path, &statbuf) != 0) return (0); if (statbuf.st_mtime <= confmod) diff --git a/share/man/man5/nsswitch.conf.5 b/share/man/man5/nsswitch.conf.5 index ec5d94c313f..4ebb4a38356 100644 --- a/share/man/man5/nsswitch.conf.5 +++ b/share/man/man5/nsswitch.conf.5 @@ -33,7 +33,7 @@ .\" .\" $FreeBSD$ .\" -.Dd June 6, 2016 +.Dd November 10, 2018 .Dt NSSWITCH.CONF 5 .Os .Sh NAME @@ -340,6 +340,12 @@ sets a policy of "if the user is notfound in nis, do not try files." This treats nis as the authoritative source of information, except when the server is down. .Sh NOTES +The +.Nm +file is parsed by each program only once. +Subsequent changes will not be applied until the program +is restarted. +.Pp If system got compiled with .Va WITHOUT_NIS you have to remove