4c8566b7ee84a0cb8a9b93d6dea2eab88d5b4cd4
[vpp.git] / src / vlib / unix / unix.h
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
43 #include <vppinfra/file.h>
44 #include <vppinfra/socket.h>
45 #include <termios.h>
46
47 typedef struct
48 {
49   f64 time;
50   clib_error_t *error;
51 } unix_error_history_t;
52
53 typedef struct
54 {
55   /* Back pointer to main structure. */
56   vlib_main_t *vlib_main;
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)
62
63   /* CLI listen socket. */
64   clib_socket_t cli_listen_socket;
65
66   /* Circular buffer of last unix errors. */
67   unix_error_history_t error_history[128];
68   u32 error_history_index;
69   u64 n_total_errors;
70
71   /* startup-config filename */
72   u8 *startup_config_filename;
73
74   /* runtime directory path */
75   u8 *runtime_dir;
76
77   /* pidfile filename */
78   u8 *pidfile;
79
80   /* unix config complete */
81   volatile int unix_config_complete;
82
83   /* CLI log file. GIGO. */
84   u8 *log_filename;
85   int log_fd;
86
87   /* Don't put CLI connections into character mode */
88   int cli_line_mode;
89
90   /* Maximum amount of command line history to keep per session */
91   u32 cli_history_limit;
92
93   /* Suppress the welcome banner at CLI session start */
94   int cli_no_banner;
95
96   /* Maximum pager buffer size */
97   u32 cli_pager_buffer_limit;
98
99   /* Suppress the pager */
100   int cli_no_pager;
101
102   /* Store the original state of stdin when it's a tty */
103   struct termios tio_stdin;
104   int tio_isset;
105 } unix_main_t;
106
107 /* Global main structure. */
108 extern unix_main_t unix_main;
109 extern clib_file_main_t file_main;
110
111 always_inline void
112 unix_save_error (unix_main_t * um, clib_error_t * error)
113 {
114   unix_error_history_t *eh = um->error_history + um->error_history_index;
115   clib_error_free_vector (eh->error);
116   eh->error = error;
117   eh->time = vlib_time_now (um->vlib_main);
118   um->n_total_errors += 1;
119   if (++um->error_history_index >= ARRAY_LEN (um->error_history))
120     um->error_history_index = 0;
121 }
122
123 /* Main function for Unix VLIB. */
124 int vlib_unix_main (int argc, char *argv[]);
125
126 clib_error_t *unix_physmem_init (vlib_main_t * vm);
127
128 /* Set prompt for CLI. */
129 void vlib_unix_cli_set_prompt (char *prompt);
130
131 static inline unix_main_t *
132 vlib_unix_get_main (void)
133 {
134   return &unix_main;
135 }
136
137 static inline char *
138 vlib_unix_get_runtime_dir (void)
139 {
140   return (char *) unix_main.runtime_dir;
141 }
142
143 /* thread stack array; vec_len = max number of threads */
144 extern u8 **vlib_thread_stacks;
145
146 /* utils */
147
148 clib_error_t *foreach_directory_file (char *dir_name,
149                                       clib_error_t * (*f) (void *arg,
150                                                            u8 * path_name,
151                                                            u8 * file_name),
152                                       void *arg, int scan_dirs);
153
154 clib_error_t *vlib_unix_recursive_mkdir (char *path);
155
156 clib_error_t *vlib_unix_validate_runtime_file (unix_main_t * um,
157                                                const char *path,
158                                                u8 ** full_path);
159
160 #endif /* included_unix_unix_h */
161
162 /*
163  * fd.io coding-style-patch-verification: ON
164  *
165  * Local Variables:
166  * eval: (c-set-style "gnu")
167  * End:
168  */