Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <sys/types.h> |
| 19 | #include <sys/mman.h> |
| 20 | #include <sys/stat.h> |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 21 | #include <pwd.h> |
| 22 | #include <grp.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 23 | #include <netinet/in.h> |
| 24 | #include <signal.h> |
| 25 | #include <pthread.h> |
| 26 | #include <unistd.h> |
| 27 | #include <time.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <string.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> |
| 41 | #include "svmdb.h" |
| 42 | |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 43 | typedef struct |
| 44 | { |
| 45 | svmdb_map_args_t map_args; |
| 46 | int uid, gid; |
| 47 | uword size; |
| 48 | } svmdbtool_main_t; |
| 49 | |
| 50 | svmdbtool_main_t svmdbtool_main; |
| 51 | |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 52 | static inline svmdb_map_args_t * |
| 53 | map_arg_setup (char *chroot_path) |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 54 | { |
| 55 | svmdbtool_main_t *sm = &svmdbtool_main; |
| 56 | svmdb_map_args_t *ma = &sm->map_args; |
| 57 | |
| 58 | memset (ma, 0, sizeof (*ma)); |
| 59 | ma->root_path = chroot_path; |
| 60 | ma->size = sm->size; |
| 61 | ma->uid = sm->uid; |
| 62 | ma->gid = sm->gid; |
| 63 | return ma; |
| 64 | } |
| 65 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 66 | static void |
| 67 | get_string (char *chroot_path, u8 * vbl) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 68 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 69 | svmdb_client_t *c; |
| 70 | char *rv; |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 71 | svmdb_map_args_t *ma; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 72 | |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 73 | ma = map_arg_setup (chroot_path); |
| 74 | |
| 75 | c = svmdb_map (ma); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 76 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 77 | rv = svmdb_local_get_string_variable (c, (char *) vbl); |
| 78 | |
| 79 | fformat (stdout, "%s\n", rv ? rv : "UNSET"); |
| 80 | vec_free (rv); |
| 81 | svmdb_unmap (c); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 84 | static void |
| 85 | set_string (char *chroot_path, u8 * vbl, u8 * value) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 86 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 87 | svmdb_client_t *c; |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 88 | svmdb_map_args_t *ma; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 90 | ma = map_arg_setup (chroot_path); |
| 91 | |
| 92 | c = svmdb_map (ma); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 93 | svmdb_local_set_string_variable (c, (char *) vbl, (char *) value); |
| 94 | svmdb_unmap (c); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 97 | static void |
| 98 | unset_string (char *chroot_path, u8 * vbl) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 100 | svmdb_client_t *c; |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 101 | svmdb_map_args_t *ma; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 102 | |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 103 | ma = map_arg_setup (chroot_path); |
| 104 | |
| 105 | c = svmdb_map (ma); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 106 | svmdb_local_unset_string_variable (c, (char *) vbl); |
| 107 | svmdb_unmap (c); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 110 | static void |
| 111 | dump_strings (char *chroot_path) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 112 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 113 | svmdb_client_t *c; |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 114 | svmdb_map_args_t *ma; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 115 | |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 116 | ma = map_arg_setup (chroot_path); |
| 117 | |
| 118 | c = svmdb_map (ma); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 119 | svmdb_local_dump_strings (c); |
| 120 | svmdb_unmap (c); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 123 | static void |
| 124 | test_vlib_vec_rate (char *chroot_path, f64 vr) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 125 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 126 | svmdb_client_t *c; |
| 127 | f64 *tv = 0; |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 128 | svmdb_map_args_t *ma; |
| 129 | |
| 130 | ma = map_arg_setup (chroot_path); |
| 131 | |
| 132 | c = svmdb_map (ma); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 133 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 134 | vec_add1 (tv, vr); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 135 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 136 | svmdb_local_set_vec_variable (c, "vlib_vector_rate", (char *) tv, |
| 137 | sizeof (*tv)); |
| 138 | svmdb_unmap (c); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 140 | vec_free (tv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | |
| 144 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 145 | static void |
| 146 | test_vec (char *chroot_path, u8 * vbl) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 147 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 148 | svmdb_client_t *c; |
| 149 | u64 *tv = 0; |
| 150 | int i; |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 151 | svmdb_map_args_t *ma; |
| 152 | |
| 153 | ma = map_arg_setup (chroot_path); |
| 154 | |
| 155 | c = svmdb_map (ma); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 156 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 157 | /* my amp goes to 11 */ |
| 158 | for (i = 0; i < 11; i++) |
| 159 | { |
| 160 | vec_add1 (tv, i); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 163 | svmdb_local_set_vec_variable (c, (char *) vbl, (char *) tv, sizeof (tv[0])); |
| 164 | svmdb_unmap (c); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 165 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 166 | vec_free (tv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 169 | static void |
| 170 | fake_install (char *chroot_path, u8 * add_value) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 171 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 172 | svmdb_client_t *c; |
| 173 | u8 *v = 0; |
| 174 | u8 **values = 0; |
| 175 | u8 *oldvalue; |
| 176 | u8 *value; |
| 177 | int nitems = 0, i; |
| 178 | serialize_main_t m; |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 179 | svmdb_map_args_t *ma; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 180 | |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 181 | ma = map_arg_setup (chroot_path); |
| 182 | |
| 183 | c = svmdb_map (ma); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 184 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 185 | oldvalue = svmdb_local_get_vec_variable (c, "installed_sw", 1); |
| 186 | if (oldvalue) |
| 187 | { |
| 188 | unserialize_open_data (&m, oldvalue, vec_len (oldvalue)); |
| 189 | nitems = unserialize_likely_small_unsigned_integer (&m); |
| 190 | for (i = 0; i < nitems; i++) |
| 191 | { |
| 192 | unserialize_cstring (&m, (char **) &value); |
| 193 | vec_add1 (values, value); |
| 194 | } |
| 195 | vec_free (v); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 196 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 197 | nitems++; |
| 198 | value = format (0, "%s%c", add_value, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 199 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 200 | vec_add1 (values, value); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 201 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 202 | fformat (stdout, "Resulting installed_sw vector:\n"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 203 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 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++) |
| 207 | { |
| 208 | fformat (stdout, "%s\n", values[i]); |
| 209 | serialize_cstring (&m, (char *) values[i]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 212 | v = serialize_close_vector (&m); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 213 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 214 | svmdb_local_set_vec_variable (c, "installed_sw", v, sizeof (v[0])); |
| 215 | svmdb_unmap (c); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 216 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 217 | for (i = 0; i < vec_len (values); i++) |
| 218 | vec_free (values[i]); |
| 219 | vec_free (values); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 222 | static void |
| 223 | sigaction_handler (int signum, siginfo_t * i, void *notused) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 224 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 225 | u32 action, opaque; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 226 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 227 | action = (u32) (uword) i->si_ptr; |
| 228 | action >>= 28; |
| 229 | opaque = (u32) (uword) i->si_ptr; |
| 230 | opaque &= ~(0xF0000000); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 231 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 232 | clib_warning ("signal %d, action %d, opaque %x", signum, action, opaque); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 235 | static void |
| 236 | test_reg (char *chroot_path, u8 * vbl) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 237 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 238 | svmdb_client_t *c; |
| 239 | svmdb_notification_args_t args; |
| 240 | svmdb_notification_args_t *a = &args; |
| 241 | struct sigaction sa; |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 242 | svmdb_map_args_t *ma; |
| 243 | |
| 244 | ma = map_arg_setup (chroot_path); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 245 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 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) |
| 250 | { |
| 251 | clib_unix_warning ("sigaction"); |
| 252 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 255 | memset (a, 0, sizeof (*a)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 256 | |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 257 | c = svmdb_map (ma); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 258 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 259 | a->add_del = 1 /* add */ ; |
| 260 | a->nspace = SVMDB_NAMESPACE_STRING; |
| 261 | a->var = (char *) vbl; |
| 262 | a->elsize = 1; |
| 263 | a->signum = SIGUSR2; |
| 264 | a->action = SVMDB_ACTION_GET; |
| 265 | a->opaque = 0x0eadbeef; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 266 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 267 | svmdb_local_add_del_notification (c, a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 268 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 269 | (void) svmdb_local_get_string_variable (c, (char *) vbl); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 270 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 271 | a->add_del = 0; /* del */ |
| 272 | svmdb_local_add_del_notification (c, a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 273 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 274 | |
| 275 | |
| 276 | svmdb_unmap (c); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 279 | static void |
| 280 | unset_vec (char *chroot_path, u8 * vbl) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 281 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 282 | svmdb_client_t *c; |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 283 | svmdb_map_args_t *ma; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 284 | |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 285 | ma = map_arg_setup (chroot_path); |
| 286 | |
| 287 | c = svmdb_map (ma); |
| 288 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 289 | svmdb_local_unset_vec_variable (c, (char *) vbl); |
| 290 | svmdb_unmap (c); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 293 | static void |
| 294 | dump_vecs (char *chroot_path) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 295 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 296 | svmdb_client_t *c; |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 297 | svmdb_map_args_t *ma; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 298 | |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 299 | ma = map_arg_setup (chroot_path); |
| 300 | |
| 301 | c = svmdb_map (ma); |
| 302 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 303 | svmdb_local_dump_vecs (c); |
| 304 | svmdb_unmap (c); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 307 | static void |
| 308 | crash_test (char *chroot_path) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 309 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 310 | svmdb_client_t *c; |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 311 | svmdb_map_args_t *ma; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 312 | |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 313 | ma = map_arg_setup (chroot_path); |
| 314 | |
| 315 | c = svmdb_map (ma); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 316 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 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); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 321 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 322 | abort (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 325 | static void |
| 326 | map_with_size (char *chroot_path, uword size) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 327 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 328 | svmdb_client_t *c; |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 329 | svmdb_map_args_t *ma; |
| 330 | |
| 331 | svmdbtool_main.size = size; |
| 332 | ma = map_arg_setup (chroot_path); |
| 333 | |
| 334 | c = svmdb_map (ma); |
| 335 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 336 | svmdb_unmap (c); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 339 | int |
| 340 | main (int argc, char **argv) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 341 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 342 | unformat_input_t input; |
| 343 | int parsed = 0; |
| 344 | u8 *vbl = 0, *value = 0; |
| 345 | char *chroot_path = 0; |
| 346 | u8 *chroot_path_u8; |
| 347 | uword size; |
| 348 | f64 vr; |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 349 | int uid, gid, rv; |
| 350 | struct passwd _pw, *pw; |
| 351 | struct group _grp, *grp; |
| 352 | char *s, buf[128]; |
| 353 | |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 354 | svmdbtool_main.uid = geteuid (); |
| 355 | svmdbtool_main.gid = getegid (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 356 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 357 | unformat_init_command_line (&input, argv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 358 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 359 | while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT) |
| 360 | { |
| 361 | if (unformat (&input, "get-string %s", &vbl)) |
| 362 | { |
| 363 | get_string (chroot_path, vbl); |
| 364 | vec_free (vbl); |
| 365 | parsed++; |
| 366 | } |
| 367 | else if (unformat (&input, "set-string %s %s", &vbl, &value)) |
| 368 | { |
| 369 | set_string (chroot_path, vbl, value); |
| 370 | vec_free (vbl); |
| 371 | vec_free (value); |
| 372 | parsed++; |
| 373 | } |
| 374 | else if (unformat (&input, "unset-string %s", &vbl)) |
| 375 | { |
| 376 | unset_string (chroot_path, vbl); |
| 377 | vec_free (vbl); |
| 378 | parsed++; |
| 379 | } |
| 380 | else if (unformat (&input, "dump-strings")) |
| 381 | { |
| 382 | dump_strings (chroot_path); |
| 383 | parsed++; |
| 384 | } |
| 385 | else if (unformat (&input, "unset-vec %s", &vbl)) |
| 386 | { |
| 387 | unset_vec (chroot_path, vbl); |
| 388 | vec_free (vbl); |
| 389 | parsed++; |
| 390 | } |
| 391 | else if (unformat (&input, "dump-vecs")) |
| 392 | { |
| 393 | dump_vecs (chroot_path); |
| 394 | parsed++; |
| 395 | } |
| 396 | else if (unformat (&input, "test-vec %s", &vbl)) |
| 397 | { |
| 398 | test_vec (chroot_path, vbl); |
| 399 | // vec_free(vbl); |
| 400 | parsed++; |
| 401 | } |
| 402 | else if (unformat (&input, "vlib-vec-rate %f", &vr)) |
| 403 | { |
| 404 | test_vlib_vec_rate (chroot_path, vr); |
| 405 | parsed++; |
| 406 | } |
| 407 | else if (unformat (&input, "test-reg %s", &vbl)) |
| 408 | { |
| 409 | test_reg (chroot_path, vbl); |
| 410 | parsed++; |
| 411 | } |
| 412 | else if (unformat (&input, "crash-test")) |
| 413 | { |
| 414 | crash_test (chroot_path); |
| 415 | } |
| 416 | else if (unformat (&input, "chroot %s", &chroot_path_u8)) |
| 417 | { |
| 418 | chroot_path = (char *) chroot_path_u8; |
| 419 | } |
| 420 | else if (unformat (&input, "fake-install %s", &value)) |
| 421 | { |
| 422 | fake_install (chroot_path, value); |
| 423 | parsed++; |
| 424 | } |
| 425 | else if (unformat (&input, "size %d", &size)) |
| 426 | { |
| 427 | map_with_size (chroot_path, size); |
| 428 | parsed++; |
| 429 | } |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 430 | else if (unformat (&input, "uid %d", &uid)) |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 431 | svmdbtool_main.uid = uid; |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 432 | else if (unformat (&input, "gid %d", &gid)) |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 433 | svmdbtool_main.gid = gid; |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 434 | else if (unformat (&input, "uid %s", &s)) |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 435 | { |
| 436 | /* lookup the username */ |
| 437 | pw = NULL; |
| 438 | rv = getpwnam_r (s, &_pw, buf, sizeof (buf), &pw); |
| 439 | if (rv < 0) |
| 440 | { |
| 441 | fformat (stderr, "cannot fetch username %s", s); |
| 442 | exit (1); |
| 443 | } |
| 444 | if (pw == NULL) |
| 445 | { |
| 446 | fformat (stderr, "username %s does not exist", s); |
| 447 | exit (1); |
| 448 | } |
| 449 | vec_free (s); |
| 450 | svmdbtool_main.uid = pw->pw_uid; |
| 451 | } |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 452 | else if (unformat (&input, "gid %s", &s)) |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 453 | { |
| 454 | /* lookup the group name */ |
| 455 | grp = NULL; |
| 456 | rv = getgrnam_r (s, &_grp, buf, sizeof (buf), &grp); |
| 457 | if (rv != 0) |
| 458 | { |
| 459 | fformat (stderr, "cannot fetch group %s", s); |
| 460 | exit (1); |
| 461 | } |
| 462 | if (grp == NULL) |
| 463 | { |
| 464 | fformat (stderr, "group %s does not exist", s); |
| 465 | exit (1); |
| 466 | } |
| 467 | vec_free (s); |
| 468 | svmdbtool_main.gid = grp->gr_gid; |
| 469 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 470 | else |
| 471 | { |
| 472 | break; |
| 473 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 476 | unformat_free (&input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 477 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 478 | if (!parsed) |
| 479 | { |
| 480 | fformat (stdout, "%s: get-string <name> | set-string <name> <value>\n", |
| 481 | argv[0]); |
| 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"); |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 485 | fformat (stdout, " chroot <prefix> [uid <nnn-or-userid>]\n"); |
| 486 | fformat (stdout, " [gid <nnn-or-group-name>]\n"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 487 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 488 | |
| 489 | exit (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 490 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 491 | |
| 492 | /* |
| 493 | * fd.io coding-style-patch-verification: ON |
| 494 | * |
| 495 | * Local Variables: |
| 496 | * eval: (c-set-style "gnu") |
| 497 | * End: |
| 498 | */ |