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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <sys/types.h>
23 #include <netinet/in.h>
30 #include <vppinfra/clib.h>
31 #include <vppinfra/vec.h>
32 #include <vppinfra/hash.h>
33 #include <vppinfra/bitmap.h>
34 #include <vppinfra/fifo.h>
35 #include <vppinfra/time.h>
36 #include <vppinfra/mheap.h>
37 #include <vppinfra/heap.h>
38 #include <vppinfra/pool.h>
39 #include <vppinfra/format.h>
40 #include <vppinfra/serialize.h>
45 svmdb_map_args_t map_args;
50 svmdbtool_main_t svmdbtool_main;
52 static inline svmdb_map_args_t *
53 map_arg_setup (char *chroot_path)
55 svmdbtool_main_t *sm = &svmdbtool_main;
56 svmdb_map_args_t *ma = &sm->map_args;
58 memset (ma, 0, sizeof (*ma));
59 ma->root_path = chroot_path;
67 get_string (char *chroot_path, u8 * vbl)
73 ma = map_arg_setup (chroot_path);
77 rv = svmdb_local_get_string_variable (c, (char *) vbl);
79 fformat (stdout, "%s\n", rv ? rv : "UNSET");
85 set_string (char *chroot_path, u8 * vbl, u8 * value)
90 ma = map_arg_setup (chroot_path);
93 svmdb_local_set_string_variable (c, (char *) vbl, (char *) value);
98 unset_string (char *chroot_path, u8 * vbl)
101 svmdb_map_args_t *ma;
103 ma = map_arg_setup (chroot_path);
106 svmdb_local_unset_string_variable (c, (char *) vbl);
111 dump_strings (char *chroot_path)
114 svmdb_map_args_t *ma;
116 ma = map_arg_setup (chroot_path);
119 svmdb_local_dump_strings (c);
124 test_vlib_vec_rate (char *chroot_path, f64 vr)
128 svmdb_map_args_t *ma;
130 ma = map_arg_setup (chroot_path);
136 svmdb_local_set_vec_variable (c, "vlib_vector_rate", (char *) tv,
146 test_vec (char *chroot_path, u8 * vbl)
151 svmdb_map_args_t *ma;
153 ma = map_arg_setup (chroot_path);
157 /* my amp goes to 11 */
158 for (i = 0; i < 11; i++)
163 svmdb_local_set_vec_variable (c, (char *) vbl, (char *) tv, sizeof (tv[0]));
170 fake_install (char *chroot_path, u8 * add_value)
179 svmdb_map_args_t *ma;
181 ma = map_arg_setup (chroot_path);
185 oldvalue = svmdb_local_get_vec_variable (c, "installed_sw", 1);
188 unserialize_open_data (&m, oldvalue, vec_len (oldvalue));
189 nitems = unserialize_likely_small_unsigned_integer (&m);
190 for (i = 0; i < nitems; i++)
192 unserialize_cstring (&m, (char **) &value);
193 vec_add1 (values, value);
198 value = format (0, "%s%c", add_value, 0);
200 vec_add1 (values, value);
202 fformat (stdout, "Resulting installed_sw vector:\n");
204 serialize_open_vector (&m, v);
205 serialize_likely_small_unsigned_integer (&m, vec_len (values));
206 for (i = 0; i < vec_len (values); i++)
208 fformat (stdout, "%s\n", values[i]);
209 serialize_cstring (&m, (char *) values[i]);
212 v = serialize_close_vector (&m);
214 svmdb_local_set_vec_variable (c, "installed_sw", v, sizeof (v[0]));
217 for (i = 0; i < vec_len (values); i++)
218 vec_free (values[i]);
223 sigaction_handler (int signum, siginfo_t * i, void *notused)
227 action = (u32) (uword) i->si_ptr;
229 opaque = (u32) (uword) i->si_ptr;
230 opaque &= ~(0xF0000000);
232 clib_warning ("signal %d, action %d, opaque %x", signum, action, opaque);
236 test_reg (char *chroot_path, u8 * vbl)
239 svmdb_notification_args_t args;
240 svmdb_notification_args_t *a = &args;
242 svmdb_map_args_t *ma;
244 ma = map_arg_setup (chroot_path);
246 memset (&sa, 0, sizeof (sa));
247 sa.sa_sigaction = sigaction_handler;
248 sa.sa_flags = SA_SIGINFO;
249 if (sigaction (SIGUSR2, &sa, 0) < 0)
251 clib_unix_warning ("sigaction");
255 memset (a, 0, sizeof (*a));
259 a->add_del = 1 /* add */ ;
260 a->nspace = SVMDB_NAMESPACE_STRING;
261 a->var = (char *) vbl;
264 a->action = SVMDB_ACTION_GET;
265 a->opaque = 0x0eadbeef;
267 svmdb_local_add_del_notification (c, a);
269 (void) svmdb_local_get_string_variable (c, (char *) vbl);
271 a->add_del = 0; /* del */
272 svmdb_local_add_del_notification (c, a);
280 unset_vec (char *chroot_path, u8 * vbl)
283 svmdb_map_args_t *ma;
285 ma = map_arg_setup (chroot_path);
289 svmdb_local_unset_vec_variable (c, (char *) vbl);
294 dump_vecs (char *chroot_path)
297 svmdb_map_args_t *ma;
299 ma = map_arg_setup (chroot_path);
303 svmdb_local_dump_vecs (c);
308 crash_test (char *chroot_path)
311 svmdb_map_args_t *ma;
313 ma = map_arg_setup (chroot_path);
317 clib_warning ("Grab region mutex and crash deliberately!");
318 c->db_rp->mutex_owner_pid = getpid ();
319 c->db_rp->mutex_owner_tag = -13;
320 pthread_mutex_lock (&c->db_rp->mutex);
326 map_with_size (char *chroot_path, uword size)
329 svmdb_map_args_t *ma;
331 svmdbtool_main.size = size;
332 ma = map_arg_setup (chroot_path);
340 main (int argc, char **argv)
342 unformat_input_t input;
344 u8 *vbl = 0, *value = 0;
345 char *chroot_path = 0;
350 struct passwd _pw, *pw;
351 struct group _grp, *grp;
354 svmdbtool_main.uid = geteuid ();
355 svmdbtool_main.gid = getegid ();
357 unformat_init_command_line (&input, argv);
359 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
361 if (unformat (&input, "get-string %s", &vbl))
363 get_string (chroot_path, vbl);
367 else if (unformat (&input, "set-string %s %s", &vbl, &value))
369 set_string (chroot_path, vbl, value);
374 else if (unformat (&input, "unset-string %s", &vbl))
376 unset_string (chroot_path, vbl);
380 else if (unformat (&input, "dump-strings"))
382 dump_strings (chroot_path);
385 else if (unformat (&input, "unset-vec %s", &vbl))
387 unset_vec (chroot_path, vbl);
391 else if (unformat (&input, "dump-vecs"))
393 dump_vecs (chroot_path);
396 else if (unformat (&input, "test-vec %s", &vbl))
398 test_vec (chroot_path, vbl);
402 else if (unformat (&input, "vlib-vec-rate %f", &vr))
404 test_vlib_vec_rate (chroot_path, vr);
407 else if (unformat (&input, "test-reg %s", &vbl))
409 test_reg (chroot_path, vbl);
412 else if (unformat (&input, "crash-test"))
414 crash_test (chroot_path);
416 else if (unformat (&input, "chroot %s", &chroot_path_u8))
418 chroot_path = (char *) chroot_path_u8;
420 else if (unformat (&input, "fake-install %s", &value))
422 fake_install (chroot_path, value);
425 else if (unformat (&input, "size %d", &size))
427 map_with_size (chroot_path, size);
430 else if (unformat (&input, "uid %d", &uid))
431 svmdbtool_main.uid = uid;
432 else if (unformat (&input, "gid %d", &gid))
433 svmdbtool_main.gid = gid;
434 else if (unformat (&input, "uid %s", &s))
436 /* lookup the username */
438 rv = getpwnam_r (s, &_pw, buf, sizeof (buf), &pw);
441 fformat (stderr, "cannot fetch username %s", s);
446 fformat (stderr, "username %s does not exist", s);
450 svmdbtool_main.uid = pw->pw_uid;
452 else if (unformat (&input, "gid %s", &s))
454 /* lookup the group name */
456 rv = getgrnam_r (s, &_grp, buf, sizeof (buf), &grp);
459 fformat (stderr, "cannot fetch group %s", s);
464 fformat (stderr, "group %s does not exist", s);
468 svmdbtool_main.gid = grp->gr_gid;
476 unformat_free (&input);
480 fformat (stdout, "%s: get-string <name> | set-string <name> <value>\n",
482 fformat (stdout, " unset-string <name> | dump-strings\n");
483 fformat (stdout, " test-vec <name> |\n");
484 fformat (stdout, " unset-vec <name> | dump-vecs\n");
485 fformat (stdout, " chroot <prefix> [uid <nnn-or-userid>]\n");
486 fformat (stdout, " [gid <nnn-or-group-name>]\n");
493 * fd.io coding-style-patch-verification: ON
496 * eval: (c-set-style "gnu")