s4:libcli: split out smb_raw_negotiate_fill_transport()
[Samba.git] / source3 / registry / reg_perfcount.c
blobdb4451ecdeb0b3f61361d02318ec7b7e45d0bd28
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.
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.
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(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(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 uint32_t working_size = 0;
172 DATA_BLOB name_index, name;
173 bool ok;
175 snprintf(temp, sizeof(temp), "%d", keyval);
176 kbuf = string_tdb_data(temp);
177 dbuf = tdb_fetch(tdb, kbuf);
178 if(dbuf.dptr == NULL)
180 /* If a key isn't there, just bypass it -- this really shouldn't
181 happen unless someone's mucking around with the tdb */
182 DEBUG(3, ("_reg_perfcount_multi_sz_from_tdb: failed to find key [%s] in [%s].\n",
183 temp, tdb_name(tdb)));
184 return buffer_size;
186 /* First encode the name_index */
187 working_size = (kbuf.dsize + 1)*sizeof(uint16_t);
188 buf1 = (char *)SMB_REALLOC(buf1, buffer_size + working_size);
189 if(!buf1) {
190 buffer_size = 0;
191 return buffer_size;
193 ok = push_reg_sz(talloc_tos(), &name_index, (const char *)kbuf.dptr);
194 if (!ok) {
195 buffer_size = 0;
196 return buffer_size;
198 memcpy(buf1+buffer_size, (char *)name_index.data, working_size);
199 buffer_size += working_size;
200 /* Now encode the actual name */
201 working_size = (dbuf.dsize + 1)*sizeof(uint16_t);
202 buf1 = (char *)SMB_REALLOC(buf1, buffer_size + working_size);
203 if(!buf1) {
204 buffer_size = 0;
205 return buffer_size;
207 memset(temp, 0, sizeof(temp));
208 memcpy(temp, dbuf.dptr, dbuf.dsize);
209 SAFE_FREE(dbuf.dptr);
210 ok = push_reg_sz(talloc_tos(), &name, temp);
211 if (!ok) {
212 buffer_size = 0;
213 return buffer_size;
215 memcpy(buf1+buffer_size, (char *)name.data, working_size);
216 buffer_size += working_size;
218 *retbuf = buf1;
220 return buffer_size;
223 /*********************************************************************
224 *********************************************************************/
226 uint32_t reg_perfcount_get_counter_help(uint32_t base_index, char **retbuf)
228 char *buf1 = NULL;
229 uint32_t buffer_size = 0;
230 TDB_CONTEXT *names;
231 char *fname;
232 int i;
234 if (base_index == 0) {
235 return 0;
238 fname = counters_directory(NAMES_DB);
239 if (fname == NULL) {
240 return 0;
243 names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
245 if (names == NULL) {
246 DEBUG(1, ("reg_perfcount_get_counter_help: unable to open [%s].\n", fname));
247 TALLOC_FREE(fname);
248 return 0;
250 TALLOC_FREE(fname);
252 for(i = 1; i <= base_index; i++)
254 buffer_size = _reg_perfcount_multi_sz_from_tdb(names, (i*2)+1, retbuf, buffer_size);
256 tdb_close(names);
258 /* Now terminate the MULTI_SZ with a double unicode NULL */
259 buf1 = *retbuf;
260 buf1 = (char *)SMB_REALLOC(buf1, buffer_size + 2);
261 if(!buf1) {
262 buffer_size = 0;
263 } else {
264 buf1[buffer_size++] = '\0';
265 buf1[buffer_size++] = '\0';
268 *retbuf = buf1;
270 return buffer_size;
273 /*********************************************************************
274 *********************************************************************/
276 uint32_t reg_perfcount_get_counter_names(uint32_t base_index, char **retbuf)
278 char *buf1 = NULL;
279 uint32_t buffer_size = 0;
280 TDB_CONTEXT *names;
281 char *fname;
282 int i;
284 if (base_index == 0) {
285 return 0;
288 fname = counters_directory(NAMES_DB);
289 if (fname == NULL) {
290 return 0;
293 names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
295 if (names == NULL) {
296 DEBUG(1, ("reg_perfcount_get_counter_names: unable to open [%s].\n", fname));
297 TALLOC_FREE(fname);
298 return 0;
300 TALLOC_FREE(fname);
302 buffer_size = _reg_perfcount_multi_sz_from_tdb(names, 1, retbuf, buffer_size);
304 for(i = 1; i <= base_index; i++)
306 buffer_size = _reg_perfcount_multi_sz_from_tdb(names, i*2, retbuf, buffer_size);
308 tdb_close(names);
310 /* Now terminate the MULTI_SZ with a double unicode NULL */
311 buf1 = *retbuf;
312 buf1 = (char *)SMB_REALLOC(buf1, buffer_size + 2);
313 if(!buf1) {
314 buffer_size = 0;
315 } else {
316 buf1[buffer_size++] = '\0';
317 buf1[buffer_size++] = '\0';
320 *retbuf=buf1;
322 return buffer_size;
325 /*********************************************************************
326 *********************************************************************/
328 static void _reg_perfcount_make_key(TDB_DATA *key,
329 char *buf,
330 int buflen,
331 int key_part1,
332 const char *key_part2)
334 memset(buf, 0, buflen);
335 if(key_part2 != NULL)
336 snprintf(buf, buflen,"%d%s", key_part1, key_part2);
337 else
338 snprintf(buf, buflen, "%d", key_part1);
340 *key = string_tdb_data(buf);
342 return;
345 /*********************************************************************
346 *********************************************************************/
348 static bool _reg_perfcount_isparent(TDB_DATA data)
350 if(data.dsize > 0)
352 if(data.dptr[0] == 'p')
353 return True;
354 else
355 return False;
357 return False;
360 /*********************************************************************
361 *********************************************************************/
363 static bool _reg_perfcount_ischild(TDB_DATA data)
365 if(data.dsize > 0)
367 if(data.dptr[0] == 'c')
368 return True;
369 else
370 return False;
372 return False;
375 /*********************************************************************
376 *********************************************************************/
378 static uint32_t _reg_perfcount_get_numinst(int objInd, TDB_CONTEXT *names)
380 TDB_DATA key, data;
381 char buf[PERFCOUNT_MAX_LEN];
383 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, objInd, "inst");
384 data = tdb_fetch(names, key);
386 if(data.dptr == NULL)
387 return (uint32_t)PERF_NO_INSTANCES;
389 memset(buf, 0, PERFCOUNT_MAX_LEN);
390 memcpy(buf, data.dptr, data.dsize);
391 SAFE_FREE(data.dptr);
392 return (uint32_t)atoi(buf);
395 /*********************************************************************
396 *********************************************************************/
398 static bool _reg_perfcount_add_instance(struct PERF_OBJECT_TYPE *obj,
399 TALLOC_CTX *mem_ctx,
400 int instInd,
401 TDB_CONTEXT *names);
403 static bool _reg_perfcount_add_object(struct PERF_DATA_BLOCK *block,
404 TALLOC_CTX *mem_ctx,
405 int num,
406 TDB_DATA data,
407 TDB_CONTEXT *names)
409 int i;
410 bool success = True;
411 struct PERF_OBJECT_TYPE *obj;
413 block->objects = (struct PERF_OBJECT_TYPE *)talloc_realloc(mem_ctx,
414 block->objects,
415 struct PERF_OBJECT_TYPE,
416 block->NumObjectTypes+1);
417 if(block->objects == NULL)
418 return False;
419 obj = &(block->objects[block->NumObjectTypes]);
420 memset((void *)&(block->objects[block->NumObjectTypes]), 0, sizeof(struct PERF_OBJECT_TYPE));
421 block->objects[block->NumObjectTypes].ObjectNameTitleIndex = num;
422 block->objects[block->NumObjectTypes].ObjectNameTitlePointer = 0;
423 block->objects[block->NumObjectTypes].ObjectHelpTitleIndex = num+1;
424 block->objects[block->NumObjectTypes].ObjectHelpTitlePointer = 0;
425 block->objects[block->NumObjectTypes].NumCounters = 0;
426 block->objects[block->NumObjectTypes].DefaultCounter = 0;
427 block->objects[block->NumObjectTypes].NumInstances = _reg_perfcount_get_numinst(num, names);
428 block->objects[block->NumObjectTypes].counters = NULL;
429 block->objects[block->NumObjectTypes].instances = NULL;
430 block->objects[block->NumObjectTypes].counter_data.ByteLength = sizeof(uint32_t);
431 block->objects[block->NumObjectTypes].counter_data.data = NULL;
432 block->objects[block->NumObjectTypes].DetailLevel = PERF_DETAIL_NOVICE;
433 block->NumObjectTypes+=1;
435 for(i = 0; i < (int)obj->NumInstances; i++) {
436 success = _reg_perfcount_add_instance(obj, mem_ctx, i, names);
439 return success;
442 /*********************************************************************
443 *********************************************************************/
445 static bool _reg_perfcount_get_counter_data(TDB_DATA key, TDB_DATA *data)
447 TDB_CONTEXT *counters;
448 char *fname;
450 fname = counters_directory(DATA_DB);
451 if (fname == NULL) {
452 return false;
455 counters = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
457 if (counters == NULL) {
458 DEBUG(1, ("reg_perfcount_get_counter_data: unable to open [%s].\n", fname));
459 TALLOC_FREE(fname);
460 return False;
462 TALLOC_FREE(fname);
464 *data = tdb_fetch(counters, key);
466 tdb_close(counters);
468 return True;
471 /*********************************************************************
472 *********************************************************************/
474 static uint32_t _reg_perfcount_get_size_field(uint32_t CounterType)
476 uint32_t retval;
478 retval = CounterType;
480 /* First mask out reserved lower 8 bits */
481 retval = retval & 0xFFFFFF00;
482 retval = retval << 22;
483 retval = retval >> 22;
485 return retval;
488 /*********************************************************************
489 *********************************************************************/
491 static uint32_t _reg_perfcount_compute_scale(int64_t data)
493 int scale = 0;
494 if(data == 0)
495 return scale;
496 while(data > 100)
498 data /= 10;
499 scale--;
501 while(data < 10)
503 data *= 10;
504 scale++;
507 return (uint32_t)scale;
510 /*********************************************************************
511 *********************************************************************/
513 static bool _reg_perfcount_get_counter_info(struct PERF_DATA_BLOCK *block,
514 TALLOC_CTX *mem_ctx,
515 int CounterIndex,
516 struct PERF_OBJECT_TYPE *obj,
517 TDB_CONTEXT *names)
519 TDB_DATA key, data;
520 char buf[PERFCOUNT_MAX_LEN];
521 size_t dsize, padding;
522 long int data32, dbuf[2];
523 int64_t data64;
524 uint32_t counter_size;
526 obj->counters[obj->NumCounters].DefaultScale = 0;
527 dbuf[0] = dbuf[1] = 0;
528 padding = 0;
530 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, CounterIndex, "type");
531 data = tdb_fetch(names, key);
532 if(data.dptr == NULL)
534 DEBUG(3, ("_reg_perfcount_get_counter_info: No type data for counter [%d].\n", CounterIndex));
535 return False;
537 memset(buf, 0, PERFCOUNT_MAX_LEN);
538 memcpy(buf, data.dptr, data.dsize);
539 obj->counters[obj->NumCounters].CounterType = atoi(buf);
540 DEBUG(10, ("_reg_perfcount_get_counter_info: Got type [%d] for counter [%d].\n",
541 obj->counters[obj->NumCounters].CounterType, CounterIndex));
542 SAFE_FREE(data.dptr);
544 /* Fetch the actual data */
545 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, CounterIndex, "");
546 _reg_perfcount_get_counter_data(key, &data);
547 if(data.dptr == NULL)
549 DEBUG(3, ("_reg_perfcount_get_counter_info: No counter data for counter [%d].\n", CounterIndex));
550 return False;
553 counter_size = _reg_perfcount_get_size_field(obj->counters[obj->NumCounters].CounterType);
555 if(counter_size == PERF_SIZE_DWORD)
557 dsize = sizeof(data32);
558 memset(buf, 0, PERFCOUNT_MAX_LEN);
559 memcpy(buf, data.dptr, data.dsize);
560 data32 = strtol(buf, NULL, 0);
561 if((obj->counters[obj->NumCounters].CounterType & 0x00000F00) == PERF_TYPE_NUMBER)
562 obj->counters[obj->NumCounters].DefaultScale = _reg_perfcount_compute_scale((int64_t)data32);
563 else
564 obj->counters[obj->NumCounters].DefaultScale = 0;
565 dbuf[0] = data32;
566 padding = (dsize - (obj->counter_data.ByteLength%dsize)) % dsize;
568 else if(counter_size == PERF_SIZE_LARGE)
570 dsize = sizeof(data64);
571 memset(buf, 0, PERFCOUNT_MAX_LEN);
572 memcpy(buf, data.dptr, data.dsize);
573 data64 = atof(buf);
574 if((obj->counters[obj->NumCounters].CounterType & 0x00000F00) == PERF_TYPE_NUMBER)
575 obj->counters[obj->NumCounters].DefaultScale = _reg_perfcount_compute_scale(data64);
576 else
577 obj->counters[obj->NumCounters].DefaultScale = 0;
578 memcpy((void *)dbuf, (const void *)&data64, dsize);
579 padding = (dsize - (obj->counter_data.ByteLength%dsize)) % dsize;
581 else /* PERF_SIZE_VARIABLE_LEN */
583 dsize = data.dsize;
584 memset(buf, 0, PERFCOUNT_MAX_LEN);
585 memcpy(buf, data.dptr, data.dsize);
587 SAFE_FREE(data.dptr);
589 obj->counter_data.ByteLength += dsize + padding;
590 obj->counter_data.data = talloc_realloc(mem_ctx,
591 obj->counter_data.data,
592 uint8_t,
593 obj->counter_data.ByteLength - sizeof(uint32_t));
594 if(obj->counter_data.data == NULL)
595 return False;
596 if(dbuf[0] != 0 || dbuf[1] != 0)
598 memcpy((void *)(obj->counter_data.data +
599 (obj->counter_data.ByteLength - (sizeof(uint32_t) + dsize))),
600 (const void *)dbuf, dsize);
602 else
604 /* Handling PERF_SIZE_VARIABLE_LEN */
605 memcpy((void *)(obj->counter_data.data +
606 (obj->counter_data.ByteLength - (sizeof(uint32_t) + dsize))),
607 (const void *)buf, dsize);
609 obj->counters[obj->NumCounters].CounterOffset = obj->counter_data.ByteLength - dsize;
610 if(obj->counters[obj->NumCounters].CounterOffset % dsize != 0)
612 DEBUG(3,("Improperly aligned counter [%d]\n", obj->NumCounters));
614 obj->counters[obj->NumCounters].CounterSize = dsize;
616 return True;
619 /*********************************************************************
620 *********************************************************************/
622 struct PERF_OBJECT_TYPE *_reg_perfcount_find_obj(struct PERF_DATA_BLOCK *block, int objind)
624 int i;
626 struct PERF_OBJECT_TYPE *obj = NULL;
628 for(i = 0; i < block->NumObjectTypes; i++)
630 if(block->objects[i].ObjectNameTitleIndex == objind)
632 obj = &(block->objects[i]);
636 return obj;
639 /*********************************************************************
640 *********************************************************************/
642 static bool _reg_perfcount_add_counter(struct PERF_DATA_BLOCK *block,
643 TALLOC_CTX *mem_ctx,
644 int num,
645 TDB_DATA data,
646 TDB_CONTEXT *names)
648 char *begin, *end, *start, *stop;
649 int parent;
650 struct PERF_OBJECT_TYPE *obj;
651 bool success = True;
652 char buf[PERFCOUNT_MAX_LEN];
654 obj = NULL;
655 memset(buf, 0, PERFCOUNT_MAX_LEN);
656 memcpy(buf, data.dptr, data.dsize);
657 begin = strchr(buf, '[');
658 end = strchr(buf, ']');
659 if(begin == NULL || end == NULL)
660 return False;
661 start = begin+1;
663 while(start < end) {
664 stop = strchr(start, ',');
665 if(stop == NULL)
666 stop = end;
667 *stop = '\0';
668 parent = atoi(start);
670 obj = _reg_perfcount_find_obj(block, parent);
671 if(obj == NULL) {
672 /* At this point we require that the parent object exist.
673 This can probably be handled better at some later time */
674 DEBUG(3, ("_reg_perfcount_add_counter: Could not find parent object [%d] for counter [%d].\n",
675 parent, num));
676 return False;
678 obj->counters = (struct PERF_COUNTER_DEFINITION *)talloc_realloc(mem_ctx,
679 obj->counters,
680 struct PERF_COUNTER_DEFINITION,
681 obj->NumCounters+1);
682 if(obj->counters == NULL)
683 return False;
684 memset((void *)&(obj->counters[obj->NumCounters]), 0, sizeof(struct PERF_COUNTER_DEFINITION));
685 obj->counters[obj->NumCounters].CounterNameTitleIndex=num;
686 obj->counters[obj->NumCounters].CounterHelpTitleIndex=num+1;
687 obj->counters[obj->NumCounters].DetailLevel = PERF_DETAIL_NOVICE;
688 obj->counters[obj->NumCounters].ByteLength = sizeof(struct PERF_COUNTER_DEFINITION);
689 success = _reg_perfcount_get_counter_info(block, mem_ctx, num, obj, names);
690 obj->NumCounters += 1;
691 start = stop + 1;
694 /* Handle case of Objects/Counters without any counter data, which would suggest
695 that the required instances are not there yet, so change NumInstances from
696 PERF_NO_INSTANCES to 0 */
698 return success;
701 /*********************************************************************
702 *********************************************************************/
704 static bool _reg_perfcount_get_instance_info(struct PERF_INSTANCE_DEFINITION *inst,
705 TALLOC_CTX *mem_ctx,
706 int instId,
707 struct PERF_OBJECT_TYPE *obj,
708 TDB_CONTEXT *names)
710 TDB_DATA key, data;
711 char buf[PERFCOUNT_MAX_LEN] = {0};
712 char temp[32] = {0};
713 smb_ucs2_t *name = NULL;
714 int pad;
716 /* First grab the instance data from the data file */
717 snprintf(temp, sizeof(temp), "i%d", instId);
718 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, obj->ObjectNameTitleIndex, temp);
719 if (!_reg_perfcount_get_counter_data(key, &data)) {
720 DEBUG(3, ("_reg_perfcount_get_counter_data failed\n"));
721 return false;
723 if(data.dptr == NULL)
725 DEBUG(3, ("_reg_perfcount_get_instance_info: No instance data for instance [%s].\n",
726 buf));
727 return False;
729 inst->counter_data.ByteLength = data.dsize + sizeof(inst->counter_data.ByteLength);
730 inst->counter_data.data = talloc_realloc(mem_ctx,
731 inst->counter_data.data,
732 uint8_t,
733 data.dsize);
734 if(inst->counter_data.data == NULL)
735 return False;
736 memset(inst->counter_data.data, 0, data.dsize);
737 memcpy(inst->counter_data.data, data.dptr, data.dsize);
738 SAFE_FREE(data.dptr);
740 /* Fetch instance name */
741 snprintf(temp, sizeof(temp), "i%dname", instId);
742 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, obj->ObjectNameTitleIndex, temp);
743 data = tdb_fetch(names, key);
744 if(data.dptr == NULL)
746 /* Not actually an error, but possibly unintended? -- just logging FYI */
747 DEBUG(3, ("_reg_perfcount_get_instance_info: No instance name for instance [%s].\n",
748 buf));
749 inst->NameLength = 0;
751 else
753 memset(buf, 0, PERFCOUNT_MAX_LEN);
754 memcpy(buf, data.dptr, MIN(PERFCOUNT_MAX_LEN-1,data.dsize));
755 buf[PERFCOUNT_MAX_LEN-1] = '\0';
756 inst->NameLength = rpcstr_push_talloc(mem_ctx, &name, buf);
757 if (inst->NameLength == (uint32_t)-1 || !name) {
758 SAFE_FREE(data.dptr);
759 return False;
761 inst->data = talloc_realloc(mem_ctx,
762 inst->data,
763 uint8_t,
764 inst->NameLength);
765 if (inst->data == NULL) {
766 SAFE_FREE(data.dptr);
767 return False;
769 memcpy(inst->data, name, inst->NameLength);
770 SAFE_FREE(data.dptr);
773 inst->ParentObjectTitleIndex = 0;
774 inst->ParentObjectTitlePointer = 0;
775 inst->UniqueID = PERF_NO_UNIQUE_ID;
776 inst->NameOffset = 6 * sizeof(uint32_t);
778 inst->ByteLength = inst->NameOffset + inst->NameLength;
779 /* Need to be aligned on a 64-bit boundary here for counter_data */
780 if((pad = (inst->ByteLength % 8)))
782 pad = 8 - pad;
783 inst->data = talloc_realloc(mem_ctx,
784 inst->data,
785 uint8_t,
786 inst->NameLength + pad);
787 memset(inst->data + inst->NameLength, 0, pad);
788 inst->ByteLength += pad;
791 return True;
794 /*********************************************************************
795 *********************************************************************/
797 static bool _reg_perfcount_add_instance(struct PERF_OBJECT_TYPE *obj,
798 TALLOC_CTX *mem_ctx,
799 int instInd,
800 TDB_CONTEXT *names)
802 struct PERF_INSTANCE_DEFINITION *inst;
804 if(obj->instances == NULL) {
805 obj->instances = talloc_realloc(mem_ctx,
806 obj->instances,
807 struct PERF_INSTANCE_DEFINITION,
808 obj->NumInstances);
810 if(obj->instances == NULL)
811 return False;
813 memset(&(obj->instances[instInd]), 0, sizeof(struct PERF_INSTANCE_DEFINITION));
814 inst = &(obj->instances[instInd]);
815 return _reg_perfcount_get_instance_info(inst, mem_ctx, instInd, obj, names);
818 /*********************************************************************
819 *********************************************************************/
821 static int _reg_perfcount_assemble_global(struct PERF_DATA_BLOCK *block,
822 TALLOC_CTX *mem_ctx,
823 int base_index,
824 TDB_CONTEXT *names)
826 bool success;
827 int i, j, retval = 0;
828 char keybuf[PERFCOUNT_MAX_LEN];
829 TDB_DATA key, data;
831 for(i = 1; i <= base_index; i++)
833 j = i*2;
834 _reg_perfcount_make_key(&key, keybuf, PERFCOUNT_MAX_LEN, j, "rel");
835 data = tdb_fetch(names, key);
836 if(data.dptr != NULL)
838 if(_reg_perfcount_isparent(data))
839 success = _reg_perfcount_add_object(block, mem_ctx, j, data, names);
840 else if(_reg_perfcount_ischild(data))
841 success = _reg_perfcount_add_counter(block, mem_ctx, j, data, names);
842 else
844 DEBUG(3, ("Bogus relationship [%s] for counter [%d].\n", data.dptr, j));
845 success = False;
847 if(success == False)
849 DEBUG(3, ("_reg_perfcount_assemble_global: Failed to add new relationship for counter [%d].\n", j));
850 retval = -1;
852 SAFE_FREE(data.dptr);
854 else
855 DEBUG(3, ("NULL relationship for counter [%d] using key [%s].\n", j, keybuf));
857 return retval;
860 /*********************************************************************
861 *********************************************************************/
863 static bool _reg_perfcount_get_64(uint64_t *retval,
864 TDB_CONTEXT *tdb,
865 int key_part1,
866 const char *key_part2)
868 TDB_DATA key, data;
869 char buf[PERFCOUNT_MAX_LEN];
871 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, key_part1, key_part2);
873 data = tdb_fetch(tdb, key);
874 if(data.dptr == NULL)
876 DEBUG(3,("_reg_perfcount_get_64: No data found for key [%s].\n", key.dptr));
877 return False;
880 memset(buf, 0, PERFCOUNT_MAX_LEN);
881 memcpy(buf, data.dptr, data.dsize);
882 SAFE_FREE(data.dptr);
884 *retval = atof(buf);
886 return True;
889 /*********************************************************************
890 *********************************************************************/
892 static bool _reg_perfcount_init_data_block_perf(struct PERF_DATA_BLOCK *block,
893 TDB_CONTEXT *names)
895 uint64_t PerfFreq, PerfTime, PerfTime100nSec;
896 TDB_CONTEXT *counters;
897 bool status = False;
898 char *fname;
900 fname = counters_directory(DATA_DB);
901 if (fname == NULL) {
902 return false;
905 counters = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
907 if (counters == NULL) {
908 DEBUG(1, ("reg_perfcount_init_data_block_perf: unable to open [%s].\n", fname));
909 TALLOC_FREE(fname);
910 return False;
912 TALLOC_FREE(fname);
914 status = _reg_perfcount_get_64(&PerfFreq, names, 0, "PerfFreq");
915 if(status == False)
917 tdb_close(counters);
918 return status;
920 memcpy((void *)&(block->PerfFreq), (const void *)&PerfFreq, sizeof(PerfFreq));
922 status = _reg_perfcount_get_64(&PerfTime, counters, 0, "PerfTime");
923 if(status == False)
925 tdb_close(counters);
926 return status;
928 memcpy((void *)&(block->PerfTime), (const void *)&PerfTime, sizeof(PerfTime));
930 status = _reg_perfcount_get_64(&PerfTime100nSec, counters, 0, "PerfTime100nSec");
931 if(status == False)
933 tdb_close(counters);
934 return status;
936 memcpy((void *)&(block->PerfTime100nSec), (const void *)&PerfTime100nSec, sizeof(PerfTime100nSec));
938 tdb_close(counters);
939 return True;
942 /*******************************************************************
943 ********************************************************************/
945 static bool make_systemtime(struct SYSTEMTIME *systime, struct tm *unixtime)
947 systime->year=unixtime->tm_year+1900;
948 systime->month=unixtime->tm_mon+1;
949 systime->dayofweek=unixtime->tm_wday;
950 systime->day=unixtime->tm_mday;
951 systime->hour=unixtime->tm_hour;
952 systime->minute=unixtime->tm_min;
953 systime->second=unixtime->tm_sec;
954 systime->milliseconds=0;
956 return True;
959 /*********************************************************************
960 *********************************************************************/
962 static bool _reg_perfcount_init_data_block(struct PERF_DATA_BLOCK *block,
963 TALLOC_CTX *mem_ctx, TDB_CONTEXT *names,
964 bool bigendian_data)
966 smb_ucs2_t *temp = NULL;
967 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
968 time_t tm;
969 size_t sz;
971 sz = rpcstr_push_talloc(tmp_ctx, &temp, "PERF");
972 if ((sz == -1) || (temp == NULL)) {
973 goto err_out;
975 memcpy(block->Signature, temp, strlen_w(temp) *2);
977 if(bigendian_data)
978 block->LittleEndian = 0;
979 else
980 block->LittleEndian = 1;
981 block->Version = 1;
982 block->Revision = 1;
983 block->TotalByteLength = 0;
984 block->NumObjectTypes = 0;
985 block->DefaultObject = -1;
986 block->objects = NULL;
987 tm = time(NULL);
988 make_systemtime(&(block->SystemTime), gmtime(&tm));
989 _reg_perfcount_init_data_block_perf(block, names);
991 sz = rpcstr_push_talloc(tmp_ctx, &temp, lp_netbios_name());
992 if ((sz == -1) || (temp == NULL)) {
993 goto err_out;
995 block->SystemNameLength = (strlen_w(temp) * 2) + 2;
996 block->data = talloc_zero_array(mem_ctx, uint8_t, block->SystemNameLength + (8 - (block->SystemNameLength % 8)));
997 if (block->data == NULL) {
998 goto err_out;
1000 memcpy(block->data, temp, block->SystemNameLength);
1001 block->SystemNameOffset = sizeof(struct PERF_DATA_BLOCK) - sizeof(block->objects) - sizeof(block->data);
1002 block->HeaderLength = block->SystemNameOffset + block->SystemNameLength;
1003 /* Make sure to adjust for 64-bit alignment for when we finish writing the system name,
1004 so that the PERF_OBJECT_TYPE struct comes out 64-bit aligned */
1005 block->HeaderLength += 8 - (block->HeaderLength % 8);
1006 talloc_free(tmp_ctx);
1008 return true;
1010 err_out:
1011 talloc_free(tmp_ctx);
1012 return false;
1015 /*********************************************************************
1016 *********************************************************************/
1018 static uint32_t _reg_perfcount_perf_data_block_fixup(struct PERF_DATA_BLOCK *block, TALLOC_CTX *mem_ctx)
1020 int obj, cnt, inst, pad, i;
1021 struct PERF_OBJECT_TYPE *object;
1022 struct PERF_INSTANCE_DEFINITION *instance;
1023 struct PERF_COUNTER_DEFINITION *counter;
1024 struct PERF_COUNTER_BLOCK *counter_data;
1025 char *temp = NULL, *src_addr, *dst_addr;
1027 block->TotalByteLength = 0;
1028 object = block->objects;
1029 for(obj = 0; obj < block->NumObjectTypes; obj++)
1031 object[obj].TotalByteLength = 0;
1032 object[obj].DefinitionLength = 0;
1033 instance = object[obj].instances;
1034 counter = object[obj].counters;
1035 for(cnt = 0; cnt < object[obj].NumCounters; cnt++)
1037 object[obj].TotalByteLength += counter[cnt].ByteLength;
1038 object[obj].DefinitionLength += counter[cnt].ByteLength;
1040 if(object[obj].NumInstances != PERF_NO_INSTANCES)
1042 for(inst = 0; inst < object[obj].NumInstances; inst++)
1044 instance = &(object[obj].instances[inst]);
1045 object[obj].TotalByteLength += instance->ByteLength;
1046 counter_data = &(instance->counter_data);
1047 counter = &(object[obj].counters[object[obj].NumCounters - 1]);
1048 counter_data->ByteLength = counter->CounterOffset + counter->CounterSize + sizeof(counter_data->ByteLength);
1049 temp = talloc_realloc(mem_ctx,
1050 temp,
1051 char,
1052 counter_data->ByteLength- sizeof(counter_data->ByteLength));
1053 if (temp == NULL) {
1054 return 0;
1056 memset(temp, 0, counter_data->ByteLength - sizeof(counter_data->ByteLength));
1057 src_addr = (char *)counter_data->data;
1058 for(i = 0; i < object[obj].NumCounters; i++)
1060 counter = &(object[obj].counters[i]);
1061 dst_addr = temp + counter->CounterOffset - sizeof(counter_data->ByteLength);
1062 memcpy(dst_addr, src_addr, counter->CounterSize);
1063 src_addr += counter->CounterSize;
1065 /* Make sure to be 64-bit aligned */
1066 if((pad = (counter_data->ByteLength % 8)))
1068 pad = 8 - pad;
1070 counter_data->data = talloc_realloc(mem_ctx,
1071 counter_data->data,
1072 uint8_t,
1073 counter_data->ByteLength - sizeof(counter_data->ByteLength) + pad);
1074 if (counter_data->data == NULL) {
1075 return 0;
1077 memset(counter_data->data, 0, counter_data->ByteLength - sizeof(counter_data->ByteLength) + pad);
1078 memcpy(counter_data->data, temp, counter_data->ByteLength - sizeof(counter_data->ByteLength));
1079 counter_data->ByteLength += pad;
1080 object[obj].TotalByteLength += counter_data->ByteLength;
1083 else
1085 /* Need to be 64-bit aligned at the end of the counter_data block, so pad counter_data to a 64-bit boundary,
1086 so that the next PERF_OBJECT_TYPE can start on a 64-bit alignment */
1087 if((pad = (object[obj].counter_data.ByteLength % 8)))
1089 pad = 8 - pad;
1090 object[obj].counter_data.data = talloc_realloc(mem_ctx,
1091 object[obj].counter_data.data,
1092 uint8_t,
1093 object[obj].counter_data.ByteLength + pad);
1094 memset((void *)(object[obj].counter_data.data + object[obj].counter_data.ByteLength), 0, pad);
1095 object[obj].counter_data.ByteLength += pad;
1097 object[obj].TotalByteLength += object[obj].counter_data.ByteLength;
1099 object[obj].HeaderLength = sizeof(*object) - (sizeof(counter) + sizeof(instance) + sizeof(struct PERF_COUNTER_BLOCK));
1100 object[obj].TotalByteLength += object[obj].HeaderLength;
1101 object[obj].DefinitionLength += object[obj].HeaderLength;
1103 block->TotalByteLength += object[obj].TotalByteLength;
1106 return block->TotalByteLength;
1109 /*********************************************************************
1110 *********************************************************************/
1112 static uint32_t reg_perfcount_get_perf_data_block(uint32_t base_index,
1113 TALLOC_CTX *mem_ctx,
1114 struct PERF_DATA_BLOCK *block,
1115 const char *object_ids,
1116 bool bigendian_data)
1118 uint32_t buffer_size = 0;
1119 char *fname;
1120 TDB_CONTEXT *names;
1121 int retval = 0;
1123 fname = counters_directory(NAMES_DB);
1124 if (fname == NULL) {
1125 return 0;
1128 names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
1130 if(names == NULL)
1132 DEBUG(1, ("reg_perfcount_get_perf_data_block: unable to open [%s].\n", fname));
1133 TALLOC_FREE(fname);
1134 return 0;
1136 TALLOC_FREE(fname);
1138 if (!_reg_perfcount_init_data_block(block, mem_ctx, names, bigendian_data)) {
1139 DEBUG(0, ("_reg_perfcount_init_data_block failed\n"));
1140 tdb_close(names);
1141 return 0;
1144 retval = _reg_perfcount_assemble_global(block, mem_ctx, base_index, names);
1146 buffer_size = _reg_perfcount_perf_data_block_fixup(block, mem_ctx);
1148 tdb_close(names);
1150 if (retval == -1) {
1151 return 0;
1154 return buffer_size + block->HeaderLength;
1157 /*******************************************************************
1158 ********************************************************************/
1160 static bool smb_io_system_time(const char *desc, prs_struct *ps, int depth, struct SYSTEMTIME *systime)
1162 if(!prs_uint16("year", ps, depth, &systime->year))
1163 return False;
1164 if(!prs_uint16("month", ps, depth, &systime->month))
1165 return False;
1166 if(!prs_uint16("dayofweek", ps, depth, &systime->dayofweek))
1167 return False;
1168 if(!prs_uint16("day", ps, depth, &systime->day))
1169 return False;
1170 if(!prs_uint16("hour", ps, depth, &systime->hour))
1171 return False;
1172 if(!prs_uint16("minute", ps, depth, &systime->minute))
1173 return False;
1174 if(!prs_uint16("second", ps, depth, &systime->second))
1175 return False;
1176 if(!prs_uint16("milliseconds", ps, depth, &systime->milliseconds))
1177 return False;
1179 return True;
1182 /*********************************************************************
1183 *********************************************************************/
1185 static bool _reg_perfcount_marshall_perf_data_block(prs_struct *ps, struct PERF_DATA_BLOCK block, int depth)
1187 int i;
1188 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_data_block");
1189 depth++;
1191 if(!prs_align(ps))
1192 return False;
1193 for(i = 0; i < 4; i++)
1195 if(!prs_uint16("Signature", ps, depth, &block.Signature[i]))
1196 return False;
1198 if(!prs_uint32("Little Endian", ps, depth, &block.LittleEndian))
1199 return False;
1200 if(!prs_uint32("Version", ps, depth, &block.Version))
1201 return False;
1202 if(!prs_uint32("Revision", ps, depth, &block.Revision))
1203 return False;
1204 if(!prs_uint32("TotalByteLength", ps, depth, &block.TotalByteLength))
1205 return False;
1206 if(!prs_uint32("HeaderLength", ps, depth, &block.HeaderLength))
1207 return False;
1208 if(!prs_uint32("NumObjectTypes", ps, depth, &block.NumObjectTypes))
1209 return False;
1210 if(!prs_uint32("DefaultObject", ps, depth, &block.DefaultObject))
1211 return False;
1212 if(!smb_io_system_time("SystemTime", ps, depth, &block.SystemTime))
1213 return False;
1214 if(!prs_uint32("Padding", ps, depth, &block.Padding))
1215 return False;
1216 if(!prs_align_uint64(ps))
1217 return False;
1218 if(!prs_uint64("PerfTime", ps, depth, &block.PerfTime))
1219 return False;
1220 if(!prs_uint64("PerfFreq", ps, depth, &block.PerfFreq))
1221 return False;
1222 if(!prs_uint64("PerfTime100nSec", ps, depth, &block.PerfTime100nSec))
1223 return False;
1224 if(!prs_uint32("SystemNameLength", ps, depth, &block.SystemNameLength))
1225 return False;
1226 if(!prs_uint32("SystemNameOffset", ps, depth, &block.SystemNameOffset))
1227 return False;
1228 /* hack to make sure we're 64-bit aligned at the end of this whole mess */
1229 if(!prs_uint8s(False, "SystemName", ps, depth, block.data,
1230 block.HeaderLength - block.SystemNameOffset))
1231 return False;
1233 return True;
1236 /*********************************************************************
1237 *********************************************************************/
1239 static bool _reg_perfcount_marshall_perf_counters(prs_struct *ps,
1240 struct PERF_OBJECT_TYPE object,
1241 int depth)
1243 int cnt;
1244 struct PERF_COUNTER_DEFINITION counter;
1246 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_counters");
1247 depth++;
1249 for(cnt = 0; cnt < object.NumCounters; cnt++)
1251 counter = object.counters[cnt];
1253 if(!prs_align(ps))
1254 return False;
1255 if(!prs_uint32("ByteLength", ps, depth, &counter.ByteLength))
1256 return False;
1257 if(!prs_uint32("CounterNameTitleIndex", ps, depth, &counter.CounterNameTitleIndex))
1258 return False;
1259 if(!prs_uint32("CounterNameTitlePointer", ps, depth, &counter.CounterNameTitlePointer))
1260 return False;
1261 if(!prs_uint32("CounterHelpTitleIndex", ps, depth, &counter.CounterHelpTitleIndex))
1262 return False;
1263 if(!prs_uint32("CounterHelpTitlePointer", ps, depth, &counter.CounterHelpTitlePointer))
1264 return False;
1265 if(!prs_uint32("DefaultScale", ps, depth, &counter.DefaultScale))
1266 return False;
1267 if(!prs_uint32("DetailLevel", ps, depth, &counter.DetailLevel))
1268 return False;
1269 if(!prs_uint32("CounterType", ps, depth, &counter.CounterType))
1270 return False;
1271 if(!prs_uint32("CounterSize", ps, depth, &counter.CounterSize))
1272 return False;
1273 if(!prs_uint32("CounterOffset", ps, depth, &counter.CounterOffset))
1274 return False;
1277 return True;
1280 /*********************************************************************
1281 *********************************************************************/
1283 static bool _reg_perfcount_marshall_perf_counter_data(prs_struct *ps,
1284 struct PERF_COUNTER_BLOCK counter_data,
1285 int depth)
1287 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_counter_data");
1288 depth++;
1290 if(!prs_align_uint64(ps))
1291 return False;
1293 if(!prs_uint32("ByteLength", ps, depth, &counter_data.ByteLength))
1294 return False;
1295 if(!prs_uint8s(False, "CounterData", ps, depth, counter_data.data, counter_data.ByteLength - sizeof(uint32_t)))
1296 return False;
1297 if(!prs_align_uint64(ps))
1298 return False;
1300 return True;
1303 /*********************************************************************
1304 *********************************************************************/
1306 static bool _reg_perfcount_marshall_perf_instances(prs_struct *ps,
1307 struct PERF_OBJECT_TYPE object,
1308 int depth)
1310 struct PERF_INSTANCE_DEFINITION instance;
1311 int inst;
1313 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_instances");
1314 depth++;
1316 for(inst = 0; inst < object.NumInstances; inst++)
1318 instance = object.instances[inst];
1320 if(!prs_align(ps))
1321 return False;
1322 if(!prs_uint32("ByteLength", ps, depth, &instance.ByteLength))
1323 return False;
1324 if(!prs_uint32("ParentObjectTitleIndex", ps, depth, &instance.ParentObjectTitleIndex))
1325 return False;
1326 if(!prs_uint32("ParentObjectTitlePointer", ps, depth, &instance.ParentObjectTitlePointer))
1327 return False;
1328 if(!prs_uint32("UniqueID", ps, depth, &instance.UniqueID))
1329 return False;
1330 if(!prs_uint32("NameOffset", ps, depth, &instance.NameOffset))
1331 return False;
1332 if(!prs_uint32("NameLength", ps, depth, &instance.NameLength))
1333 return False;
1334 if(!prs_uint8s(False, "InstanceName", ps, depth, instance.data,
1335 instance.ByteLength - instance.NameOffset))
1336 return False;
1337 if(_reg_perfcount_marshall_perf_counter_data(ps, instance.counter_data, depth) == False)
1338 return False;
1341 return True;
1344 /*********************************************************************
1345 *********************************************************************/
1347 static bool _reg_perfcount_marshall_perf_objects(prs_struct *ps, struct PERF_DATA_BLOCK block, int depth)
1349 int obj;
1351 struct PERF_OBJECT_TYPE object;
1353 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_objects");
1354 depth++;
1356 for(obj = 0; obj < block.NumObjectTypes; obj++)
1358 object = block.objects[obj];
1360 if(!prs_align(ps))
1361 return False;
1363 if(!prs_uint32("TotalByteLength", ps, depth, &object.TotalByteLength))
1364 return False;
1365 if(!prs_uint32("DefinitionLength", ps, depth, &object.DefinitionLength))
1366 return False;
1367 if(!prs_uint32("HeaderLength", ps, depth, &object.HeaderLength))
1368 return False;
1369 if(!prs_uint32("ObjectNameTitleIndex", ps, depth, &object.ObjectNameTitleIndex))
1370 return False;
1371 if(!prs_uint32("ObjectNameTitlePointer", ps, depth, &object.ObjectNameTitlePointer))
1372 return False;
1373 if(!prs_uint32("ObjectHelpTitleIndex", ps, depth, &object.ObjectHelpTitleIndex))
1374 return False;
1375 if(!prs_uint32("ObjectHelpTitlePointer", ps, depth, &object.ObjectHelpTitlePointer))
1376 return False;
1377 if(!prs_uint32("DetailLevel", ps, depth, &object.DetailLevel))
1378 return False;
1379 if(!prs_uint32("NumCounters", ps, depth, &object.NumCounters))
1380 return False;
1381 if(!prs_uint32("DefaultCounter", ps, depth, &object.DefaultCounter))
1382 return False;
1383 if(!prs_uint32("NumInstances", ps, depth, &object.NumInstances))
1384 return False;
1385 if(!prs_uint32("CodePage", ps, depth, &object.CodePage))
1386 return False;
1387 if(!prs_align_uint64(ps))
1388 return False;
1389 if(!prs_uint64("PerfTime", ps, depth, &object.PerfTime))
1390 return False;
1391 if(!prs_uint64("PerfFreq", ps, depth, &object.PerfFreq))
1392 return False;
1394 /* Now do the counters */
1395 /* If no instances, encode counter_data */
1396 /* If instances, encode instace plus counter data for each instance */
1397 if(_reg_perfcount_marshall_perf_counters(ps, object, depth) == False)
1398 return False;
1399 if(object.NumInstances == PERF_NO_INSTANCES)
1401 if(_reg_perfcount_marshall_perf_counter_data(ps, object.counter_data, depth) == False)
1402 return False;
1404 else
1406 if(_reg_perfcount_marshall_perf_instances(ps, object, depth) == False)
1407 return False;
1411 return True;
1414 /*********************************************************************
1415 *********************************************************************/
1417 WERROR reg_perfcount_get_hkpd(prs_struct *ps, uint32_t max_buf_size, uint32_t *outbuf_len, const char *object_ids)
1420 * For a detailed description of the layout of this structure,
1421 * see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/performance_data_format.asp
1423 * By 2006-11-23 this link did not work anymore, I found something
1424 * promising under
1425 * http://msdn2.microsoft.com/en-us/library/aa373105.aspx -- vl
1427 struct PERF_DATA_BLOCK block;
1428 uint32_t buffer_size, base_index;
1430 buffer_size = 0;
1431 base_index = reg_perfcount_get_base_index();
1432 ZERO_STRUCT(block);
1434 buffer_size = reg_perfcount_get_perf_data_block(base_index, ps->mem_ctx, &block, object_ids, ps->bigendian_data);
1436 if(buffer_size < max_buf_size)
1438 *outbuf_len = buffer_size;
1440 if (!_reg_perfcount_marshall_perf_data_block(ps, block, 0))
1441 return WERR_NOT_ENOUGH_MEMORY;
1443 if (!_reg_perfcount_marshall_perf_objects(ps, block, 0))
1444 return WERR_NOT_ENOUGH_MEMORY;
1446 return WERR_OK;
1448 else
1450 *outbuf_len = max_buf_size;
1451 if (!_reg_perfcount_marshall_perf_data_block(ps, block, 0))
1452 return WERR_NOT_ENOUGH_MEMORY;
1454 return WERR_INSUFFICIENT_BUFFER;