Make sure registered static threads are unique. (#12538)
vkalintiris committed
Mar 30, 2022 at 15:38 UTC
c41d8e0a012e6eef546c0ae3ceb1c763e7350a72
3 files changed
+44
-2
daemon/main.c
+7
-2
@@ -786,8 +786,13 @@ int main(int argc, char **argv) {
786
}
787
788
if(strcmp(optarg, "unittest") == 0) {
789
- if(unit_test_buffer()) return 1;
790
- if(unit_test_str2ld()) return 1;
789
+ if (unit_test_static_threads())
790
+ return 1;
791
+ if (unit_test_buffer())
792
+ return 1;
793
+ if (unit_test_str2ld())
794
+ return 1;
795
+
796
// No call to load the config file on this code-path
797
post_conf_load(&user);
798
get_netdata_configured_variables();
daemon/unit_test.c
+36
@@ -404,6 +404,42 @@ int unit_test_buffer() {
404
return 0;
405
}
406
407
+int unit_test_static_threads() {
408
+ struct netdata_static_thread *static_threads = static_threads_get();
409
+
410
+ /*
411
+ * make sure enough static threads have been registered
412
+ */
413
+ if (!static_threads) {
414
+ fprintf(stderr, "empty static_threads array\n");
415
+ return 1;
416
+ }
417
+
418
+ int n;
419
+ for (n = 0; static_threads[n].start_routine != NULL; n++) {}
420
+
421
+ if (n < 2) {
422
+ fprintf(stderr, "only %d static threads registered", n);
423
+ return 1;
424
+ }
425
+
426
+ /*
427
+ * verify that each thread's start routine is unique.
428
+ */
429
+ for (int i = 0; i != n - 1; i++) {
430
+ for (int j = i + 1; j != n; j++) {
431
+ if (static_threads[i].start_routine != static_threads[j].start_routine)
432
+ continue;
433
+
434
+ fprintf(stderr, "Found duplicate threads with name: %s\n", static_threads[i].name);
435
+ return 1;
436
+ }
437
+ }
438
+
439
+ free(static_threads);
440
+ return 0;
441
+}
442
+
443
// --------------------------------------------------------------------------------------------------------------------
444
445
struct feed_values {
daemon/unit_test.h
+1
@@ -8,6 +8,7 @@ extern int unit_test(long delay, long shift);
8
extern int run_all_mockup_tests(void);
9
extern int unit_test_str2ld(void);
10
extern int unit_test_buffer(void);
11
+extern int unit_test_static_threads(void);
12
extern int test_sqlite(void);
13
#ifdef ENABLE_DBENGINE
14
extern int test_dbengine(void);