[armoring feature] apply the system hardening options.
[platform/upstream/iproute2.git] / misc / lnstat_util.c
1 /* lnstat.c:  Unified linux network statistics
2  *
3  * Copyright (C) 2004 by Harald Welte <laforge@gnumonks.org>
4  *
5  * Development of this code was funded by Astaro AG, http://www.astaro.com/
6  *
7  * Based on original concept and ideas from predecessor rtstat.c:
8  *
9  * Copyright 2001 by Robert Olsson <robert.olsson@its.uu.se>
10  *                                 Uppsala University, Sweden
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  */
18
19 #include <unistd.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <dirent.h>
24 #include <limits.h>
25 #include <time.h>
26
27 #include <sys/time.h>
28 #include <sys/types.h>
29
30 #include "lnstat.h"
31
32 /* size of temp buffer used to read lines from procfiles */
33 #define FGETS_BUF_SIZE 1024
34
35
36 #define RTSTAT_COMPAT_LINE "entries  in_hit in_slow_tot in_no_route in_brd in_martian_dst in_martian_src  out_hit out_slow_tot out_slow_mc  gc_total gc_ignored gc_goal_miss gc_dst_overflow in_hlist_search out_hlist_search\n"
37
38 /* Read (and summarize for SMP) the different stats vars. */
39 static int scan_lines(struct lnstat_file *lf, int i)
40 {
41         int j, num_lines = 0;
42
43         for (j = 0; j < lf->num_fields; j++)
44                 lf->fields[j].values[i] = 0;
45
46         while(!feof(lf->fp)) {
47                 char buf[FGETS_BUF_SIZE];
48                 char *ptr = buf;
49
50                 num_lines++;
51
52                 if (fgets(buf, sizeof(buf)-1, lf->fp) == NULL)
53                         fprintf(stderr, "Error: failed to read from input file stream.\n");
54                 gettimeofday(&lf->last_read, NULL);
55
56                 for (j = 0; j < lf->num_fields; j++) {
57                         unsigned long f = strtoul(ptr, &ptr, 16);
58                         if (j == 0)
59                                 lf->fields[j].values[i] = f;
60                         else
61                                 lf->fields[j].values[i] += f;
62                 }
63         }
64         return num_lines;
65 }
66
67 static int time_after(struct timeval *last,
68                       struct timeval *tout,
69                       struct timeval *now)
70 {
71         if (now->tv_sec > last->tv_sec + tout->tv_sec)
72                 return 1;
73
74         if (now->tv_sec == last->tv_sec + tout->tv_sec) {
75                 if (now->tv_usec > last->tv_usec + tout->tv_usec)
76                         return 1;
77         }
78
79         return 0;
80 }
81
82 int lnstat_update(struct lnstat_file *lnstat_files)
83 {
84         struct lnstat_file *lf;
85         char buf[FGETS_BUF_SIZE];
86         struct timeval tv;
87
88         gettimeofday(&tv, NULL);
89
90         for (lf = lnstat_files; lf; lf = lf->next) {
91                 if (time_after(&lf->last_read, &lf->interval, &tv)) {
92                         int i;
93                         struct lnstat_field *lfi;
94
95                         rewind(lf->fp);
96                         if (!lf->compat) {
97                                 /* skip first line */
98                                 if (fgets(buf, sizeof(buf)-1, lf->fp) == NULL)
99                                         fprintf(stderr, "Error: failed to read from input file stream.\n");
100                         }
101                         scan_lines(lf, 1);
102
103                         for (i = 0, lfi = &lf->fields[i];
104                                         i < lf->num_fields; i++, lfi = &lf->fields[i]) {
105                                 if (i == 0)
106                                         lfi->result = lfi->values[1];
107                                 else
108                                         lfi->result = (lfi->values[1]-lfi->values[0])
109                                                 / lf->interval.tv_sec;
110                         }
111
112                         rewind(lf->fp);
113                         if (fgets(buf, sizeof(buf)-1, lf->fp) == NULL)
114                                 fprintf(stderr, "Error: failed to read from input file stream.\n");
115                         scan_lines(lf, 0);
116                 }
117         }
118
119         return 0;
120 }
121
122 /* scan first template line and fill in per-field data structures */
123 static int __lnstat_scan_fields(struct lnstat_file *lf, char *buf)
124 {
125         char *tok;
126         int i;
127
128         tok = strtok(buf, " \t\n");
129         for (i = 0; i < LNSTAT_MAX_FIELDS_PER_LINE; i++) {
130                 lf->fields[i].file = lf;
131                 strncpy(lf->fields[i].name, tok, LNSTAT_MAX_FIELD_NAME_LEN);
132                 /* has to be null-terminate since we initialize to zero
133                  * and field size is NAME_LEN + 1 */
134                 tok = strtok(NULL, " \t\n");
135                 if (!tok) {
136                         lf->num_fields = i+1;
137                         return 0;
138                 }
139         }
140         return 0;
141 }
142
143 static int lnstat_scan_fields(struct lnstat_file *lf)
144 {
145         char buf[FGETS_BUF_SIZE];
146
147         rewind(lf->fp);
148         if (fgets(buf, sizeof(buf)-1, lf->fp) == NULL)
149                 fprintf(stderr, "Error: failed to read from input file stream.\n");
150
151         return __lnstat_scan_fields(lf, buf);
152 }
153
154 /* fake function emulating lnstat_scan_fields() for old kernels */
155 static int lnstat_scan_compat_rtstat_fields(struct lnstat_file *lf)
156 {
157         char buf[FGETS_BUF_SIZE];
158
159         strncpy(buf, RTSTAT_COMPAT_LINE, sizeof(buf)-1);
160
161         return __lnstat_scan_fields(lf, buf);
162 }
163
164 /* find out whether string 'name; is in given string array */
165 static int name_in_array(const int num, const char **arr, const char *name)
166 {
167         int i;
168         for (i = 0; i < num; i++) {
169                 if (!strcmp(arr[i], name))
170                         return 1;
171         }
172         return 0;
173 }
174
175 /* allocate lnstat_file and open given file */
176 static struct lnstat_file *alloc_and_open(const char *path, const char *file)
177 {
178         struct lnstat_file *lf;
179
180         /* allocate */
181         lf = malloc(sizeof(*lf));
182         if (!lf)
183                 return NULL;
184
185         /* initialize */
186         memset(lf, 0, sizeof(*lf));
187
188         /* de->d_name is guaranteed to be <= NAME_MAX */
189         strcpy(lf->basename, file);
190         strcpy(lf->path, path);
191         strcat(lf->path, "/");
192         strcat(lf->path, lf->basename);
193
194         /* initialize to default */
195         lf->interval.tv_sec = 1;
196
197         /* open */
198         lf->fp = fopen(lf->path, "r");
199         if (!lf->fp) {
200                 free(lf);
201                 return NULL;
202         }
203
204         return lf;
205 }
206
207
208 /* lnstat_scan_dir - find and parse all available statistics files/fields */
209 struct lnstat_file *lnstat_scan_dir(const char *path, const int num_req_files,
210                                     const char **req_files)
211 {
212         DIR *dir;
213         struct lnstat_file *lnstat_files = NULL;
214         struct dirent *de;
215
216         if (!path)
217                 path = PROC_NET_STAT;
218
219         dir = opendir(path);
220         if (!dir) {
221                 struct lnstat_file *lf;
222                 /* Old kernel, before /proc/net/stat was introduced */
223                 fprintf(stderr, "Your kernel doesn't have lnstat support. ");
224
225                 /* we only support rtstat, not multiple files */
226                 if (num_req_files >= 2) {
227                         fputc('\n', stderr);
228                         return NULL;
229                 }
230
231                 /* we really only accept rt_cache */
232                 if (num_req_files && !name_in_array(num_req_files,
233                                                     req_files, "rt_cache")) {
234                         fputc('\n', stderr);
235                         return NULL;
236                 }
237
238                 fprintf(stderr, "Fallback to old rtstat-only operation\n");
239
240                 lf = alloc_and_open("/proc/net", "rt_cache_stat");
241                 if (!lf)
242                         return NULL;
243                 lf->compat = 1;
244                 strncpy(lf->basename, "rt_cache", sizeof(lf->basename));
245
246                 /* FIXME: support for old files */
247                 if (lnstat_scan_compat_rtstat_fields(lf) < 0)
248                         return NULL;
249
250                 lf->next = lnstat_files;
251                 lnstat_files = lf;
252                 return lnstat_files;
253         }
254
255         while ((de = readdir(dir))) {
256                 struct lnstat_file *lf;
257
258                 if (de->d_type != DT_REG)
259                         continue;
260
261                 if (num_req_files && !name_in_array(num_req_files,
262                                                     req_files, de->d_name))
263                         continue;
264
265                 lf = alloc_and_open(path, de->d_name);
266                 if (!lf)
267                         return NULL;
268
269                 /* fill in field structure */
270                 if (lnstat_scan_fields(lf) < 0)
271                         return NULL;
272
273                 /* prepend to global list */
274                 lf->next = lnstat_files;
275                 lnstat_files = lf;
276         }
277         closedir(dir);
278
279         return lnstat_files;
280 }
281
282 int lnstat_dump(FILE *outfd, struct lnstat_file *lnstat_files)
283 {
284         struct lnstat_file *lf;
285
286         for (lf = lnstat_files; lf; lf = lf->next) {
287                 int i;
288
289                 fprintf(outfd, "%s:\n", lf->path);
290
291                 for (i = 0; i < lf->num_fields; i++)
292                         fprintf(outfd, "\t%2u: %s\n", i+1, lf->fields[i].name);
293
294         }
295         return 0;
296 }
297
298 struct lnstat_field *lnstat_find_field(struct lnstat_file *lnstat_files,
299                                        const char *name)
300 {
301         struct lnstat_file *lf;
302         struct lnstat_field *ret = NULL;
303         const char *colon = strchr(name, ':');
304         char *file;
305         const char *field;
306
307         if (colon) {
308                 file = strndup(name, colon-name);
309                 field = colon+1;
310         } else {
311                 file = NULL;
312                 field = name;
313         }
314
315         for (lf = lnstat_files; lf; lf = lf->next) {
316                 int i;
317
318                 if (file && strcmp(file, lf->basename))
319                         continue;
320
321                 for (i = 0; i < lf->num_fields; i++) {
322                         if (!strcmp(field, lf->fields[i].name)) {
323                                 ret = &lf->fields[i];
324                                 goto out;
325                         }
326                 }
327         }
328 out:
329         if (file)
330                 free(file);
331
332         return ret;
333 }