misc: pass NULL instead of 0 for pointer in variadic functions
[vpp.git] / src / vlib / unix / main.c
old mode 100755 (executable)
new mode 100644 (file)
index 08f0506..6b1a32c
@@ -40,6 +40,7 @@
 #include <vlib/unix/unix.h>
 #include <vlib/unix/plugin.h>
 
+#include <limits.h>
 #include <signal.h>
 #include <sys/ucontext.h>
 #include <syslog.h>
@@ -67,10 +68,15 @@ unix_main_init (vlib_main_t * vm)
 {
   unix_main_t *um = &unix_main;
   um->vlib_main = vm;
-  return vlib_call_init_function (vm, unix_input_init);
+  return 0;
 }
 
-VLIB_INIT_FUNCTION (unix_main_init);
+/* *INDENT-OFF* */
+VLIB_INIT_FUNCTION (unix_main_init) =
+{
+  .runs_before = VLIB_INITS ("unix_input_init"),
+};
+/* *INDENT-ON* */
 
 static int
 unsetup_signal_handlers (int sig)
@@ -86,14 +92,20 @@ unsetup_signal_handlers (int sig)
 
 /* allocate this buffer from mheap when setting up the signal handler.
     dangerous to vec_resize it when crashing, mheap itself might have been
-    corruptted already */
+    corrupted already */
 static u8 *syslog_msg = 0;
+int vlib_last_signum = 0;
+uword vlib_last_faulting_address = 0;
 
 static void
 unix_signal_handler (int signum, siginfo_t * si, ucontext_t * uc)
 {
   uword fatal = 0;
 
+  /* These come in handy when looking at core files from optimized images */
+  vlib_last_signum = signum;
+  vlib_last_faulting_address = (uword) si->si_addr;
+
   syslog_msg = format (syslog_msg, "received signal %U, PC %U",
                       format_signal, signum, format_ucontext_pc, uc);
 
@@ -104,11 +116,16 @@ unix_signal_handler (int signum, siginfo_t * si, ucontext_t * uc)
     {
       /* these (caught) signals cause the application to exit */
     case SIGTERM:
-      if (unix_main.vlib_main->main_loop_exit_set)
+      /*
+       * Ignore SIGTERM if it's sent before we're ready.
+       */
+      if (unix_main.vlib_main && unix_main.vlib_main->main_loop_exit_set)
        {
          syslog (LOG_ERR | LOG_DAEMON, "received SIGTERM, exiting...");
          unix_main.vlib_main->main_loop_exit_now = 1;
        }
+      else
+       syslog (LOG_ERR | LOG_DAEMON, "IGNORE early SIGTERM...");
       break;
       /* fall through */
     case SIGQUIT:
@@ -128,6 +145,17 @@ unix_signal_handler (int signum, siginfo_t * si, ucontext_t * uc)
       break;
     }
 
+#ifdef CLIB_GCOV
+  /*
+   * Test framework sends SIGTERM, so we need to flush the
+   * code coverage stats here.
+   */
+  {
+    void __gcov_flush (void);
+    __gcov_flush ();
+  }
+#endif
+
   /* Null terminate. */
   vec_add1 (syslog_msg, 0);
 
@@ -150,10 +178,14 @@ unix_signal_handler (int signum, siginfo_t * si, ucontext_t * uc)
          syslog (LOG_ERR | LOG_DAEMON, "%s", syslog_msg);
        }
 
-      /* have to remove SIGABRT to avoid recusive - os_exit calling abort() */
+      /* have to remove SIGABRT to avoid recursive - os_exit calling abort() */
       unsetup_signal_handlers (SIGABRT);
 
-      os_exit (1);
+      /* os_exit(1) causes core generation, skip that for SIGINT, SIGHUP */
+      if (signum == SIGINT || signum == SIGHUP)
+       os_exit (0);
+      else
+       os_exit (1);
     }
   else
     clib_warning ("%s", syslog_msg);
