Changeset 185


Ignore:
Timestamp:
03/22/10 22:22:07 (14 years ago)
Author:
tim
Message:

reworked logging API again to simplify interface

updated regfi-threadtest to work with more recent commits

Files:
6 edited

Legend:

Unmodified
Added
Removed
  • test/regfi-threadtest.c

    r183 r185  
    4949void traverseValueList(REGFI_ITERATOR* iter)
    5050{
    51   REGFI_VK_REC* value;
     51  const REGFI_VK_REC* value;
    5252
    5353  value = regfi_iterator_first_value(iter);
     
    5555  {
    5656    printMsgs(iter->f);
    57     regfi_free_value(value);
     57    regfi_free_record(value);
    5858    value = regfi_iterator_next_value(iter);
    5959  }
     
    6565  const REGFI_NK_REC* root = NULL;
    6666  const REGFI_NK_REC* cur = NULL;
    67   REGFI_NK_REC* sub = NULL;
     67  const REGFI_NK_REC* sub = NULL;
    6868  const REGFI_SK_REC* sk;
    6969  bool print_this = true;
     
    116116      cur = regfi_iterator_cur_key(iter);
    117117      printMsgs(iter->f);
    118       regfi_free_key(sub);
     118      regfi_free_record(sub);
    119119
    120120      sub = regfi_iterator_first_subkey(iter);
     
    131131
    132132int num_iterations;
    133 void threadLoop(void* file)
     133void* threadLoop(void* file)
    134134{
    135135  REGFI_ITERATOR* iter;
    136136  int i;
    137137
     138  regfi_log_set_mask(REGFI_LOG_INFO|REGFI_LOG_WARN|REGFI_LOG_ERROR);
     139
    138140  iter = regfi_iterator_new((REGFI_FILE*)f, REGFI_ENCODING_ASCII);
    139141  if(iter == NULL)
     
    150152
    151153  regfi_iterator_free(iter);
     154
     155  return NULL;
    152156}
    153157
     
    185189  registry_file = argv[argi];
    186190
    187   regfi_log_start(REGFI_LOG_INFO|REGFI_LOG_WARN|REGFI_LOG_ERROR);
     191  regfi_log_set_mask(REGFI_LOG_INFO|REGFI_LOG_WARN|REGFI_LOG_ERROR);
    188192
    189193  fd = openHive(registry_file);
     
    211215
    212216  regfi_free(f);
    213   regfi_log_stop();
    214217  close(fd);
    215218
  • trunk/include/regfi.h

    r184 r185  
    4848 *
    4949 * Most users will find that a combination of the Base Layer and the Iterator Layer
    50  * will be sufficient for accessing registry hive files.  Those who are wiling
     50 * will be sufficient for accessing registry hive files.  Those who are willing
    5151 * to dive deep into registry data structures, for instance to recover deleted
    5252 * data structures or to research Windows registry behavior in detail, will
     
    7979
    8080
     81
     82
    8183/******************************************************************************/
    8284/* Constants for use while interacting with the library                       */
     
    8789#define REGFI_LOG_WARN  0x0004
    8890#define REGFI_LOG_ERROR 0x0010
     91#define REGFI_DEFAULT_LOG_MASK REGFI_LOG_ERROR|REGFI_LOG_WARN
    8992
    9093typedef uint8_t REGFI_ENCODING;
     
    146149
    147150/* Global thread-local storage key */
    148 pthread_key_t REGFI_LOG_KEY;
     151pthread_key_t regfi_log_key;
    149152
    150153/* Header sizes and magic number lengths for various records */
     
    877880
    878881
    879 /** Enables regfi library logging for the current thread.
    880  *
    881  * XXX: finish documenting
    882  *
    883  * @ingroup regfiBase
    884  */
    885 void regfi_log_start(uint16_t mask);
    886 
    887 
    888882/** Get errors, warnings, and/or verbose information relating to processing of
    889883 *  the given registry file.
     
    896890
    897891
    898 /** Set the verbosity level of errors and warnings generated by the library
    899  *  (as accessible via regfi_get_messages).
    900  *
    901  * This may be called at any time and will take effect immediately.
    902  *
    903  * @param mask   an integer representing the types of messages desired.
     892/** Set the verbosity level of messages generated by the library for the
     893 *  current thread.
     894 *
     895 * @param mask   An integer representing the types of messages desired.
    904896 *               Acceptable values are created through bitwise ORs of
    905  *               REGFI_MSG_* values.  For instance, if only errors and
     897 *               REGFI_LOG_* values.  For instance, if only errors and
    906898 *               informational messages were desired (but not warnings),
    907  *               then one would specify: REGFI_MSG_ERROR|REGFI_MSG_INFO
    908  *               New REGFI_FILE structures are created with:
    909  *                REGFI_MSG_ERROR|REGFI_MSG_WARN
    910  *               Note that error and warning messages will continue to
    911  *               accumulate in memory if they are not fetched using
    912  *               regfi_get_messages and then freed by the caller.
    913  *               To disable error messages entirely, supply 0, which
    914  *               will prevent message accumulation. 
     899 *               then one would specify: REGFI_LOG_ERROR|REGFI_LOG_INFO
     900 *               By default the message mask is: REGFI_LOG_ERROR|REGFI_LOG_WARN.
     901 *
     902 * @return       true on success and false on failure.  Failure occurs if
     903 *               underlying pthread functions fail.  errno is set in this case.
     904 *
     905 * Message masks are set in a thread-specific way.  If one were to set a message
     906 * mask in one thread and then spawn a new thread, then the new thread will have
     907 * it's message mask reset to the default.  This function may be called at any
     908 * time and will take effect immediately for the current thread.
     909 *
     910 * @note When a non-zero message mask is set, messages will
     911 *       accumulate in memory without limit if they are not fetched using
     912 *       @ref regfi_get_log_str and subsequently freed by the caller.  It is
     913 *       recommended that messsages be fetched after each regfi API call in
     914 *       order to provide the most context.
    915915 *
    916916 * @ingroup regfiBase
    917917 */
    918 void regfi_log_set_mask(uint16_t mask);
    919 
    920 
    921 /** Disables regfi library logging for the current thread.
    922  *
    923  * XXX: finish documenting
    924  *
    925  * @ingroup regfiBase
    926  */
    927 void regfi_log_stop();
     918bool regfi_log_set_mask(uint16_t mask);
    928919
    929920
     
    10771068 *         subkey, or NULL on failure.  Failure may be due to a lack of any
    10781069 *         subkeys or other errors.  Newly allocated keys must be freed with
    1079  *         regfi_free_record.
     1070 *         @ref regfi_free_record.
    10801071 *
    10811072 * @ingroup regfiIteratorLayer
     
    10901081 * @return A newly allocated key structure for the currently referenced subkey,
    10911082 *         or NULL on failure.  Newly allocated keys must be freed with
    1092  *         regfi_free_record.
     1083 *         @ref regfi_free_record.
    10931084 *
    10941085 * @ingroup regfiIteratorLayer
     
    11031094 *
    11041095 * @return A newly allocated key structure for the next subkey or NULL on
    1105  *         failure.  Newly allocated keys must be freed with regfi_free_record.
     1096 *         failure.  Newly allocated keys must be freed with
     1097 *         @ref regfi_free_record.
    11061098 *
    11071099 * @ingroup regfiIteratorLayer
     
    11321124 *          value, or NULL on failure.  Failure may be due to a lack of any
    11331125 *          values or other errors.  Newly allocated keys must be freed with
    1134  *          regfi_free_record.
     1126 *          @ref regfi_free_record.
    11351127 *
    11361128 * @ingroup regfiIteratorLayer
     
    11451137 * @return A newly allocated value structure for the currently referenced value,
    11461138 *         or NULL on failure.  Newly allocated values must be freed with
    1147  *         regfi_free_record.
     1139 *         @ref regfi_free_record.
    11481140 *
    11491141 * @ingroup regfiIteratorLayer
     
    11581150 *
    11591151 * @return  A newly allocated key structure for the next value or NULL on
    1160  *          failure.  Newly allocated keys must be freed with regfi_free_record.
     1152 *          failure.  Newly allocated keys must be freed with
     1153 *          @ref regfi_free_record.
    11611154 *
    11621155 * @ingroup regfiIteratorLayer
     
    11851178 *
    11861179 * @return Returns a newly allocated classname structure, or NULL on failure.
    1187  *         Classname structures must be freed with regfi_free_record.
     1180 *         Classname structures must be freed with @ref regfi_free_record.
    11881181 *
    11891182 * @ingroup regfiIteratorLayer
     
    11991192 *
    12001193 * @return Returns a newly allocated data structure, or NULL on failure.
    1201  *         Data structures must be freed with regfi_free_record.
     1194 *         Data structures must be freed with @ref regfi_free_record.
    12021195 *
    12031196 * @ingroup regfiIteratorLayer
  • trunk/lib/regfi.c

    r184 r185  
    4545
    4646
     47/* Ensures regfi_init runs only once */
     48static pthread_once_t regfi_init_once = PTHREAD_ONCE_INIT;
     49
     50
    4751
    4852/******************************************************************************
    4953 ******************************************************************************/
    50 void regfi_log_start(uint16_t msg_mask)
    51 {
     54void regfi_log_free(void* ptr)
     55{
     56  REGFI_LOG* log_info = (REGFI_LOG*)ptr;
     57 
     58  if(log_info->messages != NULL)
     59    free(log_info->messages);
     60
     61  talloc_free(log_info);
     62}
     63
     64
     65/******************************************************************************
     66 ******************************************************************************/
     67void regfi_init()
     68{
     69  int err;
     70  if((err = pthread_key_create(&regfi_log_key, regfi_log_free)) != 0)
     71    fprintf(stderr, "ERROR: key_create: %s\n", strerror(err));
     72  errno = err;
     73}
     74
     75
     76/******************************************************************************
     77 ******************************************************************************/
     78REGFI_LOG* regfi_log_new()
     79{
     80  int err;
    5281  REGFI_LOG* log_info = talloc(NULL, REGFI_LOG);
    5382  if(log_info == NULL)
    54     return;
    55 
    56   log_info->msg_mask = msg_mask;
     83    return NULL;
     84
     85  log_info->msg_mask = REGFI_DEFAULT_LOG_MASK;
    5786  log_info->messages = NULL;
    5887
    59   /* XXX: Should we bother with a destructor here? */
    60   if(pthread_key_create(&REGFI_LOG_KEY, NULL) != 0)
    61   {
    62     fprintf(stderr, "ERROR: key_create\n");
    63     goto fail;
    64   }
    65 
    66   if(pthread_setspecific(REGFI_LOG_KEY, log_info) != 0)
    67   {
    68     fprintf(stderr, "ERROR: setspecific\n");
    69     goto fail;
    70   }
    71 
    72   return;
     88  pthread_once(&regfi_init_once, regfi_init);
     89
     90  if((err = pthread_setspecific(regfi_log_key, log_info)) != 0)
     91  {
     92    fprintf(stderr, "ERROR: setspecific: %s\n", strerror(err));
     93    goto fail;
     94  }
     95
     96  return log_info;
    7397
    7498 fail:
    7599  talloc_free(log_info);
     100  errno = err;
     101  return NULL;
    76102}
    77103
     
    93119  va_list args;
    94120
    95   log_info = (REGFI_LOG*)pthread_getspecific(REGFI_LOG_KEY);
    96   if(log_info == NULL || (log_info->msg_mask & msg_type) == 0)
     121  log_info = (REGFI_LOG*)pthread_getspecific(regfi_log_key);
     122  if(log_info == NULL && (log_info = regfi_log_new()) == NULL)
     123    return;
     124
     125  if((log_info->msg_mask & msg_type) == 0)
    97126    return;
    98127
     
    138167{
    139168  char* ret_val;
    140   REGFI_LOG* log_info = (REGFI_LOG*)pthread_getspecific(REGFI_LOG_KEY);
    141   if(log_info == NULL)
    142     return NULL;
    143 
     169  REGFI_LOG* log_info = (REGFI_LOG*)pthread_getspecific(regfi_log_key);
     170  if(log_info == NULL && (log_info = regfi_log_new()) == NULL)
     171    return NULL;
     172 
    144173  ret_val = log_info->messages;
    145174  log_info->messages = NULL;
     
    151180/******************************************************************************
    152181 ******************************************************************************/
    153 void regfi_log_set_mask(uint16_t msg_mask)
    154 {
    155   REGFI_LOG* log_info = (REGFI_LOG*)pthread_getspecific(REGFI_LOG_KEY);
    156   if(log_info == NULL)
    157     return;
     182bool regfi_log_set_mask(uint16_t msg_mask)
     183{
     184  REGFI_LOG* log_info = (REGFI_LOG*)pthread_getspecific(regfi_log_key);
     185  if(log_info == NULL && (log_info = regfi_log_new()) == NULL)
     186  {
     187      return false;
     188  }
    158189
    159190  log_info->msg_mask = msg_mask;
    160 }
    161 
    162 
    163 /******************************************************************************
    164  ******************************************************************************/
    165 void regfi_log_stop()
    166 {
    167   REGFI_LOG* log_info = (REGFI_LOG*)pthread_getspecific(REGFI_LOG_KEY);
    168   if(log_info == NULL)
    169     return;
    170  
    171   if(log_info->messages != NULL)
    172     free(log_info->messages);
    173 
    174   pthread_key_delete(REGFI_LOG_KEY);
    175   talloc_free(log_info);
     191  return true;
    176192}
    177193
  • trunk/src/common.c

    r184 r185  
    4949{
    5050  fprintf(stderr, message);
    51   regfi_log_stop();
    5251  exit(code);
    5352}
  • trunk/src/reglookup-recover.c

    r184 r185  
    828828
    829829  if(print_verbose)
    830     regfi_log_start(REGFI_LOG_ERROR|REGFI_LOG_WARN|REGFI_LOG_INFO);
     830    regfi_log_set_mask(REGFI_LOG_ERROR|REGFI_LOG_WARN|REGFI_LOG_INFO);
    831831  else
    832     regfi_log_start(REGFI_LOG_ERROR);
     832    regfi_log_set_mask(REGFI_LOG_ERROR);
    833833
    834834  f = regfi_alloc(fd);
     
    994994
    995995  regfi_free(f);
    996   regfi_log_stop();
    997996  close(fd);
    998997
  • trunk/src/reglookup.c

    r184 r185  
    624624
    625625  if(print_verbose)
    626     regfi_log_start(REGFI_LOG_INFO|REGFI_LOG_WARN|REGFI_LOG_ERROR);
    627   else
    628     regfi_log_start(REGFI_LOG_ERROR|REGFI_LOG_WARN);
     626    regfi_log_set_mask(REGFI_LOG_INFO|REGFI_LOG_WARN|REGFI_LOG_ERROR);
    629627
    630628  fd = openHive(registry_file);
     
    685683  regfi_iterator_free(iter);
    686684  regfi_free(f);
    687   regfi_log_stop();
    688685  close(fd);
    689686
Note: See TracChangeset for help on using the changeset viewer.