Add config option to use dlmalloc instead of mheap
[vpp.git] / src / vlib / unix / main.c
old mode 100644 (file)
new mode 100755 (executable)
index f52316e..2420861
@@ -60,6 +60,8 @@ char *vlib_default_runtime_dir __attribute__ ((weak));
 char *vlib_default_runtime_dir = "vlib";
 
 unix_main_t unix_main;
+clib_file_main_t file_main;
+vlib_physmem_main_t physmem_main;
 
 static clib_error_t *
 unix_main_init (vlib_main_t * vm)
@@ -71,17 +73,33 @@ unix_main_init (vlib_main_t * vm)
 
 VLIB_INIT_FUNCTION (unix_main_init);
 
+static int
+unsetup_signal_handlers (int sig)
+{
+  struct sigaction sa;
+
+  sa.sa_handler = SIG_DFL;
+  sa.sa_flags = 0;
+  sigemptyset (&sa.sa_mask);
+  return sigaction (sig, &sa, 0);
+}
+
+
+/* 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 */
+static u8 *syslog_msg = 0;
+
 static void
 unix_signal_handler (int signum, siginfo_t * si, ucontext_t * uc)
 {
-  uword fatal;
-  u8 *msg = 0;
+  uword fatal = 0;
 
-  msg = format (msg, "received signal %U, PC %U",
-               format_signal, signum, format_ucontext_pc, uc);
+  syslog_msg = format (syslog_msg, "received signal %U, PC %U",
+                      format_signal, signum, format_ucontext_pc, uc);
 
   if (signum == SIGSEGV)
-    msg = format (msg, ", faulting address %p", si->si_addr);
+    syslog_msg = format (syslog_msg, ", faulting address %p", si->si_addr);
 
   switch (signum)
     {
@@ -90,10 +108,9 @@ unix_signal_handler (int signum, siginfo_t * si, ucontext_t * uc)
       if (unix_main.vlib_main->main_loop_exit_set)
        {
          syslog (LOG_ERR | LOG_DAEMON, "received SIGTERM, exiting...");
-
-         clib_longjmp (&unix_main.vlib_main->main_loop_exit,
-                       VLIB_MAIN_LOOP_EXIT_CLI);
+         unix_main.vlib_main->main_loop_exit_now = 1;
        }
+      break;
       /* fall through */
     case SIGQUIT:
     case SIGINT:
@@ -102,6 +119,7 @@ unix_signal_handler (int signum, siginfo_t * si, ucontext_t * uc)
     case SIGSEGV:
     case SIGHUP:
     case SIGFPE:
+    case SIGABRT:
       fatal = 1;
       break;
 
@@ -112,17 +130,35 @@ unix_signal_handler (int signum, siginfo_t * si, ucontext_t * uc)
     }
 
   /* Null terminate. */
-  vec_add1 (msg, 0);
+  vec_add1 (syslog_msg, 0);
 
   if (fatal)
     {
-      syslog (LOG_ERR | LOG_DAEMON, "%s", msg);
+      syslog (LOG_ERR | LOG_DAEMON, "%s", syslog_msg);
+
+      /* Address of callers: outer first, inner last. */
+      uword callers[15];
+      uword n_callers = clib_backtrace (callers, ARRAY_LEN (callers), 0);
+      int i;
+      for (i = 0; i < n_callers; i++)
+       {
+         vec_reset_length (syslog_msg);
+
+         syslog_msg =
+           format (syslog_msg, "#%-2d 0x%016lx %U%c", i, callers[i],
+                   format_clib_elf_symbol_with_address, callers[i], 0);
+
+         syslog (LOG_ERR | LOG_DAEMON, "%s", syslog_msg);
+       }
+
+      /* have to remove SIGABRT to avoid recusive - os_exit calling abort() */
+      unsetup_signal_handlers (SIGABRT);
+
       os_exit (1);
     }
   else
-    clib_warning ("%s", msg);
+    clib_warning ("%s", syslog_msg);
 
-  vec_free (msg);
 }
 
 static clib_error_t *
@@ -131,6 +167,9 @@ setup_signal_handlers (unix_main_t * um)
   uword i;
   struct sigaction sa;
 
