]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/common/ceph_argparse.cc
update sources to v12.2.3
[ceph.git] / ceph / src / common / ceph_argparse.cc
index 355224d0a84f0b975d10a722ed1d036145031207..7a2e8ea918232d449eb23827b6b4988f188bb960 100644 (file)
@@ -82,13 +82,20 @@ bool split_dashdash(const std::vector<const char*>& args,
   return dashdash;
 }
 
+static std::mutex g_str_vec_lock;
+static vector<string> g_str_vec;
+
+void clear_g_str_vec()
+{
+  g_str_vec_lock.lock();
+  g_str_vec.clear();
+  g_str_vec_lock.unlock();
+}
+
 void env_to_vec(std::vector<const char*>& args, const char *name)
 {
   if (!name)
     name = "CEPH_ARGS";
-  char *p = getenv(name);
-  if (!p)
-    return;
 
   bool dashdash = false;
   std::vector<const char*> options;
@@ -98,13 +105,25 @@ void env_to_vec(std::vector<const char*>& args, const char *name)
 
   std::vector<const char*> env_options;
   std::vector<const char*> env_arguments;
-  static vector<string> str_vec;
   std::vector<const char*> env;
-  str_vec.clear();
-  get_str_vec(p, " ", str_vec);
-  for (vector<string>::iterator i = str_vec.begin();
-       i != str_vec.end();
-       ++i)
+
+  /*
+   * We can only populate str_vec once. Other threads could hold pointers into
+   * it, so clearing it out and replacing it is not currently safe.
+   */
+  g_str_vec_lock.lock();
+  if (g_str_vec.empty()) {
+    char *p = getenv(name);
+    if (!p) {
+      g_str_vec_lock.unlock();
+      return;
+    }
+    get_str_vec(p, " ", g_str_vec);
+  }
+  g_str_vec_lock.unlock();
+
+  vector<string>::iterator i;
+  for (i = g_str_vec.begin(); i != g_str_vec.end(); ++i)
     env.push_back(i->c_str());
   if (split_dashdash(env, env_options, env_arguments))
     dashdash = true;