]> arthur.barton.de Git - netdata.git/blob - src/procfile.c
Merge pull request #277 from jgeromero/tomcat.chart.sh
[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 #include "../config.h"
24
25 #define PF_PREFIX "PROCFILE"
26
27 #define PFWORDS_INCREASE_STEP 200
28 #define PFLINES_INCREASE_STEP 10
29 #define PROCFILE_INCREMENT_BUFFER 512
30
31 int procfile_adaptive_initial_allocation = 0;
32
33 // if adaptive allocation is set, these store the
34 // max values we have seen so far
35 uint32_t procfile_max_lines = PFLINES_INCREASE_STEP;
36 uint32_t procfile_max_words = PFWORDS_INCREASE_STEP;
37 size_t procfile_max_allocation = PROCFILE_INCREMENT_BUFFER;
38
39 // ----------------------------------------------------------------------------
40 // An array of words
41
42
43 pfwords *pfwords_add(pfwords *fw, char *str) {
44         // debug(D_PROCFILE, PF_PREFIX ":       adding word No %d: '%s'", fw->len, str);
45
46         if(unlikely(fw->len == fw->size)) {
47                 // debug(D_PROCFILE, PF_PREFIX ":       expanding words");
48
49                 pfwords *new = realloc(fw, sizeof(pfwords) + (fw->size + PFWORDS_INCREASE_STEP) * sizeof(char *));
50                 if(unlikely(!new)) {
51                         error(PF_PREFIX ":      failed to expand words");
52                         free(fw);
53                         return NULL;
54                 }
55                 fw = new;
56                 fw->size += PFWORDS_INCREASE_STEP;
57         }
58
59         fw->words[fw->len++] = str;
60
61         return fw;
62 }
63
64 pfwords *pfwords_new(void) {
65         // debug(D_PROCFILE, PF_PREFIX ":       initializing words");
66
67         uint32_t size = (procfile_adaptive_initial_allocation) ? procfile_max_words : PFWORDS_INCREASE_STEP;
68
69         pfwords *new = malloc(sizeof(pfwords) + size * sizeof(char *));
70         if(unlikely(!new)) return NULL;
71
72         new->len = 0;
73         new->size = size;
74         return new;
75 }
76
77 void pfwords_reset(pfwords *fw) {
78         // debug(D_PROCFILE, PF_PREFIX ":       reseting words");
79         fw->len = 0;
80 }
81
82 void pfwords_free(pfwords *fw) {
83         // debug(D_PROCFILE, PF_PREFIX ":       freeing words");
84
85         free(fw);
86 }
87
88
89 // ----------------------------------------------------------------------------
90 // An array of lines
91
92 pflines *pflines_add(pflines *fl, uint32_t first_word) {
93         // debug(D_PROCFILE, PF_PREFIX ":       adding line %d at word %d", fl->len, first_word);
94
95         if(unlikely(fl->len == fl->size)) {
96                 // debug(D_PROCFILE, PF_PREFIX ":       expanding lines");
97
98                 pflines *new = realloc(fl, sizeof(pflines) + (fl->size + PFLINES_INCREASE_STEP) * sizeof(ffline));
99                 if(unlikely(!new)) {
100                         error(PF_PREFIX ":      failed to expand lines");
101                         free(fl);
102                         return NULL;
103                 }
104                 fl = new;
105                 fl->size += PFLINES_INCREASE_STEP;
106         }
107
108         fl->lines[fl->len].words = 0;
109         fl->lines[fl->len++].first = first_word;
110
111         return fl;
112 }
113
114 pflines *pflines_new(void) {
115         // debug(D_PROCFILE, PF_PREFIX ":       initializing lines");
116
117         uint32_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_words : PFLINES_INCREASE_STEP;
118
119         pflines *new = malloc(sizeof(pflines) + size * sizeof(ffline));
120         if(unlikely(!new)) return NULL;
121
122         new->len = 0;
123         new->size = size;
124         return new;
125 }
126
127 void pflines_reset(pflines *fl) {
128         // debug(D_PROCFILE, PF_PREFIX ":       reseting lines");
129
130         fl->len = 0;
131 }
132
133 void pflines_free(pflines *fl) {
134         // debug(D_PROCFILE, PF_PREFIX ":       freeing lines");
135
136         free(fl);
137 }
138
139
140 // ----------------------------------------------------------------------------
141 // The procfile
142
143 #define PF_CHAR_IS_SEPARATOR    ' '
144 #define PF_CHAR_IS_NEWLINE              'N'
145 #define PF_CHAR_IS_WORD                 'W'
146 #define PF_CHAR_IS_QUOTE        'Q'
147 #define PF_CHAR_IS_OPEN         'O'
148 #define PF_CHAR_IS_CLOSE        'C'
149
150 void procfile_close(procfile *ff) {
151         debug(D_PROCFILE, PF_PREFIX ": Closing file '%s'", ff->filename);
152
153         if(likely(ff->lines)) pflines_free(ff->lines);
154         if(likely(ff->words)) pfwords_free(ff->words);
155
156         if(likely(ff->fd != -1)) close(ff->fd);
157         free(ff);
158 }
159
160 procfile *procfile_parser(procfile *ff) {
161         debug(D_PROCFILE, PF_PREFIX ": Parsing file '%s'", ff->filename);
162
163         char *s = ff->data, *e = &ff->data[ff->len], *t = ff->data, quote = 0;
164         uint32_t l = 0, w = 0;
165         int opened = 0;
166
167         ff->lines = pflines_add(ff->lines, w);
168         if(unlikely(!ff->lines)) goto cleanup;
169
170         while(likely(s < e)) {
171                 // we are not at the end
172
173                 switch(ff->separators[(int)(*s)]) {
174                         case PF_CHAR_IS_OPEN:
175                                 if(s == t) {
176                                         opened++;
177                                         t = ++s;
178                                 }
179                                 else if(opened) {
180                                         opened++;
181                                         s++;
182                                 }
183                                 else
184                                         s++;
185                                 continue;
186
187                         case PF_CHAR_IS_CLOSE:
188                                 if(opened) {
189                                         opened--;
190
191                                         if(!opened) {
192                                                 *s = '\0';
193                                                 ff->words = pfwords_add(ff->words, t);
194                                                 if(unlikely(!ff->words)) goto cleanup;
195
196                                                 ff->lines->lines[l].words++;
197                                                 w++;
198
199                                                 t = ++s;
200                                         }
201                                         else
202                                                 s++;
203                                 }
204                                 else
205                                         s++;
206                                 continue;
207
208                         case PF_CHAR_IS_QUOTE:
209                                 if(unlikely(!quote && s == t)) {
210                                         // quote opened at the beginning
211                                         quote = *s;
212                                         t = ++s;
213                                 }
214                                 else if(unlikely(quote && quote == *s)) {
215                                         // quote closed
216                                         quote = 0;
217
218                                         *s = '\0';
219                                         ff->words = pfwords_add(ff->words, t);
220                                         if(unlikely(!ff->words)) goto cleanup;
221
222                                         ff->lines->lines[l].words++;
223                                         w++;
224
225                                         t = ++s;
226                                 }
227                                 else
228                                         s++;
229                                 continue;
230
231                         case PF_CHAR_IS_SEPARATOR:
232                                 if(unlikely(quote || opened)) {
233                                         // we are inside a quote
234                                         s++;
235                                         continue;
236                                 }
237
238                                 if(unlikely(s == t)) {
239                                         // skip all leading white spaces
240                                         t = ++s;
241                                         continue;
242                                 }
243
244                                 // end of word
245                                 *s = '\0';
246
247                                 ff->words = pfwords_add(ff->words, t);
248                                 if(unlikely(!ff->words)) goto cleanup;
249
250                                 ff->lines->lines[l].words++;
251                                 w++;
252
253                                 t = ++s;
254                                 continue;
255
256                         case PF_CHAR_IS_NEWLINE:
257                                 // end of line
258                                 *s = '\0';
259
260                                 ff->words = pfwords_add(ff->words, t);
261                                 if(unlikely(!ff->words)) goto cleanup;
262
263                                 ff->lines->lines[l].words++;
264                                 w++;
265
266                                 // debug(D_PROCFILE, PF_PREFIX ":       ended line %d with %d words", l, ff->lines->lines[l].words);
267
268                                 ff->lines = pflines_add(ff->lines, w);
269                                 if(unlikely(!ff->lines)) goto cleanup;
270                                 l++;
271
272                                 t = ++s;
273                                 continue;
274
275                         default:
276                                 s++;
277                                 continue;
278                 }
279         }
280
281         if(likely(s > t && t < e)) {
282                 // the last word
283                 if(likely(ff->len < ff->size))
284                         *s = '\0';
285                 else {
286                         // we are going to loose the last byte
287                         ff->data[ff->size - 1] = '\0';
288                 }
289
290                 ff->words = pfwords_add(ff->words, t);
291                 if(unlikely(!ff->words)) goto cleanup;
292
293                 ff->lines->lines[l].words++;
294                 w++;
295         }
296
297         return ff;
298
299 cleanup:
300         error(PF_PREFIX ": Failed to parse file '%s'", ff->filename);
301         procfile_close(ff);
302         return NULL;
303 }
304
305 procfile *procfile_readall(procfile *ff) {
306         debug(D_PROCFILE, PF_PREFIX ": Reading file '%s'.", ff->filename);
307
308         ssize_t s, r = 1, x;
309         ff->len = 0;
310
311         while(likely(r > 0)) {
312                 s = ff->len;
313                 x = ff->size - s;
314
315                 if(!x) {
316                         debug(D_PROCFILE, PF_PREFIX ": Expanding data buffer for file '%s'.", ff->filename);
317
318                         procfile *new = realloc(ff, sizeof(procfile) + ff->size + PROCFILE_INCREMENT_BUFFER);
319                         if(unlikely(!new)) {
320                                 error(PF_PREFIX ": Cannot allocate memory for file '%s'", ff->filename);
321                                 procfile_close(ff);
322                                 return NULL;
323                         }
324                         ff = new;
325                         ff->size += PROCFILE_INCREMENT_BUFFER;
326                 }
327
328                 debug(D_PROCFILE, "Reading file '%s', from position %ld with length %ld", ff->filename, s, ff->size - s);
329                 r = read(ff->fd, &ff->data[s], ff->size - s);
330                 if(unlikely(r == -1)) {
331                         if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot read from file '%s'", ff->filename);
332                         procfile_close(ff);
333                         return NULL;
334                 }
335
336                 ff->len += r;
337         }
338
339         debug(D_PROCFILE, "Rewinding file '%s'", ff->filename);
340         if(unlikely(lseek(ff->fd, 0, SEEK_SET) == -1)) {
341                 if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot rewind on file '%s'.", ff->filename);
342                 procfile_close(ff);
343                 return NULL;
344         }
345
346         pflines_reset(ff->lines);
347         pfwords_reset(ff->words);
348
349         ff = procfile_parser(ff);
350
351         if(unlikely(procfile_adaptive_initial_allocation)) {
352                 if(unlikely(ff->len > procfile_max_allocation)) procfile_max_allocation = ff->len;
353                 if(unlikely(ff->lines->len > procfile_max_lines)) procfile_max_lines = ff->lines->len;
354                 if(unlikely(ff->words->len > procfile_max_words)) procfile_max_words = ff->words->len;
355         }
356
357         debug(D_PROCFILE, "File '%s' updated.", ff->filename);
358         return ff;
359 }
360
361 static void procfile_set_separators(procfile *ff, const char *separators) {
362         static char def[256] = { [0 ... 255] = 0 };
363         int i;
364
365         if(unlikely(!def[255])) {
366                 // this is thread safe
367                 // we check that the last byte is non-zero
368                 // if it is zero, multiple threads may be executing this at the same time
369                 // setting in def[] the exact same values
370                 for(i = 0; likely(i < 256) ;i++) {
371                         if(unlikely(i == '\n' || i == '\r')) def[i] = PF_CHAR_IS_NEWLINE;
372                         else if(unlikely(isspace(i) || !isprint(i))) def[i] = PF_CHAR_IS_SEPARATOR;
373                         else def[i] = PF_CHAR_IS_WORD;
374                 }
375         }
376
377         // copy the default
378         char *ffs = ff->separators, *ffd = def, *ffe = &def[256];
379         while(likely(ffd != ffe)) *ffs++ = *ffd++;
380
381         // set the separators
382         if(unlikely(!separators))
383                 separators = " \t=|";
384
385         ffs = ff->separators;
386         const char *s = separators;
387         while(likely(*s))
388                 ffs[(int)*s++] = PF_CHAR_IS_SEPARATOR;
389 }
390
391 void procfile_set_quotes(procfile *ff, const char *quotes) {
392         // remove all quotes
393         int i;
394         for(i = 0; i < 256 ; i++)
395                 if(ff->separators[i] == PF_CHAR_IS_QUOTE)
396                         ff->separators[i] = PF_CHAR_IS_WORD;
397
398         // if nothing given, return
399         if(unlikely(!quotes || !*quotes))
400                 return;
401
402         // set the quotes
403         char *ffs = ff->separators;
404         const char *s = quotes;
405         while(likely(*s))
406                 ffs[(int)*s++] = PF_CHAR_IS_QUOTE;
407 }
408
409 void procfile_set_open_close(procfile *ff, const char *open, const char *close) {
410         // remove all open/close
411         int i;
412         for(i = 0; i < 256 ; i++)
413                 if(ff->separators[i] == PF_CHAR_IS_OPEN || ff->separators[i] == PF_CHAR_IS_CLOSE)
414                         ff->separators[i] = PF_CHAR_IS_WORD;
415
416         // if nothing given, return
417         if(unlikely(!open || !*open || !close || !*close))
418                 return;
419
420         // set the openings
421         char *ffs = ff->separators;
422         const char *s = open;
423         while(likely(*s))
424                 ffs[(int)*s++] = PF_CHAR_IS_OPEN;
425
426         s = close;
427         while(likely(*s))
428                 ffs[(int)*s++] = PF_CHAR_IS_CLOSE;
429 }
430
431 procfile *procfile_open(const char *filename, const char *separators, uint32_t flags) {
432         debug(D_PROCFILE, PF_PREFIX ": Opening file '%s'", filename);
433
434         int fd = open(filename, O_RDONLY, 0666);
435         if(unlikely(fd == -1)) {
436                 if(unlikely(!(flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot open file '%s'", filename);
437                 return NULL;
438         }
439
440         size_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_allocation : PROCFILE_INCREMENT_BUFFER;
441         procfile *ff = malloc(sizeof(procfile) + size);
442         if(unlikely(!ff)) {
443                 error(PF_PREFIX ": Cannot allocate memory for file '%s'", filename);
444                 close(fd);
445                 return NULL;
446         }
447
448         strncpy(ff->filename, filename, FILENAME_MAX);
449         ff->filename[FILENAME_MAX] = '\0';
450
451         ff->fd = fd;
452         ff->size = size;
453         ff->len = 0;
454         ff->flags = flags;
455
456         ff->lines = pflines_new();
457         ff->words = pfwords_new();
458
459         if(unlikely(!ff->lines || !ff->words)) {
460                 error(PF_PREFIX ": Cannot initialize parser for file '%s'", filename);
461                 procfile_close(ff);
462                 return NULL;
463         }
464
465         procfile_set_separators(ff, separators);
466
467         debug(D_PROCFILE, "File '%s' opened.", filename);
468         return ff;
469 }
470
471 procfile *procfile_reopen(procfile *ff, const char *filename, const char *separators, uint32_t flags) {
472         if(unlikely(!ff)) return procfile_open(filename, separators, flags);
473
474         if(likely(ff->fd != -1)) close(ff->fd);
475
476         ff->fd = open(filename, O_RDONLY, 0666);
477         if(unlikely(ff->fd == -1)) {
478                 procfile_close(ff);
479                 return NULL;
480         }
481
482         strncpy(ff->filename, filename, FILENAME_MAX);
483         ff->filename[FILENAME_MAX] = '\0';
484
485         ff->flags = flags;
486
487         // do not do the separators again if NULL is given
488         if(likely(separators)) procfile_set_separators(ff, separators);
489
490         return ff;
491 }
492
493 // ----------------------------------------------------------------------------
494 // example parsing of procfile data
495
496 void procfile_print(procfile *ff) {
497         uint32_t lines = procfile_lines(ff), l;
498         uint32_t words, w;
499         char *s;
500
501         debug(D_PROCFILE, "File '%s' with %d lines and %d words", ff->filename, ff->lines->len, ff->words->len);
502
503         for(l = 0; likely(l < lines) ;l++) {
504                 words = procfile_linewords(ff, l);
505
506                 debug(D_PROCFILE, "     line %d starts at word %d and has %d words", l, ff->lines->lines[l].first, ff->lines->lines[l].words);
507
508                 for(w = 0; likely(w < words) ;w++) {
509                         s = procfile_lineword(ff, l, w);
510                         debug(D_PROCFILE, "             [%d.%d] '%s'", l, w, s);
511                 }
512         }
513 }