X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fvlib%2Funix%2Fmain.c;h=786addf2e0a73c42af61d459e231aeebe9df6269;hb=88c062119b5e94ef19e02f007e38ee119b02218f;hp=6b96cc0db47bfc91426efafda3a93706ea19cc02;hpb=c8c44ebea155f15a9e3067fe05228a1e932185fc;p=vpp.git diff --git a/src/vlib/unix/main.c b/src/vlib/unix/main.c index 6b96cc0db47..786addf2e0a 100644 --- 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,8 +56,12 @@ /** 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; +vlib_physmem_main_t physmem_main; static clib_error_t * unix_main_init (vlib_main_t * vm) @@ -71,7 +76,7 @@ VLIB_INIT_FUNCTION (unix_main_init); static void unix_signal_handler (int signum, siginfo_t * si, ucontext_t * uc) { - uword fatal; + uword fatal = 0; u8 *msg = 0; msg = format (msg, "received signal %U, PC %U", @@ -87,10 +92,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: @@ -313,6 +317,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,6 +336,8 @@ 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")) @@ -404,17 +412,49 @@ 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) + { + 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) { - error = setup_signal_handlers (um); - if (error) + 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 +463,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,10 +506,17 @@ 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. + * * @cfgcmd{cli-listen, <address:port>} * Bind the CLI to listen at the address and port given. @clocalhost * on TCP port @c 5002, given as cli-listen localhost:5002, @@ -482,7 +543,7 @@ unix_config (vlib_main_t * vm, unformat_input_t * input) * Limit pager buffer to @c nn lines of output. * A value of @c 0 disables the pager. Default value: @c 100000 ?*/ -VLIB_CONFIG_FUNCTION (unix_config, "unix"); +VLIB_EARLY_CONFIG_FUNCTION (unix_config, "unix"); static clib_error_t * unix_exit (vlib_main_t * vm) @@ -510,13 +571,28 @@ thread0 (uword arg) return i; } +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); + + /* + * 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]; +} + int vlib_unix_main (int argc, char *argv[]) { vlib_main_t *vm = &vlib_global_main; /* one and only time for this! */ - vlib_thread_main_t *tm = &vlib_thread_main; unformat_input_t input; - u8 *thread_stacks; clib_error_t *e; int i; @@ -548,29 +624,10 @@ vlib_unix_main (int argc, char *argv[]) } unformat_free (&input); - /* - * allocate n x VLIB_THREAD_STACK_SIZE stacks, aligned to a - * VLIB_THREAD_STACK_SIZE boundary - * See also: os_get_cpu_number() in vlib/vlib/threads.c - */ - thread_stacks = clib_mem_alloc_aligned - ((uword) tm->n_thread_stacks * VLIB_THREAD_STACK_SIZE, - VLIB_THREAD_STACK_SIZE); + vlib_thread_stack_init (0); - vec_validate (vlib_thread_stacks, tm->n_thread_stacks - 1); - for (i = 0; i < vec_len (vlib_thread_stacks); i++) - { - vlib_thread_stacks[i] = thread_stacks; - - /* - * Disallow writes to the bottom page of the stack, to - * catch stack overflows. - */ - if (mprotect (thread_stacks, clib_mem_get_page_size (), PROT_READ) < 0) - clib_unix_warning ("thread stack"); - - thread_stacks += VLIB_THREAD_STACK_SIZE; - } + __os_thread_index = 0; + vm->thread_index = 0; i = clib_calljmp (thread0, (uword) vm, (void *) (vlib_thread_stacks[0] +