@@ -179,9 +211,11 @@ setup_signal_handlers (unix_main_t * um)
        {
          /* these signals take the default action */
        case SIGKILL:
+       case SIGCONT:
        case SIGSTOP:
        case SIGUSR1:
        case SIGUSR2:
+       case SIGPROF:
          continue;
 
          /* ignore SIGPIPE, SIGCHLD */
@@ -207,21 +241,14 @@ unix_error_handler (void *arg, u8 * msg, int msg_len)
 {
   unix_main_t *um = arg;
 
-  /* Echo to stderr when interactive. */
-  if (um->flags & UNIX_FLAG_INTERACTIVE)
+  /* Echo to stderr when interactive or syslog is disabled. */
+  if (um->flags & (UNIX_FLAG_INTERACTIVE | UNIX_FLAG_NOSYSLOG))
     {
       CLIB_UNUSED (int r) = write (2, msg, msg_len);
     }
   else
     {
-      char save = msg[msg_len - 1];
-
-      /* Null Terminate. */
-      msg[msg_len - 1] = 0;
-
-      syslog (LOG_ERR | LOG_DAEMON, "%s", msg);
-
-      msg[msg_len - 1] = save;
+      syslog (LOG_ERR | LOG_DAEMON, "%.*s", msg_len, msg);
     }
 }
 
@@ -234,20 +261,10 @@ vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error)
     return;
 
   {
-    char save;
-    u8 *msg;
-    u32 msg_len;
-
-    msg = error->what;
-    msg_len = vec_len (msg);
-
-    /* Null Terminate. */
-    save = msg[msg_len - 1];
-    msg[msg_len - 1] = 0;
-
-    syslog (LOG_ERR | LOG_DAEMON, "%s", msg);
-
-    msg[msg_len - 1] = save;
+    u8 *msg = error->what;
+    u32 len = vec_len (msg);
+    int msg_len = (len > INT_MAX) ? INT_MAX : len;
+    syslog (LOG_ERR | LOG_DAEMON, "%.*s", msg_len, msg);
   }
 }
 
