Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | /* |
| 16 | * unix.h: Unix specific main state |
| 17 | * |
| 18 | * Copyright (c) 2008 Eliot Dresselhaus |
| 19 | * |
| 20 | * Permission is hereby granted, free of charge, to any person obtaining |
| 21 | * a copy of this software and associated documentation files (the |
| 22 | * "Software"), to deal in the Software without restriction, including |
| 23 | * without limitation the rights to use, copy, modify, merge, publish, |
| 24 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 25 | * permit persons to whom the Software is furnished to do so, subject to |
| 26 | * the following conditions: |
| 27 | * |
| 28 | * The above copyright notice and this permission notice shall be |
| 29 | * included in all copies or substantial portions of the Software. |
| 30 | * |
| 31 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 32 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 33 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 34 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 35 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 36 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 37 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 38 | */ |
| 39 | |
| 40 | #ifndef included_unix_unix_h |
| 41 | #define included_unix_unix_h |
| 42 | |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 43 | #include <vppinfra/file.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 44 | #include <vppinfra/socket.h> |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 45 | #include <termios.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 46 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 47 | typedef struct |
| 48 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 49 | f64 time; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 50 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | } unix_error_history_t; |
| 52 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 53 | typedef struct |
| 54 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | /* Back pointer to main structure. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 56 | vlib_main_t *vlib_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | |
| 58 | u32 flags; |
| 59 | /* Run interactively or as daemon (background process). */ |
| 60 | #define UNIX_FLAG_INTERACTIVE (1 << 0) |
| 61 | #define UNIX_FLAG_NODAEMON (1 << 1) |
Ruslan Babayev | e31820a | 2020-02-14 17:45:02 -0800 | [diff] [blame^] | 62 | #define UNIX_FLAG_NOSYSLOG (1 << 2) |
| 63 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 64 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | /* CLI listen socket. */ |
| 66 | clib_socket_t cli_listen_socket; |
| 67 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 68 | /* Circular buffer of last unix errors. */ |
| 69 | unix_error_history_t error_history[128]; |
| 70 | u32 error_history_index; |
| 71 | u64 n_total_errors; |
| 72 | |
| 73 | /* startup-config filename */ |
| 74 | u8 *startup_config_filename; |
| 75 | |
Damjan Marion | 57d963f | 2017-07-20 19:17:06 +0200 | [diff] [blame] | 76 | /* runtime directory path */ |
| 77 | u8 *runtime_dir; |
| 78 | |
Pierre Pfister | 9a244bb | 2017-07-27 22:57:34 -0400 | [diff] [blame] | 79 | /* pidfile filename */ |
| 80 | u8 *pidfile; |
| 81 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 82 | /* unix config complete */ |
| 83 | volatile int unix_config_complete; |
| 84 | |
| 85 | /* CLI log file. GIGO. */ |
| 86 | u8 *log_filename; |
| 87 | int log_fd; |
Chris Luke | 572d812 | 2016-04-25 13:49:07 -0400 | [diff] [blame] | 88 | |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 89 | /* Don't put CLI connections into character mode */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 90 | int cli_line_mode; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 91 | |
| 92 | /* Maximum amount of command line history to keep per session */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 93 | u32 cli_history_limit; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 94 | |
Chris Luke | 572d812 | 2016-04-25 13:49:07 -0400 | [diff] [blame] | 95 | /* Suppress the welcome banner at CLI session start */ |
| 96 | int cli_no_banner; |
| 97 | |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 98 | /* Maximum pager buffer size */ |
| 99 | u32 cli_pager_buffer_limit; |
| 100 | |
| 101 | /* Suppress the pager */ |
| 102 | int cli_no_pager; |
| 103 | |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 104 | /* Store the original state of stdin when it's a tty */ |
| 105 | struct termios tio_stdin; |
| 106 | int tio_isset; |
Dave Barach | 85aa490 | 2018-06-14 18:52:46 -0400 | [diff] [blame] | 107 | |
| 108 | u32 poll_sleep_usec; |
| 109 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | } unix_main_t; |
| 111 | |
| 112 | /* Global main structure. */ |
| 113 | extern unix_main_t unix_main; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 114 | extern clib_file_main_t file_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 115 | |
| 116 | always_inline void |
| 117 | unix_save_error (unix_main_t * um, clib_error_t * error) |
| 118 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 119 | unix_error_history_t *eh = um->error_history + um->error_history_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | clib_error_free_vector (eh->error); |
| 121 | eh->error = error; |
| 122 | eh->time = vlib_time_now (um->vlib_main); |
| 123 | um->n_total_errors += 1; |
| 124 | if (++um->error_history_index >= ARRAY_LEN (um->error_history)) |
| 125 | um->error_history_index = 0; |
| 126 | } |
| 127 | |
| 128 | /* Main function for Unix VLIB. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 129 | int vlib_unix_main (int argc, char *argv[]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 131 | /* Set prompt for CLI. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 132 | void vlib_unix_cli_set_prompt (char *prompt); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 133 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 134 | static inline unix_main_t * |
| 135 | vlib_unix_get_main (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 136 | { |
| 137 | return &unix_main; |
| 138 | } |
| 139 | |
Damjan Marion | 57d963f | 2017-07-20 19:17:06 +0200 | [diff] [blame] | 140 | static inline char * |
| 141 | vlib_unix_get_runtime_dir (void) |
| 142 | { |
| 143 | return (char *) unix_main.runtime_dir; |
| 144 | } |
| 145 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 146 | /* thread stack array; vec_len = max number of threads */ |
Damjan Marion | 6a7acc2 | 2016-12-19 16:28:36 +0100 | [diff] [blame] | 147 | extern u8 **vlib_thread_stacks; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 148 | |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 149 | /* utils */ |
| 150 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 151 | clib_error_t *foreach_directory_file (char *dir_name, |
| 152 | clib_error_t * (*f) (void *arg, |
| 153 | u8 * path_name, |
| 154 | u8 * file_name), |
| 155 | void *arg, int scan_dirs); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 156 | |
Damjan Marion | 57d963f | 2017-07-20 19:17:06 +0200 | [diff] [blame] | 157 | clib_error_t *vlib_unix_recursive_mkdir (char *path); |
Chris Luke | 475674e | 2017-07-05 18:02:53 -0400 | [diff] [blame] | 158 | |
Pierre Pfister | 9a244bb | 2017-07-27 22:57:34 -0400 | [diff] [blame] | 159 | clib_error_t *vlib_unix_validate_runtime_file (unix_main_t * um, |
| 160 | const char *path, |
| 161 | u8 ** full_path); |
| 162 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 163 | #endif /* included_unix_unix_h */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 164 | |
| 165 | /* |
| 166 | * fd.io coding-style-patch-verification: ON |
| 167 | * |
| 168 | * Local Variables: |
| 169 | * eval: (c-set-style "gnu") |
| 170 | * End: |
| 171 | */ |