r10243: fix net rpc shutdown (missing alignments and sending an invalid UNISTR4 for...
[Samba.git] / source / registry / reg_perfcount.c
blob609f86da6cb69ea0d8e842c7aa696b42e3efa112
1 #include "includes.h"
3 #undef DBGC_CLASS
4 #define DBGC_CLASS DBGC_RPC_SRV
6 #define PERFCOUNT_MAX_LEN 256
8 uint32 reg_perfcount_get_base_index(void)
10 pstring fname;
11 TDB_CONTEXT *names;
12 TDB_DATA kbuf, dbuf;
13 char key[] = "1";
14 uint32 retval = 0;
15 char buf[PERFCOUNT_MAX_LEN];
16 const char *counter_dir = lp_counters_dir();
19 if ( !*counter_dir )
20 return 0;
22 pstr_sprintf( fname, "%s/names.tdb", counter_dir );
24 names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
26 if ( !names ) {
27 DEBUG(1, ("reg_perfcount_get_base_index: unable to open [%s].\n", fname));
28 return 0;
30 /* needs to read the value of key "1" from the counter_names.tdb file, as that is
31 where the total number of counters is stored. We're assuming no holes in the
32 enumeration.
33 The format for the counter_names.tdb file is:
34 key value
35 1 num_counters
36 2 perf_counter1
37 3 perf_counter1_help
38 4 perf_counter2
39 5 perf_counter2_help
40 even_num perf_counter<even_num>
41 even_num+1 perf_counter<even_num>_help
42 and so on.
43 So last_counter becomes num_counters*2, and last_help will be last_counter+1 */
44 kbuf.dptr = key;
45 kbuf.dsize = strlen(key);
46 dbuf = tdb_fetch(names, kbuf);
47 if(dbuf.dptr == NULL)
49 DEBUG(1, ("reg_perfcount_get_base_index: failed to find key \'1\' in [%s].\n", fname));
50 tdb_close(names);
51 return 0;
53 else
55 tdb_close(names);
56 memset(buf, 0, PERFCOUNT_MAX_LEN);
57 memcpy(buf, dbuf.dptr, dbuf.dsize);
58 retval = (uint32)atoi(buf);
59 SAFE_FREE(dbuf.dptr);
60 return retval;
62 return 0;
65 uint32 reg_perfcount_get_last_counter(uint32 base_index)
67 uint32 retval;
69 if(base_index == 0)
70 retval = 0;
71 else
72 retval = base_index * 2;
74 return retval;
77 uint32 reg_perfcount_get_last_help(uint32 last_counter)
79 uint32 retval;
81 if(last_counter == 0)
82 retval = 0;
83 else
84 retval = last_counter + 1;
86 return retval;
89 static uint32 _reg_perfcount_multi_sz_from_tdb(TDB_CONTEXT *tdb,
90 int keyval,
91 char **retbuf,
92 uint32 buffer_size)
94 TDB_DATA kbuf, dbuf;
95 char temp[256];
96 char *buf1 = *retbuf, *buf2 = NULL;
97 uint32 working_size = 0;
98 UNISTR2 name_index, name;
100 memset(temp, 0, sizeof(temp));
101 snprintf(temp, sizeof(temp), "%d", keyval);
102 kbuf.dptr = temp;
103 kbuf.dsize = strlen(temp);
104 dbuf = tdb_fetch(tdb, kbuf);
105 if(dbuf.dptr == NULL)
107 /* If a key isn't there, just bypass it -- this really shouldn't
108 happen unless someone's mucking around with the tdb */
109 DEBUG(3, ("_reg_perfcount_multi_sz_from_tdb: failed to find key [%s] in [%s].\n",
110 temp, tdb->name));
111 return buffer_size;
113 /* First encode the name_index */
114 working_size = (kbuf.dsize + 1)*sizeof(uint16);
115 buf2 = SMB_REALLOC(buf1, buffer_size + working_size);
116 if(!buf2)
118 SAFE_FREE(buf1);
119 buffer_size = 0;
120 return buffer_size;
122 buf1 = buf2;
123 init_unistr2(&name_index, kbuf.dptr, UNI_STR_TERMINATE);
124 memcpy(buf1+buffer_size, (char *)name_index.buffer, working_size);
125 buffer_size += working_size;
126 /* Now encode the actual name */
127 working_size = (dbuf.dsize + 1)*sizeof(uint16);
128 buf2 = SMB_REALLOC(buf1, buffer_size + working_size);
129 if(!buf2)
131 SAFE_FREE(buf1);
132 buffer_size = 0;
133 return buffer_size;
135 buf1 = buf2;
136 memset(temp, 0, sizeof(temp));
137 memcpy(temp, dbuf.dptr, dbuf.dsize);
138 SAFE_FREE(dbuf.dptr);
139 init_unistr2(&name, temp, UNI_STR_TERMINATE);
140 memcpy(buf1+buffer_size, (char *)name.buffer, working_size);
141 buffer_size += working_size;
143 *retbuf = buf1;
145 return buffer_size;
148 uint32 reg_perfcount_get_counter_help(uint32 base_index, char **retbuf)
150 char *buf1 = NULL, *buf2 = NULL;
151 uint32 buffer_size = 0;
152 TDB_CONTEXT *names;
153 pstring fname;
154 int i;
156 if(base_index == 0)
157 return 0;
159 pstrcpy(fname, lp_counters_dir());
160 pstrcat(fname, "/names.tdb");
162 names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
164 if(names == NULL)
166 DEBUG(1, ("reg_perfcount_get_counter_help: unable to open [%s].\n", fname));
167 return 0;
170 for(i = 1; i <= base_index; i++)
172 buffer_size = _reg_perfcount_multi_sz_from_tdb(names, (i*2)+1, retbuf, buffer_size);
174 tdb_close(names);
176 /* Now terminate the MULTI_SZ with a double unicode NULL */
177 buf1 = *retbuf;
178 buf2 = SMB_REALLOC(buf1, buffer_size + 2);
179 if(!buf2)
181 SAFE_FREE(buf1);
182 buffer_size = 0;
184 else
186 buf1 = buf2;
187 buf1[buffer_size++] = '\0';
188 buf1[buffer_size++] = '\0';
191 *retbuf = buf1;
193 return buffer_size;
196 uint32 reg_perfcount_get_counter_names(uint32 base_index, char **retbuf)
198 char *buf1 = NULL, *buf2 = NULL;
199 uint32 buffer_size = 0;
200 TDB_CONTEXT *names;
201 pstring fname;
202 int i;
204 if(base_index == 0)
205 return 0;
207 pstrcpy(fname, lp_counters_dir());
208 pstrcat(fname, "/names.tdb");
210 names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
212 if(names == NULL)
214 DEBUG(1, ("reg_perfcount_get_counter_names: unable to open [%s].\n", fname));
215 return 0;
218 buffer_size = _reg_perfcount_multi_sz_from_tdb(names, 1, retbuf, buffer_size);
220 for(i = 1; i <= base_index; i++)
222 buffer_size = _reg_perfcount_multi_sz_from_tdb(names, i*2, retbuf, buffer_size);
224 tdb_close(names);
226 /* Now terminate the MULTI_SZ with a double unicode NULL */
227 buf1 = *retbuf;
228 buf2 = SMB_REALLOC(buf1, buffer_size + 2);
229 if(!buf2)
231 SAFE_FREE(buf1);
232 buffer_size = 0;
234 else
236 buf1 = buf2;
237 buf1[buffer_size++] = '\0';
238 buf1[buffer_size++] = '\0';
241 *retbuf=buf1;
243 return buffer_size;
246 static void _reg_perfcount_make_key(TDB_DATA *key,
247 char *buf,
248 int buflen,
249 int key_part1,
250 const char *key_part2)
252 memset(buf, 0, buflen);
253 if(key_part2 != NULL)
254 snprintf(buf, buflen,"%d%s", key_part1, key_part2);
255 else
256 snprintf(buf, buflen, "%d", key_part1);
258 key->dptr = buf;
259 key->dsize = strlen(buf);
261 return;
264 static BOOL _reg_perfcount_isparent(TDB_DATA data)
266 if(data.dsize > 0)
268 if(data.dptr[0] == 'p')
269 return True;
270 else
271 return False;
273 return False;
276 static BOOL _reg_perfcount_ischild(TDB_DATA data)
278 if(data.dsize > 0)
280 if(data.dptr[0] == 'c')
281 return True;
282 else
283 return False;
285 return False;
288 static uint32 _reg_perfcount_get_numinst(int objInd, TDB_CONTEXT *names)
290 TDB_DATA key, data;
291 char buf[PERFCOUNT_MAX_LEN];
293 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, objInd, "inst");
294 data = tdb_fetch(names, key);
296 if(data.dptr == NULL)
297 return (uint32)PERF_NO_INSTANCES;
299 memset(buf, 0, PERFCOUNT_MAX_LEN);
300 memcpy(buf, data.dptr, data.dsize);
301 return (uint32)atoi(buf);
304 static BOOL _reg_perfcount_add_object(PERF_DATA_BLOCK *block,
305 prs_struct *ps,
306 int num,
307 TDB_DATA data,
308 TDB_CONTEXT *names)
310 int i;
311 BOOL success = False;
312 PERF_OBJECT_TYPE *obj;
314 block->objects = (PERF_OBJECT_TYPE *)TALLOC_REALLOC_ARRAY(ps->mem_ctx,
315 block->objects,
316 PERF_OBJECT_TYPE,
317 block->NumObjectTypes+1);
318 if(block->objects == NULL)
319 return False;
320 obj = &(block->objects[block->NumObjectTypes]);
321 memset((void *)&(block->objects[block->NumObjectTypes]), 0, sizeof(PERF_OBJECT_TYPE));
322 block->objects[block->NumObjectTypes].ObjectNameTitleIndex = num;
323 block->objects[block->NumObjectTypes].ObjectNameTitlePointer = 0;
324 block->objects[block->NumObjectTypes].ObjectHelpTitleIndex = num+1;
325 block->objects[block->NumObjectTypes].ObjectHelpTitlePointer = 0;
326 block->objects[block->NumObjectTypes].NumCounters = 0;
327 block->objects[block->NumObjectTypes].DefaultCounter = 0;
328 block->objects[block->NumObjectTypes].NumInstances = _reg_perfcount_get_numinst(num, names);
329 block->objects[block->NumObjectTypes].counters = NULL;
330 block->objects[block->NumObjectTypes].instances = NULL;
331 block->objects[block->NumObjectTypes].counter_data.ByteLength = sizeof(uint32);
332 block->objects[block->NumObjectTypes].counter_data.data = NULL;
333 block->objects[block->NumObjectTypes].DetailLevel = PERF_DETAIL_NOVICE;
334 block->NumObjectTypes+=1;
336 for(i = 0; i < (int)obj->NumInstances; i++)
338 success = _reg_perfcount_add_instance(obj, ps, i, names);
341 return True;
344 BOOL _reg_perfcount_get_counter_data(TDB_DATA key, TDB_DATA *data)
346 TDB_CONTEXT *counters;
348 pstring fname;
350 pstrcpy(fname, lp_counters_dir());
351 pstrcat(fname, "/data.tdb");
353 counters = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
355 if(counters == NULL)
357 DEBUG(1, ("reg_perfcount_get_counter_data: unable to open [%s].\n", fname));
358 return False;
361 *data = tdb_fetch(counters, key);
363 tdb_close(counters);
365 return True;
368 static uint32 _reg_perfcount_get_size_field(uint32 CounterType)
370 uint32 retval;
372 retval = CounterType;
374 /* First mask out reserved lower 8 bits */
375 retval = retval & 0xFFFFFF00;
376 retval = retval << 22;
377 retval = retval >> 22;
379 return retval;
382 static uint32 _reg_perfcount_compute_scale(long long int data)
384 int scale = 0;
385 if(data == 0)
386 return scale;
387 while(data > 100)
389 data /= 10;
390 scale--;
392 while(data < 10)
394 data *= 10;
395 scale++;
398 return (uint32)scale;
401 static BOOL _reg_perfcount_get_counter_info(PERF_DATA_BLOCK *block,
402 prs_struct *ps,
403 int CounterIndex,
404 PERF_OBJECT_TYPE *obj,
405 TDB_CONTEXT *names)
407 TDB_DATA key, data;
408 char buf[PERFCOUNT_MAX_LEN];
409 size_t dsize, padding;
410 long int data32, dbuf[2];
411 long long int data64;
412 uint32 counter_size;
414 obj->counters[obj->NumCounters].DefaultScale = 0;
415 dbuf[0] = dbuf[1] = 0;
416 padding = 0;
418 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, CounterIndex, "type");
419 data = tdb_fetch(names, key);
420 if(data.dptr == NULL)
422 DEBUG(3, ("_reg_perfcount_get_counter_info: No type data for counter [%d].\n", CounterIndex));
423 return False;
425 memset(buf, 0, PERFCOUNT_MAX_LEN);
426 memcpy(buf, data.dptr, data.dsize);
427 obj->counters[obj->NumCounters].CounterType = atoi(buf);
428 DEBUG(10, ("_reg_perfcount_get_counter_info: Got type [%d] for counter [%d].\n",
429 obj->counters[obj->NumCounters].CounterType, CounterIndex));
430 free(data.dptr);
432 /* Fetch the actual data */
433 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, CounterIndex, "");
434 _reg_perfcount_get_counter_data(key, &data);
435 if(data.dptr == NULL)
437 DEBUG(3, ("_reg_perfcount_get_counter_info: No counter data for counter [%d].\n", CounterIndex));
438 return False;
441 counter_size = _reg_perfcount_get_size_field(obj->counters[obj->NumCounters].CounterType);
443 if(counter_size == PERF_SIZE_DWORD)
445 dsize = sizeof(data32);
446 memset(buf, 0, PERFCOUNT_MAX_LEN);
447 memcpy(buf, data.dptr, data.dsize);
448 data32 = strtol(buf, NULL, 0);
449 if((obj->counters[obj->NumCounters].CounterType & 0x00000F00) == PERF_TYPE_NUMBER)
450 obj->counters[obj->NumCounters].DefaultScale = _reg_perfcount_compute_scale((long long int)data32);
451 else
452 obj->counters[obj->NumCounters].DefaultScale = 0;
453 dbuf[0] = data32;
454 padding = (dsize - (obj->counter_data.ByteLength%dsize)) % dsize;
456 else if(counter_size == PERF_SIZE_LARGE)
458 dsize = sizeof(data64);
459 memset(buf, 0, PERFCOUNT_MAX_LEN);
460 memcpy(buf, data.dptr, data.dsize);
461 data64 = strtoll(buf, NULL, 0);
462 if((obj->counters[obj->NumCounters].CounterType & 0x00000F00) == PERF_TYPE_NUMBER)
463 obj->counters[obj->NumCounters].DefaultScale = _reg_perfcount_compute_scale(data64);
464 else
465 obj->counters[obj->NumCounters].DefaultScale = 0;
466 memcpy((void *)dbuf, (const void *)&data64, dsize);
467 padding = (dsize - (obj->counter_data.ByteLength%dsize)) % dsize;
469 else /* PERF_SIZE_VARIABLE_LEN */
471 dsize = data.dsize;
472 memset(buf, 0, PERFCOUNT_MAX_LEN);
473 memcpy(buf, data.dptr, data.dsize);
475 free(data.dptr);
477 obj->counter_data.ByteLength += dsize + padding;
478 obj->counter_data.data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
479 obj->counter_data.data,
480 uint8,
481 obj->counter_data.ByteLength - sizeof(uint32));
482 if(obj->counter_data.data == NULL)
483 return False;
484 if(dbuf[0] != 0 || dbuf[1] != 0)
486 memcpy((void *)(obj->counter_data.data +
487 (obj->counter_data.ByteLength - (sizeof(uint32) + dsize))),
488 (const void *)dbuf, dsize);
490 else
492 /* Handling PERF_SIZE_VARIABLE_LEN */
493 memcpy((void *)(obj->counter_data.data +
494 (obj->counter_data.ByteLength - (sizeof(uint32) + dsize))),
495 (const void *)buf, dsize);
497 obj->counters[obj->NumCounters].CounterOffset = obj->counter_data.ByteLength - dsize;
498 if(obj->counters[obj->NumCounters].CounterOffset % dsize != 0)
500 DEBUG(3,("Improperly aligned counter [%d]\n", obj->NumCounters));
502 obj->counters[obj->NumCounters].CounterSize = dsize;
504 return True;
507 PERF_OBJECT_TYPE *_reg_perfcount_find_obj(PERF_DATA_BLOCK *block, int objind)
509 int i;
511 PERF_OBJECT_TYPE *obj = NULL;
513 for(i = 0; i < block->NumObjectTypes; i++)
515 if(block->objects[i].ObjectNameTitleIndex == objind)
517 obj = &(block->objects[i]);
521 return obj;
524 static BOOL _reg_perfcount_add_counter(PERF_DATA_BLOCK *block,
525 prs_struct *ps,
526 int num,
527 TDB_DATA data,
528 TDB_CONTEXT *names)
530 char *begin, *end, *start, *stop;
531 int parent;
532 PERF_OBJECT_TYPE *obj;
533 BOOL success = False;
534 char buf[PERFCOUNT_MAX_LEN];
536 obj = NULL;
537 memset(buf, 0, PERFCOUNT_MAX_LEN);
538 memcpy(buf, data.dptr, data.dsize);
539 begin = index(buf, '[');
540 end = index(buf, ']');
541 if(begin == NULL || end == NULL)
542 return False;
543 start = begin+1;
545 while(start < end)
547 stop = index(start, ',');
548 if(stop == NULL)
549 stop = end;
550 *stop = '\0';
551 parent = atoi(start);
553 obj = _reg_perfcount_find_obj(block, parent);
554 if(obj == NULL)
556 /* At this point we require that the parent object exist.
557 This can probably be handled better at some later time */
558 DEBUG(3, ("_reg_perfcount_add_counter: Could not find parent object [%d] for counter [%d].\n",
559 parent, num));
560 return False;
562 obj->counters = (PERF_COUNTER_DEFINITION *)TALLOC_REALLOC_ARRAY(ps->mem_ctx,
563 obj->counters,
564 PERF_COUNTER_DEFINITION,
565 obj->NumCounters+1);
566 if(obj->counters == NULL)
567 return False;
568 memset((void *)&(obj->counters[obj->NumCounters]), 0, sizeof(PERF_COUNTER_DEFINITION));
569 obj->counters[obj->NumCounters].CounterNameTitleIndex=num;
570 obj->counters[obj->NumCounters].CounterHelpTitleIndex=num+1;
571 obj->counters[obj->NumCounters].DetailLevel = PERF_DETAIL_NOVICE;
572 obj->counters[obj->NumCounters].ByteLength = sizeof(PERF_COUNTER_DEFINITION);
573 success = _reg_perfcount_get_counter_info(block, ps, num, obj, names);
574 obj->NumCounters += 1;
575 start = stop + 1;
578 /* Handle case of Objects/Counters without any counter data, which would suggest
579 that the required instances are not there yet, so change NumInstances from
580 PERF_NO_INSTANCES to 0 */
582 return True;
585 BOOL _reg_perfcount_get_instance_info(PERF_INSTANCE_DEFINITION *inst,
586 prs_struct *ps,
587 int instId,
588 PERF_OBJECT_TYPE *obj,
589 TDB_CONTEXT *names)
591 TDB_DATA key, data;
592 char buf[PERFCOUNT_MAX_LEN], temp[PERFCOUNT_MAX_LEN];
593 wpstring name;
594 int pad;
596 /* First grab the instance data from the data file */
597 memset(temp, 0, PERFCOUNT_MAX_LEN);
598 snprintf(temp, PERFCOUNT_MAX_LEN, "i%d", instId);
599 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, obj->ObjectNameTitleIndex, temp);
600 _reg_perfcount_get_counter_data(key, &data);
601 if(data.dptr == NULL)
603 DEBUG(3, ("_reg_perfcount_get_instance_info: No instance data for instance [%s].\n",
604 buf));
605 return False;
607 inst->counter_data.ByteLength = data.dsize + sizeof(inst->counter_data.ByteLength);
608 inst->counter_data.data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
609 inst->counter_data.data,
610 uint8,
611 data.dsize);
612 if(inst->counter_data.data == NULL)
613 return False;
614 memset(inst->counter_data.data, 0, data.dsize);
615 memcpy(inst->counter_data.data, data.dptr, data.dsize);
616 free(data.dptr);
618 /* Fetch instance name */
619 memset(temp, 0, PERFCOUNT_MAX_LEN);
620 snprintf(temp, PERFCOUNT_MAX_LEN, "i%dname", instId);
621 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, obj->ObjectNameTitleIndex, temp);
622 data = tdb_fetch(names, key);
623 if(data.dptr == NULL)
625 /* Not actually an error, but possibly unintended? -- just logging FYI */
626 DEBUG(3, ("_reg_perfcount_get_instance_info: No instance name for instance [%s].\n",
627 buf));
628 inst->NameLength = 0;
630 else
632 memset(buf, 0, PERFCOUNT_MAX_LEN);
633 memcpy(buf, data.dptr, data.dsize);
634 rpcstr_push((void *)name, buf, sizeof(name), STR_TERMINATE);
635 inst->NameLength = (strlen_w(name) * 2) + 2;
636 inst->data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
637 inst->data,
638 uint8,
639 inst->NameLength);
640 memcpy(inst->data, name, inst->NameLength);
641 free(data.dptr);
644 inst->ParentObjectTitleIndex = 0;
645 inst->ParentObjectTitlePointer = 0;
646 inst->UniqueID = PERF_NO_UNIQUE_ID;
647 inst->NameOffset = 6 * sizeof(uint32);
649 inst->ByteLength = inst->NameOffset + inst->NameLength;
650 /* Need to be aligned on a 64-bit boundary here for counter_data */
651 if((pad = (inst->ByteLength % 8)))
653 pad = 8 - pad;
654 inst->data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
655 inst->data,
656 uint8,
657 inst->NameLength + pad);
658 memset(inst->data + inst->NameLength, 0, pad);
659 inst->ByteLength += pad;
662 return True;
665 BOOL _reg_perfcount_add_instance(PERF_OBJECT_TYPE *obj,
666 prs_struct *ps,
667 int instInd,
668 TDB_CONTEXT *names)
670 BOOL success;
671 PERF_INSTANCE_DEFINITION *inst;
673 success = False;
675 if(obj->instances == NULL)
677 obj->instances = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
678 obj->instances,
679 PERF_INSTANCE_DEFINITION,
680 obj->NumInstances);
682 if(obj->instances == NULL)
683 return False;
685 memset(&(obj->instances[instInd]), 0, sizeof(PERF_INSTANCE_DEFINITION));
686 inst = &(obj->instances[instInd]);
687 success = _reg_perfcount_get_instance_info(inst, ps, instInd, obj, names);
689 return True;
692 static int _reg_perfcount_assemble_global(PERF_DATA_BLOCK *block,
693 prs_struct *ps,
694 int base_index,
695 TDB_CONTEXT *names)
697 BOOL success;
698 int i, j, retval = 0;
699 char keybuf[PERFCOUNT_MAX_LEN];
700 TDB_DATA key, data;
702 for(i = 1; i <= base_index; i++)
704 j = i*2;
705 _reg_perfcount_make_key(&key, keybuf, PERFCOUNT_MAX_LEN, j, "rel");
706 data = tdb_fetch(names, key);
707 if(data.dptr != NULL)
709 if(_reg_perfcount_isparent(data))
710 success = _reg_perfcount_add_object(block, ps, j, data, names);
711 else if(_reg_perfcount_ischild(data))
712 success = _reg_perfcount_add_counter(block, ps, j, data, names);
713 else
715 DEBUG(3, ("Bogus relationship [%s] for counter [%d].\n", data.dptr, j));
716 success = False;
718 if(success == False)
720 DEBUG(3, ("_reg_perfcount_assemble_global: Failed to add new relationship for counter [%d].\n", j));
721 retval = -1;
723 free(data.dptr);
725 else
726 DEBUG(3, ("NULL relationship for counter [%d] using key [%s].\n", j, keybuf));
728 return retval;
731 static BOOL _reg_perfcount_get_64(unsigned long long *retval,
732 TDB_CONTEXT *tdb,
733 int key_part1,
734 const char *key_part2)
736 TDB_DATA key, data;
737 char buf[PERFCOUNT_MAX_LEN];
739 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, key_part1, key_part2);
741 data = tdb_fetch(tdb, key);
742 if(data.dptr == NULL)
744 DEBUG(3,("_reg_perfcount_get_64: No data found for key [%s].\n", key.dptr));
745 return False;
748 memset(buf, 0, PERFCOUNT_MAX_LEN);
749 memcpy(buf, data.dptr, data.dsize);
750 free(data.dptr);
752 *retval = strtoll(buf, NULL, 0);
754 return True;
757 static BOOL _reg_perfcount_init_data_block_perf(PERF_DATA_BLOCK *block,
758 TDB_CONTEXT *names)
760 unsigned long long PerfFreq, PerfTime, PerfTime100nSec;
761 TDB_CONTEXT *counters;
762 BOOL status;
763 pstring fname;
765 status = False;
767 pstrcpy(fname, lp_counters_dir());
768 pstrcat(fname, "/data.tdb");
770 counters = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
772 if(counters == NULL)
774 DEBUG(1, ("reg_perfcount_init_data_block_perf: unable to open [%s].\n", fname));
775 return False;
778 status = _reg_perfcount_get_64(&PerfFreq, names, 0, "PerfFreq");
779 if(status == False)
781 tdb_close(counters);
782 return status;
784 memcpy((void *)&(block->PerfFreq), (const void *)&PerfFreq, sizeof(PerfFreq));
786 status = _reg_perfcount_get_64(&PerfTime, counters, 0, "PerfTime");
787 if(status == False)
789 tdb_close(counters);
790 return status;
792 memcpy((void *)&(block->PerfTime), (const void *)&PerfTime, sizeof(PerfTime));
794 status = _reg_perfcount_get_64(&PerfTime100nSec, counters, 0, "PerfTime100nSec");
795 if(status == False)
797 tdb_close(counters);
798 return status;
800 memcpy((void *)&(block->PerfTime100nSec), (const void *)&PerfTime100nSec, sizeof(PerfTime100nSec));
802 tdb_close(counters);
803 return True;
806 static void _reg_perfcount_init_data_block(PERF_DATA_BLOCK *block, prs_struct *ps, TDB_CONTEXT *names)
808 wpstring temp;
809 time_t tm;
811 memset(temp, 0, sizeof(temp));
812 rpcstr_push((void *)temp, "PERF", sizeof(temp), STR_TERMINATE);
813 memcpy(block->Signature, temp, strlen_w(temp) *2);
815 if(ps->bigendian_data == RPC_BIG_ENDIAN)
816 block->LittleEndian = 0;
817 else
818 block->LittleEndian = 1;
819 block->Version = 1;
820 block->Revision = 1;
821 block->TotalByteLength = 0;
822 block->NumObjectTypes = 0;
823 block->DefaultObject = -1;
824 block->objects = NULL;
825 tm = time(NULL);
826 make_systemtime(&(block->SystemTime), gmtime(&tm));
827 _reg_perfcount_init_data_block_perf(block, names);
828 memset(temp, 0, sizeof(temp));
829 rpcstr_push((void *)temp, global_myname(), sizeof(temp), STR_TERMINATE);
830 block->SystemNameLength = (strlen_w(temp) * 2) + 2;
831 block->data = TALLOC_ZERO_ARRAY(ps->mem_ctx, uint8, block->SystemNameLength + (8 - (block->SystemNameLength % 8)));
832 memcpy(block->data, temp, block->SystemNameLength);
833 block->SystemNameOffset = sizeof(PERF_DATA_BLOCK) - sizeof(block->objects) - sizeof(block->data);
834 block->HeaderLength = block->SystemNameOffset + block->SystemNameLength;
835 /* Make sure to adjust for 64-bit alignment for when we finish writing the system name,
836 so that the PERF_OBJECT_TYPE struct comes out 64-bit aligned */
837 block->HeaderLength += 8 - (block->HeaderLength % 8);
839 return;
842 static uint32 _reg_perfcount_perf_data_block_fixup(PERF_DATA_BLOCK *block, prs_struct *ps)
844 int obj, cnt, inst, pad, i;
845 PERF_OBJECT_TYPE *object;
846 PERF_INSTANCE_DEFINITION *instance;
847 PERF_COUNTER_DEFINITION *counter;
848 PERF_COUNTER_BLOCK *counter_data;
849 char *temp = NULL, *src_addr, *dst_addr;
851 block->TotalByteLength = 0;
852 object = block->objects;
853 for(obj = 0; obj < block->NumObjectTypes; obj++)
855 object[obj].TotalByteLength = 0;
856 object[obj].DefinitionLength = 0;
857 instance = object[obj].instances;
858 counter = object[obj].counters;
859 for(cnt = 0; cnt < object[obj].NumCounters; cnt++)
861 object[obj].TotalByteLength += counter[cnt].ByteLength;
862 object[obj].DefinitionLength += counter[cnt].ByteLength;
864 if(object[obj].NumInstances != PERF_NO_INSTANCES)
866 for(inst = 0; inst < object[obj].NumInstances; inst++)
868 instance = &(object[obj].instances[inst]);
869 object[obj].TotalByteLength += instance->ByteLength;
870 counter_data = &(instance->counter_data);
871 counter = &(object[obj].counters[object[obj].NumCounters - 1]);
872 counter_data->ByteLength = counter->CounterOffset + counter->CounterSize + sizeof(counter_data->ByteLength);
873 temp = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
874 temp,
875 uint8,
876 counter_data->ByteLength- sizeof(counter_data->ByteLength));
877 memset(temp, 0, counter_data->ByteLength - sizeof(counter_data->ByteLength));
878 src_addr = counter_data->data;
879 for(i = 0; i < object[obj].NumCounters; i++)
881 counter = &(object[obj].counters[i]);
882 dst_addr = temp + counter->CounterOffset - sizeof(counter_data->ByteLength);
883 memcpy(dst_addr, src_addr, counter->CounterSize);
884 src_addr += counter->CounterSize;
886 /* Make sure to be 64-bit aligned */
887 if((pad = (counter_data->ByteLength % 8)))
889 pad = 8 - pad;
891 counter_data->data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
892 counter_data->data,
893 uint8,
894 counter_data->ByteLength - sizeof(counter_data->ByteLength) + pad);
895 memset(counter_data->data, 0, counter_data->ByteLength - sizeof(counter_data->ByteLength) + pad);
896 memcpy(counter_data->data, temp, counter_data->ByteLength - sizeof(counter_data->ByteLength));
897 counter_data->ByteLength += pad;
898 object[obj].TotalByteLength += counter_data->ByteLength;
901 else
903 /* Need to be 64-bit aligned at the end of the counter_data block, so pad counter_data to a 64-bit boundary,
904 so that the next PERF_OBJECT_TYPE can start on a 64-bit alignment */
905 if((pad = (object[obj].counter_data.ByteLength % 8)))
907 pad = 8 - pad;
908 object[obj].counter_data.data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
909 object[obj].counter_data.data,
910 uint8,
911 object[obj].counter_data.ByteLength + pad);
912 memset((void *)(object[obj].counter_data.data + object[obj].counter_data.ByteLength), 0, pad);
913 object[obj].counter_data.ByteLength += pad;
915 object[obj].TotalByteLength += object[obj].counter_data.ByteLength;
917 object[obj].HeaderLength = sizeof(*object) - (sizeof(counter) + sizeof(instance) + sizeof(PERF_COUNTER_BLOCK));
918 object[obj].TotalByteLength += object[obj].HeaderLength;
919 object[obj].DefinitionLength += object[obj].HeaderLength;
921 block->TotalByteLength += object[obj].TotalByteLength;
924 return block->TotalByteLength;
927 uint32 reg_perfcount_get_perf_data_block(uint32 base_index,
928 prs_struct *ps,
929 PERF_DATA_BLOCK *block,
930 char *object_ids)
932 uint32 buffer_size = 0, last_counter;
933 pstring fname;
934 TDB_CONTEXT *names;
935 int retval;
937 pstrcpy(fname, lp_counters_dir());
938 pstrcat(fname, "/names.tdb");
940 names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
942 if(names == NULL)
944 DEBUG(1, ("reg_perfcount_get_perf_data_block: unable to open [%s].\n", fname));
945 return 0;
948 _reg_perfcount_init_data_block(block, ps, names);
950 last_counter = reg_perfcount_get_last_counter(base_index);
952 if(object_ids == NULL)
954 /* we're getting a request for "Global" here */
955 retval = _reg_perfcount_assemble_global(block, ps, base_index, names);
957 else
959 /* we're getting a request for a specific set of PERF_OBJECT_TYPES */
960 retval = _reg_perfcount_assemble_global(block, ps, base_index, names);
962 buffer_size = _reg_perfcount_perf_data_block_fixup(block, ps);
964 tdb_close(names);
966 return buffer_size + block->HeaderLength;
969 static BOOL _reg_perfcount_marshall_perf_data_block(prs_struct *ps, PERF_DATA_BLOCK block, int depth)
971 int i;
972 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_data_block");
973 depth++;
975 if(!prs_align(ps))
976 return False;
977 for(i = 0; i < 4; i++)
979 if(!prs_uint16("Signature", ps, depth, &block.Signature[i]))
980 return False;
982 if(!prs_uint32("Little Endian", ps, depth, &block.LittleEndian))
983 return False;
984 if(!prs_uint32("Version", ps, depth, &block.Version))
985 return False;
986 if(!prs_uint32("Revision", ps, depth, &block.Revision))
987 return False;
988 if(!prs_uint32("TotalByteLength", ps, depth, &block.TotalByteLength))
989 return False;
990 if(!prs_uint32("HeaderLength", ps, depth, &block.HeaderLength))
991 return False;
992 if(!prs_uint32("NumObjectTypes", ps, depth, &block.NumObjectTypes))
993 return False;
994 if(!prs_uint32("DefaultObject", ps, depth, &block.DefaultObject))
995 return False;
996 if(!spoolss_io_system_time("SystemTime", ps, depth, &block.SystemTime))
997 return False;
998 if(!prs_uint32("Padding", ps, depth, &block.Padding))
999 return False;
1000 if(!prs_align_uint64(ps))
1001 return False;
1002 if(!prs_uint64("PerfTime", ps, depth, &block.PerfTime))
1003 return False;
1004 if(!prs_uint64("PerfFreq", ps, depth, &block.PerfFreq))
1005 return False;
1006 if(!prs_uint64("PerfTime100nSec", ps, depth, &block.PerfTime100nSec))
1007 return False;
1008 if(!prs_uint32("SystemNameLength", ps, depth, &block.SystemNameLength))
1009 return False;
1010 if(!prs_uint32("SystemNameOffset", ps, depth, &block.SystemNameOffset))
1011 return False;
1012 /* hack to make sure we're 64-bit aligned at the end of this whole mess */
1013 if(!prs_uint8s(False, "SystemName", ps, depth, block.data,
1014 block.HeaderLength - block.SystemNameOffset))
1015 return False;
1017 return True;
1020 static BOOL _reg_perfcount_marshall_perf_counters(prs_struct *ps,
1021 PERF_OBJECT_TYPE object,
1022 int depth)
1024 int cnt;
1025 PERF_COUNTER_DEFINITION counter;
1027 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_counters");
1028 depth++;
1030 for(cnt = 0; cnt < object.NumCounters; cnt++)
1032 counter = object.counters[cnt];
1034 if(!prs_align(ps))
1035 return False;
1036 if(!prs_uint32("ByteLength", ps, depth, &counter.ByteLength))
1037 return False;
1038 if(!prs_uint32("CounterNameTitleIndex", ps, depth, &counter.CounterNameTitleIndex))
1039 return False;
1040 if(!prs_uint32("CounterNameTitlePointer", ps, depth, &counter.CounterNameTitlePointer))
1041 return False;
1042 if(!prs_uint32("CounterHelpTitleIndex", ps, depth, &counter.CounterHelpTitleIndex))
1043 return False;
1044 if(!prs_uint32("CounterHelpTitlePointer", ps, depth, &counter.CounterHelpTitlePointer))
1045 return False;
1046 if(!prs_uint32("DefaultScale", ps, depth, &counter.DefaultScale))
1047 return False;
1048 if(!prs_uint32("DetailLevel", ps, depth, &counter.DetailLevel))
1049 return False;
1050 if(!prs_uint32("CounterType", ps, depth, &counter.CounterType))
1051 return False;
1052 if(!prs_uint32("CounterSize", ps, depth, &counter.CounterSize))
1053 return False;
1054 if(!prs_uint32("CounterOffset", ps, depth, &counter.CounterOffset))
1055 return False;
1058 return True;
1061 static BOOL _reg_perfcount_marshall_perf_counter_data(prs_struct *ps,
1062 PERF_COUNTER_BLOCK counter_data,
1063 int depth)
1065 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_counter_data");
1066 depth++;
1068 if(!prs_align_uint64(ps))
1069 return False;
1071 if(!prs_uint32("ByteLength", ps, depth, &counter_data.ByteLength))
1072 return False;
1073 if(!prs_uint8s(False, "CounterData", ps, depth, counter_data.data, counter_data.ByteLength - sizeof(uint32)))
1074 return False;
1075 if(!prs_align_uint64(ps))
1076 return False;
1078 return True;
1081 static BOOL _reg_perfcount_marshall_perf_instances(prs_struct *ps,
1082 PERF_OBJECT_TYPE object,
1083 int depth)
1085 PERF_INSTANCE_DEFINITION instance;
1086 int inst;
1088 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_instances");
1089 depth++;
1091 for(inst = 0; inst < object.NumInstances; inst++)
1093 instance = object.instances[inst];
1095 if(!prs_align(ps))
1096 return False;
1097 if(!prs_uint32("ByteLength", ps, depth, &instance.ByteLength))
1098 return False;
1099 if(!prs_uint32("ParentObjectTitleIndex", ps, depth, &instance.ParentObjectTitleIndex))
1100 return False;
1101 if(!prs_uint32("ParentObjectTitlePointer", ps, depth, &instance.ParentObjectTitlePointer))
1102 return False;
1103 if(!prs_uint32("UniqueID", ps, depth, &instance.UniqueID))
1104 return False;
1105 if(!prs_uint32("NameOffset", ps, depth, &instance.NameOffset))
1106 return False;
1107 if(!prs_uint32("NameLength", ps, depth, &instance.NameLength))
1108 return False;
1109 if(!prs_uint8s(False, "InstanceName", ps, depth, instance.data,
1110 instance.ByteLength - instance.NameOffset))
1111 return False;
1112 if(_reg_perfcount_marshall_perf_counter_data(ps, instance.counter_data, depth) == False)
1113 return False;
1116 return True;
1119 static BOOL _reg_perfcount_marshall_perf_objects(prs_struct *ps, PERF_DATA_BLOCK block, int depth)
1121 int obj;
1123 PERF_OBJECT_TYPE object;
1125 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_objects");
1126 depth++;
1128 for(obj = 0; obj < block.NumObjectTypes; obj++)
1130 object = block.objects[obj];
1132 if(!prs_align(ps))
1133 return False;
1135 if(!prs_uint32("TotalByteLength", ps, depth, &object.TotalByteLength))
1136 return False;
1137 if(!prs_uint32("DefinitionLength", ps, depth, &object.DefinitionLength))
1138 return False;
1139 if(!prs_uint32("HeaderLength", ps, depth, &object.HeaderLength))
1140 return False;
1141 if(!prs_uint32("ObjectNameTitleIndex", ps, depth, &object.ObjectNameTitleIndex))
1142 return False;
1143 if(!prs_uint32("ObjectNameTitlePointer", ps, depth, &object.ObjectNameTitlePointer))
1144 return False;
1145 if(!prs_uint32("ObjectHelpTitleIndex", ps, depth, &object.ObjectHelpTitleIndex))
1146 return False;
1147 if(!prs_uint32("ObjectHelpTitlePointer", ps, depth, &object.ObjectHelpTitlePointer))
1148 return False;
1149 if(!prs_uint32("DetailLevel", ps, depth, &object.DetailLevel))
1150 return False;
1151 if(!prs_uint32("NumCounters", ps, depth, &object.NumCounters))
1152 return False;
1153 if(!prs_uint32("DefaultCounter", ps, depth, &object.DefaultCounter))
1154 return False;
1155 if(!prs_uint32("NumInstances", ps, depth, &object.NumInstances))
1156 return False;
1157 if(!prs_uint32("CodePage", ps, depth, &object.CodePage))
1158 return False;
1159 if(!prs_align_uint64(ps))
1160 return False;
1161 if(!prs_uint64("PerfTime", ps, depth, &object.PerfTime))
1162 return False;
1163 if(!prs_uint64("PerfFreq", ps, depth, &object.PerfFreq))
1164 return False;
1166 /* Now do the counters */
1167 /* If no instances, encode counter_data */
1168 /* If instances, encode instace plus counter data for each instance */
1169 if(_reg_perfcount_marshall_perf_counters(ps, object, depth) == False)
1170 return False;
1171 if(object.NumInstances == PERF_NO_INSTANCES)
1173 if(_reg_perfcount_marshall_perf_counter_data(ps, object.counter_data, depth) == False)
1174 return False;
1176 else
1178 if(_reg_perfcount_marshall_perf_instances(ps, object, depth) == False)
1179 return False;
1183 return True;
1186 static BOOL _reg_perfcount_marshall_hkpd(prs_struct *ps, PERF_DATA_BLOCK block)
1188 int depth = 0;
1189 if(_reg_perfcount_marshall_perf_data_block(ps, block, depth) == True)
1191 if(_reg_perfcount_marshall_perf_objects(ps, block, depth) == True)
1192 return True;
1194 return False;
1196 WERROR reg_perfcount_get_hkpd(prs_struct *ps, uint32 max_buf_size, uint32 *outbuf_len, char *object_ids)
1199 * For a detailed description of the layout of this structure,
1200 * see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/performance_data_format.asp
1202 PERF_DATA_BLOCK block;
1203 uint32 buffer_size, base_index;
1205 buffer_size = 0;
1206 base_index = reg_perfcount_get_base_index();
1207 ZERO_STRUCT(block);
1209 buffer_size = reg_perfcount_get_perf_data_block(base_index, ps, &block, object_ids);
1211 if(buffer_size < max_buf_size)
1213 *outbuf_len = buffer_size;
1214 if(_reg_perfcount_marshall_hkpd(ps, block) == True)
1215 return WERR_OK;
1216 else
1217 return WERR_NOMEM;
1219 else
1221 *outbuf_len = max_buf_size;
1222 _reg_perfcount_marshall_perf_data_block(ps, block, 0);
1223 return WERR_INSUFFICIENT_BUFFER;