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