fix proc interrupts name parsing (#22556)
* fix proc interrupts name parsing * address proc interrupts sonar findings
Aryan Katiyar committed
May 28, 2026 at 16:19 UTC
63dc86dd3e3afb5ef05900d2c3eb11836a29b8bc
3 files changed
+313
-17
src/collectors/proc.plugin/plugin_proc.h
+1
@@ -25,6 +25,7 @@ int do_proc_net_rpc_nfsd(int update_every, usec_t dt);
25
int do_proc_sys_fs_file_nr(int update_every, usec_t dt);
26
int do_proc_sys_kernel_random_entropy_avail(int update_every, usec_t dt);
27
int do_proc_interrupts(int update_every, usec_t dt);
28
+int proc_interrupts_unittest(void);
29
int do_proc_softirqs(int update_every, usec_t dt);
30
int do_proc_pressure(int update_every, usec_t dt);
31
int do_sys_kernel_mm_ksm(int update_every, usec_t dt);
src/collectors/proc.plugin/proc_interrupts.c
+304
-17
@@ -28,6 +28,171 @@ struct interrupt {
28
// given a base, get a pointer to each record
29
#define irrindex(base, line, cpus) ((struct interrupt *)&((char *)(base))[(line) * recordsize(cpus)])
30
31
+static inline void proc_interrupts_keep_colon_in_words(procfile *ff) {
32
+ if(ff)
33
+ ff->separators[(uint8_t)':'] = PF_CHAR_IS_WORD;
34
+}
35
+
36
+static inline char *proc_interrupts_lineword(procfile *ff, size_t line, size_t word) {
37
+ if(!ff)
38
+ return NULL;
39
+
40
+ if(!ff->lines)
41
+ return NULL;
42
+
43
+ if(!ff->words)
44
+ return NULL;
45
+
46
+ if(line >= ff->lines->len)
47
+ return NULL;
48
+
49
+ ffline *pfline = &ff->lines->lines[line];
50
+ if(word >= pfline->words)
51
+ return NULL;
52
+
53
+ size_t word_index = (size_t)pfline->first + word;
54
+ if(word_index >= ff->words->len)
55
+ return NULL;
56
+
57
+ return ff->words->words[word_index];
58
+}
59
+
60
+static inline bool proc_interrupts_word_is_number(const char *word) {
61
+ if(word == NULL)
62
+ return false;
63
+
64
+ const unsigned char *p = (const unsigned char *)word;
65
+ if(*p == '\0')
66
+ return false;
67
+
68
+ while(*p) {
69
+ if(!isdigit(*p))
70
+ return false;
71
+
72
+ p++;
73
+ }
74
+
75
+ return true;
76
+}
77
+
78
+static inline bool proc_interrupts_word_is_trigger(const char *word) {
79
+ if(!word)
80
+ return false;
81
+
82
+ return (
83
+ strcasecmp(word, "edge") == 0 ||
84
+ strcasecmp(word, "level") == 0 ||
85
+ strcasecmp(word, "fasteoi") == 0);
86
+}
87
+
88
+static inline bool proc_interrupts_word_has_trigger_suffix(const char *word) {
89
+ const char *separator = word ? strrchr(word, '-') : NULL;
90
+ return separator && proc_interrupts_word_is_trigger(separator + 1);
91
+}
92
+
93
+static size_t proc_interrupts_name_first_word(procfile *ff, size_t line, int cpus, size_t words) {
94
+ size_t first = (size_t)cpus + 1;
95
+ if(unlikely(first >= words))
96
+ return words;
97
+
98
+ // Skip the interrupt controller/chip column.
99
+ first++;
100
+
101
+ while(first < words) {
102
+ const char *word = proc_interrupts_lineword(ff, line, first);
103
+ if(proc_interrupts_word_is_number(word) ||
104
+ proc_interrupts_word_is_trigger(word) ||
105
+ proc_interrupts_word_has_trigger_suffix(word))
106
+ first++;
107
+ else
108
+ break;
109
+ }
110
+
111
+ return first;
112
+}
113
+
114
+static inline char proc_interrupts_name_char(char c) {
115
+ return (c == ':' || isspace((uint8_t)c)) ? '_' : c;
116
+}
117
+
118
+static size_t proc_interrupts_strnlen(const char *s, size_t max) {
119
+ size_t len = 0;
120
+
121
+ if(!s)
122
+ return 0;
123
+
124
+ while(len < max && s[len])
125
+ len++;
126
+
127
+ return len;
128
+}
129
+
130
+static inline void proc_interrupts_append_char(char *dst, size_t *len, char c) {
131
+ if(*len < MAX_INTERRUPT_NAME) {
132
+ dst[*len] = c;
133
+ (*len)++;
134
+ dst[*len] = '\0';
135
+ }
136
+}
137
+
138
+static void proc_interrupts_append_id(char *name, const char *id, size_t idlen) {
139
+ if(!id)
140
+ return;
141
+
142
+ if(!idlen)
143
+ return;
144
+
145
+ if(unlikely(idlen >= MAX_INTERRUPT_NAME)) {
146
+ strncpyz(name, id, MAX_INTERRUPT_NAME);
147
+ return;
148
+ }
149
+
150
+ size_t nlen = proc_interrupts_strnlen(name, MAX_INTERRUPT_NAME);
151
+ if(likely(nlen + 1 + idlen <= MAX_INTERRUPT_NAME)) {
152
+ name[nlen] = '_';
153
+ strncpyz(&name[nlen + 1], id, MAX_INTERRUPT_NAME - nlen - 1);
154
+ }
155
+ else {
156
+ name[MAX_INTERRUPT_NAME - idlen - 1] = '_';
157
+ strncpyz(&name[MAX_INTERRUPT_NAME - idlen], id, idlen);
158
+ }
159
+}
160
+
161
+static void proc_interrupts_build_name(
162
+ char *dst,
163
+ procfile *ff,
164
+ size_t line,
165
+ size_t first_name_word,
166
+ size_t words,
167
+ const char *id,
168
+ size_t idlen) {
169
+ dst[0] = '\0';
170
+
171
+ size_t len = 0;
172
+ for(size_t w = first_name_word; w < words ;w++) {
173
+ const char *word = proc_interrupts_lineword(ff, line, w);
174
+ if(!word)
175
+ continue;
176
+
177
+ if(!*word)
178
+ continue;
179
+
180
+ if(len && dst[len - 1] != '_')
181
+ proc_interrupts_append_char(dst, &len, '_');
182
+
183
+ while(*word) {
184
+ char c = proc_interrupts_name_char(*word++);
185
+ if(c != '_' || !len || dst[len - 1] != '_')
186
+ proc_interrupts_append_char(dst, &len, c);
187
+ }
188
+ }
189
+
190
+ if(likely(len))
191
+ proc_interrupts_append_id(dst, id, idlen);
192
+ else
193
+ strncpyz(dst, id, MAX_INTERRUPT_NAME);
194
+}
195
+
196
static inline struct interrupt *get_interrupts_array(size_t lines, int cpus) {
197
static struct interrupt *irrs = NULL;
198
static size_t allocated = 0;
@@ -66,6 +231,7 @@ int do_proc_interrupts(int update_every, usec_t dt) {
231
char filename[FILENAME_MAX + 1];
232
snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/interrupts");
233
ff = procfile_open(inicfg_get(&netdata_config, CONFIG_SECTION_PLUGIN_PROC_INTERRUPTS, "filename to monitor", filename), " \t:", PROCFILE_FLAG_DEFAULT);
234
+ proc_interrupts_keep_colon_in_words(ff);
235
}
236
if(unlikely(!ff))
237
return 1;
@@ -87,7 +253,11 @@ int do_proc_interrupts(int update_every, usec_t dt) {
253
uint32_t w;
254
cpus = 0;
255
for(w = 0; w < words ; w++) {
90
- if(likely(strncmp(procfile_lineword(ff, 0, w), "CPU", 3) == 0))
256
+ const char *word = proc_interrupts_lineword(ff, 0, w);
257
+ if(!word)
258
+ continue;
259
+
260
+ if(likely(strncmp(word, "CPU", 3) == 0))
261
cpus++;
262
}
263
}
@@ -110,8 +280,9 @@ int do_proc_interrupts(int update_every, usec_t dt) {
280
words = procfile_linewords(ff, l);
281
if(unlikely(!words)) continue;
282
113
- irr->id = procfile_lineword(ff, l, 0);
114
- if(unlikely(!irr->id || !irr->id[0])) continue;
283
+ irr->id = proc_interrupts_lineword(ff, l, 0);
284
+ if(unlikely(!irr->id)) continue;
285
+ if(unlikely(!irr->id[0])) continue;
286
287
size_t idlen = strlen(irr->id);
288
if(irr->id[idlen - 1] == ':')
@@ -119,25 +290,18 @@ int do_proc_interrupts(int update_every, usec_t dt) {
290
291
int c;
292
for(c = 0; c < cpus ;c++) {
293
+ char *word = NULL;
294
if(likely((c + 1) < (int)words))
123
- irr->cpu[c].value = str2ull(procfile_lineword(ff, l, (uint32_t) (c + 1)), NULL);
124
- else
125
- irr->cpu[c].value = 0;
295
+ word = proc_interrupts_lineword(ff, l, (uint32_t)(c + 1));
296
+
297
+ irr->cpu[c].value = word ? str2ull(word, NULL) : 0;
298
299
irr->total += irr->cpu[c].value;
300
}
301
130
- if(unlikely(isdigit(irr->id[0]) && (uint32_t)(cpus + 2) < words)) {
131
- strncpyz(irr->name, procfile_lineword(ff, l, words - 1), MAX_INTERRUPT_NAME);
132
- size_t nlen = strlen(irr->name);
133
- if(likely(nlen + 1 + idlen <= MAX_INTERRUPT_NAME)) {
134
- irr->name[nlen] = '_';
135
- strncpyz(&irr->name[nlen + 1], irr->id, MAX_INTERRUPT_NAME - nlen - 1);
136
- }
137
- else {
138
- irr->name[MAX_INTERRUPT_NAME - idlen - 1] = '_';
139
- strncpyz(&irr->name[MAX_INTERRUPT_NAME - idlen], irr->id, idlen);
140
- }
302
+ if(unlikely(isdigit(irr->id[0]))) {
303
+ size_t first_name_word = proc_interrupts_name_first_word(ff, l, cpus, words);
304
+ proc_interrupts_build_name(irr->name, ff, l, first_name_word, words, irr->id, idlen);
305
}
306
else {
307
strncpyz(irr->name, irr->id, MAX_INTERRUPT_NAME);
@@ -243,3 +407,126 @@ int do_proc_interrupts(int update_every, usec_t dt) {
407
408
return 0;
409
}
410
+
411
+int proc_interrupts_unittest(void) {
412
+#if !defined(__NR_memfd_create) || !defined(MFD_CLOEXEC)
413
+ fprintf(stderr, "%s skipped, memfd_create() is not available.\n", __FUNCTION__);
414
+ return 0;
415
+#else
416
+ int fd = (int)syscall(__NR_memfd_create, "netdata-proc-interrupts", MFD_CLOEXEC);
417
+ if(fd == -1) {
418
+ fprintf(stderr, "Cannot create in-memory /proc/interrupts fixture: %s\n", strerror(errno));
419
+ return 1;
420
+ }
421
+
422
+ static const char fixture[] =
423
+ " CPU0 CPU1\n"
424
+ "240: 1 2 PCI-MSI 1572864-edge mlx5_comp40@pci:0000:86:00.0\n"
425
+ "250: 3 4 PCI-MSI 5242880-edge nvme 0 io5\n"
426
+ " 0: 5 6 IO-APIC 2-edge timer\n"
427
+ " 1: 7 8 XT-PIC-XT keyboard\n"
428
+ " 27: 9 10 GICv3 27 Level arch_timer\n";
429
+
430
+ if(write(fd, fixture, sizeof(fixture) - 1) != (ssize_t)sizeof(fixture) - 1) {
431
+ fprintf(stderr, "Cannot write in-memory /proc/interrupts fixture: %s\n", strerror(errno));
432
+ close(fd);
433
+ return 1;
434
+ }
435
+
436
+ if(lseek(fd, 0, SEEK_SET) == -1) {
437
+ fprintf(stderr, "Cannot rewind in-memory /proc/interrupts fixture: %s\n", strerror(errno));
438
+ close(fd);
439
+ return 1;
440
+ }
441
+
442
+ char filename[FILENAME_MAX + 1];
443
+ snprintfz(filename, FILENAME_MAX, "/proc/self/fd/%d", fd);
444
+
445
+ procfile *ff = procfile_open(filename, " \t:", PROCFILE_FLAG_DEFAULT);
446
+ proc_interrupts_keep_colon_in_words(ff);
447
+ close(fd);
448
+
449
+ if(!ff)
450
+ return 1;
451
+
452
+ ff = procfile_readall(ff);
453
+ if(!ff)
454
+ return 1;
455
+
456
+ size_t words = procfile_linewords(ff, 0);
457
+ int cpus = 0;
458
+ for(uint32_t w = 0; w < words ;w++) {
459
+ const char *word = proc_interrupts_lineword(ff, 0, w);
460
+ if(!word)
461
+ continue;
462
+
463
+ if(strncmp(word, "CPU", 3) == 0)
464
+ cpus++;
465
+ }
466
+
467
+ static const char *expected[] = {
468
+ "mlx5_comp40@pci_0000_86_00.0_240",
469
+ "nvme_0_io5_250",
470
+ "timer_0",
471
+ "keyboard_1",
472
+ "arch_timer_27",
473
+ };
474
+
475
+ int rc = 0;
476
+ size_t expected_idx = 0;
477
+ for(size_t line = 1; line < procfile_lines(ff) ;line++) {
478
+ words = procfile_linewords(ff, line);
479
+ if(!words)
480
+ continue;
481
+
482
+ if(expected_idx >= _countof(expected)) {
483
+ fprintf(stderr, "proc_interrupts_unittest found unexpected parsed line %zu\n", line);
484
+ rc = 1;
485
+ break;
486
+ }
487
+
488
+ char id[MAX_INTERRUPT_NAME + 1];
489
+ const char *id_word = proc_interrupts_lineword(ff, line, 0);
490
+ if(!id_word) {
491
+ rc = 1;
492
+ break;
493
+ }
494
+
495
+ strncpyz(id, id_word, MAX_INTERRUPT_NAME);
496
+ size_t idlen = proc_interrupts_strnlen(id, MAX_INTERRUPT_NAME);
497
+ if(idlen && id[idlen - 1] == ':')
498
+ id[--idlen] = '\0';
499
+
500
+ size_t first_name_word = proc_interrupts_name_first_word(ff, line, cpus, words);
501
+
502
+ char name[MAX_INTERRUPT_NAME + 1];
503
+ proc_interrupts_build_name(name, ff, line, first_name_word, words, id, idlen);
504
+
505
+ if(strcmp(name, expected[expected_idx]) != 0) {
506
+ fprintf(
507
+ stderr,
508
+ "proc_interrupts_unittest line %zu expected '%s', got '%s'\n",
509
+ line,
510
+ expected[expected_idx],
511
+ name);
512
+ rc = 1;
513
+ break;
514
+ }
515
+
516
+ expected_idx++;
517
+ }
518
+
519
+ if(!rc && expected_idx != _countof(expected)) {
520
+ fprintf(
521
+ stderr,
522
+ "proc_interrupts_unittest expected %zu parsed lines, got %zu\n",
523
+ _countof(expected),
524
+ expected_idx);
525
+ rc = 1;
526
+ }
527
+
528
+ procfile_close(ff);
529
+
530
+ return rc;
531
+#endif
532
+}
src/daemon/unit_test.c
+8
@@ -3,6 +3,10 @@
3
#include "common.h"
4
#include "web/api/formatters/rrd2json.h"
5
6
+#if defined(OS_LINUX)
7
+#include "collectors/proc.plugin/plugin_proc.h"
8
+#endif
9
+
10
static bool cmd_arg_sanitization_test(const char *expected, const char *src, char *dst, size_t dst_size) {
11
bool ok = sanitize_command_argument_string(dst, src, dst_size);
12
@@ -1530,6 +1534,10 @@ int run_all_mockup_tests(void)
1534
if(check_rrdcalc_comparisons())
1535
return 1;
1536
1537
+#if defined(OS_LINUX)
1538
+ if(proc_interrupts_unittest())
1539
+ return 1;
1540
+#endif
1541
if(test_incremental_sum_lookup_respects_update_every())
1542
return 1;
1543