smbd: Don't talloc_zero where we assign the struct a line below
[Samba.git] / source3 / registry / reg_perfcount.c
blob7be0a1b0147627a45108dbf4e52fd89e05d80d4d
1 /*
2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
5 * Copyright (C) Marcin Krzysztof Porwit 2005,
6 * Copyright (C) Gerald (Jerry) Carter 2005.
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.
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.
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/>.
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"
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_REGISTRY
33 #define PERFCOUNT_MAX_LEN 256
35 #define PERFCOUNTDIR "perfmon"
36 #define NAMES_DB "names.tdb"
37 #define DATA_DB "data.tdb"
39 struct PERF_OBJECT_TYPE *_reg_perfcount_find_obj(struct PERF_DATA_BLOCK *block, int objind);
41 /*********************************************************************
42 *********************************************************************/
44 /* returns perfcount path for dbname allocated on talloc_tos */
45 static char *counters_directory(const char *dbname)
47 char *dir_path = NULL;
48 char *db_subpath = NULL;
49 char *ret = NULL;
51 dir_path = state_path(talloc_tos(), PERFCOUNTDIR);
52 if (dir_path == NULL) {
53 return NULL;
56 if (!directory_create_or_exist(dir_path, 0755)) {
57 TALLOC_FREE(dir_path);
58 return NULL;
61 db_subpath = talloc_asprintf(dir_path, "%s/%s", PERFCOUNTDIR, dbname);
62 if (db_subpath == NULL) {
63 TALLOC_FREE(dir_path);
64 return NULL;
67 ret = state_path(talloc_tos(), db_subpath);
68 TALLOC_FREE(dir_path);
69 return ret;
72 /*********************************************************************
73 *********************************************************************/
75 uint32_t reg_perfcount_get_base_index(void)
77 char *fname;
78 TDB_CONTEXT *names;
79 TDB_DATA kbuf, dbuf;
80 char key[] = "1";
81 uint32_t retval = 0;
82 char buf[PERFCOUNT_MAX_LEN];
84 fname = counters_directory(NAMES_DB);
85 if (fname == NULL) {
86 return 0;
89 names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
91 if ( !names ) {
92 DEBUG(2, ("reg_perfcount_get_base_index: unable to open [%s].\n", fname));
93 TALLOC_FREE(fname);
94 return 0;
96 /* needs to read the value of key "1" from the counter_names.tdb file, as that is
97 where the total number of counters is stored. We're assuming no holes in the
98 enumeration.
99 The format for the counter_names.tdb file is:
100 key value
101 1 num_counters
102 2 perf_counter1
103 3 perf_counter1_help
104 4 perf_counter2
105 5 perf_counter2_help
106 even_num perf_counter<even_num>
107 even_num+1 perf_counter<even_num>_help
108 and so on.
109 So last_counter becomes num_counters*2, and last_help will be last_counter+1 */
110 kbuf = string_tdb_data(key);
111 dbuf = tdb_fetch(names, kbuf);
112 if(dbuf.dptr == NULL)
114 DEBUG(1, ("reg_perfcount_get_base_index: failed to find key \'1\' in [%s].\n", fname));
115 tdb_close(names);
116 TALLOC_FREE(fname);
117 return 0;
120 tdb_close(names);
121 TALLOC_FREE(fname);
122 memset(buf, 0, PERFCOUNT_MAX_LEN);
123 memcpy(buf, dbuf.dptr, dbuf.dsize);
124 retval = (uint32_t)atoi(buf);
125 SAFE_FREE(dbuf.dptr);
126 return retval;
129 /*********************************************************************
130 *********************************************************************/
132 uint32_t reg_perfcount_get_last_counter(uint32_t base_index)
134 uint32_t retval;
136 if(base_index == 0)
137 retval = 0;
138 else
139 retval = base_index * 2;
141 return retval;
144 /*********************************************************************
145 *********************************************************************/
147 uint32_t reg_perfcount_get_last_help(uint32_t last_counter)
149 uint32_t retval;
151 if(last_counter == 0)
152 retval = 0;
153 else
154 retval = last_counter + 1;
156 return retval;
160 /*********************************************************************
161 *********************************************************************/
163 static uint32_t _reg_perfcount_multi_sz_from_tdb(TDB_CONTEXT *tdb,
164 int keyval,
165 char **retbuf,
166 uint32_t buffer_size)
168 TDB_DATA kbuf, dbuf;
169 char temp[PERFCOUNT_MAX_LEN] = {0};
170 char *buf1 = *retbuf;
171 char *p = NULL;
172 uint32_t working_size = 0;
173 DATA_BLOB name_index, name;
174 bool ok;
176 /* Set to NULL, to avoid possible double frees on error. */
177 *retbuf = NULL;
179 snprintf(temp, sizeof(temp), "%d", keyval);
180 kbuf = string_tdb_data(temp);
181 dbuf = tdb_fetch(tdb, kbuf);
182 if(dbuf.dptr == NULL)
184 /* If a key isn't there, just bypass it -- this really shouldn't
185 happen unless someone's mucking around with the tdb */
186 DEBUG(3, ("_reg_perfcount_multi_sz_from_tdb: failed to find key [%s] in [%s].\n",
187 temp, tdb_name(tdb)));
188 return buffer_size;
190 /* First encode the name_index */
191 working_size = (kbuf.dsize + 1)*sizeof(uint16_t);
192 /* SMB_REALLOC frees buf1 on error */
193 p = (char *)SMB_REALLOC(buf1, buffer_size + working_size);
194 if (p == NULL) {
195 buffer_size = 0;
196 return buffer_size;
198 buf1 = p;
199 ok = push_reg_sz(talloc_tos(), &name_index, (const char *)kbuf.dptr);
200 if (!ok) {
201 SAFE_FREE(buf1);
202 buffer_size = 0;
203 return buffer_size;
205 memcpy(buf1+buffer_size, (char *)name_index.data, working_size);
206 buffer_size += working_size;
207 /* Now encode the actual name */
208 working_size = (dbuf.dsize + 1)*sizeof(uint16_t);
209 /* SMB_REALLOC frees buf1 on error */
210 p = (char *)SMB_REALLOC(buf1, buffer_size + working_size);
211 if (p == NULL) {
212 buffer_size = 0;
213 return buffer_size;
215 buf1 = p;
216 memset(temp, 0, sizeof(temp));
217 memcpy(temp, dbuf.dptr, dbuf.dsize);
218 SAFE_FREE(dbuf.dptr);
219 ok = push_reg_sz(talloc_tos(), &name, temp);
220 if (!ok) {
221 SAFE_FREE(buf1);
222 buffer_size = 0;
223 return buffer_size;
225 memcpy(buf1+buffer_size, (char *)name.data, working_size);
226 buffer_size += working_size;
228 *retbuf = buf1;
230 return buffer_size;
233 /*********************************************************************
234 *********************************************************************/
236 uint32_t reg_perfcount_get_counter_help(uint32_t base_index, char **retbuf)
238 char *buf1 = NULL;
239 uint32_t buffer_size = 0;
240 TDB_CONTEXT *names;
241 char *fname;
242 int i;
244 if (base_index == 0) {
245 return 0;
248 fname = counters_directory(NAMES_DB);
249 if (fname == NULL) {
250 return 0;
253 names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
255 if (names == NULL) {
256 DEBUG(1, ("reg_perfcount_get_counter_help: unable to open [%s].\n", fname));
257 TALLOC_FREE(fname);
258 return 0;
260 TALLOC_FREE(fname);
262 for(i = 1; i <= base_index; i++)
264 buffer_size = _reg_perfcount_multi_sz_from_tdb(names, (i*2)+1, retbuf, buffer_size);
265 if (buffer_size == 0) {
266 return buffer_size;
269 tdb_close(names);
271 /* Now terminate the MULTI_SZ with a double unicode NULL */
272 buf1 = *retbuf;
273 buf1 = (char *)SMB_REALLOC(buf1, buffer_size + 2);
274 if(!buf1) {
275 buffer_size = 0;
276 } else {
277 buf1[buffer_size++] = '\0';
278 buf1[buffer_size++] = '\0';
281 *retbuf = buf1;
283 return buffer_size;
286 /*********************************************************************
287 *********************************************************************/
289 uint32_t reg_perfcount_get_counter_names(uint32_t base_index, char **retbuf)
291 char *buf1 = NULL;
292 uint32_t buffer_size = 0;
293 TDB_CONTEXT *names;
294 char *fname;
295 int i;
297 if (base_index == 0) {
298 return 0;
301 fname = counters_directory(NAMES_DB);
302 if (fname == NULL) {
303 return 0;
306 names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
308 if (names == NULL) {
309 DEBUG(1, ("reg_perfcount_get_counter_names: unable to open [%s].\n", fname));
310 TALLOC_FREE(fname);
311 return 0;
313 TALLOC_FREE(fname);
315 buffer_size = _reg_perfcount_multi_sz_from_tdb(names, 1, retbuf, buffer_size);
316 if (buffer_size == 0) {
317 return buffer_size;
320 for(i = 1; i <= base_index; i++)
322 buffer_size = _reg_perfcount_multi_sz_from_tdb(names, i*2, retbuf, buffer_size);
323 if (buffer_size == 0) {
324 return buffer_size;
327 tdb_close(names);
329 /* Now terminate the MULTI_SZ with a double unicode NULL */
330 buf1 = *retbuf;
331 buf1 = (char *)SMB_REALLOC(buf1, buffer_size + 2);
332 if(!buf1) {
333 buffer_size = 0;
334 } else {
335 buf1[buffer_size++] = '\0';
336 buf1[buffer_size++] = '\0';
339 *retbuf=buf1;
341 return buffer_size;
344 /*********************************************************************
345 *********************************************************************/
347 static void _reg_perfcount_make_key(TDB_DATA *key,
348 char *buf,
349 int buflen,
350 int key_part1,
351 const char *key_part2)
353 memset(buf, 0, buflen);
354 if(key_part2 != NULL)
355 snprintf(buf, buflen,"%d%s", key_part1, key_part2);
356 else
357 snprintf(buf, buflen, "%d", key_part1);
359 *key = string_tdb_data(buf);
361 return;
364 /*********************************************************************
365 *********************************************************************/
367 static bool _reg_perfcount_isparent(TDB_DATA data)
369 if(data.dsize > 0)
371 if(data.dptr[0] == 'p')
372 return True;
373 else
374 return False;
376 return False;
379 /*********************************************************************
380 *********************************************************************/
382 static bool _reg_perfcount_ischild(TDB_DATA data)
384 if(data.dsize > 0)
386 if(data.dptr[0] == 'c')
387 return True;
388 else
389 return False;
391 return False;
394 /*********************************************************************
395 *********************************************************************/
397 static uint32_t _reg_perfcount_get_numinst(int objInd, TDB_CONTEXT *names)
399 TDB_DATA key, data;
400 char buf[PERFCOUNT_MAX_LEN];
402 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, objInd, "inst");
403 data = tdb_fetch(names, key);
405 if(data.dptr == NULL)
406 return (uint32_t)PERF_NO_INSTANCES;
408 memset(buf, 0, PERFCOUNT_MAX_LEN);
409 memcpy(buf, data.dptr, data.dsize);
410 SAFE_FREE(data.dptr);
411 return (uint32_t)atoi(buf);
414 /*********************************************************************
415 *********************************************************************/
417 static bool _reg_perfcount_add_instance(struct PERF_OBJECT_TYPE *obj,
418 TALLOC_CTX *mem_ctx,
419 int instInd,
420 TDB_CONTEXT *names);
422 static bool _reg_perfcount_add_object(struct PERF_DATA_BLOCK *block,
423 TALLOC_CTX *mem_ctx,
424 int num,
425 TDB_DATA data,
426 TDB_CONTEXT *names)
428 int i;
429 bool success = True;
430 struct PERF_OBJECT_TYPE *obj;
432 block->objects = (struct PERF_OBJECT_TYPE *)talloc_realloc(mem_ctx,
433 block->objects,
434 struct PERF_OBJECT_TYPE,
435 block->NumObjectTypes+1);
436 if(block->objects == NULL)
437 return False;
438 obj = &(block->objects[block->NumObjectTypes]);
439 memset((void *)&(block->objects[block->NumObjectTypes]), 0, sizeof(struct PERF_OBJECT_TYPE));
440 block->objects[block->NumObjectTypes].ObjectNameTitleIndex = num;
441 block->objects[block->NumObjectTypes].ObjectNameTitlePointer = 0;
442 block->objects[block->NumObjectTypes].ObjectHelpTitleIndex = num+1;
443 block->objects[block->NumObjectTypes].ObjectHelpTitlePointer = 0;
444 block->objects[block->NumObjectTypes].NumCounters = 0;
445 block->objects[block->NumObjectTypes].DefaultCounter = 0;
446 block->objects[block->NumObjectTypes].NumInstances = _reg_perfcount_get_numinst(num, names);
447 block->objects[block->NumObjectTypes].counters = NULL;
448 block->objects[block->NumObjectTypes].instances = NULL;
449 block->objects[block->NumObjectTypes].counter_data.ByteLength = sizeof(uint32_t);
450 block->objects[block->NumObjectTypes].counter_data.data = NULL;
451 block->objects[block->NumObjectTypes].DetailLevel = PERF_DETAIL_NOVICE;
452 block->NumObjectTypes+=1;
454 for(i = 0; i < (int)obj->NumInstances; i++) {
455 success = _reg_perfcount_add_instance(obj, mem_ctx, i, names);
458 return success;
461 /*********************************************************************
462 *********************************************************************/
464 static bool _reg_perfcount_get_counter_data(TDB_DATA key, TDB_DATA *data)
466 TDB_CONTEXT *counters;
467 char *fname;
469 fname = counters_directory(DATA_DB);
470 if (fname == NULL) {
471 return false;
474 counters = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
476 if (counters == NULL) {
477 DEBUG(1, ("reg_perfcount_get_counter_data: unable to open [%s].\n", fname));
478 TALLOC_FREE(fname);
479 return False;
481 TALLOC_FREE(fname);
483 *data = tdb_fetch(counters, key);
485 tdb_close(counters);
487 return True;
490 /*********************************************************************
491 *********************************************************************/
493 static uint32_t _reg_perfcount_get_size_field(uint32_t CounterType)
495 uint32_t retval;
497 retval = CounterType;
499 /* First mask out reserved lower 8 bits */
500 retval = retval & 0xFFFFFF00;
501 retval = retval << 22;
502 retval = retval >> 22;
504 return retval;
507 /*********************************************************************
508 *********************************************************************/
510 static uint32_t _reg_perfcount_compute_scale(int64_t data)
512 int scale = 0;
513 if(data == 0)
514 return scale;
515 while(data > 100)
517 data /= 10;
518 scale--;
520 while(data < 10)
522 data *= 10;
523 scale++;
526 return (uint32_t)scale;
529 /*********************************************************************
530 *********************************************************************/
532 static bool _reg_perfcount_get_counter_info(struct PERF_DATA_BLOCK *block,
533 TALLOC_CTX *mem_ctx,
534 int CounterIndex,
535 struct PERF_OBJECT_TYPE *obj,
536 TDB_CONTEXT *names)
538 TDB_DATA key, data;
539 char buf[PERFCOUNT_MAX_LEN];
540 size_t dsize, padding;
541 long int data32, dbuf[2];
542 int64_t data64;
543 uint32_t counter_size;
545 obj->counters[obj->NumCounters].DefaultScale = 0;
546 dbuf[0] = dbuf[1] = 0;
547 padding = 0;
549 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, CounterIndex, "type");
550 data = tdb_fetch(names, key);
551 if(data.dptr == NULL)
553 DEBUG(3, ("_reg_perfcount_get_counter_info: No type data for counter [%d].\n", CounterIndex));
554 return False;
556 memset(buf, 0, PERFCOUNT_MAX_LEN);
557 memcpy(buf, data.dptr, data.dsize);
558 obj->counters[obj->NumCounters].CounterType = atoi(buf);
559 DEBUG(10, ("_reg_perfcount_get_counter_info: Got type [%d] for counter [%d].\n",
560 obj->counters[obj->NumCounters].CounterType, CounterIndex));
561 SAFE_FREE(data.dptr);
563 /* Fetch the actual data */
564 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, CounterIndex, "");
565 _reg_perfcount_get_counter_data(key, &data);
566 if(data.dptr == NULL)
568 DEBUG(3, ("_reg_perfcount_get_counter_info: No counter data for counter [%d].\n", CounterIndex));
569 return False;
572 counter_size = _reg_perfcount_get_size_field(obj->counters[obj->NumCounters].CounterType);
574 if(counter_size == PERF_SIZE_DWORD)
576 dsize = sizeof(data32);
577 memset(buf, 0, PERFCOUNT_MAX_LEN);
578 memcpy(buf, data.dptr, data.dsize);
579 data32 = strtol(buf, NULL, 0);
580 if((obj->counters[obj->NumCounters].CounterType & 0x00000F00) == PERF_TYPE_NUMBER)
581 obj->counters[obj->NumCounters].DefaultScale = _reg_perfcount_compute_scale((int64_t)data32);
582 else
583 obj->counters[obj->NumCounters].DefaultScale = 0;
584 dbuf[0] = data32;
585 padding = (dsize - (obj->counter_data.ByteLength%dsize)) % dsize;
587 else if(counter_size == PERF_SIZE_LARGE)
589 dsize = sizeof(data64);
590 memset(buf, 0, PERFCOUNT_MAX_LEN);
591 memcpy(buf, data.dptr, data.dsize);
592 data64 = atof(buf);
593 if((obj->counters[obj->NumCounters].CounterType & 0x00000F00) == PERF_TYPE_NUMBER)
594 obj->counters[obj->NumCounters].DefaultScale = _reg_perfcount_compute_scale(data64);
595 else
596 obj->counters[obj->NumCounters].DefaultScale = 0;
597 memcpy((void *)dbuf, (const void *)&data64, dsize);
598 padding = (dsize - (obj->counter_data.ByteLength%dsize)) % dsize;
600 else /* PERF_SIZE_VARIABLE_LEN */
602 dsize = data.dsize;
603 memset(buf, 0, PERFCOUNT_MAX_LEN);
604 memcpy(buf, data.dptr, data.dsize);
606 SAFE_FREE(data.dptr);
608 obj->counter_data.ByteLength += dsize + padding;
609 obj->counter_data.data = talloc_realloc(mem_ctx,
610 obj->counter_data.data,
611 uint8_t,
612 obj->counter_data.ByteLength - sizeof(uint32_t));
613 if(obj->counter_data.data == NULL)
614 return False;
615 if(dbuf[0] != 0 || dbuf[1] != 0)
617 memcpy((void *)(obj->counter_data.data +
618 (obj->counter_data.ByteLength - (sizeof(uint32_t) + dsize))),
619 (const void *)dbuf, dsize);
621 else
623 /* Handling PERF_SIZE_VARIABLE_LEN */
624 memcpy((void *)(obj->counter_data.data +
625 (obj->counter_data.ByteLength - (sizeof(uint32_t) + dsize))),
626 (const void *)buf, dsize);
628 obj->counters[obj->NumCounters].CounterOffset = obj->counter_data.ByteLength - dsize;
629 if(obj->counters[obj->NumCounters].CounterOffset % dsize != 0)
631 DEBUG(3,("Improperly aligned counter [%d]\n", obj->NumCounters));
633 obj->counters[obj->NumCounters].CounterSize = dsize;
635 return True;
638 /*********************************************************************
639 *********************************************************************/
641 struct PERF_OBJECT_TYPE *_reg_perfcount_find_obj(struct PERF_DATA_BLOCK *block, int objind)
643 int i;
645 struct PERF_OBJECT_TYPE *obj = NULL;
647 for(i = 0; i < block->NumObjectTypes; i++)
649 if(block->objects[i].ObjectNameTitleIndex == objind)
651 obj = &(block->objects[i]);
655 return obj;
658 /*********************************************************************
659 *********************************************************************/
661 static bool _reg_perfcount_add_counter(struct PERF_DATA_BLOCK *block,
662 TALLOC_CTX *mem_ctx,
663 int num,
664 TDB_DATA data,
665 TDB_CONTEXT *names)
667 char *begin, *end, *start, *stop;
668 int parent;
669 struct PERF_OBJECT_TYPE *obj;
670 bool success = True;
671 char buf[PERFCOUNT_MAX_LEN];
673 obj = NULL;
674 memset(buf, 0, PERFCOUNT_MAX_LEN);
675 memcpy(buf, data.dptr, data.dsize);
676 begin = strchr(buf, '[');
677 end = strchr(buf, ']');
678 if(begin == NULL || end == NULL)
679 return False;
680 start = begin+1;
682 while(start < end) {
683 stop = strchr(start, ',');
684 if(stop == NULL)
685 stop = end;
686 *stop = '\0';
687 parent = atoi(start);
689 obj = _reg_perfcount_find_obj(block, parent);
690 if(obj == NULL) {
691 /* At this point we require that the parent object exist.
692 This can probably be handled better at some later time */
693 DEBUG(3, ("_reg_perfcount_add_counter: Could not find parent object [%d] for counter [%d].\n",
694 parent, num));
695 return False;
697 obj->counters = (struct PERF_COUNTER_DEFINITION *)talloc_realloc(mem_ctx,
698 obj->counters,
699 struct PERF_COUNTER_DEFINITION,
700 obj->NumCounters+1);
701 if(obj->counters == NULL)
702 return False;
703 memset((void *)&(obj->counters[obj->NumCounters]), 0, sizeof(struct PERF_COUNTER_DEFINITION));
704 obj->counters[obj->NumCounters].CounterNameTitleIndex=num;
705 obj->counters[obj->NumCounters].CounterHelpTitleIndex=num+1;
706 obj->counters[obj->NumCounters].DetailLevel = PERF_DETAIL_NOVICE;
707 obj->counters[obj->NumCounters].ByteLength = sizeof(struct PERF_COUNTER_DEFINITION);
708 success = _reg_perfcount_get_counter_info(block, mem_ctx, num, obj, names);
709 obj->NumCounters += 1;
710 start = stop + 1;
713 /* Handle case of Objects/Counters without any counter data, which would suggest
714 that the required instances are not there yet, so change NumInstances from
715 PERF_NO_INSTANCES to 0 */
717 return success;
720 /*********************************************************************
721 *********************************************************************/
723 static bool _reg_perfcount_get_instance_info(struct PERF_INSTANCE_DEFINITION *inst,
724 TALLOC_CTX *mem_ctx,
725 int instId,
726 struct PERF_OBJECT_TYPE *obj,
727 TDB_CONTEXT *names)
729 TDB_DATA key, data;
730 char buf[PERFCOUNT_MAX_LEN] = {0};
731 char temp[32] = {0};
732 smb_ucs2_t *name = NULL;
733 int pad;
735 /* First grab the instance data from the data file */
736 snprintf(temp, sizeof(temp), "i%d", instId);
737 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, obj->ObjectNameTitleIndex, temp);
738 if (!_reg_perfcount_get_counter_data(key, &data)) {
739 DEBUG(3, ("_reg_perfcount_get_counter_data failed\n"));
740 return false;
742 if(data.dptr == NULL)
744 DEBUG(3, ("_reg_perfcount_get_instance_info: No instance data for instance [%s].\n",
745 buf));
746 return False;
748 inst->counter_data.ByteLength = data.dsize + sizeof(inst->counter_data.ByteLength);
749 inst->counter_data.data = talloc_realloc(mem_ctx,
750 inst->counter_data.data,
751 uint8_t,
752 data.dsize);
753 if(inst->counter_data.data == NULL)
754 return False;
755 memset(inst->counter_data.data, 0, data.dsize);
756 memcpy(inst->counter_data.data, data.dptr, data.dsize);
757 SAFE_FREE(data.dptr);
759 /* Fetch instance name */
760 snprintf(temp, sizeof(temp), "i%dname", instId);
761 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, obj->ObjectNameTitleIndex, temp);
762 data = tdb_fetch(names, key);
763 if(data.dptr == NULL)
765 /* Not actually an error, but possibly unintended? -- just logging FYI */
766 DEBUG(3, ("_reg_perfcount_get_instance_info: No instance name for instance [%s].\n",
767 buf));
768 inst->NameLength = 0;
770 else
772 memset(buf, 0, PERFCOUNT_MAX_LEN);
773 memcpy(buf, data.dptr, MIN(PERFCOUNT_MAX_LEN-1,data.dsize));
774 buf[PERFCOUNT_MAX_LEN-1] = '\0';
775 inst->NameLength = rpcstr_push_talloc(mem_ctx, &name, buf);
776 if (inst->NameLength == (uint32_t)-1 || !name) {
777 SAFE_FREE(data.dptr);
778 return False;
780 inst->data = talloc_realloc(mem_ctx,
781 inst->data,
782 uint8_t,
783 inst->NameLength);
784 if (inst->data == NULL) {
785 SAFE_FREE(data.dptr);
786 return False;
788 memcpy(inst->data, name, inst->NameLength);
789 SAFE_FREE(data.dptr);
792 inst->ParentObjectTitleIndex = 0;
793 inst->ParentObjectTitlePointer = 0;
794 inst->UniqueID = PERF_NO_UNIQUE_ID;
795 inst->NameOffset = 6 * sizeof(uint32_t);
797 inst->ByteLength = inst->NameOffset + inst->NameLength;
798 /* Need to be aligned on a 64-bit boundary here for counter_data */
799 if((pad = (inst->ByteLength % 8)))
801 pad = 8 - pad;
802 inst->data = talloc_realloc(mem_ctx,
803 inst->data,
804 uint8_t,
805 inst->NameLength + pad);
806 memset(inst->data + inst->NameLength, 0, pad);
807 inst->ByteLength += pad;
810 return True;
813 /*********************************************************************
814 *********************************************************************/
816 static bool _reg_perfcount_add_instance(struct PERF_OBJECT_TYPE *obj,
817 TALLOC_CTX *mem_ctx,
818 int instInd,
819 TDB_CONTEXT *names)
821 struct PERF_INSTANCE_DEFINITION *inst;
823 if(obj->instances == NULL) {
824 obj->instances = talloc_realloc(mem_ctx,
825 obj->instances,
826 struct PERF_INSTANCE_DEFINITION,
827 obj->NumInstances);
829 if(obj->instances == NULL)
830 return False;
832 memset(&(obj->instances[instInd]), 0, sizeof(struct PERF_INSTANCE_DEFINITION));
833 inst = &(obj->instances[instInd]);
834 return _reg_perfcount_get_instance_info(inst, mem_ctx, instInd, obj, names);
837 /*********************************************************************
838 *********************************************************************/
840 static int _reg_perfcount_assemble_global(struct PERF_DATA_BLOCK *block,
841 TALLOC_CTX *mem_ctx,
842 int base_index,
843 TDB_CONTEXT *names)
845 bool success;
846 int i, j, retval = 0;
847 char keybuf[PERFCOUNT_MAX_LEN];
848 TDB_DATA key, data;
850 for(i = 1; i <= base_index; i++)
852 j = i*2;
853 _reg_perfcount_make_key(&key, keybuf, PERFCOUNT_MAX_LEN, j, "rel");
854 data = tdb_fetch(names, key);
855 if(data.dptr != NULL)
857 if(_reg_perfcount_isparent(data))
858 success = _reg_perfcount_add_object(block, mem_ctx, j, data, names);
859 else if(_reg_perfcount_ischild(data))
860 success = _reg_perfcount_add_counter(block, mem_ctx, j, data, names);
861 else
863 DEBUG(3, ("Bogus relationship [%s] for counter [%d].\n", data.dptr, j));
864 success = False;
866 if(success == False)
868 DEBUG(3, ("_reg_perfcount_assemble_global: Failed to add new relationship for counter [%d].\n", j));
869 retval = -1;
871 SAFE_FREE(data.dptr);
873 else
874 DEBUG(3, ("NULL relationship for counter [%d] using key [%s].\n", j, keybuf));
876 return retval;
879 /*********************************************************************
880 *********************************************************************/
882 static bool _reg_perfcount_get_64(uint64_t *retval,
883 TDB_CONTEXT *tdb,
884 int key_part1,
885 const char *key_part2)
887 TDB_DATA key, data;
888 char buf[PERFCOUNT_MAX_LEN];
890 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, key_part1, key_part2);
892 data = tdb_fetch(tdb, key);
893 if(data.dptr == NULL)
895 DEBUG(3,("_reg_perfcount_get_64: No data found for key [%s].\n", key.dptr));
896 return False;
899 memset(buf, 0, PERFCOUNT_MAX_LEN);
900 memcpy(buf, data.dptr, data.dsize);
901 SAFE_FREE(data.dptr);
903 *retval = atof(buf);
905 return True;
908 /*********************************************************************
909 *********************************************************************/
911 static bool _reg_perfcount_init_data_block_perf(struct PERF_DATA_BLOCK *block,
912 TDB_CONTEXT *names)
914 uint64_t PerfFreq, PerfTime, PerfTime100nSec;
915 TDB_CONTEXT *counters;
916 bool status = False;
917 char *fname;
919 fname = counters_directory(DATA_DB);
920 if (fname == NULL) {
921 return false;
924 counters = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
926 if (counters == NULL) {
927 DEBUG(1, ("reg_perfcount_init_data_block_perf: unable to open [%s].\n", fname));
928 TALLOC_FREE(fname);
929 return False;
931 TALLOC_FREE(fname);
933 status = _reg_perfcount_get_64(&PerfFreq, names, 0, "PerfFreq");
934 if(status == False)
936 tdb_close(counters);
937 return status;
939 memcpy((void *)&(block->PerfFreq), (const void *)&PerfFreq, sizeof(PerfFreq));
941 status = _reg_perfcount_get_64(&PerfTime, counters, 0, "PerfTime");
942 if(status == False)
944 tdb_close(counters);
945 return status;
947 memcpy((void *)&(block->PerfTime), (const void *)&PerfTime, sizeof(PerfTime));
949 status = _reg_perfcount_get_64(&PerfTime100nSec, counters, 0, "PerfTime100nSec");
950 if(status == False)
952 tdb_close(counters);
953 return status;
955 memcpy((void *)&(block->PerfTime100nSec), (const void *)&PerfTime100nSec, sizeof(PerfTime100nSec));
957 tdb_close(counters);
958 return True;
961 /*******************************************************************
962 ********************************************************************/
964 static bool make_systemtime(struct SYSTEMTIME *systime, struct tm *unixtime)
966 systime->year=unixtime->tm_year+1900;
967 systime->month=unixtime->tm_mon+1;
968 systime->dayofweek=unixtime->tm_wday;
969 systime->day=unixtime->tm_mday;
970 systime->hour=unixtime->tm_hour;
971 systime->minute=unixtime->tm_min;
972 systime->second=unixtime->tm_sec;
973 systime->milliseconds=0;
975 return True;
978 /*********************************************************************
979 *********************************************************************/
981 static bool _reg_perfcount_init_data_block(struct PERF_DATA_BLOCK *block,
982 TALLOC_CTX *mem_ctx, TDB_CONTEXT *names,
983 bool bigendian_data)
985 smb_ucs2_t *temp = NULL;
986 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
987 time_t tm;
988 size_t sz;
990 sz = rpcstr_push_talloc(tmp_ctx, &temp, "PERF");
991 if ((sz == -1) || (temp == NULL)) {
992 goto err_out;
994 memcpy(block->Signature, temp, strlen_w(temp) *2);
996 if(bigendian_data)
997 block->LittleEndian = 0;
998 else
999 block->LittleEndian = 1;
1000 block->Version = 1;
1001 block->Revision = 1;
1002 block->TotalByteLength = 0;
1003 block->NumObjectTypes = 0;
1004 block->DefaultObject = -1;
1005 block->objects = NULL;
1006 tm = time(NULL);
1007 make_systemtime(&(block->SystemTime), gmtime(&tm));
1008 _reg_perfcount_init_data_block_perf(block, names);
1010 sz = rpcstr_push_talloc(tmp_ctx, &temp, lp_netbios_name());
1011 if ((sz == -1) || (temp == NULL)) {
1012 goto err_out;
1014 block->SystemNameLength = (strlen_w(temp) * 2) + 2;
1015 block->data = talloc_zero_array(mem_ctx, uint8_t, block->SystemNameLength + (8 - (block->SystemNameLength % 8)));
1016 if (block->data == NULL) {
1017 goto err_out;
1019 memcpy(block->data, temp, block->SystemNameLength);
1020 block->SystemNameOffset = sizeof(struct PERF_DATA_BLOCK) - sizeof(block->objects) - sizeof(block->data);
1021 block->HeaderLength = block->SystemNameOffset + block->SystemNameLength;
1022 /* Make sure to adjust for 64-bit alignment for when we finish writing the system name,
1023 so that the PERF_OBJECT_TYPE struct comes out 64-bit aligned */
1024 block->HeaderLength += 8 - (block->HeaderLength % 8);
1025 talloc_free(tmp_ctx);
1027 return true;
1029 err_out:
1030 talloc_free(tmp_ctx);
1031 return false;
1034 /*********************************************************************
1035 *********************************************************************/
1037 static uint32_t _reg_perfcount_perf_data_block_fixup(struct PERF_DATA_BLOCK *block, TALLOC_CTX *mem_ctx)
1039 int obj, cnt, inst, pad, i;
1040 struct PERF_OBJECT_TYPE *object;
1041 struct PERF_INSTANCE_DEFINITION *instance;
1042 struct PERF_COUNTER_DEFINITION *counter;
1043 struct PERF_COUNTER_BLOCK *counter_data;
1044 char *temp = NULL, *src_addr, *dst_addr;
1046 block->TotalByteLength = 0;
1047 object = block->objects;
1048 for(obj = 0; obj < block->NumObjectTypes; obj++)
1050 object[obj].TotalByteLength = 0;
1051 object[obj].DefinitionLength = 0;
1052 instance = object[obj].instances;
1053 counter = object[obj].counters;
1054 for(cnt = 0; cnt < object[obj].NumCounters; cnt++)
1056 object[obj].TotalByteLength += counter[cnt].ByteLength;
1057 object[obj].DefinitionLength += counter[cnt].ByteLength;
1059 if(object[obj].NumInstances != PERF_NO_INSTANCES)
1061 for(inst = 0; inst < object[obj].NumInstances; inst++)
1063 instance = &(object[obj].instances[inst]);
1064 object[obj].TotalByteLength += instance->ByteLength;
1065 counter_data = &(instance->counter_data);
1066 counter = &(object[obj].counters[object[obj].NumCounters - 1]);
1067 counter_data->ByteLength = counter->CounterOffset + counter->CounterSize + sizeof(counter_data->ByteLength);
1068 temp = talloc_realloc(mem_ctx,
1069 temp,
1070 char,
1071 counter_data->ByteLength- sizeof(counter_data->ByteLength));
1072 if (temp == NULL) {
1073 return 0;
1075 memset(temp, 0, counter_data->ByteLength - sizeof(counter_data->ByteLength));
1076 src_addr = (char *)counter_data->data;
1077 for(i = 0; i < object[obj].NumCounters; i++)
1079 counter = &(object[obj].counters[i]);
1080 dst_addr = temp + counter->CounterOffset - sizeof(counter_data->ByteLength);
1081 memcpy(dst_addr, src_addr, counter->CounterSize);
1082 src_addr += counter->CounterSize;
1084 /* Make sure to be 64-bit aligned */
1085 if((pad = (counter_data->ByteLength % 8)))
1087 pad = 8 - pad;
1089 counter_data->data = talloc_realloc(mem_ctx,
1090 counter_data->data,
1091 uint8_t,
1092 counter_data->ByteLength - sizeof(counter_data->ByteLength) + pad);
1093 if (counter_data->data == NULL) {
1094 return 0;
1096 memset(counter_data->data, 0, counter_data->ByteLength - sizeof(counter_data->ByteLength) + pad);
1097 memcpy(counter_data->data, temp, counter_data->ByteLength - sizeof(counter_data->ByteLength));
1098 counter_data->ByteLength += pad;
1099 object[obj].TotalByteLength += counter_data->ByteLength;
1102 else
1104 /* Need to be 64-bit aligned at the end of the counter_data block, so pad counter_data to a 64-bit boundary,
1105 so that the next PERF_OBJECT_TYPE can start on a 64-bit alignment */
1106 if((pad = (object[obj].counter_data.ByteLength % 8)))
1108 pad = 8 - pad;
1109 object[obj].counter_data.data = talloc_realloc(mem_ctx,
1110 object[obj].counter_data.data,
1111 uint8_t,
1112 object[obj].counter_data.ByteLength + pad);
1113 memset((void *)(object[obj].counter_data.data + object[obj].counter_data.ByteLength), 0, pad);
1114 object[obj].counter_data.ByteLength += pad;
1116 object[obj].TotalByteLength += object[obj].counter_data.ByteLength;
1118 object[obj].HeaderLength = sizeof(*object) - (sizeof(counter) + sizeof(instance) + sizeof(struct PERF_COUNTER_BLOCK));
1119 object[obj].TotalByteLength += object[obj].HeaderLength;
1120 object[obj].DefinitionLength += object[obj].HeaderLength;
1122 block->TotalByteLength += object[obj].TotalByteLength;
1125 return block->TotalByteLength;
1128 /*********************************************************************
1129 *********************************************************************/
1131 static uint32_t reg_perfcount_get_perf_data_block(uint32_t base_index,
1132 TALLOC_CTX *mem_ctx,
1133 struct PERF_DATA_BLOCK *block,
1134 const char *object_ids,
1135 bool bigendian_data)
1137 uint32_t buffer_size = 0;
1138 char *fname;
1139 TDB_CONTEXT *names;
1140 int retval = 0;
1142 fname = counters_directory(NAMES_DB);
1143 if (fname == NULL) {
1144 return 0;
1147 names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
1149 if(names == NULL)
1151 DEBUG(1, ("reg_perfcount_get_perf_data_block: unable to open [%s].\n", fname));
1152 TALLOC_FREE(fname);
1153 return 0;
1155 TALLOC_FREE(fname);
1157 if (!_reg_perfcount_init_data_block(block, mem_ctx, names, bigendian_data)) {
1158 DEBUG(0, ("_reg_perfcount_init_data_block failed\n"));
1159 tdb_close(names);
1160 return 0;
1163 retval = _reg_perfcount_assemble_global(block, mem_ctx, base_index, names);
1165 buffer_size = _reg_perfcount_perf_data_block_fixup(block, mem_ctx);
1167 tdb_close(names);
1169 if (retval == -1) {
1170 return 0;
1173 return buffer_size + block->HeaderLength;
1176 /*******************************************************************
1177 ********************************************************************/
1179 static bool smb_io_system_time(const char *desc, prs_struct *ps, int depth, struct SYSTEMTIME *systime)
1181 if(!prs_uint16("year", ps, depth, &systime->year))
1182 return False;
1183 if(!prs_uint16("month", ps, depth, &systime->month))
1184 return False;
1185 if(!prs_uint16("dayofweek", ps, depth, &systime->dayofweek))
1186 return False;
1187 if(!prs_uint16("day", ps, depth, &systime->day))
1188 return False;
1189 if(!prs_uint16("hour", ps, depth, &systime->hour))
1190 return False;
1191 if(!prs_uint16("minute", ps, depth, &systime->minute))
1192 return False;
1193 if(!prs_uint16("second", ps, depth, &systime->second))
1194 return False;
1195 if(!prs_uint16("milliseconds", ps, depth, &systime->milliseconds))
1196 return False;
1198 return True;
1201 /*********************************************************************
1202 *********************************************************************/
1204 static bool _reg_perfcount_marshall_perf_data_block(prs_struct *ps, struct PERF_DATA_BLOCK block, int depth)
1206 int i;
1207 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_data_block");
1208 depth++;
1210 if(!prs_align(ps))
1211 return False;
1212 for(i = 0; i < 4; i++)
1214 if(!prs_uint16("Signature", ps, depth, &block.Signature[i]))
1215 return False;
1217 if(!prs_uint32("Little Endian", ps, depth, &block.LittleEndian))
1218 return False;
1219 if(!prs_uint32("Version", ps, depth, &block.Version))
1220 return False;
1221 if(!prs_uint32("Revision", ps, depth, &block.Revision))
1222 return False;
1223 if(!prs_uint32("TotalByteLength", ps, depth, &block.TotalByteLength))
1224 return False;
1225 if(!prs_uint32("HeaderLength", ps, depth, &block.HeaderLength))
1226 return False;
1227 if(!prs_uint32("NumObjectTypes", ps, depth, &block.NumObjectTypes))
1228 return False;
1229 if(!prs_uint32("DefaultObject", ps, depth, &block.DefaultObject))
1230 return False;
1231 if(!smb_io_system_time("SystemTime", ps, depth, &block.SystemTime))
1232 return False;
1233 if(!prs_uint32("Padding", ps, depth, &block.Padding))
1234 return False;
1235 if(!prs_align_uint64(ps))
1236 return False;
1237 if(!prs_uint64("PerfTime", ps, depth, &block.PerfTime))
1238 return False;
1239 if(!prs_uint64("PerfFreq", ps, depth, &block.PerfFreq))
1240 return False;
1241 if(!prs_uint64("PerfTime100nSec", ps, depth, &block.PerfTime100nSec))
1242 return False;
1243 if(!prs_uint32("SystemNameLength", ps, depth, &block.SystemNameLength))
1244 return False;
1245 if(!prs_uint32("SystemNameOffset", ps, depth, &block.SystemNameOffset))
1246 return False;
1247 /* hack to make sure we're 64-bit aligned at the end of this whole mess */
1248 if(!prs_uint8s(False, "SystemName", ps, depth, block.data,
1249 block.HeaderLength - block.SystemNameOffset))
1250 return False;
1252 return True;
1255 /*********************************************************************
1256 *********************************************************************/
1258 static bool _reg_perfcount_marshall_perf_counters(prs_struct *ps,
1259 struct PERF_OBJECT_TYPE object,
1260 int depth)
1262 int cnt;
1263 struct PERF_COUNTER_DEFINITION counter;
1265 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_counters");
1266 depth++;
1268 for(cnt = 0; cnt < object.NumCounters; cnt++)
1270 counter = object.counters[cnt];
1272 if(!prs_align(ps))
1273 return False;
1274 if(!prs_uint32("ByteLength", ps, depth, &counter.ByteLength))
1275 return False;
1276 if(!prs_uint32("CounterNameTitleIndex", ps, depth, &counter.CounterNameTitleIndex))
1277 return False;
1278 if(!prs_uint32("CounterNameTitlePointer", ps, depth, &counter.CounterNameTitlePointer))
1279 return False;
1280 if(!prs_uint32("CounterHelpTitleIndex", ps, depth, &counter.CounterHelpTitleIndex))
1281 return False;
1282 if(!prs_uint32("CounterHelpTitlePointer", ps, depth, &counter.CounterHelpTitlePointer))
1283 return False;
1284 if(!prs_uint32("DefaultScale", ps, depth, &counter.DefaultScale))
1285 return False;
1286 if(!prs_uint32("DetailLevel", ps, depth, &counter.DetailLevel))
1287 return False;
1288 if(!prs_uint32("CounterType", ps, depth, &counter.CounterType))
1289 return False;
1290 if(!prs_uint32("CounterSize", ps, depth, &counter.CounterSize))
1291 return False;
1292 if(!prs_uint32("CounterOffset", ps, depth, &counter.CounterOffset))
1293 return False;
1296 return True;
1299 /*********************************************************************
1300 *********************************************************************/
1302 static bool _reg_perfcount_marshall_perf_counter_data(prs_struct *ps,
1303 struct PERF_COUNTER_BLOCK counter_data,
1304 int depth)
1306 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_counter_data");
1307 depth++;
1309 if(!prs_align_uint64(ps))
1310 return False;
1312 if(!prs_uint32("ByteLength", ps, depth, &counter_data.ByteLength))
1313 return False;
1314 if(!prs_uint8s(False, "CounterData", ps, depth, counter_data.data, counter_data.ByteLength - sizeof(uint32_t)))
1315 return False;
1316 if(!prs_align_uint64(ps))
1317 return False;
1319 return True;
1322 /*********************************************************************
1323 *********************************************************************/
1325 static bool _reg_perfcount_marshall_perf_instances(prs_struct *ps,
1326 struct PERF_OBJECT_TYPE object,
1327 int depth)
1329 struct PERF_INSTANCE_DEFINITION instance;
1330 int inst;
1332 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_instances");
1333 depth++;
1335 for(inst = 0; inst < object.NumInstances; inst++)
1337 instance = object.instances[inst];
1339 if(!prs_align(ps))
1340 return False;
1341 if(!prs_uint32("ByteLength", ps, depth, &instance.ByteLength))
1342 return False;
1343 if(!prs_uint32("ParentObjectTitleIndex", ps, depth, &instance.ParentObjectTitleIndex))
1344 return False;
1345 if(!prs_uint32("ParentObjectTitlePointer", ps, depth, &instance.ParentObjectTitlePointer))
1346 return False;
1347 if(!prs_uint32("UniqueID", ps, depth, &instance.UniqueID))
1348 return False;
1349 if(!prs_uint32("NameOffset", ps, depth, &instance.NameOffset))
1350 return False;
1351 if(!prs_uint32("NameLength", ps, depth, &instance.NameLength))
1352 return False;
1353 if(!prs_uint8s(False, "InstanceName", ps, depth, instance.data,
1354 instance.ByteLength - instance.NameOffset))
1355 return False;
1356 if(_reg_perfcount_marshall_perf_counter_data(ps, instance.counter_data, depth) == False)
1357 return False;
1360 return True;
1363 /*********************************************************************
1364 *********************************************************************/
1366 static bool _reg_perfcount_marshall_perf_objects(prs_struct *ps, struct PERF_DATA_BLOCK block, int depth)
1368 int obj;
1370 struct PERF_OBJECT_TYPE object;
1372 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_objects");
1373 depth++;
1375 for(obj = 0; obj < block.NumObjectTypes; obj++)
1377 object = block.objects[obj];
1379 if(!prs_align(ps))
1380 return False;
1382 if(!prs_uint32("TotalByteLength", ps, depth, &object.TotalByteLength))
1383 return False;
1384 if(!prs_uint32("DefinitionLength", ps, depth, &object.DefinitionLength))
1385 return False;
1386 if(!prs_uint32("HeaderLength", ps, depth, &object.HeaderLength))
1387 return False;
1388 if(!prs_uint32("ObjectNameTitleIndex", ps, depth, &object.ObjectNameTitleIndex))
1389 return False;
1390 if(!prs_uint32("ObjectNameTitlePointer", ps, depth, &object.ObjectNameTitlePointer))
1391 return False;
1392 if(!prs_uint32("ObjectHelpTitleIndex", ps, depth, &object.ObjectHelpTitleIndex))
1393 return False;
1394 if(!prs_uint32("ObjectHelpTitlePointer", ps, depth, &object.ObjectHelpTitlePointer))
1395 return False;
1396 if(!prs_uint32("DetailLevel", ps, depth, &object.DetailLevel))
1397 return False;
1398 if(!prs_uint32("NumCounters", ps, depth, &object.NumCounters))
1399 return False;
1400 if(!prs_uint32("DefaultCounter", ps, depth, &object.DefaultCounter))
1401 return False;
1402 if(!prs_uint32("NumInstances", ps, depth, &object.NumInstances))
1403 return False;
1404 if(!prs_uint32("CodePage", ps, depth, &object.CodePage))
1405 return False;
1406 if(!prs_align_uint64(ps))
1407 return False;
1408 if(!prs_uint64("PerfTime", ps, depth, &object.PerfTime))
1409 return False;
1410 if(!prs_uint64("PerfFreq", ps, depth, &object.PerfFreq))
1411 return False;
1413 /* Now do the counters */
1414 /* If no instances, encode counter_data */
1415 /* If instances, encode instance plus counter data for each instance */
1416 if(_reg_perfcount_marshall_perf_counters(ps, object, depth) == False)
1417 return False;
1418 if(object.NumInstances == PERF_NO_INSTANCES)
1420 if(_reg_perfcount_marshall_perf_counter_data(ps, object.counter_data, depth) == False)
1421 return False;
1423 else
1425 if(_reg_perfcount_marshall_perf_instances(ps, object, depth) == False)
1426 return False;
1430 return True;
1433 /*********************************************************************
1434 *********************************************************************/
1436 WERROR reg_perfcount_get_hkpd(prs_struct *ps, uint32_t max_buf_size, uint32_t *outbuf_len, const char *object_ids)
1439 * For a detailed description of the layout of this structure,
1440 * see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/performance_data_format.asp
1442 * By 2006-11-23 this link did not work anymore, I found something
1443 * promising under
1444 * http://msdn2.microsoft.com/en-us/library/aa373105.aspx -- vl
1446 struct PERF_DATA_BLOCK block;
1447 uint32_t buffer_size, base_index;
1449 buffer_size = 0;
1450 base_index = reg_perfcount_get_base_index();
1451 ZERO_STRUCT(block);
1453 buffer_size = reg_perfcount_get_perf_data_block(base_index, ps->mem_ctx, &block, object_ids, ps->bigendian_data);
1455 if(buffer_size < max_buf_size)
1457 *outbuf_len = buffer_size;
1459 if (!_reg_perfcount_marshall_perf_data_block(ps, block, 0))
1460 return WERR_NOT_ENOUGH_MEMORY;
1462 if (!_reg_perfcount_marshall_perf_objects(ps, block, 0))
1463 return WERR_NOT_ENOUGH_MEMORY;
1465 return WERR_OK;
1467 else
1469 *outbuf_len = max_buf_size;
1470 if (!_reg_perfcount_marshall_perf_data_block(ps, block, 0))
1471 return WERR_NOT_ENOUGH_MEMORY;
1473 return WERR_INSUFFICIENT_BUFFER;