Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2 | *------------------------------------------------------------------ |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 3 | * svmtool.c |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) 2009 Cisco and/or its affiliates. |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at: |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | *------------------------------------------------------------------ |
| 18 | */ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/mman.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <netinet/in.h> |
| 26 | #include <signal.h> |
| 27 | #include <pthread.h> |
| 28 | #include <unistd.h> |
| 29 | #include <time.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <string.h> |
| 32 | #include <vppinfra/clib.h> |
| 33 | #include <vppinfra/vec.h> |
| 34 | #include <vppinfra/hash.h> |
| 35 | #include <vppinfra/bitmap.h> |
| 36 | #include <vppinfra/fifo.h> |
| 37 | #include <vppinfra/time.h> |
| 38 | #include <vppinfra/mheap.h> |
| 39 | #include <vppinfra/heap.h> |
| 40 | #include <vppinfra/pool.h> |
| 41 | #include <vppinfra/format.h> |
| 42 | |
| 43 | #include "svm.h" |
| 44 | |
| 45 | |
| 46 | |
| 47 | /* |
| 48 | * format_all_svm_regions |
| 49 | * Maps / unmaps regions. Do NOT call from client code! |
| 50 | */ |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 51 | u8 * |
| 52 | format_all_svm_regions (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 53 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 54 | int verbose = va_arg (*args, int); |
| 55 | svm_region_t *root_rp = svm_get_root_rp (); |
| 56 | svm_main_region_t *mp; |
| 57 | svm_subregion_t *subp; |
| 58 | svm_region_t *rp; |
| 59 | svm_map_region_args_t *a = 0; |
| 60 | u8 **svm_names = 0; |
| 61 | u8 *name = 0; |
| 62 | int i; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 63 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 64 | ASSERT (root_rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 66 | pthread_mutex_lock (&root_rp->mutex); |
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 | s = format (s, "%U", format_svm_region, root_rp, verbose); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 69 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 70 | mp = root_rp->data_base; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 71 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 72 | /* |
| 73 | * Snapshoot names, can't hold root rp mutex across |
| 74 | * find_or_create. |
| 75 | */ |
| 76 | /* *INDENT-OFF* */ |
| 77 | pool_foreach (subp, mp->subregions, ({ |
| 78 | name = vec_dup (subp->subregion_name); |
| 79 | vec_add1(svm_names, name); |
| 80 | })); |
| 81 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 82 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 83 | pthread_mutex_unlock (&root_rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 84 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 85 | for (i = 0; i < vec_len (svm_names); i++) |
| 86 | { |
| 87 | vec_validate (a, 0); |
| 88 | a->name = (char *) svm_names[i]; |
| 89 | rp = svm_region_find_or_create (a); |
| 90 | if (rp) |
| 91 | { |
| 92 | pthread_mutex_lock (&rp->mutex); |
| 93 | s = format (s, "%U", format_svm_region, rp, verbose); |
| 94 | pthread_mutex_unlock (&rp->mutex); |
| 95 | svm_region_unmap (rp); |
| 96 | vec_free (svm_names[i]); |
| 97 | } |
| 98 | vec_free (a); |
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 | vec_free (svm_names); |
| 101 | return (s); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 104 | void |
| 105 | show (char *chroot_path, int verbose) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 107 | svm_map_region_args_t *a = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 108 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 109 | vec_validate (a, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 111 | svm_region_init_chroot (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 | fformat (stdout, "My pid is %d\n", getpid ()); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 114 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 115 | fformat (stdout, "%U", format_all_svm_regions, verbose); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 116 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 117 | svm_region_exit (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 119 | vec_free (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 123 | static void * |
| 124 | svm_map_region_nolock (svm_map_region_args_t * a) |
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 | int svm_fd; |
| 127 | svm_region_t *rp; |
| 128 | int deadman = 0; |
| 129 | u8 *shm_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 131 | ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 132 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 133 | shm_name = shm_name_from_svm_map_region_args (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 134 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 135 | svm_fd = shm_open ((char *) shm_name, O_RDWR, 0777); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 136 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 137 | if (svm_fd < 0) |
| 138 | { |
| 139 | perror ("svm_region_map(mmap open)"); |
| 140 | return (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 141 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 142 | vec_free (shm_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 143 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 144 | rp = mmap (0, MMAP_PAGESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, svm_fd, 0); |
| 145 | |
| 146 | if (rp == (svm_region_t *) MAP_FAILED) |
| 147 | { |
| 148 | close (svm_fd); |
| 149 | clib_warning ("mmap"); |
| 150 | return (0); |
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 | /* |
| 153 | * We lost the footrace to create this region; make sure |
| 154 | * the winner has crossed the finish line. |
| 155 | */ |
| 156 | while (rp->version == 0 && deadman++ < 5) |
| 157 | { |
| 158 | sleep (1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 161 | /* |
| 162 | * <bleep>-ed? |
| 163 | */ |
| 164 | if (rp->version == 0) |
| 165 | { |
| 166 | clib_warning ("rp->version %d not %d", rp->version, SVM_VERSION); |
| 167 | return (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 168 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 169 | /* Remap now that the region has been placed */ |
| 170 | a->baseva = rp->virtual_base; |
| 171 | a->size = rp->virtual_size; |
| 172 | munmap (rp, MMAP_PAGESIZE); |
| 173 | |
| 174 | rp = (void *) mmap ((void *) a->baseva, a->size, |
| 175 | PROT_READ | PROT_WRITE, |
| 176 | MAP_SHARED | MAP_FIXED, svm_fd, 0); |
| 177 | if ((uword) rp == (uword) MAP_FAILED) |
| 178 | { |
| 179 | clib_unix_warning ("mmap"); |
| 180 | return (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 183 | if ((uword) rp != rp->virtual_base) |
| 184 | { |
| 185 | clib_warning ("mmap botch"); |
| 186 | } |
| 187 | |
| 188 | if (pthread_mutex_trylock (&rp->mutex)) |
| 189 | { |
| 190 | clib_warning ("rp->mutex LOCKED by pid %d, tag %d, cleared...", |
| 191 | rp->mutex_owner_pid, rp->mutex_owner_tag); |
| 192 | memset (&rp->mutex, 0, sizeof (rp->mutex)); |
| 193 | |
| 194 | } |
| 195 | else |
| 196 | { |
| 197 | clib_warning ("mutex OK...\n"); |
| 198 | pthread_mutex_unlock (&rp->mutex); |
| 199 | } |
| 200 | |
| 201 | return ((void *) rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /* |
| 205 | * rnd_pagesize |
| 206 | * Round to a pagesize multiple, presumably 4k works |
| 207 | */ |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 208 | static unsigned int |
| 209 | rnd_pagesize (unsigned int size) |
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 | unsigned int rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 212 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 213 | rv = (size + (MMAP_PAGESIZE - 1)) & ~(MMAP_PAGESIZE - 1); |
| 214 | return (rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | #define MUTEX_DEBUG |
| 218 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 219 | always_inline void |
| 220 | region_lock (svm_region_t * rp, int tag) |
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 | pthread_mutex_lock (&rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 223 | #ifdef MUTEX_DEBUG |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 224 | rp->mutex_owner_pid = getpid (); |
| 225 | rp->mutex_owner_tag = tag; |
| 226 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 229 | always_inline void |
| 230 | region_unlock (svm_region_t * rp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 231 | { |
| 232 | #ifdef MUTEX_DEBUG |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 233 | rp->mutex_owner_pid = 0; |
| 234 | rp->mutex_owner_tag = 0; |
| 235 | #endif |
| 236 | pthread_mutex_unlock (&rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 240 | static void * |
| 241 | svm_existing_region_map_nolock (void *root_arg, svm_map_region_args_t * a) |
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 | svm_region_t *root_rp = root_arg; |
| 244 | svm_main_region_t *mp; |
| 245 | svm_region_t *rp; |
| 246 | void *oldheap; |
| 247 | uword *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 248 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 249 | a->size += MMAP_PAGESIZE + SVM_PVT_MHEAP_SIZE; |
| 250 | a->size = rnd_pagesize (a->size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 251 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 252 | region_lock (root_rp, 4); |
| 253 | oldheap = svm_push_pvt_heap (root_rp); |
| 254 | mp = root_rp->data_base; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 255 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 256 | ASSERT (mp); |
| 257 | |
| 258 | p = hash_get_mem (mp->name_hash, a->name); |
| 259 | |
| 260 | if (p) |
| 261 | { |
| 262 | rp = svm_map_region_nolock (a); |
| 263 | region_unlock (root_rp); |
| 264 | svm_pop_heap (oldheap); |
| 265 | return rp; |
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 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 268 | |
| 269 | } |
| 270 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 271 | static void |
| 272 | trace (char *chroot_path, char *name, int enable_disable) |
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 | svm_map_region_args_t *a = 0; |
| 275 | svm_region_t *db_rp; |
| 276 | void *oldheap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 277 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 278 | vec_validate (a, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 279 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 280 | svm_region_init_chroot (chroot_path); |
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 | a->name = name; |
| 283 | a->size = 1 << 20; |
| 284 | a->flags = SVM_FLAGS_MHEAP; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 285 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 286 | db_rp = svm_region_find_or_create (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 287 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 288 | ASSERT (db_rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 289 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 290 | region_lock (db_rp, 20); |
| 291 | |
| 292 | oldheap = svm_push_data_heap (db_rp); |
| 293 | |
| 294 | mheap_trace (db_rp->data_heap, enable_disable); |
| 295 | |
| 296 | svm_pop_heap (oldheap); |
| 297 | region_unlock (db_rp); |
| 298 | |
| 299 | svm_region_unmap ((void *) db_rp); |
| 300 | svm_region_exit (); |
| 301 | vec_free (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | |
| 305 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 306 | static void |
| 307 | subregion_repair (char *chroot_path) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 308 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 309 | int i; |
| 310 | svm_main_region_t *mp; |
| 311 | svm_map_region_args_t a; |
| 312 | svm_region_t *root_rp; |
| 313 | svm_region_t *rp; |
| 314 | svm_subregion_t *subp; |
| 315 | u8 *name = 0; |
| 316 | u8 **svm_names = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 317 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 318 | svm_region_init_chroot (chroot_path); |
| 319 | root_rp = svm_get_root_rp (); |
| 320 | |
| 321 | pthread_mutex_lock (&root_rp->mutex); |
| 322 | |
| 323 | mp = root_rp->data_base; |
| 324 | |
| 325 | /* |
| 326 | * Snapshoot names, can't hold root rp mutex across |
| 327 | * find_or_create. |
| 328 | */ |
| 329 | /* *INDENT-OFF* */ |
| 330 | pool_foreach (subp, mp->subregions, ({ |
| 331 | name = vec_dup (subp->subregion_name); |
| 332 | vec_add1(svm_names, name); |
| 333 | })); |
| 334 | /* *INDENT-ON* */ |
| 335 | |
| 336 | pthread_mutex_unlock (&root_rp->mutex); |
| 337 | |
| 338 | for (i = 0; i < vec_len (svm_names); i++) |
| 339 | { |
| 340 | memset (&a, 0, sizeof (a)); |
| 341 | a.root_path = chroot_path; |
| 342 | a.name = (char *) svm_names[i]; |
| 343 | fformat (stdout, "Checking %s region...\n", a.name); |
| 344 | rp = svm_existing_region_map_nolock (root_rp, &a); |
| 345 | if (rp) |
| 346 | { |
| 347 | svm_region_unmap (rp); |
| 348 | vec_free (svm_names[i]); |
| 349 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 350 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 351 | vec_free (svm_names); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 354 | void |
| 355 | repair (char *chroot_path, int crash_root_region) |
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 | svm_region_t *root_rp = 0; |
| 358 | svm_map_region_args_t *a = 0; |
| 359 | void *svm_map_region (svm_map_region_args_t * a); |
| 360 | int svm_fd; |
| 361 | u8 *shm_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 362 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 363 | fformat (stdout, "our pid: %d\n", getpid ()); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 364 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 365 | vec_validate (a, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 366 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 367 | a->root_path = chroot_path; |
| 368 | a->name = SVM_GLOBAL_REGION_NAME; |
| 369 | a->baseva = SVM_GLOBAL_REGION_BASEVA; |
| 370 | a->size = SVM_GLOBAL_REGION_SIZE; |
| 371 | a->flags = SVM_FLAGS_NODATA; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 372 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 373 | shm_name = shm_name_from_svm_map_region_args (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 374 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 375 | svm_fd = shm_open ((char *) shm_name, O_RDWR, 0777); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 376 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 377 | if (svm_fd < 0) |
| 378 | { |
| 379 | perror ("svm_region_map(mmap open)"); |
| 380 | goto out; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 383 | vec_free (shm_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 384 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 385 | root_rp = mmap (0, MMAP_PAGESIZE, |
| 386 | PROT_READ | PROT_WRITE, MAP_SHARED, svm_fd, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 387 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 388 | if (root_rp == (svm_region_t *) MAP_FAILED) |
| 389 | { |
| 390 | close (svm_fd); |
| 391 | clib_warning ("mmap"); |
| 392 | goto out; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 393 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 394 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 395 | /* Remap now that the region has been placed */ |
| 396 | clib_warning ("remap to 0x%x", root_rp->virtual_base); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 397 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 398 | a->baseva = root_rp->virtual_base; |
| 399 | a->size = root_rp->virtual_size; |
| 400 | munmap (root_rp, MMAP_PAGESIZE); |
| 401 | |
| 402 | root_rp = (void *) mmap ((void *) a->baseva, a->size, |
| 403 | PROT_READ | PROT_WRITE, |
| 404 | MAP_SHARED | MAP_FIXED, svm_fd, 0); |
| 405 | if ((uword) root_rp == (uword) MAP_FAILED) |
| 406 | { |
| 407 | clib_unix_warning ("mmap"); |
| 408 | goto out; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 409 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 410 | |
| 411 | close (svm_fd); |
| 412 | |
| 413 | if ((uword) root_rp != root_rp->virtual_base) |
| 414 | { |
| 415 | clib_warning ("mmap botch"); |
| 416 | goto out; |
| 417 | } |
| 418 | |
| 419 | if (pthread_mutex_trylock (&root_rp->mutex)) |
| 420 | { |
| 421 | clib_warning ("root_rp->mutex LOCKED by pid %d, tag %d, cleared...", |
| 422 | root_rp->mutex_owner_pid, root_rp->mutex_owner_tag); |
| 423 | memset (&root_rp->mutex, 0, sizeof (root_rp->mutex)); |
| 424 | goto out; |
| 425 | } |
| 426 | else |
| 427 | { |
| 428 | clib_warning ("root_rp->mutex OK...\n"); |
| 429 | pthread_mutex_unlock (&root_rp->mutex); |
| 430 | } |
| 431 | |
| 432 | out: |
| 433 | vec_free (a); |
| 434 | /* |
| 435 | * Now that the root region is known to be OK, |
| 436 | * fix broken subregions |
| 437 | */ |
| 438 | subregion_repair (chroot_path); |
| 439 | |
| 440 | if (crash_root_region) |
| 441 | { |
| 442 | clib_warning ("Leaving root region locked on purpose..."); |
| 443 | pthread_mutex_lock (&root_rp->mutex); |
| 444 | root_rp->mutex_owner_pid = getpid (); |
| 445 | root_rp->mutex_owner_tag = 99; |
| 446 | } |
| 447 | svm_region_exit (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 450 | int |
| 451 | main (int argc, char **argv) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 452 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 453 | unformat_input_t input; |
| 454 | int parsed = 0; |
| 455 | char *name; |
| 456 | char *chroot_path = 0; |
| 457 | u8 *chroot_u8; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 458 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 459 | unformat_init_command_line (&input, argv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 460 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 461 | while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT) |
| 462 | { |
| 463 | if (unformat (&input, "show-verbose")) |
| 464 | { |
| 465 | show (chroot_path, 1); |
| 466 | parsed++; |
| 467 | } |
| 468 | else if (unformat (&input, "show")) |
| 469 | { |
| 470 | show (chroot_path, 0); |
| 471 | parsed++; |
| 472 | } |
| 473 | else if (unformat (&input, "client-scan")) |
| 474 | { |
| 475 | svm_client_scan (chroot_path); |
| 476 | parsed++; |
| 477 | } |
| 478 | else if (unformat (&input, "repair")) |
| 479 | { |
| 480 | repair (chroot_path, 0 /* fix it */ ); |
| 481 | parsed++; |
| 482 | } |
| 483 | else if (unformat (&input, "crash")) |
| 484 | { |
| 485 | repair (chroot_path, 1 /* crash it */ ); |
| 486 | parsed++; |
| 487 | } |
| 488 | else if (unformat (&input, "trace-on %s", &name)) |
| 489 | { |
| 490 | trace (chroot_path, name, 1); |
| 491 | parsed++; |
| 492 | } |
| 493 | else if (unformat (&input, "trace-off %s", &name)) |
| 494 | { |
| 495 | trace (chroot_path, name, 0); |
| 496 | parsed++; |
| 497 | } |
| 498 | else if (unformat (&input, "chroot %s", &chroot_u8)) |
| 499 | { |
| 500 | chroot_path = (char *) chroot_u8; |
| 501 | } |
| 502 | else |
| 503 | { |
| 504 | break; |
| 505 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 508 | unformat_free (&input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 509 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 510 | if (!parsed) |
| 511 | { |
| 512 | fformat (stdout, |
| 513 | "%s: show | show-verbose | client-scan | trace-on <region-name>\n", |
| 514 | argv[0]); |
| 515 | fformat (stdout, " trace-off <region-name>\n"); |
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 | exit (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 518 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 519 | |
| 520 | /* |
| 521 | * fd.io coding-style-patch-verification: ON |
| 522 | * |
| 523 | * Local Variables: |
| 524 | * eval: (c-set-style "gnu") |
| 525 | * End: |
| 526 | */ |