summaryrefslogtreecommitdiff
path: root/src/libdlog/loglimiter.c
blob: a0e874e45aaa36d2fab34a3c29e6d6100d379804 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
 * DLOG
 * Copyright (c) 2013 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stddef.h>
#include <limits.h>
#include <sys/types.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <pthread.h>
#include <sys/time.h>
#include <time.h>

/* Included for priorities level */
#include <dlog.h>
#include "loglimiter.h"

/* Defines maximal meaningful tag length */
#define TAG_REASONABLE_LEN        32

/* Some random big odd number to make hash more diverse */
#define HASH_MAGIC_THINGY         5237231

#define TIME_FRAME                60

struct rule{
	/* TODO: List element handle, the list could be embedded
	         into structure some day, like kernel lists */
	struct rule* prev;

	unsigned hash;
	int prio;
	int limit;
	int hit;
	time_t start;
	char tag[TAG_REASONABLE_LEN];
};

typedef int (*hash_cmp_func_t)(struct rule*, struct rule*);
typedef int (*hash_match_func_t)(struct rule*, unsigned, const char*, int);

struct hashmap {
	hash_cmp_func_t cmp;
	hash_match_func_t match;
	int size;
	void* bucket[];
};

struct hashmap* rules_hashmap = NULL;

/* Keep rules table as single-linked list */
struct rule* rules_table = NULL;

#define HASHMAP_MASK(hm)          ((int)(hm->size - 1))
#define HASHMAP_LINEAR_PROBE_LEAP 1

static void rules_destroy(struct rule* rlist)
{
	struct rule* r;

	if (NULL == rlist) {
		return;
	}

	while ((r = rlist)) {
		rlist = rlist->prev;
		free(r);
	}
}

void __log_limiter_rules_purge(void)
{
	rules_destroy(rules_table);
	rules_table = NULL;
}

static int rule_compare(struct rule* r1, struct rule* r2)
{
	if (r1->hash == r2->hash) {
		if (r1->prio == r2->prio) {
			return strncmp(r1->tag, r2->tag, strlen(r2->tag));
		} else {
			return (r1->prio > r2->prio ? 1 : (-1));
		}
	}

	return (r1->hash > r2->hash ? 1 : (-1));
}

static int rule_match(struct rule* r1, unsigned key, const char* s, int prio)
{
	if (r1->hash == key) {
		if (r1->prio == prio) {
			return strncmp(r1->tag, s, strlen(s));
		} else {
			return (r1->prio > prio ? 1 : (-1));
		}
	}

	return (r1->hash > key ? 1 : (-1));
}

/* Translate fancy priority notation into common denominator */
static int util_prio_to_char(int prio)
{
	static const char pri_table[DLOG_PRIO_MAX] = {
		[DLOG_VERBOSE] = 'V',
		[DLOG_DEBUG] = 'D',
		[DLOG_INFO] = 'I',
		[DLOG_WARN] = 'W',
		[DLOG_ERROR] = 'E',
		[DLOG_FATAL] = 'F',
		[DLOG_SILENT] = 'S',
	};

	if (DLOG_PRIO_MAX > prio && prio >= 0) {
		return pri_table[prio];
	} else {
		switch (prio) {
			case 'V': case 'v': case '1':
			case 'D': case 'd': case '2':
			case 'I': case 'i': case '3':
			case 'W': case 'w': case '4':
			case 'E': case 'e': case '5':
			case 'F': case 'f': case '6':
			case 'S': case 's': case '7':
			case '*':
				return prio;

			default:
				;;
		}
	}

	return '?';
}

/* The key is built from TAG and priority by DJB algorithm (Dan Bernstein).
 * Key is only 31 bits long. Negative values are keep to hold NULL bucket */
static unsigned util_hash_key(const char* s, int c)
{
	unsigned hash = (unsigned)c;

	hash = ((hash << 5) + hash) + c;

	if (!s || !s[0]) {
		goto finish;
	}

	while ('\0' != (c = *s++)) {
		hash = ((hash << 5) + hash) + c;
	}

finish:
	/* Makes the hash more diverse */
	hash *= HASH_MAGIC_THINGY;

	return hash;
}

/* Create hashmap, it's internal interface. */
static struct hashmap* hashmap_create(int size, hash_cmp_func_t cmp_func,
				      hash_match_func_t match_func)
{
	struct hashmap* hm = NULL;
	/* please keep hashmap fill ratio around 50% */
	int internal_size = size << 1;

	if (!cmp_func || !match_func || !size) {
		return NULL;
	}


	/* Round up the lines counter to next power of two. */
	internal_size--;
	internal_size |= internal_size >> 1;
	internal_size |= internal_size >> 2;
	internal_size |= internal_size >> 4;
	internal_size |= internal_size >> 8;
	internal_size |= internal_size >> 16;
	internal_size++;

	hm = malloc(sizeof(struct hashmap) + internal_size * sizeof(void*));
	if (!hm) {
		return NULL;
	}
	/* Initialize hash field to correct value */
	memset((void*)hm, 0, sizeof(struct hashmap) + internal_size * sizeof(void*));

	hm->size = internal_size;
	hm->cmp = cmp_func;
	hm->match = match_func;

	return hm;
}

