Fix bug 7754 - samba uses index(), marked as legacy.
[samba.git] / source3 / registry / reg_perfcount.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  Virtual Windows Registry Layer
4  *
5  *  Copyright (C) Marcin Krzysztof Porwit    2005,
6  *  Copyright (C) Gerald (Jerry) Carter      2005.
7  *  
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 3 of the License, or
11  *  (at your option) any later version.
12  *  
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *  
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include "includes.h"
23 #include "../librpc/gen_ndr/perfcount.h"
24 #include "registry.h"
25 #include "reg_perfcount.h"
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_REGISTRY
29
30 #define PERFCOUNT_MAX_LEN 256
31
32 #define PERFCOUNTDIR    "perfmon"
33 #define NAMES_DB        "names.tdb"
34 #define DATA_DB         "data.tdb"
35
36 struct PERF_OBJECT_TYPE *_reg_perfcount_find_obj(struct PERF_DATA_BLOCK *block, int objind);
37
38 /*********************************************************************
39 *********************************************************************/
40
41 static char *counters_directory(const char *dbname)
42 {
43         char *path = NULL;
44         char *ret = NULL;
45         TALLOC_CTX *ctx = talloc_tos();
46
47         path = talloc_asprintf(ctx, "%s/%s", PERFCOUNTDIR, dbname);
48         if (!path) {
49                 return NULL;
50         }
51
52         ret = talloc_strdup(ctx, state_path(path));
53         TALLOC_FREE(path);
54         return ret;
55 }
56
57 /*********************************************************************
58 *********************************************************************/
59
60 void perfcount_init_keys( void )
61 {
62         char *p = state_path(PERFCOUNTDIR);
63
64         /* no registry keys; just create the perfmon directory */
65
66         if ( !directory_exist( p ) )
67                 mkdir( p, 0755 );
68
69         return;
70 }
71
72 /*********************************************************************
73 *********************************************************************/
74
75 uint32 reg_perfcount_get_base_index(void)
76 {
77         const char *fname = counters_directory( NAMES_DB );
78         TDB_CONTEXT *names;
79         TDB_DATA kbuf, dbuf;
80         char key[] = "1";
81         uint32 retval = 0;
82         char buf[PERFCOUNT_MAX_LEN];
83
84         names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
85
86         if ( !names ) {
87                 DEBUG(1, ("reg_perfcount_get_base_index: unable to open [%s].\n", fname));
88                 return 0;
89         }    
90         /* needs to read the value of key "1" from the counter_names.tdb file, as that is
91            where the total number of counters is stored. We're assuming no holes in the
92            enumeration.
93            The format for the counter_names.tdb file is:
94            key        value
95            1          num_counters
96            2          perf_counter1
97            3          perf_counter1_help
98            4          perf_counter2
99            5          perf_counter2_help
100            even_num   perf_counter<even_num>
101            even_num+1 perf_counter<even_num>_help
102            and so on.
103            So last_counter becomes num_counters*2, and last_help will be last_counter+1 */
104         kbuf = string_tdb_data(key);
105         dbuf = tdb_fetch(names, kbuf);
106         if(dbuf.dptr == NULL)
107         {
108                 DEBUG(1, ("reg_perfcount_get_base_index: failed to find key \'1\' in [%s].\n", fname));
109                 tdb_close(names);
110                 return 0;
111         }
112         else
113         {
114                 tdb_close(names);
115                 memset(buf, 0, PERFCOUNT_MAX_LEN);
116                 memcpy(buf, dbuf.dptr, dbuf.dsize);
117                 retval = (uint32)atoi(buf);
118                 SAFE_FREE(dbuf.dptr);
119                 return retval;
120         }
121         return 0;
122 }
123
124 /*********************************************************************
125 *********************************************************************/
126
127 uint32 reg_perfcount_get_last_counter(uint32 base_index)
128 {
129         uint32 retval;
130
131         if(base_index == 0)
132                 retval = 0;
133         else
134                 retval = base_index * 2;
135
136         return retval;
137 }
138
139 /*********************************************************************
140 *********************************************************************/
141
142 uint32 reg_perfcount_get_last_help(uint32 last_counter)
143 {
144         uint32 retval;
145
146         if(last_counter == 0)
147                 retval = 0;
148         else
149                 retval = last_counter + 1;
150
151         return retval;
152 }
153
154
155 /*********************************************************************
156 *********************************************************************/
157
158 static uint32 _reg_perfcount_multi_sz_from_tdb(TDB_CONTEXT *tdb, 
159                                                int keyval,
160                                                char **retbuf,
161                                                uint32 buffer_size)
162 {
163         TDB_DATA kbuf, dbuf;
164         char temp[256];
165         char *buf1 = *retbuf;
166         uint32 working_size = 0;
167         DATA_BLOB name_index, name;
168
169         memset(temp, 0, sizeof(temp));
170         snprintf(temp, sizeof(temp), "%d", keyval);
171         kbuf = string_tdb_data(temp);
172         dbuf = tdb_fetch(tdb, kbuf);
173         if(dbuf.dptr == NULL)
174         {
175                 /* If a key isn't there, just bypass it -- this really shouldn't 
176                    happen unless someone's mucking around with the tdb */
177                 DEBUG(3, ("_reg_perfcount_multi_sz_from_tdb: failed to find key [%s] in [%s].\n",
178                           temp, tdb_name(tdb)));
179                 return buffer_size;
180         }
181         /* First encode the name_index */
182         working_size = (kbuf.dsize + 1)*sizeof(uint16);
183         buf1 = (char *)SMB_REALLOC(buf1, buffer_size + working_size);
184         if(!buf1) {
185                 buffer_size = 0;
186                 return buffer_size;
187         }
188         push_reg_sz(talloc_tos(), &name_index, (const char *)kbuf.dptr);
189         memcpy(buf1+buffer_size, (char *)name_index.data, working_size);
190         buffer_size += working_size;
191         /* Now encode the actual name */
192         working_size = (dbuf.dsize + 1)*sizeof(uint16);
193         buf1 = (char *)SMB_REALLOC(buf1, buffer_size + working_size);
194         if(!buf1) {
195                 buffer_size = 0;
196                 return buffer_size;
197         }
198         memset(temp, 0, sizeof(temp));
199         memcpy(temp, dbuf.dptr, dbuf.dsize);
200         SAFE_FREE(dbuf.dptr);
201         push_reg_sz(talloc_tos(), &name, temp);
202         memcpy(buf1+buffer_size, (char *)name.data, working_size);
203         buffer_size += working_size;
204
205         *retbuf = buf1;
206
207         return buffer_size;
208 }
209
210 /*********************************************************************
211 *********************************************************************/
212
213 uint32 reg_perfcount_get_counter_help(uint32 base_index, char **retbuf)
214 {
215         char *buf1 = NULL;
216         uint32 buffer_size = 0;
217         TDB_CONTEXT *names;
218         const char *fname = counters_directory( NAMES_DB );
219         int i;
220
221         if(base_index == 0)
222                 return 0;
223
224         names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
225
226         if(names == NULL)
227         {
228                 DEBUG(1, ("reg_perfcount_get_counter_help: unable to open [%s].\n", fname));
229                 return 0;
230         }    
231
232         for(i = 1; i <= base_index; i++)
233         {
234                 buffer_size = _reg_perfcount_multi_sz_from_tdb(names, (i*2)+1, retbuf, buffer_size);
235         }
236         tdb_close(names);
237
238         /* Now terminate the MULTI_SZ with a double unicode NULL */
239         buf1 = *retbuf;
240         buf1 = (char *)SMB_REALLOC(buf1, buffer_size + 2);
241         if(!buf1) {
242                 buffer_size = 0;
243         } else {
244                 buf1[buffer_size++] = '\0';
245                 buf1[buffer_size++] = '\0';
246         }
247
248         *retbuf = buf1;
249
250         return buffer_size;
251 }
252
253 /*********************************************************************
254 *********************************************************************/
255
256 uint32 reg_perfcount_get_counter_names(uint32 base_index, char **retbuf)
257 {
258         char *buf1 = NULL;
259         uint32 buffer_size = 0;
260         TDB_CONTEXT *names;
261         const char *fname = counters_directory( NAMES_DB );
262         int i;
263
264         if(base_index == 0)
265                 return 0;
266
267         names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
268
269         if(names == NULL)
270         {
271                 DEBUG(1, ("reg_perfcount_get_counter_names: unable to open [%s].\n", fname));
272                 return 0;
273         }    
274
275         buffer_size = _reg_perfcount_multi_sz_from_tdb(names, 1, retbuf, buffer_size);
276
277         for(i = 1; i <= base_index; i++)
278         {
279                 buffer_size = _reg_perfcount_multi_sz_from_tdb(names, i*2, retbuf, buffer_size);
280         }
281         tdb_close(names);
282
283         /* Now terminate the MULTI_SZ with a double unicode NULL */
284         buf1 = *retbuf;
285         buf1 = (char *)SMB_REALLOC(buf1, buffer_size + 2);
286         if(!buf1) {
287                 buffer_size = 0;
288         } else {
289                 buf1[buffer_size++] = '\0';
290                 buf1[buffer_size++] = '\0';
291         }
292
293         *retbuf=buf1;
294
295         return buffer_size;
296 }
297
298 /*********************************************************************
299 *********************************************************************/
300
301 static void _reg_perfcount_make_key(TDB_DATA *key,
302                                     char *buf,
303                                     int buflen,
304                                     int key_part1,
305                                     const char *key_part2)
306 {
307         memset(buf, 0, buflen);
308         if(key_part2 != NULL)
309                 snprintf(buf, buflen,"%d%s", key_part1, key_part2);
310         else 
311                 snprintf(buf, buflen, "%d", key_part1);
312
313         *key = string_tdb_data(buf);
314
315         return;
316 }
317
318 /*********************************************************************
319 *********************************************************************/
320
321 static bool _reg_perfcount_isparent(TDB_DATA data)
322 {
323         if(data.dsize > 0)
324         {
325                 if(data.dptr[0] == 'p')
326                         return True;
327                 else
328                         return False;
329         }
330         return False;
331 }
332
333 /*********************************************************************
334 *********************************************************************/
335
336 static bool _reg_perfcount_ischild(TDB_DATA data)
337 {
338         if(data.dsize > 0)
339         {
340                 if(data.dptr[0] == 'c')
341                         return True;
342                 else
343                         return False;
344         }
345         return False;
346 }
347
348 /*********************************************************************
349 *********************************************************************/
350
351 static uint32 _reg_perfcount_get_numinst(int objInd, TDB_CONTEXT *names)
352 {
353         TDB_DATA key, data;
354         char buf[PERFCOUNT_MAX_LEN];
355
356         _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, objInd, "inst");
357         data = tdb_fetch(names, key);
358
359         if(data.dptr == NULL)
360                 return (uint32)PERF_NO_INSTANCES;
361
362         memset(buf, 0, PERFCOUNT_MAX_LEN);
363         memcpy(buf, data.dptr, data.dsize);
364         SAFE_FREE(data.dptr);
365         return (uint32)atoi(buf);
366 }
367
368 /*********************************************************************
369 *********************************************************************/
370
371 static bool _reg_perfcount_add_instance(struct PERF_OBJECT_TYPE *obj,
372                                         TALLOC_CTX *mem_ctx,
373                                         int instInd,
374                                         TDB_CONTEXT *names);
375
376 static bool _reg_perfcount_add_object(struct PERF_DATA_BLOCK *block,
377                                       TALLOC_CTX *mem_ctx,
378                                       int num,
379                                       TDB_DATA data,
380                                       TDB_CONTEXT *names)
381 {
382         int i;
383         bool success = True;
384         struct PERF_OBJECT_TYPE *obj;
385
386         block->objects = (struct PERF_OBJECT_TYPE *)TALLOC_REALLOC_ARRAY(mem_ctx,
387                                                                   block->objects,
388                                                                   struct PERF_OBJECT_TYPE,
389                                                                   block->NumObjectTypes+1);
390         if(block->objects == NULL)
391                 return False;
392         obj = &(block->objects[block->NumObjectTypes]);
393         memset((void *)&(block->objects[block->NumObjectTypes]), 0, sizeof(struct PERF_OBJECT_TYPE));
394         block->objects[block->NumObjectTypes].ObjectNameTitleIndex = num;
395         block->objects[block->NumObjectTypes].ObjectNameTitlePointer = 0;
396         block->objects[block->NumObjectTypes].ObjectHelpTitleIndex = num+1;
397         block->objects[block->NumObjectTypes].ObjectHelpTitlePointer = 0;
398         block->objects[block->NumObjectTypes].NumCounters = 0;
399         block->objects[block->NumObjectTypes].DefaultCounter = 0;
400         block->objects[block->NumObjectTypes].NumInstances = _reg_perfcount_get_numinst(num, names);
401         block->objects[block->NumObjectTypes].counters = NULL;
402         block->objects[block->NumObjectTypes].instances = NULL;
403         block->objects[block->NumObjectTypes].counter_data.ByteLength = sizeof(uint32);
404         block->objects[block->NumObjectTypes].counter_data.data = NULL;
405         block->objects[block->NumObjectTypes].DetailLevel = PERF_DETAIL_NOVICE;
406         block->NumObjectTypes+=1;
407
408         for(i = 0; i < (int)obj->NumInstances; i++) {
409                 success = _reg_perfcount_add_instance(obj, mem_ctx, i, names);
410         }
411
412         return success;
413 }
414
415 /*********************************************************************
416 *********************************************************************/
417
418 static bool _reg_perfcount_get_counter_data(TDB_DATA key, TDB_DATA *data)
419 {
420         TDB_CONTEXT *counters;
421         const char *fname = counters_directory( DATA_DB );
422
423         counters = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
424
425         if(counters == NULL)
426         {
427                 DEBUG(1, ("reg_perfcount_get_counter_data: unable to open [%s].\n", fname));
428                 return False;
429         }    
430
431         *data = tdb_fetch(counters, key);
432
433         tdb_close(counters);
434
435         return True;
436 }
437
438 /*********************************************************************
439 *********************************************************************/
440
441 static uint32 _reg_perfcount_get_size_field(uint32 CounterType)
442 {
443         uint32 retval;
444
445         retval = CounterType;
446
447         /* First mask out reserved lower 8 bits */
448         retval = retval & 0xFFFFFF00;
449         retval = retval << 22;
450         retval = retval >> 22;
451
452         return retval;
453 }
454
455 /*********************************************************************
456 *********************************************************************/
457
458 static uint32 _reg_perfcount_compute_scale(int64_t data)
459 {
460         int scale = 0;
461         if(data == 0)
462                 return scale;
463         while(data > 100)
464         {
465                 data /= 10;
466                 scale--;
467         }
468         while(data < 10)
469         {
470                 data *= 10;
471                 scale++;
472         }
473
474         return (uint32)scale;
475 }
476
477 /*********************************************************************
478 *********************************************************************/
479
480 static bool _reg_perfcount_get_counter_info(struct PERF_DATA_BLOCK *block,
481                                             TALLOC_CTX *mem_ctx,
482                                             int CounterIndex,
483                                             struct PERF_OBJECT_TYPE *obj,
484                                             TDB_CONTEXT *names)
485 {
486         TDB_DATA key, data;
487         char buf[PERFCOUNT_MAX_LEN];
488         size_t dsize, padding;
489         long int data32, dbuf[2];
490         int64_t data64;
491         uint32 counter_size;
492
493         obj->counters[obj->NumCounters].DefaultScale = 0;
494         dbuf[0] = dbuf[1] = 0;
495         padding = 0;
496
497         _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, CounterIndex, "type");
498         data = tdb_fetch(names, key);
499         if(data.dptr == NULL)
500         {
501                 DEBUG(3, ("_reg_perfcount_get_counter_info: No type data for counter [%d].\n", CounterIndex));
502                 return False;
503         }
504         memset(buf, 0, PERFCOUNT_MAX_LEN);
505         memcpy(buf, data.dptr, data.dsize);
506         obj->counters[obj->NumCounters].CounterType = atoi(buf);
507         DEBUG(10, ("_reg_perfcount_get_counter_info: Got type [%d] for counter [%d].\n",
508                    obj->counters[obj->NumCounters].CounterType, CounterIndex));
509         SAFE_FREE(data.dptr);
510
511         /* Fetch the actual data */
512         _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, CounterIndex, "");
513         _reg_perfcount_get_counter_data(key, &data);
514         if(data.dptr == NULL)
515         {
516                 DEBUG(3, ("_reg_perfcount_get_counter_info: No counter data for counter [%d].\n", CounterIndex));
517                 return False;
518         }
519
520         counter_size = _reg_perfcount_get_size_field(obj->counters[obj->NumCounters].CounterType);
521
522         if(counter_size == PERF_SIZE_DWORD)
523         {
524                 dsize = sizeof(data32);
525                 memset(buf, 0, PERFCOUNT_MAX_LEN);
526                 memcpy(buf, data.dptr, data.dsize);
527                 data32 = strtol(buf, NULL, 0);
528                 if((obj->counters[obj->NumCounters].CounterType & 0x00000F00) == PERF_TYPE_NUMBER)
529                         obj->counters[obj->NumCounters].DefaultScale = _reg_perfcount_compute_scale((int64_t)data32);
530                 else
531                         obj->counters[obj->NumCounters].DefaultScale = 0;
532                 dbuf[0] = data32;
533                 padding = (dsize - (obj->counter_data.ByteLength%dsize)) % dsize;
534         }
535         else if(counter_size == PERF_SIZE_LARGE)
536         {
537                 dsize = sizeof(data64);
538                 memset(buf, 0, PERFCOUNT_MAX_LEN);
539                 memcpy(buf, data.dptr, data.dsize);
540                 data64 = atof(buf);
541                 if((obj->counters[obj->NumCounters].CounterType & 0x00000F00) == PERF_TYPE_NUMBER)
542                         obj->counters[obj->NumCounters].DefaultScale = _reg_perfcount_compute_scale(data64);
543                 else
544                         obj->counters[obj->NumCounters].DefaultScale = 0;
545                 memcpy((void *)dbuf, (const void *)&data64, dsize);
546                 padding = (dsize - (obj->counter_data.ByteLength%dsize)) % dsize;
547         }
548         else /* PERF_SIZE_VARIABLE_LEN */
549         {
550                 dsize = data.dsize;
551                 memset(buf, 0, PERFCOUNT_MAX_LEN);
552                 memcpy(buf, data.dptr, data.dsize);
553         }
554         SAFE_FREE(data.dptr);
555
556         obj->counter_data.ByteLength += dsize + padding;
557         obj->counter_data.data = TALLOC_REALLOC_ARRAY(mem_ctx,
558                                                       obj->counter_data.data,
559                                                       uint8,
560                                                       obj->counter_data.ByteLength - sizeof(uint32));
561         if(obj->counter_data.data == NULL)
562                 return False;
563         if(dbuf[0] != 0 || dbuf[1] != 0)
564         {
565                 memcpy((void *)(obj->counter_data.data + 
566                                 (obj->counter_data.ByteLength - (sizeof(uint32) + dsize))), 
567                        (const void *)dbuf, dsize);
568         }
569         else
570         {
571                 /* Handling PERF_SIZE_VARIABLE_LEN */
572                 memcpy((void *)(obj->counter_data.data +
573                                 (obj->counter_data.ByteLength - (sizeof(uint32) + dsize))),
574                        (const void *)buf, dsize);
575         }
576         obj->counters[obj->NumCounters].CounterOffset = obj->counter_data.ByteLength - dsize;
577         if(obj->counters[obj->NumCounters].CounterOffset % dsize != 0)
578         {
579                 DEBUG(3,("Improperly aligned counter [%d]\n", obj->NumCounters));
580         }
581         obj->counters[obj->NumCounters].CounterSize = dsize;
582
583         return True;
584 }
585
586 /*********************************************************************
587 *********************************************************************/
588
589 struct PERF_OBJECT_TYPE *_reg_perfcount_find_obj(struct PERF_DATA_BLOCK *block, int objind)
590 {
591         int i;
592
593         struct PERF_OBJECT_TYPE *obj = NULL;
594
595         for(i = 0; i < block->NumObjectTypes; i++)
596         {
597                 if(block->objects[i].ObjectNameTitleIndex == objind)
598                 {
599                         obj = &(block->objects[i]);
600                 }
601         }
602
603         return obj;
604 }
605
606 /*********************************************************************
607 *********************************************************************/
608
609 static bool _reg_perfcount_add_counter(struct PERF_DATA_BLOCK *block,
610                                        TALLOC_CTX *mem_ctx,
611                                        int num,
612                                        TDB_DATA data,
613                                        TDB_CONTEXT *names)
614 {
615         char *begin, *end, *start, *stop;
616         int parent;
617         struct PERF_OBJECT_TYPE *obj;
618         bool success = True;
619         char buf[PERFCOUNT_MAX_LEN];
620
621         obj = NULL;
622         memset(buf, 0, PERFCOUNT_MAX_LEN);
623         memcpy(buf, data.dptr, data.dsize);
624         begin = strchr(buf, '[');
625         end = strchr(buf, ']');
626         if(begin == NULL || end == NULL)
627                 return False;
628         start = begin+1;
629
630         while(start < end) {
631                 stop = strchr(start, ',');
632                 if(stop == NULL)
633                         stop = end;
634                 *stop = '\0';
635                 parent = atoi(start);
636
637                 obj = _reg_perfcount_find_obj(block, parent);
638                 if(obj == NULL) {
639                         /* At this point we require that the parent object exist.
640                            This can probably be handled better at some later time */
641                         DEBUG(3, ("_reg_perfcount_add_counter: Could not find parent object [%d] for counter [%d].\n",
642                                   parent, num));
643                         return False;
644                 }
645                 obj->counters = (struct PERF_COUNTER_DEFINITION *)TALLOC_REALLOC_ARRAY(mem_ctx,
646                                                                                 obj->counters,
647                                                                                 struct PERF_COUNTER_DEFINITION,
648                                                                                 obj->NumCounters+1);
649                 if(obj->counters == NULL)
650                         return False;
651                 memset((void *)&(obj->counters[obj->NumCounters]), 0, sizeof(struct PERF_COUNTER_DEFINITION));
652                 obj->counters[obj->NumCounters].CounterNameTitleIndex=num;
653                 obj->counters[obj->NumCounters].CounterHelpTitleIndex=num+1;
654                 obj->counters[obj->NumCounters].DetailLevel = PERF_DETAIL_NOVICE;
655                 obj->counters[obj->NumCounters].ByteLength = sizeof(struct PERF_COUNTER_DEFINITION);
656                 success = _reg_perfcount_get_counter_info(block, mem_ctx, num, obj, names);
657                 obj->NumCounters += 1;
658                 start = stop + 1;
659         }
660
661         /* Handle case of Objects/Counters without any counter data, which would suggest
662            that the required instances are not there yet, so change NumInstances from
663            PERF_NO_INSTANCES to 0 */
664
665         return success;
666 }
667
668 /*********************************************************************
669 *********************************************************************/
670
671 static bool _reg_perfcount_get_instance_info(struct PERF_INSTANCE_DEFINITION *inst,
672                                              TALLOC_CTX *mem_ctx,
673                                              int instId,
674                                              struct PERF_OBJECT_TYPE *obj,
675                                              TDB_CONTEXT *names)
676 {
677         TDB_DATA key, data;
678         char buf[PERFCOUNT_MAX_LEN], temp[PERFCOUNT_MAX_LEN];
679         smb_ucs2_t *name = NULL;
680         int pad;
681
682         /* First grab the instance data from the data file */
683         memset(temp, 0, PERFCOUNT_MAX_LEN);
684         snprintf(temp, PERFCOUNT_MAX_LEN, "i%d", instId);
685         _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, obj->ObjectNameTitleIndex, temp);
686         if (!_reg_perfcount_get_counter_data(key, &data)) {
687                 DEBUG(3, ("_reg_perfcount_get_counter_data failed\n"));
688                 return false;
689         }
690         if(data.dptr == NULL)
691         {
692                 DEBUG(3, ("_reg_perfcount_get_instance_info: No instance data for instance [%s].\n",
693                           buf));
694                 return False;
695         }
696         inst->counter_data.ByteLength = data.dsize + sizeof(inst->counter_data.ByteLength);
697         inst->counter_data.data = TALLOC_REALLOC_ARRAY(mem_ctx,
698                                                        inst->counter_data.data,
699                                                        uint8,
700                                                        data.dsize);
701         if(inst->counter_data.data == NULL)
702                 return False;
703         memset(inst->counter_data.data, 0, data.dsize);
704         memcpy(inst->counter_data.data, data.dptr, data.dsize);
705         SAFE_FREE(data.dptr);
706
707         /* Fetch instance name */
708         memset(temp, 0, PERFCOUNT_MAX_LEN);
709         snprintf(temp, PERFCOUNT_MAX_LEN, "i%dname", instId);
710         _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, obj->ObjectNameTitleIndex, temp);
711         data = tdb_fetch(names, key);
712         if(data.dptr == NULL)
713         {
714                 /* Not actually an error, but possibly unintended? -- just logging FYI */
715                 DEBUG(3, ("_reg_perfcount_get_instance_info: No instance name for instance [%s].\n",
716                           buf));
717                 inst->NameLength = 0;
718         }
719         else
720         {
721                 memset(buf, 0, PERFCOUNT_MAX_LEN);
722                 memcpy(buf, data.dptr, MIN(PERFCOUNT_MAX_LEN-1,data.dsize));
723                 buf[PERFCOUNT_MAX_LEN-1] = '\0';
724                 inst->NameLength = rpcstr_push_talloc(mem_ctx, &name, buf);
725                 if (inst->NameLength == (uint32_t)-1 || !name) {
726                         SAFE_FREE(data.dptr);
727                         return False;
728                 }
729                 inst->data = TALLOC_REALLOC_ARRAY(mem_ctx,
730                                                   inst->data,
731                                                   uint8,
732                                                   inst->NameLength);
733                 if (inst->data == NULL) {
734                         SAFE_FREE(data.dptr);
735                         return False;
736                 }
737                 memcpy(inst->data, name, inst->NameLength);
738                 SAFE_FREE(data.dptr);
739         }
740
741         inst->ParentObjectTitleIndex = 0;
742         inst->ParentObjectTitlePointer = 0;
743         inst->UniqueID = PERF_NO_UNIQUE_ID;
744         inst->NameOffset = 6 * sizeof(uint32);
745
746         inst->ByteLength = inst->NameOffset + inst->NameLength;
747         /* Need to be aligned on a 64-bit boundary here for counter_data */
748         if((pad = (inst->ByteLength % 8)))
749         {
750                 pad = 8 - pad;
751                 inst->data = TALLOC_REALLOC_ARRAY(mem_ctx,
752                                                   inst->data,
753                                                   uint8,
754                                                   inst->NameLength + pad);
755                 memset(inst->data + inst->NameLength, 0, pad);
756                 inst->ByteLength += pad;
757         }
758
759         return True;
760 }
761
762 /*********************************************************************
763 *********************************************************************/
764
765 static bool _reg_perfcount_add_instance(struct PERF_OBJECT_TYPE *obj,
766                                         TALLOC_CTX *mem_ctx,
767                                         int instInd,
768                                         TDB_CONTEXT *names)
769 {
770         struct PERF_INSTANCE_DEFINITION *inst;
771
772         if(obj->instances == NULL) {
773                 obj->instances = TALLOC_REALLOC_ARRAY(mem_ctx,
774                                                       obj->instances,
775                                                       struct PERF_INSTANCE_DEFINITION,
776                                                       obj->NumInstances);
777         }
778         if(obj->instances == NULL)
779                 return False;
780
781         memset(&(obj->instances[instInd]), 0, sizeof(struct PERF_INSTANCE_DEFINITION));
782         inst = &(obj->instances[instInd]);
783         return _reg_perfcount_get_instance_info(inst, mem_ctx, instInd, obj, names);
784 }
785
786 /*********************************************************************
787 *********************************************************************/
788
789 static int _reg_perfcount_assemble_global(struct PERF_DATA_BLOCK *block,
790                                           TALLOC_CTX *mem_ctx,
791                                           int base_index,
792                                           TDB_CONTEXT *names)
793 {
794         bool success;
795         int i, j, retval = 0;
796         char keybuf[PERFCOUNT_MAX_LEN];
797         TDB_DATA key, data;
798
799         for(i = 1; i <= base_index; i++)
800         {
801                 j = i*2;
802                 _reg_perfcount_make_key(&key, keybuf, PERFCOUNT_MAX_LEN, j, "rel");
803                 data = tdb_fetch(names, key);
804                 if(data.dptr != NULL)
805                 {
806                         if(_reg_perfcount_isparent(data))
807                                 success = _reg_perfcount_add_object(block, mem_ctx, j, data, names);
808                         else if(_reg_perfcount_ischild(data))
809                                 success = _reg_perfcount_add_counter(block, mem_ctx, j, data, names);
810                         else
811                         {
812                                 DEBUG(3, ("Bogus relationship [%s] for counter [%d].\n", data.dptr, j));
813                                 success = False;
814                         }
815                         if(success == False)
816                         {
817                                 DEBUG(3, ("_reg_perfcount_assemble_global: Failed to add new relationship for counter [%d].\n", j));
818                                 retval = -1;
819                         }
820                         SAFE_FREE(data.dptr);
821                 }
822                 else
823                         DEBUG(3, ("NULL relationship for counter [%d] using key [%s].\n", j, keybuf));
824         }       
825         return retval;
826 }
827
828 /*********************************************************************
829 *********************************************************************/
830
831 static bool _reg_perfcount_get_64(uint64_t *retval,
832                                   TDB_CONTEXT *tdb,
833                                   int key_part1,
834                                   const char *key_part2)
835 {
836         TDB_DATA key, data;
837         char buf[PERFCOUNT_MAX_LEN];
838
839         _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, key_part1, key_part2);
840
841         data = tdb_fetch(tdb, key);
842         if(data.dptr == NULL)
843         {
844                 DEBUG(3,("_reg_perfcount_get_64: No data found for key [%s].\n", key.dptr));
845                 return False;
846         }
847
848         memset(buf, 0, PERFCOUNT_MAX_LEN);
849         memcpy(buf, data.dptr, data.dsize);
850         SAFE_FREE(data.dptr);
851
852         *retval = atof(buf);
853
854         return True;
855 }
856
857 /*********************************************************************
858 *********************************************************************/
859
860 static bool _reg_perfcount_init_data_block_perf(struct PERF_DATA_BLOCK *block,
861                                                 TDB_CONTEXT *names)
862 {
863         uint64_t PerfFreq, PerfTime, PerfTime100nSec;
864         TDB_CONTEXT *counters;
865         bool status = False;
866         const char *fname = counters_directory( DATA_DB );
867
868         counters = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
869
870         if(counters == NULL)
871         {
872                 DEBUG(1, ("reg_perfcount_init_data_block_perf: unable to open [%s].\n", fname));
873                 return False;
874         }    
875
876         status = _reg_perfcount_get_64(&PerfFreq, names, 0, "PerfFreq");
877         if(status == False)
878         {
879                 tdb_close(counters);
880                 return status;
881         }
882         memcpy((void *)&(block->PerfFreq), (const void *)&PerfFreq, sizeof(PerfFreq));
883
884         status = _reg_perfcount_get_64(&PerfTime, counters, 0, "PerfTime");
885         if(status == False)
886         {
887                 tdb_close(counters);
888                 return status;
889         }
890         memcpy((void *)&(block->PerfTime), (const void *)&PerfTime, sizeof(PerfTime));
891
892         status = _reg_perfcount_get_64(&PerfTime100nSec, counters, 0, "PerfTime100nSec");
893         if(status == False)
894         {
895                 tdb_close(counters);
896                 return status;
897         }
898         memcpy((void *)&(block->PerfTime100nSec), (const void *)&PerfTime100nSec, sizeof(PerfTime100nSec));
899
900         tdb_close(counters);
901         return True;
902 }
903
904 /*******************************************************************
905 ********************************************************************/
906
907 static bool make_systemtime(struct SYSTEMTIME *systime, struct tm *unixtime)
908 {
909         systime->year=unixtime->tm_year+1900;
910         systime->month=unixtime->tm_mon+1;
911         systime->dayofweek=unixtime->tm_wday;
912         systime->day=unixtime->tm_mday;
913         systime->hour=unixtime->tm_hour;
914         systime->minute=unixtime->tm_min;
915         systime->second=unixtime->tm_sec;
916         systime->milliseconds=0;
917
918         return True;
919 }
920
921 /*********************************************************************
922 *********************************************************************/
923
924 static bool _reg_perfcount_init_data_block(struct PERF_DATA_BLOCK *block,
925                                            TALLOC_CTX *mem_ctx, TDB_CONTEXT *names,
926                                            bool bigendian_data)
927 {
928         smb_ucs2_t *temp = NULL;
929         time_t tm;
930
931         if (rpcstr_push_talloc(mem_ctx, &temp, "PERF")==(size_t)-1) {
932                 return false;
933         }
934         if (!temp) {
935                 return false;
936         }
937         memcpy(block->Signature, temp, strlen_w(temp) *2);
938
939         if(bigendian_data)
940                 block->LittleEndian = 0;
941         else
942                 block->LittleEndian = 1;
943         block->Version = 1;
944         block->Revision = 1;
945         block->TotalByteLength = 0;
946         block->NumObjectTypes = 0;
947         block->DefaultObject = -1;
948         block->objects = NULL;
949         tm = time(NULL);
950         make_systemtime(&(block->SystemTime), gmtime(&tm));
951         _reg_perfcount_init_data_block_perf(block, names);
952         memset(temp, 0, sizeof(temp));
953         rpcstr_push((void *)temp, global_myname(), sizeof(temp), STR_TERMINATE);
954         block->SystemNameLength = (strlen_w(temp) * 2) + 2;
955         block->data = TALLOC_ZERO_ARRAY(mem_ctx, uint8, block->SystemNameLength + (8 - (block->SystemNameLength % 8)));
956         if (block->data == NULL) {
957                 return False;
958         }
959         memcpy(block->data, temp, block->SystemNameLength);
960         block->SystemNameOffset = sizeof(struct PERF_DATA_BLOCK) - sizeof(block->objects) - sizeof(block->data);
961         block->HeaderLength = block->SystemNameOffset + block->SystemNameLength;
962         /* Make sure to adjust for 64-bit alignment for when we finish writing the system name,
963            so that the PERF_OBJECT_TYPE struct comes out 64-bit aligned */
964         block->HeaderLength += 8 - (block->HeaderLength % 8);
965
966         return True;
967 }
968
969 /*********************************************************************
970 *********************************************************************/
971
972 static uint32 _reg_perfcount_perf_data_block_fixup(struct PERF_DATA_BLOCK *block, TALLOC_CTX *mem_ctx)
973 {
974         int obj, cnt, inst, pad, i;
975         struct PERF_OBJECT_TYPE *object;
976         struct PERF_INSTANCE_DEFINITION *instance;
977         struct PERF_COUNTER_DEFINITION *counter;
978         struct PERF_COUNTER_BLOCK *counter_data;
979         char *temp = NULL, *src_addr, *dst_addr;
980
981         block->TotalByteLength = 0;
982         object = block->objects;
983         for(obj = 0; obj < block->NumObjectTypes; obj++)
984         {
985                 object[obj].TotalByteLength = 0;
986                 object[obj].DefinitionLength = 0;
987                 instance = object[obj].instances;
988                 counter = object[obj].counters;
989                 for(cnt = 0; cnt < object[obj].NumCounters; cnt++)
990                 {
991                         object[obj].TotalByteLength += counter[cnt].ByteLength;
992                         object[obj].DefinitionLength += counter[cnt].ByteLength;
993                 }
994                 if(object[obj].NumInstances != PERF_NO_INSTANCES)
995                 {
996                         for(inst = 0; inst < object[obj].NumInstances; inst++)
997                         {
998                                 instance = &(object[obj].instances[inst]);
999                                 object[obj].TotalByteLength += instance->ByteLength;
1000                                 counter_data = &(instance->counter_data);
1001                                 counter = &(object[obj].counters[object[obj].NumCounters - 1]);
1002                                 counter_data->ByteLength = counter->CounterOffset + counter->CounterSize + sizeof(counter_data->ByteLength);
1003                                 temp = TALLOC_REALLOC_ARRAY(mem_ctx,
1004                                                             temp, 
1005                                                             char, 
1006                                                             counter_data->ByteLength- sizeof(counter_data->ByteLength));
1007                                 if (temp == NULL) {
1008                                         return 0;
1009                                 }
1010                                 memset(temp, 0, counter_data->ByteLength - sizeof(counter_data->ByteLength));
1011                                 src_addr = (char *)counter_data->data;
1012                                 for(i = 0; i < object[obj].NumCounters; i++)
1013                                 {
1014                                         counter = &(object[obj].counters[i]);
1015                                         dst_addr = temp + counter->CounterOffset - sizeof(counter_data->ByteLength);
1016                                         memcpy(dst_addr, src_addr, counter->CounterSize);
1017                                         src_addr += counter->CounterSize;
1018                                 }
1019                                 /* Make sure to be 64-bit aligned */
1020                                 if((pad = (counter_data->ByteLength % 8)))
1021                                 {
1022                                         pad = 8 - pad;
1023                                 }
1024                                 counter_data->data = TALLOC_REALLOC_ARRAY(mem_ctx,
1025                                                                          counter_data->data,
1026                                                                          uint8,
1027                                                                          counter_data->ByteLength - sizeof(counter_data->ByteLength) + pad);
1028                                 if (counter_data->data == NULL) {
1029                                         return 0;
1030                                 }
1031                                 memset(counter_data->data, 0, counter_data->ByteLength - sizeof(counter_data->ByteLength) + pad);
1032                                 memcpy(counter_data->data, temp, counter_data->ByteLength - sizeof(counter_data->ByteLength));
1033                                 counter_data->ByteLength += pad;
1034                                 object[obj].TotalByteLength += counter_data->ByteLength;
1035                         }
1036                 }
1037                 else
1038                 {
1039                         /* Need to be 64-bit aligned at the end of the counter_data block, so pad counter_data to a 64-bit boundary,
1040                            so that the next PERF_OBJECT_TYPE can start on a 64-bit alignment */
1041                         if((pad = (object[obj].counter_data.ByteLength % 8)))
1042                         {
1043                                 pad = 8 - pad;
1044                                 object[obj].counter_data.data = TALLOC_REALLOC_ARRAY(mem_ctx,
1045                                                                                      object[obj].counter_data.data,
1046                                                                                      uint8, 
1047                                                                                      object[obj].counter_data.ByteLength + pad);
1048                                 memset((void *)(object[obj].counter_data.data + object[obj].counter_data.ByteLength), 0, pad);
1049                                 object[obj].counter_data.ByteLength += pad;
1050                         }
1051                         object[obj].TotalByteLength += object[obj].counter_data.ByteLength;
1052                 }
1053                 object[obj].HeaderLength = sizeof(*object) - (sizeof(counter) + sizeof(instance) + sizeof(struct PERF_COUNTER_BLOCK));
1054                 object[obj].TotalByteLength += object[obj].HeaderLength;
1055                 object[obj].DefinitionLength += object[obj].HeaderLength;
1056
1057                 block->TotalByteLength += object[obj].TotalByteLength;
1058         }
1059
1060         return block->TotalByteLength;
1061 }
1062
1063 /*********************************************************************
1064 *********************************************************************/
1065
1066 static uint32 reg_perfcount_get_perf_data_block(uint32 base_index,
1067                                                 TALLOC_CTX *mem_ctx,
1068                                                 struct PERF_DATA_BLOCK *block,
1069                                                 const char *object_ids,
1070                                                 bool bigendian_data)
1071 {
1072         uint32 buffer_size = 0;
1073         const char *fname = counters_directory( NAMES_DB );
1074         TDB_CONTEXT *names;
1075         int retval = 0;
1076
1077         names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
1078
1079         if(names == NULL)
1080         {
1081                 DEBUG(1, ("reg_perfcount_get_perf_data_block: unable to open [%s].\n", fname));
1082                 return 0;
1083         }
1084
1085         if (!_reg_perfcount_init_data_block(block, mem_ctx, names, bigendian_data)) {
1086                 DEBUG(0, ("_reg_perfcount_init_data_block failed\n"));
1087                 tdb_close(names);
1088                 return 0;
1089         }
1090
1091         reg_perfcount_get_last_counter(base_index);
1092
1093         if(object_ids == NULL)
1094         {
1095                 /* we're getting a request for "Global" here */
1096                 retval = _reg_perfcount_assemble_global(block, mem_ctx, base_index, names);
1097         }
1098         else
1099         {
1100                 /* we're getting a request for a specific set of PERF_OBJECT_TYPES */
1101                 retval = _reg_perfcount_assemble_global(block, mem_ctx, base_index, names);
1102         }
1103         buffer_size = _reg_perfcount_perf_data_block_fixup(block, mem_ctx);
1104
1105         tdb_close(names);
1106
1107         if (retval == -1) {
1108                 return 0;
1109         }
1110
1111         return buffer_size + block->HeaderLength;
1112 }
1113
1114 /*******************************************************************
1115 ********************************************************************/
1116
1117 static bool smb_io_system_time(const char *desc, prs_struct *ps, int depth, struct SYSTEMTIME *systime)
1118 {
1119         if(!prs_uint16("year", ps, depth, &systime->year))
1120                 return False;
1121         if(!prs_uint16("month", ps, depth, &systime->month))
1122                 return False;
1123         if(!prs_uint16("dayofweek", ps, depth, &systime->dayofweek))
1124                 return False;
1125         if(!prs_uint16("day", ps, depth, &systime->day))
1126                 return False;
1127         if(!prs_uint16("hour", ps, depth, &systime->hour))
1128                 return False;
1129         if(!prs_uint16("minute", ps, depth, &systime->minute))
1130                 return False;
1131         if(!prs_uint16("second", ps, depth, &systime->second))
1132                 return False;
1133         if(!prs_uint16("milliseconds", ps, depth, &systime->milliseconds))
1134                 return False;
1135
1136         return True;
1137 }
1138
1139 /*********************************************************************
1140 *********************************************************************/
1141
1142 static bool _reg_perfcount_marshall_perf_data_block(prs_struct *ps, struct PERF_DATA_BLOCK block, int depth)
1143 {
1144         int i;
1145         prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_data_block");
1146         depth++;
1147
1148         if(!prs_align(ps))
1149                 return False;
1150         for(i = 0; i < 4; i++)
1151         {
1152                 if(!prs_uint16("Signature", ps, depth, &block.Signature[i]))
1153                         return False;
1154         }
1155         if(!prs_uint32("Little Endian", ps, depth, &block.LittleEndian))
1156                 return False;
1157         if(!prs_uint32("Version", ps, depth, &block.Version))
1158                 return False;
1159         if(!prs_uint32("Revision", ps, depth, &block.Revision))
1160                 return False;
1161         if(!prs_uint32("TotalByteLength", ps, depth, &block.TotalByteLength))
1162                 return False;
1163         if(!prs_uint32("HeaderLength", ps, depth, &block.HeaderLength))
1164                 return False;
1165         if(!prs_uint32("NumObjectTypes", ps, depth, &block.NumObjectTypes))
1166                 return False;
1167         if(!prs_uint32("DefaultObject", ps, depth, &block.DefaultObject))
1168                 return False;
1169         if(!smb_io_system_time("SystemTime", ps, depth, &block.SystemTime))
1170                 return False;
1171         if(!prs_uint32("Padding", ps, depth, &block.Padding))
1172                 return False;
1173         if(!prs_align_uint64(ps))
1174                 return False;
1175         if(!prs_uint64("PerfTime", ps, depth, &block.PerfTime))
1176                 return False;
1177         if(!prs_uint64("PerfFreq", ps, depth, &block.PerfFreq))
1178                 return False;
1179         if(!prs_uint64("PerfTime100nSec", ps, depth, &block.PerfTime100nSec))
1180                 return False;
1181         if(!prs_uint32("SystemNameLength", ps, depth, &block.SystemNameLength))
1182                 return False;
1183         if(!prs_uint32("SystemNameOffset", ps, depth, &block.SystemNameOffset))
1184                 return False;
1185         /* hack to make sure we're 64-bit aligned at the end of this whole mess */
1186         if(!prs_uint8s(False, "SystemName", ps, depth, block.data, 
1187                        block.HeaderLength - block.SystemNameOffset)) 
1188                 return False;
1189
1190         return True;
1191 }
1192
1193 /*********************************************************************
1194 *********************************************************************/
1195
1196 static bool _reg_perfcount_marshall_perf_counters(prs_struct *ps,
1197                                                   struct PERF_OBJECT_TYPE object,
1198                                                   int depth)
1199 {
1200         int cnt;
1201         struct PERF_COUNTER_DEFINITION counter;
1202
1203         prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_counters");
1204         depth++;
1205
1206         for(cnt = 0; cnt < object.NumCounters; cnt++)
1207         {
1208                 counter = object.counters[cnt];
1209
1210                 if(!prs_align(ps))
1211                         return False;
1212                 if(!prs_uint32("ByteLength", ps, depth, &counter.ByteLength))
1213                         return False;
1214                 if(!prs_uint32("CounterNameTitleIndex", ps, depth, &counter.CounterNameTitleIndex))
1215                         return False;
1216                 if(!prs_uint32("CounterNameTitlePointer", ps, depth, &counter.CounterNameTitlePointer))
1217                         return False;
1218                 if(!prs_uint32("CounterHelpTitleIndex", ps, depth, &counter.CounterHelpTitleIndex))
1219                         return False;
1220                 if(!prs_uint32("CounterHelpTitlePointer", ps, depth, &counter.CounterHelpTitlePointer))
1221                         return False;
1222                 if(!prs_uint32("DefaultScale", ps, depth, &counter.DefaultScale))
1223                         return False;
1224                 if(!prs_uint32("DetailLevel", ps, depth, &counter.DetailLevel))
1225                         return False;
1226                 if(!prs_uint32("CounterType", ps, depth, &counter.CounterType))
1227                         return False;
1228                 if(!prs_uint32("CounterSize", ps, depth, &counter.CounterSize))
1229                         return False;
1230                 if(!prs_uint32("CounterOffset", ps, depth, &counter.CounterOffset))
1231                         return False;
1232         }
1233
1234         return True;
1235 }
1236
1237 /*********************************************************************
1238 *********************************************************************/
1239
1240 static bool _reg_perfcount_marshall_perf_counter_data(prs_struct *ps, 
1241                                                       struct PERF_COUNTER_BLOCK counter_data,
1242                                                       int depth)
1243 {
1244         prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_counter_data");
1245         depth++;
1246
1247         if(!prs_align_uint64(ps))
1248                 return False;
1249
1250         if(!prs_uint32("ByteLength", ps, depth, &counter_data.ByteLength))
1251                 return False;
1252         if(!prs_uint8s(False, "CounterData", ps, depth, counter_data.data, counter_data.ByteLength - sizeof(uint32)))
1253                 return False;
1254         if(!prs_align_uint64(ps))
1255                 return False;
1256
1257         return True;
1258 }
1259
1260 /*********************************************************************
1261 *********************************************************************/
1262
1263 static bool _reg_perfcount_marshall_perf_instances(prs_struct *ps,
1264                                                    struct PERF_OBJECT_TYPE object,
1265                                                    int depth)
1266 {
1267         struct PERF_INSTANCE_DEFINITION instance;
1268         int inst;
1269
1270         prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_instances");
1271         depth++;
1272
1273         for(inst = 0; inst < object.NumInstances; inst++)
1274         {
1275                 instance = object.instances[inst];
1276
1277                 if(!prs_align(ps))
1278                         return False;
1279                 if(!prs_uint32("ByteLength", ps, depth, &instance.ByteLength))
1280                         return False;
1281                 if(!prs_uint32("ParentObjectTitleIndex", ps, depth, &instance.ParentObjectTitleIndex))
1282                         return False;
1283                 if(!prs_uint32("ParentObjectTitlePointer", ps, depth, &instance.ParentObjectTitlePointer))
1284                         return False;
1285                 if(!prs_uint32("UniqueID", ps, depth, &instance.UniqueID))
1286                         return False;
1287                 if(!prs_uint32("NameOffset", ps, depth, &instance.NameOffset))
1288                         return False;
1289                 if(!prs_uint32("NameLength", ps, depth, &instance.NameLength))
1290                         return False;
1291                 if(!prs_uint8s(False, "InstanceName", ps, depth, instance.data,
1292                                instance.ByteLength - instance.NameOffset))
1293                         return False;
1294                 if(_reg_perfcount_marshall_perf_counter_data(ps, instance.counter_data, depth) == False)
1295                         return False;
1296         }
1297
1298         return True;
1299 }
1300
1301 /*********************************************************************
1302 *********************************************************************/
1303
1304 static bool _reg_perfcount_marshall_perf_objects(prs_struct *ps, struct PERF_DATA_BLOCK block, int depth)
1305 {
1306         int obj;
1307
1308         struct PERF_OBJECT_TYPE object;
1309
1310         prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_objects");
1311         depth++;
1312
1313         for(obj = 0; obj < block.NumObjectTypes; obj++)
1314         {
1315                 object = block.objects[obj];
1316
1317                 if(!prs_align(ps))
1318                         return False;
1319
1320                 if(!prs_uint32("TotalByteLength", ps, depth, &object.TotalByteLength))
1321                         return False;
1322                 if(!prs_uint32("DefinitionLength", ps, depth, &object.DefinitionLength))
1323                         return False;
1324                 if(!prs_uint32("HeaderLength", ps, depth, &object.HeaderLength))
1325                         return False;
1326                 if(!prs_uint32("ObjectNameTitleIndex", ps, depth, &object.ObjectNameTitleIndex))
1327                         return False;
1328                 if(!prs_uint32("ObjectNameTitlePointer", ps, depth, &object.ObjectNameTitlePointer))
1329                         return False;
1330                 if(!prs_uint32("ObjectHelpTitleIndex", ps, depth, &object.ObjectHelpTitleIndex))
1331                         return False;
1332                 if(!prs_uint32("ObjectHelpTitlePointer", ps, depth, &object.ObjectHelpTitlePointer))
1333                         return False;
1334                 if(!prs_uint32("DetailLevel", ps, depth, &object.DetailLevel))
1335                         return False;
1336                 if(!prs_uint32("NumCounters", ps, depth, &object.NumCounters))
1337                         return False;
1338                 if(!prs_uint32("DefaultCounter", ps, depth, &object.DefaultCounter))
1339                         return False;
1340                 if(!prs_uint32("NumInstances", ps, depth, &object.NumInstances))
1341                         return False;
1342                 if(!prs_uint32("CodePage", ps, depth, &object.CodePage))
1343                         return False;
1344                 if(!prs_align_uint64(ps))
1345                         return False;
1346                 if(!prs_uint64("PerfTime", ps, depth, &object.PerfTime))
1347                         return False;
1348                 if(!prs_uint64("PerfFreq", ps, depth, &object.PerfFreq))
1349                         return False;
1350
1351                 /* Now do the counters */
1352                 /* If no instances, encode counter_data */
1353                 /* If instances, encode instace plus counter data for each instance */
1354                 if(_reg_perfcount_marshall_perf_counters(ps, object, depth) == False)
1355                         return False;
1356                 if(object.NumInstances == PERF_NO_INSTANCES)
1357                 {
1358                         if(_reg_perfcount_marshall_perf_counter_data(ps, object.counter_data, depth) == False)
1359                                 return False;
1360                 }
1361                 else
1362                 {
1363                         if(_reg_perfcount_marshall_perf_instances(ps, object, depth) == False)
1364                                 return False;
1365                 }
1366         }
1367
1368         return True;
1369 }
1370
1371 /*********************************************************************
1372 *********************************************************************/
1373
1374 WERROR reg_perfcount_get_hkpd(prs_struct *ps, uint32 max_buf_size, uint32 *outbuf_len, const char *object_ids)
1375 {
1376         /*
1377          * For a detailed description of the layout of this structure,
1378          * see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/performance_data_format.asp
1379          *
1380          * By 2006-11-23 this link did not work anymore, I found something
1381          * promising under
1382          * http://msdn2.microsoft.com/en-us/library/aa373105.aspx -- vl
1383          */
1384         struct PERF_DATA_BLOCK block;
1385         uint32 buffer_size, base_index; 
1386
1387         buffer_size = 0;
1388         base_index = reg_perfcount_get_base_index();
1389         ZERO_STRUCT(block);
1390
1391         buffer_size = reg_perfcount_get_perf_data_block(base_index, ps->mem_ctx, &block, object_ids, ps->bigendian_data);
1392
1393         if(buffer_size < max_buf_size)
1394         {
1395                 *outbuf_len = buffer_size;
1396
1397                 if (!_reg_perfcount_marshall_perf_data_block(ps, block, 0))
1398                         return WERR_NOMEM;
1399
1400                 if (!_reg_perfcount_marshall_perf_objects(ps, block, 0))
1401                         return WERR_NOMEM;
1402
1403                 return WERR_OK;
1404         }
1405         else
1406         {
1407                 *outbuf_len = max_buf_size;
1408                 if (!_reg_perfcount_marshall_perf_data_block(ps, block, 0))
1409                         return WERR_NOMEM;
1410
1411                 return WERR_INSUFFICIENT_BUFFER;
1412         }
1413 }