X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fvlib%2Funix%2Fmain.c;h=7f1a8f35126dabfeb0e6a681b286ea75d09a76c3;hb=10a8bda37eed33ada1e7c6ece7bda1fe066ba541;hp=103576db01cbbc55800b5a147a14c00a79796f4f;hpb=f55f9b851f59264d737d92c6277a87588c565d24;p=vpp.git diff --git a/src/vlib/unix/main.c b/src/vlib/unix/main.c old mode 100644 new mode 100755 index 103576db01c..7f1a8f35126 --- a/src/vlib/unix/main.c +++ b/src/vlib/unix/main.c @@ -48,6 +48,7 @@ #include #include #include +#include /** Default CLI pager limit is not configured in startup.conf */ #define UNIX_CLI_DEFAULT_PAGER_LIMIT 100000 @@ -55,42 +56,76 @@ /** Default CLI history depth if not configured in startup.conf */ #define UNIX_CLI_DEFAULT_HISTORY 50 +char *vlib_default_runtime_dir __attribute__ ((weak)); +char *vlib_default_runtime_dir = "vlib"; unix_main_t unix_main; +clib_file_main_t file_main; static clib_error_t * 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; +} + +/* *INDENT-OFF* */ +VLIB_INIT_FUNCTION (unix_main_init) = +{ + .runs_before = VLIB_INITS ("unix_input_init"), +}; +/* *INDENT-ON* */ + +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); } -VLIB_INIT_FUNCTION (unix_main_init); + +/* allocate this buffer from mheap when setting up the signal handler. + dangerous to vec_resize it when crashing, mheap itself might have been + corrupted already */ +static u8 *syslog_msg = 0; +static int last_signum = 0; +static uword last_faulting_address = 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); + /* These come in handy when looking at core files from optimized images */ + last_signum = signum; + last_faulting_address = (uword) si->si_addr; + + 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) { /* 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..."); - - clib_longjmp (&unix_main.vlib_main->main_loop_exit, - VLIB_MAIN_LOOP_EXIT_CLI); + unix_main.vlib_main->main_loop_exit_now = 1; } + else + syslog (LOG_ERR | LOG_DAEMON, "IGNORE early SIGTERM..."); + break; /* fall through */ case SIGQUIT: case SIGINT: @@ -99,6 +134,7 @@ unix_signal_handler (int signum, siginfo_t * si, ucontext_t * uc) case SIGSEGV: case SIGHUP: case SIGFPE: + case SIGABRT: fatal = 1; break; @@ -108,18 +144,51 @@ 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 (msg, 0); + vec_add1 (syslog_msg, 0); if (fatal) { - syslog (LOG_ERR | LOG_DAEMON, "%s", msg); - os_exit (1); + 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 recursive - os_exit calling abort() */ + unsetup_signal_handlers (SIGABRT); + + /* os_exit(1) causes core generation, do not do this for SIGINT */ + if (signum == SIGINT) + os_exit (0); + else + os_exit (1); } else - clib_warning ("%s", msg); + clib_warning ("%s", syslog_msg); - vec_free (msg); } static clib_error_t * @@ -128,16 +197,18 @@ 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)); + clib_memset (&sa, 0, sizeof (sa)); sa.sa_sigaction = (void *) unix_signal_handler; sa.sa_flags = SA_SIGINFO; switch (i) { /* these signals take the default action */ - case SIGABRT: case SIGKILL: case SIGSTOP: case SIGUSR1: @@ -305,6 +376,7 @@ 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* */ @@ -313,6 +385,8 @@ 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; @@ -330,12 +404,16 @@ unix_config (vlib_main_t * vm, unformat_input_t * input) else if (unformat (input, "cli-listen %s", &um->cli_listen_socket.config)) ; + else if (unformat (input, "runtime-dir %s", &um->runtime_dir)) + ; else if (unformat (input, "cli-line-mode")) um->cli_line_mode = 1; else if (unformat (input, "cli-no-banner")) 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)) ; @@ -404,17 +482,54 @@ unix_config (vlib_main_t * vm, unformat_input_t * input) vec_free (lv); } } + else if (unformat (input, "gid %U", unformat_unix_gid, &gid)) + { + 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->flags & UNIX_FLAG_INTERACTIVE)) + if (um->runtime_dir == 0) { - error = setup_signal_handlers (um); - if (error) + 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); + } + + /* 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; + + 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); clib_error_register_handler (unix_error_handler, um); @@ -423,6 +538,20 @@ unix_config (vlib_main_t * vm, unformat_input_t * input) 0) < 0) clib_error_return (0, "daemon () fails"); } + + if (pidfd >= 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; @@ -452,12 +581,20 @@ 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, <filename>} + * 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. * + * @cfgcmd{runtime-dir} + * Define directory where VPP is going to store all runtime files. + * Default is /run/vpp when running as root, /run/user//vpp if running as + * an unprivileged user. + * * @cfgcmd{cli-listen, <address:port>} - * 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 cli-listen localhost:5002, * is typical. * @@ -469,7 +606,7 @@ unix_config (vlib_main_t * vm, unformat_input_t * input) * Configure the CLI prompt to be @c string. * * @cfgcmd{cli-history-limit, <nn>} - * 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} @@ -481,8 +618,14 @@ unix_config (vlib_main_t * vm, unformat_input_t * input) * @cfgcmd{cli-pager-buffer-limit, <nn>} * Limit pager buffer to @c nn lines of output. * A value of @c 0 disables the pager. Default value: @c 100000 + * + * @cfgcmd{gid, <nn>} + * Set the effective gid under which the vpp process is to run. + * + * @cfgcmd{poll-sleep-usec, <nn>} + * Set a fixed poll sleep interval between main loop polls. ?*/ -VLIB_CONFIG_FUNCTION (unix_config, "unix"); +VLIB_EARLY_CONFIG_FUNCTION (unix_config, "unix"); static clib_error_t * unix_exit (vlib_main_t * vm) @@ -513,9 +656,9 @@ thread0 (uword arg) u8 * vlib_thread_stack_init (uword thread_index) { - vec_validate (vlib_thread_stacks, thread_index); + ASSERT (thread_index < vec_len (vlib_thread_stacks)); vlib_thread_stacks[thread_index] = clib_mem_alloc_aligned - (VLIB_THREAD_STACK_SIZE, VLIB_THREAD_STACK_SIZE); + (VLIB_THREAD_STACK_SIZE, clib_mem_get_page_size ()); /* * Disallow writes to the bottom page of the stack, to @@ -538,6 +681,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); @@ -563,9 +708,14 @@ 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); + + vec_validate (vlib_thread_stacks, 0); vlib_thread_stack_init (0); __os_thread_index = 0; + vm->thread_index = 0; i = clib_calljmp (thread0, (uword) vm, (void *) (vlib_thread_stacks[0] +