r19853: Update a link
[ira/wip.git] / source3 / registry / reg_perfcount.c
index fe8b355b958f99bd57ae0fb58a354fd93099ccec..26d640c87cab71ddde22c377cae1e744b78fd644 100644 (file)
@@ -1,3 +1,25 @@
+/* 
+ *  Unix SMB/CIFS implementation.
+ *  Virtual Windows Registry Layer
+ *
+ *  Copyright (C) Marcin Krzysztof Porwit    2005,
+ *  Copyright (C) Gerald (Jerry) Carter      2005.
+ *  
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *  
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *  
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
 #include "includes.h"
 
 #undef DBGC_CLASS
 
 #define PERFCOUNT_MAX_LEN 256
 
+#define PERFCOUNTDIR   "perfmon"
+#define NAMES_DB       "names.tdb"
+#define DATA_DB                "data.tdb"
+
+/*********************************************************************
+*********************************************************************/
+
+static char* counters_directory( const char *dbname )
+{
+       static pstring fname;
+       fstring path;
+       
+       if ( !dbname )
+               return NULL;
+       
+       fstr_sprintf( path, "%s/%s", PERFCOUNTDIR, dbname );
+       
+       pstrcpy( fname, lock_path( path ) );
+       
+       return fname;
+}
+
+/*********************************************************************
+*********************************************************************/
+
+void perfcount_init_keys( void )
+{
+       char *p = lock_path(PERFCOUNTDIR);
+
+       /* no registry keys; just create the perfmon directory */
+       
+       if ( !directory_exist( p, NULL ) )
+               mkdir( p, 0755 );
+       
+       return;
+}
+
+/*********************************************************************
+*********************************************************************/
+
 uint32 reg_perfcount_get_base_index(void)
 {
-       pstring fname;
+       const char *fname = counters_directory( NAMES_DB );
        TDB_CONTEXT *names;
        TDB_DATA kbuf, dbuf;
        char key[] = "1";
        uint32 retval = 0;
        char buf[PERFCOUNT_MAX_LEN];
-       const char *counter_dir = lp_counters_dir();
-       
-
-       if ( !*counter_dir ) 
-               return 0;
-
-       pstr_sprintf( fname, "%s/names.tdb", counter_dir );
 
        names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
 
@@ -62,6 +117,9 @@ uint32 reg_perfcount_get_base_index(void)
        return 0;
 }
 
+/*********************************************************************
+*********************************************************************/
+
 uint32 reg_perfcount_get_last_counter(uint32 base_index)
 {
        uint32 retval;
@@ -74,6 +132,9 @@ uint32 reg_perfcount_get_last_counter(uint32 base_index)
        return retval;
 }
 
+/*********************************************************************
+*********************************************************************/
+
 uint32 reg_perfcount_get_last_help(uint32 last_counter)
 {
        uint32 retval;
@@ -86,6 +147,10 @@ uint32 reg_perfcount_get_last_help(uint32 last_counter)
        return retval;
 }
 
