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 clib_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 serialize_strings (char *chroot_path, char *filename)
127 svmdb_map_args_t *ma;
129 ma = map_arg_setup (chroot_path);
132 (void) svmdb_local_serialize_strings (c, filename);
137 unserialize_strings (char *chroot_path, char *filename)
140 svmdb_map_args_t *ma;
142 ma = map_arg_setup (chroot_path);
145 (void) svmdb_local_unserialize_strings (c, filename);
150 test_vlib_vec_rate (char *chroot_path, f64 vr)
154 svmdb_map_args_t *ma;
156 ma = map_arg_setup (chroot_path);
162 svmdb_local_set_vec_variable (c, "vlib_vector_rate", (char *) tv,
172 test_vec (char *chroot_path, u8 * vbl)
177 svmdb_map_args_t *ma;
179 ma = map_arg_setup (chroot_path);
183 /* my amp goes to 11 */
184 for (i = 0; i < 11; i++)
189 svmdb_local_set_vec_variable (c, (char *) vbl, (char *) tv, sizeof (tv[0]));
196 fake_install (char *chroot_path, u8 * add_value)
205 svmdb_map_args_t *ma;
207 ma = map_arg_setup (chroot_path);
211 oldvalue = svmdb_local_get_vec_variable (c, "installed_sw", 1);
214 unserialize_open_data (&m, oldvalue, vec_len (oldvalue));
215 nitems = unserialize_likely_small_unsigned_integer (&m);
216 for (i = 0; i < nitems; i++)
218 unserialize_cstring (&m, (char **) &value);
219 vec_add1 (values, value);
224 value = format (0, "%s%c", add_value, 0);
226 vec_add1 (values, value);
228 fformat (stdout, "Resulting installed_sw vector:\n");
230 serialize_open_vector (&m, v);
231 serialize_likely_small_unsigned_integer (&m, vec_len (values));
232 for (i = 0; i < vec_len (values); i++)
234 fformat (stdout, "%s\n", values[i]);
235 serialize_cstring (&m, (char *) values[i]);
238 v = serialize_close_vector (&m);
240 svmdb_local_set_vec_variable (c, "installed_sw", v, sizeof (v[0]));
243 for (i = 0; i < vec_len (values); i++)
244 vec_free (values[i]);
249 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);
258 clib_warning ("signal %d, action %d, opaque %x", signum, action, opaque);
262 test_reg (char *chroot_path, u8 * vbl)
265 svmdb_notification_args_t args;
266 svmdb_notification_args_t *a = &args;
268 svmdb_map_args_t *ma;
270 ma = map_arg_setup (chroot_path);
272 clib_memset (&sa, 0, sizeof (sa));
273 sa.sa_sigaction = sigaction_handler;
274 sa.sa_flags = SA_SIGINFO;
275 if (sigaction (SIGUSR2, &sa, 0) < 0)
277 clib_unix_warning ("sigaction");
281 clib_memset (a, 0, sizeof (*a));
285 a->add_del = 1 /* add */ ;
286 a->nspace = SVMDB_NAMESPACE_STRING;
287 a->var = (char *) vbl;
290 a->action = SVMDB_ACTION_GET;
291 a->opaque = 0x0eadbeef;
293 svmdb_local_add_del_notification (c, a);
295 (void) svmdb_local_get_string_variable (c, (char *) vbl);
297 a->add_del = 0; /* del */
298 svmdb_local_add_del_notification (c, a);
306 unset_vec (char *chroot_path, u8 * vbl)
309 svmdb_map_args_t *ma;
311 ma = map_arg_setup (chroot_path);
315 svmdb_local_unset_vec_variable (c, (char *) vbl);
320 dump_vecs (char *chroot_path)
323 svmdb_map_args_t *ma;
325 ma = map_arg_setup (chroot_path);
329 svmdb_local_dump_vecs (c);
334 crash_test (char *chroot_path)
337 svmdb_map_args_t *ma;
339 ma = map_arg_setup (chroot_path);
343 clib_warning ("Grab region mutex and crash deliberately!");
344 c->db_rp->mutex_owner_pid = getpid ();
345 c->db_rp->mutex_owner_tag = -13;
346 pthread_mutex_lock (&c->db_rp->mutex);
352 map_with_size (char *chroot_path, uword size)
355 svmdb_map_args_t *ma;
357 svmdbtool_main.size = size;
358 ma = map_arg_setup (chroot_path);
366 main (int argc, char **argv)
368 unformat_input_t input;
370 u8 *vbl = 0, *value = 0;
371 char *chroot_path = 0;
377 struct passwd _pw, *pw;
378 struct group _grp, *grp;
381 svmdbtool_main.uid = geteuid ();
382 svmdbtool_main.gid = getegid ();
384 unformat_init_command_line (&input, argv);
386 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
388 if (unformat (&input, "get-string %s", &vbl))
390 get_string (chroot_path, vbl);
394 else if (unformat (&input, "set-string %s %s", &vbl, &value))
396 set_string (chroot_path, vbl, value);
401 else if (unformat (&input, "unset-string %s", &vbl))
403 unset_string (chroot_path, vbl);
407 else if (unformat (&input, "dump-strings"))
409 dump_strings (chroot_path);
412 else if (unformat (&input, "unset-vec %s", &vbl))
414 unset_vec (chroot_path, vbl);
418 else if (unformat (&input, "dump-vecs"))
420 dump_vecs (chroot_path);
423 else if (unformat (&input, "test-vec %s", &vbl))
425 test_vec (chroot_path, vbl);
429 else if (unformat (&input, "vlib-vec-rate %f", &vr))
431 test_vlib_vec_rate (chroot_path, vr);
434 else if (unformat (&input, "test-reg %s", &vbl))
436 test_reg (chroot_path, vbl);
439 else if (unformat (&input, "crash-test"))
441 crash_test (chroot_path);
443 else if (unformat (&input, "chroot %s", &chroot_path_u8))
445 chroot_path = (char *) chroot_path_u8;
447 else if (unformat (&input, "fake-install %s", &value))
449 fake_install (chroot_path, value);
452 else if (unformat (&input, "size %d", &size))
454 map_with_size (chroot_path, size);
457 else if (unformat (&input, "uid %d", &uid))
458 svmdbtool_main.uid = uid;
459 else if (unformat (&input, "gid %d", &gid))
460 svmdbtool_main.gid = gid;
461 else if (unformat (&input, "uid %s", &s))
463 /* lookup the username */
465 rv = getpwnam_r (s, &_pw, buf, sizeof (buf), &pw);
468 fformat (stderr, "cannot fetch username %s", s);
473 fformat (stderr, "username %s does not exist", s);
477 svmdbtool_main.uid = pw->pw_uid;
479 else if (unformat (&input, "gid %s", &s))
481 /* lookup the group name */
483 rv = getgrnam_r (s, &_grp, buf, sizeof (buf), &grp);
486 fformat (stderr, "cannot fetch group %s", s);
491 fformat (stderr, "group %s does not exist", s);
495 svmdbtool_main.gid = grp->gr_gid;
497 else if (unformat (&input, "serialize-strings %s", &filename))
499 vec_add1 (filename, 0);
500 serialize_strings (chroot_path, (char *) filename);
503 else if (unformat (&input, "unserialize-strings %s", &filename))
505 vec_add1 (filename, 0);
506 unserialize_strings (chroot_path, (char *) filename);
515 unformat_free (&input);
519 fformat (stdout, "%s: get-string <name> | set-string <name> <value>\n",
521 fformat (stdout, " unset-string <name> | dump-strings\n");
522 fformat (stdout, " test-vec <name> |\n");
523 fformat (stdout, " unset-vec <name> | dump-vecs\n");
524 fformat (stdout, " chroot <prefix> [uid <nnn-or-userid>]\n");
525 fformat (stdout, " [gid <nnn-or-group-name>]\n");
532 * fd.io coding-style-patch-verification: ON
535 * eval: (c-set-style "gnu")