+  /* give a big enough buffer for msg, most likely it can avoid vec_resize  */
+  vec_alloc (syslog_msg, 2048);
+
   for (i = 1; i < 32; i++)
     {
       memset (&sa, 0, sizeof (sa));
@@ -140,7 +179,6 @@ setup_signal_handlers (unix_main_t * um)
       switch (i)
        {
          /* these signals take the default action */
-       case SIGABRT:
        case SIGKILL:
        case SIGSTOP:
        case SIGUSR1:
@@ -317,6 +355,7 @@ unix_config (vlib_main_t * vm, unformat_input_t * input)
   unix_main_t *um = &unix_main;
   clib_error_t *error = 0;
   gid_t gid;
+  int pidfd = -1;
 
   /* Defaults */
   um->cli_pager_buffer_limit = UNIX_CLI_DEFAULT_PAGER_LIMIT;
@@ -342,6 +381,8 @@ unix_config (vlib_main_t * vm, unformat_input_t * input)
        um->cli_no_banner = 1;
       else if (unformat (input, "cli-no-pager"))
        um->cli_no_pager = 1;
+      else if (unformat (input, "poll-sleep-usec %d", &um->poll_sleep_usec))
+       ;
       else if (unformat (input, "cli-pager-buffer-limit %d",
                         &um->cli_pager_buffer_limit))
        ;
@@ -415,15 +456,42 @@ unix_config (vlib_main_t * vm, unformat_input_t * input)
          if (setegid (gid) == -1)
            return clib_error_return_unix (0, "setegid");
        }
+      else if (unformat (input, "pidfile %s", &um->pidfile))
+       ;
       else
        return clib_error_return (0, "unknown input `%U'",
                                  format_unformat_error, input);
     }
 
+  if (um->runtime_dir == 0)
+    {
+      uid_t uid = geteuid ();
+      if (uid == 00)
+       um->runtime_dir = format (0, "/run/%s%c",
+                                 vlib_default_runtime_dir, 0);
+      else
+       um->runtime_dir = format (0, "/run/user/%u/%s%c", uid,
+                                 vlib_default_runtime_dir, 0);
+    }
+
   error = setup_signal_handlers (um);
   if (error)
     return error;
 
+  if (um->pidfile)
+    {
+      if ((error = vlib_unix_validate_runtime_file (um,
+                                                   (char *) um->pidfile,
+                                                   &um->pidfile)))
+       return error;
+
+      if (((pidfd = open ((char *) um->pidfile,
+                         O_CREAT | O_WRONLY | O_TRUNC, 0644)) < 0))
+       {
+         return clib_error_return_unix (0, "open");
+       }
+    }
+
   if (!(um->flags & UNIX_FLAG_INTERACTIVE))
     {
       openlog (vm->name, LOG_CONS | LOG_PERROR | LOG_PID, LOG_DAEMON);
@@ -434,19 +502,21 @@ unix_config (vlib_main_t * vm, unformat_input_t * input)
                                                       0) < 0)
        clib_error_return (0, "daemon () fails");
     }
-  um->unix_config_complete = 1;
 
-  if (um->runtime_dir == 0)
+  if (pidfd >= 0)
     {
-      uid_t uid = geteuid ();
-      if (uid == 00)
-       um->runtime_dir = format (0, "/run/%s%c",
-                                 vlib_default_runtime_dir, 0);
-      else
-       um->runtime_dir = format (0, "/run/user/%u/%s%c", uid,
-                                 vlib_default_runtime_dir, 0);
+      u8 *lv = format (0, "%d", getpid ());
+      if (write (pidfd, (char *) lv, vec_len (lv)) != vec_len (lv))
+       {
+         vec_free (lv);
+         close (pidfd);
+         return clib_error_return_unix (0, "write");
+       }
+      vec_free (lv);
+      close (pidfd);
     }
 
+  um->unix_config_complete = 1;
 
   return 0;
 }
@@ -475,6 +545,9 @@ unix_config (vlib_main_t * vm, unformat_input_t * input)
  * Very useful in situations where folks don't remember or can't be bothered
  * to include CLI commands in bug reports.
  *
+ * @cfgcmd{pidfile, &lt;filename&gt;}
+ * Writes the pid of the main thread in @c filename.
+ *
  * @cfgcmd{full-coredump}
  * Ask the Linux kernel to dump all memory-mapped address regions, instead
  * of just text+data+bss.
@@ -565,6 +638,8 @@ vlib_unix_main (int argc, char *argv[])
   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);
 
   unformat_init_command_line (&input, (char **) vm->argv);
@@ -590,9 +665,13 @@ 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);
+
   vlib_thread_stack_init (0);
 
   __os_thread_index = 0;
+  vm->thread_index = 0;
 
   i = clib_calljmp (thread0, (uword) vm,
                    (void *) (vlib_thread_stacks[0] +