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