rules_destroy(¤t_rules_table);
current_rules_table = rules_table;
}
+
+/**
+ * @brief Dump a limiter rule to provided buffer
+ * @details Formats a string containing information about a rule and inserts it into provided buffer
+ * @param[in,out] r The pointer to the rule to be dumped (in), the pointer to the previous rule (out)
+ * @param[in,out] buf The buffer to dump the rule into
+ * @param[in] size The size of the buffer
+ * @returns 0 on success, -1 on failure
+ */
+int __log_limiter_dump_rule(struct rule **r, char *buf, const size_t size)
+{
+ struct rule *ruleptr = *r ? : current_rules_table;
+ int s = 0;
+
+ if (!ruleptr) {
+ *r = NULL;
+ return 0;
+ }
+
+ if (ruleptr->limit == 0)
+ s = snprintf(buf, size, "Deny ");
+ else if (ruleptr->limit == (__LOG_LIMITER_LIMIT_MAX + 1))
+ s = snprintf(buf, size, "Allow ");
+ else
+ s = snprintf(buf, size, "%d logs/minute ", ruleptr->limit);
+
+ if (s < 0)
+ return -1;
+
+ s = snprintf(buf + s, size - s, "for %s:%c",
+ ruleptr->tag,
+ util_prio_to_char(ruleptr->prio));
+ if (s < 0)
+ return -1;
+
+ *r = ruleptr->prev;
+ return 0;
+}
ACTION_NONE = 0,
ACTION_GET,
ACTION_SET,
+ ACTION_DUMP,
} action;
const char *tag;
char prio;
params->tag = "*";
params->prio = '*';
+ bool dump = true;
for (;;) {
int opt = getopt(argc, (char **) argv, ":hgs:t:p:");
break;
} case 't': {
params->tag = optarg;
+ dump = false;
break;
} case 'p':
params->prio = toupper(*optarg);
if (!strchr("DVIWEFS*", params->prio))
goto failure;
+ dump = false;
break;
case ':':
if (optopt == 's')
goto print_help;
}
+ if (params->action == ACTION_GET && dump)
+ params->action = ACTION_DUMP;
+
return true;
failure:
return EXIT_SUCCESS;
}
+int handle_dump(const struct parsed_params *params, char *filters_path, struct log_config *conf)
+{
+ if (!__log_limiter_create(conf)) {
+ ERR("error creating limiter\n");
+ return EXIT_FAILURE;
+ }
+ char buf[1024];
+
+ log_config_read_file(conf, filters_path); // not an error on failure - config still valid if missing, static rules apply
+ __log_limiter_update(conf);
+
+ struct rule *r = NULL;
+ do {
+ int ret = __log_limiter_dump_rule(&r, buf, sizeof buf);
+ if (ret < 0) {
+ ERR("Error dumping rule\n");
+ return EXIT_FAILURE;
+ }
+ printf("%s\n", buf);
+ } while (r);
+
+ __log_limiter_destroy();
+ return EXIT_SUCCESS;
+}
+
+
int main(int argc, const char **argv)
{
int exit_code = EXIT_FAILURE;
int (*handles[])(const struct parsed_params *, char *, struct log_config *) = {
[ACTION_GET] = handle_get,
[ACTION_SET] = handle_set,
+ [ACTION_DUMP] = handle_dump,
};
exit_code = handles[params.action](¶ms, full_inotify_path, &conf);