ldb: Build lmdb backend also in non-AD case
[samba.git] / source3 / registry / reg_perfcount.c
index 57d92ff1327ad2ef978d692d1c150e5e48752c05..7be0a1b0147627a45108dbf4e52fd89e05d80d4d 100644 (file)
@@ -1,26 +1,31 @@
-/* 
+/*
  *  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 3 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, see <http://www.gnu.org/licenses/>.
  */
 
 #include "includes.h"
+#include "system/filesys.h"
 #include "../librpc/gen_ndr/perfcount.h"
+#include "registry.h"
+#include "reg_perfcount.h"
+#include "../libcli/registry/util_reg.h"
+#include "util_tdb.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_REGISTRY
@@ -36,55 +41,58 @@ struct PERF_OBJECT_TYPE *_reg_perfcount_find_obj(struct PERF_DATA_BLOCK *block,
 /*********************************************************************
 *********************************************************************/
 
+/* returns perfcount path for dbname allocated on talloc_tos */
 static char *counters_directory(const char *dbname)
 {
-       char *path = NULL;
+       char *dir_path = NULL;
+       char *db_subpath = NULL;
        char *ret = NULL;
-       TALLOC_CTX *ctx = talloc_tos();
 
-       path = talloc_asprintf(ctx, "%s/%s", PERFCOUNTDIR, dbname);
-       if (!path) {
+       dir_path = state_path(talloc_tos(), PERFCOUNTDIR);
+       if (dir_path == NULL) {
                return NULL;
        }
 
-       ret = talloc_strdup(ctx, state_path(path));
-       TALLOC_FREE(path);
-       return ret;
-}
-
-/*********************************************************************
-*********************************************************************/
-
-void perfcount_init_keys( void )
-{
-       char *p = state_path(PERFCOUNTDIR);
-
-       /* no registry keys; just create the perfmon directory */
+       if (!directory_create_or_exist(dir_path, 0755)) {
+               TALLOC_FREE(dir_path);
+               return NULL;
+       }
 
-       if ( !directory_exist( p ) )
-               mkdir( p, 0755 );
+       db_subpath = talloc_asprintf(dir_path, "%s/%s", PERFCOUNTDIR, dbname);
+       if (db_subpath == NULL) {
+               TALLOC_FREE(dir_path);
+               return NULL;
+       }
 
-       return;
+       ret = state_path(talloc_tos(), db_subpath);
+       TALLOC_FREE(dir_path);
+       return ret;
 }
 
 /*********************************************************************
 *********************************************************************/
 
-uint32 reg_perfcount_get_base_index(void)
+uint32_t reg_perfcount_get_base_index(void)
 {
-       const char *fname = counters_directory( NAMES_DB );
+       char *fname;
        TDB_CONTEXT *names;
        TDB_DATA kbuf, dbuf;
        char key[] = "1";
-       uint32 retval = 0;
+       uint32_t retval = 0;
        char buf[PERFCOUNT_MAX_LEN];
 
+       fname = counters_directory(NAMES_DB);
+       if (fname == NULL) {
+               return 0;
+       }
+
        names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
 
        if ( !names ) {
-               DEBUG(1, ("reg_perfcount_get_base_index: unable to open [%s].\n", fname));
+               DEBUG(2, ("reg_perfcount_get_base_index: unable to open [%s].\n", fname));
+               TALLOC_FREE(fname);
                return 0;
-       }    
+       }
        /* needs to read the value of key "1" from the counter_names.tdb file, as that is
           where the total number of counters is stored. We're assuming no holes in the
           enumeration.
@@ -105,26 +113,25 @@ uint32 reg_perfcount_get_base_index(void)
        {
                DEBUG(1, ("reg_perfcount_get_base_index: failed to find key \'1\' in [%s].\n", fname));
                tdb_close(names);
+               TALLOC_FREE(fname);
                return 0;
        }
-       else
-       {
-               tdb_close(names);
-               memset(buf, 0, PERFCOUNT_MAX_LEN);
-               memcpy(buf, dbuf.dptr, dbuf.dsize);
-               retval = (uint32)atoi(buf);
-               SAFE_FREE(dbuf.dptr);
-               return retval;
-       }
-       return 0;
+
+       tdb_close(names);
+       TALLOC_FREE(fname);
+       memset(buf, 0, PERFCOUNT_MAX_LEN);
+       memcpy(buf, dbuf.dptr, dbuf.dsize);
+       retval = (uint32_t)atoi(buf);
+       SAFE_FREE(dbuf.dptr);
+       return retval;
 }
 
 /*********************************************************************
 *********************************************************************/
 
-uint32 reg_perfcount_get_last_counter(uint32 base_index)
+uint32_t reg_perfcount_get_last_counter(uint32_t base_index)
 {
-       uint32 retval;
+       uint32_t retval;
 
        if(base_index == 0)
                retval = 0;
@@ -137,9 +144,9 @@ uint32 reg_perfcount_get_last_counter(uint32 base_index)
 /*********************************************************************
 *********************************************************************/
 
-uint32 reg_perfcount_get_last_help(uint32 last_counter)
+uint32_t reg_perfcount_get_last_help(uint32_t last_counter)
 {
-       uint32 retval;
+       uint32_t retval;
 
        if(last_counter == 0)
                retval = 0;
@@ -153,50 +160,68 @@ uint32 reg_perfcount_get_last_help(uint32 last_counter)
 /*********************************************************************
 *********************************************************************/
 
-static uint32 _reg_perfcount_multi_sz_from_tdb(TDB_CONTEXT *tdb, 
+static uint32_t _reg_perfcount_multi_sz_from_tdb(TDB_CONTEXT *tdb,
                                               int keyval,
                                               char **retbuf,
-                                              uint32 buffer_size)
+                                              uint32_t buffer_size)
 {
        TDB_DATA kbuf, dbuf;
-       char temp[256];
+       char temp[PERFCOUNT_MAX_LEN] = {0};
        char *buf1 = *retbuf;
-       uint32 working_size = 0;
+       char *p = NULL;
+       uint32_t working_size = 0;
        DATA_BLOB name_index, name;
+       bool ok;
+
+       /* Set to NULL, to avoid possible double frees on error. */
+       *retbuf = NULL;
 
-       memset(temp, 0, sizeof(temp));
        snprintf(temp, sizeof(temp), "%d", keyval);
        kbuf = string_tdb_data(temp);
        dbuf = tdb_fetch(tdb, kbuf);
        if(dbuf.dptr == NULL)
        {
-               /* If a key isn't there, just bypass it -- this really shouldn't 
+               /* 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(tdb)));
                return buffer_size;
        }
        /* First encode the name_index */
-       working_size = (kbuf.dsize + 1)*sizeof(uint16);
-       buf1 = (char *)SMB_REALLOC(buf1, buffer_size + working_size);
-       if(!buf1) {
+       working_size = (kbuf.dsize + 1)*sizeof(uint16_t);
+       /* SMB_REALLOC frees buf1 on error */
+       p = (char *)SMB_REALLOC(buf1, buffer_size + working_size);
+       if (p == NULL) {
+               buffer_size = 0;
+               return buffer_size;
+       }
+       buf1 = p;
+       ok = push_reg_sz(talloc_tos(), &name_index, (const char *)kbuf.dptr);
+       if (!ok) {
+               SAFE_FREE(buf1);
                buffer_size = 0;
                return buffer_size;
        }
-       push_reg_sz(talloc_tos(), NULL, &name_index, (const char *)kbuf.dptr);
        memcpy(buf1+buffer_size, (char *)name_index.data, working_size);
        buffer_size += working_size;
        /* Now encode the actual name */
-       working_size = (dbuf.dsize + 1)*sizeof(uint16);
-       buf1 = (char *)SMB_REALLOC(buf1, buffer_size + working_size);
-       if(!buf1) {
+       working_size = (dbuf.dsize + 1)*sizeof(uint16_t);
+       /* SMB_REALLOC frees buf1 on error */
+       p = (char *)SMB_REALLOC(buf1, buffer_size + working_size);
+       if (p == NULL) {
                buffer_size = 0;
                return buffer_size;
        }
+       buf1 = p;
        memset(temp, 0, sizeof(temp));
        memcpy(temp, dbuf.dptr, dbuf.dsize);
        SAFE_FREE(dbuf.dptr);
-       push_reg_sz(talloc_tos(), NULL, &name, temp);
+       ok = push_reg_sz(talloc_tos(), &name, temp);
+       if (!ok) {
+               SAFE_FREE(buf1);
+               buffer_size = 0;
+               return buffer_size;
+       }
        memcpy(buf1+buffer_size, (char *)name.data, working_size);
        buffer_size += working_size;
 
@@ -208,28 +233,38 @@ static uint32 _reg_perfcount_multi_sz_from_tdb(TDB_CONTEXT *tdb,
 /*********************************************************************
 *********************************************************************/
 
-uint32 reg_perfcount_get_counter_help(uint32 base_index, char **retbuf)
+uint32_t reg_perfcount_get_counter_help(uint32_t base_index, char **retbuf)
 {
        char *buf1 = NULL;
-       uint32 buffer_size = 0;
+       uint32_t buffer_size = 0;
        TDB_CONTEXT *names;
-       const char *fname = counters_directory( NAMES_DB );
+       char *fname;
        int i;
 
-       if(base_index == 0)
+       if (base_index == 0) {
                return 0;
+       }
+
+       fname = counters_directory(NAMES_DB);
+       if (fname == NULL) {
+               return 0;
+       }
 
        names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
 
-       if(names == NULL)
-       {
+       if (names == NULL) {
                DEBUG(1, ("reg_perfcount_get_counter_help: unable to open [%s].\n", fname));
+               TALLOC_FREE(fname);
                return 0;
-       }    
+       }
+       TALLOC_FREE(fname);
 
        for(i = 1; i <= base_index; i++)
        {
                buffer_size = _reg_perfcount_multi_sz_from_tdb(names, (i*2)+1, retbuf, buffer_size);
+               if (buffer_size == 0) {
+                       return buffer_size;
+               }
        }
        tdb_close(names);
 
@@ -251,30 +286,43 @@ uint32 reg_perfcount_get_counter_help(uint32 base_index, char **retbuf)
 /*********************************************************************
 *********************************************************************/
 
-uint32 reg_perfcount_get_counter_names(uint32 base_index, char **retbuf)
+uint32_t reg_perfcount_get_counter_names(uint32_t base_index, char **retbuf)
 {
        char *buf1 = NULL;
-       uint32 buffer_size = 0;
+       uint32_t buffer_size = 0;
        TDB_CONTEXT *names;
-       const char *fname = counters_directory( NAMES_DB );
+       char *fname;
        int i;
 
-       if(base_index == 0)
+       if (base_index == 0) {
+               return 0;
+       }
+
+       fname = counters_directory(NAMES_DB);
+       if (fname == NULL) {
                return 0;
+       }
 
        names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
 
-       if(names == NULL)
-       {
+       if (names == NULL) {
                DEBUG(1, ("reg_perfcount_get_counter_names: unable to open [%s].\n", fname));
+               TALLOC_FREE(fname);
                return 0;
-       }    
+       }
+       TALLOC_FREE(fname);
 
        buffer_size = _reg_perfcount_multi_sz_from_tdb(names, 1, retbuf, buffer_size);
+       if (buffer_size == 0) {
+               return buffer_size;
+       }
 
        for(i = 1; i <= base_index; i++)
        {
                buffer_size = _reg_perfcount_multi_sz_from_tdb(names, i*2, retbuf, buffer_size);
+               if (buffer_size == 0) {
+                       return buffer_size;
+               }
        }
        tdb_close(names);
 
@@ -305,7 +353,7 @@ static void _reg_perfcount_make_key(TDB_DATA *key,
        memset(buf, 0, buflen);
        if(key_part2 != NULL)
                snprintf(buf, buflen,"%d%s", key_part1, key_part2);
-       else 
+       else
                snprintf(buf, buflen, "%d", key_part1);
 
        *key = string_tdb_data(buf);
@@ -346,7 +394,7 @@ static bool _reg_perfcount_ischild(TDB_DATA data)
 /*********************************************************************
 *********************************************************************/
 
-static uint32 _reg_perfcount_get_numinst(int objInd, TDB_CONTEXT *names)
+static uint32_t _reg_perfcount_get_numinst(int objInd, TDB_CONTEXT *names)
 {
        TDB_DATA key, data;
        char buf[PERFCOUNT_MAX_LEN];
@@ -355,12 +403,12 @@ static uint32 _reg_perfcount_get_numinst(int objInd, TDB_CONTEXT *names)
        data = tdb_fetch(names, key);
 
        if(data.dptr == NULL)
-               return (uint32)PERF_NO_INSTANCES;
+               return (uint32_t)PERF_NO_INSTANCES;
 
        memset(buf, 0, PERFCOUNT_MAX_LEN);
        memcpy(buf, data.dptr, data.dsize);
        SAFE_FREE(data.dptr);
-       return (uint32)atoi(buf);
+       return (uint32_t)atoi(buf);
 }
 
 /*********************************************************************
@@ -381,7 +429,7 @@ static bool _reg_perfcount_add_object(struct PERF_DATA_BLOCK *block,
        bool success = True;
        struct PERF_OBJECT_TYPE *obj;
 
-       block->objects = (struct PERF_OBJECT_TYPE *)TALLOC_REALLOC_ARRAY(mem_ctx,
+       block->objects = (struct PERF_OBJECT_TYPE *)talloc_realloc(mem_ctx,
                                                                  block->objects,
                                                                  struct PERF_OBJECT_TYPE,
                                                                  block->NumObjectTypes+1);
@@ -398,7 +446,7 @@ static bool _reg_perfcount_add_object(struct PERF_DATA_BLOCK *block,
        block->objects[block->NumObjectTypes].NumInstances = _reg_perfcount_get_numinst(num, names);
        block->objects[block->NumObjectTypes].counters = NULL;
        block->objects[block->NumObjectTypes].instances = NULL;
-       block->objects[block->NumObjectTypes].counter_data.ByteLength = sizeof(uint32);
+       block->objects[block->NumObjectTypes].counter_data.ByteLength = sizeof(uint32_t);
        block->objects[block->NumObjectTypes].counter_data.data = NULL;
        block->objects[block->NumObjectTypes].DetailLevel = PERF_DETAIL_NOVICE;
        block->NumObjectTypes+=1;
@@ -416,15 +464,21 @@ static bool _reg_perfcount_add_object(struct PERF_DATA_BLOCK *block,
 static bool _reg_perfcount_get_counter_data(TDB_DATA key, TDB_DATA *data)
 {
        TDB_CONTEXT *counters;
-       const char *fname = counters_directory( DATA_DB );
+       char *fname;
+
+       fname = counters_directory(DATA_DB);
+       if (fname == NULL) {
+               return false;
+       }
 
        counters = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
 
-       if(counters == NULL)
-       {
+       if (counters == NULL) {
                DEBUG(1, ("reg_perfcount_get_counter_data: unable to open [%s].\n", fname));
+               TALLOC_FREE(fname);
                return False;
-       }    
+       }
+       TALLOC_FREE(fname);
 
        *data = tdb_fetch(counters, key);
 
@@ -436,9 +490,9 @@ static bool _reg_perfcount_get_counter_data(TDB_DATA key, TDB_DATA *data)
 /*********************************************************************
 *********************************************************************/
 
-static uint32 _reg_perfcount_get_size_field(uint32 CounterType)
+static uint32_t _reg_perfcount_get_size_field(uint32_t CounterType)
 {
-       uint32 retval;
+       uint32_t retval;
 
        retval = CounterType;
 
@@ -453,7 +507,7 @@ static uint32 _reg_perfcount_get_size_field(uint32 CounterType)
 /*********************************************************************
 *********************************************************************/
 
-static uint32 _reg_perfcount_compute_scale(int64_t data)
+static uint32_t _reg_perfcount_compute_scale(int64_t data)
 {
        int scale = 0;
        if(data == 0)
@@ -469,7 +523,7 @@ static uint32 _reg_perfcount_compute_scale(int64_t data)
                scale++;
        }
 
-       return (uint32)scale;
+       return (uint32_t)scale;
 }
 
 /*********************************************************************
@@ -486,7 +540,7 @@ static bool _reg_perfcount_get_counter_info(struct PERF_DATA_BLOCK *block,
        size_t dsize, padding;
        long int data32, dbuf[2];
        int64_t data64;
-       uint32 counter_size;
+       uint32_t counter_size;
 
        obj->counters[obj->NumCounters].DefaultScale = 0;
        dbuf[0] = dbuf[1] = 0;
@@ -552,23 +606,23 @@ static bool _reg_perfcount_get_counter_info(struct PERF_DATA_BLOCK *block,
        SAFE_FREE(data.dptr);
 
        obj->counter_data.ByteLength += dsize + padding;
-       obj->counter_data.data = TALLOC_REALLOC_ARRAY(mem_ctx,
+       obj->counter_data.data = talloc_realloc(mem_ctx,
                                                      obj->counter_data.data,
-                                                     uint8,
-                                                     obj->counter_data.ByteLength - sizeof(uint32));
+                                                     uint8_t,
+                                                     obj->counter_data.ByteLength - sizeof(uint32_t));
        if(obj->counter_data.data == NULL)
                return False;
        if(dbuf[0] != 0 || dbuf[1] != 0)
        {
-               memcpy((void *)(obj->counter_data.data + 
-                               (obj->counter_data.ByteLength - (sizeof(uint32) + dsize))), 
+               memcpy((void *)(obj->counter_data.data +
+                               (obj->counter_data.ByteLength - (sizeof(uint32_t) + dsize))),
                       (const void *)dbuf, dsize);
        }
        else
        {
                /* Handling PERF_SIZE_VARIABLE_LEN */
                memcpy((void *)(obj->counter_data.data +
-                               (obj->counter_data.ByteLength - (sizeof(uint32) + dsize))),
+                               (obj->counter_data.ByteLength - (sizeof(uint32_t) + dsize))),
                       (const void *)buf, dsize);
        }
        obj->counters[obj->NumCounters].CounterOffset = obj->counter_data.ByteLength - dsize;
@@ -619,14 +673,14 @@ static bool _reg_perfcount_add_counter(struct PERF_DATA_BLOCK *block,
        obj = NULL;
        memset(buf, 0, PERFCOUNT_MAX_LEN);
        memcpy(buf, data.dptr, data.dsize);
-       begin = index(buf, '[');
-       end = index(buf, ']');
+       begin = strchr(buf, '[');
+       end = strchr(buf, ']');
        if(begin == NULL || end == NULL)
                return False;
        start = begin+1;
 
        while(start < end) {
-               stop = index(start, ',');
+               stop = strchr(start, ',');
                if(stop == NULL)
                        stop = end;
                *stop = '\0';
@@ -640,7 +694,7 @@ static bool _reg_perfcount_add_counter(struct PERF_DATA_BLOCK *block,
                                  parent, num));
                        return False;
                }
-               obj->counters = (struct PERF_COUNTER_DEFINITION *)TALLOC_REALLOC_ARRAY(mem_ctx,
+               obj->counters = (struct PERF_COUNTER_DEFINITION *)talloc_realloc(mem_ctx,
                                                                                obj->counters,
                                                                                struct PERF_COUNTER_DEFINITION,
                                                                                obj->NumCounters+1);
@@ -673,13 +727,13 @@ static bool _reg_perfcount_get_instance_info(struct PERF_INSTANCE_DEFINITION *in
                                             TDB_CONTEXT *names)
 {
        TDB_DATA key, data;
-       char buf[PERFCOUNT_MAX_LEN], temp[PERFCOUNT_MAX_LEN];
+       char buf[PERFCOUNT_MAX_LEN] = {0};
+       char temp[32] = {0};
        smb_ucs2_t *name = NULL;
        int pad;
 
        /* First grab the instance data from the data file */
-       memset(temp, 0, PERFCOUNT_MAX_LEN);
-       snprintf(temp, PERFCOUNT_MAX_LEN, "i%d", instId);
+       snprintf(temp, sizeof(temp), "i%d", instId);
        _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, obj->ObjectNameTitleIndex, temp);
        if (!_reg_perfcount_get_counter_data(key, &data)) {
                DEBUG(3, ("_reg_perfcount_get_counter_data failed\n"));
@@ -692,9 +746,9 @@ static bool _reg_perfcount_get_instance_info(struct PERF_INSTANCE_DEFINITION *in
                return False;
        }
        inst->counter_data.ByteLength = data.dsize + sizeof(inst->counter_data.ByteLength);
-       inst->counter_data.data = TALLOC_REALLOC_ARRAY(mem_ctx,
+       inst->counter_data.data = talloc_realloc(mem_ctx,
                                                       inst->counter_data.data,
-                                                      uint8,
+                                                      uint8_t,
                                                       data.dsize);
        if(inst->counter_data.data == NULL)
                return False;
@@ -703,8 +757,7 @@ static bool _reg_perfcount_get_instance_info(struct PERF_INSTANCE_DEFINITION *in
        SAFE_FREE(data.dptr);
 
        /* Fetch instance name */
-       memset(temp, 0, PERFCOUNT_MAX_LEN);
-       snprintf(temp, PERFCOUNT_MAX_LEN, "i%dname", instId);
+       snprintf(temp, sizeof(temp), "i%dname", instId);
        _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, obj->ObjectNameTitleIndex, temp);
        data = tdb_fetch(names, key);
        if(data.dptr == NULL)
@@ -724,9 +777,9 @@ static bool _reg_perfcount_get_instance_info(struct PERF_INSTANCE_DEFINITION *in
                        SAFE_FREE(data.dptr);
                        return False;
                }
-               inst->data = TALLOC_REALLOC_ARRAY(mem_ctx,
+               inst->data = talloc_realloc(mem_ctx,
                                                  inst->data,
-                                                 uint8,
+                                                 uint8_t,
                                                  inst->NameLength);
                if (inst->data == NULL) {
                        SAFE_FREE(data.dptr);
@@ -739,16 +792,16 @@ static bool _reg_perfcount_get_instance_info(struct PERF_INSTANCE_DEFINITION *in
        inst->ParentObjectTitleIndex = 0;
        inst->ParentObjectTitlePointer = 0;
        inst->UniqueID = PERF_NO_UNIQUE_ID;
-       inst->NameOffset = 6 * sizeof(uint32);
+       inst->NameOffset = 6 * sizeof(uint32_t);
 
        inst->ByteLength = inst->NameOffset + inst->NameLength;
        /* Need to be aligned on a 64-bit boundary here for counter_data */
        if((pad = (inst->ByteLength % 8)))
        {
                pad = 8 - pad;
-               inst->data = TALLOC_REALLOC_ARRAY(mem_ctx,
+               inst->data = talloc_realloc(mem_ctx,
                                                  inst->data,
-                                                 uint8,
+                                                 uint8_t,
                                                  inst->NameLength + pad);
                memset(inst->data + inst->NameLength, 0, pad);
                inst->ByteLength += pad;
@@ -768,7 +821,7 @@ static bool _reg_perfcount_add_instance(struct PERF_OBJECT_TYPE *obj,
        struct PERF_INSTANCE_DEFINITION *inst;
 
        if(obj->instances == NULL) {
-               obj->instances = TALLOC_REALLOC_ARRAY(mem_ctx,
+               obj->instances = talloc_realloc(mem_ctx,
                                                      obj->instances,
                                                      struct PERF_INSTANCE_DEFINITION,
                                                      obj->NumInstances);
@@ -819,7 +872,7 @@ static int _reg_perfcount_assemble_global(struct PERF_DATA_BLOCK *block,
                }
                else
                        DEBUG(3, ("NULL relationship for counter [%d] using key [%s].\n", j, keybuf));
-       }       
+       }
        return retval;
 }
 
@@ -861,15 +914,21 @@ static bool _reg_perfcount_init_data_block_perf(struct PERF_DATA_BLOCK *block,
        uint64_t PerfFreq, PerfTime, PerfTime100nSec;
        TDB_CONTEXT *counters;
        bool status = False;
-       const char *fname = counters_directory( DATA_DB );
+       char *fname;
+
+       fname = counters_directory(DATA_DB);
+       if (fname == NULL) {
+               return false;
+       }
 
        counters = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
 
-       if(counters == NULL)
-       {
+       if (counters == NULL) {
                DEBUG(1, ("reg_perfcount_init_data_block_perf: unable to open [%s].\n", fname));
+               TALLOC_FREE(fname);
                return False;
-       }    
+       }
+       TALLOC_FREE(fname);
 
        status = _reg_perfcount_get_64(&PerfFreq, names, 0, "PerfFreq");
        if(status == False)
@@ -924,13 +983,13 @@ static bool _reg_perfcount_init_data_block(struct PERF_DATA_BLOCK *block,
                                           bool bigendian_data)
 {
        smb_ucs2_t *temp = NULL;
+       TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
        time_t tm;
+       size_t sz;
 
-       if (rpcstr_push_talloc(mem_ctx, &temp, "PERF")==(size_t)-1) {
-               return false;
-       }
-       if (!temp) {
-               return false;
+       sz = rpcstr_push_talloc(tmp_ctx, &temp, "PERF");
+       if ((sz == -1) || (temp == NULL)) {
+               goto err_out;
        }
        memcpy(block->Signature, temp, strlen_w(temp) *2);
 
@@ -947,12 +1006,15 @@ static bool _reg_perfcount_init_data_block(struct PERF_DATA_BLOCK *block,
        tm = time(NULL);
        make_systemtime(&(block->SystemTime), gmtime(&tm));
        _reg_perfcount_init_data_block_perf(block, names);
-       memset(temp, 0, sizeof(temp));
-       rpcstr_push((void *)temp, global_myname(), sizeof(temp), STR_TERMINATE);
+
+       sz = rpcstr_push_talloc(tmp_ctx, &temp, lp_netbios_name());
+       if ((sz == -1) || (temp == NULL)) {
+               goto err_out;
+       }
        block->SystemNameLength = (strlen_w(temp) * 2) + 2;
-       block->data = TALLOC_ZERO_ARRAY(mem_ctx, uint8, block->SystemNameLength + (8 - (block->SystemNameLength % 8)));
+       block->data = talloc_zero_array(mem_ctx, uint8_t, block->SystemNameLength + (8 - (block->SystemNameLength % 8)));
        if (block->data == NULL) {
-               return False;
+               goto err_out;
        }
        memcpy(block->data, temp, block->SystemNameLength);
        block->SystemNameOffset = sizeof(struct PERF_DATA_BLOCK) - sizeof(block->objects) - sizeof(block->data);
@@ -960,14 +1022,19 @@ static bool _reg_perfcount_init_data_block(struct PERF_DATA_BLOCK *block,
        /* Make sure to adjust for 64-bit alignment for when we finish writing the system name,
           so that the PERF_OBJECT_TYPE struct comes out 64-bit aligned */
        block->HeaderLength += 8 - (block->HeaderLength % 8);
+       talloc_free(tmp_ctx);
 
-       return True;
+       return true;
+
+err_out:
+       talloc_free(tmp_ctx);
+       return false;
 }
 
 /*********************************************************************
 *********************************************************************/
 
-static uint32 _reg_perfcount_perf_data_block_fixup(struct PERF_DATA_BLOCK *block, TALLOC_CTX *mem_ctx)
+static uint32_t _reg_perfcount_perf_data_block_fixup(struct PERF_DATA_BLOCK *block, TALLOC_CTX *mem_ctx)
 {
        int obj, cnt, inst, pad, i;
        struct PERF_OBJECT_TYPE *object;
@@ -998,9 +1065,9 @@ static uint32 _reg_perfcount_perf_data_block_fixup(struct PERF_DATA_BLOCK *block
                                counter_data = &(instance->counter_data);
                                counter = &(object[obj].counters[object[obj].NumCounters - 1]);
                                counter_data->ByteLength = counter->CounterOffset + counter->CounterSize + sizeof(counter_data->ByteLength);
-                               temp = TALLOC_REALLOC_ARRAY(mem_ctx,
-                                                           temp, 
-                                                           char, 
+                               temp = talloc_realloc(mem_ctx,
+                                                           temp,
+                                                           char,
                                                            counter_data->ByteLength- sizeof(counter_data->ByteLength));
                                if (temp == NULL) {
                                        return 0;
@@ -1019,9 +1086,9 @@ static uint32 _reg_perfcount_perf_data_block_fixup(struct PERF_DATA_BLOCK *block
                                {
                                        pad = 8 - pad;
                                }
-                               counter_data->data = TALLOC_REALLOC_ARRAY(mem_ctx,
+                               counter_data->data = talloc_realloc(mem_ctx,
                                                                         counter_data->data,
-                                                                        uint8,
+                                                                        uint8_t,
                                                                         counter_data->ByteLength - sizeof(counter_data->ByteLength) + pad);
                                if (counter_data->data == NULL) {
                                        return 0;
@@ -1039,9 +1106,9 @@ static uint32 _reg_perfcount_perf_data_block_fixup(struct PERF_DATA_BLOCK *block
                        if((pad = (object[obj].counter_data.ByteLength % 8)))
                        {
                                pad = 8 - pad;
-                               object[obj].counter_data.data = TALLOC_REALLOC_ARRAY(mem_ctx,
+                               object[obj].counter_data.data = talloc_realloc(mem_ctx,
                                                                                     object[obj].counter_data.data,
-                                                                                    uint8
+                                                                                    uint8_t,
                                                                                     object[obj].counter_data.ByteLength + pad);
                                memset((void *)(object[obj].counter_data.data + object[obj].counter_data.ByteLength), 0, pad);
                                object[obj].counter_data.ByteLength += pad;
@@ -1061,24 +1128,31 @@ static uint32 _reg_perfcount_perf_data_block_fixup(struct PERF_DATA_BLOCK *block
 /*********************************************************************
 *********************************************************************/
 
-static uint32 reg_perfcount_get_perf_data_block(uint32 base_index,
+static uint32_t reg_perfcount_get_perf_data_block(uint32_t base_index,
                                                TALLOC_CTX *mem_ctx,
                                                struct PERF_DATA_BLOCK *block,
                                                const char *object_ids,
                                                bool bigendian_data)
 {
-       uint32 buffer_size = 0;
-       const char *fname = counters_directory( NAMES_DB );
+       uint32_t buffer_size = 0;
+       char *fname;
        TDB_CONTEXT *names;
        int retval = 0;
 
+       fname = counters_directory(NAMES_DB);
+       if (fname == NULL) {
+               return 0;
+       }
+
        names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
 
        if(names == NULL)
        {
                DEBUG(1, ("reg_perfcount_get_perf_data_block: unable to open [%s].\n", fname));
+               TALLOC_FREE(fname);
                return 0;
        }
+       TALLOC_FREE(fname);
 
        if (!_reg_perfcount_init_data_block(block, mem_ctx, names, bigendian_data)) {
                DEBUG(0, ("_reg_perfcount_init_data_block failed\n"));
@@ -1086,18 +1160,8 @@ static uint32 reg_perfcount_get_perf_data_block(uint32 base_index,
                return 0;
        }
 
-       reg_perfcount_get_last_counter(base_index);
+       retval = _reg_perfcount_assemble_global(block, mem_ctx, base_index, names);
 
-       if(object_ids == NULL)
-       {
-               /* we're getting a request for "Global" here */
-               retval = _reg_perfcount_assemble_global(block, mem_ctx, base_index, names);
-       }
-       else
-       {
-               /* we're getting a request for a specific set of PERF_OBJECT_TYPES */
-               retval = _reg_perfcount_assemble_global(block, mem_ctx, base_index, names);
-       }
        buffer_size = _reg_perfcount_perf_data_block_fixup(block, mem_ctx);
 
        tdb_close(names);
@@ -1181,8 +1245,8 @@ static bool _reg_perfcount_marshall_perf_data_block(prs_struct *ps, struct PERF_
        if(!prs_uint32("SystemNameOffset", ps, depth, &block.SystemNameOffset))
                return False;
        /* hack to make sure we're 64-bit aligned at the end of this whole mess */
-       if(!prs_uint8s(False, "SystemName", ps, depth, block.data, 
-                      block.HeaderLength - block.SystemNameOffset)) 
+       if(!prs_uint8s(False, "SystemName", ps, depth, block.data,
+                      block.HeaderLength - block.SystemNameOffset))
                return False;
 
        return True;
@@ -1235,7 +1299,7 @@ static bool _reg_perfcount_marshall_perf_counters(prs_struct *ps,
 /*********************************************************************
 *********************************************************************/
 
-static bool _reg_perfcount_marshall_perf_counter_data(prs_struct *ps, 
+static bool _reg_perfcount_marshall_perf_counter_data(prs_struct *ps,
                                                      struct PERF_COUNTER_BLOCK counter_data,
                                                      int depth)
 {
@@ -1247,7 +1311,7 @@ static bool _reg_perfcount_marshall_perf_counter_data(prs_struct *ps,
 
        if(!prs_uint32("ByteLength", ps, depth, &counter_data.ByteLength))
                return False;
-       if(!prs_uint8s(False, "CounterData", ps, depth, counter_data.data, counter_data.ByteLength - sizeof(uint32)))
+       if(!prs_uint8s(False, "CounterData", ps, depth, counter_data.data, counter_data.ByteLength - sizeof(uint32_t)))
                return False;
        if(!prs_align_uint64(ps))
                return False;
@@ -1348,7 +1412,7 @@ static bool _reg_perfcount_marshall_perf_objects(prs_struct *ps, struct PERF_DAT
 
                /* Now do the counters */
                /* If no instances, encode counter_data */
-               /* If instances, encode instace plus counter data for each instance */
+               /* If instances, encode instance plus counter data for each instance */
                if(_reg_perfcount_marshall_perf_counters(ps, object, depth) == False)
                        return False;
                if(object.NumInstances == PERF_NO_INSTANCES)
@@ -1369,7 +1433,7 @@ static bool _reg_perfcount_marshall_perf_objects(prs_struct *ps, struct PERF_DAT
 /*********************************************************************
 *********************************************************************/
 
-WERROR reg_perfcount_get_hkpd(prs_struct *ps, uint32 max_buf_size, uint32 *outbuf_len, const char *object_ids)
+WERROR reg_perfcount_get_hkpd(prs_struct *ps, uint32_t max_buf_size, uint32_t *outbuf_len, const char *object_ids)
 {
        /*
         * For a detailed description of the layout of this structure,
@@ -1380,7 +1444,7 @@ WERROR reg_perfcount_get_hkpd(prs_struct *ps, uint32 max_buf_size, uint32 *outbu
         * http://msdn2.microsoft.com/en-us/library/aa373105.aspx -- vl
         */
        struct PERF_DATA_BLOCK block;
-       uint32 buffer_size, base_index; 
+       uint32_t buffer_size, base_index;
 
        buffer_size = 0;
        base_index = reg_perfcount_get_base_index();
@@ -1393,10 +1457,10 @@ WERROR reg_perfcount_get_hkpd(prs_struct *ps, uint32 max_buf_size, uint32 *outbu
                *outbuf_len = buffer_size;
 
                if (!_reg_perfcount_marshall_perf_data_block(ps, block, 0))
-                       return WERR_NOMEM;
+                       return WERR_NOT_ENOUGH_MEMORY;
 
                if (!_reg_perfcount_marshall_perf_objects(ps, block, 0))
-                       return WERR_NOMEM;
+                       return WERR_NOT_ENOUGH_MEMORY;
 
                return WERR_OK;
        }
@@ -1404,8 +1468,8 @@ WERROR reg_perfcount_get_hkpd(prs_struct *ps, uint32 max_buf_size, uint32 *outbu
        {
                *outbuf_len = max_buf_size;
                if (!_reg_perfcount_marshall_perf_data_block(ps, block, 0))
-                       return WERR_NOMEM;
+                       return WERR_NOT_ENOUGH_MEMORY;
 
                return WERR_INSUFFICIENT_BUFFER;
        }
-}    
+}