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/>.
23 #include "system/filesys.h"
24 #include "../librpc/gen_ndr/perfcount.h"
26 #include "reg_perfcount.h"
27 #include "../libcli/registry/util_reg.h"
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 static char *counters_directory(const char *dbname)
48 TALLOC_CTX *ctx = talloc_tos();
50 path = state_path(PERFCOUNTDIR);
51 if (!directory_create_or_exist(path, geteuid(), 0755)) {
55 path = talloc_asprintf(ctx, "%s/%s", PERFCOUNTDIR, dbname);
60 ret = talloc_strdup(ctx, state_path(path));
65 /*********************************************************************
66 *********************************************************************/
68 uint32 reg_perfcount_get_base_index(void)
70 const char *fname = counters_directory( NAMES_DB );
75 char buf[PERFCOUNT_MAX_LEN];
77 names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
80 DEBUG(2, ("reg_perfcount_get_base_index: unable to open [%s].\n", fname));
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
86 The format for the counter_names.tdb file is:
93 even_num perf_counter<even_num>
94 even_num+1 perf_counter<even_num>_help
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);
101 DEBUG(1, ("reg_perfcount_get_base_index: failed to find key \'1\' in [%s].\n", fname));
108 memset(buf, 0, PERFCOUNT_MAX_LEN);
109 memcpy(buf, dbuf.dptr, dbuf.dsize);
110 retval = (uint32)atoi(buf);
111 SAFE_FREE(dbuf.dptr);
117 /*********************************************************************
118 *********************************************************************/
120 uint32 reg_perfcount_get_last_counter(uint32 base_index)
127 retval = base_index * 2;
132 /*********************************************************************
133 *********************************************************************/
135 uint32 reg_perfcount_get_last_help(uint32 last_counter)
139 if(last_counter == 0)
142 retval = last_counter + 1;
148 /*********************************************************************
149 *********************************************************************/
151 static uint32 _reg_perfcount_multi_sz_from_tdb(TDB_CONTEXT *tdb,
158 char *buf1 = *retbuf;
159 uint32 working_size = 0;
160 DATA_BLOB name_index, name;
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)
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)));
175 /* First encode the name_index */
176 working_size = (kbuf.dsize + 1)*sizeof(uint16);
177 buf1 = (char *)SMB_REALLOC(buf1, buffer_size + working_size);
182 ok = push_reg_sz(talloc_tos(), &name_index, (const char *)kbuf.dptr);
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);
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);
204 memcpy(buf1+buffer_size, (char *)name.data, working_size);
205 buffer_size += working_size;
212 /*********************************************************************
213 *********************************************************************/
215 uint32 reg_perfcount_get_counter_help(uint32 base_index, char **retbuf)
218 uint32 buffer_size = 0;
220 const char *fname = counters_directory( NAMES_DB );
226 names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
230 DEBUG(1, ("reg_perfcount_get_counter_help: unable to open [%s].\n", fname));
234 for(i = 1; i <= base_index; i++)
236 buffer_size = _reg_perfcount_multi_sz_from_tdb(names, (i*2)+1, retbuf, buffer_size);
240 /* Now terminate the MULTI_SZ with a double unicode NULL */
242 buf1 = (char *)SMB_REALLOC(buf1, buffer_size + 2);
246 buf1[buffer_size++] = '\0';
247 buf1[buffer_size++] = '\0';
255 /*********************************************************************
256 *********************************************************************/
258 uint32 reg_perfcount_get_counter_names(uint32 base_index, char **retbuf)
261 uint32 buffer_size = 0;
263 const char *fname = counters_directory( NAMES_DB );
269 names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
273 DEBUG(1, ("reg_perfcount_get_counter_names: unable to open [%s].\n", fname));
277 buffer_size = _reg_perfcount_multi_sz_from_tdb(names, 1, retbuf, buffer_size);
279 for(i = 1; i <= base_index; i++)
281 buffer_size = _reg_perfcount_multi_sz_from_tdb(names, i*2, retbuf, buffer_size);
285 /* Now terminate the MULTI_SZ with a double unicode NULL */
287 buf1 = (char *)SMB_REALLOC(buf1, buffer_size + 2);
291 buf1[buffer_size++] = '\0';
292 buf1[buffer_size++] = '\0';
300 /*********************************************************************
301 *********************************************************************/
303 static void _reg_perfcount_make_key(TDB_DATA *key,
307 const char *key_part2)
309 memset(buf, 0, buflen);
310 if(key_part2 != NULL)
311 snprintf(buf, buflen,"%d%s", key_part1, key_part2);
313 snprintf(buf, buflen, "%d", key_part1);
315 *key = string_tdb_data(buf);
320 /*********************************************************************
321 *********************************************************************/
323 static bool _reg_perfcount_isparent(TDB_DATA data)
327 if(data.dptr[0] == 'p')
335 /*********************************************************************
336 *********************************************************************/
338 static bool _reg_perfcount_ischild(TDB_DATA data)
342 if(data.dptr[0] == 'c')
350 /*********************************************************************
351 *********************************************************************/
353 static uint32 _reg_perfcount_get_numinst(int objInd, TDB_CONTEXT *names)
356 char buf[PERFCOUNT_MAX_LEN];
358 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, objInd, "inst");
359 data = tdb_fetch_compat(names, key);
361 if(data.dptr == NULL)
362 return (uint32)PERF_NO_INSTANCES;
364 memset(buf, 0, PERFCOUNT_MAX_LEN);
365 memcpy(buf, data.dptr, data.dsize);
366 SAFE_FREE(data.dptr);
367 return (uint32)atoi(buf);
370 /*********************************************************************
371 *********************************************************************/
373 static bool _reg_perfcount_add_instance(struct PERF_OBJECT_TYPE *obj,
378 static bool _reg_perfcount_add_object(struct PERF_DATA_BLOCK *block,
386 struct PERF_OBJECT_TYPE *obj;
388 block->objects = (struct PERF_OBJECT_TYPE *)talloc_realloc(mem_ctx,
390 struct PERF_OBJECT_TYPE,
391 block->NumObjectTypes+1);
392 if(block->objects == NULL)
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;
410 for(i = 0; i < (int)obj->NumInstances; i++) {
411 success = _reg_perfcount_add_instance(obj, mem_ctx, i, names);
417 /*********************************************************************
418 *********************************************************************/
420 static bool _reg_perfcount_get_counter_data(TDB_DATA key, TDB_DATA *data)
422 TDB_CONTEXT *counters;
423 const char *fname = counters_directory( DATA_DB );
425 counters = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
429 DEBUG(1, ("reg_perfcount_get_counter_data: unable to open [%s].\n", fname));
433 *data = tdb_fetch_compat(counters, key);
440 /*********************************************************************
441 *********************************************************************/
443 static uint32 _reg_perfcount_get_size_field(uint32 CounterType)
447 retval = CounterType;
449 /* First mask out reserved lower 8 bits */
450 retval = retval & 0xFFFFFF00;
451 retval = retval << 22;
452 retval = retval >> 22;
457 /*********************************************************************
458 *********************************************************************/
460 static uint32 _reg_perfcount_compute_scale(int64_t data)
476 return (uint32)scale;
479 /*********************************************************************
480 *********************************************************************/
482 static bool _reg_perfcount_get_counter_info(struct PERF_DATA_BLOCK *block,
485 struct PERF_OBJECT_TYPE *obj,
489 char buf[PERFCOUNT_MAX_LEN];
490 size_t dsize, padding;
491 long int data32, dbuf[2];
495 obj->counters[obj->NumCounters].DefaultScale = 0;
496 dbuf[0] = dbuf[1] = 0;
499 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, CounterIndex, "type");
500 data = tdb_fetch_compat(names, key);
501 if(data.dptr == NULL)
503 DEBUG(3, ("_reg_perfcount_get_counter_info: No type data for counter [%d].\n", CounterIndex));
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);
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)
518 DEBUG(3, ("_reg_perfcount_get_counter_info: No counter data for counter [%d].\n", CounterIndex));
522 counter_size = _reg_perfcount_get_size_field(obj->counters[obj->NumCounters].CounterType);
524 if(counter_size == PERF_SIZE_DWORD)
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);
533 obj->counters[obj->NumCounters].DefaultScale = 0;
535 padding = (dsize - (obj->counter_data.ByteLength%dsize)) % dsize;
537 else if(counter_size == PERF_SIZE_LARGE)
539 dsize = sizeof(data64);
540 memset(buf, 0, PERFCOUNT_MAX_LEN);
541 memcpy(buf, data.dptr, data.dsize);
543 if((obj->counters[obj->NumCounters].CounterType & 0x00000F00) == PERF_TYPE_NUMBER)
544 obj->counters[obj->NumCounters].DefaultScale = _reg_perfcount_compute_scale(data64);
546 obj->counters[obj->NumCounters].DefaultScale = 0;
547 memcpy((void *)dbuf, (const void *)&data64, dsize);
548 padding = (dsize - (obj->counter_data.ByteLength%dsize)) % dsize;
550 else /* PERF_SIZE_VARIABLE_LEN */
553 memset(buf, 0, PERFCOUNT_MAX_LEN);
554 memcpy(buf, data.dptr, data.dsize);
556 SAFE_FREE(data.dptr);
558 obj->counter_data.ByteLength += dsize + padding;
559 obj->counter_data.data = talloc_realloc(mem_ctx,
560 obj->counter_data.data,
562 obj->counter_data.ByteLength - sizeof(uint32));
563 if(obj->counter_data.data == NULL)
565 if(dbuf[0] != 0 || dbuf[1] != 0)
567 memcpy((void *)(obj->counter_data.data +
568 (obj->counter_data.ByteLength - (sizeof(uint32) + dsize))),
569 (const void *)dbuf, dsize);
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);
578 obj->counters[obj->NumCounters].CounterOffset = obj->counter_data.ByteLength - dsize;
579 if(obj->counters[obj->NumCounters].CounterOffset % dsize != 0)
581 DEBUG(3,("Improperly aligned counter [%d]\n", obj->NumCounters));
583 obj->counters[obj->NumCounters].CounterSize = dsize;
588 /*********************************************************************
589 *********************************************************************/
591 struct PERF_OBJECT_TYPE *_reg_perfcount_find_obj(struct PERF_DATA_BLOCK *block, int objind)
595 struct PERF_OBJECT_TYPE *obj = NULL;
597 for(i = 0; i < block->NumObjectTypes; i++)
599 if(block->objects[i].ObjectNameTitleIndex == objind)
601 obj = &(block->objects[i]);
608 /*********************************************************************
609 *********************************************************************/
611 static bool _reg_perfcount_add_counter(struct PERF_DATA_BLOCK *block,
617 char *begin, *end, *start, *stop;
619 struct PERF_OBJECT_TYPE *obj;
621 char buf[PERFCOUNT_MAX_LEN];
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)
633 stop = strchr(start, ',');
637 parent = atoi(start);
639 obj = _reg_perfcount_find_obj(block, parent);
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",
647 obj->counters = (struct PERF_COUNTER_DEFINITION *)talloc_realloc(mem_ctx,
649 struct PERF_COUNTER_DEFINITION,
651 if(obj->counters == NULL)
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;
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 */
670 /*********************************************************************
671 *********************************************************************/
673 static bool _reg_perfcount_get_instance_info(struct PERF_INSTANCE_DEFINITION *inst,
676 struct PERF_OBJECT_TYPE *obj,
680 char buf[PERFCOUNT_MAX_LEN], temp[PERFCOUNT_MAX_LEN];
681 smb_ucs2_t *name = NULL;
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"));
692 if(data.dptr == NULL)
694 DEBUG(3, ("_reg_perfcount_get_instance_info: No instance data for instance [%s].\n",
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,
703 if(inst->counter_data.data == NULL)
705 memset(inst->counter_data.data, 0, data.dsize);
706 memcpy(inst->counter_data.data, data.dptr, data.dsize);
707 SAFE_FREE(data.dptr);
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)
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",
719 inst->NameLength = 0;
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);
731 inst->data = talloc_realloc(mem_ctx,
735 if (inst->data == NULL) {
736 SAFE_FREE(data.dptr);
739 memcpy(inst->data, name, inst->NameLength);
740 SAFE_FREE(data.dptr);
743 inst->ParentObjectTitleIndex = 0;
744 inst->ParentObjectTitlePointer = 0;
745 inst->UniqueID = PERF_NO_UNIQUE_ID;
746 inst->NameOffset = 6 * sizeof(uint32);
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)))
753 inst->data = talloc_realloc(mem_ctx,
756 inst->NameLength + pad);
757 memset(inst->data + inst->NameLength, 0, pad);
758 inst->ByteLength += pad;
764 /*********************************************************************
765 *********************************************************************/
767 static bool _reg_perfcount_add_instance(struct PERF_OBJECT_TYPE *obj,
772 struct PERF_INSTANCE_DEFINITION *inst;
774 if(obj->instances == NULL) {
775 obj->instances = talloc_realloc(mem_ctx,
777 struct PERF_INSTANCE_DEFINITION,
780 if(obj->instances == NULL)
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);
788 /*********************************************************************
789 *********************************************************************/
791 static int _reg_perfcount_assemble_global(struct PERF_DATA_BLOCK *block,
797 int i, j, retval = 0;
798 char keybuf[PERFCOUNT_MAX_LEN];
801 for(i = 1; i <= base_index; i++)
804 _reg_perfcount_make_key(&key, keybuf, PERFCOUNT_MAX_LEN, j, "rel");
805 data = tdb_fetch_compat(names, key);
806 if(data.dptr != NULL)
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);
814 DEBUG(3, ("Bogus relationship [%s] for counter [%d].\n", data.dptr, j));
819 DEBUG(3, ("_reg_perfcount_assemble_global: Failed to add new relationship for counter [%d].\n", j));
822 SAFE_FREE(data.dptr);
825 DEBUG(3, ("NULL relationship for counter [%d] using key [%s].\n", j, keybuf));
830 /*********************************************************************
831 *********************************************************************/
833 static bool _reg_perfcount_get_64(uint64_t *retval,
836 const char *key_part2)
839 char buf[PERFCOUNT_MAX_LEN];
841 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, key_part1, key_part2);
843 data = tdb_fetch_compat(tdb, key);
844 if(data.dptr == NULL)
846 DEBUG(3,("_reg_perfcount_get_64: No data found for key [%s].\n", key.dptr));
850 memset(buf, 0, PERFCOUNT_MAX_LEN);
851 memcpy(buf, data.dptr, data.dsize);
852 SAFE_FREE(data.dptr);
859 /*********************************************************************
860 *********************************************************************/
862 static bool _reg_perfcount_init_data_block_perf(struct PERF_DATA_BLOCK *block,
865 uint64_t PerfFreq, PerfTime, PerfTime100nSec;
866 TDB_CONTEXT *counters;
868 const char *fname = counters_directory( DATA_DB );
870 counters = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
874 DEBUG(1, ("reg_perfcount_init_data_block_perf: unable to open [%s].\n", fname));
878 status = _reg_perfcount_get_64(&PerfFreq, names, 0, "PerfFreq");
884 memcpy((void *)&(block->PerfFreq), (const void *)&PerfFreq, sizeof(PerfFreq));
886 status = _reg_perfcount_get_64(&PerfTime, counters, 0, "PerfTime");
892 memcpy((void *)&(block->PerfTime), (const void *)&PerfTime, sizeof(PerfTime));
894 status = _reg_perfcount_get_64(&PerfTime100nSec, counters, 0, "PerfTime100nSec");
900 memcpy((void *)&(block->PerfTime100nSec), (const void *)&PerfTime100nSec, sizeof(PerfTime100nSec));
906 /*******************************************************************
907 ********************************************************************/
909 static bool make_systemtime(struct SYSTEMTIME *systime, struct tm *unixtime)
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;
923 /*********************************************************************
924 *********************************************************************/
926 static bool _reg_perfcount_init_data_block(struct PERF_DATA_BLOCK *block,
927 TALLOC_CTX *mem_ctx, TDB_CONTEXT *names,
930 smb_ucs2_t *temp = NULL;
931 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
935 sz = rpcstr_push_talloc(tmp_ctx, &temp, "PERF");
936 if ((sz == -1) || (temp == NULL)) {
939 memcpy(block->Signature, temp, strlen_w(temp) *2);
942 block->LittleEndian = 0;
944 block->LittleEndian = 1;
947 block->TotalByteLength = 0;
948 block->NumObjectTypes = 0;
949 block->DefaultObject = -1;
950 block->objects = NULL;
952 make_systemtime(&(block->SystemTime), gmtime(&tm));
953 _reg_perfcount_init_data_block_perf(block, names);
955 sz = rpcstr_push_talloc(tmp_ctx, &temp, lp_netbios_name());
956 if ((sz == -1) || (temp == NULL)) {
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) {
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);
975 talloc_free(tmp_ctx);
979 /*********************************************************************
980 *********************************************************************/
982 static uint32 _reg_perfcount_perf_data_block_fixup(struct PERF_DATA_BLOCK *block, TALLOC_CTX *mem_ctx)
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;
991 block->TotalByteLength = 0;
992 object = block->objects;
993 for(obj = 0; obj < block->NumObjectTypes; obj++)
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++)
1001 object[obj].TotalByteLength += counter[cnt].ByteLength;
1002 object[obj].DefinitionLength += counter[cnt].ByteLength;
1004 if(object[obj].NumInstances != PERF_NO_INSTANCES)
1006 for(inst = 0; inst < object[obj].NumInstances; inst++)
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,
1016 counter_data->ByteLength- sizeof(counter_data->ByteLength));
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++)
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;
1029 /* Make sure to be 64-bit aligned */
1030 if((pad = (counter_data->ByteLength % 8)))
1034 counter_data->data = talloc_realloc(mem_ctx,
1037 counter_data->ByteLength - sizeof(counter_data->ByteLength) + pad);
1038 if (counter_data->data == NULL) {
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;
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)))
1054 object[obj].counter_data.data = talloc_realloc(mem_ctx,
1055 object[obj].counter_data.data,
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;
1061 object[obj].TotalByteLength += object[obj].counter_data.ByteLength;
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;
1067 block->TotalByteLength += object[obj].TotalByteLength;
1070 return block->TotalByteLength;
1073 /*********************************************************************
1074 *********************************************************************/
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)
1082 uint32 buffer_size = 0;
1083 const char *fname = counters_directory( NAMES_DB );
1087 names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
1091 DEBUG(1, ("reg_perfcount_get_perf_data_block: unable to open [%s].\n", fname));
1095 if (!_reg_perfcount_init_data_block(block, mem_ctx, names, bigendian_data)) {
1096 DEBUG(0, ("_reg_perfcount_init_data_block failed\n"));
1101 reg_perfcount_get_last_counter(base_index);
1103 if(object_ids == NULL)
1105 /* we're getting a request for "Global" here */
1106 retval = _reg_perfcount_assemble_global(block, mem_ctx, base_index, names);
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);
1113 buffer_size = _reg_perfcount_perf_data_block_fixup(block, mem_ctx);
1121 return buffer_size + block->HeaderLength;
1124 /*******************************************************************
1125 ********************************************************************/
1127 static bool smb_io_system_time(const char *desc, prs_struct *ps, int depth, struct SYSTEMTIME *systime)
1129 if(!prs_uint16("year", ps, depth, &systime->year))
1131 if(!prs_uint16("month", ps, depth, &systime->month))
1133 if(!prs_uint16("dayofweek", ps, depth, &systime->dayofweek))
1135 if(!prs_uint16("day", ps, depth, &systime->day))
1137 if(!prs_uint16("hour", ps, depth, &systime->hour))
1139 if(!prs_uint16("minute", ps, depth, &systime->minute))
1141 if(!prs_uint16("second", ps, depth, &systime->second))
1143 if(!prs_uint16("milliseconds", ps, depth, &systime->milliseconds))
1149 /*********************************************************************
1150 *********************************************************************/
1152 static bool _reg_perfcount_marshall_perf_data_block(prs_struct *ps, struct PERF_DATA_BLOCK block, int depth)
1155 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_data_block");
1160 for(i = 0; i < 4; i++)
1162 if(!prs_uint16("Signature", ps, depth, &block.Signature[i]))
1165 if(!prs_uint32("Little Endian", ps, depth, &block.LittleEndian))
1167 if(!prs_uint32("Version", ps, depth, &block.Version))
1169 if(!prs_uint32("Revision", ps, depth, &block.Revision))
1171 if(!prs_uint32("TotalByteLength", ps, depth, &block.TotalByteLength))
1173 if(!prs_uint32("HeaderLength", ps, depth, &block.HeaderLength))
1175 if(!prs_uint32("NumObjectTypes", ps, depth, &block.NumObjectTypes))
1177 if(!prs_uint32("DefaultObject", ps, depth, &block.DefaultObject))
1179 if(!smb_io_system_time("SystemTime", ps, depth, &block.SystemTime))
1181 if(!prs_uint32("Padding", ps, depth, &block.Padding))
1183 if(!prs_align_uint64(ps))
1185 if(!prs_uint64("PerfTime", ps, depth, &block.PerfTime))
1187 if(!prs_uint64("PerfFreq", ps, depth, &block.PerfFreq))
1189 if(!prs_uint64("PerfTime100nSec", ps, depth, &block.PerfTime100nSec))
1191 if(!prs_uint32("SystemNameLength", ps, depth, &block.SystemNameLength))
1193 if(!prs_uint32("SystemNameOffset", ps, depth, &block.SystemNameOffset))
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))
1203 /*********************************************************************
1204 *********************************************************************/
1206 static bool _reg_perfcount_marshall_perf_counters(prs_struct *ps,
1207 struct PERF_OBJECT_TYPE object,
1211 struct PERF_COUNTER_DEFINITION counter;
1213 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_counters");
1216 for(cnt = 0; cnt < object.NumCounters; cnt++)
1218 counter = object.counters[cnt];
1222 if(!prs_uint32("ByteLength", ps, depth, &counter.ByteLength))
1224 if(!prs_uint32("CounterNameTitleIndex", ps, depth, &counter.CounterNameTitleIndex))
1226 if(!prs_uint32("CounterNameTitlePointer", ps, depth, &counter.CounterNameTitlePointer))
1228 if(!prs_uint32("CounterHelpTitleIndex", ps, depth, &counter.CounterHelpTitleIndex))
1230 if(!prs_uint32("CounterHelpTitlePointer", ps, depth, &counter.CounterHelpTitlePointer))
1232 if(!prs_uint32("DefaultScale", ps, depth, &counter.DefaultScale))
1234 if(!prs_uint32("DetailLevel", ps, depth, &counter.DetailLevel))
1236 if(!prs_uint32("CounterType", ps, depth, &counter.CounterType))
1238 if(!prs_uint32("CounterSize", ps, depth, &counter.CounterSize))
1240 if(!prs_uint32("CounterOffset", ps, depth, &counter.CounterOffset))
1247 /*********************************************************************
1248 *********************************************************************/
1250 static bool _reg_perfcount_marshall_perf_counter_data(prs_struct *ps,
1251 struct PERF_COUNTER_BLOCK counter_data,
1254 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_counter_data");
1257 if(!prs_align_uint64(ps))
1260 if(!prs_uint32("ByteLength", ps, depth, &counter_data.ByteLength))
1262 if(!prs_uint8s(False, "CounterData", ps, depth, counter_data.data, counter_data.ByteLength - sizeof(uint32)))
1264 if(!prs_align_uint64(ps))
1270 /*********************************************************************
1271 *********************************************************************/
1273 static bool _reg_perfcount_marshall_perf_instances(prs_struct *ps,
1274 struct PERF_OBJECT_TYPE object,
1277 struct PERF_INSTANCE_DEFINITION instance;
1280 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_instances");
1283 for(inst = 0; inst < object.NumInstances; inst++)
1285 instance = object.instances[inst];
1289 if(!prs_uint32("ByteLength", ps, depth, &instance.ByteLength))
1291 if(!prs_uint32("ParentObjectTitleIndex", ps, depth, &instance.ParentObjectTitleIndex))
1293 if(!prs_uint32("ParentObjectTitlePointer", ps, depth, &instance.ParentObjectTitlePointer))
1295 if(!prs_uint32("UniqueID", ps, depth, &instance.UniqueID))
1297 if(!prs_uint32("NameOffset", ps, depth, &instance.NameOffset))
1299 if(!prs_uint32("NameLength", ps, depth, &instance.NameLength))
1301 if(!prs_uint8s(False, "InstanceName", ps, depth, instance.data,
1302 instance.ByteLength - instance.NameOffset))
1304 if(_reg_perfcount_marshall_perf_counter_data(ps, instance.counter_data, depth) == False)
1311 /*********************************************************************
1312 *********************************************************************/
1314 static bool _reg_perfcount_marshall_perf_objects(prs_struct *ps, struct PERF_DATA_BLOCK block, int depth)
1318 struct PERF_OBJECT_TYPE object;
1320 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_objects");
1323 for(obj = 0; obj < block.NumObjectTypes; obj++)
1325 object = block.objects[obj];
1330 if(!prs_uint32("TotalByteLength", ps, depth, &object.TotalByteLength))
1332 if(!prs_uint32("DefinitionLength", ps, depth, &object.DefinitionLength))
1334 if(!prs_uint32("HeaderLength", ps, depth, &object.HeaderLength))
1336 if(!prs_uint32("ObjectNameTitleIndex", ps, depth, &object.ObjectNameTitleIndex))
1338 if(!prs_uint32("ObjectNameTitlePointer", ps, depth, &object.ObjectNameTitlePointer))
1340 if(!prs_uint32("ObjectHelpTitleIndex", ps, depth, &object.ObjectHelpTitleIndex))
1342 if(!prs_uint32("ObjectHelpTitlePointer", ps, depth, &object.ObjectHelpTitlePointer))
1344 if(!prs_uint32("DetailLevel", ps, depth, &object.DetailLevel))
1346 if(!prs_uint32("NumCounters", ps, depth, &object.NumCounters))
1348 if(!prs_uint32("DefaultCounter", ps, depth, &object.DefaultCounter))
1350 if(!prs_uint32("NumInstances", ps, depth, &object.NumInstances))
1352 if(!prs_uint32("CodePage", ps, depth, &object.CodePage))
1354 if(!prs_align_uint64(ps))
1356 if(!prs_uint64("PerfTime", ps, depth, &object.PerfTime))
1358 if(!prs_uint64("PerfFreq", ps, depth, &object.PerfFreq))
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)
1366 if(object.NumInstances == PERF_NO_INSTANCES)
1368 if(_reg_perfcount_marshall_perf_counter_data(ps, object.counter_data, depth) == False)
1373 if(_reg_perfcount_marshall_perf_instances(ps, object, depth) == False)
1381 /*********************************************************************
1382 *********************************************************************/
1384 WERROR reg_perfcount_get_hkpd(prs_struct *ps, uint32 max_buf_size, uint32 *outbuf_len, const char *object_ids)
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
1390 * By 2006-11-23 this link did not work anymore, I found something
1392 * http://msdn2.microsoft.com/en-us/library/aa373105.aspx -- vl
1394 struct PERF_DATA_BLOCK block;
1395 uint32 buffer_size, base_index;
1398 base_index = reg_perfcount_get_base_index();
1401 buffer_size = reg_perfcount_get_perf_data_block(base_index, ps->mem_ctx, &block, object_ids, ps->bigendian_data);
1403 if(buffer_size < max_buf_size)
1405 *outbuf_len = buffer_size;
1407 if (!_reg_perfcount_marshall_perf_data_block(ps, block, 0))
1410 if (!_reg_perfcount_marshall_perf_objects(ps, block, 0))
1417 *outbuf_len = max_buf_size;
1418 if (!_reg_perfcount_marshall_perf_data_block(ps, block, 0))
1421 return WERR_INSUFFICIENT_BUFFER;