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