summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Kralevich <nnk@google.com>2017-01-14 11:11:34 -0800
committerNick Kralevich <nnk@google.com>2017-02-09 10:53:09 -0800
commit6760f904f2cc81a73eda28cff0234b0d8cd2f053 (patch)
tree68535a7b6614e96fb1974e5112629f0a165f03af
parent821e05033ee1c7c8f0ced18831e02259c4c09b44 (diff)
downloadlibnl-6760f904f2cc81a73eda28cff0234b0d8cd2f053.tar.gz
lib/utils.c: lazy initialize user_hz and psched_hz
Rather than initializing user_hz and psched_hz when libnl is loaded, defer initialization of these variables to the first time they are used. This has several advantages: 1) Avoids an unnecessary permission denied error on /proc/net/psched, which can occur on systems where /proc/net isn't readable due to security policy. 2) Allows program code to initialize the environment variables PROC_NET_PSCHED and/or PROC_ROOT prior to the first libnl call, giving a program more flexibility about where libnl should look. 3) Trivially faster startup time (although unlikely to be significant). 4) Compiler may be able to prove that the get_psched_settings() function is unreachable and optimize appropriately, because the callers never (directly or indirectly) use this method. This could occur, for instance, in doing dead code elimination for programs which statically link libnl. Signed-off-by: Nick Kralevich <nnk@google.com> https://github.com/thom311/libnl/pull/123 (cherry picked from commit 8e0ead4b5bcf22dcc210442629ebb416116ea1f0) Test: Compiles and ueventd proc_net denial is gone. Bug: 35197529 Change-Id: Iad9ea207315c92489617334edeba73053d67cf6b
-rw-r--r--lib/utils.c9
1 files changed, 8 insertions, 1 deletions
diff --git a/lib/utils.c b/lib/utils.c
index 5cc9e94f..49cc20b2 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -403,11 +403,15 @@ static double ticks_per_usec = 1.0f;
* Supports the environment variables:
* PROC_NET_PSCHED - may point to psched file in /proc
* PROC_ROOT - may point to /proc fs */
-static void __init get_psched_settings(void)
+static void get_psched_settings(void)
{
char name[FILENAME_MAX];
FILE *fd;
int got_hz = 0;
+ static int initialized = 0;
+ if (initialized == 1) {
+ return;
+ }
if (getenv("HZ")) {
long hz = strtol(getenv("HZ"), NULL, 0);
@@ -456,6 +460,7 @@ static void __init get_psched_settings(void)
fclose(fd);
}
}
+ initialized = 1;
}
@@ -464,6 +469,7 @@ static void __init get_psched_settings(void)
*/
int nl_get_user_hz(void)
{
+ get_psched_settings();
return user_hz;
}
@@ -472,6 +478,7 @@ int nl_get_user_hz(void)
*/
int nl_get_psched_hz(void)
{
+ get_psched_settings();
return psched_hz;
}