source: test/regfi-threadtest.c @ 185

Last change on this file since 185 was 185, checked in by tim, 15 years ago

reworked logging API again to simplify interface

updated regfi-threadtest to work with more recent commits

File size: 5.0 KB
Line 
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 */
34bool print_verbose = false;
35const char* registry_file = NULL;
36
37/* Other globals */
38REGFI_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
49void traverseValueList(REGFI_ITERATOR* iter)
50{
51  const REGFI_VK_REC* value;
52
53  value = regfi_iterator_first_value(iter);
54  while(value != NULL)
55  {
56    printMsgs(iter->f);
57    regfi_free_record(value);
58    value = regfi_iterator_next_value(iter);
59  }
60}
61
62
63void traverseKeyTree(REGFI_ITERATOR* iter)
64{
65  const REGFI_NK_REC* root = NULL;
66  const REGFI_NK_REC* cur = NULL;
67  const 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_record(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
132int num_iterations;
133void* threadLoop(void* file)
134{
135  REGFI_ITERATOR* iter;
136  int i;
137
138  regfi_log_set_mask(REGFI_LOG_INFO|REGFI_LOG_WARN|REGFI_LOG_ERROR);
139
140  iter = regfi_iterator_new((REGFI_FILE*)f, REGFI_ENCODING_ASCII);
141  if(iter == NULL)
142  {
143    printMsgs(f);
144    bailOut(REGLOOKUP_EXIT_OSERR, "ERROR: Couldn't create registry iterator.\n");
145  }
146
147  for(i=0; i< num_iterations; i++)
148  {
149    traverseKeyTree(iter);
150    regfi_iterator_to_root(iter);
151  }
152
153  regfi_iterator_free(iter);
154
155  return NULL;
156}
157
158
159static void usage(void)
160{
161  fprintf(stderr, "Usage: regfi-threadtest <REGISTRY_FILE>\n");
162  fprintf(stderr, "\n");
163}
164
165
166int main(int argc, char** argv)
167{
168  int fd, tret, i;
169  uint32_t argi, arge, num_threads;
170  pthread_t* threads;
171
172  num_threads = 10;
173  num_iterations = 10;
174
175  /* Process command line arguments */
176  if(argc < 2)
177  {
178    usage();
179    bailOut(REGLOOKUP_EXIT_USAGE, "ERROR: Requires at least one argument.\n");
180  }
181 
182  arge = argc-1;
183  for(argi = 1; argi < arge; argi++)
184  {
185    usage();
186    fprintf(stderr, "ERROR: Unrecognized option: %s\n", argv[argi]);
187    bailOut(REGLOOKUP_EXIT_USAGE, "");
188  }
189  registry_file = argv[argi];
190
191  regfi_log_set_mask(REGFI_LOG_INFO|REGFI_LOG_WARN|REGFI_LOG_ERROR);
192
193  fd = openHive(registry_file);
194  if(fd < 0)
195  {
196    fprintf(stderr, "ERROR: Couldn't open registry file: %s\n", registry_file);
197    bailOut(REGLOOKUP_EXIT_NOINPUT, "");
198  }
199
200  f = regfi_alloc(fd);
201  if(f == NULL)
202  {
203    close(fd);
204    bailOut(REGLOOKUP_EXIT_NOINPUT, "ERROR: Failed to create REGFI_FILE structure.\n");
205  }
206
207  threads = malloc(sizeof(pthread_t)*num_threads);
208  for(i=0; i<num_threads; i++)
209  {
210    tret = pthread_create(threads+i, NULL, threadLoop, (void*) f);
211  }
212 
213  for(i=0; i<num_threads; i++)
214    pthread_join(threads[i], NULL);
215
216  regfi_free(f);
217  close(fd);
218
219  return 0;
220}
Note: See TracBrowser for help on using the repository browser.