]> granicus.if.org Git - libnl/commitdiff
lib/utils.c: add mutex to get_psched_settings()
authorThomas Haller <thaller@redhat.com>
Tue, 17 Jan 2017 17:49:42 +0000 (18:49 +0100)
committerThomas Haller <thaller@redhat.com>
Tue, 17 Jan 2017 17:53:54 +0000 (18:53 +0100)
Let's add a mutex to get_psched_settings() hoping to solve worst
case scenarios when calling get_psched_settings() from multiple
threads.

Also, only read the environment variables once, who knows whether
they are modified concurrently.

lib/utils.c

index 7a791e1a7280cbda9402a8052bacbb7011c6d90b..fead196ba1c48f799713947554e77f3a9f149b66 100644 (file)
@@ -432,13 +432,20 @@ static void get_psched_settings(void)
        char name[FILENAME_MAX];
        FILE *fd;
        int got_hz = 0;
-       static int initialized = 0;
-       if (initialized == 1) {
+       static volatile int initialized = 0;
+       const char *ev;
+       NL_LOCK(mutex);
+
+       if (initialized == 1)
                return;
-       }
 
-       if (getenv("HZ")) {
-               long hz = strtol(getenv("HZ"), NULL, 0);
+       nl_lock(&mutex);
+
+       if (initialized == 1)
+               return;
+
+       if ((ev = getenv("HZ"))) {
+               long hz = strtol(ev, NULL, 0);
 
                if (LONG_MIN != hz && LONG_MAX != hz) {
                        user_hz = hz;
@@ -451,16 +458,15 @@ static void get_psched_settings(void)
 
        psched_hz = user_hz;
 
-       if (getenv("TICKS_PER_USEC")) {
-               double t = strtod(getenv("TICKS_PER_USEC"), NULL);
+       if ((ev = getenv("TICKS_PER_USEC"))) {
+               double t = strtod(ev, NULL);
                ticks_per_usec = t;
        }
        else {
-               if (getenv("PROC_NET_PSCHED"))
-                       snprintf(name, sizeof(name), "%s", getenv("PROC_NET_PSCHED"));
-               else if (getenv("PROC_ROOT"))
-                       snprintf(name, sizeof(name), "%s/net/psched",
-                                getenv("PROC_ROOT"));
+               if ((ev = getenv("PROC_NET_PSCHED")))
+                       snprintf(name, sizeof(name), "%s", ev);
+               else if ((ev = getenv("PROC_ROOT")))
+                       snprintf(name, sizeof(name), "%s/net/psched", ev);
                else
                        strncpy(name, "/proc/net/psched", sizeof(name) - 1);
 
@@ -485,6 +491,8 @@ static void get_psched_settings(void)
                }
        }
        initialized = 1;
+
+       nl_unlock(&mutex);
 }