static void hashmap_destroy(struct hashmap* hm)
{
	if (hm) {
		hm->size = 0;
		free(hm);
	}
}

static void hashmap_add(struct hashmap* hm, struct rule* r)
{
	unsigned b = (r->hash & HASHMAP_MASK(hm));

	while (hm->bucket[b]) {
		if (!hm->cmp(r, (struct rule*)hm->bucket[b])) {
			break;
		}
		b = (b + 1) & HASHMAP_MASK(hm);
	}

	hm->bucket[b] = r;
}

static struct rule* hashmap_search(struct hashmap* hm, unsigned key,
				   const char* tag, int prio)
{
	unsigned b = (key & HASHMAP_MASK(hm));
	unsigned b0 = b;

	while (hm->bucket[b]) {
		if (!hm->match(hm->bucket[b], key, tag, prio)) {
			break;
		}

		b = (b + 1) & HASHMAP_MASK(hm);

		if (b0 == b) {
			return NULL;
		}
	}

	if (!hm->bucket[b]) {
		return NULL;
	}

	return hm->bucket[b];
}

/* Must be always executed after __log_config_read() */
int __log_limiter_initialize(void)
{
	int hm_size;
	struct rule* rlist = NULL;

	/* logconfig.c module had to initialize this correctly */
	if (NULL == rules_table) {
		return (-1);
	}

	/* Count rules in the table */
	hm_size = 0;
	rlist = rules_table;
	while (rlist) {
		hm_size++;
		rlist = rlist->prev;
	}

	/* Allocate hashmap */
	rules_hashmap = (struct hashmap*) hashmap_create(hm_size,
	                                                 &rule_compare,
	                                                 &rule_match);
	if (NULL == rules_hashmap || !rules_hashmap->size) {
		goto bailout;
	}

	/* Add rule to hashmap */
	rlist = rules_table;
	while (rlist) {
		hashmap_add(rules_hashmap, rlist);
		rlist = rlist->prev;
	}

	return 0;

bailout:
	hashmap_destroy(rules_hashmap);
	rules_destroy(rules_table);
	rules_table = NULL;
	rules_hashmap = NULL;

	return (-1);
}

void __log_limiter_destroy(void)
{
	hashmap_destroy(rules_hashmap);
	rules_destroy(rules_table);
	rules_table = NULL;
	rules_hashmap = NULL;
}

int __log_limiter_add_rule(const char* tag, int prio, int limit)
{
	struct rule* r;

	if (!tag) {
		return (-1);
	}

	r = (struct rule*) malloc(sizeof(struct rule));
	if (NULL == r) {
		return (-1);
	}
	memset(r, 0, sizeof(struct rule));

	snprintf(r->tag, TAG_REASONABLE_LEN, "%s", tag);
	r->prio = util_prio_to_char(prio);
	r->hash = util_hash_key(tag, r->prio);
	r->limit = limit;
	r->start = time(NULL);
	r->hit = 0;

	r->prev = rules_table;
	rules_table = r;

	return 0;
}


/* Function implement logic needed to decide,
   whenever message is written to log or not.

   Possible return values are:
	0  - to indicate that message is deny to write into log.
	(-1) - to indicate that limit of the messages is reached.
	1  - to indicate that message is allowed to write into log.
*/
int __log_limiter_pass_log(const char* tag, int prio)
{
	unsigned key = 0;
	struct rule* r = NULL;
	time_t now = 0;

	key = util_hash_key(tag, util_prio_to_char(prio));
	r = hashmap_search(rules_hashmap, key, tag, util_prio_to_char(prio));

	if (!r) {
		/* Rule not found, let's check general rule TAG:* */
		key = util_hash_key(tag, '*');
		r = hashmap_search(rules_hashmap, key, tag, '*');
		if (!r) {
			/* Rule TAG:* not found,
			   let check general rule *:priority */
			key = util_hash_key("*", util_prio_to_char(prio));
			r = hashmap_search(rules_hashmap, key, "*",
			                               util_prio_to_char(prio));
			if (!r) {
				/* All known paths were exhausted,
				   use global rule *:* */
				key = util_hash_key("*", '*');
				r = hashmap_search(rules_hashmap, key, "*", '*');

				/* *:* is not defined, so pass message through */
				if (!r) {
					return 1;
				}
			}
		}
	}

	if (!r->limit) {
		return 0;
	} else if (__LOG_LIMITER_LIMIT_MAX < r->limit) {
		return 1;
	}

	/* Decide, if it should go through or stop */
	now = time(NULL);

	if (0 > now) {
		return 1;
	}

	if (now - r->start <= TIME_FRAME) {
		if (r->hit >= 0) {
			if (r->hit < r->limit) {
				r->hit++;
				return 1;
			}
			r->hit = INT_MIN+1;
			return (-1);
		} else {
			r->hit++;
			return 0;
		}

	} else {
		r->start = now;
		r->hit = 0;
		return 1;
	}

	/* If everything failed, then pass message through */
	return 1;
}