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/heap.h>
37 #include <vppinfra/pool.h>
38 #include <vppinfra/format.h>
39 #include <vppinfra/serialize.h>
44 svmdb_map_args_t map_args;
49 svmdbtool_main_t svmdbtool_main;
51 static inline svmdb_map_args_t *
52 map_arg_setup (char *chroot_path)
54 svmdbtool_main_t *sm = &svmdbtool_main;
55 svmdb_map_args_t *ma = &sm->map_args;
57 clib_memset (ma, 0, sizeof (*ma));
58 ma->root_path = chroot_path;
66 get_string (char *chroot_path, u8 * vbl)
72 ma = map_arg_setup (chroot_path);
76 rv = svmdb_local_get_string_variable (c, (char *) vbl);
78 fformat (stdout, "%s\n", rv ? rv : "UNSET");
84 set_string (char *chroot_path, u8 * vbl, u8 * value)
89 ma = map_arg_setup (chroot_path);
92 svmdb_local_set_string_variable (c, (char *) vbl, (char *) value);
97 unset_string (char *chroot_path, u8 * vbl)
100 svmdb_map_args_t *ma;
102 ma = map_arg_setup (chroot_path);
105 svmdb_local_unset_string_variable (c, (char *) vbl);
110 dump_strings (char *chroot_path)
113 svmdb_map_args_t *ma;
115 ma = map_arg_setup (chroot_path);
118 svmdb_local_dump_strings (c);
123 serialize_strings (char *chroot_path, char *filename)
126 svmdb_map_args_t *ma;
128 ma = map_arg_setup (chroot_path);
131 (void) svmdb_local_serialize_strings (c, filename);
136 unserialize_strings (char *chroot_path, char *filename)
139 svmdb_map_args_t *ma;
141 ma = map_arg_setup (chroot_path);
144 (void) svmdb_local_unserialize_strings (c, filename);
149 test_vlib_vec_rate (char *chroot_path, f64 vr)
153 svmdb_map_args_t *ma;
155 ma = map_arg_setup (chroot_path);
161 svmdb_local_set_vec_variable (c, "vlib_vector_rate", (char *) tv,
171 test_vec (char *chroot_path, u8 * vbl)
176 svmdb_map_args_t *ma;
178 ma = map_arg_setup (chroot_path);
182 /* my amp goes to 11 */
183 for (i = 0; i < 11; i++)
188 svmdb_local_set_vec_variable (c, (char *) vbl, (char *) tv, sizeof (tv[0]));
195 fake_install (char *chroot_path, u8 * add_value)
204 svmdb_map_args_t *ma;
206 ma = map_arg_setup (chroot_path);
210 oldvalue = svmdb_local_get_vec_variable (c, "installed_sw", 1);
213 unserialize_open_data (&m, oldvalue, vec_len (oldvalue));
214 nitems = unserialize_likely_small_unsigned_integer (&m);
215 for (i = 0; i < nitems; i++)
217 unserialize_cstring (&m, (char **) &value);
218 vec_add1 (values, value);
223 value = format (0, "%s%c", add_value, 0);
225 vec_add1 (values, value);
227 fformat (stdout, "Resulting installed_sw vector:\n");
229 serialize_open_vector (&m, v);
230 serialize_likely_small_unsigned_integer (&m, vec_len (values));
231 for (i = 0; i < vec_len (values); i++)
233 fformat (stdout, "%s\n", values[i]);
234 serialize_cstring (&m, (char *) values[i]);
237 v = serialize_close_vector (&m);
239 svmdb_local_set_vec_variable (c, "installed_sw", v, sizeof (v[0]));
242 for (i = 0; i < vec_len (values); i++)
243 vec_free (values[i]);
248 sigaction_handler (int signum, siginfo_t * i, void *notused)
253 action = (u32) (uword) i->si_ptr;
255 opaque = (u32) (uword) i->si_ptr;
256 opaque &= ~(0xF0000000);
260 #endif /* __linux__ */
262 clib_warning ("signal %d, action %d, opaque %x", signum, action, opaque);
266 test_reg (char *chroot_path, u8 * vbl)
269 svmdb_notification_args_t args;
270 svmdb_notification_args_t *a = &args;
272 svmdb_map_args_t *ma;
274 ma = map_arg_setup (chroot_path);
276 clib_memset (&sa, 0, sizeof (sa));
277 sa.sa_sigaction = sigaction_handler;
278 sa.sa_flags = SA_SIGINFO;
279 if (sigaction (SIGUSR2, &sa, 0) < 0)
281 clib_unix_warning ("sigaction");
285 clib_memset (a, 0, sizeof (*a));
289 a->add_del = 1 /* add */ ;
290 a->nspace = SVMDB_NAMESPACE_STRING;
291 a->var = (char *) vbl;
294 a->action = SVMDB_ACTION_GET;
295 a->opaque = 0x0eadbeef;
297 svmdb_local_add_del_notification (c, a);
299 (void) svmdb_local_get_string_variable (c, (char *) vbl);
301 a->add_del = 0; /* del */
302 svmdb_local_add_del_notification (c, a);
310 unset_vec (char *chroot_path, u8 * vbl)
313 svmdb_map_args_t *ma;
315 ma = map_arg_setup (chroot_path);
319 svmdb_local_unset_vec_variable (c, (char *) vbl);
324 dump_vecs (char *chroot_path)
327 svmdb_map_args_t *ma;
329 ma = map_arg_setup (chroot_path);
333 svmdb_local_dump_vecs (c);
338 crash_test (char *chroot_path)
341 svmdb_map_args_t *ma;
343 ma = map_arg_setup (chroot_path);
347 clib_warning ("Grab region mutex and crash deliberately!");
348 c->db_rp->mutex_owner_pid = getpid ();
349 c->db_rp->mutex_owner_tag = -13;
350 pthread_mutex_lock (&c->db_rp->mutex);
356 map_with_size (char *chroot_path, uword size)
359 svmdb_map_args_t *ma;
361 svmdbtool_main.size = size;
362 ma = map_arg_setup (chroot_path);
370 main (int argc, char **argv)
372 unformat_input_t input;
374 u8 *vbl = 0, *value = 0;
375 char *chroot_path = 0;
381 struct passwd _pw, *pw;
382 struct group _grp, *grp;
385 clib_mem_init_thread_safe (0, 128 << 20);
387 svmdbtool_main.uid = geteuid ();
388 svmdbtool_main.gid = getegid ();
390 unformat_init_command_line (&input, argv);
392 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
394 if (unformat (&input, "get-string %s", &vbl))
396 get_string (chroot_path, vbl);
400 else if (unformat (&input, "set-string %s %s", &vbl, &value))
402 set_string (chroot_path, vbl, value);
407 else if (unformat (&input, "unset-string %s", &vbl))
409 unset_string (chroot_path, vbl);
413 else if (unformat (&input, "dump-strings"))
415 dump_strings (chroot_path);
418 else if (unformat (&input, "unset-vec %s", &vbl))
420 unset_vec (chroot_path, vbl);
424 else if (unformat (&input, "dump-vecs"))
426 dump_vecs (chroot_path);
429 else if (unformat (&input, "test-vec %s", &vbl))
431 test_vec (chroot_path, vbl);
435 else if (unformat (&input, "vlib-vec-rate %f", &vr))
437 test_vlib_vec_rate (chroot_path, vr);
440 else if (unformat (&input, "test-reg %s", &vbl))
442 test_reg (chroot_path, vbl);
445 else if (unformat (&input, "crash-test"))
447 crash_test (chroot_path);
449 else if (unformat (&input, "chroot %s", &chroot_path_u8))
451 chroot_path = (char *) chroot_path_u8;
453 else if (unformat (&input, "fake-install %s", &value))
455 fake_install (chroot_path, value);
458 else if (unformat (&input, "size %d", &size))
460 map_with_size (chroot_path, size);
463 else if (unformat (&input, "uid %d", &uid))
464 svmdbtool_main.uid = uid;
465 else if (unformat (&input, "gid %d", &gid))
466 svmdbtool_main.gid = gid;
467 else if (unformat (&input, "uid %s", &s))
469 /* lookup the username */
471 rv = getpwnam_r (s, &_pw, buf, sizeof (buf), &pw);
474 fformat (stderr, "cannot fetch username %s", s);
479 fformat (stderr, "username %s does not exist", s);
483 svmdbtool_main.uid = pw->pw_uid;
485 else if (unformat (&input, "gid %s", &s))
487 /* lookup the group name */
489 rv = getgrnam_r (s, &_grp, buf, sizeof (buf), &grp);
492 fformat (stderr, "cannot fetch group %s", s);
497 fformat (stderr, "group %s does not exist", s);
501 svmdbtool_main.gid = grp->gr_gid;
503 else if (unformat (&input, "serialize-strings %s", &filename))
505 vec_add1 (filename, 0);
506 serialize_strings (chroot_path, (char *) filename);
509 else if (unformat (&input, "unserialize-strings %s", &filename))
511 vec_add1 (filename, 0);
512 unserialize_strings (chroot_path, (char *) filename);
521 unformat_free (&input);
525 fformat (stdout, "%s: get-string <name> | set-string <name> <value>\n",
527 fformat (stdout, " unset-string <name> | dump-strings\n");
528 fformat (stdout, " test-vec <name> |\n");
529 fformat (stdout, " unset-vec <name> | dump-vecs\n");
530 fformat (stdout, " chroot <prefix> [uid <nnn-or-userid>]\n");
531 fformat (stdout, " [gid <nnn-or-group-name>]\n");
538 * fd.io coding-style-patch-verification: ON
541 * eval: (c-set-style "gnu")