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