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 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 58 | clib_memset (ma, 0, sizeof (*ma)); |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 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 |
Dave Barach | 23a7412 | 2016-11-24 16:34:20 -0500 | [diff] [blame] | 124 | serialize_strings (char *chroot_path, char *filename) |
| 125 | { |
| 126 | svmdb_client_t *c; |
| 127 | svmdb_map_args_t *ma; |
| 128 | |
| 129 | ma = map_arg_setup (chroot_path); |
| 130 | |
| 131 | c = svmdb_map (ma); |
| 132 | (void) svmdb_local_serialize_strings (c, filename); |
| 133 | svmdb_unmap (c); |
| 134 | } |
| 135 | |
| 136 | static void |
| 137 | unserialize_strings (char *chroot_path, char *filename) |
| 138 | { |
| 139 | svmdb_client_t *c; |
| 140 | svmdb_map_args_t *ma; |
| 141 | |
| 142 | ma = map_arg_setup (chroot_path); |
| 143 | |
| 144 | c = svmdb_map (ma); |
| 145 | (void) svmdb_local_unserialize_strings (c, filename); |
| 146 | svmdb_unmap (c); |
| 147 | } |
| 148 | |
| 149 | static void |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 150 | test_vlib_vec_rate (char *chroot_path, f64 vr) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 151 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 152 | svmdb_client_t *c; |
| 153 | f64 *tv = 0; |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 154 | svmdb_map_args_t *ma; |
| 155 | |
| 156 | ma = map_arg_setup (chroot_path); |
| 157 | |
| 158 | c = svmdb_map (ma); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 159 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 160 | vec_add1 (tv, vr); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 162 | svmdb_local_set_vec_variable (c, "vlib_vector_rate", (char *) tv, |
| 163 | sizeof (*tv)); |
| 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 | |
| 169 | |
| 170 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 171 | static void |
| 172 | test_vec (char *chroot_path, u8 * vbl) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 173 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 174 | svmdb_client_t *c; |
| 175 | u64 *tv = 0; |
| 176 | int i; |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 177 | svmdb_map_args_t *ma; |
| 178 | |
| 179 | ma = map_arg_setup (chroot_path); |
| 180 | |
| 181 | c = svmdb_map (ma); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 182 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 183 | /* my amp goes to 11 */ |
| 184 | for (i = 0; i < 11; i++) |
| 185 | { |
| 186 | vec_add1 (tv, i); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 189 | svmdb_local_set_vec_variable (c, (char *) vbl, (char *) tv, sizeof (tv[0])); |
| 190 | svmdb_unmap (c); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 191 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 192 | vec_free (tv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 195 | static void |
| 196 | fake_install (char *chroot_path, u8 * add_value) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 197 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 198 | svmdb_client_t *c; |
| 199 | u8 *v = 0; |
| 200 | u8 **values = 0; |
| 201 | u8 *oldvalue; |
| 202 | u8 *value; |
| 203 | int nitems = 0, i; |
| 204 | serialize_main_t m; |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 205 | svmdb_map_args_t *ma; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 206 | |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 207 | ma = map_arg_setup (chroot_path); |
| 208 | |
| 209 | c = svmdb_map (ma); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 210 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 211 | oldvalue = svmdb_local_get_vec_variable (c, "installed_sw", 1); |
| 212 | if (oldvalue) |
| 213 | { |
| 214 | unserialize_open_data (&m, oldvalue, vec_len (oldvalue)); |
| 215 | nitems = unserialize_likely_small_unsigned_integer (&m); |
| 216 | for (i = 0; i < nitems; i++) |
| 217 | { |
| 218 | unserialize_cstring (&m, (char **) &value); |
| 219 | vec_add1 (values, value); |
| 220 | } |
| 221 | vec_free (v); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 222 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 223 | nitems++; |
| 224 | value = format (0, "%s%c", add_value, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 225 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 226 | vec_add1 (values, value); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 227 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 228 | fformat (stdout, "Resulting installed_sw vector:\n"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 229 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 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++) |
| 233 | { |
| 234 | fformat (stdout, "%s\n", values[i]); |
| 235 | serialize_cstring (&m, (char *) values[i]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 238 | v = serialize_close_vector (&m); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 239 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 240 | svmdb_local_set_vec_variable (c, "installed_sw", v, sizeof (v[0])); |
| 241 | svmdb_unmap (c); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 242 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 243 | for (i = 0; i < vec_len (values); i++) |
| 244 | vec_free (values[i]); |
| 245 | vec_free (values); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 248 | static void |
| 249 | sigaction_handler (int signum, siginfo_t * i, void *notused) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 250 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 251 | u32 action, opaque; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 252 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 253 | action = (u32) (uword) i->si_ptr; |
| 254 | action >>= 28; |
| 255 | opaque = (u32) (uword) i->si_ptr; |
| 256 | opaque &= ~(0xF0000000); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 257 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 258 | clib_warning ("signal %d, action %d, opaque %x", signum, action, opaque); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 261 | static void |
| 262 | test_reg (char *chroot_path, u8 * vbl) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 263 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 264 | svmdb_client_t *c; |
| 265 | svmdb_notification_args_t args; |
| 266 | svmdb_notification_args_t *a = &args; |
| 267 | struct sigaction sa; |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 268 | svmdb_map_args_t *ma; |
| 269 | |
| 270 | ma = map_arg_setup (chroot_path); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 271 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 272 | clib_memset (&sa, 0, sizeof (sa)); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 273 | sa.sa_sigaction = sigaction_handler; |
| 274 | sa.sa_flags = SA_SIGINFO; |
| 275 | if (sigaction (SIGUSR2, &sa, 0) < 0) |
| 276 | { |
| 277 | clib_unix_warning ("sigaction"); |
| 278 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 281 | clib_memset (a, 0, sizeof (*a)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 282 | |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 283 | c = svmdb_map (ma); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 284 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 285 | a->add_del = 1 /* add */ ; |
| 286 | a->nspace = SVMDB_NAMESPACE_STRING; |
| 287 | a->var = (char *) vbl; |
| 288 | a->elsize = 1; |
| 289 | a->signum = SIGUSR2; |
| 290 | a->action = SVMDB_ACTION_GET; |
| 291 | a->opaque = 0x0eadbeef; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 292 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 293 | svmdb_local_add_del_notification (c, a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 294 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 295 | (void) svmdb_local_get_string_variable (c, (char *) vbl); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 296 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 297 | a->add_del = 0; /* del */ |
| 298 | svmdb_local_add_del_notification (c, a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 299 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 300 | |
| 301 | |
| 302 | svmdb_unmap (c); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 305 | static void |
| 306 | unset_vec (char *chroot_path, u8 * vbl) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 307 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 308 | svmdb_client_t *c; |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 309 | svmdb_map_args_t *ma; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 310 | |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 311 | ma = map_arg_setup (chroot_path); |
| 312 | |
| 313 | c = svmdb_map (ma); |
| 314 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 315 | svmdb_local_unset_vec_variable (c, (char *) vbl); |
| 316 | svmdb_unmap (c); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 319 | static void |
| 320 | dump_vecs (char *chroot_path) |
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 | svmdb_client_t *c; |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 323 | svmdb_map_args_t *ma; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 324 | |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 325 | ma = map_arg_setup (chroot_path); |
| 326 | |
| 327 | c = svmdb_map (ma); |
| 328 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 329 | svmdb_local_dump_vecs (c); |
| 330 | svmdb_unmap (c); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 333 | static void |
| 334 | crash_test (char *chroot_path) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 335 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 336 | svmdb_client_t *c; |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 337 | svmdb_map_args_t *ma; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 338 | |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 339 | ma = map_arg_setup (chroot_path); |
| 340 | |
| 341 | c = svmdb_map (ma); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 342 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 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); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 347 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 348 | abort (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 351 | static void |
| 352 | map_with_size (char *chroot_path, uword size) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 353 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 354 | svmdb_client_t *c; |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 355 | svmdb_map_args_t *ma; |
| 356 | |
| 357 | svmdbtool_main.size = size; |
| 358 | ma = map_arg_setup (chroot_path); |
| 359 | |
| 360 | c = svmdb_map (ma); |
| 361 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 362 | svmdb_unmap (c); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 365 | int |
| 366 | main (int argc, char **argv) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 367 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 368 | unformat_input_t input; |
| 369 | int parsed = 0; |
| 370 | u8 *vbl = 0, *value = 0; |
| 371 | char *chroot_path = 0; |
| 372 | u8 *chroot_path_u8; |
Dave Barach | 23a7412 | 2016-11-24 16:34:20 -0500 | [diff] [blame] | 373 | u8 *filename; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 374 | uword size; |
| 375 | f64 vr; |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 376 | int uid, gid, rv; |
| 377 | struct passwd _pw, *pw; |
| 378 | struct group _grp, *grp; |
| 379 | char *s, buf[128]; |
| 380 | |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 381 | svmdbtool_main.uid = geteuid (); |
| 382 | svmdbtool_main.gid = getegid (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 383 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 384 | unformat_init_command_line (&input, argv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 385 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 386 | while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT) |
| 387 | { |
| 388 | if (unformat (&input, "get-string %s", &vbl)) |
| 389 | { |
| 390 | get_string (chroot_path, vbl); |
| 391 | vec_free (vbl); |
| 392 | parsed++; |
| 393 | } |
| 394 | else if (unformat (&input, "set-string %s %s", &vbl, &value)) |
| 395 | { |
| 396 | set_string (chroot_path, vbl, value); |
| 397 | vec_free (vbl); |
| 398 | vec_free (value); |
| 399 | parsed++; |
| 400 | } |
| 401 | else if (unformat (&input, "unset-string %s", &vbl)) |
| 402 | { |
| 403 | unset_string (chroot_path, vbl); |
| 404 | vec_free (vbl); |
| 405 | parsed++; |
| 406 | } |
| 407 | else if (unformat (&input, "dump-strings")) |
| 408 | { |
| 409 | dump_strings (chroot_path); |
| 410 | parsed++; |
| 411 | } |
| 412 | else if (unformat (&input, "unset-vec %s", &vbl)) |
| 413 | { |
| 414 | unset_vec (chroot_path, vbl); |
| 415 | vec_free (vbl); |
| 416 | parsed++; |
| 417 | } |
| 418 | else if (unformat (&input, "dump-vecs")) |
| 419 | { |
| 420 | dump_vecs (chroot_path); |
| 421 | parsed++; |
| 422 | } |
| 423 | else if (unformat (&input, "test-vec %s", &vbl)) |
| 424 | { |
| 425 | test_vec (chroot_path, vbl); |
| 426 | // vec_free(vbl); |
| 427 | parsed++; |
| 428 | } |
| 429 | else if (unformat (&input, "vlib-vec-rate %f", &vr)) |
| 430 | { |
| 431 | test_vlib_vec_rate (chroot_path, vr); |
| 432 | parsed++; |
| 433 | } |
| 434 | else if (unformat (&input, "test-reg %s", &vbl)) |
| 435 | { |
| 436 | test_reg (chroot_path, vbl); |
| 437 | parsed++; |
| 438 | } |
| 439 | else if (unformat (&input, "crash-test")) |
| 440 | { |
| 441 | crash_test (chroot_path); |
| 442 | } |
| 443 | else if (unformat (&input, "chroot %s", &chroot_path_u8)) |
| 444 | { |
| 445 | chroot_path = (char *) chroot_path_u8; |
| 446 | } |
| 447 | else if (unformat (&input, "fake-install %s", &value)) |
| 448 | { |
| 449 | fake_install (chroot_path, value); |
| 450 | parsed++; |
| 451 | } |
| 452 | else if (unformat (&input, "size %d", &size)) |
| 453 | { |
| 454 | map_with_size (chroot_path, size); |
| 455 | parsed++; |
| 456 | } |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 457 | else if (unformat (&input, "uid %d", &uid)) |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 458 | svmdbtool_main.uid = uid; |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 459 | else if (unformat (&input, "gid %d", &gid)) |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 460 | svmdbtool_main.gid = gid; |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 461 | else if (unformat (&input, "uid %s", &s)) |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 462 | { |
| 463 | /* lookup the username */ |
| 464 | pw = NULL; |
| 465 | rv = getpwnam_r (s, &_pw, buf, sizeof (buf), &pw); |
| 466 | if (rv < 0) |
| 467 | { |
| 468 | fformat (stderr, "cannot fetch username %s", s); |
| 469 | exit (1); |
| 470 | } |
| 471 | if (pw == NULL) |
| 472 | { |
| 473 | fformat (stderr, "username %s does not exist", s); |
| 474 | exit (1); |
| 475 | } |
| 476 | vec_free (s); |
| 477 | svmdbtool_main.uid = pw->pw_uid; |
| 478 | } |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 479 | else if (unformat (&input, "gid %s", &s)) |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 480 | { |
| 481 | /* lookup the group name */ |
| 482 | grp = NULL; |
| 483 | rv = getgrnam_r (s, &_grp, buf, sizeof (buf), &grp); |
| 484 | if (rv != 0) |
| 485 | { |
| 486 | fformat (stderr, "cannot fetch group %s", s); |
| 487 | exit (1); |
| 488 | } |
| 489 | if (grp == NULL) |
| 490 | { |
| 491 | fformat (stderr, "group %s does not exist", s); |
| 492 | exit (1); |
| 493 | } |
| 494 | vec_free (s); |
| 495 | svmdbtool_main.gid = grp->gr_gid; |
| 496 | } |
Dave Barach | 23a7412 | 2016-11-24 16:34:20 -0500 | [diff] [blame] | 497 | else if (unformat (&input, "serialize-strings %s", &filename)) |
| 498 | { |
| 499 | vec_add1 (filename, 0); |
| 500 | serialize_strings (chroot_path, (char *) filename); |
| 501 | parsed++; |
| 502 | } |
| 503 | else if (unformat (&input, "unserialize-strings %s", &filename)) |
| 504 | { |
| 505 | vec_add1 (filename, 0); |
| 506 | unserialize_strings (chroot_path, (char *) filename); |
| 507 | parsed++; |
| 508 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 509 | else |
| 510 | { |
| 511 | break; |
| 512 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 515 | unformat_free (&input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 516 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 517 | if (!parsed) |
| 518 | { |
| 519 | fformat (stdout, "%s: get-string <name> | set-string <name> <value>\n", |
| 520 | argv[0]); |
| 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"); |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 524 | fformat (stdout, " chroot <prefix> [uid <nnn-or-userid>]\n"); |
| 525 | fformat (stdout, " [gid <nnn-or-group-name>]\n"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 526 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 527 | |
| 528 | exit (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 529 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 530 | |
| 531 | /* |
| 532 | * fd.io coding-style-patch-verification: ON |
| 533 | * |
| 534 | * Local Variables: |
| 535 | * eval: (c-set-style "gnu") |
| 536 | * End: |
| 537 | */ |