vlib: leave SIGPROF signal with its default handler
[vpp.git] / src / vat / main.c
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 #include "vat.h"
16 #include "plugin.h"
17 #include <signal.h>
18 #include <limits.h>
19
20 vat_main_t vat_main;
21
22 #include <vlibapi/api_helper_macros.h>
23
24 void
25 vat_suspend (vlib_main_t * vm, f64 interval)
26 {
27   /* do nothing in the standalone version, just return */
28 }
29
30 int
31 connect_to_vpe (char *name)
32 {
33   vat_main_t *vam = &vat_main;
34   api_main_t *am = vlibapi_get_main ();
35
36   if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
37     return -1;
38
39   vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
40   vam->my_client_index = am->my_client_index;
41
42   return 0;
43 }
44
45 vlib_main_t vlib_global_main;
46 vlib_main_t **vlib_mains;
47 void
48 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
49 {
50   clib_warning ("BUG");
51 }
52
53
54 static u8 *
55 format_api_error (u8 * s, va_list * args)
56 {
57   vat_main_t *vam = va_arg (*args, vat_main_t *);
58   i32 error = va_arg (*args, u32);
59   uword *p;
60
61   p = hash_get (vam->error_string_by_error_number, -error);
62
63   if (p)
64     s = format (s, "%s", p[0]);
65   else
66     s = format (s, "%d", error);
67   return s;
68 }
69
70 void
71 do_one_file (vat_main_t * vam)
72 {
73   int rv;
74   int (*fp) (vat_main_t * vam);
75   int arg_len;
76   unformat_input_t _input;
77   u8 *cmdp, *argsp;
78   uword *p;
79   u8 *this_cmd = 0;
80
81   vam->input = &_input;
82
83   /* Used by the "quit" command handler */
84   if (setjmp (vam->jump_buf) != 0)
85     return;
86
87   vam->jump_buf_set = 1;
88
89   while (1)
90     {
91       if (vam->ifp == stdin)
92         {
93           if (vam->exec_mode == 0)
94             rv = write (1, "vat# ", 5);
95           else
96             rv = write (1, "exec# ", 6);
97         }
98
99       _vec_len (vam->inbuf) = 4096;
100
101       if (vam->do_exit ||
102           fgets ((char *) vam->inbuf, vec_len (vam->inbuf), vam->ifp) == 0)
103         break;
104
105       vam->input_line_number++;
106
107       vec_free (this_cmd);
108
109       this_cmd =
110         (u8 *) clib_macro_eval (&vam->macro_main, (i8 *) vam->inbuf,
111                                 1 /* complain */ );
112
113       if (vam->exec_mode == 0)
114         {
115           /* Split input into cmd + args */
116           cmdp = this_cmd;
117
118           while (cmdp < (this_cmd + vec_len (this_cmd)))
119             {
120               if (*cmdp == ' ' || *cmdp == '\t' || *cmdp == '\n')
121                 {
122                   cmdp++;
123                 }
124               else
125                 break;
126             }
127           argsp = cmdp;
128           while (argsp < (this_cmd + vec_len (this_cmd)))
129             {
130               if (*argsp != ' ' && *argsp != '\t' && *argsp != '\n')
131                 {
132                   argsp++;
133                 }
134               else
135                 break;
136             }
137           *argsp++ = 0;
138           while (argsp < (this_cmd + vec_len (this_cmd)))
139             {
140               if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n')
141                 {
142                   argsp++;
143                 }
144               else
145                 break;
146             }
147
148
149           /* Blank input line? */
150           if (*cmdp == 0)
151             continue;
152
153           p = hash_get_mem (vam->function_by_name, cmdp);
154           if (p == 0)
155             {
156               errmsg ("'%s': function not found\n", cmdp);
157               continue;
158             }
159
160           arg_len = strlen ((char *) argsp);
161
162           unformat_init_string (vam->input, (char *) argsp, arg_len);
163           fp = (void *) p[0];
164         }
165       else
166         {
167           unformat_init_string (vam->input, (char *) this_cmd,
168                                 strlen ((char *) this_cmd));
169           cmdp = this_cmd;
170           fp = exec;
171         }
172
173       rv = (*fp) (vam);
174       if (rv < 0)
175         errmsg ("%s error: %U\n", cmdp, format_api_error, vam, rv);
176       unformat_free (vam->input);
177
178       if (vam->regenerate_interface_table)
179         {
180           vam->regenerate_interface_table = 0;
181           api_sw_interface_dump (vam);
182         }
183
184       /* Hack to pick up new client index after memfd_segment_create pivot */
185       if (vam->client_index_invalid)
186         {
187           vat_main_t *vam = &vat_main;
188           api_main_t *am = vlibapi_get_main ();
189
190           vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
191           vam->my_client_index = am->my_client_index;
192           vam->client_index_invalid = 0;
193         }
194     }
195 }
196
197 static void
198 init_error_string_table (vat_main_t * vam)
199 {
200
201   vam->error_string_by_error_number = hash_create (0, sizeof (uword));
202
203 #define _(n,v,s) hash_set (vam->error_string_by_error_number, -v, s);
204   foreach_vnet_api_error;
205 #undef _
206
207   hash_set (vam->error_string_by_error_number, 99, "Misc");
208 }
209
210 static i8 *
211 eval_current_file (macro_main_t * mm, i32 complain)
212 {
213   vat_main_t *vam = &vat_main;
214   return ((i8 *) format (0, "%s%c", vam->current_file, 0));
215 }
216
217 static i8 *
218 eval_current_line (macro_main_t * mm, i32 complain)
219 {
220   vat_main_t *vam = &vat_main;
221   return ((i8 *) format (0, "%d%c", vam->input_line_number, 0));
222 }
223
224 static void
225 signal_handler (int signum, siginfo_t * si, ucontext_t * uc)
226 {
227   vat_main_t *vam = &vat_main;
228
229   switch (signum)
230     {
231       /* these (caught) signals cause the application to exit */
232     case SIGINT:
233     case SIGTERM:
234       if (vam->jump_buf_set)
235         {
236           vam->do_exit = 1;
237           return;
238         }
239
240       /* FALLTHROUGH on purpose */
241
242     default:
243       break;
244     }
245
246   _exit (1);
247 }
248
249 static void
250 setup_signal_handlers (void)
251 {
252   uword i;
253   struct sigaction sa;
254
255   for (i = 1; i < 32; i++)
256     {
257       clib_memset (&sa, 0, sizeof (sa));
258       sa.sa_sigaction = (void *) signal_handler;
259       sa.sa_flags = SA_SIGINFO;
260
261       switch (i)
262         {
263           /* these signals take the default action */
264         case SIGABRT:
265         case SIGKILL:
266         case SIGSTOP:
267         case SIGUSR1:
268         case SIGUSR2:
269         case SIGPROF:
270           continue;
271
272           /* ignore SIGPIPE, SIGCHLD */
273         case SIGPIPE:
274         case SIGCHLD:
275         case SIGWINCH:
276           sa.sa_sigaction = (void *) SIG_IGN;
277           break;
278
279           /* catch and handle all other signals */
280         default:
281           break;
282         }
283
284       if (sigaction (i, &sa, 0) < 0)
285         clib_unix_warning ("sigaction %U", format_signal, i);
286     }
287 }
288
289 static void
290 vat_find_plugin_path ()
291 {
292   char *p, path[PATH_MAX];
293   int rv;
294   u8 *s;
295
296   /* find executable path */
297   if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
298     return;
299
300   /* readlink doesn't provide null termination */
301   path[rv] = 0;
302
303   /* strip filename */
304   if ((p = strrchr (path, '/')) == 0)
305     return;
306   *p = 0;
307
308   /* strip bin/ */
309   if ((p = strrchr (path, '/')) == 0)
310     return;
311   *p = 0;
312
313   s = format (0, "%s/lib/" CLIB_TARGET_TRIPLET "/vpp_api_test_plugins:"
314               "%s/lib/vpp_api_test_plugins", path, path);
315   vec_add1 (s, 0);
316   vat_plugin_path = (char *) s;
317 }
318
319 int
320 main (int argc, char **argv)
321 {
322   vat_main_t *vam = &vat_main;
323   unformat_input_t _argv, *a = &_argv;
324   u8 **input_files = 0;
325   u8 *output_file = 0;
326   u8 *chroot_prefix;
327   u8 *this_input_file;
328   u8 interactive = 1;
329   u8 json_output = 0;
330   int i;
331   f64 timeout;
332
333   clib_mem_init_thread_safe (0, 128 << 20);
334
335   clib_macro_init (&vam->macro_main);
336   clib_macro_add_builtin (&vam->macro_main, "current_file",
337                           eval_current_file);
338   clib_macro_add_builtin (&vam->macro_main, "current_line",
339                           eval_current_line);
340
341   init_error_string_table (vam);
342   vec_validate (vam->cmd_reply, 0);
343   vec_reset_length (vam->cmd_reply);
344
345   vat_find_plugin_path ();
346
347   unformat_init_command_line (a, argv);
348
349   while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
350     {
351       if (unformat (a, "in %s", &this_input_file))
352         vec_add1 (input_files, this_input_file);
353       else if (unformat (a, "out %s", &output_file))
354         ;
355       else if (unformat (a, "script"))
356         interactive = 0;
357       else if (unformat (a, "json"))
358         json_output = 1;
359       else if (unformat (a, "socket-name %s", &vam->socket_name))
360         ;
361       else if (unformat (a, "default-socket"))
362         {
363           vam->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
364         }
365       else if (unformat (a, "plugin_path %s", (u8 *) & vat_plugin_path))
366         vec_add1 (vat_plugin_path, 0);
367       else if (unformat (a, "plugin_name_filter %s",
368                          (u8 *) & vat_plugin_name_filter))
369         vec_add1 (vat_plugin_name_filter, 0);
370       else if (unformat (a, "chroot prefix %s", &chroot_prefix))
371         {
372           vl_set_memory_root_path ((char *) chroot_prefix);
373         }
374       else
375         {
376           fformat
377             (stderr,
378              "%s: usage [in <f1> ... in <fn>] [out <fn>] [script] [json]\n"
379              "[plugin_path <path>][default-socket][socket-name <name>]\n"
380              "[plugin_name_filter <filter>][chroot prefix <path>]\n",
381              argv[0]);
382           exit (1);
383         }
384     }
385
386   if (output_file)
387     vam->ofp = fopen ((char *) output_file, "w");
388   else
389     vam->ofp = stdout;
390
391   if (vam->ofp == NULL)
392     {
393       fformat (stderr, "Couldn't open output file %s\n",
394                output_file ? (char *) output_file : "stdout");
395       exit (1);
396     }
397
398   clib_time_init (&vam->clib_time);
399
400   vat_api_hookup (vam);
401   vat_plugin_api_reference ();
402
403   setup_signal_handlers ();
404
405   if (vam->socket_name && vat_socket_connect (vam))
406     fformat (stderr, "WARNING: socket connection failed");
407
408   if ((!vam->socket_client_main || vam->socket_client_main->socket_fd == 0)
409       && connect_to_vpe ("vpp_api_test") < 0)
410     {
411       svm_region_exit ();
412       fformat (stderr, "Couldn't connect to vpe, exiting...\n");
413       exit (1);
414     }
415
416   vam->json_output = json_output;
417
418   if (!json_output)
419     api_sw_interface_dump (vam);
420
421   vec_validate (vam->inbuf, 4096);
422
423   vam->current_file = (u8 *) "plugin-init";
424   vat_plugin_init (vam);
425
426   for (i = 0; i < vec_len (input_files); i++)
427     {
428       vam->ifp = fopen ((char *) input_files[i], "r");
429       if (vam->ifp == NULL)
430         {
431           fformat (stderr, "Couldn't open input file %s\n", input_files[i]);
432           continue;
433         }
434       vam->current_file = input_files[i];
435       vam->input_line_number = 0;
436       do_one_file (vam);
437       fclose (vam->ifp);
438     }
439
440   if (output_file)
441     fclose (vam->ofp);
442
443   if (interactive)
444     {
445       vam->ifp = stdin;
446       vam->ofp = stdout;
447       vam->current_file = (u8 *) "interactive";
448       do_one_file (vam);
449       fclose (vam->ifp);
450     }
451
452   /*
453    * Particularly when running a script, don't be in a hurry to leave.
454    * A reply message queued to this process will end up constipating
455    * the allocation rings.
456    */
457   timeout = vat_time_now (vam) + 2.0;
458   while (vam->result_ready == 0 && vat_time_now (vam) < timeout)
459     ;
460
461   if (vat_time_now (vam) > timeout)
462     clib_warning ("BUG: message reply spin-wait timeout");
463
464   vl_client_disconnect_from_vlib ();
465   exit (0);
466 }
467
468 /*
469  * fd.io coding-style-patch-verification: ON
470  *
471  * Local Variables:
472  * eval: (c-set-style "gnu")
473  * End:
474  */