snmp_agent: Avoid race conditions between thread start and locks initialization.
authorPavel Rochnyack <pavel2000@ngs.ru>
Wed, 12 Jul 2017 14:06:22 +0000 (21:06 +0700)
committerPavel Rochnyack <pavel2000@ngs.ru>
Thu, 13 Jul 2017 15:20:45 +0000 (22:20 +0700)
Lock initialization should be done before starting pthread which uses them.

src/snmp_agent.c

index 0908b4e..1ea5cc9 100644 (file)
@@ -1406,13 +1406,6 @@ static int snmp_agent_init(void) {
   if (ret != 0)
     return ret;
 
-  /* create a second thread to listen for requests from AgentX*/
-  ret = pthread_create(&g_agent->thread, NULL, &snmp_agent_thread_run, NULL);
-  if (ret != 0) {
-    ERROR(PLUGIN_NAME ": Failed to create a separate thread, err %u", ret);
-    return ret;
-  }
-
   ret = pthread_mutex_init(&g_agent->lock, NULL);
   if (ret != 0) {
     ERROR(PLUGIN_NAME ": Failed to initialize mutex, err %u", ret);
@@ -1425,6 +1418,13 @@ static int snmp_agent_init(void) {
     return ret;
   }
 
+  /* create a second thread to listen for requests from AgentX*/
+  ret = pthread_create(&g_agent->thread, NULL, &snmp_agent_thread_run, NULL);
+  if (ret != 0) {
+    ERROR(PLUGIN_NAME ": Failed to create a separate thread, err %u", ret);
+    return ret;
+  }
+
   return 0;
 }