11 #include <sys/types.h>
16 #include <dynamic_config.h>
17 #include <logcommon.h>
18 #include <loglimiter.h>
20 #define DYNAMIC_CONF "/tmp/dlog-filters/" DYNAMIC_CONFIG_FILENAME
21 #define DYNAMIC_CONF_ALTER "/tmp/" DYNAMIC_CONFIG_FILENAME "_ALTER" // Weird but ok
22 #define BUF_LEN (MAX_CONF_KEY_LEN + MAX_CONF_VAL_LEN + 3) // +3 because KEY=VAL\n\0
24 #define FILTER_ALLOW (__LOG_LIMITER_LIMIT_MAX + 1)
27 // limit values same as in loglimiter
28 int set_config(const char *filename, int flag, char prio, const char *tag, int limit)
31 assert(flag == O_APPEND || flag == O_TRUNC);
34 const int flags = O_CREAT | O_WRONLY | flag;
38 __attribute__((cleanup(close_fd))) const int fd = open(filename, flags, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
41 perror("error opening dynamic conf file");
45 if (limit == FILTER_ALLOW)
46 r = snprintf(buf, sizeof buf, "limiter|%s|%c=allow\n", tag, prio);
47 else if (limit == FILTER_DENY)
48 r = snprintf(buf, sizeof buf, "limiter|%s|%c=deny\n", tag, prio);
50 r = snprintf(buf, sizeof buf, "limiter|%s|%c=%d\n", tag, prio, limit);
53 perror("snprintf error");
57 s = write(fd, buf, r);
60 perror("error writing config file");
67 int erase_file(const char *filename)
69 const int fd = open(filename, O_RDWR|O_TRUNC);
72 perror("error opening dynamic conf file");
81 int main(int argc, char **argv)
83 // test allow all filter
84 if (set_config(DYNAMIC_CONF, O_TRUNC, '*', "*", FILTER_ALLOW) < 0)
86 for (int i = 0; i < 3; i++)
87 RLOGE("[1] ALLOWED message %d\n", i);
89 // test deny all filter
90 if (set_config(DYNAMIC_CONF, O_TRUNC, '*', "*", FILTER_DENY) < 0)
92 for (int i = 0; i < 3; i++)
93 RLOGE("[2] DENIED message %d\n", i);
95 // still deny all, but allow by tag filter
96 if (set_config(DYNAMIC_CONF, O_APPEND, '*', "YYY", FILTER_ALLOW) < 0)
98 if (set_config(DYNAMIC_CONF_ALTER, O_TRUNC, '*', "ZZZ", FILTER_ALLOW) < 0)
101 for (int i = 0; i < 3; i++)
102 RLOG(LOG_ERROR, "YYY", "[3] ALLOWED message %d\n", i);
103 for (int i = 0; i < 3; i++)
104 RLOG(LOG_ERROR, "ZZZ", "[4] DENIED message %d\n", i);
105 for (int i = 0; i < 3; i++)
106 RLOGE("[5] DENIED message %d\n", i);
109 // test deny all but allow by priority filter
110 if (set_config(DYNAMIC_CONF, O_TRUNC, '*', "*", FILTER_DENY) < 0
111 || set_config(DYNAMIC_CONF, O_APPEND, 'W', "*", FILTER_ALLOW) < 0)
113 for (int i = 0; i < 3; i++)
114 RLOG(LOG_WARN, "ZZZ", "[6] ALLOWED message %d\n", i);
115 for (int i = 0; i < 3; i++)
116 RLOG(LOG_DEBUG, "ZZZ", "[7] DENIED message %d\n", i);
118 // test time based limiter
119 static const int TIMED_LIMIT = 2;
120 if (set_config(DYNAMIC_CONF, O_TRUNC, '*', "*", TIMED_LIMIT) < 0)
122 for (int i = 0; i < 10; i++)
123 RLOG(LOG_DEBUG, "AAA", "[8] %s message %d/%d\n", i < TIMED_LIMIT ? "ALLOWED" : "DENIED", i + 1, TIMED_LIMIT);
125 if (erase_file(DYNAMIC_CONF_ALTER) < 0)
127 if (erase_file(DYNAMIC_CONF) < 0)