]> arthur.barton.de Git - netdata.git/blob - src/procfile.c
fixed an issue where certain proc files did not read in full
[netdata.git] / src / procfile.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <string.h>
10 #include <malloc.h>
11 #include <ctype.h>
12 #include <time.h>
13 #include <sys/time.h>
14 #include <sys/wait.h>
15
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/mman.h>
19
20 #include "common.h"
21 #include "log.h"
22 #include "procfile.h"
23
24 #define PF_PREFIX "PROCFILE"
25
26 #define PFWORDS_INCREASE_STEP 200
27 #define PFLINES_INCREASE_STEP 10
28 #define PROCFILE_INCREMENT_BUFFER 512
29
30 int procfile_adaptive_initial_allocation = 0;
31
32 // if adaptive allocation is set, these store the
33 // max values we have seen so far
34 uint32_t procfile_max_lines = PFLINES_INCREASE_STEP;
35 uint32_t procfile_max_words = PFWORDS_INCREASE_STEP;
36 size_t procfile_max_allocation = PROCFILE_INCREMENT_BUFFER;
37
38 // ----------------------------------------------------------------------------
39 // An array of words
40
41
42 pfwords *pfwords_add(pfwords *fw, char *str) {
43         // debug(D_PROCFILE, PF_PREFIX ":       adding word No %d: '%s'", fw->len, str);
44
45         if(unlikely(fw->len == fw->size)) {
46                 // debug(D_PROCFILE, PF_PREFIX ":       expanding words");
47
48                 pfwords *new = realloc(fw, sizeof(pfwords) + (fw->size + PFWORDS_INCREASE_STEP) * sizeof(char *));
49                 if(unlikely(!new)) {
50                         error(PF_PREFIX ":      failed to expand words");
51                         free(fw);
52                         return NULL;
53                 }
54                 fw = new;
55                 fw->size += PFWORDS_INCREASE_STEP;
56         }
57
58         fw->words[fw->len++] = str;
59
60         return fw;
61 }
62
63 pfwords *pfwords_new(void) {
64         // debug(D_PROCFILE, PF_PREFIX ":       initializing words");
65
66         uint32_t size = (procfile_adaptive_initial_allocation) ? procfile_max_words : PFWORDS_INCREASE_STEP;
67
68         pfwords *new = malloc(sizeof(pfwords) + size * sizeof(char *));
69         if(unlikely(!new)) return NULL;
70
71         new->len = 0;
72         new->size = size;
73         return new;
74 }
75
76 void pfwords_reset(pfwords *fw) {
77         // debug(D_PROCFILE, PF_PREFIX ":       reseting words");
78         fw->len = 0;
79 }
80
81 void pfwords_free(pfwords *fw) {
82         // debug(D_PROCFILE, PF_PREFIX ":       freeing words");
83
84         free(fw);
85 }
86
87
88 // ----------------------------------------------------------------------------
89 // An array of lines
90
91 pflines *pflines_add(pflines *fl, uint32_t first_word) {
92         // debug(D_PROCFILE, PF_PREFIX ":       adding line %d at word %d", fl->len, first_word);
93
94         if(unlikely(fl->len == fl->size)) {
95                 // debug(D_PROCFILE, PF_PREFIX ":       expanding lines");
96
97                 pflines *new = realloc(fl, sizeof(pflines) + (fl->size + PFLINES_INCREASE_STEP) * sizeof(ffline));
98                 if(unlikely(!new)) {
99                         error(PF_PREFIX ":      failed to expand lines");
100                         free(fl);
101                         return NULL;
102                 }
103                 fl = new;
104                 fl->size += PFLINES_INCREASE_STEP;
105         }
106
107         fl->lines[fl->len].words = 0;
108         fl->lines[fl->len++].first = first_word;
109
110         return fl;
111 }
112
113 pflines *pflines_new(void) {
114         // debug(D_PROCFILE, PF_PREFIX ":       initializing lines");
115
116         uint32_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_words : PFLINES_INCREASE_STEP;
117
118         pflines *new = malloc(sizeof(pflines) + size * sizeof(ffline));
119         if(unlikely(!new)) return NULL;
120
121         new->len = 0;
122         new->size = size;
123         return new;
124 }
125
126 void pflines_reset(pflines *fl) {
127         // debug(D_PROCFILE, PF_PREFIX ":       reseting lines");
128
129         fl->len = 0;
130 }
131
132 void pflines_free(pflines *fl) {
133         // debug(D_PROCFILE, PF_PREFIX ":       freeing lines");
134
135         free(fl);
136 }
137
138
139 // ----------------------------------------------------------------------------
140 // The procfile
141
142 #define PF_CHAR_IS_SEPARATOR    ' '
143 #define PF_CHAR_IS_NEWLINE              'N'
144 #define PF_CHAR_IS_WORD                 'W'
145
146 void procfile_close(procfile *ff) {
147         debug(D_PROCFILE, PF_PREFIX ": Closing file '%s'", ff->filename);
148
149         if(likely(ff->lines)) pflines_free(ff->lines);
150         if(likely(ff->words)) pfwords_free(ff->words);
151
152         if(likely(ff->fd != -1)) close(ff->fd);
153         free(ff);
154 }
155
156 procfile *procfile_parser(procfile *ff) {
157         debug(D_PROCFILE, PF_PREFIX ": Parsing file '%s'", ff->filename);
158
159         char *s = ff->data, *e = ff->data, *t = ff->data;
160         uint32_t l = 0, w = 0;
161         e += ff->len;
162
163         ff->lines = pflines_add(ff->lines, w);
164         if(unlikely(!ff->lines)) goto cleanup;
165
166         while(likely(s < e)) {
167                 switch(ff->separators[(int)(*s)]) {
168                         case PF_CHAR_IS_SEPARATOR:
169                                 if(likely(s == t)) {
170                                         // skip all leading white spaces
171                                         t = ++s;
172                                         continue;
173                                 }
174
175                                 // end of word
176                                 *s = '\0';
177
178                                 ff->words = pfwords_add(ff->words, t);
179                                 if(unlikely(!ff->words)) goto cleanup;
180
181                                 ff->lines->lines[l].words++;
182                                 w++;
183
184                                 t = ++s;
185                                 continue;
186
187                         case PF_CHAR_IS_NEWLINE:
188                                 // end of line
189                                 *s = '\0';
190
191                                 ff->words = pfwords_add(ff->words, t);
192                                 if(unlikely(!ff->words)) goto cleanup;
193
194                                 ff->lines->lines[l].words++;
195                                 w++;
196
197                                 // debug(D_PROCFILE, PF_PREFIX ":       ended line %d with %d words", l, ff->lines->lines[l].words);
198
199                                 ff->lines = pflines_add(ff->lines, w);
200                                 if(unlikely(!ff->lines)) goto cleanup;
201                                 l++;
202
203                                 t = ++s;
204                                 continue;
205
206                         default:
207                                 s++;
208                                 continue;
209                 }
210         }
211
212         if(likely(s != t)) {
213                 // the last word
214                 if(likely(ff->len < ff->size)) *s = '\0';
215                 else {
216                         // we are going to loose the last byte
217                         ff->data[ff->size - 1] = '\0';
218                 }
219
220                 ff->words = pfwords_add(ff->words, t);
221                 if(unlikely(!ff->words)) goto cleanup;
222
223                 ff->lines->lines[l].words++;
224                 w++;
225         }
226
227         return ff;
228
229 cleanup:
230         error(PF_PREFIX ": Failed to parse file '%s'", ff->filename);
231         procfile_close(ff);
232         return NULL;
233 }
234
235 procfile *procfile_readall(procfile *ff) {
236         debug(D_PROCFILE, PF_PREFIX ": Reading file '%s'.", ff->filename);
237
238         ssize_t s, r = 1, x = ff->size;
239         ff->len = 0;
240
241         while(likely(r > 0)) {
242                 s = ff->len;
243                 x = ff->size - s;
244
245                 if(!x) {
246                         debug(D_PROCFILE, PF_PREFIX ": Expanding data buffer for file '%s'.", ff->filename);
247
248                         procfile *new = realloc(ff, sizeof(procfile) + ff->size + PROCFILE_INCREMENT_BUFFER);
249                         if(unlikely(!new)) {
250                                 error(PF_PREFIX ": Cannot allocate memory for file '%s'", ff->filename);
251                                 procfile_close(ff);
252                                 return NULL;
253                         }
254                         ff = new;
255                         ff->size += PROCFILE_INCREMENT_BUFFER;
256                         x = PROCFILE_INCREMENT_BUFFER;
257                 }
258
259                 debug(D_PROCFILE, "Reading file '%s', from position %ld with length %ld", ff->filename, s, ff->size - s);
260                 r = read(ff->fd, &ff->data[s], ff->size - s);
261                 if(unlikely(r == -1)) {
262                         if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot read from file '%s'", ff->filename);
263                         procfile_close(ff);
264                         return NULL;
265                 }
266
267                 ff->len += r;
268         }
269
270         debug(D_PROCFILE, "Rewinding file '%s'", ff->filename);
271         if(unlikely(lseek(ff->fd, 0, SEEK_SET) == -1)) {
272                 if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot rewind on file '%s'.", ff->filename);
273                 procfile_close(ff);
274                 return NULL;
275         }
276
277         pflines_reset(ff->lines);
278         pfwords_reset(ff->words);
279
280         ff = procfile_parser(ff);
281
282         if(unlikely(procfile_adaptive_initial_allocation)) {
283                 if(unlikely(ff->len > procfile_max_allocation)) procfile_max_allocation = ff->len;
284                 if(unlikely(ff->lines->len > procfile_max_lines)) procfile_max_lines = ff->lines->len;
285                 if(unlikely(ff->words->len > procfile_max_words)) procfile_max_words = ff->words->len;
286         }
287
288         debug(D_PROCFILE, "File '%s' updated.", ff->filename);
289         return ff;
290 }
291
292 static void procfile_set_separators(procfile *ff, const char *separators) {
293         static char def[256] = { [0 ... 255] = 0 };
294         int i;
295
296         if(unlikely(!def[255])) {
297                 // this is thread safe
298                 // we check that the last byte is non-zero
299                 // if it is zero, multiple threads may be executing this at the same time
300                 // setting in def[] the exact same values
301                 for(i = 0; likely(i < 256) ;i++) {
302                         if(unlikely(i == '\n' || i == '\r')) def[i] = PF_CHAR_IS_NEWLINE;
303                         else if(unlikely(isspace(i) || !isprint(i))) def[i] = PF_CHAR_IS_SEPARATOR;
304                         else def[i] = PF_CHAR_IS_WORD;
305                 }
306         }
307
308         // copy the default
309         char *ffs = ff->separators, *ffd = def, *ffe = &def[256];
310         while(likely(ffd != ffe)) *ffs++ = *ffd++;
311
312         // set the separators
313         if(unlikely(!separators)) separators = " \t=|";
314         ffs = ff->separators;
315         const char *s = separators;
316         while(likely(*s)) ffs[(int)*s++] = PF_CHAR_IS_SEPARATOR;
317 }
318
319 procfile *procfile_open(const char *filename, const char *separators, uint32_t flags) {
320         debug(D_PROCFILE, PF_PREFIX ": Opening file '%s'", filename);
321
322         int fd = open(filename, O_RDONLY, 0666);
323         if(unlikely(fd == -1)) {
324                 if(unlikely(!(flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot open file '%s'", filename);
325                 return NULL;
326         }
327
328         size_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_allocation : PROCFILE_INCREMENT_BUFFER;
329         procfile *ff = malloc(sizeof(procfile) + size);
330         if(unlikely(!ff)) {
331                 error(PF_PREFIX ": Cannot allocate memory for file '%s'", filename);
332                 close(fd);
333                 return NULL;
334         }
335
336         strncpy(ff->filename, filename, FILENAME_MAX);
337         ff->filename[FILENAME_MAX] = '\0';
338
339         ff->fd = fd;
340         ff->size = size;
341         ff->len = 0;
342         ff->flags = flags;
343
344         ff->lines = pflines_new();
345         ff->words = pfwords_new();
346
347         if(unlikely(!ff->lines || !ff->words)) {
348                 error(PF_PREFIX ": Cannot initialize parser for file '%s'", filename);
349                 procfile_close(ff);
350                 return NULL;
351         }
352
353         procfile_set_separators(ff, separators);
354
355         debug(D_PROCFILE, "File '%s' opened.", filename);
356         return ff;
357 }
358
359 procfile *procfile_reopen(procfile *ff, const char *filename, const char *separators, uint32_t flags) {
360         if(unlikely(!ff)) return procfile_open(filename, separators, flags);
361
362         if(likely(ff->fd != -1)) close(ff->fd);
363
364         ff->fd = open(filename, O_RDONLY, 0666);
365         if(unlikely(ff->fd == -1)) {
366                 procfile_close(ff);
367                 return NULL;
368         }
369
370         strncpy(ff->filename, filename, FILENAME_MAX);
371         ff->filename[FILENAME_MAX] = '\0';
372
373         ff->flags = flags;
374
375         // do not do the separators again if NULL is given
376         if(likely(separators)) procfile_set_separators(ff, separators);
377
378         return ff;
379 }
380
381 // ----------------------------------------------------------------------------
382 // example parsing of procfile data
383
384 void procfile_print(procfile *ff) {
385         uint32_t lines = procfile_lines(ff), l;
386         uint32_t words, w;
387         char *s;
388
389         debug(D_PROCFILE, "File '%s' with %d lines and %d words", ff->filename, ff->lines->len, ff->words->len);
390
391         for(l = 0; likely(l < lines) ;l++) {
392                 words = procfile_linewords(ff, l);
393
394                 debug(D_PROCFILE, "     line %d starts at word %d and has %d words", l, ff->lines->lines[l].first, ff->lines->lines[l].words);
395
396                 for(w = 0; likely(w < words) ;w++) {
397                         s = procfile_lineword(ff, l, w);
398                         debug(D_PROCFILE, "             [%d.%d] '%s'", l, w, s);
399                 }
400         }
401 }