+
+/*********************************************************************
+*********************************************************************/
+
 static uint32 _reg_perfcount_multi_sz_from_tdb(TDB_CONTEXT *tdb, 
                                               int keyval,
                                               char **retbuf,
@@ -93,7 +158,7 @@ static uint32 _reg_perfcount_multi_sz_from_tdb(TDB_CONTEXT *tdb,
 {
        TDB_DATA kbuf, dbuf;
        char temp[256];
-       char *buf1 = *retbuf, *buf2 = NULL;
+       char *buf1 = *retbuf;
        uint32 working_size = 0;
        UNISTR2 name_index, name;
 
@@ -107,32 +172,26 @@ static uint32 _reg_perfcount_multi_sz_from_tdb(TDB_CONTEXT *tdb,
                /* If a key isn't there, just bypass it -- this really shouldn't 
                   happen unless someone's mucking around with the tdb */
                DEBUG(3, ("_reg_perfcount_multi_sz_from_tdb: failed to find key [%s] in [%s].\n",
-                         temp, tdb->name));
+                         temp, tdb_name(tdb)));
                return buffer_size;
        }
        /* First encode the name_index */
        working_size = (kbuf.dsize + 1)*sizeof(uint16);
-       buf2 = SMB_REALLOC(buf1, buffer_size + working_size);
-       if(!buf2)
-       {
-               SAFE_FREE(buf1);
+       buf1 = (char *)SMB_REALLOC(buf1, buffer_size + working_size);
+       if(!buf1) {
                buffer_size = 0;
                return buffer_size;
        }
-       buf1 = buf2;
        init_unistr2(&name_index, kbuf.dptr, UNI_STR_TERMINATE);
        memcpy(buf1+buffer_size, (char *)name_index.buffer, working_size);
        buffer_size += working_size;
        /* Now encode the actual name */
        working_size = (dbuf.dsize + 1)*sizeof(uint16);
-       buf2 = SMB_REALLOC(buf1, buffer_size + working_size);
-       if(!buf2)
-       {
-               SAFE_FREE(buf1);
+       buf1 = (char *)SMB_REALLOC(buf1, buffer_size + working_size);
+       if(!buf1) {
                buffer_size = 0;
                return buffer_size;
        }
-       buf1 = buf2;
        memset(temp, 0, sizeof(temp));
        memcpy(temp, dbuf.dptr, dbuf.dsize);
        SAFE_FREE(dbuf.dptr);
@@ -145,20 +204,20 @@ static uint32 _reg_perfcount_multi_sz_from_tdb(TDB_CONTEXT *tdb,
        return buffer_size;
 }
 
+/*********************************************************************
+*********************************************************************/
+
 uint32 reg_perfcount_get_counter_help(uint32 base_index, char **retbuf)
 {
-       char *buf1 = NULL, *buf2 = NULL;
+       char *buf1 = NULL;
        uint32 buffer_size = 0;
        TDB_CONTEXT *names;
-       pstring fname;
+       const char *fname = counters_directory( NAMES_DB );
        int i;
 
        if(base_index == 0)
                return 0;
 
-       pstrcpy(fname, lp_counters_dir());
-       pstrcat(fname, "/names.tdb");
-
        names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
 
        if(names == NULL)
@@ -175,15 +234,10 @@ uint32 reg_perfcount_get_counter_help(uint32 base_index, char **retbuf)
 
        /* Now terminate the MULTI_SZ with a double unicode NULL */
        buf1 = *retbuf;
-       buf2 = SMB_REALLOC(buf1, buffer_size + 2);
-       if(!buf2)
-       {
-               SAFE_FREE(buf1);
+       buf1 = (char *)SMB_REALLOC(buf1, buffer_size + 2);
+       if(!buf1) {
                buffer_size = 0;
-       }
-       else
-       {
-               buf1 = buf2;
+       } else {
                buf1[buffer_size++] = '\0';
                buf1[buffer_size++] = '\0';
        }
@@ -193,20 +247,20 @@ uint32 reg_perfcount_get_counter_help(uint32 base_index, char **retbuf)
        return buffer_size;
 }
 
+/*********************************************************************
+*********************************************************************/
+
 uint32 reg_perfcount_get_counter_names(uint32 base_index, char **retbuf)
 {
-       char *buf1 = NULL, *buf2 = NULL;
+       char *buf1 = NULL;
        uint32 buffer_size = 0;
        TDB_CONTEXT *names;
-       pstring fname;
+       const char *fname = counters_directory( NAMES_DB );
        int i;
 
        if(base_index == 0)
                return 0;
 
-       pstrcpy(fname, lp_counters_dir());
-       pstrcat(fname, "/names.tdb");
-
        names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
 
        if(names == NULL)
@@ -225,15 +279,10 @@ uint32 reg_perfcount_get_counter_names(uint32 base_index, char **retbuf)
 
        /* Now terminate the MULTI_SZ with a double unicode NULL */
        buf1 = *retbuf;
-       buf2 = SMB_REALLOC(buf1, buffer_size + 2);
-       if(!buf2)
-       {
-               SAFE_FREE(buf1);
+       buf1 = (char *)SMB_REALLOC(buf1, buffer_size + 2);
+       if(!buf1) {
                buffer_size = 0;
-       }
-       else
-       {
-               buf1 = buf2;
+       } else {
                buf1[buffer_size++] = '\0';
                buf1[buffer_size++] = '\0';
        }
@@ -243,6 +292,9 @@ uint32 reg_perfcount_get_counter_names(uint32 base_index, char **retbuf)
        return buffer_size;
 }
 
+/*********************************************************************
+*********************************************************************/
+
 static void _reg_perfcount_make_key(TDB_DATA *key,
                                    char *buf,
                                    int buflen,
@@ -261,6 +313,9 @@ static void _reg_perfcount_make_key(TDB_DATA *key,
        return;
 }
 
+/*********************************************************************
+*********************************************************************/
+
 static BOOL _reg_perfcount_isparent(TDB_DATA data)
 {
        if(data.dsize > 0)
@@ -273,6 +328,9 @@ static BOOL _reg_perfcount_isparent(TDB_DATA data)
        return False;
 }
 
+/*********************************************************************
+*********************************************************************/
+
 static BOOL _reg_perfcount_ischild(TDB_DATA data)
 {
        if(data.dsize > 0)
@@ -285,6 +343,9 @@ static BOOL _reg_perfcount_ischild(TDB_DATA data)
        return False;
 }
 
+/*********************************************************************
+*********************************************************************/
+
 static uint32 _reg_perfcount_get_numinst(int objInd, TDB_CONTEXT *names)
 {
        TDB_DATA key, data;
@@ -301,6 +362,9 @@ static uint32 _reg_perfcount_get_numinst(int objInd, TDB_CONTEXT *names)
        return (uint32)atoi(buf);
 }
 
+/*********************************************************************
+*********************************************************************/
+
 static BOOL _reg_perfcount_add_object(PERF_DATA_BLOCK *block,
                                      prs_struct *ps,
                                      int num,
@@ -308,7 +372,7 @@ static BOOL _reg_perfcount_add_object(PERF_DATA_BLOCK *block,
                                      TDB_CONTEXT *names)
 {
        int i;
-       BOOL success = False;
+       BOOL success = True;
        PERF_OBJECT_TYPE *obj;
 
        block->objects = (PERF_OBJECT_TYPE *)TALLOC_REALLOC_ARRAY(ps->mem_ctx,
@@ -333,23 +397,21 @@ static BOOL _reg_perfcount_add_object(PERF_DATA_BLOCK *block,
        block->objects[block->NumObjectTypes].DetailLevel = PERF_DETAIL_NOVICE;
        block->NumObjectTypes+=1;
 
-       for(i = 0; i < (int)obj->NumInstances; i++)
-       {
+       for(i = 0; i < (int)obj->NumInstances; i++) {
                success = _reg_perfcount_add_instance(obj, ps, i, names);
        }
 
-       return True;
+       return success;
 }
 
+/*********************************************************************
+*********************************************************************/
+
 BOOL _reg_perfcount_get_counter_data(TDB_DATA key, TDB_DATA *data)
 {
        TDB_CONTEXT *counters;
-       pstring fname;
+       const char *fname = counters_directory( DATA_DB );
     
-       pstrcpy(fname, lp_counters_dir());
-       pstrcat(fname, "/data.tdb");
-
        counters = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
 
        if(counters == NULL)
@@ -365,6 +427,9 @@ BOOL _reg_perfcount_get_counter_data(TDB_DATA key, TDB_DATA *data)
        return True;
 }
 
+/*********************************************************************
+*********************************************************************/
+
 static uint32 _reg_perfcount_get_size_field(uint32 CounterType)
 {
        uint32 retval;
@@ -379,7 +444,10 @@ static uint32 _reg_perfcount_get_size_field(uint32 CounterType)
        return retval;
 }
 
-static uint32 _reg_perfcount_compute_scale(long long int data)
+/*********************************************************************
+*********************************************************************/
+
+static uint32 _reg_perfcount_compute_scale(SMB_BIG_INT data)
 {
        int scale = 0;
        if(data == 0)
@@ -398,6 +466,9 @@ static uint32 _reg_perfcount_compute_scale(long long int data)
        return (uint32)scale;
 }
 
+/*********************************************************************
+*********************************************************************/
+
 static BOOL _reg_perfcount_get_counter_info(PERF_DATA_BLOCK *block,
                                            prs_struct *ps,
                                            int CounterIndex,
@@ -408,7 +479,7 @@ static BOOL _reg_perfcount_get_counter_info(PERF_DATA_BLOCK *block,
        char buf[PERFCOUNT_MAX_LEN];
        size_t dsize, padding;
        long int data32, dbuf[2];
-       long long int data64;
+       SMB_BIG_INT data64;
        uint32 counter_size;
 
        obj->counters[obj->NumCounters].DefaultScale = 0;
@@ -427,7 +498,7 @@ static BOOL _reg_perfcount_get_counter_info(PERF_DATA_BLOCK *block,
        obj->counters[obj->NumCounters].CounterType = atoi(buf);
        DEBUG(10, ("_reg_perfcount_get_counter_info: Got type [%d] for counter [%d].\n",
                   obj->counters[obj->NumCounters].CounterType, CounterIndex));
-       free(data.dptr);
+       SAFE_FREE(data.dptr);
 
        /* Fetch the actual data */
        _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, CounterIndex, "");
@@ -447,7 +518,7 @@ static BOOL _reg_perfcount_get_counter_info(PERF_DATA_BLOCK *block,
                memcpy(buf, data.dptr, data.dsize);
                data32 = strtol(buf, NULL, 0);
                if((obj->counters[obj->NumCounters].CounterType & 0x00000F00) == PERF_TYPE_NUMBER)
-                       obj->counters[obj->NumCounters].DefaultScale = _reg_perfcount_compute_scale((long long int)data32);
+                       obj->counters[obj->NumCounters].DefaultScale = _reg_perfcount_compute_scale((SMB_BIG_INT)data32);
                else
                        obj->counters[obj->NumCounters].DefaultScale = 0;
                dbuf[0] = data32;
@@ -458,7 +529,7 @@ static BOOL _reg_perfcount_get_counter_info(PERF_DATA_BLOCK *block,
                dsize = sizeof(data64);
                memset(buf, 0, PERFCOUNT_MAX_LEN);
                memcpy(buf, data.dptr, data.dsize);
-               data64 = strtoll(buf, NULL, 0);
+               data64 = atof(buf);
                if((obj->counters[obj->NumCounters].CounterType & 0x00000F00) == PERF_TYPE_NUMBER)
                        obj->counters[obj->NumCounters].DefaultScale = _reg_perfcount_compute_scale(data64);
                else
@@ -472,7 +543,7 @@ static BOOL _reg_perfcount_get_counter_info(PERF_DATA_BLOCK *block,
                memset(buf, 0, PERFCOUNT_MAX_LEN);
                memcpy(buf, data.dptr, data.dsize);
        }
-       free(data.dptr);
+       SAFE_FREE(data.dptr);
 
        obj->counter_data.ByteLength += dsize + padding;
        obj->counter_data.data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
@@ -504,6 +575,9 @@ static BOOL _reg_perfcount_get_counter_info(PERF_DATA_BLOCK *block,
        return True;
 }
 
+/*********************************************************************
+*********************************************************************/
+
 PERF_OBJECT_TYPE *_reg_perfcount_find_obj(PERF_DATA_BLOCK *block, int objind)
 {
        int i;
@@ -521,6 +595,9 @@ PERF_OBJECT_TYPE *_reg_perfcount_find_obj(PERF_DATA_BLOCK *block, int objind)
        return obj;
 }
 
+/*********************************************************************
+*********************************************************************/
+
 static BOOL _reg_perfcount_add_counter(PERF_DATA_BLOCK *block,
                                       prs_struct *ps,
                                       int num,
@@ -530,7 +607,7 @@ static BOOL _reg_perfcount_add_counter(PERF_DATA_BLOCK *block,
        char *begin, *end, *start, *stop;
        int parent;
        PERF_OBJECT_TYPE *obj;
-       BOOL success = False;
+       BOOL success = True;
        char buf[PERFCOUNT_MAX_LEN];
     
        obj = NULL;
@@ -542,8 +619,7 @@ static BOOL _reg_perfcount_add_counter(PERF_DATA_BLOCK *block,
                return False;
        start = begin+1;
 
-       while(start < end)
-       {
+       while(start < end) {
                stop = index(start, ',');
                if(stop == NULL)
                        stop = end;
@@ -551,8 +627,7 @@ static BOOL _reg_perfcount_add_counter(PERF_DATA_BLOCK *block,
                parent = atoi(start);
 
                obj = _reg_perfcount_find_obj(block, parent);
-               if(obj == NULL)
-               {
+               if(obj == NULL) {
                        /* At this point we require that the parent object exist.
                           This can probably be handled better at some later time */
                        DEBUG(3, ("_reg_perfcount_add_counter: Could not find parent object [%d] for counter [%d].\n",
@@ -579,9 +654,12 @@ static BOOL _reg_perfcount_add_counter(PERF_DATA_BLOCK *block,
           that the required instances are not there yet, so change NumInstances from
           PERF_NO_INSTANCES to 0 */
 
-       return True;
+       return success;
 }
 
+/*********************************************************************
+*********************************************************************/
+
 BOOL _reg_perfcount_get_instance_info(PERF_INSTANCE_DEFINITION *inst,
                                      prs_struct *ps,
                                      int instId,
@@ -613,7 +691,7 @@ BOOL _reg_perfcount_get_instance_info(PERF_INSTANCE_DEFINITION *inst,
                return False;
        memset(inst->counter_data.data, 0, data.dsize);
        memcpy(inst->counter_data.data, data.dptr, data.dsize);
-       free(data.dptr);
+       SAFE_FREE(data.dptr);
 
        /* Fetch instance name */
        memset(temp, 0, PERFCOUNT_MAX_LEN);
@@ -637,8 +715,12 @@ BOOL _reg_perfcount_get_instance_info(PERF_INSTANCE_DEFINITION *inst,
                                                  inst->data,
                                                  uint8,
                                                  inst->NameLength);
+               if (inst->data == NULL) {
+                       SAFE_FREE(data.dptr);
+                       return False;
+               }
                memcpy(inst->data, name, inst->NameLength);
-               free(data.dptr);
+               SAFE_FREE(data.dptr);
        }
 
        inst->ParentObjectTitleIndex = 0;
@@ -662,18 +744,17 @@ BOOL _reg_perfcount_get_instance_info(PERF_INSTANCE_DEFINITION *inst,
        return True;
 }
 
+/*********************************************************************
+*********************************************************************/
+
 BOOL _reg_perfcount_add_instance(PERF_OBJECT_TYPE *obj,
                                 prs_struct *ps,
                                 int instInd,
                                 TDB_CONTEXT *names)
 {
-       BOOL success;
        PERF_INSTANCE_DEFINITION *inst;
 
-       success = False;
-
-       if(obj->instances == NULL)
-       {
+       if(obj->instances == NULL) {
                obj->instances = TALLOC_REALLOC_ARRAY(ps->mem_ctx, 
                                                      obj->instances,
                                                      PERF_INSTANCE_DEFINITION,
@@ -684,11 +765,12 @@ BOOL _reg_perfcount_add_instance(PERF_OBJECT_TYPE *obj,
     
        memset(&(obj->instances[instInd]), 0, sizeof(PERF_INSTANCE_DEFINITION));
        inst = &(obj->instances[instInd]);
-       success = _reg_perfcount_get_instance_info(inst, ps, instInd, obj, names);
-    
-       return True;
+       return _reg_perfcount_get_instance_info(inst, ps, instInd, obj, names);
 }
 
+/*********************************************************************
+*********************************************************************/
+
 static int _reg_perfcount_assemble_global(PERF_DATA_BLOCK *block,
                                          prs_struct *ps,
                                          int base_index,
@@ -720,7 +802,7 @@ static int _reg_perfcount_assemble_global(PERF_DATA_BLOCK *block,
                                DEBUG(3, ("_reg_perfcount_assemble_global: Failed to add new relationship for counter [%d].\n", j));
                                retval = -1;
                        }
-                       free(data.dptr);
+                       SAFE_FREE(data.dptr);
                }
                else
                        DEBUG(3, ("NULL relationship for counter [%d] using key [%s].\n", j, keybuf));
@@ -728,7 +810,10 @@ static int _reg_perfcount_assemble_global(PERF_DATA_BLOCK *block,
        return retval;
 }
 
-static BOOL _reg_perfcount_get_64(unsigned long long *retval,
+/*********************************************************************
+*********************************************************************/
+
+static BOOL _reg_perfcount_get_64(SMB_BIG_UINT *retval,
                                  TDB_CONTEXT *tdb,
                                  int key_part1,
                                  const char *key_part2)
@@ -747,25 +832,23 @@ static BOOL _reg_perfcount_get_64(unsigned long long *retval,
 
        memset(buf, 0, PERFCOUNT_MAX_LEN);
        memcpy(buf, data.dptr, data.dsize);
-       free(data.dptr);
+       SAFE_FREE(data.dptr);
 
-       *retval = strtoll(buf, NULL, 0);
+       *retval = atof(buf);
 
        return True;
 }
 
+/*********************************************************************
+*********************************************************************/
+
 static BOOL _reg_perfcount_init_data_block_perf(PERF_DATA_BLOCK *block,
                                                TDB_CONTEXT *names)
 {
-       unsigned long long PerfFreq, PerfTime, PerfTime100nSec;
+       SMB_BIG_UINT PerfFreq, PerfTime, PerfTime100nSec;
        TDB_CONTEXT *counters;
-       BOOL status;
-       pstring fname;
-    
-       status = False;
-    
-       pstrcpy(fname, lp_counters_dir());
-       pstrcat(fname, "/data.tdb");
+       BOOL status = False;
+       const char *fname = counters_directory( DATA_DB );
     
        counters = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
     
@@ -803,7 +886,11 @@ static BOOL _reg_perfcount_init_data_block_perf(PERF_DATA_BLOCK *block,
        return True;
 }
 
-static void _reg_perfcount_init_data_block(PERF_DATA_BLOCK *block, prs_struct *ps, TDB_CONTEXT *names)
+/*********************************************************************
+*********************************************************************/
+
+static BOOL _reg_perfcount_init_data_block(PERF_DATA_BLOCK *block,
+                                          prs_struct *ps, TDB_CONTEXT *names)
 {
        wpstring temp;
        time_t tm;
@@ -829,6 +916,9 @@ static void _reg_perfcount_init_data_block(PERF_DATA_BLOCK *block, prs_struct *p
        rpcstr_push((void *)temp, global_myname(), sizeof(temp), STR_TERMINATE);
        block->SystemNameLength = (strlen_w(temp) * 2) + 2;
        block->data = TALLOC_ZERO_ARRAY(ps->mem_ctx, uint8, block->SystemNameLength + (8 - (block->SystemNameLength % 8)));
+       if (block->data == NULL) {
+               return False;
+       }
        memcpy(block->data, temp, block->SystemNameLength);
        block->SystemNameOffset = sizeof(PERF_DATA_BLOCK) - sizeof(block->objects) - sizeof(block->data); 
        block->HeaderLength = block->SystemNameOffset + block->SystemNameLength;
@@ -836,9 +926,12 @@ static void _reg_perfcount_init_data_block(PERF_DATA_BLOCK *block, prs_struct *p
           so that the PERF_OBJECT_TYPE struct comes out 64-bit aligned */
        block->HeaderLength += 8 - (block->HeaderLength % 8);
 
-       return;
+       return True;
 }
 
+/*********************************************************************
+*********************************************************************/
+
 static uint32 _reg_perfcount_perf_data_block_fixup(PERF_DATA_BLOCK *block, prs_struct *ps)
 {
        int obj, cnt, inst, pad, i;
@@ -874,6 +967,9 @@ static uint32 _reg_perfcount_perf_data_block_fixup(PERF_DATA_BLOCK *block, prs_s
                                                            temp, 
                                                            char, 
                                                            counter_data->ByteLength- sizeof(counter_data->ByteLength));
+                               if (temp == NULL) {
+                                       return 0;
+                               }
                                memset(temp, 0, counter_data->ByteLength - sizeof(counter_data->ByteLength));
                                src_addr = (char *)counter_data->data;
                                for(i = 0; i < object[obj].NumCounters; i++)
@@ -892,6 +988,9 @@ static uint32 _reg_perfcount_perf_data_block_fixup(PERF_DATA_BLOCK *block, prs_s
                                                                         counter_data->data,
                                                                         uint8,
                                                                         counter_data->ByteLength - sizeof(counter_data->ByteLength) + pad);
+                               if (counter_data->data == NULL) {
+                                       return 0;
+                               }
                                memset(counter_data->data, 0, counter_data->ByteLength - sizeof(counter_data->ByteLength) + pad);
                                memcpy(counter_data->data, temp, counter_data->ByteLength - sizeof(counter_data->ByteLength));
                                counter_data->ByteLength += pad;
@@ -923,20 +1022,20 @@ static uint32 _reg_perfcount_perf_data_block_fixup(PERF_DATA_BLOCK *block, prs_s
 
        return block->TotalByteLength;
 }
-    
+
+/*********************************************************************
+*********************************************************************/
+
 uint32 reg_perfcount_get_perf_data_block(uint32 base_index, 
                                         prs_struct *ps, 
                                         PERF_DATA_BLOCK *block,
                                         char *object_ids)
 {
-       uint32 buffer_size = 0, last_counter;
-       pstring fname;
+       uint32 buffer_size = 0;
+       const char *fname = counters_directory( NAMES_DB );
        TDB_CONTEXT *names;
-       int retval;
-
-       pstrcpy(fname, lp_counters_dir());
-       pstrcat(fname, "/names.tdb");
-
+       int retval = 0;
+       
        names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
 
        if(names == NULL)
@@ -945,9 +1044,13 @@ uint32 reg_perfcount_get_perf_data_block(uint32 base_index,
                return 0;
        }
 
-       _reg_perfcount_init_data_block(block, ps, names);
+       if (!_reg_perfcount_init_data_block(block, ps, names)) {
+               DEBUG(0, ("_reg_perfcount_init_data_block failed\n"));
+               tdb_close(names);
+               return 0;
+       }
 
-       last_counter = reg_perfcount_get_last_counter(base_index);
+       reg_perfcount_get_last_counter(base_index);
     
        if(object_ids == NULL)
        {
@@ -963,9 +1066,16 @@ uint32 reg_perfcount_get_perf_data_block(uint32 base_index,
 
        tdb_close(names);
 
+       if (retval == -1) {
+               return 0;
+       }
+
        return buffer_size + block->HeaderLength;
 }
 
+/*********************************************************************
+*********************************************************************/
+
 static BOOL _reg_perfcount_marshall_perf_data_block(prs_struct *ps, PERF_DATA_BLOCK block, int depth)
 {
        int i;
@@ -1017,6 +1127,9 @@ static BOOL _reg_perfcount_marshall_perf_data_block(prs_struct *ps, PERF_DATA_BL
        return True;
 }
 
+/*********************************************************************
+*********************************************************************/
+
 static BOOL _reg_perfcount_marshall_perf_counters(prs_struct *ps,
                                                  PERF_OBJECT_TYPE object,
                                                  int depth)
@@ -1058,6 +1171,9 @@ static BOOL _reg_perfcount_marshall_perf_counters(prs_struct *ps,
        return True;
 }
 
+/*********************************************************************
+*********************************************************************/
+
 static BOOL _reg_perfcount_marshall_perf_counter_data(prs_struct *ps, 
                                                      PERF_COUNTER_BLOCK counter_data, 
                                                      int depth)
@@ -1078,6 +1194,9 @@ static BOOL _reg_perfcount_marshall_perf_counter_data(prs_struct *ps,
        return True;
 }
 
+/*********************************************************************
+*********************************************************************/
+
 static BOOL _reg_perfcount_marshall_perf_instances(prs_struct *ps,
                                                   PERF_OBJECT_TYPE object, 
                                                   int depth)
@@ -1116,6 +1235,9 @@ static BOOL _reg_perfcount_marshall_perf_instances(prs_struct *ps,
        return True;
 }
 
+/*********************************************************************
+*********************************************************************/
+
 static BOOL _reg_perfcount_marshall_perf_objects(prs_struct *ps, PERF_DATA_BLOCK block, int depth)
 {
        int obj;
@@ -1183,6 +1305,9 @@ static BOOL _reg_perfcount_marshall_perf_objects(prs_struct *ps, PERF_DATA_BLOCK
        return True;
 }
 
+/*********************************************************************
+*********************************************************************/
+
 static BOOL _reg_perfcount_marshall_hkpd(prs_struct *ps, PERF_DATA_BLOCK block)
 {
        int depth = 0;
@@ -1193,11 +1318,19 @@ static BOOL _reg_perfcount_marshall_hkpd(prs_struct *ps, PERF_DATA_BLOCK block)
        }
        return False;
 }
+
+/*********************************************************************
+*********************************************************************/
+
 WERROR reg_perfcount_get_hkpd(prs_struct *ps, uint32 max_buf_size, uint32 *outbuf_len, char *object_ids)
 {
        /*
         * For a detailed description of the layout of this structure,
         * see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/performance_data_format.asp
+        *
+        * By 2006-11-23 this link did not work anymore, I found something
+        * promising under
+        * http://msdn2.microsoft.com/en-us/library/aa373105.aspx -- vl
         */
        PERF_DATA_BLOCK block;
        uint32 buffer_size, base_index;