vlib: create unix runtime directory

Ensure the runtime directory is created at startup.
Default /run/vpp

Type: fix
Fixes: I53d70939c8125d04a365ac51a6cbf8926dc52adf
Change-Id: I6d70364ea756b86768c4dd1f6a9383238ed275c8
Signed-off-by: Ole Troan <ot@cisco.com>
diff --git a/src/vlib/unix/main.c b/src/vlib/unix/main.c
index 3ec4c34..42c3064 100755
--- a/src/vlib/unix/main.c
+++ b/src/vlib/unix/main.c
@@ -500,6 +500,11 @@
 				  vlib_default_runtime_dir, 0);
     }
 
+  /* Ensure the runtime directory is created */
+  error = vlib_unix_recursive_mkdir ((char *) um->runtime_dir);
+  if (error)
+    return error;
+
   error = setup_signal_handlers (um);
   if (error)
     return error;
diff --git a/src/vpp/stats/stat_segment.c b/src/vpp/stats/stat_segment.c
index 7bb01b3..303d704 100644
--- a/src/vpp/stats/stat_segment.c
+++ b/src/vpp/stats/stat_segment.c
@@ -708,6 +708,26 @@
   return 0;			/* or not */
 }
 
+static clib_error_t *
+statseg_init (vlib_main_t * vm)
+{
+  stat_segment_main_t *sm = &stat_segment_main;
+
+  /* set default socket file name when statseg config stanza is empty. */
+  if (!vec_len (sm->socket_name))
+    sm->socket_name = format (0, "%s/%s", vlib_unix_get_runtime_dir (),
+			      STAT_SEGMENT_SOCKET_FILENAME);
+  return stats_segment_socket_init ();
+}
+
+/* *INDENT-OFF* */
+VLIB_INIT_FUNCTION (statseg_init) =
+{
+  .runs_after = VLIB_INITS("unix_input_init"),
+};
+/* *INDENT-ON* */
+
+
 clib_error_t *
 stat_segment_register_gauge (u8 * name, stat_segment_update_fn update_fn,
 			     u32 caller_index)
@@ -775,18 +795,14 @@
 				  format_unformat_error, input);
     }
 
-  /* set default socket file name when statseg config stanza is empty. */
-  if (!vec_len (sm->socket_name))
-    sm->socket_name = format (0, "%s/%s", vlib_unix_get_runtime_dir (),
-			      STAT_SEGMENT_SOCKET_FILENAME);
-
   /*
    * NULL-terminate socket name string
    * clib_socket_init()->socket_config() use C str*
    */
-  vec_terminate_c_string (sm->socket_name);
+  if (vec_len (sm->socket_name))
+    vec_terminate_c_string (sm->socket_name);
 
-  return stats_segment_socket_init ();
+  return 0;
 }
 
 static clib_error_t *
@@ -857,7 +873,7 @@
   return 0;
 }
 
-VLIB_CONFIG_FUNCTION (statseg_config, "statseg");
+VLIB_EARLY_CONFIG_FUNCTION (statseg_config, "statseg");
 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (statseg_sw_interface_add_del);
 
 /* *INDENT-OFF* */