[180] | 1 | /* |
---|
| 2 | * A program to stress test regfi under multithreaded use. |
---|
| 3 | * |
---|
| 4 | * Copyright (C) 2005-2010 Timothy D. Morgan |
---|
| 5 | * Copyright (C) 2002 Richard Sharpe, rsharpe@richardsharpe.com |
---|
| 6 | * |
---|
| 7 | * This program is free software; you can redistribute it and/or modify |
---|
| 8 | * it under the terms of the GNU General Public License as published by |
---|
| 9 | * the Free Software Foundation; version 3 of the License. |
---|
| 10 | * |
---|
| 11 | * This program is distributed in the hope that it will be useful, |
---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | * GNU General Public License for more details. |
---|
| 15 | * |
---|
| 16 | * You should have received a copy of the GNU General Public License |
---|
| 17 | * along with this program; if not, write to the Free Software |
---|
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 19 | * |
---|
| 20 | * $Id: $ |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | #include <stdlib.h> |
---|
| 25 | #include <stdio.h> |
---|
| 26 | #include <string.h> |
---|
| 27 | #include <strings.h> |
---|
| 28 | #include <time.h> |
---|
| 29 | #include <pthread.h> |
---|
| 30 | #include "regfi.h" |
---|
| 31 | #include "void_stack.h" |
---|
| 32 | |
---|
| 33 | /* Globals, influenced by command line parameters */ |
---|
| 34 | bool print_verbose = false; |
---|
[183] | 35 | const char* registry_file = NULL; |
---|
[180] | 36 | |
---|
| 37 | /* Other globals */ |
---|
| 38 | REGFI_FILE* f; |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | /* XXX: A hack to share some functions with reglookup-recover.c. |
---|
| 42 | * Should move these into a proper library at some point. |
---|
| 43 | */ |
---|
| 44 | #include "common.c" |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | void traverseValueList(REGFI_ITERATOR* iter) |
---|
| 50 | { |
---|
| 51 | REGFI_VK_REC* value; |
---|
| 52 | |
---|
| 53 | value = regfi_iterator_first_value(iter); |
---|
| 54 | while(value != NULL) |
---|
| 55 | { |
---|
| 56 | printMsgs(iter->f); |
---|
| 57 | regfi_free_value(value); |
---|
| 58 | value = regfi_iterator_next_value(iter); |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | void traverseKeyTree(REGFI_ITERATOR* iter) |
---|
| 64 | { |
---|
| 65 | const REGFI_NK_REC* root = NULL; |
---|
| 66 | const REGFI_NK_REC* cur = NULL; |
---|
| 67 | REGFI_NK_REC* sub = NULL; |
---|
| 68 | const REGFI_SK_REC* sk; |
---|
| 69 | bool print_this = true; |
---|
| 70 | |
---|
| 71 | root = cur = regfi_iterator_cur_key(iter); |
---|
| 72 | sub = regfi_iterator_first_subkey(iter); |
---|
| 73 | printMsgs(iter->f); |
---|
| 74 | |
---|
| 75 | if(root == NULL) |
---|
| 76 | bailOut(REGLOOKUP_EXIT_DATAERR, "ERROR: root cannot be NULL.\n"); |
---|
| 77 | |
---|
| 78 | do |
---|
| 79 | { |
---|
| 80 | if(print_this) |
---|
| 81 | traverseValueList(iter); |
---|
| 82 | |
---|
| 83 | if(sub == NULL) |
---|
| 84 | { |
---|
| 85 | if(cur != root) |
---|
| 86 | { |
---|
| 87 | /* We're done with this sub-tree, going up and hitting other branches. */ |
---|
| 88 | if(!regfi_iterator_up(iter)) |
---|
| 89 | { |
---|
| 90 | printMsgs(iter->f); |
---|
| 91 | bailOut(REGLOOKUP_EXIT_DATAERR, "ERROR: could not traverse iterator upward.\n"); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | cur = regfi_iterator_cur_key(iter); |
---|
| 95 | /* fprintf(stderr, "%s\n", cur->keyname);*/ |
---|
| 96 | printMsgs(iter->f); |
---|
| 97 | sk = regfi_iterator_cur_sk(iter); |
---|
| 98 | printMsgs(iter->f); |
---|
| 99 | if(cur == NULL) |
---|
| 100 | bailOut(REGLOOKUP_EXIT_DATAERR, "ERROR: unexpected NULL for key.\n"); |
---|
| 101 | |
---|
| 102 | sub = regfi_iterator_next_subkey(iter); |
---|
| 103 | } |
---|
| 104 | print_this = false; |
---|
| 105 | } |
---|
| 106 | else |
---|
| 107 | { /* We have unexplored sub-keys. |
---|
| 108 | * Let's move down and print this first sub-tree out. |
---|
| 109 | */ |
---|
| 110 | if(!regfi_iterator_down(iter)) |
---|
| 111 | { |
---|
| 112 | printMsgs(iter->f); |
---|
| 113 | bailOut(REGLOOKUP_EXIT_DATAERR, "ERROR: could not traverse iterator downward.\n"); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | cur = regfi_iterator_cur_key(iter); |
---|
| 117 | printMsgs(iter->f); |
---|
| 118 | regfi_free_key(sub); |
---|
| 119 | |
---|
| 120 | sub = regfi_iterator_first_subkey(iter); |
---|
| 121 | printMsgs(iter->f); |
---|
| 122 | print_this = true; |
---|
| 123 | } |
---|
| 124 | printMsgs(iter->f); |
---|
| 125 | } while(!((cur == root) && (sub == NULL))); |
---|
| 126 | |
---|
| 127 | if(print_verbose) |
---|
| 128 | fprintf(stderr, "INFO: Finished printing key tree.\n"); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | int num_iterations; |
---|
| 133 | void threadLoop(void* file) |
---|
| 134 | { |
---|
| 135 | REGFI_ITERATOR* iter; |
---|
| 136 | int i; |
---|
| 137 | |
---|
| 138 | iter = regfi_iterator_new((REGFI_FILE*)f, REGFI_ENCODING_ASCII); |
---|
| 139 | if(iter == NULL) |
---|
| 140 | { |
---|
| 141 | printMsgs(f); |
---|
| 142 | bailOut(REGLOOKUP_EXIT_OSERR, "ERROR: Couldn't create registry iterator.\n"); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | for(i=0; i< num_iterations; i++) |
---|
| 146 | { |
---|
| 147 | traverseKeyTree(iter); |
---|
| 148 | regfi_iterator_to_root(iter); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | regfi_iterator_free(iter); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | |
---|
| 155 | static void usage(void) |
---|
| 156 | { |
---|
| 157 | fprintf(stderr, "Usage: regfi-threadtest <REGISTRY_FILE>\n"); |
---|
| 158 | fprintf(stderr, "\n"); |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | int main(int argc, char** argv) |
---|
| 163 | { |
---|
| 164 | int fd, tret, i; |
---|
| 165 | uint32_t argi, arge, num_threads; |
---|
| 166 | pthread_t* threads; |
---|
| 167 | |
---|
| 168 | num_threads = 10; |
---|
| 169 | num_iterations = 10; |
---|
| 170 | |
---|
| 171 | /* Process command line arguments */ |
---|
| 172 | if(argc < 2) |
---|
| 173 | { |
---|
| 174 | usage(); |
---|
| 175 | bailOut(REGLOOKUP_EXIT_USAGE, "ERROR: Requires at least one argument.\n"); |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | arge = argc-1; |
---|
| 179 | for(argi = 1; argi < arge; argi++) |
---|
| 180 | { |
---|
| 181 | usage(); |
---|
| 182 | fprintf(stderr, "ERROR: Unrecognized option: %s\n", argv[argi]); |
---|
| 183 | bailOut(REGLOOKUP_EXIT_USAGE, ""); |
---|
| 184 | } |
---|
[183] | 185 | registry_file = argv[argi]; |
---|
[180] | 186 | |
---|
[183] | 187 | regfi_log_start(REGFI_LOG_INFO|REGFI_LOG_WARN|REGFI_LOG_ERROR); |
---|
[180] | 188 | |
---|
| 189 | fd = openHive(registry_file); |
---|
| 190 | if(fd < 0) |
---|
| 191 | { |
---|
| 192 | fprintf(stderr, "ERROR: Couldn't open registry file: %s\n", registry_file); |
---|
| 193 | bailOut(REGLOOKUP_EXIT_NOINPUT, ""); |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | f = regfi_alloc(fd); |
---|
| 197 | if(f == NULL) |
---|
| 198 | { |
---|
| 199 | close(fd); |
---|
| 200 | bailOut(REGLOOKUP_EXIT_NOINPUT, "ERROR: Failed to create REGFI_FILE structure.\n"); |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | threads = malloc(sizeof(pthread_t)*num_threads); |
---|
| 204 | for(i=0; i<num_threads; i++) |
---|
| 205 | { |
---|
| 206 | tret = pthread_create(threads+i, NULL, threadLoop, (void*) f); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | for(i=0; i<num_threads; i++) |
---|
| 210 | pthread_join(threads[i], NULL); |
---|
| 211 | |
---|
| 212 | regfi_free(f); |
---|
[183] | 213 | regfi_log_stop(); |
---|
[180] | 214 | close(fd); |
---|
| 215 | |
---|
| 216 | return 0; |
---|
| 217 | } |
---|