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