@@ -299,7 +316,7 @@ startup_config_process (vlib_main_t * vm,
          n = read (fd, buf + l, 4096);
          if (n > 0)
            {
-             _vec_len (buf) = l + n;
+             vec_set_len (buf, l + n);
              if (n < 4096)
                break;
            }
@@ -310,17 +327,16 @@ startup_config_process (vlib_main_t * vm,
        {
          u8 *lv = 0;
          lv = format (lv, "%U: ***** Startup Config *****\n%v",
-                      format_timeval, 0 /* current bat-time */ ,
-                      0 /* current bat-format */ ,
-                      buf);
+                      format_timeval, NULL /* current bat-format */,
+                      0 /* current bat-time */, buf);
          {
            int rv __attribute__ ((unused)) =
              write (um->log_fd, lv, vec_len (lv));
          }
          vec_reset_length (lv);
-         lv = format (lv, "%U: ***** End Startup Config *****\n",
-                      format_timeval, 0 /* current bat-time */ ,
-                      0 /* current bat-format */ );
+         lv =
+           format (lv, "%U: ***** End Startup Config *****\n", format_timeval,
+                   NULL /* current bat-format */, 0 /* current bat-time */);
          {
            int rv __attribute__ ((unused)) =
              write (um->log_fd, lv, vec_len (lv));
@@ -330,8 +346,20 @@ startup_config_process (vlib_main_t * vm,
 
       if (vec_len (buf))
        {
+         unformat_input_t in;
          unformat_init_vector (&sub_input, buf);
-         vlib_cli_input (vm, &sub_input, 0, 0);
+
+         while (unformat_user (&sub_input, unformat_vlib_cli_line, &in))
+           {
+             if (vlib_cli_input (vm, &in, 0, 0) != 0)
+               {
+                 /* cli failed - stop */
+                 unformat_free (&in);
+                 break;
+               }
+             unformat_free (&in);
+           }
+
          /* frees buf for us */
          unformat_free (&sub_input);
        }
@@ -345,12 +373,14 @@ VLIB_REGISTER_NODE (startup_config_node,static) = {
     .function = startup_config_process,
     .type = VLIB_NODE_TYPE_PROCESS,
     .name = "startup-config-process",
+    .process_log2_n_stack_bytes = 18,
 };
 /* *INDENT-ON* */
 
 static clib_error_t *
 unix_config (vlib_main_t * vm, unformat_input_t * input)
 {
+  vlib_global_main_t *vgm = vlib_get_global_main ();
   unix_main_t *um = &unix_main;
   clib_error_t *error = 0;
   gid_t gid;
@@ -367,6 +397,12 @@ unix_config (vlib_main_t * vm, unformat_input_t * input)
        um->flags |= UNIX_FLAG_INTERACTIVE;
       else if (unformat (input, "nodaemon"))
        um->flags |= UNIX_FLAG_NODAEMON;
+      else if (unformat (input, "nosyslog"))
+       um->flags |= UNIX_FLAG_NOSYSLOG;
+      else if (unformat (input, "nocolor"))
+       um->flags |= UNIX_FLAG_NOCOLOR;
+      else if (unformat (input, "nobanner"))
+       um->flags |= UNIX_FLAG_NOBANNER;
       else if (unformat (input, "cli-prompt %s", &cli_prompt))
        vlib_unix_cli_set_prompt (cli_prompt);
       else
@@ -440,9 +476,8 @@ unix_config (vlib_main_t * vm, unformat_input_t * input)
            {
              u8 *lv = 0;
              lv = format (0, "%U: ***** Start: PID %d *****\n",
-                          format_timeval, 0 /* current bat-time */ ,
-                          0 /* current bat-format */ ,
-                          getpid ());
+                          format_timeval, NULL /* current bat-format */,
+                          0 /* current bat-time */, getpid ());
              {
                int rv __attribute__ ((unused)) =
                  write (um->log_fd, lv, vec_len (lv));
@@ -473,6 +508,11 @@ unix_config (vlib_main_t * vm, unformat_input_t * input)
                                  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;
@@ -491,9 +531,9 @@ unix_config (vlib_main_t * vm, unformat_input_t * input)
        }
     }
 
-  if (!(um->flags & UNIX_FLAG_INTERACTIVE))
+  if (!(um->flags & (UNIX_FLAG_INTERACTIVE | UNIX_FLAG_NOSYSLOG)))
     {
-      openlog (vm->name, LOG_CONS | LOG_PERROR | LOG_PID, LOG_DAEMON);
+      openlog (vgm->name, LOG_CONS | LOG_PERROR | LOG_PID, LOG_DAEMON);
       clib_error_register_handler (unix_error_handler, um);
 
       if (!(um->flags & UNIX_FLAG_NODAEMON) && daemon ( /* chdir to / */ 0,
@@ -531,6 +571,17 @@ unix_config (vlib_main_t * vm, unformat_input_t * input)
  * Do not fork or background the VPP process. Typically used when invoking
  * VPP applications from a process monitor.
  *
+ * @cfgcmd{nosyslog}
+ * Do not send e.g. clib_warning(...) output to syslog. Used
+ * when invoking VPP applications from a process monitor which
+ * pipe stdout/stderr to a dedicated logger service.
+ *
+ * @cfgcmd{nocolor}
+ * Do not use colors in outputs.
+ * *
+ * @cfgcmd{nobanner}
+ * Do not display startup banner.
+ *
  * @cfgcmd{exec, &lt;filename&gt;}
  * @par <code>startup-config &lt;filename&gt;</code>
  * Read startup operational configuration from @c filename.
@@ -553,10 +604,11 @@ unix_config (vlib_main_t * vm, unformat_input_t * input)
  *
  * @cfgcmd{runtime-dir}
  * Define directory where VPP is going to store all runtime files.
- * Default is /run/vpp.
+ * Default is /run/vpp when running as root, /run/user/<UID>/vpp if running as
+ * an unprivileged user.
  *
  * @cfgcmd{cli-listen, &lt;address:port&gt;}
- * Bind the CLI to listen at the address and port given. @clocalhost
+ * Bind the CLI to listen at the address and port given. @c localhost
  * on TCP port @c 5002, given as <tt>cli-listen localhost:5002</tt>,
  * is typical.
  *
@@ -568,7 +620,7 @@ unix_config (vlib_main_t * vm, unformat_input_t * input)
  * Configure the CLI prompt to be @c string.
  *
  * @cfgcmd{cli-history-limit, &lt;nn&gt;}
- * Limit commmand history to @c nn  lines. A value of @c 0
+ * Limit command history to @c nn  lines. A value of @c 0
  * disables command history. Default value: @c 50
  *
  * @cfgcmd{cli-no-banner}
@@ -580,6 +632,12 @@ unix_config (vlib_main_t * vm, unformat_input_t * input)
  * @cfgcmd{cli-pager-buffer-limit, &lt;nn&gt;}
  * Limit pager buffer to @c nn lines of output.
  * A value of @c 0 disables the pager. Default value: @c 100000
+ *
+ * @cfgcmd{gid, &lt;nn&gt;}
+ * Set the effective gid under which the vpp process is to run.
+ *
+ * @cfgcmd{poll-sleep-usec, &lt;nn&gt;}
+ * Set a fixed poll sleep interval between main loop polls.
 ?*/
 VLIB_EARLY_CONFIG_FUNCTION (unix_config, "unix");
 
@@ -599,10 +657,13 @@ static uword
 thread0 (uword arg)
 {
   vlib_main_t *vm = (vlib_main_t *) arg;
+  vlib_global_main_t *vgm = vlib_get_global_main ();
   unformat_input_t input;
   int i;
 
-  unformat_init_command_line (&input, (char **) vm->argv);
+  vlib_process_finish_switch_stack (vm);
+
+  unformat_init_command_line (&input, (char **) vgm->argv);
   i = vlib_main (vm, &input);
   unformat_free (&input);
 
@@ -612,36 +673,60 @@ thread0 (uword arg)
 u8 *
 vlib_thread_stack_init (uword thread_index)
 {
-  vec_validate (vlib_thread_stacks, thread_index);
-  vlib_thread_stacks[thread_index] = clib_mem_alloc_aligned
-    (VLIB_THREAD_STACK_SIZE, VLIB_THREAD_STACK_SIZE);
+  void *stack;
+  ASSERT (thread_index < vec_len (vlib_thread_stacks));
+  stack = clib_mem_vm_map_stack (VLIB_THREAD_STACK_SIZE,
+                                CLIB_MEM_PAGE_SZ_DEFAULT,
+                                "thread stack: thread %u", thread_index);
 
-  /*
-   * Disallow writes to the bottom page of the stack, to
-   * catch stack overflows.
-   */
-  if (mprotect (vlib_thread_stacks[thread_index],
-               clib_mem_get_page_size (), PROT_READ) < 0)
-    clib_unix_warning ("thread stack");
-  return vlib_thread_stacks[thread_index];
+  if (stack == CLIB_MEM_VM_MAP_FAILED)
+    clib_panic ("failed to allocate thread %u stack", thread_index);
+
+  vlib_thread_stacks[thread_index] = stack;
+  return stack;
 }
 
+#ifndef PATH_MAX
+#define PATH_MAX 4096
+#endif
+
 int
 vlib_unix_main (int argc, char *argv[])
 {
-  vlib_main_t *vm = &vlib_global_main; /* one and only time for this! */
+  vlib_global_main_t *vgm = vlib_get_global_main ();
+  vlib_main_t *vm = vlib_get_first_main (); /* one and only time for this! */
   unformat_input_t input;
   clib_error_t *e;
+  char buffer[PATH_MAX];
   int i;
 
-  vm->argv = (u8 **) argv;
-  vm->name = argv[0];
-  vm->heap_base = clib_mem_get_heap ();
-  vm->heap_aligned_base = (void *)
-    (((uword) vm->heap_base) & ~(VLIB_FRAME_ALIGN - 1));
-  ASSERT (vm->heap_base);
+  vec_validate_aligned (vgm->vlib_mains, 0, CLIB_CACHE_LINE_BYTES);
+
+  if ((i = readlink ("/proc/self/exe", buffer, sizeof (buffer) - 1)) > 0)
+    {
+      int j;
+      buffer[i] = 0;
+      vgm->exec_path = vec_new (char, i + 1);
+      clib_memcpy_fast (vgm->exec_path, buffer, i + 1);
+      for (j = i - 1; j > 0; j--)
+       if (buffer[j - 1] == '/')
+         break;
+      vgm->name = vec_new (char, i - j + 1);
+      clib_memcpy_fast (vgm->name, buffer + j, i - j + 1);
+    }
+  else
+    vgm->exec_path = vgm->name = argv[0];
+
+  vgm->argv = (u8 **) argv;
+
+  clib_time_init (&vm->clib_time);
+
+  /* Turn on the event logger at the first possible moment */
+  vgm->configured_elog_ring_size = 128 << 10;
+  elog_init (vlib_get_elog_main (), vgm->configured_elog_ring_size);
+  elog_enable_disable (vlib_get_elog_main (), 1);
 
-  unformat_init_command_line (&input, (char **) vm->argv);
+  unformat_init_command_line (&input, (char **) vgm->argv);
   if ((e = vlib_plugin_config (vm, &input)))
     {
       clib_error_report (e);
@@ -653,9 +738,9 @@ vlib_unix_main (int argc, char *argv[])
   if (i)
     return i;
 
-  unformat_init_command_line (&input, (char **) vm->argv);
-  if (vm->init_functions_called == 0)
-    vm->init_functions_called = hash_create (0, /* value bytes */ 0);
+  unformat_init_command_line (&input, (char **) vgm->argv);
+  if (vgm->init_functions_called == 0)
+    vgm->init_functions_called = hash_create (0, /* value bytes */ 0);
   e = vlib_call_all_config_functions (vm, &input, 1 /* early */ );
   if (e != 0)
     {
@@ -665,13 +750,15 @@ vlib_unix_main (int argc, char *argv[])
   unformat_free (&input);
 
   /* always load symbols, for signal handler and mheap memory get/put backtrace */
-  clib_elf_main_init (vm->name);
+  clib_elf_main_init (vgm->exec_path);
 
+  vec_validate (vlib_thread_stacks, 0);
   vlib_thread_stack_init (0);
 
   __os_thread_index = 0;
   vm->thread_index = 0;
 
+  vlib_process_start_switch_stack (vm, 0);
   i = clib_calljmp (thread0, (uword) vm,
                    (void *) (vlib_thread_stacks[0] +
                              VLIB_THREAD_STACK_SIZE));